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