-
Notifications
You must be signed in to change notification settings - Fork 28
/
CMakeLists.txt
173 lines (153 loc) · 5.96 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
cmake_minimum_required(VERSION 3.15)
set(STAQ_VERSION_NUM 3.5)
set(STAQ_VERSION_STR "${STAQ_VERSION_NUM}")
project(
staq
VERSION ${STAQ_VERSION_NUM}
LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}")
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/")
enable_testing()
# staq version
add_compile_definitions(STAQ_VERSION_NUM=${STAQ_VERSION_NUM})
add_compile_definitions(STAQ_VERSION_STR="${STAQ_VERSION_STR}")
# staq root directory
add_compile_definitions(PROJECT_ROOT_DIR="${PROJECT_SOURCE_DIR}")
# Guard against in-source builds (snippet from Eigen's CMakeLists.txt)
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(
FATAL_ERROR
"In-source builds not allowed. Please instruct CMake to use an\
out-of-source build, e.g.,
cmake -B build && cmake --build build
You may need to remove CMakeCache.txt.")
endif()
# Force clang to use libc++
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
add_compile_options("-stdlib=libc++")
endif()
# Windows issues with Microsoft Visual Studio
if(MSVC)
include_directories(SYSTEM libs/third_party/pthreadwin32)
add_compile_options(-bigobj)
if(MSVC_VERSION GREATER_EQUAL 1914)
add_compile_options("/Zc:__cplusplus")
endif()
endif()
# MinGW or Cygwin have issues with object files that are too large
if(MINGW OR CYGWIN)
add_compile_options("-Wa,-mbig-obj")
endif()
# staq headers
add_library(libstaq INTERFACE)
target_compile_definitions(libstaq
INTERFACE -DSTAQ_VERSION_NUM=${STAQ_VERSION_NUM})
target_compile_definitions(libstaq
INTERFACE -DSTAQ_VERSION_STR="${STAQ_VERSION_STR}")
target_include_directories(
libstaq INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/>
$<INSTALL_INTERFACE:include/staq>)
# qasmtools library
target_include_directories(
libstaq
INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/qasmtools/include/>
$<INSTALL_INTERFACE:include/>)
# 3rd party libs
include_directories(SYSTEM libs/third_party)
target_include_directories(
libstaq INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/libs>
$<INSTALL_INTERFACE:include/staq/third_party>)
# pystaq and pybind11 (only if the Python development kit is detected)
find_package(Python3 QUIET COMPONENTS Interpreter Development)
if(${Python3_FOUND})
include(cmake/pybind11.cmake)
include(cmake/pystaq.cmake)
endif()
# Enable OpenQASM 2.0 Specs
option(USE_OPENQASM2_SPECS
"Use OpenQASM 2.0 standard instead of Qiskit gate specifications" OFF)
if(${USE_OPENQASM2_SPECS})
target_compile_definitions(libstaq INTERFACE -DUSE_OPENQASM2_SPECS=true)
message(STATUS "OpenQASM2 specs - ON")
else()
target_compile_definitions(libstaq INTERFACE -DUSE_OPENQASM2_SPECS=false)
message(STATUS "OpenQASM2 specs - OFF")
endif()
# Compiler
set(COMPILER "staq")
add_executable(${COMPILER} ${PROJECT_SOURCE_DIR}/src/staq/main.cpp)
target_link_libraries(${COMPILER} PUBLIC libstaq)
# Additional command line tools
add_subdirectory(src/tools)
# Unit testing
add_subdirectory(${CMAKE_SOURCE_DIR}/unit_tests/ EXCLUDE_FROM_ALL SYSTEM)
# Enable all warnings for GNU gcc and Clang/AppleClang
if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" OR ${CMAKE_CXX_COMPILER_ID}
STREQUAL "GNU")
add_compile_options("-pedantic" "-Wall" "-Wextra" "-Weffc++")
endif()
# Default build type
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE
Release
CACHE STRING "Choose the type of build, options are: \
None Debug Release MinSizeRel RelWithDebInfo." FORCE)
endif()
# Installation (binaries)
install(TARGETS ${COMPILER} DESTINATION ${CMAKE_INSTALL_BINDIR})
option(INSTALL_SOURCES "Enable staq's source code installation" OFF)
# Installation (source)
if(INSTALL_SOURCES)
set(STAQ_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/include/${PROJECT_NAME}")
install(DIRECTORY include/ DESTINATION ${STAQ_INSTALL_DIR})
install(DIRECTORY qasmtools/include/ DESTINATION ${STAQ_INSTALL_DIR})
install(DIRECTORY libs/third_party DESTINATION ${STAQ_INSTALL_DIR})
install(TARGETS libstaq EXPORT staq_targets)
install(EXPORT staq_targets DESTINATION "lib/cmake/${PROJECT_NAME}")
include(CMakePackageConfigHelpers)
configure_package_config_file(
"cmake/staqConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/staqConfig.cmake"
INSTALL_DESTINATION "lib/cmake/${PROJECT_NAME}")
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/staqConfig.cmake"
DESTINATION "lib/cmake/${PROJECT_NAME}")
install(FILES "${CMAKE_SOURCE_DIR}/cmake/staq_msvc.cmake"
DESTINATION "lib/cmake/${PROJECT_NAME}")
endif()
# Uninstall
# https://gitlab.kitware.com/cmake/community/-/wikis/FAQ#can-i-do-make-uninstall-with-cmake
# UNIX/Linux: sudo cmake --build build --target uninstall Windows: cmake
# --build build --target uninstall
if(NOT TARGET uninstall)
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/cmake/staq_uninstall.cmake.in"
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY)
if(NOT MSVC)
if(INSTALL_SOURCES)
add_custom_target(
uninstall
COMMAND ${CMAKE_COMMAND} -P
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
COMMAND ${CMAKE_COMMAND} -E remove_directory
"${CMAKE_INSTALL_PREFIX}/lib/cmake/${PROJECT_NAME}"
COMMAND ${CMAKE_COMMAND} -E remove_directory "${STAQ_INSTALL_DIR}"
COMMENT "Uninstall staq and its sources")
else()
add_custom_target(
uninstall
COMMAND ${CMAKE_COMMAND} -P
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
COMMENT "Uninstall staq")
endif()
else()
add_custom_target(
uninstall
COMMAND ${CMAKE_COMMAND} -P
"${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake"
COMMAND ${CMAKE_COMMAND} -E remove_directory "${CMAKE_INSTALL_PREFIX}"
COMMENT "Uninstall staq")
endif()
endif()