From 090cf713f37939ad73151620255cd5c1162f2754 Mon Sep 17 00:00:00 2001 From: Caleb Xu Date: Sat, 14 Aug 2021 18:33:05 -0400 Subject: [PATCH 1/3] Support additional CMake build options - Add option BUILD_TESTS. Default to ON (following existing behavior) - Add option BUILD_README_HELPER. Default to ON (following existing behavior). - Add option BUILD_SHARED_LIBS. Default to OFF (following existing behavior). - Add option USE_EXTERNAL_JSON. Default to OFF (following existing behavior). BUILD_TESTS and BUILD_README_HELPER toggle whether contents in subdirectories "test" and "readme-helper" will be built. BUILD_SHARED_LIBS toggles whether libsdptransform will be built as a static or shared library. add_library automatically checks the value of BUILD_SHARED_LIBS at configure/build time. USE_EXTERNAL_JSON toggles whether an external (e.g. system) copy of nlohmann-json should be used instead of the bundled one. If enabled, CMake will automatically search for an installation of nlohmann-json on the system and configure step will fail if not found. If nlohmann-json is installed in a non-standard location, CMAKE_MODULE_PATH can be set so that CMake can find the nlohmann-json installation. json.hpp has been moved from include/ to new folder thirdparty/, so that the thirdparty folder can be added as an include directory when external nlohmann-json is not in use. include/sdptransform.hpp is now include/sdptransform.hpp.in. At configure time, the .hpp.in file is preprocessed into the build tree. This step is necessary in order to set the #include path to json.hpp in sdptransform.hpp. --- CMakeLists.txt | 38 ++++++++++++++----- .../{sdptransform.hpp => sdptransform.hpp.in} | 2 +- {include => thirdparty}/json.hpp | 0 3 files changed, 30 insertions(+), 10 deletions(-) rename include/{sdptransform.hpp => sdptransform.hpp.in} (96%) rename {include => thirdparty}/json.hpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index b4f4648..8b9c787 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,10 +9,6 @@ set(CMAKE_CXX_EXTENSIONS OFF) # For CMake < 3.1. add_compile_options(-std=c++11) -subdirs(test readme-helper) - -include_directories(${sdptransform_SOURCE_DIR}/include) - set( SOURCE_FILES src/grammar.cpp @@ -22,11 +18,35 @@ set( set( HEADER_FILES - include/sdptransform.hpp - include/json.hpp + ${sdptransform_BINARY_DIR}/include/sdptransform.hpp ) -add_library(sdptransform STATIC ${SOURCE_FILES} ${HEADER_FILES}) - -install(TARGETS sdptransform DESTINATION lib) +option(BUILD_TESTS "Build tests" ON) +if(BUILD_TESTS) + add_subdirectory(test) +endif() + +option(BUILD_README_HELPER "Build readme-helper" ON) +if(BUILD_README_HELPER) + add_subdirectory(readme-helper) +endif() + +option(BUILD_SHARED_LIBS "Build shared library" OFF) +add_library(sdptransform ${SOURCE_FILES}) + +target_include_directories(sdptransform PUBLIC ${sdptransform_BINARY_DIR}/include) + +option(USE_EXTERNAL_JSON "Use external nlohmann_json instead of bundled" OFF) +if(USE_EXTERNAL_JSON) + find_package(nlohmann_json REQUIRED) + target_link_libraries(sdptransform nlohmann_json::nlohmann_json) + set(JSON_HPP_LOCATION "") +else() + target_include_directories(sdptransform PUBLIC thirdparty) + list(APPEND HEADER_FILES thirdparty/json.hpp) + set(JSON_HPP_LOCATION "\"json.hpp\"") +endif() +configure_file(include/sdptransform.hpp.in include/sdptransform.hpp) + +install(TARGETS sdptransform) install(FILES ${HEADER_FILES} DESTINATION include/sdptransform) diff --git a/include/sdptransform.hpp b/include/sdptransform.hpp.in similarity index 96% rename from include/sdptransform.hpp rename to include/sdptransform.hpp.in index cdf6828..5fd2296 100644 --- a/include/sdptransform.hpp +++ b/include/sdptransform.hpp.in @@ -1,7 +1,7 @@ #ifndef SDPTRANSFORM_HPP #define SDPTRANSFORM_HPP -#include "json.hpp" +#include @JSON_HPP_LOCATION@ #include #include #include diff --git a/include/json.hpp b/thirdparty/json.hpp similarity index 100% rename from include/json.hpp rename to thirdparty/json.hpp From 53fb3b1d5205473d9adaa8acf50a0fa34cd4b468 Mon Sep 17 00:00:00 2001 From: Caleb Xu Date: Sat, 14 Aug 2021 18:45:18 -0400 Subject: [PATCH 2/3] readme-helper: use/inherit target-specific includes With changes in top-level CMakeLists.txt logic, sdptransform's includes are automatically added since readme-helper links to the sdptransform target. --- readme-helper/CMakeLists.txt | 2 -- 1 file changed, 2 deletions(-) diff --git a/readme-helper/CMakeLists.txt b/readme-helper/CMakeLists.txt index 97d75c0..d5e45c9 100644 --- a/readme-helper/CMakeLists.txt +++ b/readme-helper/CMakeLists.txt @@ -1,5 +1,3 @@ -include_directories(${sdptransform_SOURCE_DIR}/include) - set( SOURCE_FILES readme.cpp From e8ac0d2af3f0f61f1ebf2531b89133f8d624fe79 Mon Sep 17 00:00:00 2001 From: Caleb Xu Date: Sat, 14 Aug 2021 18:46:50 -0400 Subject: [PATCH 3/3] test: use/inherit target-specific includes With changes in top-level CMakeLists.txt logic, sdptransform's includes are automatically added since test links to the sdptransform target. --- test/CMakeLists.txt | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 239e0f7..7c9dd28 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,3 @@ -include_directories(${sdptransform_SOURCE_DIR}/include) -include_directories(include) - set( SOURCE_FILES tests.cpp @@ -14,6 +11,7 @@ set( ) add_executable(test_sdptransform ${SOURCE_FILES} ${HEADER_FILES}) +target_include_directories(test_sdptransform PRIVATE include) if(${CMAKE_SYSTEM_NAME} STREQUAL "Android") target_link_libraries(test_sdptransform sdptransform android log)