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