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. 17function(push_variable var val) 18 set("${var}_SAVE_STACK" "${${var}}" "${${var}_SAVE_STACK}" PARENT_SCOPE) 19 set(${var} ${val} PARENT_SCOPE) 20endfunction() 21function(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) 27endfunction() 28 29if (DEFINED SPIRV-Headers_SOURCE_DIR) 30 # This allows flexible position of the SPIRV-Headers repo. 31 set(SPIRV_HEADER_DIR ${SPIRV-Headers_SOURCE_DIR}) 32else() 33 if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/SPIRV-Headers) 34 set(SPIRV_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/SPIRV-Headers) 35 else() 36 set(SPIRV_HEADER_DIR ${CMAKE_CURRENT_SOURCE_DIR}/spirv-headers) 37 endif() 38endif() 39 40if (IS_DIRECTORY ${SPIRV_HEADER_DIR}) 41 # TODO(dneto): We should not be modifying the parent scope. 42 set(SPIRV_HEADER_INCLUDE_DIR ${SPIRV_HEADER_DIR}/include PARENT_SCOPE) 43 44 # Add SPIRV-Headers as a sub-project if it isn't already defined. 45 # Do this so enclosing projects can use SPIRV-Headers_SOURCE_DIR to find 46 # headers to include. 47 if (NOT DEFINED SPIRV-Headers_SOURCE_DIR) 48 set(SPIRV_HEADERS_SKIP_INSTALL ON) 49 set(SPIRV_HEADERS_SKIP_EXAMPLES ON) 50 add_subdirectory(${SPIRV_HEADER_DIR}) 51 endif() 52else() 53 message(FATAL_ERROR 54 "SPIRV-Headers was not found - please checkout a copy under external/.") 55endif() 56 57if (NOT ${SPIRV_SKIP_TESTS}) 58 # Find gmock if we can. If it's not already configured, then try finding 59 # it in external/googletest. 60 if (TARGET gmock) 61 message(STATUS "Google Mock already configured") 62 else() 63 set(GMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/googletest) 64 if(EXISTS ${GMOCK_DIR}) 65 if(MSVC) 66 # Our tests use ::testing::Combine. Work around a compiler 67 # detection problem in googletest, where that template is 68 # accidentally disabled for VS 2017. 69 # See https://github.com/google/googletest/issues/1352 70 add_definitions(-DGTEST_HAS_COMBINE=1) 71 endif() 72 if(WIN32) 73 option(gtest_force_shared_crt 74 "Use shared (DLL) run-time lib even when Google Test is built as static lib." 75 ON) 76 endif() 77 # gtest requires special defines for building as a shared 78 # library, simply always build as static. 79 push_variable(BUILD_SHARED_LIBS 0) 80 add_subdirectory(${GMOCK_DIR} EXCLUDE_FROM_ALL) 81 pop_variable(BUILD_SHARED_LIBS) 82 endif() 83 endif() 84 if (TARGET gmock) 85 set(GTEST_TARGETS 86 gtest 87 gtest_main 88 gmock 89 gmock_main 90 ) 91 foreach(target ${GTEST_TARGETS}) 92 set_property(TARGET ${target} PROPERTY FOLDER GoogleTest) 93 endforeach() 94 endif() 95 96 # Find Effcee and RE2, for testing. 97 98 # First find RE2, since Effcee depends on it. 99 # If already configured, then use that. Otherwise, prefer to find it under 're2' 100 # in this directory. 101 if (NOT TARGET re2) 102 # If we are configuring RE2, then turn off its testing. It takes a long time and 103 # does not add much value for us. If an enclosing project configured RE2, then it 104 # has already chosen whether to enable RE2 testing. 105 set(RE2_BUILD_TESTING OFF CACHE STRING "Run RE2 Tests") 106 if (NOT RE2_SOURCE_DIR) 107 if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/re2) 108 set(RE2_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/re2" CACHE STRING "RE2 source dir" ) 109 endif() 110 endif() 111 endif() 112 113 if (NOT TARGET effcee) 114 # Expect to find effcee in this directory. 115 if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/effcee) 116 # If we're configuring RE2 (via Effcee), then turn off RE2 testing. 117 if (NOT TARGET re2) 118 set(RE2_BUILD_TESTING OFF) 119 endif() 120 if (MSVC) 121 # SPIRV-Tools uses the shared CRT with MSVC. Tell Effcee to do the same. 122 set(EFFCEE_ENABLE_SHARED_CRT ON) 123 endif() 124 set(EFFCEE_BUILD_SAMPLES OFF CACHE BOOL "Do not build Effcee examples") 125 if (NOT TARGET effcee) 126 set(EFFCEE_BUILD_TESTING OFF CACHE BOOL "Do not build Effcee test suite") 127 endif() 128 push_variable(BUILD_SHARED_LIBS 0) # effcee does not export any symbols for building as a DLL. Always build as static. 129 add_subdirectory(effcee EXCLUDE_FROM_ALL) 130 pop_variable(BUILD_SHARED_LIBS) 131 set_property(TARGET effcee PROPERTY FOLDER Effcee) 132 # Turn off warnings for effcee and re2 133 set_property(TARGET effcee APPEND PROPERTY COMPILE_OPTIONS -w) 134 set_property(TARGET re2 APPEND PROPERTY COMPILE_OPTIONS -w) 135 endif() 136 endif() 137endif() 138 139if(SPIRV_BUILD_FUZZER) 140 141 function(backup_compile_options) 142 get_property( 143 SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS 144 DIRECTORY 145 PROPERTY COMPILE_OPTIONS 146 ) 147 endfunction() 148 149 function(restore_compile_options) 150 set_property( 151 DIRECTORY 152 PROPERTY COMPILE_OPTIONS 153 ${SPIRV_TOOLS_BACKUP_EXTERNAL_COMPILE_OPTIONS} 154 ) 155 endfunction() 156 157 if(NOT TARGET protobuf::libprotobuf OR NOT TARGET protobuf::protoc) 158 159 set(SPIRV_TOOLS_PROTOBUF_DIR ${CMAKE_CURRENT_SOURCE_DIR}/protobuf/cmake) 160 if (NOT IS_DIRECTORY ${SPIRV_TOOLS_PROTOBUF_DIR}) 161 message( 162 FATAL_ERROR 163 "protobuf not found - please checkout a copy under external/.") 164 endif() 165 set(protobuf_BUILD_TESTS OFF CACHE BOOL "Disable protobuf tests") 166 set(protobuf_MSVC_STATIC_RUNTIME OFF CACHE BOOL "Do not build protobuf static runtime") 167 168 backup_compile_options() 169 170 if (${CMAKE_CXX_COMPILER_ID} MATCHES Clang) 171 add_compile_options(-Wno-inconsistent-missing-override) 172 endif() 173 174 add_subdirectory(${SPIRV_TOOLS_PROTOBUF_DIR} EXCLUDE_FROM_ALL) 175 176 restore_compile_options() 177 178 endif() 179endif() 180