blob: 61a7fffad68f4a5a321ee5457806bba4b9c8a1d3 [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})
dan sinclair4b71b9e2020-03-18 14:08:48 +000045option(TINT_BUILD_SPV_READER "Build the SPIR-V input reader" ON)
Ben Claytoned2b9782020-12-01 18:04:17 +000046option(TINT_BUILD_WGSL_READER "Build the WGSL input reader" ON)
dan sinclairfeffa9d2020-07-20 22:13:37 +000047option(TINT_BUILD_HLSL_WRITER "Build the HLSL output writer" ON)
dan sinclair2a599012020-06-23 17:48:40 +000048option(TINT_BUILD_MSL_WRITER "Build the MSL output writer" ON)
dan sinclair4b71b9e2020-03-18 14:08:48 +000049option(TINT_BUILD_SPV_WRITER "Build the SPIR-V output writer" ON)
50option(TINT_BUILD_WGSL_WRITER "Build the WGSL output writer" ON)
dan sinclair4b71b9e2020-03-18 14:08:48 +000051option(TINT_BUILD_FUZZERS "Build fuzzers" OFF)
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000052option(TINT_BUILD_SPIRV_TOOLS_FUZZER "Build SPIRV-Tools fuzzer" OFF)
Ben Clayton6341fa52021-03-09 13:55:37 +000053option(TINT_BUILD_TESTS "Build tests" ${TINT_BUILD_TESTS_DEFAULT})
Ben Clayton7687ec12021-04-16 14:43:34 +000054option(TINT_BUILD_AS_OTHER_OS "Override OS detection to force building of *_other.cc files" OFF)
Ryan Harrison563d3e92020-04-28 19:27:38 +000055
Dan Sinclair6e581892020-03-02 15:47:43 -050056option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
57option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
58option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
59
Ben Claytonadb10d62020-10-27 21:04:59 +000060option(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
61
Ryan Harrison563d3e92020-04-28 19:27:38 +000062option(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
63
Dan Sinclair6e581892020-03-02 15:47:43 -050064message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000065message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
66message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
dan sinclairfeffa9d2020-07-20 22:13:37 +000067message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
dan sinclair2a599012020-06-23 17:48:40 +000068message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000069message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
70message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
Dan Sinclair6e581892020-03-02 15:47:43 -050071message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000072message(STATUS "Tint build SPIRV-Tools fuzzer: ${TINT_BUILD_SPIRV_TOOLS_FUZZER}")
Corentin Wallezf3717fa2020-12-09 14:47:30 +000073message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
Dan Sinclair6e581892020-03-02 15:47:43 -050074message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}")
75message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}")
76message(STATUS "Tint build with UBSAN: ${TINT_ENABLE_UBSAN}")
Ryan Harrison563d3e92020-04-28 19:27:38 +000077message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
Dan Sinclair6e581892020-03-02 15:47:43 -050078
79message(STATUS "Using python3")
80find_package(PythonInterp 3 REQUIRED)
81
Vasyl Teliman0b3611b2021-06-24 18:10:46 +000082if (${TINT_BUILD_SPIRV_TOOLS_FUZZER})
83 message(STATUS "TINT_BUILD_SPIRV_TOOLS_FUZZER is ON - setting
84 TINT_BUILD_FUZZERS,
85 TINT_BUILD_SPV_READER,
86 TINT_BUILD_WGSL_READER,
87 TINT_BUILD_WGSL_WRITER,
88 TINT_BUILD_HLSL_WRITER,
89 TINT_BUILD_MSL_WRITER,
90 TINT_BUILD_SPV_WRITER to ON")
91 set(TINT_BUILD_FUZZERS ON)
92 set(TINT_BUILD_SPV_READER ON)
93 set(TINT_BUILD_WGSL_READER ON)
94 set(TINT_BUILD_WGSL_WRITER ON)
95 set(TINT_BUILD_HLSL_WRITER ON)
96 set(TINT_BUILD_MSL_WRITER ON)
97 set(TINT_BUILD_SPV_WRITER ON)
98endif()
99
Antonio Maioranod6009722021-03-17 13:32:54 +0000100# CMake < 3.15 sets /W3 in CMAKE_CXX_FLAGS. Remove it if it's there.
101# See https://gitlab.kitware.com/cmake/cmake/-/issues/18317
102if (MSVC)
Vasyl Teliman0b3611b2021-06-24 18:10:46 +0000103 if (CMAKE_CXX_FLAGS MATCHES "/W3")
104 string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
105 endif()
Antonio Maioranod6009722021-03-17 13:32:54 +0000106endif()
107
Ryan Harrison563d3e92020-04-28 19:27:38 +0000108if (${TINT_CHECK_CHROMIUM_STYLE})
109 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -add-plugin -Xclang find-bad-constructs")
110endif()
111
dan sinclair4b71b9e2020-03-18 14:08:48 +0000112if (${TINT_BUILD_SPV_READER})
Dan Sinclair6e581892020-03-02 15:47:43 -0500113 include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
114endif()
115
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000116if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
117 set(COMPILER_IS_CLANG_CL TRUE)
118endif()
119
120if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
121 (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR
122 ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND
123 (NOT COMPILER_IS_CLANG_CL)))
Dan Sinclair6e581892020-03-02 15:47:43 -0500124 set(COMPILER_IS_LIKE_GNU TRUE)
125endif()
126
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000127# Enable msbuild multiprocessor builds
128if (MSVC AND NOT COMPILER_IS_CLANG_CL)
129 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
130endif()
131
Ben Clayton7687ec12021-04-16 14:43:34 +0000132set(TINT_OS_CC_SUFFIX "other")
133if (NOT TINT_BUILD_AS_OTHER_OS)
134 if(UNIX OR APPLE)
135 set(TINT_OS_CC_SUFFIX "posix")
136 set(TINT_OS_CC_SUFFIX "posix")
137 elseif(WIN32)
138 set(TINT_OS_CC_SUFFIX "windows")
139 set(TINT_OS_CC_SUFFIX "windows")
140 endif()
141endif()
142
David Neto5b46d712020-06-26 22:29:27 +0000143if(${TINT_BUILD_DOCS})
144 find_package(Doxygen)
145 if(DOXYGEN_FOUND)
146 add_custom_target(tint-docs ALL
Ben Clayton4ace8222021-05-12 08:15:41 +0000147 COMMAND ${CMAKE_COMMAND}
148 -E env "DOXYGEN_OUTPUT_DIRECTORY=${CMAKE_BINARY_DIR}/docs"
149 ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
David Neto5b46d712020-06-26 22:29:27 +0000150 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
151 COMMENT "Generating API documentation"
152 VERBATIM)
153 else()
154 message("Doxygen not found. Skipping documentation")
155 endif(DOXYGEN_FOUND)
dan sinclairb0391c62020-07-15 20:54:48 +0000156endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500157
158function(tint_default_compile_options TARGET)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000159 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}")
160 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/include")
Dan Sinclair6e581892020-03-02 15:47:43 -0500161
dan sinclairf6fdcb12020-10-23 17:04:10 +0000162 if (${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
163 target_include_directories(${TARGET} PUBLIC
164 "${PROJECT_SOURCE_DIR}/third_party/spirv-headers/include")
165 endif()
166
167 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000168 -DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000169 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000170 -DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000171 target_compile_definitions(${TARGET} PUBLIC
dan sinclairfeffa9d2020-07-20 22:13:37 +0000172 -DTINT_BUILD_HLSL_WRITER=$<BOOL:${TINT_BUILD_HLSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000173 target_compile_definitions(${TARGET} PUBLIC
dan sinclair2a599012020-06-23 17:48:40 +0000174 -DTINT_BUILD_MSL_WRITER=$<BOOL:${TINT_BUILD_MSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000175 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000176 -DTINT_BUILD_SPV_WRITER=$<BOOL:${TINT_BUILD_SPV_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000177 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000178 -DTINT_BUILD_WGSL_WRITER=$<BOOL:${TINT_BUILD_WGSL_WRITER}>)
dan sinclair37dbf992020-03-11 18:43:12 +0000179
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000180 set(COMMON_GNU_OPTIONS
181 -Wall
182 -Werror
183 -Wextra
184 -Wno-documentation-unknown-command
185 -Wno-padded
186 -Wno-switch-enum
187 -Wno-unknown-pragmas
188 )
189
190 set(COMMON_CLANG_OPTIONS
191 -Wno-c++98-compat
192 -Wno-c++98-compat-pedantic
193 -Wno-format-pedantic
194 -Wno-return-std-move-in-c++11
195 -Wno-unknown-warning-option
196 -Wno-undefined-var-template
197 -Wno-used-but-marked-unused
198 -Weverything
199 )
200
Dan Sinclair6e581892020-03-02 15:47:43 -0500201 if (${COMPILER_IS_LIKE_GNU})
202 target_compile_options(${TARGET} PRIVATE
203 -std=c++14
204 -fno-exceptions
205 -fno-rtti
Dan Sinclair6e581892020-03-02 15:47:43 -0500206 -pedantic-errors
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000207 ${COMMON_GNU_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500208 )
209
210 if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
211 ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
212 target_compile_options(${TARGET} PRIVATE
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000213 ${COMMON_CLANG_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500214 )
215 endif()
216
217 if (${TINT_ENABLE_MSAN})
218 target_compile_options(${TARGET} PRIVATE -fsanitize=memory)
219 target_link_options(${TARGET} PRIVATE -fsanitize=memory)
220 elseif (${TINT_ENABLE_ASAN})
221 target_compile_options(${TARGET} PRIVATE -fsanitize=address)
222 target_link_options(${TARGET} PRIVATE -fsanitize=address)
223 elseif (${TINT_ENABLE_UBSAN})
224 target_compile_options(${TARGET} PRIVATE -fsanitize=undefined)
225 target_link_options(${TARGET} PRIVATE -fsanitize=undefined)
226 endif()
227 endif()
228
Ben Claytonadb10d62020-10-27 21:04:59 +0000229 if (${TINT_EMIT_COVERAGE})
230 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
231 target_compile_options(${TARGET} PRIVATE "--coverage")
232 target_link_options(${TARGET} PRIVATE "gcov")
233 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
234 target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
235 target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
236 else()
237 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
238 endif()
239 endif()
240
Dan Sinclair6e581892020-03-02 15:47:43 -0500241 if (MSVC)
242 # Specify /EHs for exception handling.
243 target_compile_options(${TARGET} PRIVATE
244 /bigobj
245 /EHsc
Antonio Maioranoac39fb42021-03-16 15:05:33 +0000246 /W4
Dan Sinclair6e581892020-03-02 15:47:43 -0500247 /WX
248 /wd4068
Antonio Maioranod6009722021-03-17 13:32:54 +0000249 /wd4127
dan sinclair6ca26992020-05-04 18:58:24 +0000250 /wd4244
251 /wd4267
Antonio Maioranoe5476052021-03-23 14:03:58 +0000252 /wd4324
Antonio Maioranod6009722021-03-17 13:32:54 +0000253 /wd4458
Dan Sinclair6e581892020-03-02 15:47:43 -0500254 /wd4514
255 /wd4571
256 /wd4625
257 /wd4626
258 /wd4710
259 /wd4774
260 /wd4820
261 /wd5026
262 /wd5027
263 )
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000264
265 # When building with clang-cl on Windows, try to match our clang build
266 # options as much as possible.
267 if (COMPILER_IS_CLANG_CL)
268 target_compile_options(${TARGET} PRIVATE
269 ${COMMON_GNU_OPTIONS}
270 ${COMMON_CLANG_OPTIONS}
271 # Disable warnings that are usually disabled in downstream deps for
272 # gcc/clang, but aren't for clang-cl.
273 -Wno-global-constructors
274 -Wno-zero-as-null-pointer-constant
275 -Wno-shorten-64-to-32
276 )
277 endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500278 endif()
dan sinclair6ca26992020-05-04 18:58:24 +0000279
Dan Sinclair6e581892020-03-02 15:47:43 -0500280endfunction()
281
282add_subdirectory(third_party)
283add_subdirectory(src)
284add_subdirectory(samples)
285
286if (${TINT_BUILD_FUZZERS})
Ryan Harrison13a65292020-04-23 13:51:39 +0000287 add_subdirectory(fuzzers)
Dan Sinclair6e581892020-03-02 15:47:43 -0500288endif()
289
dan sinclairb5950522020-03-19 13:03:35 +0000290add_custom_target(tint-lint
291 COMMAND ./tools/lint
292 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
293 COMMENT "Running linter"
294 VERBATIM)
295
296add_custom_target(tint-format
297 COMMAND ./tools/format
298 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
299 COMMENT "Running formatter"
300 VERBATIM)
Ben Claytonadb10d62020-10-27 21:04:59 +0000301
302
303if (${TINT_EMIT_COVERAGE} AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
304 # Generates a lcov.info file at the project root.
305 # This can be used by tools such as VSCode's Coverage Gutters extension to
306 # visualize code coverage in the editor.
307 get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
308 set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
309 add_custom_target(tint-generate-coverage
310 COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
311 DEPENDS tint_unittests
312 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
313 COMMENT "Generating tint coverage data"
314 VERBATIM)
315endif()