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_property(GLOBAL PROPERTY USE_FOLDERS ON) 42 43# Adhere to GNU filesystem layout conventions 44include(GNUInstallDirs) 45include(CMakePackageConfigHelpers) 46 47# Needed for CMAKE_DEPENDENT_OPTION macro 48include(CMakeDependentOption) 49 50option(BUILD_SHARED_LIBS "Build Shared Libraries") 51option(BUILD_EXTERNAL "Build external dependencies in /External" ON) 52option(BUILD_WERROR "Enable warnings as errors (default is OFF)" OFF) 53 54set(LIB_TYPE STATIC) 55 56if(BUILD_SHARED_LIBS) 57 set(LIB_TYPE SHARED) 58endif() 59 60if ("${CMAKE_BUILD_TYPE}" STREQUAL "") 61 # This logic inside SPIRV-Tools, which can upset build target dependencies 62 # if changed after targets are already defined. To prevent these issues, 63 # ensure CMAKE_BUILD_TYPE is assigned early and at the glslang root scope. 64 message(STATUS "No build type selected, default to Debug") 65 set(CMAKE_BUILD_TYPE "Debug") 66endif() 67 68# Currently iOS and Android are very similar. 69# They both have their own packaging (APP/APK). 70# Which makes regular executables/testing problematic. 71# 72# Currently the only deliverables for these platforms are 73# libraries (either STATIC or SHARED). 74# 75# Furthermore testing is equally problematic. 76if (IOS OR ANDROID) 77 set(ENABLE_GLSLANG_BINARIES OFF) 78 set(GLSLANG_TESTS OFF) 79endif() 80 81option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON) 82 83option(ENABLE_GLSLANG_BINARIES "Builds glslang and spirv-remap" ON) 84 85option(ENABLE_GLSLANG_JS "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing.") 86CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE 87 "If using Emscripten, enables SINGLE_FILE build" 88 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN" 89 OFF) 90CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE 91 "If using Emscripten, builds to run on Node instead of Web" 92 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN" 93 OFF) 94 95option(ENABLE_HLSL "Enables HLSL input support" ON) 96option(ENABLE_RTTI "Enables RTTI") 97option(ENABLE_EXCEPTIONS "Enables Exceptions") 98option(ENABLE_OPT "Enables spirv-opt capability if present" ON) 99 100if(MINGW OR (APPLE AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")) 101 # Workaround for CMake behavior on Mac OS with gcc, cmake generates -Xarch_* arguments 102 # which gcc rejects 103 set(ENABLE_PCH OFF) 104 message(NOTICE "Disabling PCH") 105endif() 106 107option(ENABLE_PCH "Enables Precompiled header" ON) 108 109if(ENABLE_HLSL) 110 add_compile_definitions(ENABLE_HLSL) 111endif() 112 113if(WIN32) 114 set(CMAKE_DEBUG_POSTFIX "d") 115 add_definitions(-DGLSLANG_OSINCLUDE_WIN32) 116elseif(UNIX OR ANDROID) 117 add_definitions(-DGLSLANG_OSINCLUDE_UNIX) 118else() 119 message("unknown platform") 120endif() 121 122if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU") 123 add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs 124 -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions) 125 if(NOT ENABLE_RTTI) 126 add_compile_options(-fno-rtti) 127 endif() 128 if(NOT ENABLE_EXCEPTIONS) 129 add_compile_options(-fno-exceptions) 130 endif() 131 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0") 132 add_compile_options(-Werror=deprecated-copy) 133 endif() 134 135 if(NOT (CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")) 136 if (NOT APPLE) 137 # Error if there's symbols that are not found at link time. 138 add_link_options("-Wl,--no-undefined") 139 endif() 140 endif() 141elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC) 142 add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs 143 -Wunused-parameter -Wunused-value -Wunused-variable) 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 151 if(NOT (CMAKE_SYSTEM_NAME MATCHES "OpenBSD|Emscripten")) 152 # Error if there's symbols that are not found at link time. Some linkers do not support this flag. 153 if(NOT APPLE) 154 add_link_options("-Wl,--no-undefined") 155 endif() 156 endif() 157elseif(MSVC) 158 if(NOT ENABLE_RTTI) 159 string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR) 160 if(MSVC_HAS_GR) 161 string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 162 else() 163 add_compile_options(/GR-) # Disable RTTI 164 endif() 165 endif() 166 if(ENABLE_EXCEPTIONS) 167 add_compile_options(/EHsc) # Enable Exceptions 168 else() 169 string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag 170 add_compile_options(/D_HAS_EXCEPTIONS=0) 171 endif() 172endif() 173 174# NOTE we could potentially replace this logic with COMPILE_WARNING_AS_ERROR if cmake minimum is bumped to >= 3.24 175if (BUILD_WERROR) 176 if (NOT MSVC) 177 add_compile_options(-Werror) 178 else() 179 add_compile_options(/WX) 180 endif() 181endif() 182 183if(ENABLE_GLSLANG_JS) 184 if(MSVC) 185 add_compile_options(/Os /GR-) 186 else() 187 add_compile_options(-Os -fno-rtti -fno-exceptions) 188 if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC) 189 add_compile_options(-Wno-unused-parameter) 190 add_compile_options(-Wno-unused-variable -Wno-unused-const-variable) 191 endif() 192 endif() 193endif() 194 195# Request C++17 196set(CMAKE_CXX_STANDARD 17) 197set(CMAKE_CXX_STANDARD_REQUIRED ON) 198set(CMAKE_CXX_EXTENSIONS OFF) 199 200function(glslang_set_link_args TARGET) 201 # For MinGW compiles, statically link against the GCC and C++ runtimes. 202 # This avoids the need to ship those runtimes as DLLs. 203 # This is supported by GCC and Clang. 204 if(WIN32 AND NOT MSVC) 205 set_target_properties(${TARGET} PROPERTIES 206 LINK_FLAGS "-static -static-libgcc -static-libstdc++") 207 endif() 208endfunction(glslang_set_link_args) 209 210# Root directory for build-time generated include files 211set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include") 212 213################################################################################ 214# Build version information generation 215################################################################################ 216include(parse_version.cmake) 217set(GLSLANG_CHANGES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md") 218set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl") 219set(GLSLANG_BUILD_INFO_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h") 220 221parse_version(${GLSLANG_CHANGES_FILE} GLSLANG) 222 223function(configurate_version) 224 set(major ${GLSLANG_VERSION_MAJOR}) 225 set(minor ${GLSLANG_VERSION_MINOR}) 226 set(patch ${GLSLANG_VERSION_PATCH}) 227 set(flavor ${GLSLANG_VERSION_FLAVOR}) 228 configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY) 229endfunction() 230 231configurate_version() 232 233# glslang_add_build_info_dependency() adds the glslang-build-info dependency and 234# generated include directories to target. 235function(glslang_add_build_info_dependency target) 236 target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>) 237endfunction() 238 239# glslang_only_export_explicit_symbols() makes the symbol visibility hidden by 240# default for <target> when building shared libraries, and sets the 241# GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically 242# building <target>. 243function(glslang_only_export_explicit_symbols target) 244 if(BUILD_SHARED_LIBS) 245 target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1") 246 set_target_properties(${target} PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidden) 247 if(WIN32) 248 target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1") 249 endif() 250 endif() 251endfunction() 252 253# glslang_pch() adds precompiled header rules to <target> for the pre-compiled 254# header file <pch>. As target_precompile_headers() was added in CMake 3.16, 255# this is a no-op if called on earlier versions of CMake. 256function(glslang_pch target pch) 257 if(ENABLE_PCH) 258 target_precompile_headers(${target} PRIVATE ${pch}) 259 endif() 260endfunction() 261 262if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External) 263 # We depend on these for later projects, so they should come first. 264 add_subdirectory(External) 265endif() 266 267option(ALLOW_EXTERNAL_SPIRV_TOOLS "Allows to build against installed SPIRV-Tools-opt") 268if(NOT TARGET SPIRV-Tools-opt) 269 if(ALLOW_EXTERNAL_SPIRV_TOOLS) 270 # Look for external SPIR-V Tools build, if not building in-tree 271 message(STATUS "Trying to find local SPIR-V tools") 272 find_package(SPIRV-Tools-opt) 273 if(NOT TARGET SPIRV-Tools-opt) 274 if(ENABLE_OPT) 275 message(WARNING "ENABLE_OPT set but SPIR-V tools not found! Disabling SPIR-V optimization.") 276 endif() 277 set(ENABLE_OPT OFF) 278 endif() 279 else() 280 if(ENABLE_OPT) 281 message(SEND_ERROR "ENABLE_OPT set but SPIR-V tools not found. Please run update_glslang_sources.py, " 282 "set the ALLOW_EXTERNAL_SPIRV_TOOLS option to use a local install of SPIRV-Tools, or set ENABLE_OPT=0.") 283 endif() 284 endif() 285endif() 286 287if(ENABLE_OPT) 288 message(STATUS "optimizer enabled") 289 add_definitions(-DENABLE_OPT=1) 290else() 291 if(ENABLE_HLSL) 292 message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL") 293 endif() 294 add_definitions(-DENABLE_OPT=0) 295endif() 296 297add_subdirectory(glslang) 298if(ENABLE_GLSLANG_BINARIES) 299 add_subdirectory(StandAlone) 300endif() 301add_subdirectory(SPIRV) 302 303# Testing / installation only makes sense when the project is top level. 304# 305# Otherwise add_subdirectory users have a harder time consuming the library. 306# Since glslang will pollute the installation and add undesirable testing. 307if(PROJECT_IS_TOP_LEVEL) 308 option(GLSLANG_TESTS "Enable glslang testing") 309 if(GLSLANG_TESTS) 310 enable_testing() 311 add_subdirectory(gtests) 312 313 # glslang-testsuite runs a bash script on Windows. 314 # Make sure to use '-o igncr' flag to ignore carriage returns (\r). 315 set(IGNORE_CR_FLAG "") 316 if(WIN32) 317 set(IGNORE_CR_FLAG -o igncr) 318 endif() 319 320 if (CMAKE_CONFIGURATION_TYPES) 321 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIG>/localResults) 322 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/glslang) 323 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIG>/spirv-remap) 324 else() 325 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults) 326 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslang) 327 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap) 328 endif() 329 330 add_test(NAME glslang-testsuite 331 COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH} 332 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/) 333 endif() 334 335 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[ 336 @PACKAGE_INIT@ 337 @INSTALL_CONFIG_UNIX@ 338 include("@PACKAGE_PATH_EXPORT_TARGETS@") 339 ]=]) 340 341 set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake") 342 if(UNIX OR "${CMAKE_SYSTEM_NAME}" STREQUAL "Fuchsia") 343 set(INSTALL_CONFIG_UNIX [=[ 344 include(CMakeFindDependencyMacro) 345 set(THREADS_PREFER_PTHREAD_FLAG ON) 346 find_dependency(Threads REQUIRED) 347 ]=]) 348 endif() 349 configure_package_config_file( 350 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" 351 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake" 352 PATH_VARS 353 PATH_EXPORT_TARGETS 354 INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME} 355 ) 356 357 write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake" 358 VERSION ${GLSLANG_VERSION} 359 COMPATIBILITY SameMajorVersion 360 ) 361 362 install( 363 EXPORT glslang-targets 364 NAMESPACE "glslang::" 365 DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 366 ) 367 368 install( 369 FILES 370 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake" 371 "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake" 372 DESTINATION 373 "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}" 374 ) 375endif() 376