forked from NVIDIA/cuda-quantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
641 lines (553 loc) · 23.7 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
# ============================================================================ #
# Copyright (c) 2022 - 2024 NVIDIA Corporation & Affiliates. #
# All rights reserved. #
# #
# This source code and the accompanying materials are made available under #
# the terms of the Apache License 2.0 which accompanies this distribution. #
# ============================================================================ #
cmake_minimum_required(VERSION 3.22 FATAL_ERROR)
# Policies
# ==============================================================================
if(POLICY CMP0068)
cmake_policy(SET CMP0068 NEW)
set(CMAKE_BUILD_WITH_INSTALL_NAME_DIR ON)
endif()
if(POLICY CMP0075)
cmake_policy(SET CMP0075 NEW)
endif()
if(POLICY CMP0077)
cmake_policy(SET CMP0077 NEW)
endif()
if(POLICY CMP0057)
cmake_policy(SET CMP0057 NEW)
endif()
if(POLICY CMP0074)
cmake_policy(SET CMP0074 NEW)
endif()
# CMP0135: Timestamp for content downloaded with ExternalProject_Add()
# New in CMake 3.24 https://cmake.org/cmake/help/latest/policy/CMP0135.html
if(POLICY CMP0135)
cmake_policy(SET CMP0135 NEW)
endif()
# CMP0116: Ninja generators transform `DEPFILE`s from `add_custom_command()`
# New in CMake 3.20. https://cmake.org/cmake/help/latest/policy/CMP0116.html
if(POLICY CMP0116)
cmake_policy(SET CMP0116 OLD)
endif()
# Project setup
# ==============================================================================
project(cudaq LANGUAGES CXX C)
# Prevent In-source builds
# ==============================================================================
# Using the undocumented `CMAKE_DISABLE_IN_SOURCE_BUILDS` and
# `CMAKE_DISABLE_SOURCE_CHANCES` variables is not a good idea: They can change
# without warning and they do not accomplish what they are supposed to do, i.e.,
# cmake files will still be created in the source tree.
# Going the extra mile to prevent the user from playing tricks with symlinks.
get_filename_component(REAL_PROJECT_SOURCE_DIR "${PROJECT_SOURCE_DIR}" REALPATH)
get_filename_component(REAL_PROJECT_BINARY_DIR "${PROJECT_BINARY_DIR}" REALPATH)
if("${REAL_PROJECT_SOURCE_DIR}" STREQUAL "${REAL_PROJECT_BINARY_DIR}")
message(FATAL_ERROR
"In-source builds are not permitted. You must run cmake in a separeted "
"directory, e.g.:\n"
" mkdir build && cd build && cmake ..\n"
"NOTE: Remeber to clean up the source tree by deleting the files already "
"created by CMake, e.g, CMakeCache.txt and cmake.check_cache")
endif()
# Project globals
# ==============================================================================
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED on)
set(CMAKE_POSITION_INDEPENDENT_CODE TRUE)
# Set warnings as errors by default.
# Individual targets and the command line invocation can override this behavior.
if(NOT DEFINED CMAKE_COMPILE_WARNING_AS_ERROR)
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
endif()
# Enable the REST code by default.
if (NOT DEFINED CUDAQ_ENABLE_REST)
set(CUDAQ_ENABLE_REST ON CACHE BOOL "Enable building REST client & server.")
endif()
# Enable the remote simulator by default.
if (CUDAQ_ENABLE_REST AND NOT DEFINED CUDAQ_ENABLE_REMOTE_SIM)
set(CUDAQ_ENABLE_REMOTE_SIM ON CACHE BOOL "Enable building cudaq-qpud.")
# Optionally enable the tests that use cudaq-qpud.
if (NOT DEFINED CUDAQ_TEST_REMOTE_SIM)
set(CUDAQ_TEST_REMOTE_SIM ON CACHE BOOL "Run remote-sim tests.")
endif()
endif()
# Generate a CompilationDatabase (compile_commands.json file) for our build,
# for use by clang_complete, YouCompleteMe, etc.
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
if(NOT CUDAQ_LLVM_VERSION)
set(CUDAQ_LLVM_VERSION 16)
endif()
find_package(Git QUIET)
if(GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git")
# Update submodules as needed
option(GIT_SUBMODULE "Check submodules during build" ON)
if(GIT_SUBMODULE)
message(STATUS "Submodule update")
execute_process(COMMAND ${GIT_EXECUTABLE} -c submodule.tpls/llvm.update=none submodule update --init --recursive
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_SUBMOD_RESULT)
if(NOT GIT_SUBMOD_RESULT EQUAL "0")
message(FATAL_ERROR "Cloning git submodules failed with ${GIT_SUBMOD_RESULT}, please checkout submodules manually")
endif()
endif()
endif()
if(NOT EXISTS "${PROJECT_SOURCE_DIR}/tpls/fmt/CMakeLists.txt")
message(FATAL_ERROR "The submodules were not downloaded! GIT_SUBMODULE was turned off or failed. Please update submodules and try again.")
endif()
# Options
# ==============================================================================
option(CUDAQ_BUILD_TESTS "Build cudaq tests" ON)
option(CUDAQ_ENABLE_RPC_LOGGING "Enable verbose printout for client/server qpud connection." OFF)
option(CUDAQ_BUILD_RELOCATABLE_PACKAGE "Make CUDA Quantum install tree relocatable, system headers included." OFF)
option(CUDAQ_TEST_MOCK_SERVERS "Enable Remote QPU Tests via Mock Servers." OFF)
option(CUDAQ_DISABLE_RUNTIME "Build without the CUDA Quantum runtime, just the compiler toolchain." OFF)
option(CUDAQ_SKIP_MPI "Do not build with MPI, even when it is available." OFF)
option(CUDAQ_DISABLE_CPP_FRONTEND "Build without the CUDA Quantum C++ Clang-based Frontend." OFF)
option(CUDAQ_ENABLE_CC "Enable CUDA Quantum code coverage generation." OFF)
# Certain build configurations may be set directly in the environment.
# This facilitates some of the packaging (e.g. python packages built based on the pyproject.toml).
if(NOT LLVM_DIR AND EXISTS "$ENV{LLVM_INSTALL_PREFIX}/lib/cmake/llvm")
SET(LLVM_DIR "$ENV{LLVM_INSTALL_PREFIX}/lib/cmake/llvm")
endif()
if(NOT CUSTATEVEC_ROOT)
SET(CUSTATEVEC_ROOT "$ENV{CUQUANTUM_INSTALL_PREFIX}")
endif()
if(NOT CUTENSORNET_ROOT)
SET(CUTENSORNET_ROOT "$ENV{CUQUANTUM_INSTALL_PREFIX}")
endif()
if(NOT CUTENSOR_ROOT)
SET(CUTENSOR_ROOT "$ENV{CUTENSOR_INSTALL_PREFIX}")
endif()
if(NOT ZLIB_ROOT)
SET(ZLIB_ROOT "$ENV{ZLIB_INSTALL_PREFIX}")
endif()
if(NOT OPENSSL_ROOT_DIR)
SET(OPENSSL_ROOT_DIR "$ENV{OPENSSL_INSTALL_PREFIX}")
endif()
if(NOT BLAS_LIBRARIES AND EXISTS "$ENV{BLAS_INSTALL_PREFIX}/libblas.a")
# CACHE INTERNAL is needed due to how FindBLAS.cmake works...
SET(BLAS_LIBRARIES "$ENV{BLAS_INSTALL_PREFIX}/libblas.a" CACHE INTERNAL "")
endif()
if(NOT CURL_LIBRARY AND EXISTS "$ENV{CURL_INSTALL_PREFIX}/lib/libcurl.a")
SET(CURL_LIBRARY "$ENV{CURL_INSTALL_PREFIX}/lib/libcurl.a")
SET(CURL_INCLUDE_DIR "$ENV{CURL_INSTALL_PREFIX}/include")
SET(CURL_CONFIG_EXECUTABLE "$ENV{CURL_INSTALL_PREFIX}/bin/curl-config")
SET(CMAKE_USE_SYSTEM_CURL TRUE)
SET(CURL_NO_CURL_CMAKE ON)
endif()
if(NOT CUDAQ_EXTERNAL_NVQIR_SIMS)
SET(CUDAQ_EXTERNAL_NVQIR_SIMS $ENV{CUDAQ_EXTERNAL_NVQIR_SIMS})
endif()
if(NOT CUDAQ_ENABLE_CC)
SET(CUDAQ_ENABLE_CC $ENV{CUDAQ_ENABLE_CC})
endif()
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
SET(CMAKE_SKIP_INSTALL_RPATH FALSE)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE)
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
SET(CMAKE_INSTALL_RPATH "$ORIGIN:$ORIGIN/lib:$ORIGIN/lib/plugins:$ORIGIN/../lib:$ORIGIN/../lib/plugins")
SET(BLA_STATIC ON)
if(NOT DEFINED ZLIB_USE_STATIC_LIBS)
SET(ZLIB_USE_STATIC_LIBS TRUE)
endif()
if(NOT DEFINED OPENSSL_USE_STATIC_LIBS)
SET(OPENSSL_USE_STATIC_LIBS TRUE)
endif()
if(NOT DEFINED CUDAQ_ENABLE_STATIC_LINKING)
SET(CUDAQ_ENABLE_STATIC_LINKING $ENV{CUDAQ_ENABLE_STATIC_LINKING})
endif()
if (CUDAQ_ENABLE_STATIC_LINKING)
# Using -static-libgcc -static-libstdc++ offers more fine grained control here if needed.
# At the time of writing, there wasn't any benefit to using those over -static.
# However, while this so far seems to be working reasonably well, this will
# unfortunately also link glibc statically, which in general is likely to cause
# issues sooner or later. A better option might be to include the required GNU C Library
# and have the nvq++ compiler dynamically pick the newer one of the included or system one.
# See also these posts:
# - https://blog.gibson.sh/2017/11/26/creating-portable-linux-binaries/
# - https://www.jertype.com/upgrading-glibc/
# If the final application loads multiple dynamic libraries, and two or more
# of those dynamic libraries are linked to different versions of the same static
# libraries, then the different copies of those static libraries will conflict
# with each other. If we keep linking everything statically, we should at least set
# CMAKE_<LANG>_VISIBILITY_PRESET to hidden and CMAKE_VISIBILITY_INLINES_HIDDEN to 1,
# to avoid some of these issues.
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static")
SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-pie")
SET(OPENSSL_USE_STATIC_LIBS TRUE)
SET(ZLIB_USE_STATIC_LIBS TRUE)
set(CURL_STATICLIB ON)
message(STATUS "CUDA Quantum static linking enabled.")
else()
message(STATUS "CUDA Quantum static linking disabled.")
endif()
# ZLIB is required when we build LLVM with zlib support.
# ZLIB support in LLVM is good to have e.g. for the linker,
# since some binaries will use zlib compression to store symbols.
find_package(ZLIB REQUIRED)
include(ExternalProject)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose Release or Debug" FORCE)
endif()
# Find and configure LLVM, Clang and MLIR
# ==============================================================================
# The user can specify the path to LLVM cmake directory using
# `-DLLVM_DIR=path/to/cmake/llvm`. If this definition is not provided, we look
# for `llvm-config` tool. The user can also provide a LLVM version to look for
# using `CUDAQ_LLVM_VERSION`, e.g. "-DCUDAQ_LLVM_VERSION=15". Note that this
# version variable is set to the latest LLVM version by default, and setting it
# to an older version might break the project.
find_package(LLVM ${CUDAQ_LLVM_VERSION} CONFIG QUIET)
if(NOT LLVM_DIR)
message(STATUS "LLVM_DIR not found, will try with llvm-config executable.")
macro(find_llvm_config name version_major)
set(extra_args ${ARGN})
list(LENGTH extra_args extra_count)
if (${extra_count} GREATER 0)
list(GET extra_args 0 path)
find_program(LLVM_CONFIG NAMES ${name} PATHS ${path} NO_DEFAULT_PATH
DOC "Path to llvm-config tool")
else()
find_program(LLVM_CONFIG NAMES ${name} DOC "Path to llvm-config tool")
endif()
if(LLVM_CONFIG)
execute_process(
COMMAND ${LLVM_CONFIG} --version
RESULT_VARIABLE LLVM_CONFIG_RESULT
OUTPUT_VARIABLE LLVM_CONFIG_VERSION
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(NOT LLVM_CONFIG_VERSION MATCHES "^${version_major}[.][0-9]+[.][0-9]+")
unset(LLVM_CONFIG CACHE)
endif()
endif()
endmacro()
# First we try to find llvm-config in the llvm submodule.
find_llvm_config(llvm-config ${CUDAQ_LLVM_VERSION}
"${CMAKE_CURRENT_SOURCE_DIR}/tpls/llvm/build/bin")
# Try to find a system llvm-config and make sure it is the correct version.
if(NOT LLVM_CONFIG)
find_llvm_config(llvm-config ${CUDAQ_LLVM_VERSION})
endif()
# If it is not the correct version, try finding llvm-config-VERSION
if(NOT LLVM_CONFIG)
find_llvm_config(llvm-config-${CUDAQ_LLVM_VERSION} ${CUDAQ_LLVM_VERSION})
if (LLVM_CONFIG)
set(NVQPP_LLVM_EXECUTABLE_SUFFIX -${CUDAQ_LLVM_VERSION})
endif()
endif()
execute_process(
COMMAND ${LLVM_CONFIG} --cmakedir
RESULT_VARIABLE LLVM_CONFIG_RESULT
OUTPUT_VARIABLE LLVM_CONFIG_CMAKE_DIR
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
if(LLVM_CONFIG_RESULT)
message(FATAL_ERROR
"Could not find suitable llvm-config(-${CUDAQ_LLVM_VERSION}).\
\nTry providing valid -DLLVM_DIR=/path/to/llvm/lib/cmake/llvm.")
else()
find_package(LLVM ${CUDAQ_LLVM_VERSION} REQUIRED CONFIG
HINTS ${LLVM_CONFIG_CMAKE_DIR} NO_DEFAULT_PATH)
endif()
endif()
if(NOT MLIR_DIR)
set(MLIR_DIR ${LLVM_BINARY_DIR}/lib/cmake/mlir)
endif()
find_package(MLIR REQUIRED CONFIG)
if(NOT Clang_DIR)
set(Clang_DIR ${LLVM_BINARY_DIR}/lib/cmake/clang)
endif()
find_package(Clang CONFIG)
if (NOT Clang_FOUND)
message(STATUS "Clang not found, turning off C++ Frontend.")
set (CUDAQ_DISABLE_CPP_FRONTEND ON)
endif()
message(STATUS "Using LLVMConfig.cmake in: ${LLVM_DIR}")
message(STATUS "Using ClangConfig.cmake in: ${Clang_DIR}")
message(STATUS "Using MLIRConfig.cmake in: ${MLIR_DIR}")
set(LLVM_RUNTIME_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/bin)
set(LLVM_LIBRARY_OUTPUT_INTDIR ${CMAKE_BINARY_DIR}/lib)
set(MLIR_BINARY_DIR ${CMAKE_BINARY_DIR})
list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}")
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules")
include(TableGen)
include(AddLLVM)
include(AddMLIR)
include(HandleLLVMOptions)
# Add LLVM, Clang and MLIR headers to the include path
include_directories(SYSTEM
${LLVM_INCLUDE_DIRS}
${CLANG_INCLUDE_DIRS}
${MLIR_INCLUDE_DIRS})
link_directories(${LLVM_BUILD_LIBRARY_DIR})
add_definitions(${LLVM_DEFINITIONS})
# Define the default arguments to use with 'lit', and an option for the user to
# override.
set(LIT_ARGS_DEFAULT "-sv")
if(MSVC_IDE OR XCODE)
set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
endif()
set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
# CUDA Quantum configuration
# ==============================================================================
message(STATUS "Build type is ${CMAKE_BUILD_TYPE}")
set(CUDAQ_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}) # --src-root
set(CUDAQ_MAIN_INCLUDE_DIR ${CUDAQ_MAIN_SRC_DIR}/include)
set(CUDAQ_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
set(CUDAQ_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
set(CUDAQ_CMAKE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
set(CUDAQ_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/include)
set(CUDAQ_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib)
set(CUDAQ_TOOLS_DIR ${CMAKE_BINARY_DIR}/bin)
list(APPEND CMAKE_MODULE_PATH "${CUDAQ_CMAKE_DIR}")
include(AddCUDAQ)
# Add CUDA Quantum files to the include path
include_directories(BEFORE
${CUDAQ_SOURCE_DIR}/include
${CUDAQ_BINARY_DIR}/include)
# Installing the headers and docs needs to depend on generating any public
# tablegen'd targets.
add_custom_target(cudaq-headers)
set_target_properties(cudaq-headers PROPERTIES FOLDER "Misc")
add_custom_target(cudaq-doc)
# Install
# ==============================================================================
# If building normally, this will add the required
# system headers to the cudaq-quake include paths.
if (NOT CUDAQ_BUILD_RELOCATABLE_PACKAGE)
foreach(dir ${CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES})
set(CUDAQ_CXX_NVQPP_LINK_STR "${CUDAQ_CXX_NVQPP_LINK_STR} -L${dir}")
endforeach()
else()
# If we are building the relocatable package,
# we need to do a bit more for Apple Mac
if (APPLE)
foreach(dir ${CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES})
if (${dir} MATCHES ".*/c.*/v1")
# Copy the bundled c++ includes from theClang install to ours
file(COPY ${dir} DESTINATION include/c++/)
# We want the driver to include the bundled C++ headers from Clang
set(CUDAQ_CXX_NVQPP_INCLUDE_STR "${CUDAQ_CXX_NVQPP_INCLUDE_STR} -I include/c++/v1")
elseif(${dir} MATCHES ".*sdk/usr/include")
# We want the driver to include the SDK/usr/include dir
set(CUDAQ_CXX_NVQPP_INCLUDE_STR "${CUDAQ_CXX_NVQPP_INCLUDE_STR} -I ${dir}")
endif()
endforeach()
endif()
endif()
configure_file("${CMAKE_SOURCE_DIR}/include/nvqpp_config.h.in"
"${CUDAQ_BINARY_DIR}/include/nvqpp_config.h")
install(FILES ${CMAKE_BINARY_DIR}/include/nvqpp_config.h
DESTINATION include/nvqpp)
# Version
# ==============================================================================
if (DEFINED ENV{CUDA_QUANTUM_VERSION})
# The version was defined by the user (likely a bot performing the build), so
# use the value provided as is.
set(CUDA_QUANTUM_VERSION "$ENV{CUDA_QUANTUM_VERSION}")
else()
# Otherwise, create a version based on the nearest tag in the git repo.
execute_process(COMMAND git describe --tags --abbrev=0 --dirty=-developer
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE CUDA_QUANTUM_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
endif()
# Retrieve the commit SHA for full revision description
execute_process(COMMAND git rev-parse --verify HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE CUDA_QUANTUM_COMMIT_SHA OUTPUT_STRIP_TRAILING_WHITESPACE)
configure_file("${CMAKE_SOURCE_DIR}/include/cudaq/Support/Version.h.in"
"${CUDAQ_BINARY_DIR}/include/cudaq/Support/Version.h" @ONLY)
# Check optional dependencies
# ==============================================================================
find_package(OpenMP)
find_package(OpenSSL)
if (NOT CUDAQ_SKIP_MPI)
find_package(MPI COMPONENTS CXX)
if (MPI_FOUND)
message(STATUS "MPI CXX Found: ${MPIEXEC}")
# Build the built-in MPI Comm plugin
add_subdirectory(runtime/cudaq/distributed/builtin)
endif()
endif()
# Third-party libraries (tpls)
# ==============================================================================
set(FMT_INSTALL ON)
cmake_policy(PUSH)
# CMP0146: The FindCUDA module is removed.
# CMake 3.27 and above no longer provide the modules.
# https://cmake.org/cmake/help/latest/policy/CMP0146.html
if(POLICY CMP0146)
cmake_policy(SET CMP0146 OLD)
endif()
add_subdirectory(tpls/fmt)
cmake_policy(POP)
include_directories(SYSTEM tpls/json/include)
# Add spdlog
set(SPDLOG_BUILD_SHARED OFF)
set(SPDLOG_FMT_EXTERNAL ON)
set(SPDLOG_BUILD_PIC ON)
set(SPDLOG_SYSTEM_INCLUDES ON)
add_subdirectory(tpls/spdlog)
target_compile_options(spdlog PRIVATE -Wno-covered-switch-default)
if (OPENSSL_FOUND AND CUDAQ_ENABLE_REST)
# Look for CURL
set(CURL_STATICLIB ON)
set(CURL_USE_STATIC_LIBS TRUE)
find_package(CURL COMPONENTS HTTP HTTPS)
if (CURL_FOUND)
message(STATUS "Using system curl for CPR.")
set(CPR_FORCE_USE_SYSTEM_CURL TRUE)
elseif (CMAKE_USE_SYSTEM_CURL)
message(FATAL_ERROR "CMAKE_USE_SYSTEM_CURL defined but a suitable package was not found. Make sure that `curl-config --protocols` lists HTTP and HTTPS.")
else()
message(STATUS "Curl not found. Building curl for CPR.")
set(CPR_FORCE_USE_SYSTEM_CURL FALSE)
endif()
# Now, build CPR as needed
set(BUILD_SHARED_LIBS OFF)
set(CPR_ENABLE_SSL ON) # needed for https requests
set(CPR_FORCE_OPENSSL_BACKEND ON)
add_subdirectory(tpls/cpr)
target_compile_options(cpr PRIVATE -Wno-covered-switch-default -w)
endif()
# QPP simulator
if (NOT CUDAQ_DISABLE_RUNTIME)
add_subdirectory(tpls/qpp EXCLUDE_FROM_ALL)
# Required workaround to add libqpp as SYSTEM. This will prevent it from
# generating warnings, which we treat as errors.
get_target_property(LIBQPP_INCLUDE libqpp INTERFACE_INCLUDE_DIRECTORIES)
set_target_properties(libqpp
PROPERTIES
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "${LIBQPP_INCLUDE}")
if (OpenMP_CXX_FOUND)
target_compile_definitions(libqpp INTERFACE HAS_OPENMP)
endif()
endif()
if (OPENSSL_FOUND AND CUDAQ_ENABLE_REST)
# The asio submodule doesn't use cmake, so use find_path for it.
find_path(ASIO_INCLUDE_DIR asio.hpp PATHS tpls/asio/asio/include)
add_subdirectory(tpls/Crow)
endif()
# Check for CUDA Support
# ==============================================================================
include(CheckLanguage)
check_language(CUDA)
set(CUDA_FOUND FALSE)
# Generate -gencode arch=compute_XX,code=sm_XX for list of supported
# arch values.
# List should be sorted in increasing order.
function(CUDA_get_gencode_args out_args_string arch_values)
# allow the user to pass the list like a normal variable
set(arch_list ${arch_values} ${ARGN})
set(out "")
foreach(arch IN LISTS arch_list)
set(out "${out} -gencode arch=compute_${arch},code=sm_${arch}")
endforeach(arch)
# Repeat the last one as to ensure the generation of PTX for most
# recent virtual architecture for forward compatibility
list(GET arch_list -1 last_arch)
set(out "${out} -gencode arch=compute_${last_arch},code=compute_${last_arch}")
set(${out_args_string} ${out} PARENT_SCOPE)
endfunction()
if(CMAKE_CUDA_COMPILER)
if (NOT CUDA_TARGET_ARCHS)
# Volta, Ampere, Hopper
set(CUDA_TARGET_ARCHS "70;80;90")
endif()
CUDA_get_gencode_args(CUDA_gencode_flags ${CUDA_TARGET_ARCHS})
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -shared -std=c++17 ${CUDA_gencode_flags} --compiler-options -fPIC")
enable_language(CUDA)
set(CUDA_FOUND TRUE)
set(CMAKE_CUDA_STANDARD 17)
set(CMAKE_CUDA_STANDARD_REQUIRED TRUE)
message(STATUS "Cuda language found.")
endif()
# Code coverage setup
# ==============================================================================
if(CUDAQ_ENABLE_CC)
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
endif()
endif(CUDAQ_ENABLE_CC)
# Directory setup
# ==============================================================================
add_subdirectory(cmake)
add_subdirectory(include)
add_subdirectory(lib)
if (NOT CUDAQ_DISABLE_RUNTIME)
add_subdirectory(runtime)
endif()
add_subdirectory(tools)
add_subdirectory(utils)
if (CUDAQ_ENABLE_PYTHON)
find_package(Python 3 COMPONENTS Interpreter Development)
# Apply specific patch to pybind11 for our documentation.
# Only apply the patch if not already applied.
execute_process(COMMAND ${GIT_EXECUTABLE} -C tpls/pybind11/ apply ../customizations/pybind11/pybind.h.diff --ignore-whitespace --reverse --check
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_PATCH_RESULT
ERROR_QUIET)
if (NOT GIT_PATCH_RESULT EQUAL "0")
execute_process(COMMAND ${GIT_EXECUTABLE} -C tpls/pybind11/ apply ../customizations/pybind11/pybind.h.diff --ignore-whitespace
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
RESULT_VARIABLE GIT_PATCH_RESULT)
endif()
if (NOT GIT_PATCH_RESULT EQUAL "0")
message(FATAL_ERROR "Applying patch to submodule failed with ${GIT_PATCH_RESULT}, please update patch")
endif()
add_subdirectory(tpls/pybind11)
add_subdirectory(python)
endif()
if(CUDAQ_BUILD_TESTS)
umbrella_lit_testsuite_begin(check-all)
set(INSTALL_GTEST OFF)
add_subdirectory(tpls/googletest-src)
# Bug in GCC 12 leads to spurious warnings (-Wrestrict)
# https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105329
if (CMAKE_COMPILER_IS_GNUCXX
AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0.0
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 13.0.0)
target_compile_options(gtest PUBLIC --param=evrp-mode=legacy)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$")
target_compile_options(gtest PUBLIC -Wno-covered-switch-default)
endif()
include(GoogleTest)
include(CTest)
add_subdirectory(test)
if (NOT CUDAQ_DISABLE_RUNTIME)
add_subdirectory(targettests)
add_subdirectory(unittests)
add_subdirectory(docs)
endif()
umbrella_lit_testsuite_end(check-all)
endif()
if (CUDAQ_BUILD_RELOCATABLE_PACKAGE AND NOT CUDAQ_DISABLE_CPP_FRONTEND)
# Grab the Clang system headers / resource-dir
install(DIRECTORY ${LLVM_BINARY_DIR}/lib/clang/ DESTINATION lib/clang)
# Install the executables we need
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/llc DESTINATION bin)
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/lld DESTINATION bin)
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/ld.lld DESTINATION bin)
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/clang-${CUDAQ_LLVM_VERSION} DESTINATION bin)
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/clang DESTINATION bin)
install(PROGRAMS ${LLVM_BINARY_DIR}/bin/clang++ DESTINATION bin)
endif()
if (CUDAQ_EXTERNAL_NVQIR_SIMS)
include(WheelBuildHelpers)
while(CUDAQ_EXTERNAL_NVQIR_SIMS)
list(POP_FRONT CUDAQ_EXTERNAL_NVQIR_SIMS LIB_SO_OR_CONFIG_FILE)
add_target_libs_to_wheel(${LIB_SO_OR_CONFIG_FILE})
endwhile()
endif()