From e9e7ca44e240da91ff0633d19484419c471c25f1 Mon Sep 17 00:00:00 2001 From: Patryk Kaminski Date: Mon, 10 Jul 2023 13:29:10 +0200 Subject: [PATCH] Set global compile flags for UR targets only Currently, some of compile flags are set globally for the whole project and third-party libraries. Wrap all Unified Runtime CMake targets so that compile and linking flags can be added globally but without affecting third-party repositories' builds. --- CMakeLists.txt | 98 ++++++++++++++++++++++----------------------- cmake/helpers.cmake | 52 +++++++++++++++++++++++- 2 files changed, 99 insertions(+), 51 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7b03bb00f..49daec1f7a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,39 +48,6 @@ if(MSVC) set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$) endif() -# CXX flags setup -if(NOT MSVC) - add_compile_options(-fPIC -Wall -Wpedantic - $<$:-fdiagnostics-color=always> - $<$:-fcolor-diagnostics>) - if(UR_DEVELOPER_MODE) - add_compile_options(-Werror -fno-omit-frame-pointer) - endif() - # Determine if libstdc++ is being used. - check_cxx_source_compiles(" - #include - #ifndef __GLIBCXX__ - #error not using libstdc++ - #endif - int main() {}" - USING_LIBSTDCXX) - if(USING_LIBSTDCXX) - # Support older versions of GCC where the header is not - # available and must be used instead. This - # requires linking against libstdc++fs.a, on systems where - # is available we still need to link this library. - link_libraries(stdc++fs) - endif() -elseif(MSVC) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") - add_compile_options(/MP /W3 /MD$<$:d>) - - if(UR_DEVELOPER_MODE) - add_compile_options(/WX) - endif() -endif() - if(UR_ENABLE_TRACING) add_compile_definitions(UR_ENABLE_TRACING) @@ -119,22 +86,6 @@ if(UR_ENABLE_TRACING) endif() endif() -if(UR_USE_ASAN) - add_sanitizer_flag(address) -endif() - -if(UR_USE_UBSAN) - add_sanitizer_flag(undefined) -endif() - -if(UR_USE_MSAN) - message(WARNING "MemorySanitizer requires that all code is built with - its instrumentation, otherwise false positives are possible. - See https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo#instrumented-libc - for details") - add_sanitizer_flag(memory) -endif() - # Allow custom third_party folder if(NOT DEFINED THIRD_PARTY_DIR) set(THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party) @@ -209,6 +160,55 @@ if(UR_BUILD_TOOLS) add_subdirectory(tools) endif() +# CXX flags setup +if(NOT MSVC) + add_compile_flags_to_native_targets(-fPIC -Wall -Wpedantic + $<$:-fdiagnostics-color=always> + $<$:-fcolor-diagnostics>) + if(UR_DEVELOPER_MODE) + add_compile_flags_to_native_targets(-Werror -fno-omit-frame-pointer) + endif() + # Determine if libstdc++ is being used. + check_cxx_source_compiles(" + #include + #ifndef __GLIBCXX__ + #error not using libstdc++ + #endif + int main() {}" + USING_LIBSTDCXX) + if(USING_LIBSTDCXX) + # Support older versions of GCC where the header is not + # available and must be used instead. This + # requires linking against libstdc++fs.a, on systems where + # is available we still need to link this library. + link_libraries(stdc++fs) + endif() +elseif(MSVC) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP") + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP") + add_compile_flags_to_native_targets(/MP /W3 /MD$<$:d>) + + if(UR_DEVELOPER_MODE) + add_compile_flags_to_native_targets(/WX) + endif() +endif() + +if(UR_USE_ASAN) + add_sanitizer_flag(address) +endif() + +if(UR_USE_UBSAN) + add_sanitizer_flag(undefined) +endif() + +if(UR_USE_MSAN) + message(WARNING "MemorySanitizer requires that all code is built with + its instrumentation, otherwise false positives are possible. + See https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo#instrumented-libc + for details") + add_sanitizer_flag(memory) +endif() + # Add the list of installed targets to the install. This includes the namespace # which all installed targets will be prefixed with, e.g. for the headers # target users will depend on ${PROJECT_NAME}::headers. diff --git a/cmake/helpers.cmake b/cmake/helpers.cmake index 4651cba59d..a76706e560 100644 --- a/cmake/helpers.cmake +++ b/cmake/helpers.cmake @@ -40,6 +40,54 @@ function(add_cppformat name) add_dependencies(cppformat cppformat-${name}) endfunction() +macro(set_ur_targets_list) + set(UR_TARGETS + collector + disjoint_pool + hello_world + test-context + test-device + test-event + test-loader-platforms + test-memory + test-params + test-platform + test-queue + test-sampler + test-usm + test-virtual_memory + umf_test-base + umf_test-disjointPool + umf_test-disjointPoolConfigParser + umf_test-memoryPool + umf_test-memoryProvider + umf_test_helpers + ur_adapter_null + ur_collector + ur_loader + usm_test-usmPoolManager + validation_test-leaks + validation_test-leaks_mt + validation_test-parameters + ) +endmacro() + +function(add_compile_flags_to_native_targets) + set_ur_targets_list() + set(flags ${ARGV}) + foreach(ur_target ${UR_TARGETS}) + target_compile_options(${ur_target} PRIVATE ${flags}) + endforeach() +endfunction() + +function(add_link_options_to_native_targets) + set_ur_targets_list() + set(options ${ARGV}) + foreach(ur_target ${UR_TARGETS}) + target_link_options(${ur_target} PRIVATE ${options}) + endforeach() +endfunction() + include(CheckCXXCompilerFlag) macro(add_sanitizer_flag flag) @@ -48,8 +96,8 @@ macro(add_sanitizer_flag flag) check_cxx_compiler_flag("-fsanitize=${flag}" CXX_HAS_SANITIZER) if(CXX_HAS_SANITIZER) - add_compile_options(-fsanitize=${flag}) - add_link_options(-fsanitize=${flag}) + add_compile_flags_to_native_targets(-fsanitize=${flag}) + add_link_options_to_native_targets(-fsanitize=${flag}) else() message("${flag} sanitizer not supported") endif()