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