Add options to link against external JSON libraries (#91)

Add options to link against external JSON libraries

This adds two options to the CMake config:

`CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE` and `CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE`
If either of these is set, instead of using the json submodule, the build will be configured by using `find_package()` and linking against the specified JSON library.
This commit is contained in:
nikitalita 2023-01-10 05:35:23 -08:00 committed by GitHub
parent a4bc977343
commit 0a340c6d71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 21 deletions

View File

@ -15,7 +15,7 @@
cmake_policy(SET CMP0048 NEW) cmake_policy(SET CMP0048 NEW)
project(cppdap VERSION 1.58.0 LANGUAGES CXX C) project(cppdap VERSION 1.58.0 LANGUAGES CXX C)
cmake_minimum_required(VERSION 3.11) cmake_minimum_required(VERSION 3.13)
set (CMAKE_CXX_STANDARD 11) set (CMAKE_CXX_STANDARD 11)
@ -38,6 +38,8 @@ option_if_not_defined(CPPDAP_ASAN "Build dap with address sanitizer" OFF)
option_if_not_defined(CPPDAP_MSAN "Build dap with memory sanitizer" OFF) option_if_not_defined(CPPDAP_MSAN "Build dap with memory sanitizer" OFF)
option_if_not_defined(CPPDAP_TSAN "Build dap with thread sanitizer" OFF) option_if_not_defined(CPPDAP_TSAN "Build dap with thread sanitizer" OFF)
option_if_not_defined(CPPDAP_INSTALL_VSCODE_EXAMPLES "Build and install dap examples into vscode extensions directory" OFF) option_if_not_defined(CPPDAP_INSTALL_VSCODE_EXAMPLES "Build and install dap examples into vscode extensions directory" OFF)
option_if_not_defined(CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE "Use nlohmann_json with find_package() instead of building internal submodule" OFF)
option_if_not_defined(CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE "Use RapidJSON with find_package()" OFF)
########################################################### ###########################################################
# Directories # Directories
@ -68,6 +70,17 @@ endif(CPPDAP_BUILD_TESTS)
########################################################### ###########################################################
# JSON library # JSON library
########################################################### ###########################################################
if(CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE AND CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE)
message(FATAL_ERROR "Don't set both CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE and CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE")
elseif(CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE)
find_package(nlohmann_json CONFIG REQUIRED)
set(CPPDAP_JSON_LIBRARY "nlohmann")
elseif(CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE)
find_package(RapidJSON CONFIG REQUIRED)
set(CPPDAP_JSON_LIBRARY "rapid")
endif()
if(NOT DEFINED CPPDAP_JSON_LIBRARY) if(NOT DEFINED CPPDAP_JSON_LIBRARY)
# Attempt to detect JSON library from CPPDAP_JSON_DIR # Attempt to detect JSON library from CPPDAP_JSON_DIR
if(NOT EXISTS "${CPPDAP_JSON_DIR}") if(NOT EXISTS "${CPPDAP_JSON_DIR}")
@ -79,7 +92,7 @@ if(NOT DEFINED CPPDAP_JSON_LIBRARY)
elseif(EXISTS "${CPPDAP_JSON_DIR}/include/rapidjson") elseif(EXISTS "${CPPDAP_JSON_DIR}/include/rapidjson")
set(CPPDAP_JSON_LIBRARY "rapid") set(CPPDAP_JSON_LIBRARY "rapid")
else() else()
message(FATAL_ERROR "Could not determine JSON library from ${CPPDAP_JSON_LIBRARY}") message(FATAL_ERROR "Could not determine JSON library from ${CPPDAP_JSON_DIR}")
endif() endif()
endif() endif()
string(TOUPPER ${CPPDAP_JSON_LIBRARY} CPPDAP_JSON_LIBRARY_UPPER) string(TOUPPER ${CPPDAP_JSON_LIBRARY} CPPDAP_JSON_LIBRARY_UPPER)
@ -120,6 +133,17 @@ endif()
########################################################### ###########################################################
# Functions # Functions
########################################################### ###########################################################
function(cppdap_set_json_links target)
if (CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE)
target_link_libraries(${target} PRIVATE nlohmann_json nlohmann_json::nlohmann_json)
elseif(CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE)
target_link_libraries(${target} PRIVATE rapidjson)
else()
target_include_directories(${target} PRIVATE "${CPPDAP_JSON_DIR}/include/")
endif()
endfunction(cppdap_set_json_links)
function(cppdap_set_target_options target) function(cppdap_set_target_options target)
# Enable all warnings # Enable all warnings
if(MSVC) if(MSVC)
@ -139,8 +163,8 @@ function(cppdap_set_target_options target)
endif() endif()
# Add define for JSON library in use # Add define for JSON library in use
set_target_properties(${target} PROPERTIES target_compile_definitions(${target}
COMPILE_DEFINITIONS "CPPDAP_JSON_${CPPDAP_JSON_LIBRARY_UPPER}=1" PUBLIC "CPPDAP_JSON_${CPPDAP_JSON_LIBRARY_UPPER}=1"
) )
# Treat all warnings as errors # Treat all warnings as errors
@ -154,13 +178,13 @@ function(cppdap_set_target_options target)
if(CPPDAP_ASAN) if(CPPDAP_ASAN)
target_compile_options(${target} PUBLIC "-fsanitize=address") target_compile_options(${target} PUBLIC "-fsanitize=address")
target_link_libraries(${target} "-fsanitize=address") target_link_options(${target} PUBLIC "-fsanitize=address")
elseif(CPPDAP_MSAN) elseif(CPPDAP_MSAN)
target_compile_options(${target} PUBLIC "-fsanitize=memory") target_compile_options(${target} PUBLIC "-fsanitize=memory")
target_link_libraries(${target} "-fsanitize=memory") target_link_options(${target} PUBLIC "-fsanitize=memory")
elseif(CPPDAP_TSAN) elseif(CPPDAP_TSAN)
target_compile_options(${target} PUBLIC "-fsanitize=thread") target_compile_options(${target} PUBLIC "-fsanitize=thread")
target_link_libraries(${target} "-fsanitize=thread") target_link_options(${target} PUBLIC "-fsanitize=thread")
endif() endif()
# Error on undefined symbols # Error on undefined symbols
@ -173,6 +197,8 @@ function(cppdap_set_target_options target)
"$<BUILD_INTERFACE:${CPPDAP_INCLUDE_DIR}>" "$<BUILD_INTERFACE:${CPPDAP_INCLUDE_DIR}>"
"$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>" "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>"
) )
cppdap_set_json_links(${target})
target_link_libraries(cppdap PRIVATE ${CPPDAP_OS_LIBS})
endfunction(cppdap_set_target_options) endfunction(cppdap_set_target_options)
########################################################### ###########################################################
@ -183,12 +209,8 @@ endfunction(cppdap_set_target_options)
add_library(cppdap STATIC ${CPPDAP_LIST}) add_library(cppdap STATIC ${CPPDAP_LIST})
set_target_properties(cppdap PROPERTIES POSITION_INDEPENDENT_CODE 1) set_target_properties(cppdap PROPERTIES POSITION_INDEPENDENT_CODE 1)
target_include_directories(cppdap PRIVATE "${CPPDAP_JSON_DIR}/include/")
cppdap_set_target_options(cppdap) cppdap_set_target_options(cppdap)
target_link_libraries(cppdap "${CPPDAP_OS_LIBS}")
set(CPPDAP_TARGET_NAME ${PROJECT_NAME}) set(CPPDAP_TARGET_NAME ${PROJECT_NAME})
set(CPPDAP_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "") set(CPPDAP_CONFIG_INSTALL_DIR "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" CACHE INTERNAL "")
set(CPPDAP_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}") set(CPPDAP_INCLUDE_INSTALL_DIR "${CMAKE_INSTALL_INCLUDEDIR}")
@ -254,13 +276,12 @@ if(CPPDAP_BUILD_TESTS)
${CPPDAP_GOOGLETEST_DIR}/googlemock/include/ ${CPPDAP_GOOGLETEST_DIR}/googlemock/include/
${CPPDAP_GOOGLETEST_DIR}/googletest/ ${CPPDAP_GOOGLETEST_DIR}/googletest/
${CPPDAP_GOOGLETEST_DIR}/googletest/include/ ${CPPDAP_GOOGLETEST_DIR}/googletest/include/
${CPPDAP_JSON_DIR}/include/
) )
add_executable(cppdap-unittests ${DAP_TEST_LIST}) add_executable(cppdap-unittests ${DAP_TEST_LIST})
target_include_directories(cppdap-unittests PUBLIC ${DAP_TEST_INCLUDE_DIR} )
set_target_properties(cppdap-unittests PROPERTIES set_target_properties(cppdap-unittests PROPERTIES
INCLUDE_DIRECTORIES "${DAP_TEST_INCLUDE_DIR}"
FOLDER "Tests" FOLDER "Tests"
) )
@ -270,8 +291,7 @@ if(CPPDAP_BUILD_TESTS)
endif() endif()
cppdap_set_target_options(cppdap-unittests) cppdap_set_target_options(cppdap-unittests)
target_link_libraries(cppdap-unittests PRIVATE cppdap)
target_link_libraries(cppdap-unittests cppdap "${CPPDAP_OS_LIBS}")
endif(CPPDAP_BUILD_TESTS) endif(CPPDAP_BUILD_TESTS)
# fuzzer # fuzzer
@ -286,23 +306,24 @@ if(CPPDAP_BUILD_FUZZER)
add_executable(cppdap-fuzzer ${DAP_FUZZER_LIST}) add_executable(cppdap-fuzzer ${DAP_FUZZER_LIST})
if(CPPDAP_ASAN) if(CPPDAP_ASAN)
target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,address") target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,address")
target_link_libraries(cppdap-fuzzer "-fsanitize=fuzzer,address") target_link_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,address")
elseif(CPPDAP_MSAN) elseif(CPPDAP_MSAN)
target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,memory") target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,memory")
target_link_libraries(cppdap-fuzzer "-fsanitize=fuzzer,memory") target_link_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,memory")
elseif(CPPDAP_TSAN) elseif(CPPDAP_TSAN)
target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,thread") target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,thread")
target_link_libraries(cppdap-fuzzer "-fsanitize=fuzzer,thread") target_link_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer,thread")
else() else()
target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer") target_compile_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer")
target_link_libraries(cppdap-fuzzer "-fsanitize=fuzzer") target_link_options(cppdap-fuzzer PUBLIC "-fsanitize=fuzzer")
endif() endif()
target_include_directories(cppdap-fuzzer PUBLIC target_include_directories(cppdap-fuzzer PUBLIC
${CPPDAP_INCLUDE_DIR} ${CPPDAP_INCLUDE_DIR}
${CPPDAP_SRC_DIR} ${CPPDAP_SRC_DIR}
${CPPDAP_JSON_DIR}/include/
) )
target_link_libraries(cppdap-fuzzer cppdap "${CPPDAP_OS_LIBS}") cppdap_set_json_links(cppdap-fuzzer)
target_link_libraries(cppdap-fuzzer PRIVATE cppdap "${CPPDAP_OS_LIBS}")
endif(CPPDAP_BUILD_FUZZER) endif(CPPDAP_BUILD_FUZZER)
# examples # examples

View File

@ -17,3 +17,9 @@ include(CMakeFindDependencyMacro)
include("${CMAKE_CURRENT_LIST_DIR}/@CPPDAP_TARGETS_EXPORT_NAME@.cmake") include("${CMAKE_CURRENT_LIST_DIR}/@CPPDAP_TARGETS_EXPORT_NAME@.cmake")
check_required_components("@CPPDAP_TARGET_NAME@") check_required_components("@CPPDAP_TARGET_NAME@")
if ( @CPPDAP_USE_EXTERNAL_NLOHMANN_JSON_PACKAGE@ )
find_dependency(nlohmann_json CONFIG)
elseif( @CPPDAP_USE_EXTERNAL_RAPIDJSON_PACKAGE@ )
find_dependency(RapidJSON CONFIG)
endif()