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_SPVREMAPPER "Enables building of SPVRemapper" ON) 101 102option(ENABLE_GLSLANG_BINARIES "Builds glslang and spirv-remap" ON) 103 104option(ENABLE_GLSLANG_JS "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing.") 105CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE 106 "If using Emscripten, enables SINGLE_FILE build" 107 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN" 108 OFF) 109CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE 110 "If using Emscripten, builds to run on Node instead of Web" 111 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN" 112 OFF) 113 114option(ENABLE_HLSL "Enables HLSL input support" ON) 115option(ENABLE_RTTI "Enables RTTI") 116option(ENABLE_EXCEPTIONS "Enables Exceptions") 117option(ENABLE_OPT "Enables spirv-opt capability if present" ON) 118 119if(MINGW OR (APPLE AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")) 120 # Workaround for CMake behavior on Mac OS with gcc, cmake generates -Xarch_* arguments 121 # which gcc rejects 122 set(ENABLE_PCH OFF) 123 message(NOTICE "Disabling PCH") 124endif() 125 126option(ENABLE_PCH "Enables Precompiled header" ON) 127 128if(ENABLE_HLSL) 129 add_compile_definitions(ENABLE_HLSL) 130endif() 131 132if(WIN32) 133 set(CMAKE_DEBUG_POSTFIX "d") 134 add_definitions(-DGLSLANG_OSINCLUDE_WIN32) 135elseif(UNIX OR ANDROID) 136 add_definitions(-DGLSLANG_OSINCLUDE_UNIX) 137else() 138 message("unknown platform") 139endif() 140 141if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") 142 add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs -Wimplicit-fallthrough 143 -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions) 144 if(NOT ENABLE_RTTI) 145 add_compile_options(-fno-rtti) 146 endif() 147 if(NOT ENABLE_EXCEPTIONS) 148 add_compile_options(-fno-exceptions) 149 endif() 150 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0") 151 add_compile_options(-Werror=deprecated-copy) 152 endif() 153 154 if(NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")) 155 if (NOT APPLE) 156 # Error if there's symbols that are not found at link time. 157 add_link_options("-Wl,--no-undefined") 158 endif() 159 endif() 160elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC) 161 add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs -Wimplicit-fallthrough 162 -Wunused-parameter -Wunused-value -Wunused-variable) 163 if(NOT ENABLE_RTTI) 164 add_compile_options(-fno-rtti) 165 endif() 166 if(NOT ENABLE_EXCEPTIONS) 167 add_compile_options(-fno-exceptions) 168 endif() 169 170 if(NOT (CMAKE_SYSTEM_NAME MATCHES "OpenBSD|Emscripten")) 171 # Error if there's symbols that are not found at link time. Some linkers do not support this flag. 172 if(NOT APPLE) 173 add_link_options("-Wl,--no-undefined") 174 endif() 175 endif() 176elseif(MSVC) 177 if(NOT ENABLE_RTTI) 178 string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR) 179 if(MSVC_HAS_GR) 180 string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 181 else() 182 add_compile_options(/GR-) # Disable RTTI 183 endif() 184 endif() 185 if(ENABLE_EXCEPTIONS) 186 add_compile_options(/EHsc) # Enable Exceptions 187 else() 188 string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag 189 add_compile_options(/D_HAS_EXCEPTIONS=0) 190 endif() 191endif() 192 193# NOTE we could potentially replace this logic with COMPILE_WARNING_AS_ERROR if cmake minimum is bumped to >= 3.24 194if (BUILD_WERROR) 195 if (NOT MSVC) 196 add_compile_options(-Werror) 197 else() 198 add_compile_options(/WX) 199 endif() 200endif() 201 202if(ENABLE_GLSLANG_JS) 203 if(MSVC) 204 add_compile_options(/Os /GR-) 205 else() 206 add_compile_options(-Os -fno-rtti -fno-exceptions) 207 if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC) 208 add_compile_options(-Wno-unused-parameter) 209 add_compile_options(-Wno-unused-variable -Wno-unused-const-variable) 210 endif() 211 endif() 212endif() 213 214# Request C++17 215set(CMAKE_CXX_STANDARD 17) 216set(CMAKE_CXX_STANDARD_REQUIRED ON) 217set(CMAKE_CXX_EXTENSIONS OFF) 218 219function(glslang_set_link_args TARGET) 220 # For MinGW compiles, statically link against the GCC and C++ runtimes. 221 # This avoids the need to ship those runtimes as DLLs. 222 # This is supported by GCC and Clang. 223 if(WIN32 AND NOT MSVC) 224 set_target_properties(${TARGET} PROPERTIES 225 LINK_FLAGS "-static -static-libgcc -static-libstdc++") 226 endif() 227endfunction(glslang_set_link_args) 228 229# Root directory for build-time generated include files 230set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include") 231 232################################################################################ 233# Build version information generation 234################################################################################ 235include(parse_version.cmake) 236set(GLSLANG_CHANGES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md") 237set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl") 238set(GLSLANG_BUILD_INFO_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h") 239 240parse_version(${GLSLANG_CHANGES_FILE} GLSLANG) 241 242function(configurate_version) 243 set(major ${GLSLANG_VERSION_MAJOR}) 244 set(minor ${GLSLANG_VERSION_MINOR}) 245 set(patch ${GLSLANG_VERSION_PATCH}) 246 set(flavor ${GLSLANG_VERSION_FLAVOR}) 247 configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY) 248endfunction() 249 250configurate_version() 251 252# glslang_add_build_info_dependency() adds the glslang-build-info dependency and 253# generated include directories to target. 254function(glslang_add_build_info_dependency target) 255 target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>) 256endfunction() 257 258# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by 259# default for <target> when building shared libraries, and sets the 260# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically 261# building <target>. 262function(glslang_only_export_explicit_symbols target) 263 if(BUILD_SHARED_LIBS) 264 target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1") 265 set_target_properties(${target} PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidden) 266 if(WIN32) 267 target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1") 268 endif() 269 endif() 270endfunction() 271 272# glslang_pch() adds precompiled header rules to <target> for the pre-compiled 273# header file <pch>. As target_precompile_headers() was added in CMake 3.16, 274# this is a no-op if called on earlier versions of CMake. 275function(glslang_pch target pch) 276 if(ENABLE_PCH) 277 target_precompile_headers(${target} PRIVATE ${pch}) 278 endif() 279endfunction() 280 281if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External) 282 # We depend on these for later projects, so they should come first. 283 add_subdirectory(External) 284endif() 285 286option(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") 287if(NOT TARGET SPIRV-Tools-opt) 288 if(ALLOW_EXTERNAL_SPIRV_TOOLS) 289 # Look for external SPIR-V Tools build, if not building in-tree 290 message(STATUS "Trying to find local SPIR-V tools") 291 find_package(SPIRV-Tools-opt) 292 if(NOT TARGET SPIRV-Tools-opt) 293 if(ENABLE_OPT) 294 message(WARNING "ENABLE_OPT set but SPIR-V tools not found! Disabling SPIR-V optimization.") 295 endif() 296 set(ENABLE_OPT OFF) 297 endif() 298 else() 299 if(ENABLE_OPT) 300 message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found. Please run update_glslang_sources.py, " 301 "set the ALLOW_EXTERNAL_SPIRV_TOOLS option to use a local install of SPIRV-Tools, or set ENABLE_OPT=0.") 302 endif() 303 endif() 304endif() 305 306if(ENABLE_OPT) 307 message(STATUS "optimizer enabled") 308 add_definitions(-DENABLE_OPT=1) 309else() 310 if(ENABLE_HLSL) 311 message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL") 312 endif() 313 add_definitions(-DENABLE_OPT=0) 314endif() 315 316add_subdirectory(glslang) 317if(ENABLE_GLSLANG_BINARIES) 318 add_subdirectory(StandAlone) 319endif() 320add_subdirectory(SPIRV) 321 322if(GLSLANG_TESTS) 323 enable_testing() 324 add_subdirectory(gtests) 325 326 # glslang-testsuite runs a bash script on Windows. 327 # Make sure to use '-o igncr' flag to ignore carriage returns (\r). 328 set(IGNORE_CR_FLAG "") 329 if(WIN32) 330 set(IGNORE_CR_FLAG -o igncr) 331 endif() 332 333 if (CMAKE_CONFIGURATION_TYPES) 334 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/localResults) 335 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/glslang) 336 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/spirv-remap) 337 else() 338 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults) 339 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslang) 340 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap) 341 endif() 342 343 add_test(NAME glslang-testsuite 344 COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH} 345 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/) 346endif(GLSLANG_TESTS) 347 348if (GLSLANG_ENABLE_INSTALL) 349 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[ 350 @PACKAGE_INIT@ 351 include(CMakeFindDependencyMacro) 352 if(@ENABLE_OPT@) 353 find_dependency(SPIRV-Tools-opt) 354 endif() 355 @INSTALL_CONFIG_UNIX@ 356 include("@PACKAGE_PATH_EXPORT_TARGETS@") 357 ]=]) 358 359 set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake") 360 if(UNIX OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia") 361 set(INSTALL_CONFIG_UNIX [=[ 362 set(THREADS_PREFER_PTHREAD_FLAG ON) 363 find_dependency(Threads) 364 ]=]) 365 endif() 366 configure_package_config_file( 367 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" 368 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake" 369 PATH_VARS 370 PATH_EXPORT_TARGETS 371 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} 372 ) 373 374 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake" 375 VERSION ${GLSLANG_VERSION} 376 COMPATIBILITY SameMajorVersion 377 ) 378 379 install( 380 EXPORT glslang-targets 381 NAMESPACE "glslang::" 382 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 383 ) 384 385 install( 386 FILES 387 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake" 388 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake" 389 DESTINATION 390 "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 391 ) 392endif(GLSLANG_ENABLE_INSTALL) 393