@@ -4,6 +4,10 @@ project(devdat VERSION 1.0.0 LANGUAGES CXX)
44# Options
55option (BUILD_TESTING "Build tests" ON )
66option (DEVDAT_USE_SYSTEM_JSON "Use system-installed nlohmann_json" OFF )
7+ # Add this to recognize the variable passed from superbuild
8+ if (DEFINED USE_SYSTEM_JSON)
9+ set (DEVDAT_USE_SYSTEM_JSON ${USE_SYSTEM_JSON} )
10+ endif ()
711
812# Set C++ standard
913set (CMAKE_CXX_STANDARD 17)
@@ -24,21 +28,27 @@ endif()
2428
2529# Handle nlohmann_json dependency
2630if (DEVDAT_USE_SYSTEM_JSON)
27- # Check if JSON_INCLUDE_DIR is provided by the superbuild
31+ message (STATUS "Using system JSON (DEVDAT_USE_SYSTEM_JSON=${DEVDAT_USE_SYSTEM_JSON} )" )
32+
2833 if (DEFINED JSON_INCLUDE_DIR)
29- message (STATUS "Using JSON include directory provided by superbuild: ${JSON_INCLUDE_DIR} " )
30- # Simple interface library to handle the include directory
31- add_library (json_interface INTERFACE )
32- target_include_directories (json_interface INTERFACE ${JSON_INCLUDE_DIR} )
33- set (JSON_TARGET json_interface)
34+ message (STATUS "JSON_INCLUDE_DIR is set to: ${JSON_INCLUDE_DIR} " )
35+ # Create a wrapper target that will be included in the export set
36+ add_library (devdat_json_wrapper INTERFACE )
37+ target_include_directories (devdat_json_wrapper INTERFACE
38+ $<BUILD_INTERFACE:${JSON_INCLUDE_DIR} >
39+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
40+ )
41+ set (JSON_TARGET devdat_json_wrapper)
42+ set (EXPORT_JSON_TARGET TRUE )
3443 else ()
35- # Find system package
36- message (STATUS "Finding system nlohmann_json package" )
44+ # When using standard system package, handle dependency in config file
3745 find_package (nlohmann_json REQUIRED)
3846 set (JSON_TARGET nlohmann_json::nlohmann_json)
47+ # We'll handle this dependency in the config file
48+ set (EXPORT_JSON_TARGET FALSE )
3949 endif ()
4050else ()
41- # Fetch our own copy
51+ # Download nlohmann_json at configure time
4252 message (STATUS "Fetching nlohmann_json" )
4353 include (FetchContent)
4454 FetchContent_Declare(
@@ -47,10 +57,22 @@ else()
4757 GIT_TAG v3.11.2
4858 )
4959 FetchContent_MakeAvailable(nlohmann_json)
50- set (JSON_TARGET nlohmann_json::nlohmann_json)
5160
52- # Note: When not using system JSON, we'll need to ensure the headers are installed
53- set (INSTALL_JSON_HEADERS TRUE )
61+ # Create our own interface that will be included in the export set
62+ add_library (devdat_json_wrapper INTERFACE )
63+ target_include_directories (devdat_json_wrapper INTERFACE
64+ $<BUILD_INTERFACE:${nlohmann_json_SOURCE_DIR} /include >
65+ $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR} >
66+ )
67+
68+ # Install the headers
69+ install (
70+ DIRECTORY ${nlohmann_json_SOURCE_DIR} /include /
71+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
72+ )
73+
74+ set (JSON_TARGET devdat_json_wrapper)
75+ set (EXPORT_JSON_TARGET TRUE )
5476endif ()
5577
5678# Create the library (header-only)
@@ -78,35 +100,28 @@ add_library(${PROJECT_NAME}::devdat ALIAS devdat)
78100include (GNUInstallDirs)
79101set (INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR} /cmake/${PROJECT_NAME} )
80102
81- # Install the headers
103+ # Install headers
82104install (
83105 DIRECTORY include /
84106 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
85107)
86108
87- # If we fetched JSON and need to install the headers
88- if (DEFINED INSTALL_JSON_HEADERS)
109+ # Install targets - always install devdat
110+ if (EXPORT_JSON_TARGET)
111+ # If we created our own JSON wrapper, install both together
89112 install (
90- DIRECTORY ${nlohmann_json_SOURCE_DIR} / include /
91- DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
113+ TARGETS devdat devdat_json_wrapper
114+ EXPORT ${PROJECT_NAME} Targets
92115 )
93- endif ()
94-
95- # Install the target
96- install (
97- TARGETS devdat
98- EXPORT ${PROJECT_NAME} Targets
99- )
100-
101- # If using a custom JSON interface target, install it too
102- if (DEVDAT_USE_SYSTEM_JSON AND DEFINED JSON_INCLUDE_DIR)
116+ else ()
117+ # Otherwise just install devdat
103118 install (
104- TARGETS json_interface
119+ TARGETS devdat
105120 EXPORT ${PROJECT_NAME} Targets
106121 )
107122endif ()
108123
109- # Export the targets
124+ # Export targets
110125install (
111126 EXPORT ${PROJECT_NAME} Targets
112127 FILE ${PROJECT_NAME} Targets.cmake
@@ -116,6 +131,8 @@ install(
116131
117132# Create config files
118133include (CMakePackageConfigHelpers)
134+
135+ # Create config file variables
119136configure_file (
120137 ${CMAKE_CURRENT_SOURCE_DIR} /cmake/devdatConfig.cmake.in
121138 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake
@@ -128,15 +145,15 @@ write_basic_package_version_file(
128145 COMPATIBILITY SameMajorVersion
129146)
130147
131- # Install the config files
148+ # Install config files
132149install (
133150 FILES
134151 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} Config.cmake
135152 ${CMAKE_CURRENT_BINARY_DIR} /${PROJECT_NAME} ConfigVersion.cmake
136153 DESTINATION ${INSTALL_CONFIGDIR}
137154)
138155
139- # Add the tests if enabled
156+ # Add tests if enabled
140157if (BUILD_TESTING)
141158 enable_testing ()
142159 add_subdirectory (tests)
0 commit comments