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