1+
2+ # Force-set internal cache variable "MLIR_TRT_APPLY_EXTRA_CHECKS" to
3+ # the result of the expression
4+ # (MLIR_TRT_ENABLE_EXTRA_CHECKS AND (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")).
5+ # This is done to avoid repeated checks when we populate target compilation options.
6+ set (MLIR_TRT_APPLY_EXTRA_CHECKS FALSE CACHE INTERNAL "" )
7+ if (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" AND MLIR_TRT_ENABLE_EXTRA_CHECKS)
8+ set (MLIR_TRT_APPLY_EXTRA_CHECKS TRUE CACHE INTERNAL "" )
9+ endif ()
10+
111if (MLIR_TRT_ENABLE_WERROR)
212 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror" )
313 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror" )
414 set (CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Wno-error" )
515endif ()
616
7- function (append_c_cxx_flags)
17+ # ------------------------------------------------------------------------------
18+ # Applies additional compilation options to a C/CXX library or executable
19+ # target. This only has an effect if MLIR_TRT_ENABLE_EXTRA_CHECKS is ON.
20+ # ------------------------------------------------------------------------------
21+
22+ macro (mtrt_apply_extra_checks_to_target_helper target )
23+ target_compile_options (${target} PRIVATE
24+ -Wmissing-declarations
25+ -Wmissing-prototypes
26+ -Wunused
27+ -fstrict-flex-arrays=3
28+ )
29+ endmacro ()
30+
31+ macro (mtrt_apply_extra_check_options target )
32+ if (MLIR_TRT_APPLY_EXTRA_CHECKS)
33+ if (TARGET "obj.${target} " )
34+ mtrt_apply_extra_checks_to_target_helper("obj.${target} " )
35+ else ()
36+ mtrt_apply_extra_checks_to_target_helper("${target} " )
37+ endif ()
38+ endif ()
39+ endmacro ()
40+
41+ # ------------------------------------------------------------------------------
42+ # Updates global C/CXX library or executable compilation flags.
43+ # This only has an effect if MLIR_TRT_ENABLE_EXTRA_CHECKS is ON.
44+ #
45+ # Some flags enable additional warnings, others may enable additional runtime
46+ # safety or preconditions checks.
47+ #
48+ # For a description of each flag, see the below reference:
49+ # https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
50+ # ------------------------------------------------------------------------------
51+ function (mtrt_update_global_c_cxx_flags)
852 string (JOIN " " flags ${ARGN} )
953 set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flags} " PARENT_SCOPE)
1054 set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flags} " PARENT_SCOPE)
1155endfunction ()
1256
13- # Enable additional warnings and runtime checks recommended
14- # by https://best.openssf.org/Compiler-Hardening-Guides/Compiler-Options-Hardening-Guide-for-C-and-C++.html
15- if (MLIR_TRT_ENABLE_EXTRA_CHECKS AND
16- (CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang" ))
57+ if (MLIR_TRT_APPLY_EXTRA_CHECKS)
1758 # FORTIFY_SOURCE=3 requires O1 or higher.
18- # GCC has default FORTIFY_SOURCE=2.
19- if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND
20- (NOT CMAKE_BUILD_TYPE STREQUAL "Debug" ))
59+ # GCC has default FORTIFY_SOURCE=2 and would require `-U_FORTIFY_SOURCE`
60+ # to change the value, so we just set it to 3 when using clang.
61+ if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug"
62+ AND CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
2163 add_compile_definitions (
2264 _FORTIFY_SOURCE=3
2365 )
2466 endif ()
2567
26- # Note: '-D_GLIBCXX_ASSERTIONS' already added by
27- # the 'HandleLLVMOptions'.
28-
29- append_c_cxx_flags(
30- # Enforce that all global functions have declarations. This catches
31- # missing `static` qualifiers on functions which are intended to be TU-local.
32- -Wmissing-declarations
33- -Wmissing-prototypes
34- -Wunused
68+ mtrt_update_global_c_cxx_flags(
3569 # Make template instantiation errors more readable.
3670 -fdiagnostics-show-template-tree
3771 # Enable better bounds checking for trailing array members.
38- # Value is 0 to 3, with 3 being the most strict. LLVM has some code that
39- # only allows us to use 1 here.
4072 -fstrict-flex-arrays=1
4173 # Enable runtime checks for variable-size stack allocation validity.
4274 -fstack-clash-protection
@@ -48,25 +80,61 @@ if(MLIR_TRT_ENABLE_EXTRA_CHECKS AND
4880 -Werror=int-conversion
4981 )
5082
51- if (CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64" OR
52- CMAKE_SYSTEM_PROCESSOR STREQUAL "amd64" )
53- append_c_cxx_flags(
83+ if (CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64" )
84+ mtrt_update_global_c_cxx_flags(
5485 -fcf-protection=full
55- )
56- endif ()
57-
58- if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" )
59- append_c_cxx_flags(
6086 -Wself-assign
6187 )
6288 endif ()
6389endif ()
6490
65- # The CUDA headers will trigger excessive "C++20 extensions warning" with
66- # clang19+ in macro usage.
67- include (CheckCXXCompilerFlag)
68- check_cxx_compiler_flag("-Wc++20-extensions"
69- CXX_SUPPORTS_C20_EXTENSIONS_FLAG)
70- if (CXX_SUPPORTS_C20_EXTENSIONS_FLAG)
71- add_compile_options (-Wno-c++20-extensions)
91+ # This ensures that `__FILE__` in log printing expands to a relative path and file paths
92+ # in debug info will expand relative to the source or binary directories.
93+ #
94+ #
95+ # It adds the following global compilation flag:
96+ # '-fno-canonical-prefixes'
97+ # '-ffile-prefix-map=[project_src_root]=.'
98+ # '-fdebug-prefix-map=[project_src_root]=.'
99+ # In addition, this flag is added if the build directory is not a subdirectory of the source directory:
100+ # '-ffile-prefix-map=[project_binary_root]=build'.
101+ # '-fdebug-prefix-map=[project_binary_root]=build'.
102+ #
103+ # This is only applied if the project is the top-level project and MLIR_TRT_RELATIVE_DEBUG_PATHS is ON.
104+ function (mtrt_apply_relative_path_options)
105+ if (NOT COMMAND mtrt_append_compiler_flag_if_supported)
106+ message (FATAL_ERROR "MTRTCompilationOptions must be included after MTRTCMakeExtras" )
107+ endif ()
108+
109+ # In CMake, you can't know where the "cmake" command is invoked from, so we
110+ # just assume that we want make paths relative to the source directory.
111+ set (relative_src "." )
112+
113+ # The following checks if the source directory is a prefix of the binary directory.
114+ cmake_path(IS_PREFIX CMAKE_SOURCE_DIR "${CMAKE_BINARY_DIR} " src_dir_is_prefix_of_bin_dir)
115+
116+ set (debug_remaps "-fdebug-prefix-map=${CMAKE_SOURCE_DIR} =." )
117+ set (file_remaps "-ffile-prefix-map=${CMAKE_SOURCE_DIR} =." )
118+ if (src_dir_is_prefix_of_bin_dir)
119+ set (debug_remaps "${debug_remaps} -fdebug-prefix-map=${CMAKE_BINARY_DIR} =build" )
120+ set (file_remaps "${file_remaps} -ffile-prefix-map=${CMAKE_BINARY_DIR} =build" )
121+ endif ()
122+
123+ mtrt_append_compiler_flag_if_supported(
124+ CHECK "-fdebug-prefix-map=foo=bar"
125+ APPEND "${debug_remaps} "
126+ )
127+
128+ mtrt_append_compiler_flag_if_supported(
129+ CHECK "-ffile-prefix-map=foo=bar"
130+ APPEND "${file_remaps} "
131+ )
132+ mtrt_append_compiler_flag_if_supported(
133+ CHECK "-no-canonical-prefixes"
134+ APPEND "-no-canonical-prefixes"
135+ )
136+ endfunction ()
137+
138+ if (PROJECT_IS_TOP_LEVEL AND MLIR_TRT_RELATIVE_DEBUG_PATHS)
139+ mtrt_apply_relative_path_options()
72140endif ()
0 commit comments