yum-mirror/glslang

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

Matt TurnerCheck error message formats with the printf format attribute31b9aacf

main
16.2 KiB435 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    # error() and friends take "" as the format when there is no extra info.
150    add_compile_options(-Wno-format-zero-length)
151    if(NOT ENABLE_RTTI)
152        add_compile_options(-fno-rtti)
153    endif()
154    if(NOT ENABLE_EXCEPTIONS)
155        add_compile_options(-fno-exceptions)
156    endif()
157    if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
158        add_compile_options(-Werror=deprecated-copy)
159    endif()
160
161    if(NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD"))
162        if (NOT APPLE)
163            # Error if there's symbols that are not found at link time.
164            add_link_options("-Wl,--no-undefined")
165        endif()
166    endif()
167elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
168    add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs -Wimplicit-fallthrough
169                        -Wunused-parameter -Wunused-value  -Wunused-variable)
170    if(NOT ENABLE_RTTI)
171        add_compile_options(-fno-rtti)
172    endif()
173    if(NOT ENABLE_EXCEPTIONS)
174        add_compile_options(-fno-exceptions)
175    endif()
176
177    if(NOT (CMAKE_SYSTEM_NAME MATCHES "OpenBSD|Emscripten"))
178        # Error if there's symbols that are not found at link time. Some linkers do not support this flag.
179        if(NOT APPLE)
180            add_link_options("-Wl,--no-undefined")
181        endif()
182    endif()
183elseif(MSVC)
184    # A unary minus operator was applied to an unsigned type, resulting in an unsigned result.
185    add_compile_options(/w14146)
186    # A negative integral constant converted to unsigned type, resulting in a possibly meaningless result.
187    add_compile_options(/w14308)
188    # Use of an uninitialized local variable.
189    add_compile_options(/w14700)
190    # Use of a potentially uninitialized local pointer variable.
191    add_compile_options(/w14703)
192    if(NOT ENABLE_RTTI)
193        add_compile_options(/GR-) # Disable RTTI
194    endif()
195    if(ENABLE_EXCEPTIONS)
196        add_compile_options(/EHsc) # Enable Exceptions
197	else()
198        string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag
199        add_compile_options(/D_HAS_EXCEPTIONS=0)
200    endif()
201endif()
202
203if (PROJECT_IS_TOP_LEVEL)
204    # enable parallel builds for msbuild
205    list(APPEND CMAKE_VS_GLOBALS UseMultiToolTask=true)
206    list(APPEND CMAKE_VS_GLOBALS EnforceProcessCountAcrossBuilds=true)
207endif()
208
209# NOTE we could potentially replace this logic with COMPILE_WARNING_AS_ERROR if cmake minimum is bumped to >= 3.24
210if (BUILD_WERROR)
211    if (NOT MSVC)
212        add_compile_options(-Werror)
213    else()
214        add_compile_options(/WX)
215    endif()
216endif()
217
218if(ENABLE_GLSLANG_JS)
219    if(MSVC)
220        add_compile_options(/Os /GR-)
221    else()
222        add_compile_options(-Os -fno-rtti -fno-exceptions)
223        if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
224            add_compile_options(-Wno-unused-parameter)
225            add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
226        endif()
227    endif()
228endif()
229
230# Request C++17
231set(CMAKE_CXX_STANDARD 17)
232set(CMAKE_CXX_STANDARD_REQUIRED ON)
233set(CMAKE_CXX_EXTENSIONS OFF)
234
235function(glslang_set_link_args TARGET)
236    # For MinGW compiles, statically link against the GCC and C++ runtimes.
237    # This avoids the need to ship those runtimes as DLLs.
238    # This is supported by GCC and Clang.
239    if(MINGW)
240        target_link_options(${TARGET} PRIVATE -static -static-libgcc -static-libstdc++)
241    endif()
242endfunction()
243
244# Root directory for build-time generated include files
245set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
246
247################################################################################
248# Build version information generation
249################################################################################
250include(parse_version.cmake)
251set(GLSLANG_CHANGES_FILE      "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
252set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
253set(GLSLANG_BUILD_INFO_H      "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
254
255parse_version(${GLSLANG_CHANGES_FILE} GLSLANG)
256
257function(configurate_version)
258    set(major ${GLSLANG_VERSION_MAJOR})
259    set(minor ${GLSLANG_VERSION_MINOR})
260    set(patch ${GLSLANG_VERSION_PATCH})
261    set(flavor ${GLSLANG_VERSION_FLAVOR})
262    configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY)
263endfunction()
264
265configurate_version()
266
267# glslang_add_build_info_dependency() adds the glslang-build-info dependency and
268# generated include directories to target.
269function(glslang_add_build_info_dependency target)
270    target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
271endfunction()
272
273# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
274# default for <target> when building shared libraries, and sets the
275# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
276# building <target>.
277function(glslang_only_export_explicit_symbols target)
278    if(BUILD_SHARED_LIBS)
279        target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
280        set_target_properties(${target} PROPERTIES CXX_VISIBILITY_PRESET hidden)
281        set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden)
282        if(WIN32)
283            target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
284        endif()
285    endif()
286endfunction()
287
288# glslang_pch() adds precompiled header rules to <target> for the pre-compiled
289# header file <pch>. As target_precompile_headers() was added in CMake 3.16,
290# this is a no-op if called on earlier versions of CMake.
291function(glslang_pch target pch)
292    if(ENABLE_PCH)
293        target_precompile_headers(${target} PRIVATE ${pch})
294    endif()
295endfunction()
296
297if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
298    # We depend on these for later projects, so they should come first.
299    add_subdirectory(External)
300endif()
301
302option(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")
303if(NOT TARGET SPIRV-Tools-opt)
304    if(ALLOW_EXTERNAL_SPIRV_TOOLS)
305        # Look for external SPIR-V Tools build, if not building in-tree
306        message(STATUS "Trying to find local SPIR-V tools")
307        find_package(SPIRV-Tools-opt)
308        if(NOT TARGET SPIRV-Tools-opt)
309            if(ENABLE_OPT)
310                message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found! Disabling SPIR-V optimization.")
311            endif()
312            set(ENABLE_OPT OFF)
313        endif()
314    else()
315        if(ENABLE_OPT)
316            message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found. Please run update_glslang_sources.py, "
317                "set the ALLOW_EXTERNAL_SPIRV_TOOLS option to use a local install of SPIRV-Tools, or set ENABLE_OPT=0.")
318        endif()
319    endif()
320endif()
321
322if(ENABLE_OPT)
323    message(STATUS "optimizer enabled")
324    add_compile_definitions(ENABLE_OPT=1)
325else()
326    if(ENABLE_HLSL)
327        message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
328    endif()
329    add_compile_definitions(ENABLE_OPT=0)
330endif()
331
332if(ENABLE_SPIRV)
333    add_subdirectory(SPIRV)
334endif()
335add_subdirectory(glslang)
336if(ENABLE_GLSLANG_BINARIES)
337    add_subdirectory(StandAlone)
338endif()
339
340option(ALLOW_EXTERNAL_GTEST "Allows to build against installed googletest. This is unsupported if the commit isn't the one in known_good.json")
341set(GMOCK_TARGET gmock)
342if(NOT TARGET ${GMOCK_TARGET})
343    if(ALLOW_EXTERNAL_GTEST)
344        message(STATUS "Trying to find local googletest")
345        find_package(GTest)
346        if(TARGET GTest::gmock)
347            set(GMOCK_TARGET GTest::gmock)
348        endif()
349    endif()
350endif()
351
352if(GLSLANG_TESTS)
353    enable_testing()
354    add_subdirectory(gtests)
355
356    # glslang-testsuite runs a bash script on Windows.
357    # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
358    set(IGNORE_CR_FLAG "")
359    if(WIN32)
360        set(IGNORE_CR_FLAG -o igncr)
361    endif()
362
363    if (isMultiConfig)
364        set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/localResults)
365        set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/glslang)
366    else()
367        set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
368        set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslang)
369    endif()
370
371    # The TARGET_RUNTIME_DLL_DIRS feature requires CMake 3.27 or greater.
372    if(WIN32 AND BUILD_SHARED_LIBS AND CMAKE_VERSION VERSION_LESS "3.27")
373        message(WARNING "The Windows shared library test configuration requires CMake 3.27 or greater")
374    else()
375	if (ENABLE_HLSL)
376	  set(HLSL_ARG 1)
377	else()
378	  set(HLSL_ARG 0)
379	endif()
380        add_test(NAME glslang-testsuite
381	    COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${HLSL_ARG}
382            WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
383
384        # Prepend paths to shared libraries.
385        if (BUILD_SHARED_LIBS)
386            set_tests_properties(glslang-testsuite PROPERTIES ENVIRONMENT_MODIFICATION "PATH=path_list_prepend:$<JOIN:$<TARGET_RUNTIME_DLL_DIRS:glslang-standalone>,\;>")
387        endif()
388    endif()
389endif()
390
391if (GLSLANG_ENABLE_INSTALL)
392    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[
393        @PACKAGE_INIT@
394        include(CMakeFindDependencyMacro)
395        if(@ENABLE_OPT@)
396            find_dependency(SPIRV-Tools-opt)
397        endif()
398        @INSTALL_CONFIG_UNIX@
399        include("@PACKAGE_PATH_EXPORT_TARGETS@")
400    ]=])
401
402    set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake")
403    if(UNIX OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
404        set(INSTALL_CONFIG_UNIX [=[
405            set(THREADS_PREFER_PTHREAD_FLAG ON)
406            find_dependency(Threads)
407        ]=])
408    endif()
409    configure_package_config_file(
410        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in"
411        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
412        PATH_VARS
413            PATH_EXPORT_TARGETS
414        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
415    )
416
417    write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
418        VERSION ${GLSLANG_VERSION}
419        COMPATIBILITY SameMajorVersion
420    )
421
422    install(
423        EXPORT      glslang-targets
424        NAMESPACE   "glslang::"
425        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
426    )
427
428    install(
429        FILES
430            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
431            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
432        DESTINATION
433            "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
434    )
435endif()