Skip to content

Commit e6720b7

Browse files
Add CMake installation and export (#35)
Previously, the installation and export sections in CMakeLists.txt were commented out to streamline development. This worked well for rapid iteration but prevented proper library installation, which is now required for building Docker containers with cleanly separated build and runtime environments. This commit re-enables and extends the installation system to support both submodule usage and standalone installation, while dynamically handling dependencies to avoid duplication and export conflicts. - Add installation targets for dx2 library, headers, and CMake package config files. - Re-enable CMake export system to support `find_package(DX2)` in downstream projects. - Add `STREQUAL` fix for proper CMake path comparison in root detection. These changes enable proper Docker container builds with managed dependencies while maintaining full compatibility with existing submodule usage patterns and adding find_package support for projects that prefer system-managed installations.
1 parent 07f7b14 commit e6720b7

File tree

2 files changed

+67
-79
lines changed

2 files changed

+67
-79
lines changed

CMakeLists.txt

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(CMAKE_CXX_STANDARD 20)
77
set(CMAKE_CXX_STANDARD_REQUIRED ON)
88

99
# Only include global build-configuration features if we are the root CMakeLists.txt
10-
if(CMAKE_CURRENT_SOURCE_DIR EQUAL CMAKE_SOURCE_DIR)
10+
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
1111
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules")
1212
include(SetDefaultBuildRelWithDebInfo)
1313
include(AlwaysColourCompilation)
@@ -19,50 +19,34 @@ option(BUILD_SHARED_LIBS "Build using shared libraries" ON)
1919
# External Dependencies
2020
find_package(HDF5 REQUIRED)
2121
find_package(gemmi CONFIG REQUIRED)
22+
find_package(nlohmann_json 3.11.3 REQUIRED)
23+
find_package(fmt 11.0 REQUIRED)
24+
find_package(Eigen3 3.4 REQUIRED)
2225

23-
# #######################################################################
24-
# Automatic Dependencies
26+
# mdspan and GTest can use FetchContent as fallback
2527
set(FETCHCONTENT_QUIET OFF)
2628
include(FetchContent)
27-
FetchContent_Declare(
28-
nlohmann_json
29-
GIT_REPOSITORY https://github.com/nlohmann/json.git
30-
GIT_TAG v3.11.3
31-
EXCLUDE_FROM_ALL
32-
FIND_PACKAGE_ARGS
33-
)
34-
FetchContent_Declare(
35-
GTest
36-
GIT_REPOSITORY https://github.com/google/googletest.git
37-
GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # v1.15.2
38-
EXCLUDE_FROM_ALL
39-
FIND_PACKAGE_ARGS
40-
)
41-
FetchContent_Declare(
42-
fmt
43-
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
44-
GIT_TAG 11.2.0
45-
EXCLUDE_FROM_ALL
46-
FIND_PACKAGE_ARGS
47-
)
48-
FetchContent_Declare(
49-
Eigen3
50-
GIT_REPOSITORY https://gitlab.com/libeigen/eigen.git
51-
GIT_TAG 3.4.0
52-
EXCLUDE_FROM_ALL
53-
FIND_PACKAGE_ARGS
54-
)
55-
FetchContent_Declare(
56-
mdspan
57-
GIT_REPOSITORY https://github.com/kokkos/mdspan
58-
GIT_TAG mdspan-0.6.0
59-
EXCLUDE_FROM_ALL
60-
FIND_PACKAGE_ARGS
61-
)
62-
FetchContent_MakeAvailable(Eigen3 fmt nlohmann_json mdspan)
6329

64-
# GTest could have been made available under a different name
65-
if(NOT TARGET GTest::gtest_main)
30+
find_package(mdspan QUIET)
31+
if(NOT mdspan_FOUND AND NOT TARGET std::mdspan)
32+
FetchContent_Declare(
33+
mdspan
34+
GIT_REPOSITORY https://github.com/kokkos/mdspan
35+
GIT_TAG mdspan-0.6.0
36+
EXCLUDE_FROM_ALL
37+
)
38+
FetchContent_MakeAvailable(mdspan)
39+
endif()
40+
41+
# GTest for testing
42+
find_package(GTest QUIET)
43+
if(NOT GTest_FOUND AND NOT TARGET GTest::gtest_main)
44+
FetchContent_Declare(
45+
GTest
46+
GIT_REPOSITORY https://github.com/google/googletest.git
47+
GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # v1.15.2
48+
EXCLUDE_FROM_ALL
49+
)
6650
FetchContent_MakeAvailable(GTest)
6751
endif()
6852

@@ -75,44 +59,45 @@ add_subdirectory(tests)
7559

7660
# #######################################################################
7761
# Installation
78-
# install(
79-
# TARGETS dx2
80-
# EXPORT DX2Targets
81-
# LIBRARY DESTINATION lib
8262

83-
# # FILE_SET HEADERS DESTINATION include/dx2
84-
# )
85-
# install(
86-
# FILES
87-
# include/dx2/dx2.h
88-
# DESTINATION include/dx2
89-
# )
63+
install(
64+
TARGETS dx2
65+
EXPORT DX2Targets
66+
LIBRARY DESTINATION lib
67+
ARCHIVE DESTINATION lib
68+
RUNTIME DESTINATION bin
69+
COMPONENT Runtime
70+
)
9071

91-
# install(
92-
# EXPORT DX2Targets
93-
# FILE DX2Targets.cmake
94-
# DESTINATION lib/cmake/DX2
95-
# )
72+
install(
73+
DIRECTORY include/dx2/
74+
DESTINATION include/dx2
75+
COMPONENT Runtime
76+
FILES_MATCHING PATTERN "*.hpp"
77+
)
9678

97-
# include(CMakePackageConfigHelpers)
98-
# configure_package_config_file(
99-
# ${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
100-
# "${CMAKE_CURRENT_BINARY_DIR}/DX2Config.cmake"
101-
# INSTALL_DESTINATION "lib/cmake/DX2"
102-
# NO_SET_AND_CHECK_MACRO
103-
# NO_CHECK_REQUIRED_COMPONENTS_MACRO
104-
# )
105-
# write_basic_package_version_file(
106-
# "${CMAKE_CURRENT_BINARY_DIR}/DX2ConfigVersion.cmake"
107-
# VERSION "${DX2_VERSION_MAJOR}.${DX2_VERSION_MINOR}"
108-
# COMPATIBILITY AnyNewerVersion
109-
# )
110-
# install(
111-
# FILES
112-
# ${CMAKE_CURRENT_BINARY_DIR}/DX2Config.cmake
113-
# ${CMAKE_CURRENT_BINARY_DIR}/DX2ConfigVersion.cmake
114-
# DESTINATION lib/cmake/DX2
115-
# )
116-
# export(EXPORT DX2Targets
117-
# FILE "${CMAKE_CURRENT_BINARY_DIR}/DX2Targets.cmake"
118-
# )
79+
# Export targets
80+
install(
81+
EXPORT DX2Targets
82+
FILE DX2Targets.cmake
83+
NAMESPACE dx2::
84+
DESTINATION lib/cmake/DX2
85+
)
86+
87+
include(CMakePackageConfigHelpers)
88+
configure_package_config_file(
89+
${CMAKE_CURRENT_SOURCE_DIR}/Config.cmake.in
90+
"${CMAKE_CURRENT_BINARY_DIR}/DX2Config.cmake"
91+
INSTALL_DESTINATION "lib/cmake/DX2"
92+
)
93+
write_basic_package_version_file(
94+
"${CMAKE_CURRENT_BINARY_DIR}/DX2ConfigVersion.cmake"
95+
VERSION "${PROJECT_VERSION}"
96+
COMPATIBILITY AnyNewerVersion
97+
)
98+
install(
99+
FILES
100+
${CMAKE_CURRENT_BINARY_DIR}/DX2Config.cmake
101+
${CMAKE_CURRENT_BINARY_DIR}/DX2ConfigVersion.cmake
102+
DESTINATION lib/cmake/DX2
103+
)

requirements.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,6 @@ gemmi
22
cmake
33
hdf5=1.12.2
44
hdf5-external-filter-plugins
5+
nlohmann_json>=3.11.3
6+
fmt>=11.0
7+
eigen>=3.4

0 commit comments

Comments
 (0)