Merge pull request #1940 from tsuoranta/fix-gcc9
[platform/upstream/glslang.git] / CMakeLists.txt
1 # increase to 3.1 once all major distributions
2 # include a version of CMake >= 3.1
3 cmake_minimum_required(VERSION 2.8.12)
4 if (POLICY CMP0048)
5   cmake_policy(SET CMP0048 NEW)
6 endif()
7 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
8
9 # Enable compile commands database
10 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
11
12 # Adhere to GNU filesystem layout conventions
13 include(GNUInstallDirs)
14
15 # Needed for CMAKE_DEPENDENT_OPTION macro
16 include(CMakeDependentOption)
17
18 option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
19 option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
20
21 set(LIB_TYPE STATIC)
22
23 if(BUILD_SHARED_LIBS)
24     set(LIB_TYPE SHARED)
25 endif()
26
27 option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
28 if(NOT ${SKIP_GLSLANG_INSTALL})
29   set(ENABLE_GLSLANG_INSTALL ON)
30 endif()
31 option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
32
33 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
34
35 option(ENABLE_GLSLANG_WEB "Reduces glslang to minimum needed for web use" OFF)
36 option(ENABLE_GLSLANG_WEB_DEVEL "For ENABLE_GLSLANG_WEB builds, enables compilation error messages" OFF)
37 option(ENABLE_EMSCRIPTEN_SINGLE_FILE "If using Emscripten, enables SINGLE_FILE build" OFF)
38 option(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE "If using Emscripten, builds to run on Node instead of Web" OFF)
39
40 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL "Enables HLSL input support" ON "NOT ENABLE_GLSLANG_WEB" OFF)
41
42 option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
43 option(ENABLE_PCH "Enables Precompiled header" ON)
44 option(ENABLE_CTEST "Enables testing" ON)
45
46 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
47     set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
48 endif()
49
50 option(USE_CCACHE "Use ccache" OFF)
51 if(USE_CCACHE)
52     find_program(CCACHE_FOUND ccache)
53     if(CCACHE_FOUND)
54         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
55     endif(CCACHE_FOUND)
56 endif()
57
58 # Precompiled header macro. Parameters are source file list and filename for pch cpp file.
59 macro(glslang_pch SRCS PCHCPP)
60   if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND ENABLE_PCH)
61     set(PCH_NAME "$(IntDir)\\pch.pch")
62     # make source files use/depend on PCH_NAME
63     set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yupch.h /FIpch.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
64     # make PCHCPP file compile and generate PCH_NAME
65     set_source_files_properties(${PCHCPP} PROPERTIES COMPILE_FLAGS "/Ycpch.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
66     list(APPEND ${SRCS} "${PCHCPP}")
67   endif()
68 endmacro(glslang_pch)
69
70 project(glslang)
71
72 if(ENABLE_CTEST)
73     include(CTest)
74 endif()
75
76 if(ENABLE_HLSL)
77     add_definitions(-DENABLE_HLSL)
78 endif(ENABLE_HLSL)
79
80 if(ENABLE_GLSLANG_WEB)
81     add_definitions(-DGLSLANG_WEB)
82     if(ENABLE_GLSLANG_WEB_DEVEL)
83         add_definitions(-DGLSLANG_WEB_DEVEL)
84     endif(ENABLE_GLSLANG_WEB_DEVEL)
85 endif(ENABLE_GLSLANG_WEB)
86
87 if(WIN32)
88     set(CMAKE_DEBUG_POSTFIX "d")
89     if(MSVC)
90         include(ChooseMSVCCRT.cmake)
91     endif(MSVC)
92     add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
93 elseif(UNIX)
94     add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
95 else(WIN32)
96     message("unknown platform")
97 endif(WIN32)
98
99 if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
100     add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
101                         -Wunused-parameter -Wunused-value  -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
102     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
103     add_compile_options(-fno-rtti)
104     if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL "9.0.0")
105         add_compile_options(-Werror=deprecated-copy)
106     endif()
107 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
108     add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
109                         -Wunused-parameter -Wunused-value  -Wunused-variable)
110     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
111     add_compile_options(-fno-rtti)
112 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
113     add_compile_options(/GR-) # Disable RTTI
114 endif()
115
116 if(EMSCRIPTEN)
117     add_compile_options(-Os -fno-exceptions)
118     add_compile_options("SHELL: -s WASM=1")
119     add_compile_options("SHELL: -s WASM_OBJECT_FILES=0")
120     add_link_options(-Os)
121     add_link_options("SHELL: -s FILESYSTEM=0")
122     add_link_options("SHELL: --llvm-lto 1")
123     add_link_options("SHELL: --closure 1")
124     add_link_options("SHELL: -s ALLOW_MEMORY_GROWTH=1")
125
126     if(ENABLE_EMSCRIPTEN_SINGLE_FILE)
127         add_link_options("SHELL: -s SINGLE_FILE=1")
128     endif(ENABLE_EMSCRIPTEN_SINGLE_FILE)
129 else()
130     if(ENABLE_GLSLANG_WEB)
131         if(MSVC)
132             add_compile_options(/Os /GR-)
133         else()
134             add_compile_options(-Os -fno-exceptions)
135             add_link_options(-Os)
136         endif()
137     endif(ENABLE_GLSLANG_WEB)
138 endif(EMSCRIPTEN)
139
140 # Request C++11
141 if(${CMAKE_VERSION} VERSION_LESS 3.1)
142     # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
143     # remove this block once CMake >=3.1 has fixated in the ecosystem
144     add_compile_options(-std=c++11)
145 else()
146     set(CMAKE_CXX_STANDARD 11)
147     set(CMAKE_CXX_STANDARD_REQUIRED ON)
148     set(CMAKE_CXX_EXTENSIONS OFF)
149 endif()
150
151 function(glslang_set_link_args TARGET)
152     # For MinGW compiles, statically link against the GCC and C++ runtimes.
153     # This avoids the need to ship those runtimes as DLLs.
154     if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
155         set_target_properties(${TARGET} PROPERTIES
156                               LINK_FLAGS "-static -static-libgcc -static-libstdc++")
157     endif()
158 endfunction(glslang_set_link_args)
159
160 # CMake needs to find the right version of python, right from the beginning,
161 # otherwise, it will find the wrong version and fail later
162 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
163     find_package(PythonInterp 3 REQUIRED)
164
165         # We depend on these for later projects, so they should come first.
166         add_subdirectory(External)
167 endif()
168
169 if(NOT TARGET SPIRV-Tools-opt)
170     set(ENABLE_OPT OFF)
171 endif()
172
173 if(ENABLE_OPT)
174     message(STATUS "optimizer enabled")
175     add_definitions(-DENABLE_OPT=1)
176 else()
177     if(ENABLE_HLSL)
178         message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
179     endif()
180     add_definitions(-DENABLE_OPT=0)
181 endif()
182
183 add_subdirectory(glslang)
184 add_subdirectory(OGLCompilersDLL)
185 if(ENABLE_GLSLANG_BINARIES)
186     add_subdirectory(StandAlone)
187 endif()
188 add_subdirectory(SPIRV)
189 if(ENABLE_HLSL)
190     add_subdirectory(hlsl)
191 endif(ENABLE_HLSL)
192 if(ENABLE_CTEST)
193     add_subdirectory(gtests)
194 endif()
195
196 if(BUILD_TESTING)
197     # glslang-testsuite runs a bash script on Windows.
198     # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
199     set(IGNORE_CR_FLAG "")
200     if(WIN32)
201         set(IGNORE_CR_FLAG -o igncr)
202     endif()
203
204     if (CMAKE_CONFIGURATION_TYPES)
205         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
206         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
207         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
208     else(CMAKE_CONFIGURATION_TYPES)
209         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
210         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
211         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
212     endif(CMAKE_CONFIGURATION_TYPES)
213
214     add_test(NAME glslang-testsuite
215         COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
216         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
217 endif(BUILD_TESTING)