CMakeLists.txt 15 KB

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