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)
40 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
42 # Enable compile commands database
43 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
45 # Adhere to GNU filesystem layout conventions
46 include(GNUInstallDirs)
48 # Needed for CMAKE_DEPENDENT_OPTION macro
49 include(CMakeDependentOption)
51 option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
52 option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
60 option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
61 if(NOT ${SKIP_GLSLANG_INSTALL})
62 set(ENABLE_GLSLANG_INSTALL ON)
64 option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
66 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
68 option(ENABLE_GLSLANG_JS
69 "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing." OFF)
70 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN
71 "Reduces glslang to minimum needed for web use"
72 OFF "ENABLE_GLSLANG_JS"
74 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
75 "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
76 OFF "ENABLE_GLSLANG_WEBMIN"
78 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
79 "If using Emscripten, enables SINGLE_FILE build"
80 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
82 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
83 "If using Emscripten, builds to run on Node instead of Web"
84 OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
87 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
88 "Enables HLSL input support"
89 ON "NOT ENABLE_GLSLANG_WEBMIN"
92 option(ENABLE_RTTI "Enables RTTI" OFF)
93 option(ENABLE_EXCEPTIONS "Enables Exceptions" OFF)
94 option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
95 option(ENABLE_PCH "Enables Precompiled header" ON)
96 option(ENABLE_CTEST "Enables testing" ON)
98 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
99 set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
102 option(USE_CCACHE "Use ccache" OFF)
104 find_program(CCACHE_FOUND ccache)
106 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
110 # If projects are statically importing glslang targets into a shared library
111 # then they'll likely need to build with -fPIC. This can be enabled by setting
112 # GLSLANG_BUILD_PIC to 1 before calling add_subdirectory() to import glslang.
113 # Note: -fPIC is automatically used when BUILD_SHARED_LIBS is enabled.
114 if(NOT DEFINED GLSLANG_BUILD_PIC)
115 option(GLSLANG_BUILD_PIC "Compile glslang with -fPIC" OFF)
118 # Precompiled header macro. Parameters are source file list and filename for pch cpp file.
119 macro(glslang_pch SRCS PCHCPP)
120 if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND ENABLE_PCH)
121 set(PCH_NAME "$(IntDir)\\pch.pch")
122 # make source files use/depend on PCH_NAME
123 set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yupch.h /FIpch.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
124 # make PCHCPP file compile and generate PCH_NAME
125 set_source_files_properties(${PCHCPP} PROPERTIES COMPILE_FLAGS "/Ycpch.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
126 list(APPEND ${SRCS} "${PCHCPP}")
128 endmacro(glslang_pch)
137 add_definitions(-DENABLE_HLSL)
140 if(ENABLE_GLSLANG_WEBMIN)
141 add_definitions(-DGLSLANG_WEB)
142 if(ENABLE_GLSLANG_WEBMIN_DEVEL)
143 add_definitions(-DGLSLANG_WEB_DEVEL)
144 endif(ENABLE_GLSLANG_WEBMIN_DEVEL)
145 endif(ENABLE_GLSLANG_WEBMIN)
148 set(CMAKE_DEBUG_POSTFIX "d")
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.
163 if(BUILD_SHARED_LIBS OR GLSLANG_BUILD_PIC)
164 add_compile_options(-fPIC)
167 add_compile_options(-fno-rtti)
169 if(NOT ENABLE_EXCEPTIONS)
170 add_compile_options(-fno-exceptions)
172 if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
173 add_compile_options(-Werror=deprecated-copy)
176 if(NOT CMAKE_VERSION VERSION_LESS "3.13")
177 # Error if there's symbols that are not found at link time.
178 # add_link_options() was added in CMake 3.13 - if using an earlier
179 # version don't set this - it should be caught by presubmits anyway.
180 add_link_options("-Wl,--no-undefined")
182 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
183 add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
184 -Wunused-parameter -Wunused-value -Wunused-variable)
185 add_compile_options(-Wno-reorder) # disable this from -Wall, since it happens all over.
186 if(BUILD_SHARED_LIBS OR GLSLANG_BUILD_PIC)
187 add_compile_options(-fPIC)
190 add_compile_options(-fno-rtti)
192 if(NOT ENABLE_EXCEPTIONS)
193 add_compile_options(-fno-exceptions)
196 if(NOT CMAKE_VERSION VERSION_LESS "3.13")
197 # Error if there's symbols that are not found at link time.
198 # add_link_options() was added in CMake 3.13 - if using an earlier
199 # version don't set this - it should be caught by presubmits anyway.
200 add_link_options("-Wl,-undefined,error")
204 string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
206 string(REGEX REPLACE /GR /GR- CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
208 add_compile_options(/GR-) # Disable RTTI
211 if(ENABLE_EXCEPTIONS)
212 add_compile_options(/EHsc) # Enable Exceptions
216 if(ENABLE_GLSLANG_JS)
218 add_compile_options(/Os /GR-)
220 add_compile_options(-Os -fno-rtti -fno-exceptions)
221 if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
222 add_compile_options(-Wno-unused-parameter)
223 add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
226 endif(ENABLE_GLSLANG_JS)
229 if(${CMAKE_VERSION} VERSION_LESS 3.1)
230 # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
231 # remove this block once CMake >=3.1 has fixated in the ecosystem
232 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
234 set(CMAKE_CXX_STANDARD 11)
235 set(CMAKE_CXX_STANDARD_REQUIRED ON)
236 set(CMAKE_CXX_EXTENSIONS OFF)
239 function(glslang_set_link_args TARGET)
240 # For MinGW compiles, statically link against the GCC and C++ runtimes.
241 # This avoids the need to ship those runtimes as DLLs.
242 if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
243 set_target_properties(${TARGET} PROPERTIES
244 LINK_FLAGS "-static -static-libgcc -static-libstdc++")
246 endfunction(glslang_set_link_args)
248 # CMake needs to find the right version of python, right from the beginning,
249 # otherwise, it will find the wrong version and fail later
250 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
251 find_package(PythonInterp 3 REQUIRED)
253 # We depend on these for later projects, so they should come first.
254 add_subdirectory(External)
257 # glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
258 # default for <target> when building shared libraries, and sets the
259 # GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
261 function(glslang_only_export_explicit_symbols target)
262 if(BUILD_SHARED_LIBS)
263 target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
265 target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
267 target_compile_options(${target} PRIVATE "-fvisibility=hidden")
272 if(NOT TARGET SPIRV-Tools-opt)
277 message(STATUS "optimizer enabled")
278 add_definitions(-DENABLE_OPT=1)
281 message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
283 add_definitions(-DENABLE_OPT=0)
286 add_subdirectory(glslang)
287 add_subdirectory(OGLCompilersDLL)
288 if(ENABLE_GLSLANG_BINARIES)
289 add_subdirectory(StandAlone)
291 add_subdirectory(SPIRV)
293 add_subdirectory(hlsl)
296 add_subdirectory(gtests)
299 if(ENABLE_CTEST AND BUILD_TESTING)
300 # glslang-testsuite runs a bash script on Windows.
301 # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
302 set(IGNORE_CR_FLAG "")
304 set(IGNORE_CR_FLAG -o igncr)
307 if (CMAKE_CONFIGURATION_TYPES)
308 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
309 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
310 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
311 else(CMAKE_CONFIGURATION_TYPES)
312 set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
313 set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
314 set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
315 endif(CMAKE_CONFIGURATION_TYPES)
317 add_test(NAME glslang-testsuite
318 COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
319 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)