-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathCMakeLists.txt
285 lines (233 loc) · 10.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(QX C CXX)
# If QX was already included elsewhere in the project, don't include it again.
# There should be only one place for it and one version per project.
if(NOT TARGET qx)
# Loads up the appropriate directories for installing stuff.
include(GNUInstallDirs)
# Packages
include(FetchContent)
#=============================================================================#
# Configuration options #
#=============================================================================#
# Library type option. Default is a shared object, because for CMake it doesn't matter,
# but outside of CMake dependency information is lost for static libraries.
# That requires the user to link all of QX's direct and transitive dependencies as well, which is terribly ugly.
# setup.py *has* to do this however, because "pip install ." builds this in a temporary directory,
# so the shared objects that get built and installed and are then depended on by the Python lib
# get deleted by pip after the install.
option(
BUILD_SHARED_LIBS
"Whether libraries should be built as a shared object or as a static library"
OFF
)
if(NOT CMAKE_BUILD_TYPE)
message(STATUS "No build type selected, default to Release")
set(CMAKE_BUILD_TYPE "Release")
endif()
option(
QX_CPU_COMPATIBILITY_MODE
"Produce binaries that would work on any CPU, instead of just the one of the machine on which the code is built"
OFF
)
option(
QX_BUILD_TESTS
"Whether the tests should be built and added to `make test`"
OFF
)
option(
QX_BUILD_PYTHON
"Whether the Python module should be built"
OFF
)
mark_as_advanced(QX_BUILD_PYTHON)
set(
QX_PYTHON_DIR "${CMAKE_INSTALL_FULL_LIBDIR}/python/qxelarator"
CACHE STRING "Where to install the Python library"
)
mark_as_advanced(QX_PYTHON_DIR)
set(
QX_PYTHON_EXT ""
CACHE STRING "Basename for the Python extension, or \"\" to let CMake's SWIG implementation handle it"
)
mark_as_advanced(QX_PYTHON_EXT)
#=============================================================================#
# CMake weirdness and compatibility #
#=============================================================================#
# On Windows builds, CMake complains that the CMake internal variable "CMAKE_MODULE_LINKER_FLAGS_MAINTAINER"
# is not defined *the first time you configure*.
# Weirdly, all is good with the world if you then just try again.
# It seems to have to do with the "maintainer" build type in MSVC, but there is no documentation whatsoever.
# In any case, this just mimics what CMake does automatically the second time you invoke it,
# so it works around the issue.
if(NOT DEFINED CMAKE_MODULE_LINKER_FLAGS_MAINTAINER)
set(
CMAKE_MODULE_LINKER_FLAGS_MAINTAINER ""
CACHE STRING "Flags used by the linker during the creation of modules during MAINTAINER builds."
)
endif()
#=============================================================================#
# Global build configuration #
#=============================================================================#
# Since we have multiple libraries to link together,
# we unfortunately have to worry about RPATH handling on Linux and OSX. See
# https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_FULL_LIBDIR}")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
# Windows weirdness: need a .lib file to link against a DLL at compile-time (I think),
# but only the .dll is generated when there are no exported symbols.
# This sort of fixes that (https://stackoverflow.com/questions/1941443)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
# Generate compile_commands.json build file for clang-tidy
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#=============================================================================#
# Dependencies #
#=============================================================================#
find_package(Boost REQUIRED)
find_package(Eigen3 REQUIRED)
find_package(fmt REQUIRED)
find_package(libqasm REQUIRED)
find_package(range-v3 REQUIRED)
#=============================================================================#
# QX library target #
#=============================================================================#
add_library(qx
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/circuit.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/circuit_builder.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/cqasm_v3x.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/dense_unitary_matrix.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/error_models.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/instructions.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/operands_helper.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/quantum_state.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/qxelarator.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/random.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/register_manager.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/simulation_result.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/simulator.cpp"
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx/sparse_array.cpp"
)
target_compile_features(qx PRIVATE
cxx_std_23
)
target_include_directories(qx PUBLIC
"${CMAKE_CURRENT_SOURCE_DIR}/include/"
"${Boost_INCLUDE_DIRS}"
)
if(CMAKE_COMPILER_IS_GNUCXX)
target_compile_options(qx PRIVATE
-Wall -Wextra -Werror -Wfatal-errors
)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(qx PRIVATE
-Wall -Wextra -Werror -Wfatal-errors
-Wno-error=unused-but-set-variable
-Wno-error=unused-function
-Wno-error=unused-local-typedef
)
elseif(MSVC)
target_compile_options(qx PRIVATE
/MP /EHsc /bigobj
)
else()
message(SEND_ERROR "Unknown compiler!")
endif()
if(!${QX_CPU_COMPATIBILITY_MODE})
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
target_compile_options(qx PUBLIC
-march=native
)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
target_compile_options(qx PUBLIC
-ipo -xHost -no-prec-div
)
elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
target_compile_options(qx PUBLIC
-march=native
)
endif()
endif()
# Address sanitizer
if(ASAN_ENABLED)
if(MSVC AND CMAKE_BUILD_TYPE STREQUAL "Debug")
string(REGEX REPLACE "/RTC(su|[1su])" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}\n")
# /MTd is needed to link clang_rt.asan-i386.lib statically
# Otherwise the path to clang_rt.asan-i386.dll should be provided
add_compile_options(/fsanitize=address /MTd)
elseif(${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
add_compile_options(-fsanitize=address,undefined -fno-omit-frame-pointer)
add_link_options(-fsanitize=address,undefined)
endif()
endif()
#=============================================================================#
# Configure, build, and link dependencies #
#=============================================================================#
target_link_libraries(qx PUBLIC
Eigen3::Eigen
fmt::fmt
libqasm::libqasm
range-v3::range-v3
)
#=============================================================================#
# Executables #
#=============================================================================#
add_executable(qx-simulator
"${CMAKE_CURRENT_SOURCE_DIR}/src/qx-simulator/simulator.cpp"
)
target_link_libraries(qx-simulator PRIVATE
qx
)
#=============================================================================#
# Testing #
#=============================================================================#
if(QX_BUILD_TESTS)
enable_testing()
include(CTest)
include(GoogleTest)
set(CMAKE_CTEST_ARGUMENTS "--output-on-failure")
add_subdirectory(test)
endif()
#=============================================================================#
# Python module #
#=============================================================================#
if(QX_BUILD_PYTHON)
add_subdirectory(python)
endif()
#=============================================================================#
# Debug info #
#=============================================================================#
message(STATUS
"[${PROJECT_NAME}] Target include directories:\n"
" CMAKE_CURRENT_SOURCE_DIR/include/: ${CMAKE_CURRENT_SOURCE_DIR}/include/\n"
" Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}\n"
" fmt_INCLUDE_DIRS: ${fmt_INCLUDE_DIRS}\n"
" libqasm_INCLUDE_DIRS: ${libqasm_INCLUDE_DIRS}\n"
" range-v3_INCLUDE_DIRS: ${range-v3_INCLUDE_DIRS}\n"
)
#=============================================================================#
# Installation #
#=============================================================================#
# Install targets for the QX library.
install(
TARGETS qx
LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}"
ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}"
)
install(
DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "*.cpp"
)
install(
DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include/"
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING PATTERN "*.h" PATTERN "*.hpp" PATTERN "*.cpp"
)
endif() # NOT TARGET qx