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