blob: 47a130ad15ef223ef11bab408d907f814f2a9a47 [file] [log] [blame]
Dan Sinclair6e581892020-03-02 15:47:43 -05001# Copyright 2020 The Tint Authors.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_minimum_required(VERSION 3.10.2)
16
17project(tint)
18enable_testing()
19set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR})
20set(CMAKE_POSITION_INDEPENDENT_CODE ON)
21set(CMAKE_CXX_STANDARD 14)
22set(CMAKE_DEBUG_POSTFIX "")
23
24if ("${CMAKE_BUILD_TYPE}" STREQUAL "")
25 message(STATUS "No build type selected, default to Debug")
26 set(CMAKE_BUILD_TYPE "Debug")
27endif()
28
Ben Clayton6341fa52021-03-09 13:55:37 +000029# TINT_IS_SUBPROJECT is 1 if added via add_subdirectory() from another project.
30get_directory_property(TINT_IS_SUBPROJECT PARENT_DIRECTORY)
31if(TINT_IS_SUBPROJECT)
32 set(TINT_IS_SUBPROJECT 1)
33
34 # If tint is used as a subproject, default to disabling the building of
35 # documentation and tests. These are unlikely to be desirable, but can be
36 # enabled.
37 set(TINT_BUILD_DOCS_DEFAULT OFF)
38 set(TINT_BUILD_TESTS_DEFAULT OFF)
39else()
40 set(TINT_BUILD_DOCS_DEFAULT ON)
41 set(TINT_BUILD_TESTS_DEFAULT ON)
42endif()
43
44option(TINT_BUILD_DOCS "Build documentation" ${TINT_BUILD_DOCS_DEFAULT})
Antonio Maiorano6241f962021-06-25 14:00:36 +000045option(TINT_DOCS_WARN_AS_ERROR "When building documentation, treat warnings as errors" OFF)
dan sinclair4b71b9e2020-03-18 14:08:48 +000046option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ON)
Ben Claytoned2b9782020-12-01 18:04:17 +000047option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
dan sinclairfeffa9d2020-07-20 22:13:37 +000048option(TINT_BUILD_HLSL_WRITER "Build the HLSL output writer" ON)
dan sinclair2a599012020-06-23 17:48:40 +000049option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" ON)
dan sinclair4b71b9e2020-03-18 14:08:48 +000050option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
51option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
dan sinclair4b71b9e2020-03-18 14:08:48 +000052option(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000053option(TINT_BUILD_SPIRV_TOOLS_FUZZER "Build SPIRV-Tools fuzzer" OFF)
Ben Clayton6341fa52021-03-09 13:55:37 +000054option(TINT_BUILD_TESTS "Build tests" ${TINT_BUILD_TESTS_DEFAULT})
Ben Clayton7687ec12021-04-16 14:43:34 +000055option(TINT_BUILD_AS_OTHER_OS "Override OS detection to force building of *_other.cc files" OFF)
Ryan Harrison563d3e92020-04-28 19:27:38 +000056
Dan Sinclair6e581892020-03-02 15:47:43 -050057option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
58option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
59option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
60
Ben Claytonadb10d62020-10-27 21:04:59 +000061option(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
62
Ryan Harrison563d3e92020-04-28 19:27:38 +000063option(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
64
Dan Sinclair6e581892020-03-02 15:47:43 -050065message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
Antonio Maiorano6241f962021-06-25 14:00:36 +000066message(STATUS "Tint build docs with warn as error: ${TINT_DOCS_WARN_AS_ERROR}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000067message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
68message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
dan sinclairfeffa9d2020-07-20 22:13:37 +000069message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
dan sinclair2a599012020-06-23 17:48:40 +000070message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000071message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
72message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
Dan Sinclair6e581892020-03-02 15:47:43 -050073message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000074message(STATUS "Tint build SPIRV-Tools fuzzer: ${TINT_BUILD_SPIRV_TOOLS_FUZZER}")
Corentin Wallezf3717fa2020-12-09 14:47:30 +000075message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
Dan Sinclair6e581892020-03-02 15:47:43 -050076message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}")
77message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}")
78message(STATUS "Tint build with UBSAN: ${TINT_ENABLE_UBSAN}")
Ryan Harrison563d3e92020-04-28 19:27:38 +000079message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
Dan Sinclair6e581892020-03-02 15:47:43 -050080
81message(STATUS "Using python3")
82find_package(PythonInterp 3 REQUIRED)
83
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000084if (${TINT_BUILD_SPIRV_TOOLS_FUZZER})
85 message(STATUS "TINT_BUILD_SPIRV_TOOLS_FUZZER is ON - setting
86 TINT_BUILD_FUZZERS,
87 TINT_BUILD_SPV_READER,
88 TINT_BUILD_WGSL_READER,
89 TINT_BUILD_WGSL_WRITER,
90 TINT_BUILD_HLSL_WRITER,
91 TINT_BUILD_MSL_WRITER,
92 TINT_BUILD_SPV_WRITER to ON")
93 set(TINT_BUILD_FUZZERS ON)
94 set(TINT_BUILD_SPV_READER ON)
95 set(TINT_BUILD_WGSL_READER ON)
96 set(TINT_BUILD_WGSL_WRITER ON)
97 set(TINT_BUILD_HLSL_WRITER ON)
98 set(TINT_BUILD_MSL_WRITER ON)
99 set(TINT_BUILD_SPV_WRITER ON)
100endif()
101
Antonio Maioranod6009722021-03-17 13:32:54 +0000102# CMake < 3.15 sets /W3 in CMAKE_CXX_FLAGS. Remove it if it's there.
103# See https://gitlab.kitware.com/cmake/cmake/-/issues/18317
104if (MSVC)
Vasyl Teliman0b3611b2021-06-24 18:10:46 +0000105 if (CMAKE_CXX_FLAGS MATCHES "/W3")
106 string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
107 endif()
Antonio Maioranod6009722021-03-17 13:32:54 +0000108endif()
109
Ryan Harrison563d3e92020-04-28 19:27:38 +0000110if (${TINT_CHECK_CHROMIUM_STYLE})
111 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -add-plugin -Xclang find-bad-constructs")
112endif()
113
dan sinclair4b71b9e2020-03-18 14:08:48 +0000114if (${TINT_BUILD_SPV_READER})
Dan Sinclair6e581892020-03-02 15:47:43 -0500115 include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
116endif()
117
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000118if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
119 set(COMPILER_IS_CLANG_CL TRUE)
120endif()
121
122if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
123 (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR
124 ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND
125 (NOT COMPILER_IS_CLANG_CL)))
Dan Sinclair6e581892020-03-02 15:47:43 -0500126 set(COMPILER_IS_LIKE_GNU TRUE)
127endif()
128
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000129# Enable msbuild multiprocessor builds
130if (MSVC AND NOT COMPILER_IS_CLANG_CL)
131 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
132endif()
133
Ben Clayton7687ec12021-04-16 14:43:34 +0000134set(TINT_OS_CC_SUFFIX "other")
135if (NOT TINT_BUILD_AS_OTHER_OS)
136 if(UNIX OR APPLE)
137 set(TINT_OS_CC_SUFFIX "posix")
138 set(TINT_OS_CC_SUFFIX "posix")
139 elseif(WIN32)
140 set(TINT_OS_CC_SUFFIX "windows")
141 set(TINT_OS_CC_SUFFIX "windows")
142 endif()
143endif()
144
David Neto5b46d712020-06-26 22:29:27 +0000145if(${TINT_BUILD_DOCS})
146 find_package(Doxygen)
147 if(DOXYGEN_FOUND)
Antonio Maiorano6241f962021-06-25 14:00:36 +0000148 set(DOXYGEN_WARN_AS_ERROR NO)
149 if(TINT_DOCS_WARN_AS_ERROR)
150 set(DOXYGEN_WARN_AS_ERROR YES)
151 endif()
Antonio Maioranoe25a8fc2021-06-25 14:53:06 +0000152
153 set(DOXYGEN_WARN_FORMAT "$file:$line: $text")
154 if (MSVC)
155 set(DOXYGEN_WARN_FORMAT "$file($line): $text")
156 endif()
157
David Neto5b46d712020-06-26 22:29:27 +0000158 add_custom_target(tint-docs ALL
Ben Clayton4ace8222021-05-12 08:15:41 +0000159 COMMAND ${CMAKE_COMMAND}
Antonio Maiorano6241f962021-06-25 14:00:36 +0000160 -E env
161 "DOXYGEN_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/docs"
162 "DOXYGEN_WARN_AS_ERROR=${DOXYGEN_WARN_AS_ERROR}"
Antonio Maioranoe25a8fc2021-06-25 14:53:06 +0000163 "DOXYGEN_WARN_FORMAT=${DOXYGEN_WARN_FORMAT}"
Ben Clayton4ace8222021-05-12 08:15:41 +0000164 ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
David Neto5b46d712020-06-26 22:29:27 +0000165 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
166 COMMENT "Generating API documentation"
167 VERBATIM)
168 else()
169 message("Doxygen not found. Skipping documentation")
170 endif(DOXYGEN_FOUND)
dan sinclairb0391c62020-07-15 20:54:48 +0000171endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500172
173function(tint_default_compile_options TARGET)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000174 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}")
175 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/include")
Dan Sinclair6e581892020-03-02 15:47:43 -0500176
dan sinclairf6fdcb12020-10-23 17:04:10 +0000177 if (${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
178 target_include_directories(${TARGET} PUBLIC
179 "${PROJECT_SOURCE_DIR}/third_party/spirv-headers/include")
180 endif()
181
182 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000183 -DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000184 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000185 -DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000186 target_compile_definitions(${TARGET} PUBLIC
dan sinclairfeffa9d2020-07-20 22:13:37 +0000187 -DTINT_BUILD_HLSL_WRITER=$<BOOL:${TINT_BUILD_HLSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000188 target_compile_definitions(${TARGET} PUBLIC
dan sinclair2a599012020-06-23 17:48:40 +0000189 -DTINT_BUILD_MSL_WRITER=$<BOOL:${TINT_BUILD_MSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000190 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000191 -DTINT_BUILD_SPV_WRITER=$<BOOL:${TINT_BUILD_SPV_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000192 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000193 -DTINT_BUILD_WGSL_WRITER=$<BOOL:${TINT_BUILD_WGSL_WRITER}>)
dan sinclair37dbf992020-03-11 18:43:12 +0000194
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000195 set(COMMON_GNU_OPTIONS
196 -Wall
197 -Werror
198 -Wextra
199 -Wno-documentation-unknown-command
200 -Wno-padded
201 -Wno-switch-enum
202 -Wno-unknown-pragmas
203 )
204
205 set(COMMON_CLANG_OPTIONS
206 -Wno-c++98-compat
207 -Wno-c++98-compat-pedantic
208 -Wno-format-pedantic
209 -Wno-return-std-move-in-c++11
210 -Wno-unknown-warning-option
211 -Wno-undefined-var-template
212 -Wno-used-but-marked-unused
213 -Weverything
214 )
215
Dan Sinclair6e581892020-03-02 15:47:43 -0500216 if (${COMPILER_IS_LIKE_GNU})
217 target_compile_options(${TARGET} PRIVATE
218 -std=c++14
219 -fno-exceptions
220 -fno-rtti
Dan Sinclair6e581892020-03-02 15:47:43 -0500221 -pedantic-errors
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000222 ${COMMON_GNU_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500223 )
224
225 if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
226 ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
227 target_compile_options(${TARGET} PRIVATE
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000228 ${COMMON_CLANG_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500229 )
230 endif()
231
232 if (${TINT_ENABLE_MSAN})
233 target_compile_options(${TARGET} PRIVATE -fsanitize=memory)
234 target_link_options(${TARGET} PRIVATE -fsanitize=memory)
235 elseif (${TINT_ENABLE_ASAN})
236 target_compile_options(${TARGET} PRIVATE -fsanitize=address)
237 target_link_options(${TARGET} PRIVATE -fsanitize=address)
238 elseif (${TINT_ENABLE_UBSAN})
239 target_compile_options(${TARGET} PRIVATE -fsanitize=undefined)
240 target_link_options(${TARGET} PRIVATE -fsanitize=undefined)
241 endif()
242 endif()
243
Ben Claytonadb10d62020-10-27 21:04:59 +0000244 if (${TINT_EMIT_COVERAGE})
245 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
246 target_compile_options(${TARGET} PRIVATE "--coverage")
247 target_link_options(${TARGET} PRIVATE "gcov")
248 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
249 target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
250 target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
251 else()
252 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
253 endif()
254 endif()
255
Dan Sinclair6e581892020-03-02 15:47:43 -0500256 if (MSVC)
257 # Specify /EHs for exception handling.
258 target_compile_options(${TARGET} PRIVATE
259 /bigobj
260 /EHsc
Antonio Maioranoac39fb42021-03-16 15:05:33 +0000261 /W4
Dan Sinclair6e581892020-03-02 15:47:43 -0500262 /WX
263 /wd4068
Antonio Maioranod6009722021-03-17 13:32:54 +0000264 /wd4127
dan sinclair6ca26992020-05-04 18:58:24 +0000265 /wd4244
266 /wd4267
Antonio Maioranoe5476052021-03-23 14:03:58 +0000267 /wd4324
Antonio Maioranod6009722021-03-17 13:32:54 +0000268 /wd4458
Dan Sinclair6e581892020-03-02 15:47:43 -0500269 /wd4514
270 /wd4571
271 /wd4625
272 /wd4626
273 /wd4710
274 /wd4774
275 /wd4820
276 /wd5026
277 /wd5027
278 )
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000279
280 # When building with clang-cl on Windows, try to match our clang build
281 # options as much as possible.
282 if (COMPILER_IS_CLANG_CL)
283 target_compile_options(${TARGET} PRIVATE
284 ${COMMON_GNU_OPTIONS}
285 ${COMMON_CLANG_OPTIONS}
286 # Disable warnings that are usually disabled in downstream deps for
287 # gcc/clang, but aren't for clang-cl.
288 -Wno-global-constructors
289 -Wno-zero-as-null-pointer-constant
290 -Wno-shorten-64-to-32
291 )
292 endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500293 endif()
dan sinclair6ca26992020-05-04 18:58:24 +0000294
Dan Sinclair6e581892020-03-02 15:47:43 -0500295endfunction()
296
297add_subdirectory(third_party)
298add_subdirectory(src)
299add_subdirectory(samples)
300
301if (${TINT_BUILD_FUZZERS})
Ryan Harrison13a65292020-04-23 13:51:39 +0000302 add_subdirectory(fuzzers)
Dan Sinclair6e581892020-03-02 15:47:43 -0500303endif()
304
dan sinclairb5950522020-03-19 13:03:35 +0000305add_custom_target(tint-lint
306 COMMAND ./tools/lint
307 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
308 COMMENT "Running linter"
309 VERBATIM)
310
311add_custom_target(tint-format
312 COMMAND ./tools/format
313 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
314 COMMENT "Running formatter"
315 VERBATIM)
Ben Claytonadb10d62020-10-27 21:04:59 +0000316
317
318if (${TINT_EMIT_COVERAGE} AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
319 # Generates a lcov.info file at the project root.
320 # This can be used by tools such as VSCode's Coverage Gutters extension to
321 # visualize code coverage in the editor.
322 get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
323 set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
324 add_custom_target(tint-generate-coverage
325 COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
326 DEPENDS tint_unittests
327 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
328 COMMENT "Generating tint coverage data"
329 VERBATIM)
330endif()