e679228175cab2d486b9fe422219742465e0867a
[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_JS
36     "If using Emscripten, build glslang.js. Otherwise, builds a sample executable for binary-size testing." OFF)
37 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN
38     "Reduces glslang to minimum needed for web use"
39     OFF "ENABLE_GLSLANG_JS"
40     OFF)
41 CMAKE_DEPENDENT_OPTION(ENABLE_GLSLANG_WEBMIN_DEVEL
42     "For ENABLE_GLSLANG_WEBMIN builds, enables compilation error messages"
43     OFF "ENABLE_GLSLANG_WEBMIN"
44     OFF)
45 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_SINGLE_FILE
46     "If using Emscripten, enables SINGLE_FILE build"
47     OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
48     OFF)
49 CMAKE_DEPENDENT_OPTION(ENABLE_EMSCRIPTEN_ENVIRONMENT_NODE
50     "If using Emscripten, builds to run on Node instead of Web"
51     OFF "ENABLE_GLSLANG_JS AND EMSCRIPTEN"
52     OFF)
53
54 CMAKE_DEPENDENT_OPTION(ENABLE_HLSL
55     "Enables HLSL input support"
56     ON "NOT ENABLE_GLSLANG_WEBMIN"
57     OFF)
58
59 option(ENABLE_RTTI "Enables RTTI" OFF)
60 option(ENABLE_EXCEPTIONS "Enables Exceptions" OFF)
61 option(ENABLE_OPT "Enables spirv-opt capability if present" ON)
62 option(ENABLE_PCH "Enables Precompiled header" ON)
63 option(ENABLE_CTEST "Enables testing" ON)
64
65 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
66     set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
67 endif()
68
69 option(USE_CCACHE "Use ccache" OFF)
70 if(USE_CCACHE)
71     find_program(CCACHE_FOUND ccache)
72     if(CCACHE_FOUND)
73         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
74     endif(CCACHE_FOUND)
75 endif()
76
77 # Precompiled header macro. Parameters are source file list and filename for pch cpp file.
78 macro(glslang_pch SRCS PCHCPP)
79   if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND NOT ${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND ENABLE_PCH)
80     set(PCH_NAME "$(IntDir)\\pch.pch")
81     # make source files use/depend on PCH_NAME
82     set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yupch.h /FIpch.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
83     # make PCHCPP file compile and generate PCH_NAME
84     set_source_files_properties(${PCHCPP} PROPERTIES COMPILE_FLAGS "/Ycpch.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
85     list(APPEND ${SRCS} "${PCHCPP}")
86   endif()
87 endmacro(glslang_pch)
88
89 project(glslang)
90
91 if(ENABLE_CTEST)
92     include(CTest)
93 endif()
94
95 if(ENABLE_HLSL)
96     add_definitions(-DENABLE_HLSL)
97 endif(ENABLE_HLSL)
98
99 if(ENABLE_GLSLANG_WEBMIN)
100     add_definitions(-DGLSLANG_WEB)
101     if(ENABLE_GLSLANG_WEBMIN_DEVEL)
102         add_definitions(-DGLSLANG_WEB_DEVEL)
103     endif(ENABLE_GLSLANG_WEBMIN_DEVEL)
104 endif(ENABLE_GLSLANG_WEBMIN)
105
106 if(WIN32)
107     set(CMAKE_DEBUG_POSTFIX "d")
108     if(MSVC)
109         include(ChooseMSVCCRT.cmake)
110     endif(MSVC)
111     add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
112 elseif(UNIX)
113     add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
114 else(WIN32)
115     message("unknown platform")
116 endif(WIN32)
117
118 if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
119     add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
120                         -Wunused-parameter -Wunused-value  -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
121     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
122     if(BUILD_SHARED_LIBS)
123         add_compile_options(-fPIC)
124     endif()
125     if(NOT ENABLE_RTTI)
126         add_compile_options(-fno-rtti)
127     endif()
128     if(NOT ENABLE_EXCEPTIONS)
129         add_compile_options(-fno-exceptions)
130     endif()
131     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
132         add_compile_options(-Werror=deprecated-copy)
133     endif()
134
135     # Error if there's symbols that are not found at link time.
136     add_link_options("-Wl,--no-undefined")
137 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
138     add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
139                         -Wunused-parameter -Wunused-value  -Wunused-variable)
140     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
141     if(BUILD_SHARED_LIBS)
142         add_compile_options(-fPIC)
143     endif()
144     if(NOT ENABLE_RTTI)
145         add_compile_options(-fno-rtti)
146     endif()
147     if(NOT ENABLE_EXCEPTIONS)
148         add_compile_options(-fno-exceptions)
149     endif()
150
151     # Error if there's symbols that are not found at link time.
152     add_link_options("-Wl,-undefined,error")
153 elseif(MSVC)
154     if(NOT ENABLE_RTTI)
155         string(FIND "${CMAKE_CXX_FLAGS}" "/GR" MSVC_HAS_GR)
156         if(MSVC_HAS_GR)
157             string(REGEX REPLACE /GR /GR- CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
158         else()
159             add_compile_options(/GR-) # Disable RTTI
160         endif()
161     endif()
162     if(ENABLE_EXCEPTIONS)
163         add_compile_options(/EHsc) # Enable Exceptions
164     endif()
165 endif()
166
167 if(ENABLE_GLSLANG_JS)
168     if(MSVC)
169         add_compile_options(/Os /GR-)
170     else()
171         add_compile_options(-Os -fno-rtti -fno-exceptions)
172         if(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang" AND NOT MSVC)
173             add_compile_options(-Wno-unused-parameter)
174             add_compile_options(-Wno-unused-variable -Wno-unused-const-variable)
175         endif()
176     endif()
177 endif(ENABLE_GLSLANG_JS)
178
179 # Request C++11
180 if(${CMAKE_VERSION} VERSION_LESS 3.1)
181     # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
182     # remove this block once CMake >=3.1 has fixated in the ecosystem
183     set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
184 else()
185     set(CMAKE_CXX_STANDARD 11)
186     set(CMAKE_CXX_STANDARD_REQUIRED ON)
187     set(CMAKE_CXX_EXTENSIONS OFF)
188 endif()
189
190 function(glslang_set_link_args TARGET)
191     # For MinGW compiles, statically link against the GCC and C++ runtimes.
192     # This avoids the need to ship those runtimes as DLLs.
193     if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
194         set_target_properties(${TARGET} PROPERTIES
195                               LINK_FLAGS "-static -static-libgcc -static-libstdc++")
196     endif()
197 endfunction(glslang_set_link_args)
198
199 # CMake needs to find the right version of python, right from the beginning,
200 # otherwise, it will find the wrong version and fail later
201 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
202     find_package(PythonInterp 3 REQUIRED)
203
204         # We depend on these for later projects, so they should come first.
205         add_subdirectory(External)
206 endif()
207
208 if(NOT TARGET SPIRV-Tools-opt)
209     set(ENABLE_OPT OFF)
210 endif()
211
212 if(ENABLE_OPT)
213     message(STATUS "optimizer enabled")
214     add_definitions(-DENABLE_OPT=1)
215 else()
216     if(ENABLE_HLSL)
217         message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
218     endif()
219     add_definitions(-DENABLE_OPT=0)
220 endif()
221
222 add_subdirectory(glslang)
223 add_subdirectory(OGLCompilersDLL)
224 if(ENABLE_GLSLANG_BINARIES)
225     add_subdirectory(StandAlone)
226 endif()
227 add_subdirectory(SPIRV)
228 if(ENABLE_HLSL)
229     add_subdirectory(hlsl)
230 endif(ENABLE_HLSL)
231 if(ENABLE_CTEST)
232     add_subdirectory(gtests)
233 endif()
234
235 if(ENABLE_CTEST AND BUILD_TESTING)
236     # glslang-testsuite runs a bash script on Windows.
237     # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
238     set(IGNORE_CR_FLAG "")
239     if(WIN32)
240         set(IGNORE_CR_FLAG -o igncr)
241     endif()
242
243     if (CMAKE_CONFIGURATION_TYPES)
244         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
245         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
246         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
247     else(CMAKE_CONFIGURATION_TYPES)
248         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
249         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
250         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
251     endif(CMAKE_CONFIGURATION_TYPES)
252
253     add_test(NAME glslang-testsuite
254         COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
255         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
256 endif()