diff --git a/CMakeLists.txt b/CMakeLists.txt index 80a9f64ea7..fbf9947688 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,9 @@ option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace set(UR_DPCXX "" CACHE FILEPATH "Path of the DPC++ compiler executable") set(UR_SYCL_LIBRARY_DIR "" CACHE PATH "Path of the SYCL runtime library directory") +option(UR_ENABLE_ASSERTIONS "Enable assertions for all build types" OFF) + +include(Assertions) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) diff --git a/cmake/Assertions.cmake b/cmake/Assertions.cmake new file mode 100644 index 0000000000..9d8f6c0f26 --- /dev/null +++ b/cmake/Assertions.cmake @@ -0,0 +1,30 @@ +# From the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +# See https://llvm.org/LICENSE.txt for license information. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +# This is lifted from llvm's LLVM_ENABLE_ASSERTIONS implementation +# https://github.com/llvm/llvm-project/blob/6be0e979896f7dd610abf263f845c532f1be3762/llvm/cmake/modules/HandleLLVMOptions.cmake#L89 +if(UR_ENABLE_ASSERTIONS) + # MSVC doesn't like _DEBUG on release builds + if( NOT MSVC ) + add_compile_definitions(_DEBUG) + endif() + # On non-Debug builds cmake automatically defines NDEBUG, so we + # explicitly undefine it: + if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) + add_compile_options($<$,$>:-UNDEBUG>) + if (MSVC) + # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines. + foreach (flags_var_to_scrub + CMAKE_CXX_FLAGS_RELEASE + CMAKE_CXX_FLAGS_RELWITHDEBINFO + CMAKE_CXX_FLAGS_MINSIZEREL + CMAKE_C_FLAGS_RELEASE + CMAKE_C_FLAGS_RELWITHDEBINFO + CMAKE_C_FLAGS_MINSIZEREL) + string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " + "${flags_var_to_scrub}" "${${flags_var_to_scrub}}") + endforeach() + endif() + endif() +endif()