1 # Copyright (C) 2020 The Khronos Group Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
9 # Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
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.
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.
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.
34 # increase to 3.1 once all major distributions
35 # include a version of CMake >= 3.1
36 cmake_minimum_required(VERSION 2.8.12)
38 cmake_policy(SET CMP0048 NEW)
41 cmake_policy(SET CMP0054 NEW)
44 project(glslang LANGUAGES CXX)
46 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
48 # Enable compile commands database
49 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
51 # Adhere to GNU filesystem layout conventions
52 include(GNUInstallDirs)
54 # Needed for CMAKE_DEPENDENT_OPTION macro
55 include(CMakeDependentOption)
57 option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
58 option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
66 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
67 # This logic inside SPIRV-Tools, which can upset build target dependencies
68 # if changed after targets are already defined. To prevent these issues,
69 # ensure CMAKE_BUILD_TYPE is assigned early and at the glslang root scope.
70 message(STATUS "No build type selected, default to Debug")
71 set(CMAKE_BUILD_TYPE "Debug")
74 option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
75 if(NOT ${SKIP_GLSLANG_INSTALL})
76 set(ENABLE_GLSLANG_INSTALL ON)
78 option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
80 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
82 option(ENABLE_GLSLANG_JS
83 "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing." OFF)
84 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN
85 "Reduces glslang to minimum needed for web use"
86 OFF "ENABLE_GLSLANG_JS"
88 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
89 "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
90 OFF "ENABLE_GLSLANG_WEBMIN"
92 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
93 "If using Emscripten, enables SINGLE_FILE build"
94 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
96 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
97 "If using Emscripten, builds to run on Node instead of Web"
98 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
101 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
102 "Enables HLSL input support"
103 ON "NOT ENABLE_GLSLANG_WEBMIN"
106 option(ENABLE_RTTI "Enables RTTI" OFF)
107 option(ENABLE_EXCEPTIONS "Enables Exceptions" OFF)
108 option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
110 if(MINGW OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU"))
111 # Workaround for CMake behavior on Mac OS with gcc, cmake generates -Xarch_* arguments
113 option(ENABLE_PCH "Enables Precompiled header" OFF)
115 option(ENABLE_PCH "Enables Precompiled header" ON)
117 option(ENABLE_CTEST "Enables testing" ON)
119 if(ENABLE_GLSLANG_INSTALL AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
120 set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
123 option(USE_CCACHE "Use ccache" OFF)
125 find_program(CCACHE_FOUND ccache)
127 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
136 add_definitions(-DENABLE_HLSL)
139 if(ENABLE_GLSLANG_WEBMIN)
140 add_definitions(-DGLSLANG_WEB)
141 if(ENABLE_GLSLANG_WEBMIN_DEVEL)
142 add_definitions(-DGLSLANG_WEB_DEVEL)
147 set(CMAKE_DEBUG_POSTFIX "d")
148 option(OVERRIDE_MSVCCRT "Overrides runtime of MSVC " ON)
149 if(MSVC AND OVERRIDE_MSVCCRT)
150 include(ChooseMSVCCRT.cmake)
152 add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
154 add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
156 message("unknown platform")
159 if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
160 add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
161 -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
162 add_compile_options(-Wno-reorder) # disable this from -Wall, since it happens all over.
164 add_compile_options(-fno-rtti)
166 if(NOT ENABLE_EXCEPTIONS)
167 add_compile_options(-fno-exceptions)
169 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
170 add_compile_options(-Werror=deprecated-copy)
173 if(NOT CMAKE_VERSION VERSION_LESS "3.13" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
174 # Error if there's symbols that are not found at link time.
175 # add_link_options() was added in CMake 3.13 - if using an earlier
176 # version don't set this - it should be caught by presubmits anyway.
177 add_link_options("-Wl,--no-undefined")
179 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
180 add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
181 -Wunused-parameter -Wunused-value -Wunused-variable)
182 add_compile_options(-Wno-reorder) # disable this from -Wall, since it happens all over.
184 add_compile_options(-fno-rtti)
186 if(NOT ENABLE_EXCEPTIONS)
187 add_compile_options(-fno-exceptions)
190 if(NOT CMAKE_VERSION VERSION_LESS "3.13")
191 # Error if there's symbols that are not found at link time.
192 # add_link_options() was added in CMake 3.13 - if using an earlier
193 # version don't set this - it should be caught by presubmits anyway.
195 add_link_options("-Wl,--no-undefined")
197 add_link_options("-Wl,-undefined,error")
202 string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
204 string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
206 add_compile_options(/GR-) # Disable RTTI
209 if(ENABLE_EXCEPTIONS)
210 add_compile_options(/EHsc) # Enable Exceptions
212 string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag
213 add_compile_options(/D_HAS_EXCEPTIONS=0)
217 if(ENABLE_GLSLANG_JS)
219 add_compile_options(/Os /GR-)
221 add_compile_options(-Os -fno-rtti -fno-exceptions)
222 if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
223 add_compile_options(-Wno-unused-parameter)
224 add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
230 if(${CMAKE_VERSION} VERSION_LESS 3.1)
231 # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
232 # remove this block once CMake >=3.1 has fixated in the ecosystem
233 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
235 set(CMAKE_CXX_STANDARD 11)
236 set(CMAKE_CXX_STANDARD_REQUIRED ON)
237 set(CMAKE_CXX_EXTENSIONS OFF)
240 function(glslang_set_link_args TARGET)
241 # For MinGW compiles, statically link against the GCC and C++ runtimes.
242 # This avoids the need to ship those runtimes as DLLs.
243 if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
244 set_target_properties(${TARGET} PROPERTIES
245 LINK_FLAGS "-static -static-libgcc -static-libstdc++")
247 endfunction(glslang_set_link_args)
249 if(NOT COMMAND find_host_package)
250 macro(find_host_package)
251 find_package(${ARGN})
255 # Root directory for build-time generated include files
256 set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
258 ################################################################################
259 # Build version information generation
260 ################################################################################
261 include(parse_version.cmake)
262 set(GLSLANG_CHANGES_FILE "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
263 set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
264 set(GLSLANG_BUILD_INFO_H "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
266 parse_version(${GLSLANG_CHANGES_FILE} GLSLANG)
268 function(configurate_version)
269 set(major ${GLSLANG_VERSION_MAJOR})
270 set(minor ${GLSLANG_VERSION_MINOR})
271 set(patch ${GLSLANG_VERSION_PATCH})
272 set(flavor ${GLSLANG_VERSION_FLAVOR})
273 configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY)
276 configurate_version()
278 # glslang_add_build_info_dependency() adds the glslang-build-info dependency and
279 # generated include directories to target.
280 function(glslang_add_build_info_dependency target)
281 target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
284 # glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
285 # default for <target> when building shared libraries, and sets the
286 # GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
288 function(glslang_only_export_explicit_symbols target)
289 if(BUILD_SHARED_LIBS)
290 target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
292 target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
294 target_compile_options(${target} PRIVATE "-fvisibility=hidden")
299 # glslang_pch() adds precompiled header rules to <target> for the pre-compiled
300 # header file <pch>. As target_precompile_headers() was added in CMake 3.16,
301 # this is a no-op if called on earlier versions of CMake.
302 if(NOT CMAKE_VERSION VERSION_LESS "3.16" AND ENABLE_PCH)
303 function(glslang_pch target pch)
304 target_precompile_headers(${target} PRIVATE ${pch})
307 function(glslang_pch target pch)
310 message("Your CMake version is ${CMAKE_VERSION}. Update to at least 3.16 to enable precompiled headers to speed up incremental builds")
314 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
315 find_host_package(PythonInterp 3 REQUIRED)
317 # We depend on these for later projects, so they should come first.
318 add_subdirectory(External)
321 if(NOT TARGET SPIRV-Tools-opt)
326 message(STATUS "optimizer enabled")
327 add_definitions(-DENABLE_OPT=1)
330 message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
332 add_definitions(-DENABLE_OPT=0)
335 add_subdirectory(glslang)
336 add_subdirectory(OGLCompilersDLL)
337 if(ENABLE_GLSLANG_BINARIES)
338 add_subdirectory(StandAlone)
340 add_subdirectory(SPIRV)
342 add_subdirectory(hlsl)
345 add_subdirectory(gtests)
348 if(ENABLE_CTEST AND BUILD_TESTING)
349 # glslang-testsuite runs a bash script on Windows.
350 # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
351 set(IGNORE_CR_FLAG "")
353 set(IGNORE_CR_FLAG -o igncr)
356 if (CMAKE_CONFIGURATION_TYPES)
357 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
358 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
359 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
361 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
362 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
363 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
366 add_test(NAME glslang-testsuite
367 COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
368 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)