| 1 |
# Copyright (c) 2015-2016 The Khronos Group Inc. |
| 2 |
# |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
# Utility functions for pushing & popping variables. |
| 17 |
function(push_variable var val) |
| 18 |
set("${var}_SAVE_STACK" "${${var}}" "${${var}_SAVE_STACK}" PARENT_SCOPE) |
| 19 |
set(${var} ${val} PARENT_SCOPE) |
| 20 |
endfunction() |
| 21 |
function(pop_variable var) |
| 22 |
set(save_stack "${${var}_SAVE_STACK}") |
| 23 |
list(GET save_stack 0 val) |
| 24 |
list(REMOVE_AT save_stack 0) |
| 25 |
set("${var}_SAVE_STACK" "${save_stack}" PARENT_SCOPE) |
| 26 |
set(${var} ${val} PARENT_SCOPE) |
| 27 |
endfunction() |
| 28 |
|
| 29 |
if (DEFINED mimalloc_SOURCE_DIR) |
| 30 |
# This allows flexible position of the mimalloc repo. |
| 31 |
set(MIMALLOC_DIR ${mimalloc_SOURCE_DIR}) |
| 32 |
else() |
| 33 |
if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/mimalloc) |
| 34 |
set(MIMALLOC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/mimalloc) |
| 35 |
endif() |
| 36 |
endif() |
| 37 |
|
| 38 |
# Used on Windows by default, but allow opt-in on other platforms |
| 39 |
if (WIN32) |
| 40 |
set(SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE ON) |
| 41 |
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE OFF) |
| 42 |
else() |
| 43 |
set(SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE OFF) |
| 44 |
set(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE OFF) |
| 45 |
endif() |
| 46 |
|
| 47 |
# To avoid unexpected side effects on users of the static library, mimalloc |
| 48 |
# must be explicitly enabled when building static libraries. |
| 49 |
include(CMakeDependentOption) |
| 50 |
cmake_dependent_option(SPIRV_TOOLS_USE_MIMALLOC |
| 51 |
"Executables and shared libraries use mimalloc instead of the default allocator" |
| 52 |
${SPIRV_TOOLS_USE_MIMALLOC_DEFAULT_VALUE} "MIMALLOC_DIR" OFF) |
| 53 |
|
| 54 |
cmake_dependent_option(SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD |
| 55 |
"Static libraries use mimalloc instead of the default allocator" |
| 56 |
${SPIRV_TOOLS_USE_MIMALLOC_IN_STATIC_BUILD_DEFAULT_VALUE} SPIRV_TOOLS_USE_MIMALLOC OFF) |
| 57 |
|
| 58 |
if (SPIRV_TOOLS_USE_MIMALLOC) |
| 59 |
if (NOT WIN32) |
| 60 |
push_variable(MI_OVERRIDE 0) |
| 61 |
endif() |
| 62 |
push_variable(MI_BUILD_TESTS 0) |
| 63 |
|
| 64 |
add_subdirectory(${MIMALLOC_DIR} ${CMAKE_BINARY_DIR}/external/mimalloc EXCLUDE_FROM_ALL) |
| 65 |
if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang) |
| 66 |
target_compile_options(mimalloc-static PRIVATE -Wno-int-conversion) |
| 67 |
endif() |
| 68 |
|
| 69 |
if (NOT WIN32) |
| 70 |
pop_variable(MI_OVERRIDE) |
| 71 |
endif() |
| 72 |
pop_variable(MI_BUILD_TESTS) |
| 73 |
endif() |
| 74 |
|
| 75 |
if (DEFINED SPIRV-Headers_SOURCE_DIR) |
| 76 |
# This allows flexible position of the SPIRV-Headers repo. |
| 77 |
set(SPIRV_HEADER_DIR ${SPIRV-Headers_SOURCE_DIR}) |
| 78 |
else() |
| 79 |
set(SPIRV_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers) |
| 80 |
endif() |
| 81 |
|
| 82 |
if (IS_DIRECTORY ${SPIRV_HEADER_DIR}) |
| 83 |
# TODO(dneto): We should not be modifying the parent scope. |
| 84 |
set(SPIRV_HEADER_INCLUDE_DIR ${SPIRV_HEADER_DIR}/include PARENT_SCOPE) |
| 85 |
|
| 86 |
# Add SPIRV-Headers as a sub-project if it isn't already defined. |
| 87 |
# Do this so enclosing projects can use SPIRV-Headers_SOURCE_DIR to find |
| 88 |
# headers to include. |
| 89 |
if (NOT DEFINED SPIRV-Headers_SOURCE_DIR) |
| 90 |
add_subdirectory(${SPIRV_HEADER_DIR}) |
| 91 |
endif() |
| 92 |
else() |
| 93 |
message(FATAL_ERROR |
| 94 |
"SPIRV-Headers was not found - please checkout a copy at external/spirv-headers.") |
| 95 |
endif() |
| 96 |
|
| 97 |
if (NOT ${SPIRV_SKIP_TESTS}) |
| 98 |
# Find gmock if we can. If it's not already configured, then try finding |
| 99 |
# it in external/googletest. |
| 100 |
if (TARGET gmock) |
| 101 |
message(STATUS "Google Mock already configured") |
| 102 |
else() |
| 103 |
if (NOT GMOCK_DIR) |
| 104 |
set(GMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest) |
| 105 |
endif() |
| 106 |
if(EXISTS ${GMOCK_DIR}) |
| 107 |
if(MSVC) |
| 108 |
# Our tests use ::testing::Combine. Work around a compiler |
| 109 |
# detection problem in googletest, where that template is |
| 110 |
# accidentally disabled for VS 2017. |
| 111 |
# See https://github.com/google/googletest/issues/1352 |
| 112 |
add_definitions(-DGTEST_HAS_COMBINE=1) |
| 113 |
endif() |
| 114 |
if(WIN32) |
| 115 |
option(gtest_force_shared_crt |
| 116 |
"Use shared (DLL) run-time lib even when Google Test is built as static lib." |
| 117 |
ON) |
| 118 |
endif() |
| 119 |
# gtest requires special defines for building as a shared |
| 120 |
# library, simply always build as static. |
| 121 |
push_variable(BUILD_SHARED_LIBS 0) |
| 122 |
add_subdirectory(${GMOCK_DIR} ${CMAKE_CURRENT_BINARY_DIR}/googletest EXCLUDE_FROM_ALL) |
| 123 |
pop_variable(BUILD_SHARED_LIBS) |
| 124 |
endif() |
| 125 |
endif() |
| 126 |
if (TARGET gmock) |
| 127 |
set(GTEST_TARGETS |
| 128 |
gtest |
| 129 |
gtest_main |
| 130 |
gmock |
| 131 |
gmock_main |
| 132 |
) |
| 133 |
foreach(target ${GTEST_TARGETS}) |
| 134 |
set_property(TARGET ${target} PROPERTY FOLDER GoogleTest) |
| 135 |
endforeach() |
| 136 |
endif() |
| 137 |
|
| 138 |
# Find Effcee and RE2, for testing. |
| 139 |
|
| 140 |
# RE2 depends on Abseil. We set absl_SOURCE_DIR if it is not already set, so |
| 141 |
# that effcee can find abseil. |
| 142 |
if(NOT TARGET absl::base) |
| 143 |
if (NOT absl_SOURCE_DIR) |
| 144 |
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/abseil_cpp) |
| 145 |
set(absl_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/abseil_cpp" CACHE STRING "Abseil source dir" ) |
| 146 |
endif() |
| 147 |
endif() |
| 148 |
endif() |
| 149 |
|
| 150 |
# First find RE2, since Effcee depends on it. |
| 151 |
# If already configured, then use that. Otherwise, prefer to find it under 're2' |
| 152 |
# in this directory. |
| 153 |
if (NOT TARGET re2) |
| 154 |
|
| 155 |
|
| 156 |
# If we are configuring RE2, then turn off its testing. It takes a long time and |
| 157 |
# does not add much value for us. If an enclosing project configured RE2, then it |
| 158 |
# has already chosen whether to enable RE2 testing. |
| 159 |
set(RE2_BUILD_TESTING OFF CACHE STRING "Run RE2 Tests") |
| 160 |
if (NOT RE2_SOURCE_DIR) |
| 161 |
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/re2) |
| 162 |
set(RE2_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/re2" CACHE STRING "RE2 source dir" ) |
| 163 |
endif() |
| 164 |
endif() |
| 165 |
endif() |
| 166 |
|
| 167 |
if (NOT TARGET effcee) |
| 168 |
# Expect to find effcee in this directory. |
| 169 |
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/effcee) |
| 170 |
# If we're configuring RE2 (via Effcee), then turn off RE2 testing. |
| 171 |
if (NOT TARGET re2) |
| 172 |
set(RE2_BUILD_TESTING OFF) |
| 173 |
endif() |
| 174 |
if (MSVC) |
| 175 |
# SPIRV-Tools uses the shared CRT with MSVC. Tell Effcee to do the same. |
| 176 |
set(EFFCEE_ENABLE_SHARED_CRT ON) |
| 177 |
endif() |
| 178 |
set(EFFCEE_BUILD_SAMPLES OFF CACHE BOOL "Do not build Effcee examples") |
| 179 |
if (NOT TARGET effcee) |
| 180 |
set(EFFCEE_BUILD_TESTING OFF CACHE BOOL "Do not build Effcee test suite") |
| 181 |
endif() |
| 182 |
push_variable(BUILD_SHARED_LIBS 0) # effcee does not export any symbols for building as a DLL. Always build as static. |
| 183 |
add_subdirectory(effcee EXCLUDE_FROM_ALL) |
| 184 |
pop_variable(BUILD_SHARED_LIBS) |
| 185 |
set_property(TARGET effcee PROPERTY FOLDER Effcee) |
| 186 |
# Turn off warnings for effcee and re2 |
| 187 |
set_property(TARGET effcee APPEND PROPERTY COMPILE_OPTIONS -w) |
| 188 |
set_property(TARGET re2 APPEND PROPERTY COMPILE_OPTIONS -w) |
| 189 |
endif() |
| 190 |
endif() |
| 191 |
endif() |
| 192 |
|
| 193 |
if(SPIRV_BUILD_FUZZER) |
| 194 |
|
| 195 |
function(backup_compile_options) |
| 196 |
get_property( |
| 197 |
SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS |
| 198 |
DIRECTORY |
| 199 |
PROPERTY COMPILE_OPTIONS |
| 200 |
) |
| 201 |
endfunction() |
| 202 |
|
| 203 |
function(restore_compile_options) |
| 204 |
set_property( |
| 205 |
DIRECTORY |
| 206 |
PROPERTY COMPILE_OPTIONS |
| 207 |
${SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS} |
| 208 |
) |
| 209 |
endfunction() |
| 210 |
|
| 211 |
if(NOT TARGET protobuf::libprotobuf OR NOT TARGET protobuf::protoc) |
| 212 |
|
| 213 |
set(SPIRV_TOOLS_PROTOBUF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protobuf) |
| 214 |
if (NOT IS_DIRECTORY ${SPIRV_TOOLS_PROTOBUF_DIR}) |
| 215 |
message( |
| 216 |
FATAL_ERROR |
| 217 |
"protobuf not found - please checkout a copy under external/.") |
| 218 |
endif() |
| 219 |
set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable protobuf tests") |
| 220 |
set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Do not build protobuf static runtime") |
| 221 |
|
| 222 |
backup_compile_options() |
| 223 |
|
| 224 |
if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang) |
| 225 |
add_compile_options(-Wno-inconsistent-missing-override) |
| 226 |
endif() |
| 227 |
|
| 228 |
add_subdirectory(${SPIRV_TOOLS_PROTOBUF_DIR} EXCLUDE_FROM_ALL) |
| 229 |
|
| 230 |
restore_compile_options() |
| 231 |
|
| 232 |
endif() |
| 233 |
endif() |