yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

Diego NovilloDeprecate the HLSL front-end.9593cbf4

16.3.0
16.0 KiB428 linesraw
1# Copyright (C) 2020-2025 The Khronos Group Inc.
2#
3# All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8#
9#    Redistributions of source code must retain the above copyright
10#    notice, this list of conditions and the following disclaimer.
11#
12#    Redistributions in binary form must reproduce the above
13#    copyright notice, this list of conditions and the following
14#    disclaimer in the documentation and/or other materials provided
15#    with the distribution.
16#
17#    Neither the name of The Khronos Group Inc. nor the names of its
18#    contributors may be used to endorse or promote products derived
19#    from this software without specific prior written permission.
20#
21# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32# POSSIBILITY OF SUCH DAMAGE.
33cmake_minimum_required(VERSION 3.22.1)
34project(glslang)
35
36set(GLSLANG_TESTS_DEFAULT ON) # Can be turned off, below, based on environment.
37set(GLSLANG_ENABLE_INSTALL_DEFAULT ON) # Can be turned off, below, based on environment.
38
39set_property(GLOBAL PROPERTY USE_FOLDERS ON) # Can be removed when min is 3.26 see policy CMP0143.
40
41# Adhere to GNU filesystem layout conventions
42include(GNUInstallDirs)
43include(CMakePackageConfigHelpers)
44
45# Needed for cmake_dependent_option macro
46include(CMakeDependentOption)
47
48option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
49option(BUILD_WERROR "Enable warnings as errors (default is OFF)" OFF)
50
51set(LIB_TYPE STATIC)
52
53if(BUILD_SHARED_LIBS)
54    set(LIB_TYPE SHARED)
55endif()
56
57get_cmake_property(isMultiConfig "GENERATOR_IS_MULTI_CONFIG")
58if (NOT isMultiConfig AND NOT CMAKE_BUILD_TYPE)
59    # This logic inside SPIRV-Tools, which can upset build target dependencies
60    # if changed after targets are already defined. To prevent these issues,
61    # ensure CMAKE_BUILD_TYPE is assigned early and at the glslang root scope.
62    message(STATUS "No build type selected, default to Debug")
63    set(CMAKE_BUILD_TYPE "Debug")
64endif()
65
66# Currently iOS and Android are very similar.
67# They both have their own packaging (APP/APK).
68# Which makes regular executables/testing problematic.
69#
70# Currently the only deliverables for these platforms are
71# libraries (either STATIC or SHARED).
72#
73# Furthermore testing is equally problematic.
74if (IOS OR ANDROID)
75    set(ENABLE_GLSLANG_BINARIES OFF)
76    set(GLSLANG_TESTS_DEFAULT OFF)
77endif()
78
79# Simplify the default case of including this project.
80# Otherwise add_subdirectory users have a harder time consuming the library.
81# Since glslang will pollute the installation and add undesirable testing.
82if(NOT PROJECT_IS_TOP_LEVEL)
83    set(GLSLANG_TESTS_DEFAULT OFF)
84    set(GLSLANG_ENABLE_INSTALL_DEFAULT OFF)
85endif()
86
87# Control whether Glslang self-tests are built and tested.
88# Always expose this as an option, so the defaults can be overridden.
89option(GLSLANG_TESTS "Enable glslang testing" ${GLSLANG_TESTS_DEFAULT})
90
91# Control whether to install Glslang.
92# Always expose this as an option, so the defaults can be overridden.
93option(GLSLANG_ENABLE_INSTALL "Enable glslang installation" ${GLSLANG_ENABLE_INSTALL_DEFAULT})
94
95option(ENABLE_SPIRV "Enables SPIRV output support" ON)
96
97option(ENABLE_GLSLANG_BINARIES "Builds glslang" ON)
98
99option(ENABLE_GLSLANG_JS "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing.")
100cmake_dependent_option(ENABLE_EMSCRIPTEN_SINGLE_FILE
101    "If using Emscripten, enables SINGLE_FILE build"
102    OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
103    OFF)
104cmake_dependent_option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
105    "If using Emscripten, builds to run on Node instead of Web"
106    OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
107    OFF)
108
109option(ENABLE_HLSL "Enables HLSL input support" ON)
110if(ENABLE_HLSL)
111    message(DEPRECATION
112        "ENABLE_HLSL is deprecated. The HLSL front-end will be removed in "
113        "a future major release of glslang. "
114        "See https://github.com/KhronosGroup/glslang/issues/4210 for details.")
115endif()
116option(ENABLE_RTTI "Enables RTTI")
117option(ENABLE_EXCEPTIONS "Enables Exceptions")
118cmake_dependent_option(ENABLE_OPT "Enables spirv-opt capability if present" ON "ENABLE_SPIRV" OFF)
119
120if(MINGW OR (APPLE AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU"))
121    # Workaround for CMake behavior on Mac OS with gcc, cmake generates -Xarch_* arguments
122    # which gcc rejects
123    set(ENABLE_PCH OFF)
124    message(NOTICE "Disabling PCH")
125endif()
126
127option(ENABLE_PCH "Enables Precompiled header" ON)
128
129if(ENABLE_SPIRV)
130    add_compile_definitions(ENABLE_SPIRV)
131endif()
132
133if(ENABLE_HLSL)
134    add_compile_definitions(ENABLE_HLSL)
135endif()
136
137if(WIN32)
138    set(CMAKE_DEBUG_POSTFIX "d")
139    add_compile_definitions(GLSLANG_OSINCLUDE_WIN32)
140elseif(UNIX OR ANDROID)
141    add_compile_definitions(GLSLANG_OSINCLUDE_UNIX)
142else()
143    message("unknown platform")
144endif()
145
146if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
147    add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs -Wimplicit-fallthrough
148                        -Wunused-parameter -Wunused-value  -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable)
149    if(NOT ENABLE_RTTI)
150        add_compile_options(-fno-rtti)
151    endif()
152    if(NOT ENABLE_EXCEPTIONS)
153        add_compile_options(-fno-exceptions)
154    endif()
155    if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
156        add_compile_options(-Werror=deprecated-copy)
157    endif()
158
159    if(NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD"))
160        if (NOT APPLE)
161            # Error if there's symbols that are not found at link time.
162            add_link_options("-Wl,--no-undefined")
163        endif()
164    endif()
165elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
166    add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs -Wimplicit-fallthrough
167                        -Wunused-parameter -Wunused-value  -Wunused-variable)
168    if(NOT ENABLE_RTTI)
169        add_compile_options(-fno-rtti)
170    endif()
171    if(NOT ENABLE_EXCEPTIONS)
172        add_compile_options(-fno-exceptions)
173    endif()
174
175    if(NOT (CMAKE_SYSTEM_NAME MATCHES "OpenBSD|Emscripten"))
176        # Error if there's symbols that are not found at link time. Some linkers do not support this flag.
177        if(NOT APPLE)
178            add_link_options("-Wl,--no-undefined")
179        endif()
180    endif()
181elseif(MSVC)
182    # A unary minus operator was applied to an unsigned type, resulting in an unsigned result.
183    add_compile_options(/w14146)
184    # A negative integral constant converted to unsigned type, resulting in a possibly meaningless result.
185    add_compile_options(/w14308)
186    # Use of an uninitialized local variable.
187    add_compile_options(/w14700)
188    # Use of a potentially uninitialized local pointer variable.
189    add_compile_options(/w14703)
190    if(NOT ENABLE_RTTI)
191        add_compile_options(/GR-) # Disable RTTI
192    endif()
193    if(ENABLE_EXCEPTIONS)
194        add_compile_options(/EHsc) # Enable Exceptions
195	else()
196        string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag
197        add_compile_options(/D_HAS_EXCEPTIONS=0)
198    endif()
199endif()
200
201if (PROJECT_IS_TOP_LEVEL)
202    # enable parallel builds for msbuild
203    list(APPEND CMAKE_VS_GLOBALS UseMultiToolTask=true)
204    list(APPEND CMAKE_VS_GLOBALS EnforceProcessCountAcrossBuilds=true)
205endif()
206
207# NOTE we could potentially replace this logic with COMPILE_WARNING_AS_ERROR if cmake minimum is bumped to >= 3.24
208if (BUILD_WERROR)
209    if (NOT MSVC)
210        add_compile_options(-Werror)
211    else()
212        add_compile_options(/WX)
213    endif()
214endif()
215
216if(ENABLE_GLSLANG_JS)
217    if(MSVC)
218        add_compile_options(/Os /GR-)
219    else()
220        add_compile_options(-Os -fno-rtti -fno-exceptions)
221        if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
222            add_compile_options(-Wno-unused-parameter)
223            add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
224        endif()
225    endif()
226endif()
227
228# Request C++17
229set(CMAKE_CXX_STANDARD 17)
230set(CMAKE_CXX_STANDARD_REQUIRED ON)
231set(CMAKE_CXX_EXTENSIONS OFF)
232
233function(glslang_set_link_args TARGET)
234    # For MinGW compiles, statically link against the GCC and C++ runtimes.
235    # This avoids the need to ship those runtimes as DLLs.
236    # This is supported by GCC and Clang.
237    if(MINGW)
238        target_link_options(${TARGET} PRIVATE -static -static-libgcc -static-libstdc++)
239    endif()
240endfunction()
241
242# Root directory for build-time generated include files
243set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
244
245################################################################################
246# Build version information generation
247################################################################################
248include(parse_version.cmake)
249set(GLSLANG_CHANGES_FILE      "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
250set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
251set(GLSLANG_BUILD_INFO_H      "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
252
253parse_version(${GLSLANG_CHANGES_FILE} GLSLANG)
254
255function(configurate_version)
256    set(major ${GLSLANG_VERSION_MAJOR})
257    set(minor ${GLSLANG_VERSION_MINOR})
258    set(patch ${GLSLANG_VERSION_PATCH})
259    set(flavor ${GLSLANG_VERSION_FLAVOR})
260    configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY)
261endfunction()
262
263configurate_version()
264
265# glslang_add_build_info_dependency() adds the glslang-build-info dependency and
266# generated include directories to target.
267function(glslang_add_build_info_dependency target)
268    target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
269endfunction()
270
271# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
272# default for <target> when building shared libraries, and sets the
273# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
274# building <target>.
275function(glslang_only_export_explicit_symbols target)
276    if(BUILD_SHARED_LIBS)
277        target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
278        set_target_properties(${target} PROPERTIES CXX_VISIBILITY_PRESET hidden)
279        set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden)
280        if(WIN32)
281            target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
282        endif()
283    endif()
284endfunction()
285
286# glslang_pch() adds precompiled header rules to <target> for the pre-compiled
287# header file <pch>. As target_precompile_headers() was added in CMake 3.16,
288# this is a no-op if called on earlier versions of CMake.
289function(glslang_pch target pch)
290    if(ENABLE_PCH)
291        target_precompile_headers(${target} PRIVATE ${pch})
292    endif()
293endfunction()
294
295if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
296    # We depend on these for later projects, so they should come first.
297    add_subdirectory(External)
298endif()
299
300option(ALLOW_EXTERNAL_SPIRV_TOOLS "Allows to build against installed SPIRV-Tools-opt. This is unsupported if the commit isn't the one in known_good.json")
301if(NOT TARGET SPIRV-Tools-opt)
302    if(ALLOW_EXTERNAL_SPIRV_TOOLS)
303        # Look for external SPIR-V Tools build, if not building in-tree
304        message(STATUS "Trying to find local SPIR-V tools")
305        find_package(SPIRV-Tools-opt)
306        if(NOT TARGET SPIRV-Tools-opt)
307            if(ENABLE_OPT)
308                message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found! Disabling SPIR-V optimization.")
309            endif()
310            set(ENABLE_OPT OFF)
311        endif()
312    else()
313        if(ENABLE_OPT)
314            message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found. Please run update_glslang_sources.py, "
315                "set the ALLOW_EXTERNAL_SPIRV_TOOLS option to use a local install of SPIRV-Tools, or set ENABLE_OPT=0.")
316        endif()
317    endif()
318endif()
319
320if(ENABLE_OPT)
321    message(STATUS "optimizer enabled")
322    add_compile_definitions(ENABLE_OPT=1)
323else()
324    if(ENABLE_HLSL)
325        message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
326    endif()
327    add_compile_definitions(ENABLE_OPT=0)
328endif()
329
330if(ENABLE_SPIRV)
331    add_subdirectory(SPIRV)
332endif()
333add_subdirectory(glslang)
334if(ENABLE_GLSLANG_BINARIES)
335    add_subdirectory(StandAlone)
336endif()
337
338option(ALLOW_EXTERNAL_GTEST "Allows to build against installed googletest. This is unsupported if the commit isn't the one in known_good.json")
339set(GMOCK_TARGET gmock)
340if(NOT TARGET ${GMOCK_TARGET})
341    if(ALLOW_EXTERNAL_GTEST)
342        message(STATUS "Trying to find local googletest")
343        find_package(GTest)
344        if(TARGET GTest::gmock)
345            set(GMOCK_TARGET GTest::gmock)
346        endif()
347    endif()
348endif()
349
350if(GLSLANG_TESTS)
351    enable_testing()
352    add_subdirectory(gtests)
353
354    # glslang-testsuite runs a bash script on Windows.
355    # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
356    set(IGNORE_CR_FLAG "")
357    if(WIN32)
358        set(IGNORE_CR_FLAG -o igncr)
359    endif()
360
361    if (isMultiConfig)
362        set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/localResults)
363        set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/glslang)
364    else()
365        set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
366        set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslang)
367    endif()
368
369    # The TARGET_RUNTIME_DLL_DIRS feature requires CMake 3.27 or greater.
370    if(WIN32 AND BUILD_SHARED_LIBS AND CMAKE_VERSION VERSION_LESS "3.27")
371        message(WARNING "The Windows shared library test configuration requires CMake 3.27 or greater")
372    else()
373        add_test(NAME glslang-testsuite
374            COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH}
375            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
376
377        # Prepend paths to shared libraries.
378        if (BUILD_SHARED_LIBS)
379            set_tests_properties(glslang-testsuite PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<JOIN:$<TARGET_RUNTIME_DLL_DIRS:glslang-standalone>,\;>")
380        endif()
381    endif()
382endif()
383
384if (GLSLANG_ENABLE_INSTALL)
385    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[
386        @PACKAGE_INIT@
387        include(CMakeFindDependencyMacro)
388        if(@ENABLE_OPT@)
389            find_dependency(SPIRV-Tools-opt)
390        endif()
391        @INSTALL_CONFIG_UNIX@
392        include("@PACKAGE_PATH_EXPORT_TARGETS@")
393    ]=])
394
395    set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake")
396    if(UNIX OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
397        set(INSTALL_CONFIG_UNIX [=[
398            set(THREADS_PREFER_PTHREAD_FLAG ON)
399            find_dependency(Threads)
400        ]=])
401    endif()
402    configure_package_config_file(
403        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in"
404        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
405        PATH_VARS
406            PATH_EXPORT_TARGETS
407        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
408    )
409
410    write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
411        VERSION ${GLSLANG_VERSION}
412        COMPATIBILITY SameMajorVersion
413    )
414
415    install(
416        EXPORT      glslang-targets
417        NAMESPACE   "glslang::"
418        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
419    )
420
421    install(
422        FILES
423            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
424            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
425        DESTINATION
426            "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
427    )
428endif()