Skip to content

Commit

Permalink
Set global compile flags for UR targets only
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
PatKamin committed Jul 10, 2023
1 parent 0ae3b04 commit e9e7ca4
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 51 deletions.
98 changes: 49 additions & 49 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,6 @@ if(MSVC)
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
endif()

# CXX flags setup
if(NOT MSVC)
add_compile_options(-fPIC -Wall -Wpedantic
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
$<$<CXX_COMPILER_ID:Clang>:-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 <array>
#ifndef __GLIBCXX__
#error not using libstdc++
#endif
int main() {}"
USING_LIBSTDCXX)
if(USING_LIBSTDCXX)
# Support older versions of GCC where the <filesystem> header is not
# available and <experimental/filesystem> must be used instead. This
# requires linking against libstdc++fs.a, on systems where <filesystem>
# 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$<$<CONFIG:Debug>:d>)

if(UR_DEVELOPER_MODE)
add_compile_options(/WX)
endif()
endif()

if(UR_ENABLE_TRACING)
add_compile_definitions(UR_ENABLE_TRACING)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
$<$<CXX_COMPILER_ID:Clang>:-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 <array>
#ifndef __GLIBCXX__
#error not using libstdc++
#endif
int main() {}"
USING_LIBSTDCXX)
if(USING_LIBSTDCXX)
# Support older versions of GCC where the <filesystem> header is not
# available and <experimental/filesystem> must be used instead. This
# requires linking against libstdc++fs.a, on systems where <filesystem>
# 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$<$<CONFIG:Debug>: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.
Expand Down
52 changes: 50 additions & 2 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand Down

0 comments on commit e9e7ca4

Please sign in to comment.