-
Notifications
You must be signed in to change notification settings - Fork 4
/
CMakeLists.txt.in
191 lines (166 loc) · 6.84 KB
/
CMakeLists.txt.in
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
cmake_minimum_required(VERSION 3.20)
set(CMAKE_OSX_DEPLOYMENT_TARGET "10.15" CACHE STRING "Minimum OS X deployment version")
project(CASMcode_crystallography VERSION 2.0.0 LANGUAGES CXX)
# set CMAKE_INSTALL_X variables
include(GNUInstallDirs)
# specify the C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# try to use ccache
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
##############################################
## Find dependencies
# Should find ZLIB::ZLIB
find_package(ZLIB)
# Find CASM
if(NOT DEFINED CASM_PREFIX)
message(STATUS "CASM_PREFIX not defined")
# try to find Python
find_package (Python COMPONENTS Interpreter Development)
if(DEFINED Python_EXECUTABLE)
# if Python found, obtain CASM_PREFIX from the libcasm.casmglobal
message(STATUS "found Python_EXECUTABLE: ${Python_EXECUTABLE}")
message(STATUS "checking for libcasm-global")
execute_process(
COMMAND pip show libcasm-global
RESULT_VARIABLE EXIT_CODE
OUTPUT_QUIET
)
if (${EXIT_CODE} EQUAL 0)
message(STATUS "found libcasm-global")
execute_process(COMMAND ${Python_EXECUTABLE} -m libcasm.casmglobal --prefix
OUTPUT_VARIABLE CASM_PREFIX_RAW)
string(STRIP ${CASM_PREFIX_RAW} CASM_PREFIX)
message(STATUS "CASM_PREFIX: ${CASM_PREFIX}")
else()
message(STATUS "did not find libcasm-global")
endif()
endif()
endif()
if(DEFINED CASM_PREFIX)
set(CASMcode_global_ROOT ${CASM_PREFIX}/share/CASMcode_global/cmake)
endif()
find_package(CASMcode_global)
if(NOT CASMcode_global_FOUND)
message(FATAL_ERROR "CMake failed to find CASMcode_global")
endif()
# if successful, we have CASM::casm_global
# if no user CMAKE_INSTALL_PREFIX, use CASM_PREFIX if it exists
IF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
if(DEFINED CASM_PREFIX)
message(STATUS "CMAKE_INSTALL_PREFIX initialized to default, so updating CMAKE_INSTALL_PREFIX to CASM_PREFIX")
set(CMAKE_INSTALL_PREFIX ${CASM_PREFIX} CACHE PATH "set CMAKE_INSTALL_PREFIX to CASM_PREFIX" FORCE)
message(STATUS "CMAKE_INSTALL_PREFIX: ${CMAKE_INSTALL_PREFIX}")
endif()
ENDIF(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
##############################################
## Build libcasm_crystallography
# create libcasm_crystallography
set(
libcasm_crystallography_HEADERS
@header_files@)
set(
libcasm_crystallography_SOURCES
@source_files@)
add_library(casm_crystallography SHARED ${libcasm_crystallography_SOURCES})
target_include_directories(casm_crystallography
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/casm/external>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/casm/external/gzstream>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/casm/external>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/casm/external/gzstream>
)
target_compile_options(casm_crystallography
PUBLIC
"-DCASM_XTAL_TXT_VERSION=\"${CMAKE_PROJECT_VERSION}\""
-DEIGEN_DEFAULT_DENSE_INDEX_TYPE=long
-DGZSTREAM_NAMESPACE=gz
)
target_link_libraries(casm_crystallography
ZLIB::ZLIB
${CMAKE_DL_LIBS}
CASM::casm_global
)
if(APPLE)
set_target_properties(
casm_crystallography PROPERTIES INSTALL_RPATH "@loader_path")
else()
set_target_properties(
casm_crystallography PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()
##############################################
## Install libcasm_crystallography
# install header files in <prefix>/libcasm/include/,
# while preserving directory structure
foreach ( filevar ${libcasm_crystallography_HEADERS} )
file(RELATIVE_PATH relfile ${PROJECT_SOURCE_DIR}/include/ ${filevar})
get_filename_component( reldir ${relfile} DIRECTORY )
install( FILES ${filevar} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${reldir} )
endforeach()
# install libcasm_crystallography in <prefix>/libcasm/lib/
install(
TARGETS casm_crystallography
EXPORT CASMcode_crystallographyTargets
DESTINATION lib)
##############################################
## Python extensions
# The CMake package config and target files are installed under the Python
# package root. This is necessary to ensure that all the relative paths in the
# helloTargets.cmake resolve correctly. It also provides encapsulation.
#
# The actual path used must be selected so that consuming projects can locate it
# via `find_package`. To support finding CMake packages in the Python package
# prefix, using `find_package`s default search path of
# `<prefix>/<name>/share/<name>*/cmake/` is reasonable. Adding the Python
# package installation prefix to CMAKE_PREFIX_PATH in combination with this path
# will allow `find_package` to find this package and any other package installed
# via a Python package if the CMake and Python packages are named the same.
set(CASM_CRYSTALLOGRAPHY_CMAKE_PACKAGE_INSTALL_SUBDIR "share/CASMcode_crystallography/cmake")
install(
EXPORT CASMcode_crystallographyTargets
NAMESPACE CASM::
DESTINATION ${CASM_CRYSTALLOGRAPHY_CMAKE_PACKAGE_INSTALL_SUBDIR})
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
CASMcode_crystallographyConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMinorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/CASMcode_crystallographyConfig.cmake.in" CASMcode_crystallographyConfig.cmake
INSTALL_DESTINATION ${CASM_CRYSTALLOGRAPHY_CMAKE_PACKAGE_INSTALL_SUBDIR})
install(FILES "${PROJECT_BINARY_DIR}/CASMcode_crystallographyConfig.cmake"
"${PROJECT_BINARY_DIR}/CASMcode_crystallographyConfigVersion.cmake"
DESTINATION ${CASM_CRYSTALLOGRAPHY_CMAKE_PACKAGE_INSTALL_SUBDIR})
# We are using the SKBUILD variable, which is defined when scikit-build is
# running the CMake build, to control building the Python wrapper. This allows
# the C++ project to be installed, standalone, when using the standard CMake
# build flow.
if(DEFINED SKBUILD)
# call pybind11-config to obtain the root of the cmake package
execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pybind11 --cmakedir
OUTPUT_VARIABLE pybind11_ROOT_RAW)
string(STRIP ${pybind11_ROOT_RAW} pybind11_ROOT)
find_package(pybind11)
# The extension modules must load:
# - the casm_global library
# - the casm_crystallography library
# They can be found by setting a relative rpath
### libcasm.xtal._xtal ###
pybind11_add_module(_xtal MODULE
"${PROJECT_SOURCE_DIR}/python/src/xtal.cpp")
target_link_libraries(_xtal PRIVATE CASM::casm_global)
target_link_libraries(_xtal PRIVATE casm_crystallography)
install(TARGETS _xtal DESTINATION xtal)
if(APPLE)
set_target_properties(
_xtal PROPERTIES INSTALL_RPATH "@loader_path/../lib")
else()
set_target_properties(
_xtal PROPERTIES INSTALL_RPATH "$ORIGIN/../lib")
endif()
endif()