Merge pull request #2319 from ben-clayton/build-pic
[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 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
41
42 # Enable compile commands database
43 set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
44
45 # Adhere to GNU filesystem layout conventions
46 include(GNUInstallDirs)
47
48 # Needed for CMAKE_DEPENDENT_OPTION macro
49 include(CMakeDependentOption)
50
51 option(BUILD_SHARED_LIBS "Build Shared Libraries" OFF)
52 option(BUILD_EXTERNAL "Build external dependencies in /External" ON)
53
54 set(LIB_TYPE STATIC)
55
56 if(BUILD_SHARED_LIBS)
57     set(LIB_TYPE SHARED)
58 endif()
59
60 option(SKIP_GLSLANG_INSTALL "Skip installation" ${SKIP_GLSLANG_INSTALL})
61 if(NOT ${SKIP_GLSLANG_INSTALL})
62   set(ENABLE_GLSLANG_INSTALL ON)
63 endif()
64 option(ENABLE_SPVREMAPPER "Enables building of SPVRemapper" ON)
65
66 option(ENABLE_GLSLANG_BINARIES "Builds glslangValidator and spirv-remap" ON)
67
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"
73     OFF)
74 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
75     "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
76     OFF "ENABLE_GLSLANG_WEBMIN"
77     OFF)
78 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
79     "If using Emscripten, enables SINGLE_FILE build"
80     OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
81     OFF)
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"
85     OFF)
86
87 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
88     "Enables HLSL input support"
89     ON "NOT ENABLE_GLSLANG_WEBMIN"
90     OFF)
91
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)
97
98 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
99     set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
100 endif()
101
102 option(USE_CCACHE "Use ccache" OFF)
103 if(USE_CCACHE)
104     find_program(CCACHE_FOUND ccache)
105     if(CCACHE_FOUND)
106         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
107     endif(CCACHE_FOUND)
108 endif()
109
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)
116 endif()
117
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}")
127   endif()
128 endmacro(glslang_pch)
129
130 project(glslang)
131
132 if(ENABLE_CTEST)
133     include(CTest)
134 endif()
135
136 if(ENABLE_HLSL)
137     add_definitions(-DENABLE_HLSL)
138 endif(ENABLE_HLSL)
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(ENABLE_GLSLANG_WEBMIN_DEVEL)
145 endif(ENABLE_GLSLANG_WEBMIN)
146
147 if(WIN32)
148     set(CMAKE_DEBUG_POSTFIX "d")
149     if(MSVC)
150         include(ChooseMSVCCRT.cmake)
151     endif(MSVC)
152     add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
153 elseif(UNIX)
154     add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
155 else(WIN32)
156     message("unknown platform")
157 endif(WIN32)
158
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)
165     endif()
166     if(NOT ENABLE_RTTI)
167         add_compile_options(-fno-rtti)
168     endif()
169     if(NOT ENABLE_EXCEPTIONS)
170         add_compile_options(-fno-exceptions)
171     endif()
172     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
173         add_compile_options(-Werror=deprecated-copy)
174     endif()
175
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")
181     endif()
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)
188     endif()
189     if(NOT ENABLE_RTTI)
190         add_compile_options(-fno-rtti)
191     endif()
192     if(NOT ENABLE_EXCEPTIONS)
193         add_compile_options(-fno-exceptions)
194     endif()
195
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")
201     endif()
202 elseif(MSVC)
203     if(NOT ENABLE_RTTI)
204         string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
205         if(MSVC_HAS_GR)
206             string(REGEX REPLACE /GR /GR- CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
207         else()
208             add_compile_options(/GR-) # Disable RTTI
209         endif()
210     endif()
211     if(ENABLE_EXCEPTIONS)
212         add_compile_options(/EHsc) # Enable Exceptions
213     endif()
214 endif()
215
216 if(ENABLE_GLSLANG_JS)
217     if(MSVC)
218         add_compile_options(/Os /GR-)
219     else()
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)
224         endif()
225     endif()
226 endif(ENABLE_GLSLANG_JS)
227
228 # Request C++11
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")
233 else()
234     set(CMAKE_CXX_STANDARD 11)
235     set(CMAKE_CXX_STANDARD_REQUIRED ON)
236     set(CMAKE_CXX_EXTENSIONS OFF)
237 endif()
238
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++")
245     endif()
246 endfunction(glslang_set_link_args)
247
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)
252
253         # We depend on these for later projects, so they should come first.
254         add_subdirectory(External)
255 endif()
256
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
260 # building <target>.
261 function(glslang_only_export_explicit_symbols target)
262     if(BUILD_SHARED_LIBS)
263         target_compile_definitions(${target} PUBLIC "GLSLANG_IS_SHARED_LIBRARY=1")
264         if(WIN32)
265             target_compile_definitions(${target} PRIVATE "GLSLANG_EXPORTING=1")
266         else()
267             target_compile_options(${target} PRIVATE "-fvisibility=hidden")
268         endif()
269     endif()
270 endfunction()
271
272 if(NOT TARGET SPIRV-Tools-opt)
273     set(ENABLE_OPT OFF)
274 endif()
275
276 if(ENABLE_OPT)
277     message(STATUS "optimizer enabled")
278     add_definitions(-DENABLE_OPT=1)
279 else()
280     if(ENABLE_HLSL)
281         message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
282     endif()
283     add_definitions(-DENABLE_OPT=0)
284 endif()
285
286 add_subdirectory(glslang)
287 add_subdirectory(OGLCompilersDLL)
288 if(ENABLE_GLSLANG_BINARIES)
289     add_subdirectory(StandAlone)
290 endif()
291 add_subdirectory(SPIRV)
292 if(ENABLE_HLSL)
293     add_subdirectory(hlsl)
294 endif(ENABLE_HLSL)
295 if(ENABLE_CTEST)
296     add_subdirectory(gtests)
297 endif()
298
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 "")
303     if(WIN32)
304         set(IGNORE_CR_FLAG -o igncr)
305     endif()
306
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)
316
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/)
320 endif()