xref: /aosp_15_r20/external/angle/third_party/glslang/src/CMakeLists.txt (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1# Copyright (C) 2020-2023 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.17.2)
34project(glslang)
35
36if (CMAKE_VERSION VERSION_LESS "3.21")
37    # https://cmake.org/cmake/help/latest/variable/PROJECT_IS_TOP_LEVEL.html
38    string(COMPARE EQUAL ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_SOURCE_DIR} PROJECT_IS_TOP_LEVEL)
39endif()
40
41set(GLSLANG_TESTS_DEFAULT ON) # Can be turned off, below, based on environment.
42set(GLSLANG_ENABLE_INSTALL_DEFAULT ON) # Can be turned off, below, based on environment.
43
44set_property(GLOBAL PROPERTY USE_FOLDERS ON)
45
46# Adhere to GNU filesystem layout conventions
47include(GNUInstallDirs)
48include(CMakePackageConfigHelpers)
49
50# Needed for CMAKE_DEPENDENT_OPTION macro
51include(CMakeDependentOption)
52
53option(BUILD_SHARED_LIBS "Build Shared Libraries")
54option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
55option(BUILD_WERROR "Enable warnings as errors (default is OFF)" OFF)
56
57set(LIB_TYPE STATIC)
58
59if(BUILD_SHARED_LIBS)
60    set(LIB_TYPE SHARED)
61endif()
62
63if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
64    # This logic inside SPIRV-Tools, which can upset build target dependencies
65    # if changed after targets are already defined. To prevent these issues,
66    # ensure CMAKE_BUILD_TYPE is assigned early and at the glslang root scope.
67    message(STATUS "No build type selected, default to Debug")
68    set(CMAKE_BUILD_TYPE "Debug")
69endif()
70
71# Currently iOS and Android are very similar.
72# They both have their own packaging (APP/APK).
73# Which makes regular executables/testing problematic.
74#
75# Currently the only deliverables for these platforms are
76# libraries (either STATIC or SHARED).
77#
78# Furthermore testing is equally problematic.
79if (IOS OR ANDROID)
80    set(ENABLE_GLSLANG_BINARIES OFF)
81    set(GLSLANG_TESTS_DEFAULT OFF)
82endif()
83
84# Simplify the default case of including this project.
85# Otherwise add_subdirectory users have a harder time consuming the library.
86# Since glslang will pollute the installation and add undesirable testing.
87if(NOT PROJECT_IS_TOP_LEVEL)
88    set(GLSLANG_TESTS_DEFAULT OFF)
89    set(GLSLANG_ENABLE_INSTALL_DEFAULT OFF)
90endif()
91
92# Control whether Glslang self-tests are built and tested.
93# Always expose this as an option, so the defaults can be overridden.
94option(GLSLANG_TESTS "Enable glslang testing" ${GLSLANG_TESTS_DEFAULT})
95
96# Control whether to install Glslang.
97# Always expose this as an option, so the defaults can be overridden.
98option(GLSLANG_ENABLE_INSTALL "Enable glslang installation" ${GLSLANG_ENABLE_INSTALL_DEFAULT})
99
100option(ENABLE_SPIRV "Enables SPIRV output support" ON)
101option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
102
103option(ENABLE_GLSLANG_BINARIES "Builds glslang and spirv-remap" ON)
104
105option(ENABLE_GLSLANG_JS "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing.")
106CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
107    "If using Emscripten, enables SINGLE_FILE build"
108    OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
109    OFF)
110CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
111    "If using Emscripten, builds to run on Node instead of Web"
112    OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
113    OFF)
114
115option(ENABLE_HLSL "Enables HLSL input support" ON)
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_definitions(-DGLSLANG_OSINCLUDE_WIN32)
140elseif(UNIX OR ANDROID)
141    add_definitions(-DGLSLANG_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 -fno-exceptions)
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    if(NOT ENABLE_RTTI)
183        string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
184        if(MSVC_HAS_GR)
185            string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
186        else()
187            add_compile_options(/GR-) # Disable RTTI
188        endif()
189    endif()
190    if(ENABLE_EXCEPTIONS)
191        add_compile_options(/EHsc) # Enable Exceptions
192	else()
193        string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag
194        add_compile_options(/D_HAS_EXCEPTIONS=0)
195    endif()
196endif()
197
198# NOTE we could potentially replace this logic with COMPILE_WARNING_AS_ERROR if cmake minimum is bumped to >= 3.24
199if (BUILD_WERROR)
200    if (NOT MSVC)
201        add_compile_options(-Werror)
202    else()
203        add_compile_options(/WX)
204    endif()
205endif()
206
207if(ENABLE_GLSLANG_JS)
208    if(MSVC)
209        add_compile_options(/Os /GR-)
210    else()
211        add_compile_options(-Os -fno-rtti -fno-exceptions)
212        if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
213            add_compile_options(-Wno-unused-parameter)
214            add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
215        endif()
216    endif()
217endif()
218
219# Request C++17
220set(CMAKE_CXX_STANDARD 17)
221set(CMAKE_CXX_STANDARD_REQUIRED ON)
222set(CMAKE_CXX_EXTENSIONS OFF)
223
224function(glslang_set_link_args TARGET)
225    # For MinGW compiles, statically link against the GCC and C++ runtimes.
226    # This avoids the need to ship those runtimes as DLLs.
227    # This is supported by GCC and Clang.
228    if(WIN32 AND NOT MSVC)
229        set_target_properties(${TARGET} PROPERTIES
230                              LINK_FLAGS "-static -static-libgcc -static-libstdc++")
231    endif()
232endfunction(glslang_set_link_args)
233
234# Root directory for build-time generated include files
235set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
236
237################################################################################
238# Build version information generation
239################################################################################
240include(parse_version.cmake)
241set(GLSLANG_CHANGES_FILE      "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
242set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
243set(GLSLANG_BUILD_INFO_H      "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
244
245parse_version(${GLSLANG_CHANGES_FILE} GLSLANG)
246
247function(configurate_version)
248    set(major ${GLSLANG_VERSION_MAJOR})
249    set(minor ${GLSLANG_VERSION_MINOR})
250    set(patch ${GLSLANG_VERSION_PATCH})
251    set(flavor ${GLSLANG_VERSION_FLAVOR})
252    configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY)
253endfunction()
254
255configurate_version()
256
257# glslang_add_build_info_dependency() adds the glslang-build-info dependency and
258# generated include directories to target.
259function(glslang_add_build_info_dependency target)
260    target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
261endfunction()
262
263# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
264# default for <target> when building shared libraries, and sets the
265# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
266# building <target>.
267function(glslang_only_export_explicit_symbols target)
268    if(BUILD_SHARED_LIBS)
269        target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
270        set_target_properties(${target} PROPERTIES CXX_VISIBILITY_PRESET hidden)
271        set_target_properties(${target} PROPERTIES C_VISIBILITY_PRESET hidden)
272        if(WIN32)
273            target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
274        endif()
275    endif()
276endfunction()
277
278# glslang_pch() adds precompiled header rules to <target> for the pre-compiled
279# header file <pch>. As target_precompile_headers() was added in CMake 3.16,
280# this is a no-op if called on earlier versions of CMake.
281function(glslang_pch target pch)
282    if(ENABLE_PCH)
283        target_precompile_headers(${target} PRIVATE ${pch})
284    endif()
285endfunction()
286
287if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
288    # We depend on these for later projects, so they should come first.
289    add_subdirectory(External)
290endif()
291
292option(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")
293if(NOT TARGET SPIRV-Tools-opt)
294    if(ALLOW_EXTERNAL_SPIRV_TOOLS)
295        # Look for external SPIR-V Tools build, if not building in-tree
296        message(STATUS "Trying to find local SPIR-V tools")
297        find_package(SPIRV-Tools-opt)
298        if(NOT TARGET SPIRV-Tools-opt)
299            if(ENABLE_OPT)
300                message(WARNING "ENABLE_OPT set but SPIR-V tools not found! Disabling SPIR-V optimization.")
301            endif()
302            set(ENABLE_OPT OFF)
303        endif()
304    else()
305        if(ENABLE_OPT)
306            message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found. Please run update_glslang_sources.py, "
307                "set the ALLOW_EXTERNAL_SPIRV_TOOLS option to use a local install of SPIRV-Tools, or set ENABLE_OPT=0.")
308        endif()
309    endif()
310endif()
311
312if(ENABLE_OPT)
313    message(STATUS "optimizer enabled")
314    add_definitions(-DENABLE_OPT=1)
315else()
316    if(ENABLE_HLSL)
317        message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
318    endif()
319    add_definitions(-DENABLE_OPT=0)
320endif()
321
322if(ENABLE_SPIRV)
323    add_subdirectory(SPIRV)
324endif()
325add_subdirectory(glslang)
326if(ENABLE_GLSLANG_BINARIES)
327    add_subdirectory(StandAlone)
328endif()
329
330if(GLSLANG_TESTS)
331    enable_testing()
332    add_subdirectory(gtests)
333
334    # glslang-testsuite runs a bash script on Windows.
335    # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
336    set(IGNORE_CR_FLAG "")
337    if(WIN32)
338	set(IGNORE_CR_FLAG -o igncr)
339    endif()
340
341    if (CMAKE_CONFIGURATION_TYPES)
342	set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/localResults)
343	set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/glslang)
344	set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/spirv-remap)
345    else()
346	set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
347	set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslang)
348	set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
349    endif()
350
351    add_test(NAME glslang-testsuite
352	COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
353	WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
354endif(GLSLANG_TESTS)
355
356if (GLSLANG_ENABLE_INSTALL)
357    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[
358        @PACKAGE_INIT@
359        include(CMakeFindDependencyMacro)
360        if(@ENABLE_OPT@)
361            find_dependency(SPIRV-Tools-opt)
362        endif()
363        @INSTALL_CONFIG_UNIX@
364        include("@PACKAGE_PATH_EXPORT_TARGETS@")
365    ]=])
366
367    set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake")
368    if(UNIX OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia")
369        set(INSTALL_CONFIG_UNIX [=[
370            set(THREADS_PREFER_PTHREAD_FLAG ON)
371            find_dependency(Threads)
372        ]=])
373    endif()
374    configure_package_config_file(
375        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in"
376        "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
377        PATH_VARS
378            PATH_EXPORT_TARGETS
379        INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
380    )
381
382    write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
383        VERSION ${GLSLANG_VERSION}
384        COMPATIBILITY SameMajorVersion
385    )
386
387    install(
388        EXPORT      glslang-targets
389        NAMESPACE   "glslang::"
390        DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
391    )
392
393    install(
394        FILES
395            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
396            "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
397        DESTINATION
398            "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
399    )
400endif(GLSLANG_ENABLE_INSTALL)
401