Skip to content

Commit 0033c6e

Browse files
committed
Set global compile flags for UR targets only
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.
1 parent efee787 commit 0033c6e

File tree

18 files changed

+60
-36
lines changed

18 files changed

+60
-36
lines changed

CMakeLists.txt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,7 @@ if(MSVC)
5252
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
5353
endif()
5454

55-
# CXX flags setup
5655
if(NOT MSVC)
57-
add_compile_options(-fPIC -Wall -Wpedantic
58-
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
59-
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>)
60-
if (CMAKE_BUILD_TYPE STREQUAL "Release")
61-
add_compile_options(-D_FORTIFY_SOURCE=2)
62-
endif()
63-
if(UR_DEVELOPER_MODE)
64-
add_compile_options(-Werror -fno-omit-frame-pointer -fstack-protector-strong)
65-
endif()
6656
# Determine if libstdc++ is being used.
6757
check_cxx_source_compiles("
6858
#include <array>
@@ -78,14 +68,6 @@ if(NOT MSVC)
7868
# is available we still need to link this library.
7969
link_libraries(stdc++fs)
8070
endif()
81-
elseif(MSVC)
82-
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
83-
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
84-
add_compile_options(/MP /W3 /MD$<$<CONFIG:Debug>:d>)
85-
86-
if(UR_DEVELOPER_MODE)
87-
add_compile_options(/WX /GS)
88-
endif()
8971
endif()
9072

9173
if(UR_ENABLE_TRACING)

cmake/helpers.cmake

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,48 @@ macro(add_sanitizer_flag flag)
5757
set(CMAKE_REQUIRED_LIBRARIES ${SAVED_CMAKE_REQUIRED_LIBRARIES})
5858
endmacro()
5959

60+
function(add_ur_target_compile_options name)
61+
if(NOT MSVC)
62+
target_compile_options(${name} PRIVATE
63+
-fPIC
64+
-Wall
65+
-Wpedantic
66+
$<$<CXX_COMPILER_ID:GNU>:-fdiagnostics-color=always>
67+
$<$<CXX_COMPILER_ID:Clang,AppleClang>:-fcolor-diagnostics>
68+
)
69+
if (CMAKE_BUILD_TYPE STREQUAL "Release")
70+
target_compile_definitions(${name} PRIVATE -D_FORTIFY_SOURCE=2)
71+
endif()
72+
if(UR_DEVELOPER_MODE)
73+
target_compile_options(${name} PRIVATE
74+
-Werror
75+
-fno-omit-frame-pointer
76+
-fstack-protector-strong
77+
)
78+
endif()
79+
elseif(MSVC)
80+
target_compile_options(${name} PRIVATE
81+
/MP
82+
/W3
83+
/MD$<$<CONFIG:Debug>:d>
84+
)
85+
86+
if(UR_DEVELOPER_MODE)
87+
target_compile_options(${name} PRIVATE /WX /GS)
88+
endif()
89+
endif()
90+
endfunction()
91+
92+
function(add_ur_executable name)
93+
add_executable(${name} ${ARGN})
94+
add_ur_target_compile_options(${name})
95+
endfunction()
96+
97+
function(add_ur_library name)
98+
add_library(${name} ${ARGN})
99+
add_ur_target_compile_options(${name})
100+
endfunction()
101+
60102
include(FetchContent)
61103

62104
# A wrapper around FetchContent_Declare that supports git sparse checkout.

examples/collector/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
set(TARGET_NAME collector)
77

8-
add_library(${TARGET_NAME} SHARED
8+
add_ur_library(${TARGET_NAME} SHARED
99
${CMAKE_CURRENT_SOURCE_DIR}/collector.cpp
1010
)
1111

examples/hello_world/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
set(TARGET_NAME hello_world)
77

8-
add_executable(${TARGET_NAME}
8+
add_ur_executable(${TARGET_NAME}
99
${CMAKE_CURRENT_SOURCE_DIR}/hello_world.cpp
1010
)
1111

source/adapters/null/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
set(TARGET_NAME ur_adapter_null)
77

8-
add_library(${TARGET_NAME}
8+
add_ur_library(${TARGET_NAME}
99
SHARED
1010
${CMAKE_CURRENT_SOURCE_DIR}/ur_null.hpp
1111
${CMAKE_CURRENT_SOURCE_DIR}/ur_null.cpp

source/common/umf_pools/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See LICENSE.TXT
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6-
add_library(disjoint_pool STATIC
6+
add_ur_library(disjoint_pool STATIC
77
disjoint_pool.cpp
88
disjoint_pool_config_parser.cpp
99
)

source/common/unified_malloc_framework/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ if(UMF_BUILD_SHARED_LIBRARY)
1414
message(WARNING "Unified Malloc Framework is still an early work in progress."
1515
"There are no API/ABI backward compatibility guarantees. There will be breakages."
1616
"Do not use the shared library in production software.")
17-
add_library(unified_malloc_framework SHARED
17+
add_ur_library(unified_malloc_framework SHARED
1818
${UMF_SOURCES})
1919
target_compile_definitions(unified_malloc_framework PUBLIC UMF_SHARED_LIBRARY)
2020
else()
21-
add_library(unified_malloc_framework STATIC
21+
add_ur_library(unified_malloc_framework STATIC
2222
${UMF_SOURCES})
2323
endif()
2424

source/loader/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ configure_file(
99
@ONLY
1010
)
1111

12-
add_library(ur_loader
12+
add_ur_library(ur_loader
1313
SHARED
1414
""
1515
${CMAKE_CURRENT_BINARY_DIR}/UrLoaderVersion.rc

test/conformance/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set(UR_CONFORMANCE_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR})
77

88
function(add_conformance_test name)
99
set(TEST_TARGET_NAME test-${name})
10-
add_executable(${TEST_TARGET_NAME}
10+
add_ur_executable(${TEST_TARGET_NAME}
1111
${ARGN}
1212
${UR_CONFORMANCE_TEST_DIR}/source/environment.cpp
1313
${UR_CONFORMANCE_TEST_DIR}/source/main.cpp)

test/conformance/testing/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# See LICENSE.TXT
44
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
55

6-
add_library(ur_testing STATIC
6+
add_ur_library(ur_testing STATIC
77
source/utils.cpp)
88
target_include_directories(ur_testing PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
99
target_link_libraries(ur_testing PRIVATE gtest_main unified-runtime::headers)

0 commit comments

Comments
 (0)