-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMakeLists.txt
186 lines (159 loc) · 4.97 KB
/
CMakeLists.txt
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
# Supported CMake versions
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
# Project configuration
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
project(MatrixPermanent VERSION 0.0.1 LANGUAGES CXX)
if(PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(
FATAL_ERROR
"In-source builds not allowed; make a build directory and run CMake from it\n")
endif()
if(ENV{PERMANENT_TUNE})
set(PERMANENT_TUNE ON)
endif()
if(SKBUILD OR ENV{PERMANENT_PYTHON})
set(PERMANENT_PYTHON ON)
endif()
# Dependencies
if(PROJECT_IS_TOP_LEVEL AND PERMANENT_PYTHON)
find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
elseif(PERMANENT_TUNE)
find_package(Python REQUIRED COMPONENTS Interpreter)
endif()
# Tuning header target
set(PERMANENT_TUNING_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/include/permanent/tuning.h")
set(PERMANENT_TUNING_DATA "${CMAKE_BINARY_DIR}/tuning.csv")
if(PERMANENT_TUNE)
add_executable(PermanentTune src/tuning.cc)
target_include_directories(PermanentTune PRIVATE include)
add_custom_target(TuningData ALL
COMMAND
PermanentTune "${PERMANENT_TUNING_DATA}"
BYPRODUCTS
"${PERMANENT_TUNING_DATA}")
add_custom_target(TuningHeader ALL
COMMAND
${Python_EXECUTABLE}
"${CMAKE_CURRENT_SOURCE_DIR}/tools/generate_tuning_header.py"
"${PERMANENT_TUNING_DATA}"
"${PERMANENT_TUNING_HEADER}"
BYPRODUCTS
"${PERMANENT_TUNING_HEADER}")
add_dependencies(TuningHeader TuningData)
else()
add_custom_target(TuningHeader ALL
COMMAND
cp "-f"
"${CMAKE_CURRENT_SOURCE_DIR}/include/permanent/tuning.default.h"
"${PERMANENT_TUNING_HEADER}"
BYPRODUCTS
"${PERMANENT_TUNING_HEADER}")
endif()
# Header library target
add_library(permanent_headers INTERFACE)
add_library(MatrixPermanent::headers ALIAS permanent_headers)
add_dependencies(permanent_headers TuningHeader)
target_include_directories(permanent_headers
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
if(PROJECT_IS_TOP_LEVEL)
set(CPACK_RESOURCE_FILE_LICENSE "${PROJECT_SOURCE_DIR}/LICENSE")
include(GNUInstallDirs)
include(CMakePackageConfigHelpers)
include(CPack)
write_basic_package_version_file(
"${PROJECT_NAME}ConfigVersion.cmake"
VERSION
${PROJECT_VERSION}
COMPATIBILITY
SameMajorVersion)
configure_package_config_file(
"${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
INSTALL_DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(TARGETS permanent_headers
EXPORT ${PROJECT_NAME}_Targets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
install(
EXPORT
${PROJECT_NAME}_Targets
FILE
${PROJECT_NAME}Targets.cmake
NAMESPACE
${PROJECT_NAME}::
DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(
FILES
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake"
"${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake"
DESTINATION
${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake)
install(
DIRECTORY
${PROJECT_SOURCE_DIR}/include/permanent
DESTINATION
include)
endif()
# Python library target
if(PROJECT_IS_TOP_LEVEL AND PERMANENT_PYTHON)
execute_process(
COMMAND
sh -c "${CMAKE_CXX_COMPILER} --version | head -n 1"
WORKING_DIRECTORY
${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE
_PERMANENT_COMPILER_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND
git rev-parse --abbrev-ref HEAD
WORKING_DIRECTORY
${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE
_PERMANENT_GIT_BRANCH
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND
git log -1 --format=%h
WORKING_DIRECTORY
${PROJECT_SOURCE_DIR}
OUTPUT_VARIABLE
_PERMANENT_GIT_COMMIT_HASH
OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process(
COMMAND
${Python_EXECUTABLE} "-c" "import numpy; print(numpy.get_include())"
OUTPUT_VARIABLE
NumPy_INCLUDE_DIRS
OUTPUT_STRIP_TRAILING_WHITESPACE)
python_add_library(permanent MODULE src/main.cc WITH_SOABI)
target_include_directories(permanent
PRIVATE
include
${NumPy_INCLUDE_DIRS})
target_compile_definitions(permanent
PRIVATE
"_PERMANENT_VERSION=${PROJECT_VERSION}"
"_PERMANENT_COMPILER_VERSION=${_PERMANENT_COMPILER_VERSION}"
"_PERMANENT_GIT_BRANCH=${_PERMANENT_GIT_BRANCH}"
"_PERMANENT_GIT_COMMIT_HASH=${_PERMANENT_GIT_COMMIT_HASH}"
)
if (UNIX)
if (APPLE)
set_target_properties(permanent
PROPERTIES
LINK_FLAGS "-Wl,-dylib,-undefined,dynamic_lookup")
else()
set_target_properties(permanent
PROPERTIES
LINK_FLAGS "-Wl,--allow-shlib-undefined")
endif()
endif()
install(TARGETS permanent DESTINATION .)
endif()