blob: 0c9e4438132803870962c9c6b0dd34ed5ca6cabb [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)
Ben Clayton6341fa52021-03-09 13:55:37 +000052option(TINT_BUILD_TESTS "Build tests" ${TINT_BUILD_TESTS_DEFAULT})
Ben Clayton7687ec12021-04-16 14:43:34 +000053option(TINT_BUILD_AS_OTHER_OS "Override OS detection to force building of *_other.cc files" OFF)
Ryan Harrison563d3e92020-04-28 19:27:38 +000054
Dan Sinclair6e581892020-03-02 15:47:43 -050055option(TINT_ENABLE_MSAN "Enable memory sanitizer" OFF)
56option(TINT_ENABLE_ASAN "Enable address sanitizer" OFF)
57option(TINT_ENABLE_UBSAN "Enable undefined behaviour sanitizer" OFF)
58
Ben Claytonadb10d62020-10-27 21:04:59 +000059option(TINT_EMIT_COVERAGE "Emit code coverage information" OFF)
60
Ryan Harrison563d3e92020-04-28 19:27:38 +000061option(TINT_CHECK_CHROMIUM_STYLE "Check for [chromium-style] issues during build" OFF)
62
Dan Sinclair6e581892020-03-02 15:47:43 -050063message(STATUS "Tint build docs: ${TINT_BUILD_DOCS}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000064message(STATUS "Tint build SPIR-V reader: ${TINT_BUILD_SPV_READER}")
65message(STATUS "Tint build WGSL reader: ${TINT_BUILD_WGSL_READER}")
dan sinclairfeffa9d2020-07-20 22:13:37 +000066message(STATUS "Tint build HLSL writer: ${TINT_BUILD_HLSL_WRITER}")
dan sinclair2a599012020-06-23 17:48:40 +000067message(STATUS "Tint build MSL writer: ${TINT_BUILD_MSL_WRITER}")
dan sinclair4b71b9e2020-03-18 14:08:48 +000068message(STATUS "Tint build SPIR-V writer: ${TINT_BUILD_SPV_WRITER}")
69message(STATUS "Tint build WGSL writer: ${TINT_BUILD_WGSL_WRITER}")
Dan Sinclair6e581892020-03-02 15:47:43 -050070message(STATUS "Tint build fuzzers: ${TINT_BUILD_FUZZERS}")
Corentin Wallezf3717fa2020-12-09 14:47:30 +000071message(STATUS "Tint build tests: ${TINT_BUILD_TESTS}")
Dan Sinclair6e581892020-03-02 15:47:43 -050072message(STATUS "Tint build with ASAN: ${TINT_ENABLE_ASAN}")
73message(STATUS "Tint build with MSAN: ${TINT_ENABLE_MSAN}")
74message(STATUS "Tint build with UBSAN: ${TINT_ENABLE_UBSAN}")
Ryan Harrison563d3e92020-04-28 19:27:38 +000075message(STATUS "Tint build checking [chromium-style]: ${TINT_CHECK_CHROMIUM_STYLE}")
Dan Sinclair6e581892020-03-02 15:47:43 -050076
77message(STATUS "Using python3")
78find_package(PythonInterp 3 REQUIRED)
79
Antonio Maioranod6009722021-03-17 13:32:54 +000080# CMake < 3.15 sets /W3 in CMAKE_CXX_FLAGS. Remove it if it's there.
81# See https://gitlab.kitware.com/cmake/cmake/-/issues/18317
82if (MSVC)
83 if (CMAKE_CXX_FLAGS MATCHES "/W3")
84 string(REPLACE "/W3" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
85 endif()
86endif()
87
Ryan Harrison563d3e92020-04-28 19:27:38 +000088if (${TINT_CHECK_CHROMIUM_STYLE})
89 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Xclang -add-plugin -Xclang find-bad-constructs")
90endif()
91
dan sinclair4b71b9e2020-03-18 14:08:48 +000092if (${TINT_BUILD_SPV_READER})
Dan Sinclair6e581892020-03-02 15:47:43 -050093 include_directories("${PROJECT_SOURCE_DIR}/third_party/spirv-tools/include")
94endif()
95
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +000096if((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND (CMAKE_CXX_SIMULATE_ID STREQUAL "MSVC"))
97 set(COMPILER_IS_CLANG_CL TRUE)
98endif()
99
100if((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR
101 (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang") OR
102 ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND
103 (NOT COMPILER_IS_CLANG_CL)))
Dan Sinclair6e581892020-03-02 15:47:43 -0500104 set(COMPILER_IS_LIKE_GNU TRUE)
105endif()
106
Antonio Maiorano88d3d2e2021-03-23 20:51:09 +0000107# Enable msbuild multiprocessor builds
108if (MSVC AND NOT COMPILER_IS_CLANG_CL)
109 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
110endif()
111
Ben Clayton7687ec12021-04-16 14:43:34 +0000112set(TINT_OS_CC_SUFFIX "other")
113if (NOT TINT_BUILD_AS_OTHER_OS)
114 if(UNIX OR APPLE)
115 set(TINT_OS_CC_SUFFIX "posix")
116 set(TINT_OS_CC_SUFFIX "posix")
117 elseif(WIN32)
118 set(TINT_OS_CC_SUFFIX "windows")
119 set(TINT_OS_CC_SUFFIX "windows")
120 endif()
121endif()
122
David Neto5b46d712020-06-26 22:29:27 +0000123if(${TINT_BUILD_DOCS})
124 find_package(Doxygen)
125 if(DOXYGEN_FOUND)
126 add_custom_target(tint-docs ALL
127 COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/Doxyfile
128 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
129 COMMENT "Generating API documentation"
130 VERBATIM)
131 else()
132 message("Doxygen not found. Skipping documentation")
133 endif(DOXYGEN_FOUND)
dan sinclairb0391c62020-07-15 20:54:48 +0000134endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500135
136function(tint_default_compile_options TARGET)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000137 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}")
138 target_include_directories(${TARGET} PUBLIC "${PROJECT_SOURCE_DIR}/include")
Dan Sinclair6e581892020-03-02 15:47:43 -0500139
dan sinclairf6fdcb12020-10-23 17:04:10 +0000140 if (${TINT_BUILD_SPV_READER} OR ${TINT_BUILD_SPV_WRITER})
141 target_include_directories(${TARGET} PUBLIC
142 "${PROJECT_SOURCE_DIR}/third_party/spirv-headers/include")
143 endif()
144
145 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000146 -DTINT_BUILD_SPV_READER=$<BOOL:${TINT_BUILD_SPV_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000147 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000148 -DTINT_BUILD_WGSL_READER=$<BOOL:${TINT_BUILD_WGSL_READER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000149 target_compile_definitions(${TARGET} PUBLIC
dan sinclairfeffa9d2020-07-20 22:13:37 +0000150 -DTINT_BUILD_HLSL_WRITER=$<BOOL:${TINT_BUILD_HLSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000151 target_compile_definitions(${TARGET} PUBLIC
dan sinclair2a599012020-06-23 17:48:40 +0000152 -DTINT_BUILD_MSL_WRITER=$<BOOL:${TINT_BUILD_MSL_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000153 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000154 -DTINT_BUILD_SPV_WRITER=$<BOOL:${TINT_BUILD_SPV_WRITER}>)
dan sinclairf6fdcb12020-10-23 17:04:10 +0000155 target_compile_definitions(${TARGET} PUBLIC
dan sinclair4b71b9e2020-03-18 14:08:48 +0000156 -DTINT_BUILD_WGSL_WRITER=$<BOOL:${TINT_BUILD_WGSL_WRITER}>)
dan sinclair37dbf992020-03-11 18:43:12 +0000157
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000158 set(COMMON_GNU_OPTIONS
159 -Wall
160 -Werror
161 -Wextra
162 -Wno-documentation-unknown-command
163 -Wno-padded
164 -Wno-switch-enum
165 -Wno-unknown-pragmas
166 )
167
168 set(COMMON_CLANG_OPTIONS
169 -Wno-c++98-compat
170 -Wno-c++98-compat-pedantic
171 -Wno-format-pedantic
172 -Wno-return-std-move-in-c++11
173 -Wno-unknown-warning-option
174 -Wno-undefined-var-template
175 -Wno-used-but-marked-unused
176 -Weverything
177 )
178
Dan Sinclair6e581892020-03-02 15:47:43 -0500179 if (${COMPILER_IS_LIKE_GNU})
180 target_compile_options(${TARGET} PRIVATE
181 -std=c++14
182 -fno-exceptions
183 -fno-rtti
Dan Sinclair6e581892020-03-02 15:47:43 -0500184 -pedantic-errors
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000185 ${COMMON_GNU_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500186 )
187
188 if (("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") OR
189 ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "AppleClang"))
190 target_compile_options(${TARGET} PRIVATE
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000191 ${COMMON_CLANG_OPTIONS}
Dan Sinclair6e581892020-03-02 15:47:43 -0500192 )
193 endif()
194
195 if (${TINT_ENABLE_MSAN})
196 target_compile_options(${TARGET} PRIVATE -fsanitize=memory)
197 target_link_options(${TARGET} PRIVATE -fsanitize=memory)
198 elseif (${TINT_ENABLE_ASAN})
199 target_compile_options(${TARGET} PRIVATE -fsanitize=address)
200 target_link_options(${TARGET} PRIVATE -fsanitize=address)
201 elseif (${TINT_ENABLE_UBSAN})
202 target_compile_options(${TARGET} PRIVATE -fsanitize=undefined)
203 target_link_options(${TARGET} PRIVATE -fsanitize=undefined)
204 endif()
205 endif()
206
Ben Claytonadb10d62020-10-27 21:04:59 +0000207 if (${TINT_EMIT_COVERAGE})
208 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
209 target_compile_options(${TARGET} PRIVATE "--coverage")
210 target_link_options(${TARGET} PRIVATE "gcov")
211 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
212 target_compile_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
213 target_link_options(${TARGET} PRIVATE "-fprofile-instr-generate" "-fcoverage-mapping")
214 else()
215 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
216 endif()
217 endif()
218
Dan Sinclair6e581892020-03-02 15:47:43 -0500219 if (MSVC)
220 # Specify /EHs for exception handling.
221 target_compile_options(${TARGET} PRIVATE
222 /bigobj
223 /EHsc
Antonio Maioranoac39fb42021-03-16 15:05:33 +0000224 /W4
Dan Sinclair6e581892020-03-02 15:47:43 -0500225 /WX
226 /wd4068
Antonio Maioranod6009722021-03-17 13:32:54 +0000227 /wd4127
dan sinclair6ca26992020-05-04 18:58:24 +0000228 /wd4244
229 /wd4267
Antonio Maioranoe5476052021-03-23 14:03:58 +0000230 /wd4324
Antonio Maioranod6009722021-03-17 13:32:54 +0000231 /wd4458
Dan Sinclair6e581892020-03-02 15:47:43 -0500232 /wd4514
233 /wd4571
234 /wd4625
235 /wd4626
236 /wd4710
237 /wd4774
238 /wd4820
239 /wd5026
240 /wd5027
241 )
Antonio Maiorano5bdece52021-04-27 19:24:47 +0000242
243 # When building with clang-cl on Windows, try to match our clang build
244 # options as much as possible.
245 if (COMPILER_IS_CLANG_CL)
246 target_compile_options(${TARGET} PRIVATE
247 ${COMMON_GNU_OPTIONS}
248 ${COMMON_CLANG_OPTIONS}
249 # Disable warnings that are usually disabled in downstream deps for
250 # gcc/clang, but aren't for clang-cl.
251 -Wno-global-constructors
252 -Wno-zero-as-null-pointer-constant
253 -Wno-shorten-64-to-32
254 )
255 endif()
Dan Sinclair6e581892020-03-02 15:47:43 -0500256 endif()
dan sinclair6ca26992020-05-04 18:58:24 +0000257
Dan Sinclair6e581892020-03-02 15:47:43 -0500258endfunction()
259
260add_subdirectory(third_party)
261add_subdirectory(src)
262add_subdirectory(samples)
263
264if (${TINT_BUILD_FUZZERS})
Ryan Harrison13a65292020-04-23 13:51:39 +0000265 add_subdirectory(fuzzers)
Dan Sinclair6e581892020-03-02 15:47:43 -0500266endif()
267
dan sinclairb5950522020-03-19 13:03:35 +0000268add_custom_target(tint-lint
269 COMMAND ./tools/lint
270 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
271 COMMENT "Running linter"
272 VERBATIM)
273
274add_custom_target(tint-format
275 COMMAND ./tools/format
276 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
277 COMMENT "Running formatter"
278 VERBATIM)
Ben Claytonadb10d62020-10-27 21:04:59 +0000279
280
281if (${TINT_EMIT_COVERAGE} AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
282 # Generates a lcov.info file at the project root.
283 # This can be used by tools such as VSCode's Coverage Gutters extension to
284 # visualize code coverage in the editor.
285 get_filename_component(CLANG_BIN_DIR ${CMAKE_C_COMPILER} DIRECTORY)
286 set(PATH_WITH_CLANG "${CLANG_BIN_DIR}:$ENV{PATH}")
287 add_custom_target(tint-generate-coverage
288 COMMAND ${CMAKE_COMMAND} -E env PATH=${PATH_WITH_CLANG} ./tools/tint-generate-coverage $<TARGET_FILE:tint_unittests>
289 DEPENDS tint_unittests
290 WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
291 COMMENT "Generating tint coverage data"
292 VERBATIM)
293endif()