CMakeLists.txt.dist 13 KB

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