-
Notifications
You must be signed in to change notification settings - Fork 370
/
Copy pathutils.cmake
182 lines (173 loc) · 7.17 KB
/
utils.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# Copyright 2020 The Shaderc Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# utility functions
function (shaderc_use_gmock TARGET)
target_include_directories(${TARGET} PRIVATE
${gmock_SOURCE_DIR}/include
${gtest_SOURCE_DIR}/include)
target_link_libraries(${TARGET} PRIVATE gmock gtest_main)
endfunction(shaderc_use_gmock)
function(shaderc_default_c_compile_options TARGET)
if (NOT "${MSVC}")
if (SHADERC_ENABLE_WERROR_COMPILE)
target_compile_options(${TARGET} PRIVATE -Wall -Werror -fvisibility=hidden)
else()
target_compile_options(${TARGET} PRIVATE -Wall -fvisibility=hidden)
endif()
check_cxx_compiler_flag(-fPIC COMPILER_SUPPORTS_PIC)
if (NOT "${MINGW}" AND COMPILER_SUPPORTS_PIC)
target_compile_options(${TARGET} PRIVATE -fPIC)
endif()
if (ENABLE_CODE_COVERAGE)
# The --coverage option is a synonym for -fprofile-arcs -ftest-coverage
# when compiling.
target_compile_options(${TARGET} PRIVATE -g -O0 --coverage)
# The --coverage option is a synonym for -lgcov when linking for gcc.
# For clang, it links in a different library, libclang_rt.profile, which
# requires clang to be built with compiler-rt.
target_link_libraries(${TARGET} PRIVATE --coverage)
endif()
if (NOT SHADERC_ENABLE_SHARED_CRT)
if (WIN32)
# For MinGW cross compile, statically link to the libgcc runtime.
# But it still depends on MSVCRT.dll.
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS "-static -static-libgcc")
endif(WIN32)
endif(NOT SHADERC_ENABLE_SHARED_CRT)
else()
# disable warning C4800: 'int' : forcing value to bool 'true' or 'false'
# (performance warning)
target_compile_options(${TARGET} PRIVATE /wd4800)
endif()
endfunction(shaderc_default_c_compile_options)
function(shaderc_default_compile_options TARGET)
shaderc_default_c_compile_options(${TARGET})
if (NOT "${MSVC}")
if (NOT SHADERC_ENABLE_SHARED_CRT)
if (WIN32)
# For MinGW cross compile, statically link to the C++ runtime.
# But it still depends on MSVCRT.dll.
set_target_properties(${TARGET} PROPERTIES
LINK_FLAGS "-static -static-libgcc -static-libstdc++")
endif(WIN32)
endif(NOT SHADERC_ENABLE_SHARED_CRT)
endif()
endfunction(shaderc_default_compile_options)
# Build an asciidoc file; additional arguments past the base filename specify
# additional dependencies for the file.
function(shaderc_add_asciidoc TARGET FILE)
if (ASCIIDOCTOR_EXE)
set(DEST ${CMAKE_CURRENT_BINARY_DIR}/${FILE}.html)
add_custom_command(
COMMAND ${ASCIIDOCTOR_EXE} -a toc -o ${DEST}
${CMAKE_CURRENT_SOURCE_DIR}/${FILE}.asciidoc
DEPENDS ${FILE}.asciidoc ${ARGN}
OUTPUT ${DEST})
# Create the target, but the default build target does not depend on it.
# Some Asciidoctor installations are mysteriously broken, and it's hard
# to detect those cases. Generating HTML is not critical by default.
add_custom_target(${TARGET} DEPENDS ${DEST})
endif(ASCIIDOCTOR_EXE)
endfunction()
# Adds a set of tests.
# This function accepts the following parameters:
# TEST_PREFIX: a prefix for each test target name
# TEST_NAMES: a list of test names where each TEST_NAME has a corresponding
# file residing at src/${TEST_NAME}_test.cc
# LINK_LIBS: (optional) a list of libraries to be linked to the test target
# INCLUDE_DIRS: (optional) a list of include directories to be searched
# for header files.
function(shaderc_add_tests)
if(${SHADERC_ENABLE_TESTS})
cmake_parse_arguments(PARSED_ARGS
""
"TEST_PREFIX"
"TEST_NAMES;LINK_LIBS;INCLUDE_DIRS"
${ARGN})
if (NOT PARSED_ARGS_TEST_NAMES)
message(FATAL_ERROR "Tests must have a target")
endif()
if (NOT PARSED_ARGS_TEST_PREFIX)
message(FATAL_ERROR "Tests must have a prefix")
endif()
foreach(TARGET ${PARSED_ARGS_TEST_NAMES})
set(TEST_NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET}_test)
add_executable(${TEST_NAME} src/${TARGET}_test.cc)
shaderc_default_compile_options(${TEST_NAME})
if (MINGW)
target_compile_options(${TEST_NAME} PRIVATE -DSHADERC_DISABLE_THREADED_TESTS)
endif()
if (CMAKE_CXX_COMPILER_ID MATCHES "GNU")
# Disable this warning, which is useless in test code.
# Fixes https://github.com/google/shaderc/issues/334
target_compile_options(${TEST_NAME} PRIVATE -Wno-noexcept-type)
endif()
if (PARSED_ARGS_LINK_LIBS)
target_link_libraries(${TEST_NAME} PRIVATE
${PARSED_ARGS_LINK_LIBS})
endif()
if (PARSED_ARGS_INCLUDE_DIRS)
target_include_directories(${TEST_NAME} PRIVATE
${PARSED_ARGS_INCLUDE_DIRS})
endif()
shaderc_use_gmock(${TEST_NAME})
add_test(
NAME ${PARSED_ARGS_TEST_PREFIX}_${TARGET}
COMMAND ${TEST_NAME})
endforeach()
endif(${SHADERC_ENABLE_TESTS})
endfunction(shaderc_add_tests)
# Finds all transitive static library dependencies of a given target
# including possibly the target itself.
# This will skip libraries that were statically linked that were not
# built by CMake, for example -lpthread.
macro(shaderc_get_transitive_libs target out_list)
if (TARGET ${target})
get_target_property(libtype ${target} TYPE)
# If this target is a static library, get anything it depends on.
if ("${libtype}" STREQUAL "STATIC_LIBRARY")
# Get the original library if this is an alias library. This is
# to avoid putting both the original library and the alias library
# in the list (given we are deduplicating according to target names).
# Otherwise, we may pack the same library twice, resulting in
# duplicated symbols.
get_target_property(aliased_target ${target} ALIASED_TARGET)
if (aliased_target)
list(INSERT ${out_list} 0 "${aliased_target}")
else()
list(INSERT ${out_list} 0 "${target}")
endif()
get_target_property(libs ${target} LINK_LIBRARIES)
if (libs)
foreach(lib ${libs})
shaderc_get_transitive_libs(${lib} ${out_list})
endforeach()
endif()
endif()
endif()
# If we know the location (i.e. if it was made with CMake) then we
# can add it to our list.
LIST(REMOVE_DUPLICATES ${out_list})
endmacro()
# Combines the static library "target" with all of its transitive static
# library dependencies into a single static library "new_target".
function(shaderc_combine_static_lib new_target target)
set(all_libs "")
shaderc_get_transitive_libs(${target} all_libs)
add_library(${new_target} STATIC)
foreach(lib IN LISTS all_libs)
target_sources(${new_target} PRIVATE $<TARGET_OBJECTS:${lib}>)
endforeach()
endfunction()