Skip to content

Commit b99deed

Browse files
committed
improving json dependency
1 parent 0454866 commit b99deed

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed

CMakeLists.txt

Lines changed: 48 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ project(devdat VERSION 1.0.0 LANGUAGES CXX)
44
# Options
55
option(BUILD_TESTING "Build tests" ON)
66
option(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
913
set(CMAKE_CXX_STANDARD 17)
@@ -24,21 +28,27 @@ endif()
2428

2529
# Handle nlohmann_json dependency
2630
if(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()
4050
else()
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)
5476
endif()
5577

5678
# Create the library (header-only)
@@ -78,35 +100,28 @@ add_library(${PROJECT_NAME}::devdat ALIAS devdat)
78100
include(GNUInstallDirs)
79101
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
80102

81-
# Install the headers
103+
# Install headers
82104
install(
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
)
107122
endif()
108123

109-
# Export the targets
124+
# Export targets
110125
install(
111126
EXPORT ${PROJECT_NAME}Targets
112127
FILE ${PROJECT_NAME}Targets.cmake
@@ -116,6 +131,8 @@ install(
116131

117132
# Create config files
118133
include(CMakePackageConfigHelpers)
134+
135+
# Create config file variables
119136
configure_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
132149
install(
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
140157
if(BUILD_TESTING)
141158
enable_testing()
142159
add_subdirectory(tests)

cmake/devdatConfig.cmake.in

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,15 @@
22

33
include(CMakeFindDependencyMacro)
44

5-
# Find or load CUDA toolkit (required for Thrust)
5+
# Find CUDA toolkit (required for Thrust)
66
find_dependency(CUDAToolkit REQUIRED)
77

8-
# Handle nlohmann_json dependency
9-
if(@DEVDAT_USE_SYSTEM_JSON@)
10-
# Check if we used a custom include path provided by superbuild
11-
if(NOT TARGET json_interface)
12-
# We used the system package during build
13-
find_dependency(nlohmann_json REQUIRED)
14-
endif()
15-
# Otherwise, json_interface target is already defined in the Targets file
8+
# Handle nlohmann_json dependency - only if we didn't export our own wrapper
9+
if(@DEVDAT_USE_SYSTEM_JSON@ AND NOT @EXPORT_JSON_TARGET@)
10+
find_dependency(nlohmann_json REQUIRED)
1611
endif()
1712

1813
# Include the exported targets
1914
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
2015

21-
# Provide compatibility with older FindPackage behavior
22-
set(devdat_FOUND TRUE)
23-
set(devdat_VERSION @PROJECT_VERSION@)
24-
set(devdat_INCLUDE_DIRS "${CMAKE_CURRENT_LIST_DIR}/../../../include")
25-
2616
message(STATUS "Found @PROJECT_NAME@ @PROJECT_VERSION@")

0 commit comments

Comments
 (0)