1fd4e5da5Sopenharmony_ci# Copyright (c) 2015-2023 The Khronos Group Inc.
2fd4e5da5Sopenharmony_ci#
3fd4e5da5Sopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci# you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci# You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci#
7fd4e5da5Sopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci#
9fd4e5da5Sopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci# See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci# limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_cicmake_minimum_required(VERSION 3.17.2)
16fd4e5da5Sopenharmony_ci
17fd4e5da5Sopenharmony_ciproject(spirv-tools)
18fd4e5da5Sopenharmony_ci
19fd4e5da5Sopenharmony_ci# Avoid a bug in CMake 3.22.1. By default it will set -std=c++11 for
20fd4e5da5Sopenharmony_ci# targets in test/*, when those tests need -std=c++17.
21fd4e5da5Sopenharmony_ci# https://github.com/KhronosGroup/SPIRV-Tools/issues/5340
22fd4e5da5Sopenharmony_ci# The bug is fixed in CMake 3.22.2
23fd4e5da5Sopenharmony_ciif (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.22.1")
24fd4e5da5Sopenharmony_ci  if (${CMAKE_VERSION} VERSION_LESS "3.22.2")
25fd4e5da5Sopenharmony_ci    cmake_policy(SET CMP0128 NEW)
26fd4e5da5Sopenharmony_ci  endif()
27fd4e5da5Sopenharmony_ciendif()
28fd4e5da5Sopenharmony_ci
29fd4e5da5Sopenharmony_ciset_property(GLOBAL PROPERTY USE_FOLDERS ON)
30fd4e5da5Sopenharmony_ci
31fd4e5da5Sopenharmony_cienable_testing()
32fd4e5da5Sopenharmony_ciset(SPIRV_TOOLS "SPIRV-Tools")
33fd4e5da5Sopenharmony_ci
34fd4e5da5Sopenharmony_ciinclude(GNUInstallDirs)
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ciset(CMAKE_POSITION_INDEPENDENT_CODE ON)
37fd4e5da5Sopenharmony_ci
38fd4e5da5Sopenharmony_ci# Require at least C++17
39fd4e5da5Sopenharmony_ciif(NOT CMAKE_CXX_STANDARD)
40fd4e5da5Sopenharmony_ci  set(CMAKE_CXX_STANDARD 17)
41fd4e5da5Sopenharmony_ciendif()
42fd4e5da5Sopenharmony_ciif(${CMAKE_CXX_STANDARD} LESS 17)
43fd4e5da5Sopenharmony_ci  message(FATAL_ERROR "SPIRV-Tools requires C++17 or later, but is configured for C++${CMAKE_CXX_STANDARD})")
44fd4e5da5Sopenharmony_ciendif()
45fd4e5da5Sopenharmony_ciset(CMAKE_CXX_EXTENSIONS OFF)
46fd4e5da5Sopenharmony_ci
47fd4e5da5Sopenharmony_ci
48fd4e5da5Sopenharmony_cioption(ENABLE_RTTI "Enables RTTI" OFF)
49fd4e5da5Sopenharmony_cioption(SPIRV_ALLOW_TIMERS "Allow timers via clock_gettime on supported platforms" ON)
50fd4e5da5Sopenharmony_ci
51fd4e5da5Sopenharmony_ciif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
52fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_LINUX)
53fd4e5da5Sopenharmony_ci  set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS})
54fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Emscripten")
55fd4e5da5Sopenharmony_ci    add_definitions(-DSPIRV_EMSCRIPTEN)
56fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" MATCHES "Windows")
57fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_WINDOWS)
58fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "CYGWIN")
59fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_WINDOWS)
60fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
61fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_MAC)
62fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "iOS")
63fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_IOS)
64fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "tvOS")
65fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_TVOS)
66fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "visionOS")
67fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_VISIONOS)
68fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
69fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_ANDROID)
70fd4e5da5Sopenharmony_ci  set(SPIRV_TIMER_ENABLED ${SPIRV_ALLOW_TIMERS})
71fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "FreeBSD")
72fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_FREEBSD)
73fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "OpenBSD")
74fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_OPENBSD)
75fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
76fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_FUCHSIA)
77fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "GNU")
78fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_GNU)
79fd4e5da5Sopenharmony_cielseif("${CMAKE_SYSTEM_NAME}" STREQUAL "QNX")
80fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_QNX)
81fd4e5da5Sopenharmony_cielse()
82fd4e5da5Sopenharmony_ci  message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
83fd4e5da5Sopenharmony_ciendif()
84fd4e5da5Sopenharmony_ci
85fd4e5da5Sopenharmony_ciif (${SPIRV_TIMER_ENABLED})
86fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_TIMER_ENABLED)
87fd4e5da5Sopenharmony_ciendif()
88fd4e5da5Sopenharmony_ci
89fd4e5da5Sopenharmony_ciif ("${CMAKE_BUILD_TYPE}" STREQUAL "")
90fd4e5da5Sopenharmony_ci  message(STATUS "No build type selected, default to Debug")
91fd4e5da5Sopenharmony_ci  set(CMAKE_BUILD_TYPE "Debug")
92fd4e5da5Sopenharmony_ciendif()
93fd4e5da5Sopenharmony_ci
94fd4e5da5Sopenharmony_cioption(SKIP_SPIRV_TOOLS_INSTALL "Skip installation" ${SKIP_SPIRV_TOOLS_INSTALL})
95fd4e5da5Sopenharmony_ciif(NOT ${SKIP_SPIRV_TOOLS_INSTALL})
96fd4e5da5Sopenharmony_ci  set(ENABLE_SPIRV_TOOLS_INSTALL ON)
97fd4e5da5Sopenharmony_ciendif()
98fd4e5da5Sopenharmony_ci
99fd4e5da5Sopenharmony_cioption(SPIRV_BUILD_COMPRESSION "Build SPIR-V compressing codec" OFF)
100fd4e5da5Sopenharmony_ciif(SPIRV_BUILD_COMPRESSION)
101fd4e5da5Sopenharmony_ci  message(FATAL_ERROR "SPIR-V compression codec has been removed from SPIR-V tools. "
102fd4e5da5Sopenharmony_ci          "Please remove SPIRV_BUILD_COMPRESSION from your build options.")
103fd4e5da5Sopenharmony_ciendif(SPIRV_BUILD_COMPRESSION)
104fd4e5da5Sopenharmony_ci
105fd4e5da5Sopenharmony_cioption(SPIRV_BUILD_FUZZER "Build spirv-fuzz" OFF)
106fd4e5da5Sopenharmony_ci
107fd4e5da5Sopenharmony_ciset(SPIRV_LIB_FUZZING_ENGINE_LINK_OPTIONS "" CACHE STRING "Used by OSS-Fuzz to control, via link options, which fuzzing engine should be used")
108fd4e5da5Sopenharmony_ci
109fd4e5da5Sopenharmony_cioption(SPIRV_BUILD_LIBFUZZER_TARGETS "Build libFuzzer targets" OFF)
110fd4e5da5Sopenharmony_ci
111fd4e5da5Sopenharmony_cioption(SPIRV_WERROR "Enable error on warning" ON)
112fd4e5da5Sopenharmony_ciif(("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR (("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") AND (NOT CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC")))
113fd4e5da5Sopenharmony_ci  set(COMPILER_IS_LIKE_GNU TRUE)
114fd4e5da5Sopenharmony_ciendif()
115fd4e5da5Sopenharmony_ciif(${COMPILER_IS_LIKE_GNU})
116fd4e5da5Sopenharmony_ci  set(SPIRV_WARNINGS -Wall -Wextra -Wnon-virtual-dtor -Wno-missing-field-initializers)
117fd4e5da5Sopenharmony_ci
118fd4e5da5Sopenharmony_ci  if("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
119fd4e5da5Sopenharmony_ci    set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wno-self-assign)
120fd4e5da5Sopenharmony_ci  endif()
121fd4e5da5Sopenharmony_ci
122fd4e5da5Sopenharmony_ci  option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
123fd4e5da5Sopenharmony_ci  if(${SPIRV_WARN_EVERYTHING})
124fd4e5da5Sopenharmony_ci    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
125fd4e5da5Sopenharmony_ci      set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
126fd4e5da5Sopenharmony_ci        -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
127fd4e5da5Sopenharmony_ci    elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
128fd4e5da5Sopenharmony_ci      set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
129fd4e5da5Sopenharmony_ci    else()
130fd4e5da5Sopenharmony_ci      message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
131fd4e5da5Sopenharmony_ci                     "so SPIRV_WARN_EVERYTHING has no effect")
132fd4e5da5Sopenharmony_ci    endif()
133fd4e5da5Sopenharmony_ci  endif()
134fd4e5da5Sopenharmony_ci
135fd4e5da5Sopenharmony_ci  if(${SPIRV_WERROR})
136fd4e5da5Sopenharmony_ci    set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
137fd4e5da5Sopenharmony_ci  endif()
138fd4e5da5Sopenharmony_cielseif(MSVC)
139fd4e5da5Sopenharmony_ci  set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS -D_SCL_SECURE_NO_WARNINGS /wd4800 /wd4819)
140fd4e5da5Sopenharmony_ci
141fd4e5da5Sopenharmony_ci  if(${SPIRV_WERROR})
142fd4e5da5Sopenharmony_ci    set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
143fd4e5da5Sopenharmony_ci  endif()
144fd4e5da5Sopenharmony_ciendif()
145fd4e5da5Sopenharmony_ci
146fd4e5da5Sopenharmony_ciinclude_directories(${CMAKE_CURRENT_SOURCE_DIR}/)
147fd4e5da5Sopenharmony_ci
148fd4e5da5Sopenharmony_cioption(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
149fd4e5da5Sopenharmony_ciif(${SPIRV_COLOR_TERMINAL})
150fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_COLOR_TERMINAL)
151fd4e5da5Sopenharmony_ciendif()
152fd4e5da5Sopenharmony_ci
153fd4e5da5Sopenharmony_cioption(SPIRV_LOG_DEBUG "Enable excessive debug output" OFF)
154fd4e5da5Sopenharmony_ciif(${SPIRV_LOG_DEBUG})
155fd4e5da5Sopenharmony_ci  add_definitions(-DSPIRV_LOG_DEBUG)
156fd4e5da5Sopenharmony_ciendif()
157fd4e5da5Sopenharmony_ci
158fd4e5da5Sopenharmony_ciif (DEFINED SPIRV_TOOLS_EXTRA_DEFINITIONS)
159fd4e5da5Sopenharmony_ci  add_definitions(${SPIRV_TOOLS_EXTRA_DEFINITIONS})
160fd4e5da5Sopenharmony_ciendif()
161fd4e5da5Sopenharmony_ci
162fd4e5da5Sopenharmony_ci# Library build setting definitions:
163fd4e5da5Sopenharmony_ci#
164fd4e5da5Sopenharmony_ci# * SPIRV_TOOLS_BUILD_STATIC - ON or OFF - Defaults to ON.
165fd4e5da5Sopenharmony_ci#   If enabled the following targets will be created:
166fd4e5da5Sopenharmony_ci#     ${SPIRV_TOOLS}-static - STATIC library.
167fd4e5da5Sopenharmony_ci#                             Has full public symbol visibility.
168fd4e5da5Sopenharmony_ci#     ${SPIRV_TOOLS}-shared - SHARED library.
169fd4e5da5Sopenharmony_ci#                             Has default-hidden symbol visibility.
170fd4e5da5Sopenharmony_ci#     ${SPIRV_TOOLS}        - will alias to one of above, based on BUILD_SHARED_LIBS.
171fd4e5da5Sopenharmony_ci#   If disabled the following targets will be created:
172fd4e5da5Sopenharmony_ci#     ${SPIRV_TOOLS}        - either STATIC or SHARED based on SPIRV_TOOLS_LIBRARY_TYPE.
173fd4e5da5Sopenharmony_ci#                             Has full public symbol visibility.
174fd4e5da5Sopenharmony_ci#     ${SPIRV_TOOLS}-shared - SHARED library.
175fd4e5da5Sopenharmony_ci#                             Has default-hidden symbol visibility.
176fd4e5da5Sopenharmony_ci#
177fd4e5da5Sopenharmony_ci# * SPIRV_TOOLS_LIBRARY_TYPE - SHARED or STATIC.
178fd4e5da5Sopenharmony_ci#   Specifies the library type used for building SPIRV-Tools libraries.
179fd4e5da5Sopenharmony_ci#   Defaults to SHARED when BUILD_SHARED_LIBS=1, otherwise STATIC.
180fd4e5da5Sopenharmony_ci#
181fd4e5da5Sopenharmony_ci# * SPIRV_TOOLS_FULL_VISIBILITY - "${SPIRV_TOOLS}-static" or "${SPIRV_TOOLS}"
182fd4e5da5Sopenharmony_ci#   Evaluates to the SPIRV_TOOLS target library name that has no hidden symbols.
183fd4e5da5Sopenharmony_ci#   This is used by internal targets for accessing symbols that are non-public.
184fd4e5da5Sopenharmony_ci#   Note this target provides no API stability guarantees.
185fd4e5da5Sopenharmony_ci#
186fd4e5da5Sopenharmony_ci# Ideally, all of these will go away - see https://github.com/KhronosGroup/SPIRV-Tools/issues/3909.
187fd4e5da5Sopenharmony_cioption(ENABLE_EXCEPTIONS_ON_MSVC "Build SPIRV-TOOLS with c++ exceptions enabled in MSVC" ON)
188fd4e5da5Sopenharmony_cioption(SPIRV_TOOLS_BUILD_STATIC "Build ${SPIRV_TOOLS}-static target. ${SPIRV_TOOLS} will alias to ${SPIRV_TOOLS}-static or ${SPIRV_TOOLS}-shared based on BUILD_SHARED_LIBS" ON)
189fd4e5da5Sopenharmony_ciif(SPIRV_TOOLS_BUILD_STATIC)
190fd4e5da5Sopenharmony_ci  set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS}-static)
191fd4e5da5Sopenharmony_ci  set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
192fd4e5da5Sopenharmony_cielse(SPIRV_TOOLS_BUILD_STATIC)
193fd4e5da5Sopenharmony_ci  set(SPIRV_TOOLS_FULL_VISIBILITY ${SPIRV_TOOLS})
194fd4e5da5Sopenharmony_ci  if (NOT DEFINED SPIRV_TOOLS_LIBRARY_TYPE)
195fd4e5da5Sopenharmony_ci      if(BUILD_SHARED_LIBS)
196fd4e5da5Sopenharmony_ci        set(SPIRV_TOOLS_LIBRARY_TYPE "SHARED")
197fd4e5da5Sopenharmony_ci      else()
198fd4e5da5Sopenharmony_ci        set(SPIRV_TOOLS_LIBRARY_TYPE "STATIC")
199fd4e5da5Sopenharmony_ci      endif()
200fd4e5da5Sopenharmony_ci  endif()
201fd4e5da5Sopenharmony_ciendif(SPIRV_TOOLS_BUILD_STATIC)
202fd4e5da5Sopenharmony_ci
203fd4e5da5Sopenharmony_cifunction(spvtools_default_compile_options TARGET)
204fd4e5da5Sopenharmony_ci  target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
205fd4e5da5Sopenharmony_ci
206fd4e5da5Sopenharmony_ci  if (${COMPILER_IS_LIKE_GNU})
207fd4e5da5Sopenharmony_ci    target_compile_options(${TARGET} PRIVATE
208fd4e5da5Sopenharmony_ci      -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
209fd4e5da5Sopenharmony_ci      -Wno-sign-conversion -fno-exceptions)
210fd4e5da5Sopenharmony_ci
211fd4e5da5Sopenharmony_ci    if(NOT ENABLE_RTTI)
212fd4e5da5Sopenharmony_ci        add_compile_options(-fno-rtti)
213fd4e5da5Sopenharmony_ci    endif()
214fd4e5da5Sopenharmony_ci    # For good call stacks in profiles, keep the frame pointers.
215fd4e5da5Sopenharmony_ci    if(NOT "${SPIRV_PERF}" STREQUAL "")
216fd4e5da5Sopenharmony_ci      target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
217fd4e5da5Sopenharmony_ci    endif()
218fd4e5da5Sopenharmony_ci    if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang")
219fd4e5da5Sopenharmony_ci      set(SPIRV_USE_SANITIZER "" CACHE STRING
220fd4e5da5Sopenharmony_ci        "Use the clang sanitizer [address|memory|thread|...]")
221fd4e5da5Sopenharmony_ci      if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
222fd4e5da5Sopenharmony_ci        target_compile_options(${TARGET} PRIVATE
223fd4e5da5Sopenharmony_ci          -fsanitize=${SPIRV_USE_SANITIZER})
224fd4e5da5Sopenharmony_ci        set_target_properties(${TARGET} PROPERTIES
225fd4e5da5Sopenharmony_ci          LINK_FLAGS -fsanitize=${SPIRV_USE_SANITIZER})
226fd4e5da5Sopenharmony_ci      endif()
227fd4e5da5Sopenharmony_ci      target_compile_options(${TARGET} PRIVATE
228fd4e5da5Sopenharmony_ci         -ftemplate-depth=1024)
229fd4e5da5Sopenharmony_ci    else()
230fd4e5da5Sopenharmony_ci      target_compile_options(${TARGET} PRIVATE
231fd4e5da5Sopenharmony_ci         -Wno-missing-field-initializers)
232fd4e5da5Sopenharmony_ci    endif()
233fd4e5da5Sopenharmony_ci  endif()
234fd4e5da5Sopenharmony_ci
235fd4e5da5Sopenharmony_ci  if (MSVC)
236fd4e5da5Sopenharmony_ci    # Specify /EHs for exception handling. This makes using SPIRV-Tools as
237fd4e5da5Sopenharmony_ci    # dependencies in other projects easier.
238fd4e5da5Sopenharmony_ci    if(ENABLE_EXCEPTIONS_ON_MSVC)
239fd4e5da5Sopenharmony_ci      target_compile_options(${TARGET} PRIVATE /EHs)
240fd4e5da5Sopenharmony_ci    endif()
241fd4e5da5Sopenharmony_ci  endif()
242fd4e5da5Sopenharmony_ci
243fd4e5da5Sopenharmony_ci  # For MinGW cross compile, statically link to the C++ runtime.
244fd4e5da5Sopenharmony_ci  # But it still depends on MSVCRT.dll.
245fd4e5da5Sopenharmony_ci  if (${CMAKE_SYSTEM_NAME} MATCHES "Windows")
246fd4e5da5Sopenharmony_ci    if (NOT MSVC)
247fd4e5da5Sopenharmony_ci      set_target_properties(${TARGET} PROPERTIES
248fd4e5da5Sopenharmony_ci        LINK_FLAGS -static -static-libgcc -static-libstdc++)
249fd4e5da5Sopenharmony_ci    endif()
250fd4e5da5Sopenharmony_ci  endif()
251fd4e5da5Sopenharmony_ciendfunction()
252fd4e5da5Sopenharmony_ci
253fd4e5da5Sopenharmony_ciif(NOT COMMAND find_host_package)
254fd4e5da5Sopenharmony_ci  macro(find_host_package)
255fd4e5da5Sopenharmony_ci    find_package(${ARGN})
256fd4e5da5Sopenharmony_ci  endmacro()
257fd4e5da5Sopenharmony_ciendif()
258fd4e5da5Sopenharmony_ciif(NOT COMMAND find_host_program)
259fd4e5da5Sopenharmony_ci  macro(find_host_program)
260fd4e5da5Sopenharmony_ci    find_program(${ARGN})
261fd4e5da5Sopenharmony_ci  endmacro()
262fd4e5da5Sopenharmony_ciendif()
263fd4e5da5Sopenharmony_ci
264fd4e5da5Sopenharmony_ci# Tests require Python3
265fd4e5da5Sopenharmony_cifind_host_package(Python3 REQUIRED)
266fd4e5da5Sopenharmony_ci
267fd4e5da5Sopenharmony_ci# Check for symbol exports on Linux.
268fd4e5da5Sopenharmony_ci# At the moment, this check will fail on the OSX build machines for the Android NDK.
269fd4e5da5Sopenharmony_ci# It appears they don't have objdump.
270fd4e5da5Sopenharmony_ciif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
271fd4e5da5Sopenharmony_ci  macro(spvtools_check_symbol_exports TARGET)
272fd4e5da5Sopenharmony_ci    if (NOT "${SPIRV_SKIP_TESTS}")
273fd4e5da5Sopenharmony_ci      add_test(NAME spirv-tools-symbol-exports-${TARGET}
274fd4e5da5Sopenharmony_ci               COMMAND Python3::Interpreter
275fd4e5da5Sopenharmony_ci               ${spirv-tools_SOURCE_DIR}/utils/check_symbol_exports.py "$<TARGET_FILE:${TARGET}>")
276fd4e5da5Sopenharmony_ci    endif()
277fd4e5da5Sopenharmony_ci  endmacro()
278fd4e5da5Sopenharmony_cielse()
279fd4e5da5Sopenharmony_ci  macro(spvtools_check_symbol_exports TARGET)
280fd4e5da5Sopenharmony_ci    if (NOT "${SPIRV_SKIP_TESTS}")
281fd4e5da5Sopenharmony_ci      message("Skipping symbol exports test for ${TARGET}")
282fd4e5da5Sopenharmony_ci    endif()
283fd4e5da5Sopenharmony_ci  endmacro()
284fd4e5da5Sopenharmony_ciendif()
285fd4e5da5Sopenharmony_ci
286fd4e5da5Sopenharmony_ciif(ENABLE_SPIRV_TOOLS_INSTALL)
287fd4e5da5Sopenharmony_ci  if(WIN32 AND NOT MINGW)
288fd4e5da5Sopenharmony_ci    macro(spvtools_config_package_dir TARGET PATH)
289fd4e5da5Sopenharmony_ci      set(${PATH} ${TARGET}/cmake)
290fd4e5da5Sopenharmony_ci    endmacro()
291fd4e5da5Sopenharmony_ci  else()
292fd4e5da5Sopenharmony_ci    macro(spvtools_config_package_dir TARGET PATH)
293fd4e5da5Sopenharmony_ci      set(${PATH} ${CMAKE_INSTALL_LIBDIR}/cmake/${TARGET})
294fd4e5da5Sopenharmony_ci    endmacro()
295fd4e5da5Sopenharmony_ci  endif()
296fd4e5da5Sopenharmony_ci
297fd4e5da5Sopenharmony_ci  macro(spvtools_generate_config_file TARGET)
298fd4e5da5Sopenharmony_ci    file(WRITE ${CMAKE_BINARY_DIR}/${TARGET}Config.cmake
299fd4e5da5Sopenharmony_ci      "include(CMakeFindDependencyMacro)\n"
300fd4e5da5Sopenharmony_ci      "find_dependency(${SPIRV_TOOLS})\n"
301fd4e5da5Sopenharmony_ci      "include(\${CMAKE_CURRENT_LIST_DIR}/${TARGET}Targets.cmake)\n"
302fd4e5da5Sopenharmony_ci      "set(${TARGET}_LIBRARIES ${TARGET})\n"
303fd4e5da5Sopenharmony_ci      "get_target_property(${TARGET}_INCLUDE_DIRS ${TARGET} INTERFACE_INCLUDE_DIRECTORIES)\n")
304fd4e5da5Sopenharmony_ci  endmacro()
305fd4e5da5Sopenharmony_ciendif()
306fd4e5da5Sopenharmony_ci
307fd4e5da5Sopenharmony_ci# Currently iOS and Android are very similar.
308fd4e5da5Sopenharmony_ci# They both have their own packaging (APP/APK).
309fd4e5da5Sopenharmony_ci# Which makes regular executables/testing problematic.
310fd4e5da5Sopenharmony_ci#
311fd4e5da5Sopenharmony_ci# Currently the only deliverables for these platforms are
312fd4e5da5Sopenharmony_ci# libraries (either STATIC or SHARED).
313fd4e5da5Sopenharmony_ci#
314fd4e5da5Sopenharmony_ci# Furthermore testing is equally problematic.
315fd4e5da5Sopenharmony_ciif (IOS OR ANDROID)
316fd4e5da5Sopenharmony_ci  set(SPIRV_SKIP_EXECUTABLES ON)
317fd4e5da5Sopenharmony_ciendif()
318fd4e5da5Sopenharmony_ci
319fd4e5da5Sopenharmony_cioption(SPIRV_SKIP_EXECUTABLES "Skip building the executable and tests along with the library")
320fd4e5da5Sopenharmony_ciif (SPIRV_SKIP_EXECUTABLES)
321fd4e5da5Sopenharmony_ci  set(SPIRV_SKIP_TESTS ON)
322fd4e5da5Sopenharmony_ciendif()
323fd4e5da5Sopenharmony_cioption(SPIRV_SKIP_TESTS "Skip building tests along with the library")
324fd4e5da5Sopenharmony_ci
325fd4e5da5Sopenharmony_ci# Defaults to ON.  The checks can be time consuming.
326fd4e5da5Sopenharmony_ci# Turn off if they take too long.
327fd4e5da5Sopenharmony_cioption(SPIRV_CHECK_CONTEXT "In a debug build, check if the IR context is in a valid state." ON)
328fd4e5da5Sopenharmony_ciif (${SPIRV_CHECK_CONTEXT})
329fd4e5da5Sopenharmony_ci  add_compile_options($<$<CONFIG:Debug>:-DSPIRV_CHECK_CONTEXT>)
330fd4e5da5Sopenharmony_ciendif()
331fd4e5da5Sopenharmony_ci
332fd4e5da5Sopenharmony_ci# Precompiled header macro. Parameters are source file list and filename for pch cpp file.
333fd4e5da5Sopenharmony_cimacro(spvtools_pch SRCS PCHPREFIX)
334fd4e5da5Sopenharmony_ci  if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
335fd4e5da5Sopenharmony_ci    set(PCH_NAME "$(IntDir)\\${PCHPREFIX}.pch")
336fd4e5da5Sopenharmony_ci    # make source files use/depend on PCH_NAME
337fd4e5da5Sopenharmony_ci    set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yu${PCHPREFIX}.h /FI${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
338fd4e5da5Sopenharmony_ci    # make PCHPREFIX.cpp file compile and generate PCH_NAME
339fd4e5da5Sopenharmony_ci    set_source_files_properties("${PCHPREFIX}.cpp" PROPERTIES COMPILE_FLAGS "/Yc${PCHPREFIX}.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
340fd4e5da5Sopenharmony_ci    list(APPEND ${SRCS} "${PCHPREFIX}.cpp")
341fd4e5da5Sopenharmony_ci  endif()
342fd4e5da5Sopenharmony_ciendmacro(spvtools_pch)
343fd4e5da5Sopenharmony_ci
344fd4e5da5Sopenharmony_ciadd_subdirectory(external)
345fd4e5da5Sopenharmony_ci
346fd4e5da5Sopenharmony_ci# Warning about extra semi-colons.
347fd4e5da5Sopenharmony_ci#
348fd4e5da5Sopenharmony_ci# This is not supported on all compilers/versions. so enabling only
349fd4e5da5Sopenharmony_ci# for clang, since that works for all versions that our bots run.
350fd4e5da5Sopenharmony_ci#
351fd4e5da5Sopenharmony_ci# This is intentionally done after adding the external subdirectory,
352fd4e5da5Sopenharmony_ci# so we don't enforce this flag on our dependencies, some of which do
353fd4e5da5Sopenharmony_ci# not pass it.
354fd4e5da5Sopenharmony_ci#
355fd4e5da5Sopenharmony_ci# If the minimum version of CMake supported is updated to 3.0 or
356fd4e5da5Sopenharmony_ci# later, then check_cxx_compiler_flag could be used instead.
357fd4e5da5Sopenharmony_ciif("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
358fd4e5da5Sopenharmony_ci    add_compile_options("-Wextra-semi")
359fd4e5da5Sopenharmony_ciendif()
360fd4e5da5Sopenharmony_ci
361fd4e5da5Sopenharmony_ciadd_subdirectory(source)
362fd4e5da5Sopenharmony_ciadd_subdirectory(tools)
363fd4e5da5Sopenharmony_ci
364fd4e5da5Sopenharmony_ciadd_subdirectory(test)
365fd4e5da5Sopenharmony_ciadd_subdirectory(examples)
366fd4e5da5Sopenharmony_ci
367fd4e5da5Sopenharmony_ciif(ENABLE_SPIRV_TOOLS_INSTALL)
368fd4e5da5Sopenharmony_ci  install(
369fd4e5da5Sopenharmony_ci    FILES
370fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.h
371fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/libspirv.hpp
372fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/optimizer.hpp
373fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/linker.hpp
374fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_SOURCE_DIR}/include/spirv-tools/instrument.hpp
375fd4e5da5Sopenharmony_ci    DESTINATION
376fd4e5da5Sopenharmony_ci      ${CMAKE_INSTALL_INCLUDEDIR}/spirv-tools/)
377fd4e5da5Sopenharmony_ciendif(ENABLE_SPIRV_TOOLS_INSTALL)
378fd4e5da5Sopenharmony_ci
379fd4e5da5Sopenharmony_ciif (NOT "${SPIRV_SKIP_TESTS}")
380fd4e5da5Sopenharmony_ci  add_test(NAME spirv-tools-copyrights
381fd4e5da5Sopenharmony_ci           COMMAND Python3::Interpreter utils/check_copyright.py
382fd4e5da5Sopenharmony_ci           WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
383fd4e5da5Sopenharmony_ciendif()
384fd4e5da5Sopenharmony_ci
385fd4e5da5Sopenharmony_ciset(SPIRV_LIBRARIES "-lSPIRV-Tools-opt -lSPIRV-Tools -lSPIRV-Tools-link")
386fd4e5da5Sopenharmony_ciset(SPIRV_SHARED_LIBRARIES "-lSPIRV-Tools-shared")
387fd4e5da5Sopenharmony_ci
388fd4e5da5Sopenharmony_ci# Build pkg-config file
389fd4e5da5Sopenharmony_ci# Use a first-class target so it's regenerated when relevant files are updated.
390fd4e5da5Sopenharmony_ciadd_custom_target(spirv-tools-pkg-config ALL
391fd4e5da5Sopenharmony_ci        COMMAND ${CMAKE_COMMAND}
392fd4e5da5Sopenharmony_ci                      -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
393fd4e5da5Sopenharmony_ci                      -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools.pc.in
394fd4e5da5Sopenharmony_ci                      -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
395fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
396fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
397fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
398fd4e5da5Sopenharmony_ci                      -DSPIRV_LIBRARIES=${SPIRV_LIBRARIES}
399fd4e5da5Sopenharmony_ci                      -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
400fd4e5da5Sopenharmony_ci        DEPENDS "CHANGES" "cmake/SPIRV-Tools.pc.in" "cmake/write_pkg_config.cmake")
401fd4e5da5Sopenharmony_ciadd_custom_target(spirv-tools-shared-pkg-config ALL
402fd4e5da5Sopenharmony_ci        COMMAND ${CMAKE_COMMAND}
403fd4e5da5Sopenharmony_ci                      -DCHANGES_FILE=${CMAKE_CURRENT_SOURCE_DIR}/CHANGES
404fd4e5da5Sopenharmony_ci                      -DTEMPLATE_FILE=${CMAKE_CURRENT_SOURCE_DIR}/cmake/SPIRV-Tools-shared.pc.in
405fd4e5da5Sopenharmony_ci                      -DOUT_FILE=${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
406fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_PREFIX=${CMAKE_INSTALL_PREFIX}
407fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_LIBDIR=${CMAKE_INSTALL_LIBDIR}
408fd4e5da5Sopenharmony_ci                      -DCMAKE_INSTALL_INCLUDEDIR=${CMAKE_INSTALL_INCLUDEDIR}
409fd4e5da5Sopenharmony_ci                      -DSPIRV_SHARED_LIBRARIES=${SPIRV_SHARED_LIBRARIES}
410fd4e5da5Sopenharmony_ci                      -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/write_pkg_config.cmake
411fd4e5da5Sopenharmony_ci        DEPENDS "CHANGES" "cmake/SPIRV-Tools-shared.pc.in" "cmake/write_pkg_config.cmake")
412fd4e5da5Sopenharmony_ci
413fd4e5da5Sopenharmony_ci# Install pkg-config file
414fd4e5da5Sopenharmony_ciif (ENABLE_SPIRV_TOOLS_INSTALL)
415fd4e5da5Sopenharmony_ci  install(
416fd4e5da5Sopenharmony_ci    FILES
417fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools.pc
418fd4e5da5Sopenharmony_ci      ${CMAKE_CURRENT_BINARY_DIR}/SPIRV-Tools-shared.pc
419fd4e5da5Sopenharmony_ci    DESTINATION
420fd4e5da5Sopenharmony_ci      ${CMAKE_INSTALL_LIBDIR}/pkgconfig)
421fd4e5da5Sopenharmony_ciendif()
422