|
| 1 | +# This file defines the `cccl_build_compiler_targets()` function, which |
| 2 | +# creates the following interface targets: |
| 3 | +# |
| 4 | +# cccl.compiler_interface |
| 5 | +# - Interface target providing compiler-specific options needed to build |
| 6 | +# CCCL's tests, examples, etc. This includes warning flags and the like. |
| 7 | +# |
| 8 | +# cccl.compiler_interface_cppXX |
| 9 | +# - Interface targets providing compiler-specific options that should only be |
| 10 | +# applied to certain dialects of C++. Includes `compiler_interface`, and will |
| 11 | +# be defined for each supported dialect. |
| 12 | +# |
| 13 | +# cccl.silence_unreachable_code_warnings |
| 14 | +# - Interface target that silences unreachable code warnings. |
| 15 | +# - Used to selectively disable such warnings in unit tests caused by |
| 16 | +# unconditionally thrown exceptions. |
| 17 | + |
| 18 | +set(CCCL_KNOWN_CXX_DIALECTS 11 14 17 20) |
| 19 | + |
| 20 | +# sccache cannot handle the -Fd option generating pdb files |
| 21 | +set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT Embedded) |
| 22 | + |
| 23 | +function(cccl_build_compiler_interface interface_target cuda_compile_options cxx_compile_options compile_defs) |
| 24 | + add_library(${interface_target} INTERFACE) |
| 25 | + |
| 26 | + foreach (cuda_option IN LISTS cuda_compile_options) |
| 27 | + target_compile_options(${interface_target} INTERFACE |
| 28 | + $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:${cuda_option}> |
| 29 | + ) |
| 30 | + endforeach() |
| 31 | + |
| 32 | + foreach (cxx_option IN LISTS cxx_compile_options) |
| 33 | + target_compile_options(${interface_target} INTERFACE |
| 34 | + $<$<COMPILE_LANGUAGE:CXX>:${cxx_option}> |
| 35 | + $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=${cxx_option}> |
| 36 | + ) |
| 37 | + endforeach() |
| 38 | + |
| 39 | + target_compile_definitions(${interface_target} INTERFACE |
| 40 | + ${compile_defs} |
| 41 | + ) |
| 42 | +endfunction() |
| 43 | + |
| 44 | +function(cccl_build_compiler_targets) |
| 45 | + set(cuda_compile_options) |
| 46 | + set(cxx_compile_options) |
| 47 | + set(cxx_compile_definitions) |
| 48 | + |
| 49 | + list(APPEND cuda_compile_definitions "-Xcudafe=--display_error_number") |
| 50 | + list(APPEND cuda_compile_definitions "-Xcudafe=--promote_warnings") |
| 51 | + list(APPEND cuda_compile_definitions "-Wno-deprecated-gpu-targets") |
| 52 | + |
| 53 | + # Ensure that we build our tests without treating ourself as system header |
| 54 | + list(APPEND cxx_compile_definitions "_CCCL_NO_SYSTEM_HEADER") |
| 55 | + |
| 56 | + if ("MSVC" STREQUAL "${CMAKE_CXX_COMPILER_ID}") |
| 57 | + list(APPEND cuda_compile_options "--use-local-env") |
| 58 | + list(APPEND cxx_compile_options "/bigobj") |
| 59 | + list(APPEND cxx_compile_definitions "_ENABLE_EXTENDED_ALIGNED_STORAGE") |
| 60 | + list(APPEND cxx_compile_definitions "NOMINMAX") |
| 61 | + |
| 62 | + append_option_if_available("/W4" cxx_compile_options) |
| 63 | + # Treat all warnings as errors. This is only supported on Release builds, |
| 64 | + # as `nv_exec_check_disable` doesn't seem to work with MSVC debug iterators |
| 65 | + # and spurious warnings are emitted. |
| 66 | + # See NVIDIA/thrust#1273, NVBug 3129879. |
| 67 | + if (CMAKE_BUILD_TYPE STREQUAL "Release") |
| 68 | + append_option_if_available("/WX" cxx_compile_options) |
| 69 | + endif() |
| 70 | + |
| 71 | + # Suppress overly-pedantic/unavoidable warnings brought in with /W4: |
| 72 | + # C4324: structure was padded due to alignment specifier |
| 73 | + append_option_if_available("/wd4324" cxx_compile_options) |
| 74 | + # C4505: unreferenced local function has been removed |
| 75 | + # The CUDA `host_runtime.h` header emits this for |
| 76 | + # `__cudaUnregisterBinaryUtil`. |
| 77 | + append_option_if_available("/wd4505" cxx_compile_options) |
| 78 | + # C4706: assignment within conditional expression |
| 79 | + # MSVC doesn't provide an opt-out for this warning when the assignment is |
| 80 | + # intentional. Clang will warn for these, but suppresses the warning when |
| 81 | + # double-parentheses are used around the assignment. We'll let Clang catch |
| 82 | + # unintentional assignments and suppress all such warnings on MSVC. |
| 83 | + append_option_if_available("/wd4706" cxx_compile_options) |
| 84 | + |
| 85 | + # Disabled loss-of-data conversion warnings. |
| 86 | + # append_option_if_available("/wd4244" cxx_compile_options) |
| 87 | + |
| 88 | + # Disable warning about applying unary operator- to unsigned type. |
| 89 | + # append_option_if_available("/wd4146" cxx_compile_options) |
| 90 | + |
| 91 | + # MSVC STL assumes that `allocator_traits`'s allocator will use raw pointers, |
| 92 | + # and the `__DECLSPEC_ALLOCATOR` macro causes issues with thrust's universal |
| 93 | + # allocators: |
| 94 | + # warning C4494: 'std::allocator_traits<_Alloc>::allocate' : |
| 95 | + # Ignoring __declspec(allocator) because the function return type is not |
| 96 | + # a pointer or reference |
| 97 | + # See https://github.com/microsoft/STL/issues/696 |
| 98 | + append_option_if_available("/wd4494" cxx_compile_options) |
| 99 | + |
| 100 | + else() |
| 101 | + list(APPEND cuda_compile_options "-Wreorder") |
| 102 | + |
| 103 | + append_option_if_available("-Werror" cxx_compile_options) |
| 104 | + append_option_if_available("-Wall" cxx_compile_options) |
| 105 | + append_option_if_available("-Wextra" cxx_compile_options) |
| 106 | + append_option_if_available("-Wreorder" cxx_compile_options) |
| 107 | + append_option_if_available("-Winit-self" cxx_compile_options) |
| 108 | + append_option_if_available("-Woverloaded-virtual" cxx_compile_options) |
| 109 | + append_option_if_available("-Wcast-qual" cxx_compile_options) |
| 110 | + append_option_if_available("-Wpointer-arith" cxx_compile_options) |
| 111 | + append_option_if_available("-Wunused-local-typedef" cxx_compile_options) |
| 112 | + append_option_if_available("-Wvla" cxx_compile_options) |
| 113 | + |
| 114 | + # Disable GNU extensions (flag is clang only) |
| 115 | + append_option_if_available("-Wgnu" cxx_compile_options) |
| 116 | + append_option_if_available("-Wno-gnu-line-marker" cxx_compile_options) # WAR 3916341 |
| 117 | + # Calling a variadic macro with zero args is a GNU extension until C++20, |
| 118 | + # but the THRUST_PP_ARITY macro is used with zero args. Need to see if this |
| 119 | + # is a real problem worth fixing. |
| 120 | + append_option_if_available("-Wno-gnu-zero-variadic-macro-arguments" cxx_compile_options) |
| 121 | + |
| 122 | + # This complains about functions in CUDA system headers when used with nvcc. |
| 123 | + append_option_if_available("-Wno-unused-function" cxx_compile_options) |
| 124 | + endif() |
| 125 | + |
| 126 | + if ("GNU" STREQUAL "${CMAKE_CXX_COMPILER_ID}") |
| 127 | + if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 7.3) |
| 128 | + # GCC 7.3 complains about name mangling changes due to `noexcept` |
| 129 | + # becoming part of the type system; we don't care. |
| 130 | + append_option_if_available("-Wno-noexcept-type" cxx_compile_options) |
| 131 | + endif() |
| 132 | + endif() |
| 133 | + |
| 134 | + if ("Intel" STREQUAL "${CMAKE_CXX_COMPILER_ID}") |
| 135 | + # Do not flush denormal floats to zero |
| 136 | + append_option_if_available("-no-ftz" cxx_compile_options) |
| 137 | + # Disable warning that inlining is inhibited by compiler thresholds. |
| 138 | + append_option_if_available("-diag-disable=11074" cxx_compile_options) |
| 139 | + append_option_if_available("-diag-disable=11076" cxx_compile_options) |
| 140 | + # Disable warning about deprecated classic compiler |
| 141 | + append_option_if_available("-diag-disable=10441" cxx_compile_options) |
| 142 | + endif() |
| 143 | + |
| 144 | + cccl_build_compiler_interface(cccl.compiler_interface |
| 145 | + "${cuda_compile_options}" |
| 146 | + "${cxx_compile_options}" |
| 147 | + "${cxx_compile_definitions}" |
| 148 | + ) |
| 149 | + |
| 150 | + # These targets are used for dialect-specific options: |
| 151 | + foreach (dialect IN LISTS CCCL_KNOWN_CXX_DIALECTS) |
| 152 | + add_library(cccl.compiler_interface_cpp${dialect} INTERFACE) |
| 153 | + target_link_libraries(cccl.compiler_interface_cpp${dialect} INTERFACE cccl.compiler_interface) |
| 154 | + endforeach() |
| 155 | + |
| 156 | + if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 157 | + # C4127: conditional expression is constant |
| 158 | + # Disable this MSVC warning for C++11/C++14. In C++17+, we can use |
| 159 | + # _CCCL_IF_CONSTEXPR to address these warnings. |
| 160 | + target_compile_options(cccl.compiler_interface_cpp11 INTERFACE |
| 161 | + $<$<COMPILE_LANGUAGE:CXX>:/wd4127> |
| 162 | + $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4127> |
| 163 | + ) |
| 164 | + target_compile_options(cccl.compiler_interface_cpp14 INTERFACE |
| 165 | + $<$<COMPILE_LANGUAGE:CXX>:/wd4127> |
| 166 | + $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4127> |
| 167 | + ) |
| 168 | + endif() |
| 169 | + |
| 170 | + # Some of our unit tests unconditionally throw exceptions, and compilers will |
| 171 | + # detect that the following instructions are unreachable. This is intentional |
| 172 | + # and unavoidable in these cases. This target can be used to silence |
| 173 | + # unreachable code warnings. |
| 174 | + add_library(cccl.silence_unreachable_code_warnings INTERFACE) |
| 175 | + if (CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") |
| 176 | + target_compile_options(cccl.silence_unreachable_code_warnings INTERFACE |
| 177 | + $<$<COMPILE_LANGUAGE:CXX>:/wd4702> |
| 178 | + $<$<COMPILE_LANG_AND_ID:CUDA,NVIDIA>:-Xcompiler=/wd4702> |
| 179 | + ) |
| 180 | + endif() |
| 181 | +endfunction() |
0 commit comments