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