Split "is emscripten" config from "enable glslang.js"
[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_OPT "Enables spirv-opt capability if present" ON)
60 option(ENABLE_PCH "Enables Precompiled header" ON)
61 option(ENABLE_CTEST "Enables testing" ON)
62
63 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT AND WIN32)
64     set(CMAKE_INSTALL_PREFIX "install" CACHE STRING "..." FORCE)
65 endif()
66
67 option(USE_CCACHE "Use ccache" OFF)
68 if(USE_CCACHE)
69     find_program(CCACHE_FOUND ccache)
70     if(CCACHE_FOUND)
71         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
72     endif(CCACHE_FOUND)
73 endif()
74
75 # Precompiled header macro. Parameters are source file list and filename for pch cpp file.
76 macro(glslang_pch SRCS PCHCPP)
77   if(MSVC AND CMAKE_GENERATOR MATCHES "^Visual Studio" AND ENABLE_PCH)
78     set(PCH_NAME "$(IntDir)\\pch.pch")
79     # make source files use/depend on PCH_NAME
80     set_source_files_properties(${${SRCS}} PROPERTIES COMPILE_FLAGS "/Yupch.h /FIpch.h /Fp${PCH_NAME} /Zm300" OBJECT_DEPENDS "${PCH_NAME}")
81     # make PCHCPP file compile and generate PCH_NAME
82     set_source_files_properties(${PCHCPP} PROPERTIES COMPILE_FLAGS "/Ycpch.h /Fp${PCH_NAME} /Zm300" OBJECT_OUTPUTS "${PCH_NAME}")
83     list(APPEND ${SRCS} "${PCHCPP}")
84   endif()
85 endmacro(glslang_pch)
86
87 project(glslang)
88
89 if(ENABLE_CTEST)
90     include(CTest)
91 endif()
92
93 if(ENABLE_HLSL)
94     add_definitions(-DENABLE_HLSL)
95 endif(ENABLE_HLSL)
96
97 if(ENABLE_GLSLANG_WEBMIN)
98     add_definitions(-DGLSLANG_WEB)
99     if(ENABLE_GLSLANG_WEBMIN_DEVEL)
100         add_definitions(-DGLSLANG_WEB_DEVEL)
101     endif(ENABLE_GLSLANG_WEBMIN_DEVEL)
102 endif(ENABLE_GLSLANG_WEBMIN)
103
104 if(WIN32)
105     set(CMAKE_DEBUG_POSTFIX "d")
106     if(MSVC)
107         include(ChooseMSVCCRT.cmake)
108     endif(MSVC)
109     add_definitions(-DGLSLANG_OSINCLUDE_WIN32)
110 elseif(UNIX)
111     add_definitions(-DGLSLANG_OSINCLUDE_UNIX)
112 else(WIN32)
113     message("unknown platform")
114 endif(WIN32)
115
116 if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
117     add_compile_options(-Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs
118                         -Wunused-parameter -Wunused-value  -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions)
119     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
120     add_compile_options(-fno-rtti)
121     if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "9.0.0")
122         add_compile_options(-Werror=deprecated-copy)
123     endif()
124 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
125     add_compile_options(-Wall -Wuninitialized -Wunused -Wunused-local-typedefs
126                         -Wunused-parameter -Wunused-value  -Wunused-variable)
127     add_compile_options(-Wno-reorder)  # disable this from -Wall, since it happens all over.
128     add_compile_options(-fno-rtti)
129 elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "MSVC")
130     add_compile_options(/GR-) # Disable RTTI
131 endif()
132
133 if(ENABLE_GLSLANG_JS)
134     if(MSVC)
135         add_compile_options(/Os /GR-)
136     else()
137         add_compile_options(-Os -fno-exceptions)
138         add_link_options(-Os)
139     endif()
140 endif(ENABLE_GLSLANG_JS)
141
142 # Request C++11
143 if(${CMAKE_VERSION} VERSION_LESS 3.1)
144     # CMake versions before 3.1 do not understand CMAKE_CXX_STANDARD
145     # remove this block once CMake >=3.1 has fixated in the ecosystem
146     add_compile_options(-std=c++11)
147 else()
148     set(CMAKE_CXX_STANDARD 11)
149     set(CMAKE_CXX_STANDARD_REQUIRED ON)
150     set(CMAKE_CXX_EXTENSIONS OFF)
151 endif()
152
153 function(glslang_set_link_args TARGET)
154     # For MinGW compiles, statically link against the GCC and C++ runtimes.
155     # This avoids the need to ship those runtimes as DLLs.
156     if(WIN32 AND ${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
157         set_target_properties(${TARGET} PROPERTIES
158                               LINK_FLAGS "-static -static-libgcc -static-libstdc++")
159     endif()
160 endfunction(glslang_set_link_args)
161
162 # CMake needs to find the right version of python, right from the beginning,
163 # otherwise, it will find the wrong version and fail later
164 if(BUILD_EXTERNAL AND IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/External)
165     find_package(PythonInterp 3 REQUIRED)
166
167         # We depend on these for later projects, so they should come first.
168         add_subdirectory(External)
169 endif()
170
171 if(NOT TARGET SPIRV-Tools-opt)
172     set(ENABLE_OPT OFF)
173 endif()
174
175 if(ENABLE_OPT)
176     message(STATUS "optimizer enabled")
177     add_definitions(-DENABLE_OPT=1)
178 else()
179     if(ENABLE_HLSL)
180         message(STATUS "spirv-tools not linked - illegal SPIRV may be generated for HLSL")
181     endif()
182     add_definitions(-DENABLE_OPT=0)
183 endif()
184
185 add_subdirectory(glslang)
186 add_subdirectory(OGLCompilersDLL)
187 if(ENABLE_GLSLANG_BINARIES)
188     add_subdirectory(StandAlone)
189 endif()
190 add_subdirectory(SPIRV)
191 if(ENABLE_HLSL)
192     add_subdirectory(hlsl)
193 endif(ENABLE_HLSL)
194 if(ENABLE_CTEST)
195     add_subdirectory(gtests)
196 endif()
197
198 if(BUILD_TESTING)
199     # glslang-testsuite runs a bash script on Windows.
200     # Make sure to use '-o igncr' flag to ignore carriage returns (\r).
201     set(IGNORE_CR_FLAG "")
202     if(WIN32)
203         set(IGNORE_CR_FLAG -o igncr)
204     endif()
205
206     if (CMAKE_CONFIGURATION_TYPES)
207         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/$<CONFIGURATION>/localResults)
208         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/glslangValidator)
209         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/$<CONFIGURATION>/spirv-remap)
210     else(CMAKE_CONFIGURATION_TYPES)
211         set(RESULTS_PATH ${CMAKE_CURRENT_BINARY_DIR}/localResults)
212         set(VALIDATOR_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/glslangValidator)
213         set(REMAP_PATH ${CMAKE_CURRENT_BINARY_DIR}/StandAlone/spirv-remap)
214     endif(CMAKE_CONFIGURATION_TYPES)
215
216     add_test(NAME glslang-testsuite
217         COMMAND bash ${IGNORE_CR_FLAG} runtests ${RESULTS_PATH} ${VALIDATOR_PATH} ${REMAP_PATH}
218         WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/Test/)
219 endif(BUILD_TESTING)