CMakeLists.txt 15 KB

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