1 # Copyright (c) 2015-2016 The Khronos Group Inc.
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and/or associated documentation files (the
5 # "Materials"), to deal in the Materials without restriction, including
6 # without limitation the rights to use, copy, modify, merge, publish,
7 # distribute, sublicense, and/or sell copies of the Materials, and to
8 # permit persons to whom the Materials are furnished to do so, subject to
9 # the following conditions:
11 # The above copyright notice and this permission notice shall be included
12 # in all copies or substantial portions of the Materials.
14 # MODIFICATIONS TO THIS FILE MAY MEAN IT NO LONGER ACCURATELY REFLECTS
15 # KHRONOS STANDARDS. THE UNMODIFIED, NORMATIVE VERSIONS OF KHRONOS
16 # SPECIFICATIONS AND HEADER INFORMATION ARE LOCATED AT
17 # https://www.khronos.org/registry/
19 # THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 # MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
27 cmake_minimum_required(VERSION 2.8)
30 set(SPIRV_TOOLS "SPIRV-Tools")
32 if("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
33 add_definitions(-DSPIRV_LINUX)
34 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
35 add_definitions(-DSPIRV_WINDOWS)
36 elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
37 add_definitions(-DSPIRV_MAC)
39 message(FATAL_ERROR "Your platform '${CMAKE_SYSTEM_NAME}' is not supported!")
42 if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
43 message(STATUS "No build type selected, default to Debug")
44 set(CMAKE_BUILD_TYPE "Debug")
47 option(SPIRV_WERROR "Enable error on warning" ON)
49 set(SPIRV_WARNINGS -Wall -Wextra -Wno-missing-field-initializers)
51 option(SPIRV_WARN_EVERYTHING "Enable -Weverything" ${SPIRV_WARN_EVERYTHING})
52 if(${SPIRV_WARN_EVERYTHING})
53 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
54 set(SPIRV_WARNINGS ${SPIRV_WARNINGS}
55 -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded)
56 elseif("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
57 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Wpedantic -pedantic-errors)
59 message(STATUS "Unknown compiler ${CMAKE_CXX_COMPILER_ID}, "
60 "so SPIRV_WARN_EVERYTHING has no effect")
65 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} -Werror)
68 set(SPIRV_WARNINGS -D_CRT_SECURE_NO_WARNINGS /wd4800)
71 set(SPIRV_WARNINGS ${SPIRV_WARNINGS} /WX)
75 option(SPIRV_COLOR_TERMINAL "Enable color terminal output" ON)
76 if(${SPIRV_COLOR_TERMINAL})
77 add_definitions(-DSPIRV_COLOR_TERMINAL)
80 function(spvtools_default_compile_options TARGET)
81 target_compile_options(${TARGET} PRIVATE ${SPIRV_WARNINGS})
83 target_compile_options(${TARGET} PRIVATE
84 -std=c++11 -fno-exceptions -fno-rtti)
85 target_compile_options(${TARGET} PRIVATE
86 -Wall -Wextra -Wno-long-long -Wshadow -Wundef -Wconversion
88 # For good call stacks in profiles, keep the frame pointers.
89 if(NOT "${SPIRV_PERF}" STREQUAL "")
90 target_compile_options(${TARGET} PRIVATE -fno-omit-frame-pointer)
92 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
93 set(SPIRV_USE_SANITIZER "" CACHE STRING
94 "Use the clang sanitizer [address|memory|thread|...]")
95 if(NOT "${SPIRV_USE_SANITIZER}" STREQUAL "")
96 target_compile_options(${TARGET} PRIVATE
97 -fsanitize=${SPIRV_USE_SANITIZER})
100 target_compile_options(${TARGET} PRIVATE
101 -Wno-missing-field-initializers)
107 ${CMAKE_CURRENT_SOURCE_DIR}/include/libspirv/libspirv.h
108 ${CMAKE_CURRENT_SOURCE_DIR}/include/util/bitutils.h
109 ${CMAKE_CURRENT_SOURCE_DIR}/include/util/hex_float.h
110 ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.h
111 ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.h
112 ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.h
113 ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.h
114 ${CMAKE_CURRENT_SOURCE_DIR}/source/instruction.h
115 ${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.h
116 ${CMAKE_CURRENT_SOURCE_DIR}/source/operand.h
117 ${CMAKE_CURRENT_SOURCE_DIR}/source/print.h
118 ${CMAKE_CURRENT_SOURCE_DIR}/source/spirv_constant.h
119 ${CMAKE_CURRENT_SOURCE_DIR}/source/spirv_definition.h
120 ${CMAKE_CURRENT_SOURCE_DIR}/source/spirv_endian.h
121 ${CMAKE_CURRENT_SOURCE_DIR}/source/table.h
122 ${CMAKE_CURRENT_SOURCE_DIR}/source/text.h
123 ${CMAKE_CURRENT_SOURCE_DIR}/source/text_handler.h
124 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate.h
125 ${CMAKE_CURRENT_SOURCE_DIR}/source/assembly_grammar.cpp
126 ${CMAKE_CURRENT_SOURCE_DIR}/source/binary.cpp
127 ${CMAKE_CURRENT_SOURCE_DIR}/source/disassemble.cpp
128 ${CMAKE_CURRENT_SOURCE_DIR}/source/diagnostic.cpp
129 ${CMAKE_CURRENT_SOURCE_DIR}/source/ext_inst.cpp
130 ${CMAKE_CURRENT_SOURCE_DIR}/source/opcode.cpp
131 ${CMAKE_CURRENT_SOURCE_DIR}/source/operand.cpp
132 ${CMAKE_CURRENT_SOURCE_DIR}/source/print.cpp
133 ${CMAKE_CURRENT_SOURCE_DIR}/source/spirv_endian.cpp
134 ${CMAKE_CURRENT_SOURCE_DIR}/source/table.cpp
135 ${CMAKE_CURRENT_SOURCE_DIR}/source/text.cpp
136 ${CMAKE_CURRENT_SOURCE_DIR}/source/text_handler.cpp
137 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate.cpp
138 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_cfg.cpp
139 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_id.cpp
140 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_instruction.cpp
141 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_layout.cpp
142 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_ssa.cpp
143 ${CMAKE_CURRENT_SOURCE_DIR}/source/validate_types.cpp)
145 add_library(${SPIRV_TOOLS} ${SPIRV_SOURCES})
146 spvtools_default_compile_options(${SPIRV_TOOLS})
147 target_include_directories(${SPIRV_TOOLS} PUBLIC
148 ${CMAKE_CURRENT_SOURCE_DIR}/include
149 ${CMAKE_CURRENT_SOURCE_DIR}/external/include)
151 # Defaults to OFF if the user didn't set it.
152 option(SPIRV_SKIP_EXECUTABLES
153 "Skip building the executable and tests along with the library"
154 ${SPIRV_SKIP_EXECUTABLES})
155 set(SPIRV_INSTALL_TARGETS ${SPIRV_TOOLS})
156 if (NOT ${SPIRV_SKIP_EXECUTABLES})
157 list(APPEND SPIRV_INSTALL_TARGETS spirv-as spirv-dis spirv-val)
159 add_executable(spirv-as ${CMAKE_CURRENT_SOURCE_DIR}/tools/as/as.cpp)
160 spvtools_default_compile_options(spirv-as)
161 target_link_libraries(spirv-as PRIVATE ${SPIRV_TOOLS})
163 add_executable(spirv-dis ${CMAKE_CURRENT_SOURCE_DIR}/tools/dis/dis.cpp)
164 spvtools_default_compile_options(spirv-dis)
165 target_link_libraries(spirv-dis PRIVATE ${SPIRV_TOOLS})
167 add_executable(spirv-val ${CMAKE_CURRENT_SOURCE_DIR}/tools/val/val.cpp)
168 spvtools_default_compile_options(spirv-val)
169 target_link_libraries(spirv-val PRIVATE ${SPIRV_TOOLS})
171 # Find gmock if we can. If it's not already configured, then try finding
172 # it in external/googletest
174 message(STATUS "Google Mock already configured")
176 set(GMOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/external/googletest/googlemock)
177 if(EXISTS ${GMOCK_DIR})
179 option(gtest_force_shared_crt
180 "Use shared (DLL) run-time lib even when Google Test is built as static lib."
183 add_subdirectory(${GMOCK_DIR})
187 message(STATUS "Found Google Mock, building tests.")
190 ${CMAKE_CURRENT_SOURCE_DIR}/test/TestFixture.h
191 ${CMAKE_CURRENT_SOURCE_DIR}/test/UnitSPIRV.h
193 ${CMAKE_CURRENT_SOURCE_DIR}/test/AssemblyContext.cpp
194 ${CMAKE_CURRENT_SOURCE_DIR}/test/AssemblyFormat.cpp
195 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryDestroy.cpp
196 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryEndianness.cpp
197 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryHeaderGet.cpp
198 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryParse.cpp
199 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryToText.cpp
200 ${CMAKE_CURRENT_SOURCE_DIR}/test/BinaryToText.Literal.cpp
201 ${CMAKE_CURRENT_SOURCE_DIR}/test/Comment.cpp
202 ${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticDestroy.cpp
203 ${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticPrint.cpp
204 ${CMAKE_CURRENT_SOURCE_DIR}/test/DiagnosticStream.cpp
205 ${CMAKE_CURRENT_SOURCE_DIR}/test/ExtInstGLSLstd450.cpp
206 ${CMAKE_CURRENT_SOURCE_DIR}/test/ExtInst.OpenCL.std.cpp
207 ${CMAKE_CURRENT_SOURCE_DIR}/test/FixWord.cpp
208 ${CMAKE_CURRENT_SOURCE_DIR}/test/GeneratorMagicNumber.cpp
209 ${CMAKE_CURRENT_SOURCE_DIR}/test/HexFloat.cpp
210 ${CMAKE_CURRENT_SOURCE_DIR}/test/ImmediateInt.cpp
211 ${CMAKE_CURRENT_SOURCE_DIR}/test/LibspirvMacros.cpp
212 ${CMAKE_CURRENT_SOURCE_DIR}/test/NamedId.cpp
213 ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeMake.cpp
214 ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeRequiresCapabilities.cpp
215 ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeSplit.cpp
216 ${CMAKE_CURRENT_SOURCE_DIR}/test/OpcodeTableGet.cpp
217 ${CMAKE_CURRENT_SOURCE_DIR}/test/OperandCapabilities.cpp
218 ${CMAKE_CURRENT_SOURCE_DIR}/test/Operand.cpp
219 ${CMAKE_CURRENT_SOURCE_DIR}/test/OperandPattern.cpp
220 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextAdvance.cpp
221 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextDestroy.cpp
222 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextLiteral.cpp
223 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextStartsNewInst.cpp
224 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Annotation.cpp
225 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Barrier.cpp
226 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Constant.cpp
227 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.ControlFlow.cpp
228 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.cpp
229 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Debug.cpp
230 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.DeviceSideEnqueue.cpp
231 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Extension.cpp
232 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Function.cpp
233 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Group.cpp
234 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Image.cpp
235 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Literal.cpp
236 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Memory.cpp
237 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.Miscellaneous.cpp
238 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.ModeSetting.cpp
239 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextToBinary.TypeDeclaration.cpp
240 ${CMAKE_CURRENT_SOURCE_DIR}/test/TextWordGet.cpp
241 ${CMAKE_CURRENT_SOURCE_DIR}/test/UnitSPIRV.cpp
242 ${CMAKE_CURRENT_SOURCE_DIR}/test/ValidateFixtures.cpp
243 ${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.Capability.cpp
244 ${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.Layout.cpp
245 ${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.Storage.cpp
246 ${CMAKE_CURRENT_SOURCE_DIR}/test/Validate.SSA.cpp
247 ${CMAKE_CURRENT_SOURCE_DIR}/test/ValidateID.cpp
248 ${CMAKE_CURRENT_SOURCE_DIR}/test/ValidationState.cpp
249 ${CMAKE_CURRENT_SOURCE_DIR}/test/main.cpp)
251 add_executable(UnitSPIRV ${TEST_SOURCES})
252 spvtools_default_compile_options(UnitSPIRV)
254 target_compile_options(UnitSPIRV PRIVATE -Wno-undef)
256 if(${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
257 # Disable C4503 "decorated name length exceeded" warning, triggered
258 # by some heavily templated types.
259 # We don't care much about that in test code.
260 # Important to do since we have warnings-as-errors.
261 target_compile_options(UnitSPIRV PRIVATE /wd4503)
263 target_include_directories(UnitSPIRV PRIVATE
264 ${CMAKE_CURRENT_SOURCE_DIR}
265 ${gmock_SOURCE_DIR}/include ${gtest_SOURCE_DIR}/include)
266 target_link_libraries(UnitSPIRV PRIVATE ${SPIRV_TOOLS} gmock)
267 add_test(NAME spirv-tools-testsuite COMMAND UnitSPIRV)
269 message(STATUS "Did not find googletest, tests will not be built."
270 "To enable tests place googletest in '<spirv-dir>/external/googletest'.")
274 install(TARGETS ${SPIRV_INSTALL_TARGETS}
275 RUNTIME DESTINATION bin
276 LIBRARY DESTINATION lib
277 ARCHIVE DESTINATION lib)
279 install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/libspirv/libspirv.h
280 DESTINATION include/libspirv/)