CMakeLists.txt 15 KB

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