-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeMacros.cmake
134 lines (126 loc) · 4.26 KB
/
CMakeMacros.cmake
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Borrowed from http://stackoverflow.com/a/3323227/445373
function(sm_list_replace container index newvalue)
list(INSERT ${container} ${index} ${newvalue})
math(EXPR __INDEX "${index} + 1")
list(REMOVE_AT ${container} ${__INDEX})
endfunction()
function(sm_append_simple_target_property target property str)
get_target_property(current_property ${target} ${property})
if(current_property)
list(APPEND current_property ${str})
set_target_properties(${target}
PROPERTIES ${property} "${current_property}")
else()
set_target_properties(${target} PROPERTIES ${property} ${str})
endif()
endfunction()
# Borrowed from http://stackoverflow.com/a/7172941/445373 TODO: Upgrade to cmake
# 3.x so that this function is not needed.
function(sm_join values glue output)
string(REPLACE ";"
"${glue}"
_TMP_STR
"${values}")
set(${output} "${_TMP_STR}" PARENT_SCOPE)
endfunction()
function(sm_add_compile_definition target def)
sm_append_simple_target_property(${target} COMPILE_DEFINITIONS ${def})
endfunction()
function(sm_add_compile_flag target flag)
get_target_property(current_property ${target} COMPILE_FLAGS)
if(current_property)
set(current_property "${current_property} ${flag}")
set_target_properties(${target}
PROPERTIES COMPILE_FLAGS "${current_property}")
else()
set_target_properties(${target} PROPERTIES COMPILE_FLAGS ${flag})
endif()
endfunction()
function(sm_add_link_flag target flag)
if(MSVC)
# Use a modified form of sm_append_simple_target_property.
get_target_property(current_property ${target} LINK_FLAGS)
if(current_property)
set_target_properties(${target}
PROPERTIES LINK_FLAGS "${current_property} ${flag}")
else()
set_target_properties(${target} PROPERTIES LINK_FLAGS ${flag})
endif()
else()
sm_append_simple_target_property(${target} LINK_FLAGS ${flag})
endif()
endfunction()
function(disable_project_warnings projectName)
if(NOT WITH_EXTERNAL_WARNINGS)
if(MSVC)
sm_add_compile_flag(${projectName} "/W0")
elseif(APPLE)
set_target_properties(
${projectName}
PROPERTIES XCODE_ATTRIBUTE_GCC_WARN_INHIBIT_ALL_WARNINGS "YES")
else()
set_target_properties(${projectName} PROPERTIES COMPILE_FLAGS "-w")
endif()
endif()
endfunction()
macro(check_compile_features
BIN_DIR
SOURCE_FILE
GREETER
GREET_SUCCESS
GREET_FAILURE
TARGET_VAR
ON_SUCCESS)
if(NOT DEFINED "${TARGET_VAR}")
message(STATUS "${GREETER}")
try_compile(${TARGET_VAR} "${BIN_DIR}" SOURCES "${SOURCE_FILE}")
if(${TARGET_VAR})
if(${ON_SUCCESS})
message(STATUS "${GREETER} - ${GREET_SUCCESS}")
set(${TARGET_VAR} 1 CACHE INTERNAL "${GREETER}")
else()
message(STATUS "${GREETER} - ${GREET_FAILURE}")
set(${TARGET_VAR} "" CACHE INTERNAL "${GREETER}")
endif()
else()
if(${ON_SUCCESS})
message(STATUS "${GREETER} - ${GREET_FAILURE}")
set(${TARGET_VAR} 1 CACHE INTERNAL "${GREETER}")
else()
message(STATUS "${GREETER} - ${GREET_SUCCESS}")
set(${TARGET_VAR} "" CACHE INTERNAL "${GREETER}")
endif()
endif()
endif()
endmacro()
# Borrowed from http://stackoverflow.com/q/10113017
macro(configure_msvc_runtime)
if(MSVC)
# Get the compiler options generally used.
list(APPEND COMPILER_VARIABLES
CMAKE_C_FLAGS_DEBUG
CMAKE_C_FLAGS_MINSIZEREL
CMAKE_C_FLAGS_RELEASE
CMAKE_C_FLAGS_RELWITHDEBINFO
CMAKE_CXX_FLAGS_DEBUG
CMAKE_CXX_FLAGS_MINSIZEREL
CMAKE_CXX_FLAGS_RELEASE
CMAKE_CXX_FLAGS_RELWITHDEBINFO)
if(WITH_STATIC_LINKING)
set(TO_REPLACE "/MD")
set(REPLACE_WITH "/MT")
else()
set(TO_REPLACE "/MT")
set(REPLACE_WITH "/MD")
endif()
foreach(COMPILER_VARIABLE ${COMPILER_VARIABLES})
if(${COMPILER_VARIABLE} MATCHES "${TO_REPLACE}")
string(REGEX
REPLACE "${TO_REPLACE}"
"${REPLACE_WITH}"
${COMPILER_VARIABLE}
"${${COMPILER_VARIABLE}}")
endif()
endforeach()
endif()
endmacro()