Skip to content

Commit fb83446

Browse files
authored
Merge pull request #1025 from aarongreig/aaron/cmakeAssertionsFlag
Add UR_ENABLE_ASSERTIONS flag for enabling asserts on all build types.
2 parents deb9491 + da145aa commit fb83446

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace
4747
set(UR_DPCXX "" CACHE FILEPATH "Path of the DPC++ compiler executable")
4848
set(UR_SYCL_LIBRARY_DIR "" CACHE PATH
4949
"Path of the SYCL runtime library directory")
50+
option(UR_ENABLE_ASSERTIONS "Enable assertions for all build types" OFF)
51+
52+
include(Assertions)
5053

5154
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
5255
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

cmake/Assertions.cmake

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# From the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2+
# See https://llvm.org/LICENSE.txt for license information.
3+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
# This is lifted from llvm's LLVM_ENABLE_ASSERTIONS implementation
6+
# https://github.com/llvm/llvm-project/blob/6be0e979896f7dd610abf263f845c532f1be3762/llvm/cmake/modules/HandleLLVMOptions.cmake#L89
7+
if(UR_ENABLE_ASSERTIONS)
8+
# MSVC doesn't like _DEBUG on release builds
9+
if( NOT MSVC )
10+
add_compile_definitions(_DEBUG)
11+
endif()
12+
# On non-Debug builds cmake automatically defines NDEBUG, so we
13+
# explicitly undefine it:
14+
if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
15+
add_compile_options($<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:-UNDEBUG>)
16+
if (MSVC)
17+
# Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
18+
foreach (flags_var_to_scrub
19+
CMAKE_CXX_FLAGS_RELEASE
20+
CMAKE_CXX_FLAGS_RELWITHDEBINFO
21+
CMAKE_CXX_FLAGS_MINSIZEREL
22+
CMAKE_C_FLAGS_RELEASE
23+
CMAKE_C_FLAGS_RELWITHDEBINFO
24+
CMAKE_C_FLAGS_MINSIZEREL)
25+
string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
26+
"${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
27+
endforeach()
28+
endif()
29+
endif()
30+
endif()

0 commit comments

Comments
 (0)