CMakeLists.txt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. # Copyright (C) 2020 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. project(glslang
  34. LANGUAGES CXX)
  35. # increase to 3.1 once all major distributions
  36. # include a version of CMake >= 3.1
  37. cmake_minimum_required(VERSION 2.8.12)
  38. if (POLICY CMP0048)
  39. cmake_policy(SET CMP0048 NEW)
  40. endif()
  41. set_property(GLOBAL PROPERTY USE_FOLDERS ON)
  42. # Enable compile commands database
  43. set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
  44. # Adhere to GNU filesystem layout conventions
  45. include(GNUInstallDirs)
  46. # Needed for CMAKE_DEPENDENT_OPTION macro
  47. include(CMakeDependentOption)
  48. option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
  49. option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
  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. option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
  62. if(NOT ${SKIP_GLSLANG_INSTALL})
  63. set(ENABLE_GLSLANG_INSTALL ON)
  64. endif()
  65. option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
  66. option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
  67. option(ENABLE_GLSLANG_JS
  68. "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing." OFF)
  69. CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN
  70. "Reduces glslang to minimum needed for web use"
  71. OFF "ENABLE_GLSLANG_JS"
  72. OFF)
  73. CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
  74. "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
  75. OFF "ENABLE_GLSLANG_WEBMIN"
  76. OFF)
  77. CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
  78. "If using Emscripten, enables SINGLE_FILE build"
  79. OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
  80. OFF)
  81. CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
  82. "If using Emscripten, builds to run on Node instead of Web"
  83. OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
  84. OFF)
  85. CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
  86. "Enables HLSL input support"
  87. ON "NOT ENABLE_GLSLANG_WEBMIN"
  88. OFF)
  89. option(ENABLE_RTTI "Enables RTTI" OFF)
  90. option(ENABLE_EXCEPTIONS "Enables Exceptions" OFF)
  91. option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
  92. option(ENABLE_PCH "Enables Precompiled header" ON)
  93. option(ENABLE_CTEST "Enables testing" OFF)
  94. if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
  95. set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
  96. endif()
  97. option(USE_CCACHE "Use ccache" OFF)
  98. if(USE_CCACHE)
  99. find_program(CCACHE_FOUND ccache)
  100. if(CCACHE_FOUND)
  101. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
  102. endif(CCACHE_FOUND)
  103. endif()
  104. if(ENABLE_CTEST)
  105. include(CTest)
  106. endif()
  107. if(ENABLE_HLSL)
  108. add_definitions(-DENABLE_HLSL)
  109. endif(ENABLE_HLSL)
  110. if(ENABLE_GLSLANG_WEBMIN)
  111. add_definitions(-DGLSLANG_WEB)
  112. if(ENABLE_GLSLANG_WEBMIN_DEVEL)
  113. add_definitions(-DGLSLANG_WEB_DEVEL)
  114. endif(ENABLE_GLSLANG_WEBMIN_DEVEL)
  115. endif(ENABLE_GLSLANG_WEBMIN)
  116. if(WIN32)
  117. set(CMAKE_DEBUG_POSTFIX "d")
  118. if(MSVC)
  119. include(ChooseMSVCCRT.cmake)
  120. endif(MSVC)
  121. add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
  122. elseif(UNIX)
  123. add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
  124. else(WIN32)
  125. message("unknown platform")
  126. endif(WIN32)
  127. if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
  128. add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
  129. -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
  130. add_compile_options(-Wno-reorder) # disable this from -Wall, since it happens all over.
  131. if(NOT ENABLE_RTTI)
  132. add_compile_options(-fno-rtti)
  133. endif()
  134. if(NOT ENABLE_EXCEPTIONS)
  135. add_compile_options(-fno-exceptions)
  136. endif()
  137. if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
  138. add_compile_options(-Werror=deprecated-copy)
  139. endif()
  140. if(NOT CMAKE_VERSION VERSION_LESS "3.13")
  141. # Error if there's symbols that are not found at link time.
  142. # add_link_options() was added in CMake 3.13 - if using an earlier
  143. # version don't set this - it should be caught by presubmits anyway.
  144. add_link_options("-Wl,--no-undefined")
  145. endif()
  146. elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
  147. add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
  148. -Wunused-parameter -Wunused-value -Wunused-variable)
  149. add_compile_options(-Wno-reorder) # disable this from -Wall, since it happens all over.
  150. if(NOT ENABLE_RTTI)
  151. add_compile_options(-fno-rtti)
  152. endif()
  153. if(NOT ENABLE_EXCEPTIONS)
  154. add_compile_options(-fno-exceptions)
  155. endif()
  156. if(NOT CMAKE_VERSION VERSION_LESS "3.13")
  157. # Error if there's symbols that are not found at link time.
  158. # add_link_options() was added in CMake 3.13 - if using an earlier
  159. # version don't set this - it should be caught by presubmits anyway.
  160. add_link_options("-Wl,-undefined,error")
  161. endif()
  162. elseif(MSVC)
  163. if(NOT ENABLE_RTTI)
  164. string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
  165. if(MSVC_HAS_GR)
  166. string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  167. else()
  168. add_compile_options(/GR-) # Disable RTTI
  169. endif()
  170. endif()
  171. if(ENABLE_EXCEPTIONS)
  172. add_compile_options(/EHsc) # Enable Exceptions
  173. endif()
  174. endif()
  175. if(ENABLE_GLSLANG_JS)
  176. if(MSVC)
  177. add_compile_options(/Os /GR-)
  178. else()
  179. add_compile_options(-Os -fno-rtti -fno-exceptions)
  180. if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
  181. add_compile_options(-Wno-unused-parameter)
  182. add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
  183. endif()
  184. endif()
  185. endif(ENABLE_GLSLANG_JS)
  186. # Request C++11
  187. if(${CMAKE_VERSION} VERSION_LESS 3.1)
  188. # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
  189. # remove this block once CMake >=3.1 has fixated in the ecosystem
  190. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  191. else()
  192. set(CMAKE_CXX_STANDARD 11)
  193. set(CMAKE_CXX_STANDARD_REQUIRED ON)
  194. set(CMAKE_CXX_EXTENSIONS OFF)
  195. endif()
  196. function(glslang_set_link_args TARGET)
  197. # For MinGW compiles, statically link against the GCC and C++ runtimes.
  198. # This avoids the need to ship those runtimes as DLLs.
  199. if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
  200. set_target_properties(${TARGET} PROPERTIES
  201. LINK_FLAGS "-static -static-libgcc -static-libstdc++")
  202. endif()
  203. endfunction(glslang_set_link_args)
  204. if(NOT COMMAND find_host_package)
  205. macro(find_host_package)
  206. find_package(${ARGN})
  207. endmacro()
  208. endif()
  209. # CMake needs to find the right version of python, right from the beginning,
  210. # otherwise, it will find the wrong version and fail later
  211. find_host_package(PythonInterp 3 REQUIRED)
  212. # Root directory for build-time generated include files
  213. set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
  214. ################################################################################
  215. # Build version information generation
  216. ################################################################################
  217. set(GLSLANG_CHANGES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
  218. set(GLSLANG_BUILD_INFO_PY "${CMAKE_CURRENT_SOURCE_DIR}/build_info.py")
  219. set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
  220. set(GLSLANG_BUILD_INFO_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
  221. # Command to build the build_info.h file
  222. add_custom_command(
  223. OUTPUT ${GLSLANG_BUILD_INFO_H}
  224. COMMAND ${PYTHON_EXECUTABLE} "${GLSLANG_BUILD_INFO_PY}"
  225. ${CMAKE_CURRENT_SOURCE_DIR}
  226. "-i" ${GLSLANG_BUILD_INFO_H_TMPL}
  227. "-o" ${GLSLANG_BUILD_INFO_H}
  228. DEPENDS ${GLSLANG_BUILD_INFO_PY}
  229. ${GLSLANG_CHANGES_FILE}
  230. ${GLSLANG_BUILD_INFO_H_TMPL}
  231. COMMENT "Generating ${GLSLANG_BUILD_INFO_H}")
  232. # Target to build the build_info.h file
  233. add_custom_target(glslang-build-info DEPENDS ${GLSLANG_BUILD_INFO_H})
  234. # Populate the CMake GLSLANG_VERSION* variables with the build version
  235. # information.
  236. execute_process(
  237. COMMAND ${PYTHON_EXECUTABLE} "${GLSLANG_BUILD_INFO_PY}"
  238. ${CMAKE_CURRENT_SOURCE_DIR} "<major>.<minor>.<patch><-flavor>;<major>;<minor>;<patch>;<flavor>"
  239. OUTPUT_VARIABLE "GLSLANG_VERSIONS"
  240. OUTPUT_STRIP_TRAILING_WHITESPACE)
  241. list(GET "GLSLANG_VERSIONS" 0 "GLSLANG_VERSION")
  242. list(GET "GLSLANG_VERSIONS" 1 "GLSLANG_VERSION_MAJOR")
  243. list(GET "GLSLANG_VERSIONS" 2 "GLSLANG_VERSION_MINOR")
  244. list(GET "GLSLANG_VERSIONS" 3 "GLSLANG_VERSION_PATCH")
  245. list(GET "GLSLANG_VERSIONS" 4 "GLSLANG_VERSION_FLAVOR")
  246. configure_file(${GLSLANG_CHANGES_FILE} "${CMAKE_CURRENT_BINARY_DIR}/CHANGES.md") # Required to re-run cmake on version change
  247. # glslang_add_build_info_dependency() adds the glslang-build-info dependency and
  248. # generated include directories to target.
  249. function(glslang_add_build_info_dependency target)
  250. target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
  251. add_dependencies(${target} glslang-build-info)
  252. endfunction()
  253. # glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
  254. # default for <target> when building shared libraries, and sets the
  255. # GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
  256. # building <target>.
  257. function(glslang_only_export_explicit_symbols target)
  258. if(BUILD_SHARED_LIBS)
  259. target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
  260. if(WIN32)
  261. target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
  262. else()
  263. target_compile_options(${target} PRIVATE "-fvisibility=hidden")
  264. endif()
  265. endif()
  266. endfunction()
  267. # glslang_pch() adds precompiled header rules to <target> for the pre-compiled
  268. # header file <pch>. As target_precompile_headers() was added in CMake 3.16,
  269. # this is a no-op if called on earlier versions of CMake.
  270. if(NOT CMAKE_VERSION VERSION_LESS "3.16" AND ENABLE_PCH)
  271. function(glslang_pch target pch)
  272. target_precompile_headers(${target} PRIVATE ${pch})
  273. endfunction()
  274. else()
  275. function(glslang_pch target pch)
  276. endfunction()
  277. if(ENABLE_PCH)
  278. message("Your CMake version is ${CMAKE_VERSION}. Update to at least 3.16 to enable precompiled headers to speed up incremental builds")
  279. endif()
  280. endif()
  281. if(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)
  284. endif()
  285. if(NOT TARGET SPIRV-Tools-opt)
  286. set(ENABLE_OPT OFF)
  287. endif()
  288. if(ENABLE_OPT)
  289. message(STATUS "optimizer enabled")
  290. add_definitions(-DENABLE_OPT=1)
  291. else()
  292. if(ENABLE_HLSL)
  293. message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
  294. endif()
  295. add_definitions(-DENABLE_OPT=0)
  296. endif()
  297. add_subdirectory(glslang)
  298. add_subdirectory(OGLCompilersDLL)
  299. if(ENABLE_GLSLANG_BINARIES)
  300. add_subdirectory(StandAlone)
  301. endif()
  302. add_subdirectory(SPIRV)
  303. if(ENABLE_HLSL)
  304. add_subdirectory(hlsl)
  305. endif(ENABLE_HLSL)
  306. if(ENABLE_CTEST)
  307. add_subdirectory(gtests)
  308. endif()
  309. if(ENABLE_CTEST AND BUILD_TESTING)
  310. # glslang-testsuite runs a bash script on Windows.
  311. # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
  312. set(IGNORE_CR_FLAG "")
  313. if(WIN32)
  314. set(IGNORE_CR_FLAG -o igncr)
  315. endif()
  316. if (CMAKE_CONFIGURATION_TYPES)
  317. set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
  318. set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
  319. set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
  320. else(CMAKE_CONFIGURATION_TYPES)
  321. set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
  322. set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
  323. set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
  324. endif(CMAKE_CONFIGURATION_TYPES)
  325. add_test(NAME glslang-testsuite
  326. COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
  327. WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
  328. endif()