From 5c75096fe5957b68db7638de9a48e96e983cc7c4 Mon Sep 17 00:00:00 2001 From: selimnairb Date: Mon, 11 Sep 2023 13:30:28 -0400 Subject: [PATCH] CMake: Add ability to do standalone build examples --- CMakeLists.txt | 10 ++-- CMakeModules/FindBAG.cmake | 107 +++++++++++++++++++++++++++++++++++++ docs/BUILDING.md | 25 +++++++-- examples/CMakeLists.txt | 28 +++++++--- 4 files changed, 156 insertions(+), 14 deletions(-) create mode 100644 CMakeModules/FindBAG.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b9a094b170..95dbd7442b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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() @@ -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() diff --git a/CMakeModules/FindBAG.cmake b/CMakeModules/FindBAG.cmake new file mode 100644 index 0000000000..cec60a23c1 --- /dev/null +++ b/CMakeModules/FindBAG.cmake @@ -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 +) diff --git a/docs/BUILDING.md b/docs/BUILDING.md index 35895febf1..c5876b16d9 100644 --- a/docs/BUILDING.md +++ b/docs/BUILDING.md @@ -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 @@ -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 diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 98f195687b..3df05eedbe 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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 #----------------------------------------------------------------------------- @@ -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 $<$:_USE_MATH_DEFINES> @@ -20,13 +32,15 @@ foreach(example ${examples}) $<$,$>:BAG_DLL> ) - target_compile_options(baglib - PRIVATE - $<$,$,$,19.10>>:/permissive- /W4 /WX> - $<$,$,$,19.13>>:/experimental:external /external:W0 /external:templates-> - $<$,$,$,19.13>>:/external:anglebrackets> - $<$,$,$,19.14>>:/Zc:__cplusplus> - ) + if(BAG_BUILD_BAG_LIB) + target_compile_options(baglib + PRIVATE + $<$,$,$,19.10>>:/permissive- /W4 /WX> + $<$,$,$,19.13>>:/experimental:external /external:W0 /external:templates-> + $<$,$,$,19.13>>:/external:anglebrackets> + $<$,$,$,19.14>>:/Zc:__cplusplus> + ) + endif() target_link_libraries(${example} PRIVATE