Merge pull request #3046 from amdrexu/bugfix
[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 3.14.0)
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 include(CMakePackageConfigHelpers)
54
55 # Needed for CMAKE_DEPENDENT_OPTION macro
56 include(CMakeDependentOption)
57
58 option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
59 option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
60
61 set(LIB_TYPE STATIC)
62
63 if(BUILD_SHARED_LIBS)
64     set(LIB_TYPE SHARED)
65 endif()
66
67 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
68     # This logic inside SPIRV-Tools, which can upset build target dependencies
69     # if changed after targets are already defined. To prevent these issues,
70     # ensure CMAKE_BUILD_TYPE is assigned early and at the glslang root scope.
71     message(STATUS "No build type selected, default to Debug")
72     set(CMAKE_BUILD_TYPE "Debug")
73 endif()
74
75 option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
76 if(NOT ${SKIP_GLSLANG_INSTALL})
77   set(ENABLE_GLSLANG_INSTALL ON)
78 endif()
79 option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
80
81 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
82
83 option(ENABLE_GLSLANG_JS
84     "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing." OFF)
85 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN
86     "Reduces glslang to minimum needed for web use"
87     OFF "ENABLE_GLSLANG_JS"
88     OFF)
89 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
90     "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
91     OFF "ENABLE_GLSLANG_WEBMIN"
92     OFF)
93 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
94     "If using Emscripten, enables SINGLE_FILE build"
95     OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
96     OFF)
97 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
98     "If using Emscripten, builds to run on Node instead of Web"
99     OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
100     OFF)
101
102 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
103     "Enables HLSL input support"
104     ON "NOT ENABLE_GLSLANG_WEBMIN"
105     OFF)
106
107 option(ENABLE_RTTI "Enables RTTI" OFF)
108 option(ENABLE_EXCEPTIONS "Enables Exceptions" OFF)
109 option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
110
111 if(MINGW OR (CMAKE_SYSTEM_NAME STREQUAL "Darwin" AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU"))
112     # Workaround for CMake behavior on Mac OS with gcc, cmake generates -Xarch_* arguments
113     # which gcc rejects
114     option(ENABLE_PCH "Enables Precompiled header" OFF)
115 else()
116     option(ENABLE_PCH "Enables Precompiled header" ON)
117 endif()
118 option(ENABLE_CTEST "Enables testing" ON)
119
120 if(ENABLE_GLSLANG_INSTALL AND CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
121     set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
122 endif()
123
124 option(USE_CCACHE "Use ccache" OFF)
125 if(USE_CCACHE)
126     find_program(CCACHE_FOUND ccache)
127     if(CCACHE_FOUND)
128         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
129     endif()
130 endif()
131
132 if(ENABLE_CTEST)
133     include(CTest)
134 endif()
135
136 if(ENABLE_HLSL)
137     add_definitions(-DENABLE_HLSL)
138 endif()
139
140 if(ENABLE_GLSLANG_WEBMIN)
141     add_definitions(-DGLSLANG_WEB)
142     if(ENABLE_GLSLANG_WEBMIN_DEVEL)
143         add_definitions(-DGLSLANG_WEB_DEVEL)
144     endif()
145 endif()
146
147 if(WIN32)
148     set(CMAKE_DEBUG_POSTFIX "d")
149     option(OVERRIDE_MSVCCRT "Overrides runtime of MSVC " ON)
150     if(MSVC AND OVERRIDE_MSVCCRT)
151         include(ChooseMSVCCRT.cmake)
152     endif()
153     add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
154 elseif(UNIX)
155     add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
156 else()
157     message("unknown platform")
158 endif()
159
160 if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
161     add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
162                         -Wunused-parameter -Wunused-value  -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
163     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
164     if(NOT ENABLE_RTTI)
165         add_compile_options(-fno-rtti)
166     endif()
167     if(NOT ENABLE_EXCEPTIONS)
168         add_compile_options(-fno-exceptions)
169     endif()
170     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
171         add_compile_options(-Werror=deprecated-copy)
172     endif()
173
174     if(NOT CMAKE_VERSION VERSION_LESS "3.13" AND NOT CMAKE_SYSTEM_NAME STREQUAL "Darwin")
175         # Error if there's symbols that are not found at link time.
176         # add_link_options() was added in CMake 3.13 - if using an earlier
177         # version don't set this - it should be caught by presubmits anyway.
178         add_link_options("-Wl,--no-undefined")
179     endif()
180 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
181     add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
182                         -Wunused-parameter -Wunused-value  -Wunused-variable)
183     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
184     if(NOT ENABLE_RTTI)
185         add_compile_options(-fno-rtti)
186     endif()
187     if(NOT ENABLE_EXCEPTIONS)
188         add_compile_options(-fno-exceptions)
189     endif()
190
191     if(NOT CMAKE_VERSION VERSION_LESS "3.13")
192         # Error if there's symbols that are not found at link time.
193         # add_link_options() was added in CMake 3.13 - if using an earlier
194         # version don't set this - it should be caught by presubmits anyway.
195         if (WIN32)
196             add_link_options("-Wl,--no-undefined")
197         else()
198             add_link_options("-Wl,-undefined,error")
199         endif()
200     endif()
201 elseif(MSVC)
202     if(NOT ENABLE_RTTI)
203         string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
204         if(MSVC_HAS_GR)
205             string(REGEX REPLACE "/GR" "/GR-" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
206         else()
207             add_compile_options(/GR-) # Disable RTTI
208         endif()
209     endif()
210     if(ENABLE_EXCEPTIONS)
211         add_compile_options(/EHsc) # Enable Exceptions
212         else()
213         string(REGEX REPLACE "/EHsc" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") # Try to remove default /EHsc cxx_flag
214         add_compile_options(/D_HAS_EXCEPTIONS=0)
215     endif()
216 endif()
217
218 if(ENABLE_GLSLANG_JS)
219     if(MSVC)
220         add_compile_options(/Os /GR-)
221     else()
222         add_compile_options(-Os -fno-rtti -fno-exceptions)
223         if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
224             add_compile_options(-Wno-unused-parameter)
225             add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
226         endif()
227     endif()
228 endif()
229
230 # Request C++11
231 if(${CMAKE_VERSION} VERSION_LESS 3.1)
232     # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
233     # remove this block once CMake >=3.1 has fixated in the ecosystem
234     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
235 else()
236     set(CMAKE_CXX_STANDARD 11)
237     set(CMAKE_CXX_STANDARD_REQUIRED ON)
238     set(CMAKE_CXX_EXTENSIONS OFF)
239 endif()
240
241 function(glslang_set_link_args TARGET)
242     # For MinGW compiles, statically link against the GCC and C++ runtimes.
243     # This avoids the need to ship those runtimes as DLLs.
244     if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
245         set_target_properties(${TARGET} PROPERTIES
246                               LINK_FLAGS "-static -static-libgcc -static-libstdc++")
247     endif()
248 endfunction(glslang_set_link_args)
249
250 if(NOT COMMAND find_host_package)
251     macro(find_host_package)
252         find_package(${ARGN})
253     endmacro()
254 endif()
255
256 # Root directory for build-time generated include files
257 set(GLSLANG_GENERATED_INCLUDEDIR "${CMAKE_BINARY_DIR}/include")
258
259 ################################################################################
260 # Build version information generation
261 ################################################################################
262 include(parse_version.cmake)
263 set(GLSLANG_CHANGES_FILE      "${CMAKE_CURRENT_SOURCE_DIR}/CHANGES.md")
264 set(GLSLANG_BUILD_INFO_H_TMPL "${CMAKE_CURRENT_SOURCE_DIR}/build_info.h.tmpl")
265 set(GLSLANG_BUILD_INFO_H      "${GLSLANG_GENERATED_INCLUDEDIR}/glslang/build_info.h")
266
267 parse_version(${GLSLANG_CHANGES_FILE} GLSLANG)
268
269 function(configurate_version)
270     set(major ${GLSLANG_VERSION_MAJOR})
271     set(minor ${GLSLANG_VERSION_MINOR})
272     set(patch ${GLSLANG_VERSION_PATCH})
273     set(flavor ${GLSLANG_VERSION_FLAVOR})
274     configure_file(${GLSLANG_BUILD_INFO_H_TMPL} ${GLSLANG_BUILD_INFO_H} @ONLY)
275 endfunction()
276
277 configurate_version()
278
279 # glslang_add_build_info_dependency() adds the glslang-build-info dependency and
280 # generated include directories to target.
281 function(glslang_add_build_info_dependency target)
282     target_include_directories(${target} PUBLIC $<BUILD_INTERFACE:${GLSLANG_GENERATED_INCLUDEDIR}>)
283 endfunction()
284
285 # glslang_only_export_explicit_symbols() makes the symbol visibility hidden by
286 # default for <target> when building shared libraries, and sets the
287 # GLSLANG_IS_SHARED_LIBRARY define, and GLSLANG_EXPORTING to 1 when specifically
288 # building <target>.
289 function(glslang_only_export_explicit_symbols target)
290     if(BUILD_SHARED_LIBS)
291         target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
292         if(WIN32)
293             target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
294         else()
295             target_compile_options(${target} PRIVATE "-fvisibility=hidden")
296         endif()
297     endif()
298 endfunction()
299
300 # glslang_pch() adds precompiled header rules to <target> for the pre-compiled
301 # header file <pch>. As target_precompile_headers() was added in CMake 3.16,
302 # this is a no-op if called on earlier versions of CMake.
303 if(NOT CMAKE_VERSION VERSION_LESS "3.16" AND ENABLE_PCH)
304     function(glslang_pch target pch)
305         target_precompile_headers(${target} PRIVATE ${pch})
306     endfunction()
307 else()
308     function(glslang_pch target pch)
309     endfunction()
310     if(ENABLE_PCH)
311         message("Your CMake version is ${CMAKE_VERSION}. Update to at least 3.16 to enable precompiled headers to speed up incremental builds")
312     endif()
313 endif()
314
315 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
316     find_host_package(PythonInterp 3 REQUIRED)
317
318     # We depend on these for later projects, so they should come first.
319     add_subdirectory(External)
320 endif()
321
322 if(NOT TARGET SPIRV-Tools-opt)
323     set(ENABLE_OPT OFF)
324 endif()
325
326 if(ENABLE_OPT)
327     message(STATUS "optimizer enabled")
328     add_definitions(-DENABLE_OPT=1)
329 else()
330     if(ENABLE_HLSL)
331         message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
332     endif()
333     add_definitions(-DENABLE_OPT=0)
334 endif()
335
336 add_subdirectory(glslang)
337 add_subdirectory(OGLCompilersDLL)
338 if(ENABLE_GLSLANG_BINARIES)
339     add_subdirectory(StandAlone)
340 endif()
341 add_subdirectory(SPIRV)
342 if(ENABLE_HLSL)
343     add_subdirectory(hlsl)
344 endif()
345 if(ENABLE_CTEST)
346     add_subdirectory(gtests)
347 endif()
348
349 if(ENABLE_CTEST AND BUILD_TESTING)
350     # glslang-testsuite runs a bash script on Windows.
351     # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
352     set(IGNORE_CR_FLAG "")
353     if(WIN32)
354         set(IGNORE_CR_FLAG -o igncr)
355     endif()
356
357     if (CMAKE_CONFIGURATION_TYPES)
358         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
359         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
360         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
361     else()
362         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
363         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
364         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
365     endif()
366
367     add_test(NAME glslang-testsuite
368         COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
369         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
370 endif()
371
372 if(ENABLE_GLSLANG_INSTALL)
373     file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in" [=[
374         @PACKAGE_INIT@
375         include("@PACKAGE_PATH_EXPORT_TARGETS@")
376     ]=])
377     
378     set(PATH_EXPORT_TARGETS "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/glslang-targets.cmake")
379     configure_package_config_file(
380         "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake.in"
381         "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
382         PATH_VARS
383             PATH_EXPORT_TARGETS
384         INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}
385     )
386     
387     write_basic_package_version_file("${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
388         VERSION ${GLSLANG_VERSION}
389         COMPATIBILITY SameMajorVersion
390     )
391     
392     install(
393         EXPORT      glslang-targets
394         NAMESPACE   "glslang::"
395         DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
396     )
397     
398     install(
399         FILES
400             "${CMAKE_CURRENT_BINARY_DIR}/glslang-config.cmake"
401             "${CMAKE_CURRENT_BINARY_DIR}/glslang-config-version.cmake"
402         DESTINATION
403             "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}"
404     )
405 endif()