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
including third-party libraries.
Introduce wrapper functions for add_executable and add_library
CMake functions in order to add compile flags only where needed.
  • Loading branch information
PatKamin committed Jul 13, 2023
1 parent 566169d commit 360746c
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 33 deletions.
15 changes: 0 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,7 @@ 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,AppleClang>:-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>
Expand All @@ -75,14 +68,6 @@ if(NOT MSVC)
# 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)
Expand Down
38 changes: 38 additions & 0 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,44 @@ macro(add_sanitizer_flag flag)
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
endmacro()

function(add_ur_target_compile_options name)
if(NOT MSVC)
target_compile_options(${name} PRIVATE
-fPIC
-Wall
-Wpedantic
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>
)
if(UR_DEVELOPER_MODE)
target_compile_options(${name} PRIVATE
-Werror
-fno-omit-frame-pointer
)
endif()
elseif(MSVC)
target_compile_options(${name} PRIVATE
/MP
/W3
/MD$<$<CONFIG:Debug>:d>
)

if(UR_DEVELOPER_MODE)
target_compile_options(${name} PRIVATE /WX)
endif()
endif()
endfunction()

function(add_ur_executable name)
add_executable(${name} ${ARGN})
add_ur_target_compile_options(${name})
endfunction()

function(add_ur_library name)
add_library(${name} ${ARGN})
add_ur_target_compile_options(${name})
endfunction()

include(FetchContent)

# A wrapper around FetchContent_Declare that supports git sparse checkout.
Expand Down
2 changes: 1 addition & 1 deletion examples/collector/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set(TARGET_NAME collector)

add_library(${TARGET_NAME} SHARED
add_ur_library(${TARGET_NAME} SHARED
${CMAKE_CURRENT_SOURCE_DIR}/collector.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set(TARGET_NAME hello_world)

add_executable(${TARGET_NAME}
add_ur_executable(${TARGET_NAME}
${CMAKE_CURRENT_SOURCE_DIR}/hello_world.cpp
)

Expand Down
2 changes: 1 addition & 1 deletion source/adapters/null/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set(TARGET_NAME ur_adapter_null)

add_library(${TARGET_NAME}
add_ur_library(${TARGET_NAME}
SHARED
${CMAKE_CURRENT_SOURCE_DIR}/ur_null.hpp
${CMAKE_CURRENT_SOURCE_DIR}/ur_null.cpp
Expand Down
2 changes: 1 addition & 1 deletion source/common/umf_pools/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_library(disjoint_pool STATIC
add_ur_library(disjoint_pool STATIC
disjoint_pool.cpp
disjoint_pool_config_parser.cpp
)
Expand Down
4 changes: 2 additions & 2 deletions source/common/unified_malloc_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ if(UMF_BUILD_SHARED_LIBRARY)
message(WARNING "Unified Malloc Framework is still an early work in progress."
"There are no API/ABI backward compatibility guarantees. There will be breakages."
"Do not use the shared library in production software.")
add_library(unified_malloc_framework SHARED
add_ur_library(unified_malloc_framework SHARED
${UMF_SOURCES})
target_compile_definitions(unified_malloc_framework PUBLIC UMF_SHARED_LIBRARY)
else()
add_library(unified_malloc_framework STATIC
add_ur_library(unified_malloc_framework STATIC
${UMF_SOURCES})
endif()

Expand Down
2 changes: 1 addition & 1 deletion source/loader/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ configure_file(
@ONLY
)

add_library(ur_loader
add_ur_library(ur_loader
SHARED
""
${CMAKE_CURRENT_BINARY_DIR}/UrLoaderVersion.rc
Expand Down
2 changes: 1 addition & 1 deletion test/conformance/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(UR_CONFORMANCE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})

function(add_conformance_test name)
set(TEST_TARGET_NAME test-${name})
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${ARGN}
${UR_CONFORMANCE_TEST_DIR}/source/environment.cpp
${UR_CONFORMANCE_TEST_DIR}/source/main.cpp)
Expand Down
2 changes: 1 addition & 1 deletion test/conformance/testing/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_library(ur_testing STATIC
add_ur_library(ur_testing STATIC
source/utils.cpp)
target_include_directories(ur_testing PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_link_libraries(ur_testing PRIVATE gtest_main unified-runtime::headers)
Expand Down
2 changes: 1 addition & 1 deletion test/layers/validation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(UR_VALIDATION_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})

function(add_validation_test name)
set(TEST_TARGET_NAME validation_test-${name})
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${ARGN})
target_link_libraries(${TEST_TARGET_NAME}
PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion test/loader/adapter_registry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function(add_adapter_reg_search_test name)
cmake_parse_arguments(TEST "" "" "SEARCH_PATH;ENVS;SOURCES" ${ARGN})

set(TEST_TARGET_NAME adapter-reg-test-${name})
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${TEST_SOURCES})

if(WIN32)
Expand Down
2 changes: 1 addition & 1 deletion test/loader/platforms/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

add_executable(test-loader-platforms
add_ur_executable(test-loader-platforms
platforms.cpp
)

Expand Down
4 changes: 2 additions & 2 deletions test/unified_malloc_framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set(UR_UMF_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})

add_library(umf_test_helpers STATIC
add_ur_library(umf_test_helpers STATIC
${UR_UMF_TEST_DIR}/common/pool.c
${UR_UMF_TEST_DIR}/common/provider.c
)
Expand All @@ -14,7 +14,7 @@ target_link_libraries(umf_test_helpers PRIVATE ${PROJECT_NAME}::common)

function(add_umf_test name)
set(TEST_TARGET_NAME umf_test-${name})
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${ARGN})
target_link_libraries(${TEST_TARGET_NAME}
PRIVATE
Expand Down
2 changes: 1 addition & 1 deletion test/unit/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ target_include_directories(unit_tests_helpers INTERFACE
function(add_unit_test name)
set(TEST_TARGET_NAME test-${name})

add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${ARGN}
)
target_link_libraries(${TEST_TARGET_NAME}
Expand Down
2 changes: 1 addition & 1 deletion test/unit/logger/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_unit_test(logger
)

set(TEST_TARGET_NAME test-logger_env_var)
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
env_var.cpp
)
target_link_libraries(${TEST_TARGET_NAME}
Expand Down
2 changes: 1 addition & 1 deletion test/usm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ set(UR_USM_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})

function(add_usm_test name)
set(TEST_TARGET_NAME usm_test-${name})
add_executable(${TEST_TARGET_NAME}
add_ur_executable(${TEST_TARGET_NAME}
${UR_USM_TEST_DIR}/../conformance/source/environment.cpp
${UR_USM_TEST_DIR}/../conformance/source/main.cpp
${ARGN})
Expand Down
2 changes: 1 addition & 1 deletion tools/urtrace/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

set(TARGET_NAME ur_collector)

add_library(${TARGET_NAME} SHARED
add_ur_library(${TARGET_NAME} SHARED
${CMAKE_CURRENT_SOURCE_DIR}/collector.cpp
)

Expand Down

0 comments on commit 360746c

Please sign in to comment.