Skip to content

Commit

Permalink
CMake: Add ability to do standalone build examples
Browse files Browse the repository at this point in the history
  • Loading branch information
selimnairb committed Sep 11, 2023
1 parent 5c242dd commit 5c75096
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 14 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ if(WIN32)
set(CMAKE_OBJECT_PATH_MAX 1024)
endif()

# Local modules
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})

# Options
option(BAG_BUILD_BAG_LIB "Build baglib" ON)
option(BAG_BUILD_SHARED_LIBS "Build Shared Libraries" ON)
option(BAG_BUILD_PYTHON "Build Python bindings using SWIG" OFF)
option(BAG_BUILD_TESTS "Build Tests" OFF)
Expand Down Expand Up @@ -72,13 +76,14 @@ configure_file(${CMAKE_CURRENT_SOURCE_DIR}/api/swig/python/setup.py.in
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/api/swig/python/pyproject.toml
DESTINATION ${CMAKE_CURRENT_BINARY_DIR}/api/swig/python/)

if(BAG_BUILD_BAG_LIB)
add_subdirectory(api)
endif()

if(BAG_BUILD_PYTHON)
set(BAG_BUILD_SWIG ON)
endif()

add_subdirectory(api)

if(BAG_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
Expand All @@ -88,6 +93,5 @@ if(BAG_BUILD_TESTS)
endif()

if(BAG_BUILD_DOCS)
set(CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/CMakeModules" ${CMAKE_MODULE_PATH})
add_subdirectory(docs)
endif()
107 changes: 107 additions & 0 deletions CMakeModules/FindBAG.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# Distributed under the OSI-approved BSD 3-Clause License. See LICENSE
# file accompanying BAG source for details.

#[=======================================================================[.rst:
FindBAG
-------
Finds the BAG library.
Imported Targets
^^^^^^^^^^^^^^^^
This module provides the following imported targets, if found:
``BAG::BAG``
The baglib library
Result Variables
^^^^^^^^^^^^^^^^
This will define the following variables:
``BAG_FOUND``
True if the system has the BAG library.
``BAG_VERSION``
The version of the BAG library which was found.
``BAG_INCLUDE_DIRS``
Include directories needed to use BAG.
``BAG_LIBRARIES``
Libraries needed to link to BAG.
Cache Variables
^^^^^^^^^^^^^^^
The following cache variables may also be set:
``BAG_INCLUDE_DIR``
The directory containing ``bag_dataset.h``.
``BAG_LIBRARY``
The path to the BAG library.
#]=======================================================================]

find_package(PkgConfig)
pkg_check_modules(PC_BAG QUIET BAG)

find_path(BAG_INCLUDE_DIR
NAMES bag_dataset.h
PATHS ${PC_BAG_INCLUDE_DIRS}
PATH_SUFFIXES
include
include/bag
include/BAG
include/baglib
)
find_library(BAG_LIBRARY
NAMES baglib
PATHS ${PC_BAG_LIBRARY_DIRS}
)

set(BAG_VERSION ${PC_BAG_VERSION})

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(BAG
FOUND_VAR BAG_FOUND
REQUIRED_VARS
BAG_LIBRARY
BAG_INCLUDE_DIR
VERSION_VAR BAG_VERSION
)

#if(BAG_FOUND)
# if(NOT TARGET BAG::BAG)
# add_library(BAG::BAG UNKNOWN IMPORTED)
# endif()
# if(BAG_LIBRARY_RELEASE)
# set_property(TARGET BAG::BAG APPEND PROPERTY
# IMPORTED_CONFIGURATIONS RELEASE
# )
# set_target_properties(BAG::BAG PROPERTIES
# IMPORTED_LOCATION_RELEASE "${BAG_LIBRARY_RELEASE}"
# )
# endif()
# if(BAG_LIBRARY_DEBUG)
# set_property(TARGET BAG::BAG APPEND PROPERTY
# IMPORTED_CONFIGURATIONS DEBUG
# )
# set_target_properties(BAG::BAG PROPERTIES
# IMPORTED_LOCATION_DEBUG "${BAG_LIBRARY_DEBUG}"
# )
# endif()
# set_target_properties(BAG::BAG PROPERTIES
# INTERFACE_COMPILE_OPTIONS "${PC_BAG_CFLAGS_OTHER}"
# INTERFACE_INCLUDE_DIRECTORIES "${BAG_INCLUDE_DIR}"
# )
#endif()

if(BAG_FOUND)
set(BAG_LIBRARIES ${BAG_LIBRARY})
set(BAG_INCLUDE_DIRS ${BAG_INCLUDE_DIR})
set(BAG_DEFINITIONS ${PC_BAG_CFLAGS_OTHER})
endif()

mark_as_advanced(
BAG_INCLUDE_DIR
BAG_LIBRARY
)
25 changes: 21 additions & 4 deletions docs/BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

#### Build C++ library, tests, and example binaries:
```shell
cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -B build -S . \
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Debug -B build -S . \
-DBAG_BUILD_TESTS:BOOL=ON -DBAG_CODE_COVERAGE:BOOL=ON \
-DBAG_BUILD_PYTHON:BOOL=OFF -DBAG_BUILD_EXAMPLES:BOOL=ON && \
cmake --build build -j 8
BAG_SAMPLES_PATH=/ABSOLUTE/PATH/TO/REPO/BAG/examples/sample-data ninja -C build build ccov-all-export-lcov
-DBAG_BUILD_PYTHON:BOOL=OFF -DBAG_BUILD_EXAMPLES:BOOL=ON
$ cmake --build build -j 8
$ BAG_SAMPLES_PATH=/ABSOLUTE/PATH/TO/REPO/BAG/examples/sample-data ninja -C build build ccov-all-export-lcov
```

Where `/ABSOLUTE/PATH/TO/REPO/BAG/examples/sample-data` should be replaced by the absolute path
Expand Down Expand Up @@ -41,6 +41,23 @@ Then you can install the wheel with:
$ python -m pip install ./wheel/bagPy-*.whl
```

### Build examples only
Build examples only as follows:
```shell
$ cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -B bag-examples -S . \
-DBAG_BUILD_BAG_LIB:BOOL=OFF -DBAG_BUILD_EXAMPLES:BOOL=ON
$ cmake --build bag-examples -j 8
```

> Note: This assumes you have either built `baglib` yourself, or installed
> binaries via `conda`.
Then run, for example `bag_georefmetadata_layer`:
```shell
$ ./bag-examples/examples/bag_georefmetadata_layer \
examples/sample-data/bag_georefmetadata_layer.xml bag_georefmetadata.bag
```

## Windows: Visual Studio 2022/2019

### Dependencies from Miniconda
Expand Down
28 changes: 21 additions & 7 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
if(NOT BAG_BUILD_BAG_LIB)
# Find installed BAGLIB as we did not build it
find_package(BAG REQUIRED)
endif()

#-----------------------------------------------------------------------------
# Define Sources
#-----------------------------------------------------------------------------
Expand All @@ -12,6 +17,13 @@ set(examples
foreach(example ${examples})
add_executable(${example} ${example}.cpp getopt.c getopt.h)

if(NOT BAG_BUILD_BAG_LIB)
target_include_directories(${example}
PUBLIC
${BAG_INCLUDE_DIRS}
)
endif()

target_compile_definitions(${example}
PRIVATE
$<$<CXX_COMPILER_ID:MSVC>:_USE_MATH_DEFINES>
Expand All @@ -20,13 +32,15 @@ foreach(example ${examples})
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<BOOL:BAG_BUILD_SHARED_LIBS>>:BAG_DLL>
)

target_compile_options(baglib
PRIVATE
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.10>>:/permissive- /W4 /WX>
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.13>>:/experimental:external /external:W0 /external:templates->
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.13>>:/external:anglebrackets>
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.14>>:/Zc:__cplusplus>
)
if(BAG_BUILD_BAG_LIB)
target_compile_options(baglib
PRIVATE
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.10>>:/permissive- /W4 /WX>
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.13>>:/experimental:external /external:W0 /external:templates->
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.13>>:/external:anglebrackets>
$<$<AND:$<CXX_COMPILER_ID:MSVC>,$<COMPILE_LANGUAGE:CXX>,$<VERSION_GREATER_EQUAL:$<CXX_COMPILER_VERSION>,19.14>>:/Zc:__cplusplus>
)
endif()

target_link_libraries(${example}
PRIVATE
Expand Down

0 comments on commit 5c75096

Please sign in to comment.