Skip to content

Commit cb16287

Browse files
committed
updating cmake
1 parent 6b32926 commit cb16287

File tree

3 files changed

+146
-54
lines changed

3 files changed

+146
-54
lines changed

CMakeLists.txt

Lines changed: 115 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,140 @@
11
cmake_minimum_required(VERSION 3.18)
2+
project(devdat VERSION 1.0.0 LANGUAGES CXX)
23

3-
# Project definition
4-
project(devdat VERSION 0.0.1 LANGUAGES CXX CUDA)
4+
# Options
5+
option(BUILD_TESTING "Build tests" ON)
6+
option(DEVDAT_USE_SYSTEM_JSON "Use system-installed nlohmann_json" OFF)
57

68
# Set C++ standard
79
set(CMAKE_CXX_STANDARD 17)
810
set(CMAKE_CXX_STANDARD_REQUIRED ON)
9-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
11+
set(CMAKE_CXX_EXTENSIONS OFF)
1012

11-
# Optionally set CUDA architectures (uncomment and set as needed)
12-
# set(CMAKE_CUDA_ARCHITECTURES 60 70 75 80 86)
13+
# Check for CUDA and enable it (required for Thrust)
14+
include(CheckLanguage)
15+
check_language(CUDA)
16+
if(CMAKE_CUDA_COMPILER)
17+
enable_language(CUDA)
18+
set(CMAKE_CUDA_STANDARD 14)
19+
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
20+
find_package(CUDAToolkit REQUIRED)
21+
else()
22+
message(FATAL_ERROR "CUDA compiler not found! devdat requires CUDA for Thrust support.")
23+
endif()
1324

14-
# Install directories
15-
include(GNUInstallDirs)
25+
# Handle nlohmann_json dependency
26+
if(DEVDAT_USE_SYSTEM_JSON)
27+
find_package(nlohmann_json REQUIRED)
28+
set(JSON_TARGET nlohmann_json::nlohmann_json)
29+
else()
30+
# Download nlohmann_json at configure time
31+
include(FetchContent)
32+
FetchContent_Declare(
33+
nlohmann_json
34+
GIT_REPOSITORY https://github.com/nlohmann/json.git
35+
GIT_TAG v3.11.2
36+
)
37+
FetchContent_MakeAvailable(nlohmann_json)
38+
39+
# Create our own interface that doesn't need to be exported
40+
add_library(devdat_json_dep INTERFACE)
41+
target_include_directories(devdat_json_dep
42+
INTERFACE
43+
$<BUILD_INTERFACE:${nlohmann_json_SOURCE_DIR}/include>
44+
# Important: This is how consumers will find the headers
45+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
46+
)
47+
48+
# Install the nlohmann_json headers directly to include directory
49+
# This ensures they're found alongside our own headers
50+
install(
51+
DIRECTORY ${nlohmann_json_SOURCE_DIR}/include/
52+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
53+
)
54+
55+
set(JSON_TARGET devdat_json_dep)
56+
endif()
57+
58+
# Create the library (header-only)
59+
add_library(devdat INTERFACE)
1660

17-
# Fetch nlohmann_json (modern FetchContent usage)
18-
include(FetchContent)
19-
FetchContent_Declare(
20-
json
21-
GIT_REPOSITORY https://github.com/ArthurSonzogni/nlohmann_json_cmake_fetchcontent
22-
GIT_TAG v3.0.0
23-
GIT_SHALLOW TRUE
61+
# Set target properties (note: header-only libraries use INTERFACE)
62+
target_include_directories(devdat
63+
INTERFACE
64+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
65+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
66+
$<BUILD_INTERFACE:${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}>
67+
$<INSTALL_INTERFACE:${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}>
2468
)
25-
FetchContent_MakeAvailable(json)
2669

27-
# Header-only library target
28-
add_library(devdat INTERFACE)
29-
target_include_directories(devdat INTERFACE
30-
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/include>
31-
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
70+
# Link dependencies (using our local target and CUDA)
71+
target_link_libraries(devdat
72+
INTERFACE
73+
${JSON_TARGET}
74+
CUDA::toolkit
3275
)
3376

34-
# Alias for downstream usage
35-
add_library(devdat::devdat ALIAS devdat)
77+
# Add an alias target for use within the build tree
78+
add_library(${PROJECT_NAME}::devdat ALIAS devdat)
3679

37-
# Testing
38-
include(CTest)
39-
if(BUILD_TESTING)
40-
add_subdirectory(tests)
41-
endif()
80+
# Installation
81+
include(GNUInstallDirs)
82+
set(INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME})
4283

43-
# === Installation ===
84+
# Install the headers
85+
install(
86+
DIRECTORY include/
87+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
88+
)
4489

90+
# Install the target (note: header-only libraries have special install syntax)
4591
install(
46-
TARGETS devdat
47-
EXPORT devdat-config
48-
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
92+
TARGETS devdat
93+
EXPORT ${PROJECT_NAME}Targets
4994
)
5095

96+
if(NOT DEVDAT_USE_SYSTEM_JSON)
97+
# Add our bundled dependency to the exported targets
98+
install(
99+
TARGETS devdat_json_dep
100+
EXPORT ${PROJECT_NAME}Targets
101+
)
102+
endif()
103+
104+
# Export the targets
51105
install(
52-
EXPORT devdat-config
53-
NAMESPACE devdat::
54-
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/devdat
106+
EXPORT ${PROJECT_NAME}Targets
107+
FILE ${PROJECT_NAME}Targets.cmake
108+
NAMESPACE ${PROJECT_NAME}::
109+
DESTINATION ${INSTALL_CONFIGDIR}
55110
)
56111

112+
# Create config files
113+
include(CMakePackageConfigHelpers)
114+
115+
# Create a config file that handles dependencies
116+
configure_file(
117+
${CMAKE_CURRENT_SOURCE_DIR}/cmake/DevdatConfig.cmake.in
118+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
119+
@ONLY
120+
)
121+
122+
write_basic_package_version_file(
123+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
124+
VERSION ${PROJECT_VERSION}
125+
COMPATIBILITY SameMajorVersion
126+
)
127+
128+
# Install the config files
57129
install(
58-
DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/include/
59-
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
130+
FILES
131+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
132+
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
133+
DESTINATION ${INSTALL_CONFIGDIR}
60134
)
61135

62-
# Feature summary
63-
include(FeatureSummary)
64-
feature_summary(WHAT ALL)
136+
# Add the tests if enabled
137+
if(BUILD_TESTING)
138+
enable_testing()
139+
add_subdirectory(tests)
140+
endif()

cmake/DevdatConfig.cmake.in

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
@PACKAGE_INIT@
2+
3+
include(CMakeFindDependencyMacro)
4+
5+
# Find or load CUDA toolkit (required for Thrust)
6+
find_dependency(CUDAToolkit REQUIRED)
7+
8+
# Handle nlohmann_json dependency if using system version
9+
if(@DEVDAT_USE_SYSTEM_JSON@)
10+
find_dependency(nlohmann_json REQUIRED)
11+
endif()
12+
13+
# Include the exported targets
14+
include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Targets.cmake")
15+
16+
message(STATUS "Found @PROJECT_NAME@ @PROJECT_VERSION@")

examples/CMakeLists.txt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
cmake_minimum_required(VERSION 3.18)
2+
project(devdat_examples LANGUAGES CXX CUDA)
23

3-
project(Example VERSION 0.0.1 LANGUAGES CXX CUDA)
4-
4+
# Set C++ and CUDA standards
55
set(CMAKE_CXX_STANDARD 17)
66
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7-
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8-
9-
# Optionally set CUDA architectures (uncomment and set as needed)
10-
# set(CMAKE_CUDA_ARCHITECTURES 60 70 75 80 86)
7+
set(CMAKE_CUDA_STANDARD 14)
8+
set(CMAKE_CUDA_STANDARD_REQUIRED ON)
119

12-
include(GNUInstallDirs)
10+
# Find the devdat package
11+
find_package(devdat REQUIRED)
1312

14-
find_package(devdat CONFIG REQUIRED)
13+
# Add CUDA directories
14+
include_directories(include)
1515

16+
# Add the executable
1617
add_executable(devdat_examples
18+
main.cpp
1719
src/devdat_t.cu
1820
src/random.cu
19-
main.cpp
2021
)
2122

23+
# Link with devdat - all necessary include paths should come from this
2224
target_link_libraries(devdat_examples PRIVATE devdat::devdat)
2325

24-
target_include_directories(devdat_examples PRIVATE
25-
${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES}
26-
)
27-
28-
include(FeatureSummary)
29-
feature_summary(WHAT ALL)
26+
# If using CUDA, you might need to specify compute capability
27+
set_target_properties(devdat_examples PROPERTIES
28+
CUDA_ARCHITECTURES "75" # Adjust based on your GPU
29+
)

0 commit comments

Comments
 (0)