blob: e64465c13553443813858a5f685680993328f479 [file] [log] [blame]
Ben Clayton3c690342020-03-24 22:38:59 +00001# Copyright 2020 The SwiftShader Authors. All Rights Reserved.
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.6.3)
Corentin Wallez0866b292015-12-09 13:49:40 -050016
Ben Clayton3cc0aea2020-01-08 19:09:25 +000017set(CMAKE_CXX_STANDARD 14)
18
Ben Clayton30b6b592019-08-07 15:04:11 +010019project(SwiftShader C CXX ASM)
Corentin Wallez0866b292015-12-09 13:49:40 -050020
Corentin Wallez0866b292015-12-09 13:49:40 -050021###########################################################
22# Detect system
23###########################################################
24
Nicolas Capens6f422092015-12-23 15:12:45 -050025if(CMAKE_SYSTEM_NAME MATCHES "Linux")
Nicolas Capens1dfcdb02020-03-12 21:12:52 +000026 set(LINUX TRUE)
Stephen Whitee6ab01f2019-04-04 14:31:25 -040027elseif(CMAKE_SYSTEM_NAME MATCHES "Android")
Nicolas Capens1dfcdb02020-03-12 21:12:52 +000028 set(ANDROID TRUE)
Stephen Whitee6ab01f2019-04-04 14:31:25 -040029 set(CMAKE_CXX_FLAGS "-DANDROID_NDK_BUILD")
Corentin Wallez0866b292015-12-09 13:49:40 -050030elseif(WIN32)
31elseif(APPLE)
David 'Digit' Turnerd3717932019-11-19 17:54:00 +010032elseif(FUCHSIA)
33 # NOTE: Building for Fuchsia requires a Fuchsia CMake-based SDK.
34 # See https://fuchsia-review.googlesource.com/c/fuchsia/+/379673
35 find_package(FuchsiaSdk)
Corentin Wallez0866b292015-12-09 13:49:40 -050036else()
37 message(FATAL_ERROR "Platform is not supported")
38endif()
39
Nicolas Capens30cd7d42017-04-25 15:17:25 -040040if(CMAKE_SYSTEM_PROCESSOR MATCHES "arm" OR CMAKE_SYSTEM_PROCESSOR MATCHES "aarch")
41 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
42 set(ARCH "aarch64")
43 else()
44 set(ARCH "arm")
45 endif()
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +020046elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "mips*")
47 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
48 set(ARCH "mips64el")
49 else()
50 set(ARCH "mipsel")
51 endif()
Colin Samplesf63a3ab2019-06-13 12:53:09 -040052elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "ppc*")
53 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
54 set(ARCH "ppc64le")
55 else()
56 message(FATAL_ERROR "Architecture is not supported")
57 endif()
Corentin Wallez0866b292015-12-09 13:49:40 -050058else()
Nicolas Capens30cd7d42017-04-25 15:17:25 -040059 if(CMAKE_SIZEOF_VOID_P EQUAL 8)
60 set(ARCH "x86_64")
61 else()
62 set(ARCH "x86")
63 endif()
Corentin Wallez0866b292015-12-09 13:49:40 -050064endif()
65
Nicolas Capens1dfcdb02020-03-12 21:12:52 +000066set(CMAKE_MACOSX_RPATH TRUE)
Nicolas Capens007c6c52017-06-09 11:21:48 -040067
Nicolas Capensd7a21cc2018-09-11 13:09:28 -040068if ((CMAKE_GENERATOR MATCHES "Visual Studio") AND (CMAKE_GENERATOR_TOOLSET STREQUAL ""))
69 message(WARNING "Visual Studio generators use the x86 host compiler by "
70 "default, even for 64-bit targets. This can result in linker "
71 "instability and out of memory errors. To use the 64-bit "
72 "host compiler, pass -Thost=x64 on the CMake command line.")
73endif()
74
Ben Clayton4901ffd2019-06-27 10:39:07 +010075# Use CCache if available
76find_program(CCACHE_FOUND ccache)
77if(CCACHE_FOUND)
78 message(STATUS "Using ccache")
79 set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
80 set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
Ben Clayton1e8486b2020-01-22 17:01:52 +000081endif()
Ben Clayton4901ffd2019-06-27 10:39:07 +010082
Corentin Wallez0866b292015-12-09 13:49:40 -050083###########################################################
Ben Claytona9af8832019-08-14 13:09:43 +010084# Host libraries
85###########################################################
86
87find_library(X11 X11)
88find_library(XCB xcb)
89
90###########################################################
Nicolas Capens18b8d682017-07-25 15:31:45 -040091# Options
92###########################################################
93
94if(NOT CMAKE_BUILD_TYPE)
95 set(CMAKE_BUILD_TYPE "Release" CACHE STRING "The type of build: Debug Release MinSizeRel RelWithDebInfo." FORCE)
96endif()
97set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS Debug Release MinSizeRel RelWithDebInfo)
98
Ben Clayton5837d872020-01-20 16:23:36 +000099function (option_if_not_defined name description default)
100 if(NOT DEFINED ${name})
101 option(${name} ${description} ${default})
102 endif()
103endfunction()
Nicolas Capens18b8d682017-07-25 15:31:45 -0400104
Ben Clayton9cc163c2020-01-20 16:26:36 +0000105function (set_if_not_defined name value)
106 if(NOT DEFINED ${name})
107 set(${name} ${value} PARENT_SCOPE)
108 endif()
109endfunction()
110
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000111option_if_not_defined(SWIFTSHADER_BUILD_EGL "Build the EGL library" TRUE)
112option_if_not_defined(SWIFTSHADER_BUILD_GLESv2 "Build the OpenGL ES 2 library" TRUE)
113option_if_not_defined(SWIFTSHADER_BUILD_GLES_CM "Build the OpenGL ES 1.1 library" TRUE)
114option_if_not_defined(SWIFTSHADER_BUILD_VULKAN "Build the Vulkan library" TRUE)
Nicolas Capens13943ba2020-03-17 22:36:24 -0400115option_if_not_defined(SWIFTSHADER_BUILD_PVR "Build the PowerVR examples" TRUE)
116option_if_not_defined(SWIFTSHADER_GET_PVR "Check out the PowerVR submodule" FALSE)
Nicolas Capens18b8d682017-07-25 15:31:45 -0400117
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000118option_if_not_defined(SWIFTSHADER_USE_GROUP_SOURCES "Group the source files in a folder tree for Visual Studio" TRUE)
Nicolas Capens18b8d682017-07-25 15:31:45 -0400119
Nicolas Capens45755df2020-03-30 12:42:40 -0400120option_if_not_defined(SWIFTSHADER_BUILD_TESTS "Build unit tests" TRUE)
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000121option_if_not_defined(SWIFTSHADER_BUILD_BENCHMARKS "Build benchmarks" FALSE)
Ben Clayton5837d872020-01-20 16:23:36 +0000122
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000123option_if_not_defined(SWIFTSHADER_MSAN "Build with memory sanitizer" FALSE)
124option_if_not_defined(SWIFTSHADER_ASAN "Build with address sanitizer" FALSE)
125option_if_not_defined(SWIFTSHADER_TSAN "Build with thread sanitizer" FALSE)
126option_if_not_defined(SWIFTSHADER_UBSAN "Build with undefined behavior sanitizer" FALSE)
Ben Clayton063fc022020-03-23 13:18:09 +0000127option_if_not_defined(SWIFTSHADER_EMIT_COVERAGE "Emit code coverage information" FALSE)
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000128option_if_not_defined(SWIFTSHADER_WARNINGS_AS_ERRORS "Treat all warnings as errors" TRUE)
129option_if_not_defined(SWIFTSHADER_DCHECK_ALWAYS_ON "Check validation macros even in release builds" FALSE)
130option_if_not_defined(REACTOR_EMIT_DEBUG_INFO "Emit debug info for JIT functions" FALSE)
131option_if_not_defined(REACTOR_EMIT_PRINT_LOCATION "Emit printing of location info for JIT functions" FALSE)
132option_if_not_defined(REACTOR_ENABLE_PRINT "Enable RR_PRINT macros" FALSE)
133option_if_not_defined(REACTOR_VERIFY_LLVM_IR "Check reactor-generated LLVM IR is valid even in release builds" FALSE)
134option_if_not_defined(SWIFTSHADER_LESS_DEBUG_INFO "Generate less debug info to reduce file size" FALSE)
135option_if_not_defined(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER "Enable Vulkan debugger support" FALSE)
136option_if_not_defined(SWIFTSHADER_ENABLE_ASTC "Enable ASTC compressed textures support" TRUE) # TODO(b/150130101)
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400137
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500138set(BUILD_MARL ${SWIFTSHADER_BUILD_VULKAN})
Ben Clayton5e4d55f2019-12-10 19:40:58 +0000139
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500140if(${SWIFTSHADER_BUILD_VULKAN} AND ${SWIFTSHADER_ENABLE_VULKAN_DEBUGGER})
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000141 set_if_not_defined(SWIFTSHADER_BUILD_CPPDAP TRUE)
Ben Clayton5e4d55f2019-12-10 19:40:58 +0000142else()
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000143 set_if_not_defined(SWIFTSHADER_BUILD_CPPDAP FALSE)
Ben Clayton5e4d55f2019-12-10 19:40:58 +0000144endif()
Ben Claytone693b622019-09-05 12:48:37 +0100145
Nicolas Capens5f8a16a2019-08-15 10:36:13 -0400146set(DEFAULT_REACTOR_BACKEND "LLVM")
Nicolas Capens3957b7f2018-10-15 12:54:41 -0400147set(REACTOR_BACKEND ${DEFAULT_REACTOR_BACKEND} CACHE STRING "JIT compiler back-end used by Reactor")
Nicolas Capens18b8d682017-07-25 15:31:45 -0400148set_property(CACHE REACTOR_BACKEND PROPERTY STRINGS LLVM Subzero)
149
Ben Claytoncafff782020-03-26 11:18:05 +0000150set(DEFAULT_SWIFTSHADER_LLVM_VERSION "7.0")
151set(SWIFTSHADER_LLVM_VERSION ${DEFAULT_SWIFTSHADER_LLVM_VERSION} CACHE STRING "LLVM version to use")
152set_property(CACHE SWIFTSHADER_LLVM_VERSION PROPERTY STRINGS "7.0" "10.0")
153
Antonio Maiorano062dc182019-12-09 11:52:31 -0500154# If defined, overrides the default optimization level of the current reactor backend.
155# Set to one of the rr::Optimization::Level enum values.
156set(REACTOR_DEFAULT_OPT_LEVEL "Default" CACHE STRING "Reactor default optimization level")
157set_property(CACHE REACTOR_DEFAULT_OPT_LEVEL PROPERTY STRINGS "None" "Less" "Default" "Aggressive")
158
Nicolas Capens18b8d682017-07-25 15:31:45 -0400159# LLVM disallows calling cmake . from the main LLVM dir, the reason is that
160# it builds header files that could overwrite the orignal ones. Here we
161# want to include LLVM as a subdirectory and even though it wouldn't cause
162# the problem, if cmake . is called from the main dir, the condition that
Erwin Jansend46faeb2018-11-19 16:01:37 -0800163# LLVM checkes, "CMAKE_CURRENT_SOURCE_DIR == CMAKE_CURRENT_BINARY_DIR" will be true. So we
Nicolas Capens18b8d682017-07-25 15:31:45 -0400164# disallow it ourselves too to. In addition if there are remining CMakeFiles
165# and CMakeCache in the directory, cmake .. from a subdirectory will still
166# try to build from the main directory so we instruct users to delete these
167# files when they get the error.
Erwin Jansend46faeb2018-11-19 16:01:37 -0800168if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_CURRENT_BINARY_DIR)
Nicolas Capens18b8d682017-07-25 15:31:45 -0400169 message(FATAL_ERROR "In source builds are not allowed by LLVM, please create a build/ directory and build from there. You may have to delete the CMakeCache.txt file and CMakeFiles directory that are next to the CMakeLists.txt.")
170endif()
171
Nicolas Capens1dfcdb02020-03-12 21:12:52 +0000172set_property(GLOBAL PROPERTY USE_FOLDERS TRUE)
Nicolas Capens18b8d682017-07-25 15:31:45 -0400173
174###########################################################
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400175# Directories
176###########################################################
177
Antonio Maiorano8772b422020-04-15 15:00:36 -0400178set(SWIFTSHADER_DIR ${CMAKE_CURRENT_SOURCE_DIR})
179set(SOURCE_DIR ${SWIFTSHADER_DIR}/src)
180set(THIRD_PARTY_DIR ${SWIFTSHADER_DIR}/third_party)
181set(TESTS_DIR ${SWIFTSHADER_DIR}/tests)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400182
183###########################################################
Nicolas Capensfe5861b2018-08-03 16:01:48 -0400184# Initialize submodules
185###########################################################
186
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400187function(InitSubmodule target submodule_dir)
188 if (NOT TARGET ${target})
189 if(NOT EXISTS ${submodule_dir}/.git)
Ben Clayton55890e12020-01-31 14:07:21 +0000190 message(WARNING "
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400191 Target ${target} from submodule ${submodule_dir} missing.
Ben Clayton55890e12020-01-31 14:07:21 +0000192 Running 'git submodule update --init' to download it:
193 ")
Nicolas Capensfe5861b2018-08-03 16:01:48 -0400194
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400195 execute_process(COMMAND git -C ${SWIFTSHADER_DIR} submodule update --init ${submodule_dir})
Ben Clayton55890e12020-01-31 14:07:21 +0000196 endif()
Dan Sinclair6480d4e2019-03-11 10:48:19 -0400197 endif()
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400198endfunction()
199
200if (SWIFTSHADER_BUILD_TESTS)
201 InitSubmodule(gtest ${THIRD_PARTY_DIR}/googletest)
Nicolas Capensfe5861b2018-08-03 16:01:48 -0400202endif()
203
Ben Clayton55890e12020-01-31 14:07:21 +0000204if(SWIFTSHADER_BUILD_BENCHMARKS)
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400205 InitSubmodule(benchmark::benchmark ${THIRD_PARTY_DIR}/benchmark)
206endif()
Ben Clayton55890e12020-01-31 14:07:21 +0000207
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400208if(REACTOR_EMIT_DEBUG_INFO)
209 InitSubmodule(libbacktrace ${THIRD_PARTY_DIR}/libbacktrace/src)
Ben Clayton755467c2019-03-23 11:57:02 +0000210endif()
211
Nicolas Capens13943ba2020-03-17 22:36:24 -0400212if(SWIFTSHADER_GET_PVR)
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400213 InitSubmodule(PVRCore ${THIRD_PARTY_DIR}/PowerVR_Examples)
Nicolas Capens13943ba2020-03-17 22:36:24 -0400214 set(SWIFTSHADER_GET_PVR FALSE CACHE BOOL "Check out the PowerVR submodule" FORCE)
215endif()
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400216if(EXISTS ${THIRD_PARTY_DIR}/PowerVR_Examples/.git)
Nicolas Capens13943ba2020-03-17 22:36:24 -0400217 set(HAVE_PVR_SUBMODULE TRUE)
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500218endif()
219
Antonio Maiorano1e2fba32020-04-15 16:02:58 -0400220if(SWIFTSHADER_BUILD_CPPDAP)
221 InitSubmodule(json ${THIRD_PARTY_DIR}/json)
222 InitSubmodule(cppdap ${THIRD_PARTY_DIR}/cppdap)
223endif()
224
225
Nicolas Capensfe5861b2018-08-03 16:01:48 -0400226###########################################################
Corentin Wallez0866b292015-12-09 13:49:40 -0500227# Convenience macros
228###########################################################
229
230# Recursively calls source_group on the files of the directory
231# so that Visual Studio has the files in a folder tree
232macro(group_all_sources directory)
Antonio Maiorano8772b422020-04-15 15:00:36 -0400233 file(GLOB files RELATIVE ${SWIFTSHADER_DIR}/${directory} ${SWIFTSHADER_DIR}/${directory}/*)
Corentin Wallez0866b292015-12-09 13:49:40 -0500234 foreach(file ${files})
Antonio Maiorano8772b422020-04-15 15:00:36 -0400235 if(IS_DIRECTORY ${SWIFTSHADER_DIR}/${directory}/${file})
Corentin Wallez0866b292015-12-09 13:49:40 -0500236 group_all_sources(${directory}/${file})
237 else()
238 string(REPLACE "/" "\\" groupname ${directory})
Antonio Maiorano8772b422020-04-15 15:00:36 -0400239 source_group(${groupname} FILES ${SWIFTSHADER_DIR}/${directory}/${file})
Corentin Wallez0866b292015-12-09 13:49:40 -0500240 endif()
241 endforeach()
242endmacro()
243
244# Takes target library and a directory where the export map is
245# and add the linker options so that only the API symbols are
246# exported.
Nicolas Capens499bb762018-06-29 13:30:57 -0400247macro(set_shared_library_export_map TARGET DIR)
Corentin Wallez0866b292015-12-09 13:49:40 -0500248 if(MSVC)
Nicolas Capens499bb762018-06-29 13:30:57 -0400249 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " /DEF:\"${DIR}/${TARGET}.def\"")
Ben Clayton8565e772019-06-10 11:58:37 +0100250 elseif(APPLE)
251 # The exported symbols list only exports the API functions and
252 # hides all the others.
253 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS "-exported_symbols_list ${DIR}/${TARGET}.exports")
254 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_DEPENDS "${DIR}/${TARGET}.exports;")
255 # Don't allow undefined symbols, unless it's a Sanitizer build.
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500256 if(NOT SWIFTSHADER_MSAN AND NOT SWIFTSHADER_ASAN AND NOT SWIFTSHADER_TSAN AND NOT SWIFTSHADER_UBSAN)
Ben Clayton8565e772019-06-10 11:58:37 +0100257 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-undefined,error")
258 endif()
David 'Digit' Turnerd3717932019-11-19 17:54:00 +0100259 elseif(LINUX OR FUCHSIA)
Corentin Wallez0866b292015-12-09 13:49:40 -0500260 # The version script only exports the API functions and
Nicolas Capens499bb762018-06-29 13:30:57 -0400261 # hides all the others.
262 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--version-script=${DIR}/${TARGET}.lds")
263 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_DEPENDS "${DIR}/${TARGET}.lds;")
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400264
Nicolas Capense3621dc2020-02-25 22:45:42 -0500265 # -Bsymbolic binds symbol references to their global definitions within
266 # a shared object, thereby preventing symbol preemption.
James Price126720b2020-03-03 10:20:00 -0500267 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-Bsymbolic")
Nicolas Capens517a57f2018-06-29 13:30:57 -0400268
Gordana Cmiljanovic20622c02018-11-05 15:00:11 +0100269 if(ARCH STREQUAL "mipsel" OR ARCH STREQUAL "mips64el")
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200270 # MIPS supports sysv hash-style only.
271 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--hash-style=sysv")
David 'Digit' Turnerd3717932019-11-19 17:54:00 +0100272 elseif(LINUX)
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200273 # Both hash-style are needed, because we want both gold and
274 # GNU ld to be able to read our libraries.
275 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--hash-style=both")
276 endif()
Nicolas Capens499bb762018-06-29 13:30:57 -0400277
Ben Clayton063fc022020-03-23 13:18:09 +0000278 if(NOT ${SWIFTSHADER_EMIT_COVERAGE})
279 # Gc sections is used in combination with each functions being
280 # in its own section, to reduce the binary size.
281 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--gc-sections")
282 endif()
Nicolas Capens499bb762018-06-29 13:30:57 -0400283
284 # Don't allow undefined symbols, unless it's a Sanitizer build.
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500285 if(NOT SWIFTSHADER_MSAN AND NOT SWIFTSHADER_ASAN AND NOT SWIFTSHADER_TSAN AND NOT SWIFTSHADER_UBSAN)
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400286 set_property(TARGET ${TARGET} APPEND_STRING PROPERTY LINK_FLAGS " -Wl,--no-undefined")
287 endif()
Corentin Wallez0866b292015-12-09 13:49:40 -0500288 endif()
289endmacro()
290
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500291if(SWIFTSHADER_USE_GROUP_SOURCES)
Corentin Wallez0866b292015-12-09 13:49:40 -0500292 group_all_sources(src)
293endif()
294
295###########################################################
Corentin Wallez0866b292015-12-09 13:49:40 -0500296# Compile flags
297###########################################################
298
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100299# Flags for project code (non 3rd party)
300set(SWIFTSHADER_COMPILE_OPTIONS "")
Ben Clayton063fc022020-03-23 13:18:09 +0000301set(SWIFTSHADER_LINK_FLAGS "")
302set(SWIFTSHADER_LIBS "")
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100303
Nicolas Capens6f422092015-12-23 15:12:45 -0500304macro(set_cpp_flag FLAG)
305 if(${ARGC} GREATER 1)
306 set(CMAKE_CXX_FLAGS_${ARGV1} "${CMAKE_CXX_FLAGS_${ARGV1}} ${FLAG}")
Corentin Wallez0866b292015-12-09 13:49:40 -0500307 else()
Nicolas Capens6f422092015-12-23 15:12:45 -0500308 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAG}")
Corentin Wallez0866b292015-12-09 13:49:40 -0500309 endif()
310endmacro()
311
Ben Clayton48c8a182019-05-21 20:00:20 +0100312macro(set_linker_flag FLAG)
313 if(${ARGC} GREATER 1)
314 set(CMAKE_EXE_LINKER_FLAGS ${ARGV1} "${CMAKE_EXE_LINKER_FLAGS ${ARGV1}} ${FLAG}")
315 else()
316 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAG}")
317 endif()
318endmacro()
319
Corentin Wallez0866b292015-12-09 13:49:40 -0500320if(MSVC)
321 set_cpp_flag("/MP")
322 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400323 add_definitions(-D_SCL_SECURE_NO_WARNINGS)
Nicolas Capens4c9f04b2019-01-31 22:09:03 -0500324 add_definitions(-D_SBCS) # Single Byte Character Set (ASCII)
Ben Clayton30b6b592019-08-07 15:04:11 +0100325 add_definitions(-D_ENABLE_EXTENDED_ALIGNED_STORAGE) # Disable MSVC warnings about std::aligned_storage being broken before VS 2017 15.8
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400326
Nicolas Capensf554c542020-01-09 17:19:35 +0000327 set_cpp_flag("/DEBUG:FASTLINK" DEBUG)
328 set_cpp_flag("/DEBUG:FASTLINK" RELWITHDEBINFO)
329
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400330 # Disable specific warnings
331 # TODO: Not all of these should be disabled, but for now, we want a warning-free msvc build. Remove these one by one
332 # and fix the actual warnings in code.
333 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
334 "/wd4005" # 'identifier' : macro redefinition
335 "/wd4018" # 'expression' : signed/unsigned mismatch
Ben Clayton4d4a1902019-05-15 11:15:42 +0100336 "/wd4065" # switch statement contains 'default' but no 'case' labels
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400337 "/wd4141" # 'modifier' : used more than once
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400338 "/wd4244" # 'conversion' conversion from 'type1' to 'type2', possible loss of data
339 "/wd4267" # 'var' : conversion from 'size_t' to 'type', possible loss of data
340 "/wd4291" # 'void X new(size_t,unsigned int,unsigned int)': no matching operator delete found; memory will not be freed if initialization throws an exception
341 "/wd4309" # 'conversion' : truncation of constant value
342 "/wd4624" # 'derived class' : destructor was implicitly defined as deleted because a base class destructor is inaccessible or deleted
343 "/wd4800" # 'type' : forcing value to bool 'true' or 'false' (performance warning)
344 "/wd4838" # conversion from 'type_1' to 'type_2' requires a narrowing conversion
345 "/wd5030" # attribute 'attribute' is not recognized
346 "/wd5038" # data member 'member1' will be initialized after data member 'member2' data member 'member' will be initialized after base class 'base_class'
347 )
348
349 # Treat specific warnings as errors
350 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
351 "/we4018" # 'expression' : signed/unsigned mismatch
Antonio Maiorano23da0732019-05-14 22:32:16 -0400352 "/we4471" # 'enumeration': a forward declaration of an unscoped enumeration must have an underlying type (int assumed)
Antonio Maiorano5bce1f42019-05-10 16:03:49 -0400353 "/we4838" # conversion from 'type_1' to 'type_2' requires a narrowing conversion
354 "/we5038" # data member 'member1' will be initialized after data member 'member2' data member 'member' will be initialized after base class 'base_class'
355 )
Corentin Wallez0866b292015-12-09 13:49:40 -0500356else()
Ben Claytona5f07632020-02-04 11:43:25 +0000357 # Explicitly enable these warnings.
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100358 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100359 "-Wall"
Ben Clayton8a983f72019-06-18 17:56:36 +0100360 "-Wreorder"
361 "-Wsign-compare"
362 "-Wmissing-braces"
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100363 )
Corentin Wallez0866b292015-12-09 13:49:40 -0500364
Ben Clayton5e828762019-04-24 19:16:52 +0100365 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100366 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
Ben Clayton8a983f72019-06-18 17:56:36 +0100367 "-Wunused-lambda-capture"
368 "-Wstring-conversion"
369 "-Wextra-semi"
370 "-Wignored-qualifiers"
Ben Claytona5f07632020-02-04 11:43:25 +0000371 )
372 endif()
373
Ben Clayton063fc022020-03-23 13:18:09 +0000374 if (SWIFTSHADER_EMIT_COVERAGE)
375 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
376 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "--coverage")
377 list(APPEND SWIFTSHADER_LIBS "gcov")
378 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
379 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-fprofile-instr-generate" "-fcoverage-mapping")
380 list(APPEND SWIFTSHADER_LINK_FLAGS "-fprofile-instr-generate" "-fcoverage-mapping")
381 else()
382 message(FATAL_ERROR "Coverage generation not supported for the ${CMAKE_CXX_COMPILER_ID} toolchain")
383 endif()
384 endif()
385
Ben Claytona5f07632020-02-04 11:43:25 +0000386 # Disable pedanitc warnings
387 if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
388 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
389 "-Wno-ignored-attributes" # ignoring attributes on template argument 'X'
390 "-Wno-attributes" # 'X' attribute ignored
391 "-Wno-strict-aliasing" # dereferencing type-punned pointer will break strict-aliasing rules
392 "-Wno-comment" # multi-line comment
393 )
394 if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 9)
395 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
396 "-Wno-init-list-lifetime" # assignment from temporary initializer_list does not extend the lifetime of the underlying array
397 )
398 endif()
399 elseif(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
400 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
401 "-Wno-unneeded-internal-declaration" # function 'X' is not needed and will not be emitted
402 "-Wno-unused-private-field" # private field 'offset' is not used - TODO: Consider enabling this once Vulkan is further implemented.
403 "-Wno-comment" # multi-line comment
404 "-Wno-undefined-var-template" # instantiation of variable 'X' required here, but no definition is available
Ben Claytona7bc2b92020-03-26 11:24:49 +0000405 "-Wno-extra-semi" # extra ';' after member function definition
Ben Claytona5f07632020-02-04 11:43:25 +0000406
Nicolas Capens67180a02019-06-17 15:27:03 -0400407 # Silence errors caused by unknown warnings when building with older
408 # versions of Clang. This demands checking that warnings added above
409 # are spelled correctly and work as intended!
410 "-Wno-unknown-warning-option"
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100411 )
Nicolas Capens825d3442018-11-06 23:50:05 -0500412 endif()
413
Corentin Wallez0866b292015-12-09 13:49:40 -0500414 # Remove xor, and, or and friends from the list of keywords, they are used
415 # by Reactor
Ben Clayton4ceb77d2019-04-24 12:09:59 +0100416 list(APPEND SWIFTSHADER_COMPILE_OPTIONS
417 "-fno-operator-names"
418 )
Corentin Wallez0866b292015-12-09 13:49:40 -0500419
420 # LLVM headers requires these flags to be set
421 set_cpp_flag("-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS")
422
Nicolas Capens499bb762018-06-29 13:30:57 -0400423 if(ARCH STREQUAL "x86")
Corentin Wallez0866b292015-12-09 13:49:40 -0500424 set_cpp_flag("-m32")
425 set_cpp_flag("-msse2")
Nicolas Capens0424edc2018-01-03 14:06:30 -0500426 set_cpp_flag("-mfpmath=sse")
427 set_cpp_flag("-march=pentium4")
428 set_cpp_flag("-mtune=generic")
Corentin Wallez0866b292015-12-09 13:49:40 -0500429 endif()
Nicolas Capens499bb762018-06-29 13:30:57 -0400430 if(ARCH STREQUAL "x86_64")
Corentin Wallez0866b292015-12-09 13:49:40 -0500431 set_cpp_flag("-m64")
432 set_cpp_flag("-fPIC")
Nicolas Capens0424edc2018-01-03 14:06:30 -0500433 set_cpp_flag("-march=x86-64")
434 set_cpp_flag("-mtune=generic")
Corentin Wallez0866b292015-12-09 13:49:40 -0500435 endif()
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200436 if(ARCH STREQUAL "mipsel")
Jiaxun Yang55275c32020-02-09 14:52:42 +0800437 set_cpp_flag("-EL")
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200438 set_cpp_flag("-march=mips32r2")
439 set_cpp_flag("-fPIC")
440 set_cpp_flag("-mhard-float")
441 set_cpp_flag("-mfp32")
Jiaxun Yang55275c32020-02-09 14:52:42 +0800442 set_cpp_flag("-mxgot")
Gordana Cmiljanovic082dfec2018-10-19 11:36:15 +0200443 endif()
Gordana Cmiljanovic20622c02018-11-05 15:00:11 +0100444 if(ARCH STREQUAL "mips64el")
Jiaxun Yang55275c32020-02-09 14:52:42 +0800445 set_cpp_flag("-EL")
Gordana Cmiljanovic20622c02018-11-05 15:00:11 +0100446 set_cpp_flag("-march=mips64r2")
447 set_cpp_flag("-mabi=64")
448 set_cpp_flag("-fPIC")
Jiaxun Yang55275c32020-02-09 14:52:42 +0800449 set_cpp_flag("-mxgot")
Gordana Cmiljanovic20622c02018-11-05 15:00:11 +0100450 endif()
Nicolas Capens499bb762018-06-29 13:30:57 -0400451
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500452 if(SWIFTSHADER_LESS_DEBUG_INFO)
Paul Thomson09b50792019-10-17 12:55:56 +0100453 # Use -g1 to be able to get stack traces
454 set_cpp_flag("-g -g1" DEBUG)
455 set_cpp_flag("-g -g1" RELWITHDEBINFO)
456 else()
457 # Use -g3 to have even more debug info
458 set_cpp_flag("-g -g3" DEBUG)
459 set_cpp_flag("-g -g3" RELWITHDEBINFO)
460 endif()
461
Ben Clayton09a91e42019-02-05 17:58:38 +0000462 if(NOT CMAKE_CXX_COMPILER_ID MATCHES "Clang")
463 # Treated as an unused argument with clang
464 set_cpp_flag("-s" RELEASE)
465 endif()
Corentin Wallez0866b292015-12-09 13:49:40 -0500466
467 # For distribution it is more important to be slim than super optimized
Alexis Hetu2c0546d2017-05-24 11:16:26 -0400468 set_cpp_flag("-Os" RELEASE)
469 set_cpp_flag("-Os" RELWITHDEBINFO)
Corentin Wallez0866b292015-12-09 13:49:40 -0500470
471 set_cpp_flag("-DNDEBUG" RELEASE)
472 set_cpp_flag("-DNDEBUG" RELWITHDEBINFO)
473 set_cpp_flag("-DANGLE_DISABLE_TRACE" RELEASE)
474 set_cpp_flag("-DANGLE_DISABLE_TRACE" RELWITHDEBINFO)
475
476 # Put each variable and function in its own section so that when linking
477 # with -gc-sections unused functions and variables are removed.
478 set_cpp_flag("-ffunction-sections" RELEASE)
479 set_cpp_flag("-fdata-sections" RELEASE)
480 set_cpp_flag("-fomit-frame-pointer" RELEASE)
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400481
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500482 if(SWIFTSHADER_MSAN)
Ben Claytondae97922019-05-17 12:09:31 +0100483 set_cpp_flag("-fsanitize=memory")
Ben Clayton48c8a182019-05-21 20:00:20 +0100484 set_linker_flag("-fsanitize=memory")
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500485 elseif(SWIFTSHADER_ASAN)
Ben Claytondae97922019-05-17 12:09:31 +0100486 set_cpp_flag("-fsanitize=address")
Ben Clayton48c8a182019-05-21 20:00:20 +0100487 set_linker_flag("-fsanitize=address")
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500488 elseif(SWIFTSHADER_TSAN)
Ben Claytondae97922019-05-17 12:09:31 +0100489 set_cpp_flag("-fsanitize=thread")
Ben Clayton48c8a182019-05-21 20:00:20 +0100490 set_linker_flag("-fsanitize=thread")
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500491 elseif(SWIFTSHADER_UBSAN)
Ben Claytondae97922019-05-17 12:09:31 +0100492 set_cpp_flag("-fsanitize=undefined")
Ben Clayton48c8a182019-05-21 20:00:20 +0100493 set_linker_flag("-fsanitize=undefined")
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400494 endif()
Corentin Wallez0866b292015-12-09 13:49:40 -0500495endif()
496
Antonio Maiorano4b8b0782020-03-23 14:11:01 -0400497if(SWIFTSHADER_DCHECK_ALWAYS_ON)
498 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DDCHECK_ALWAYS_ON")
499endif()
500
Nicolas Capens8c13b2f2020-03-06 01:12:01 -0500501if(SWIFTSHADER_WARNINGS_AS_ERRORS)
502 if(MSVC)
503 set(WARNINGS_AS_ERRORS "/WX") # Treat all warnings as errors
504 else()
505 set(WARNINGS_AS_ERRORS "-Werror") # Treat all warnings as errors
506 endif()
507endif()
508
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400509if(REACTOR_EMIT_PRINT_LOCATION)
Antonio Maiorano415d1812020-02-11 16:22:55 -0500510 # This feature depends on REACTOR_EMIT_DEBUG_INFO and REACTOR_ENABLE_PRINT
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400511 set(REACTOR_EMIT_DEBUG_INFO "On")
Antonio Maiorano415d1812020-02-11 16:22:55 -0500512 set(REACTOR_ENABLE_PRINT "On")
Antonio Maioranof448d8e2019-04-26 16:19:16 -0400513 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_RR_EMIT_PRINT_LOCATION")
514endif()
515
516if(REACTOR_EMIT_DEBUG_INFO)
517 message(WARNING "REACTOR_EMIT_DEBUG_INFO is enabled. This will likely affect performance.")
518 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_RR_DEBUG_INFO")
519endif()
520
Antonio Maiorano415d1812020-02-11 16:22:55 -0500521if(REACTOR_ENABLE_PRINT)
522 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_RR_PRINT")
523endif()
524
Ben Clayton5375f472019-06-24 13:33:11 +0100525if(REACTOR_VERIFY_LLVM_IR)
526 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DENABLE_RR_LLVM_IR_VERIFICATION")
527endif()
528
Antonio Maiorano062dc182019-12-09 11:52:31 -0500529if(REACTOR_DEFAULT_OPT_LEVEL)
530 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-DREACTOR_DEFAULT_OPT_LEVEL=${REACTOR_DEFAULT_OPT_LEVEL}")
531endif()
532
Nicolas Capensbf8fd5b2018-06-21 00:42:00 -0400533if(WIN32)
Corentin Wallez0866b292015-12-09 13:49:40 -0500534 add_definitions(-DWINVER=0x501 -DNOMINMAX -DSTRICT)
Nicolas Capens6f422092015-12-23 15:12:45 -0500535 set(CMAKE_FIND_LIBRARY_PREFIXES ${CMAKE_FIND_LIBRARY_PREFIXES} "" "lib")
Corentin Wallez0866b292015-12-09 13:49:40 -0500536endif()
537
Antonio Maiorano61022762020-03-30 11:11:16 -0400538set(USE_EXCEPTIONS
539 ${REACTOR_EMIT_DEBUG_INFO} # boost::stacktrace uses exceptions
540)
541if(NOT MSVC)
542 if (${USE_EXCEPTIONS})
543 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-fexceptions")
544 else()
545 list(APPEND SWIFTSHADER_COMPILE_OPTIONS "-fno-exceptions")
546 endif()
547endif()
Antonio Maiorano9418b512020-04-08 23:18:13 -0400548unset(USE_EXCEPTIONS)
Antonio Maiorano61022762020-03-30 11:11:16 -0400549
Ben Clayton063fc022020-03-23 13:18:09 +0000550# Transform SWIFTSHADER_LINK_FLAGS from semicolon delimited to whitespace
551# delimited (what is expected by LINK_FLAGS)
552string(REPLACE ";" " " SWIFTSHADER_LINK_FLAGS "${SWIFTSHADER_LINK_FLAGS}")
553
Corentin Wallez0866b292015-12-09 13:49:40 -0500554###########################################################
Antonio Maioranofa8f48d2020-03-30 16:41:48 -0400555# libbacktrace and boost
556###########################################################
557if(REACTOR_EMIT_DEBUG_INFO)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400558 add_subdirectory(${THIRD_PARTY_DIR}/libbacktrace EXCLUDE_FROM_ALL)
559 add_subdirectory(${THIRD_PARTY_DIR}/boost EXCLUDE_FROM_ALL)
Antonio Maioranofa8f48d2020-03-30 16:41:48 -0400560endif()
561
562###########################################################
Corentin Wallez0866b292015-12-09 13:49:40 -0500563# LLVM
564###########################################################
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400565add_subdirectory(${THIRD_PARTY_DIR}/llvm-${SWIFTSHADER_LLVM_VERSION} EXCLUDE_FROM_ALL)
Ben Clayton8f71f732019-02-01 09:38:45 +0000566
Antonio Maiorano4bde1c32020-03-27 15:01:53 -0400567###########################################################
568# Subzero
569###########################################################
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400570add_subdirectory(${THIRD_PARTY_DIR}/llvm-subzero EXCLUDE_FROM_ALL)
571add_subdirectory(${THIRD_PARTY_DIR}/subzero EXCLUDE_FROM_ALL)
Antonio Maiorano8bce0672020-02-28 13:13:45 -0500572
573###########################################################
574# marl
575###########################################################
576if(BUILD_MARL)
577 set(MARL_THIRD_PARTY_DIR ${THIRD_PARTY_DIR})
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400578 add_subdirectory(${THIRD_PARTY_DIR}/marl)
Antonio Maiorano8bce0672020-02-28 13:13:45 -0500579endif()
580
Antonio Maiorano8bce0672020-02-28 13:13:45 -0500581###########################################################
582# cppdap
583###########################################################
584if(SWIFTSHADER_BUILD_CPPDAP)
585 set(CPPDAP_THIRD_PARTY_DIR ${THIRD_PARTY_DIR})
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400586 add_subdirectory(${THIRD_PARTY_DIR}/cppdap)
Antonio Maiorano8bce0672020-02-28 13:13:45 -0500587endif()
588
Antonio Maioranob02a7082020-03-30 21:55:20 -0400589###########################################################
590# astc-encoder
591###########################################################
592if(SWIFTSHADER_ENABLE_ASTC)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400593 add_subdirectory(${THIRD_PARTY_DIR}/astc-encoder)
Antonio Maioranob02a7082020-03-30 21:55:20 -0400594endif()
Nicolas Capens19291ef2017-01-09 13:35:14 -0500595
Nicolas Capensf53adbd2017-01-06 12:47:46 -0500596###########################################################
Antonio Maiorano8f02f582020-03-31 11:01:43 -0400597# gtest and gmock
598###########################################################
599if(SWIFTSHADER_BUILD_TESTS)
600 # For Win32, force gtest to match our CRT (shared)
601 set(gtest_force_shared_crt TRUE CACHE BOOL "" FORCE)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400602 add_subdirectory(${THIRD_PARTY_DIR}/googletest EXCLUDE_FROM_ALL)
Antonio Maiorano8f02f582020-03-31 11:01:43 -0400603 # gtest finds python, which picks python 2 first, if present.
604 # We need to undo this so that SPIR-V can later find python3.
605 unset(PYTHON_EXECUTABLE CACHE)
606endif()
607
608###########################################################
Corentin Wallez0866b292015-12-09 13:49:40 -0500609# File Lists
610###########################################################
611
Corentin Wallez0866b292015-12-09 13:49:40 -0500612###########################################################
613# Append OS specific files to lists
614###########################################################
615
616if(WIN32)
Corentin Wallez0866b292015-12-09 13:49:40 -0500617 set(OS_LIBS odbc32 odbccp32 WS2_32 dxguid)
618elseif(LINUX)
Nicolas Capens681d97b2016-05-17 16:02:32 -0400619 set(OS_LIBS dl pthread)
David 'Digit' Turnerd3717932019-11-19 17:54:00 +0100620elseif(FUCHSIA)
621 set(OS_LIBS zircon)
Corentin Wallezcd0a4572015-12-10 15:59:28 -0500622elseif(APPLE)
623 find_library(COCOA_FRAMEWORK Cocoa)
624 find_library(QUARTZ_FRAMEWORK Quartz)
Alexis Hetud23cf632018-04-10 10:48:42 -0400625 find_library(CORE_FOUNDATION_FRAMEWORK CoreFoundation)
626 find_library(IOSURFACE_FRAMEWORK IOSurface)
Corentin Wallezcb586622020-03-27 17:38:29 +0100627 find_library(METAL_FRAMEWORK Metal)
628 set(OS_LIBS "${COCOA_FRAMEWORK}" "${QUARTZ_FRAMEWORK}" "${CORE_FOUNDATION_FRAMEWORK}" "${IOSURFACE_FRAMEWORK}" "${METAL_FRAMEWORK}")
Corentin Wallez0866b292015-12-09 13:49:40 -0500629endif()
630
631###########################################################
Nicolas Capens5a105bc2015-12-22 22:04:28 -0500632# SwiftShader Targets
Corentin Wallez0866b292015-12-09 13:49:40 -0500633###########################################################
634
Antonio Maioranofa8f48d2020-03-30 16:41:48 -0400635add_subdirectory(src/Reactor) # Add ReactorSubzero and ReactorLLVM targets
Nicolas Capense329f012020-03-13 14:54:21 +0000636
Ben Claytonb99bc1f2019-04-15 13:56:08 -0400637if(${REACTOR_BACKEND} STREQUAL "LLVM")
Nicolas Capensf53adbd2017-01-06 12:47:46 -0500638 set(Reactor ReactorLLVM)
639elseif(${REACTOR_BACKEND} STREQUAL "Subzero")
640 set(Reactor ReactorSubzero)
641else()
642 message(FATAL_ERROR "REACTOR_BACKEND must be 'LLVM' or 'Subzero'")
643endif()
Corentin Wallez0866b292015-12-09 13:49:40 -0500644
Antonio Maiorano4ce6a882020-04-06 16:16:21 -0400645add_subdirectory(src/Common) # Add gl_common target
646add_subdirectory(src/Main) # Add gl_main target
647add_subdirectory(src/Shader) # Add gl_shader target
648add_subdirectory(src/Renderer) # Add gl_renderer target
649
650# Combine all gl_* targets into an interface target
651# along with other dependencies
652add_library(gl_swiftshader_core INTERFACE)
653target_link_libraries(gl_swiftshader_core INTERFACE
654 # Transitively depends on shader, main, core, Reactor,
655 # OS_LIBS and SWIFTSHADER_LIBS
656 gl_renderer
657)
658
Antonio Maioranoa71aff22020-04-07 10:09:41 -0400659add_subdirectory(src/OpenGL/common EXCLUDE_FROM_ALL) # Add libGLESCommon target
660add_subdirectory(src/OpenGL/compiler EXCLUDE_FROM_ALL) # Add GLCompiler target
Nicolas Capens6f422092015-12-23 15:12:45 -0500661
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500662if(SWIFTSHADER_BUILD_EGL)
Antonio Maioranoa71aff22020-04-07 10:09:41 -0400663 add_subdirectory(src/OpenGL/libEGL) # Add libEGL target
Corentin Wallez0866b292015-12-09 13:49:40 -0500664endif()
665
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500666if(SWIFTSHADER_BUILD_GLESv2)
Antonio Maioranoa71aff22020-04-07 10:09:41 -0400667 add_subdirectory(src/OpenGL/libGLESv2) # Add libGLESv2 target
Corentin Wallez0866b292015-12-09 13:49:40 -0500668endif()
669
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500670if(SWIFTSHADER_BUILD_GLES_CM)
Antonio Maioranoa71aff22020-04-07 10:09:41 -0400671 add_subdirectory(src/OpenGL/libGLES_CM) # Add libGLES_CM target
Ben Clayton1e8486b2020-01-22 17:01:52 +0000672endif(SWIFTSHADER_BUILD_GLES_CM)
Corentin Wallez0866b292015-12-09 13:49:40 -0500673
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500674if(SWIFTSHADER_BUILD_VULKAN)
Dan Sinclair6480d4e2019-03-11 10:48:19 -0400675 if (NOT TARGET SPIRV-Tools)
676 # This variable is also used by SPIRV-Tools to locate SPIRV-Headers
Ben Claytonafb4ebd2019-12-02 19:33:17 +0000677 set(SPIRV-Headers_SOURCE_DIR "${THIRD_PARTY_DIR}/SPIRV-Headers")
Antonio Maiorano8f02f582020-03-31 11:01:43 -0400678 set(SPIRV_SKIP_TESTS TRUE CACHE BOOL "" FORCE)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400679 add_subdirectory(${THIRD_PARTY_DIR}/SPIRV-Tools) # Add SPIRV-Tools target
Ben Clayton1e8486b2020-01-22 17:01:52 +0000680 endif()
Nicolas Capens4c9f04b2019-01-31 22:09:03 -0500681
Antonio Maiorano9418b512020-04-08 23:18:13 -0400682 # Add a vk_base interface library for shared vulkan build options.
683 # TODO: Create src/Base and make this a lib target, and move stuff from
684 # src/Vulkan into it that is needed by vk_pipeline, vk_device, and vk_wsi.
685 add_library(vk_base INTERFACE)
Ben Clayton4cdbb542020-04-14 22:51:50 +0100686
Antonio Maiorano9418b512020-04-08 23:18:13 -0400687 if(SWIFTSHADER_ENABLE_VULKAN_DEBUGGER)
688 target_compile_definitions(vk_base INTERFACE "ENABLE_VK_DEBUGGER")
David 'Digit' Turnerd3717932019-11-19 17:54:00 +0100689 endif()
Antonio Maiorano9418b512020-04-08 23:18:13 -0400690
Nicolas Capensd3545372019-08-09 13:59:18 -0400691 if(WIN32)
Antonio Maiorano9418b512020-04-08 23:18:13 -0400692 target_compile_definitions(vk_base INTERFACE "VK_USE_PLATFORM_WIN32_KHR")
Nicolas Capensd3545372019-08-09 13:59:18 -0400693 elseif(LINUX)
Ben Claytona9af8832019-08-14 13:09:43 +0100694 if(X11)
Antonio Maiorano9418b512020-04-08 23:18:13 -0400695 target_compile_definitions(vk_base INTERFACE "VK_USE_PLATFORM_XLIB_KHR")
Ben Clayton1e8486b2020-01-22 17:01:52 +0000696 endif()
Ben Claytona9af8832019-08-14 13:09:43 +0100697 if(XCB)
Antonio Maiorano9418b512020-04-08 23:18:13 -0400698 target_compile_definitions(vk_base INTERFACE "VK_USE_PLATFORM_XCB_KHR")
Ben Clayton1e8486b2020-01-22 17:01:52 +0000699 endif()
Nicolas Capensd3545372019-08-09 13:59:18 -0400700 elseif(APPLE)
Antonio Maiorano9418b512020-04-08 23:18:13 -0400701 target_compile_definitions(vk_base INTERFACE "VK_USE_PLATFORM_MACOS_MVK")
David 'Digit' Turnerd3717932019-11-19 17:54:00 +0100702 elseif(FUCHSIA)
Antonio Maiorano9418b512020-04-08 23:18:13 -0400703 target_compile_definitions(vk_base INTERFACE "VK_USE_PLATFORM_FUCHSIA")
Nicolas Capens29a98092019-04-03 14:35:10 -0400704 else()
705 message(FATAL_ERROR "Platform does not support Vulkan yet")
Nicolas Capensd3545372019-08-09 13:59:18 -0400706 endif()
707
Antonio Maiorano9418b512020-04-08 23:18:13 -0400708 add_subdirectory(src/System) # Add vk_system target
709 add_subdirectory(src/Pipeline) # Add vk_pipeline target
710 add_subdirectory(src/WSI) # Add vk_wsi target
711 add_subdirectory(src/Device) # Add vk_device target
712 add_subdirectory(src/Vulkan) # Add vk_swiftshader target
Ben Claytonac736122020-03-24 17:48:31 +0000713
714 if(SWIFTSHADER_EMIT_COVERAGE)
Antonio Maiorano2430d662020-04-14 17:04:36 -0400715 add_subdirectory(${TESTS_DIR}/regres/cov/turbo-cov)
Ben Claytonac736122020-03-24 17:48:31 +0000716 endif()
717
Nicolas Capens29a98092019-04-03 14:35:10 -0400718endif()
Chris Forbes3d27f2e2018-09-26 09:24:39 -0700719
Corentin Wallez0866b292015-12-09 13:49:40 -0500720###########################################################
Nicolas Capens29a98092019-04-03 14:35:10 -0400721# Sample programs and tests
Corentin Wallez0866b292015-12-09 13:49:40 -0500722###########################################################
723
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500724if(SWIFTSHADER_BUILD_TESTS)
Antonio Maiorano4210ea12020-04-14 15:44:14 -0400725 add_subdirectory(${TESTS_DIR}/ReactorUnitTests) # Add ReactorUnitTests target
726 add_subdirectory(${TESTS_DIR}/GLESUnitTests) # Add gles-unittests target
727 add_subdirectory(${TESTS_DIR}/MathUnitTests) # Add math-unittests target
728 add_subdirectory(${TESTS_DIR}/SystemUnitTests) # Add system-unittests target
729endif()
Nicolas Capens5c09b6a2019-08-07 11:13:03 -0400730
Ben Clayton55890e12020-01-31 14:07:21 +0000731if(SWIFTSHADER_BUILD_BENCHMARKS)
732 if (NOT TARGET benchmark::benchmark)
733 set(BENCHMARK_ENABLE_TESTING FALSE CACHE BOOL FALSE FORCE)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400734 add_subdirectory(${THIRD_PARTY_DIR}/benchmark)
Ben Clayton55890e12020-01-31 14:07:21 +0000735 endif()
736
Antonio Maiorano4210ea12020-04-14 15:44:14 -0400737 add_subdirectory(${TESTS_DIR}/ReactorBenchmarks) # Add ReactorBenchmarks target
738 add_subdirectory(${TESTS_DIR}/SystemBenchmarks) # Add system-benchmarks target
739endif()
Ben Clayton55890e12020-01-31 14:07:21 +0000740
Sean Risserf6d3cbb2020-01-08 14:44:53 -0500741if(SWIFTSHADER_BUILD_TESTS AND SWIFTSHADER_BUILD_VULKAN)
Antonio Maiorano4210ea12020-04-14 15:44:14 -0400742 add_subdirectory(${TESTS_DIR}/VulkanUnitTests) # Add VulkanUnitTests target
743endif()
Nicolas Capens51b28002020-01-30 16:41:00 -0500744
Nicolas Capens13943ba2020-03-17 22:36:24 -0400745if(HAVE_PVR_SUBMODULE AND SWIFTSHADER_BUILD_PVR)
Nicolas Capens51b28002020-01-30 16:41:00 -0500746 if(UNIX AND NOT APPLE)
747 set(PVR_WINDOW_SYSTEM XCB)
Nicolas Capens7e857092020-03-06 13:21:10 -0500748
749 # Set the RPATH of the next defined build targets to $ORIGIN,
750 # allowing them to load shared libraries from the execution directory.
751 set(CMAKE_BUILD_RPATH "$ORIGIN")
Nicolas Capens51b28002020-01-30 16:41:00 -0500752 endif()
753
Nicolas Capens13943ba2020-03-17 22:36:24 -0400754 set(PVR_BUILD_EXAMPLES TRUE CACHE BOOL "Build the PowerVR SDK Examples" FORCE)
Antonio Maioranoda4315d2020-04-15 13:49:56 -0400755 add_subdirectory(${THIRD_PARTY_DIR}/PowerVR_Examples)
Nicolas Capens51b28002020-01-30 16:41:00 -0500756
Nicolas Capens7e857092020-03-06 13:21:10 -0500757 if(NOT APPLE)
Nicolas Capens51b28002020-01-30 16:41:00 -0500758 # Copy the 'loader' library to the bin/ directory
759 # where the PowerVR executables are placed.
Ben Clayton491555c2020-04-09 12:06:19 +0100760 if(SWIFTSHADER_BUILD_VULKAN)
761 add_custom_command(
762 TARGET vk_swiftshader
763 POST_BUILD
764 COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/bin
765 COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:vk_swiftshader> ${CMAKE_BINARY_DIR}/bin/${VULKAN_API_LIBRARY_NAME}
766 )
767 endif()
Nicolas Capens51b28002020-01-30 16:41:00 -0500768 endif()
769
770 # Samples known to work well
771 set(PVR_VULKAN_TARGET_GOOD
772 VulkanBumpmap
Nicolas Capens3702e012020-03-30 09:08:47 -0400773 VulkanExampleUI
774 VulkanGaussianBlur
Nicolas Capens51b28002020-01-30 16:41:00 -0500775 VulkanGlass
776 VulkanGnomeHorde
777 VulkanHelloAPI
778 VulkanImageBasedLighting
Nicolas Capens3702e012020-03-30 09:08:47 -0400779 VulkanIntroducingPVRUtils
Nicolas Capens51b28002020-01-30 16:41:00 -0500780 VulkanMultiSampling
Nicolas Capens3702e012020-03-30 09:08:47 -0400781 VulkanNavigation2D
782 VulkanParticleSystem
Nicolas Capens51b28002020-01-30 16:41:00 -0500783 VulkanSkinning
784 )
785
Nicolas Capens3702e012020-03-30 09:08:47 -0400786 set(PVR_GLES_TARGET_GOOD
787 OpenGLESHelloAPI
788 OpenGLESIntroducingPVRUtils
789 OpenGLESNavigation2D
790 )
791
Nicolas Capens51b28002020-01-30 16:41:00 -0500792 set(PVR_VULKAN_TARGET_OTHER
793 VulkanDeferredShading
794 VulkanDeferredShadingPFX
Nicolas Capens51b28002020-01-30 16:41:00 -0500795 VulkanGameOfLife
Nicolas Capens51b28002020-01-30 16:41:00 -0500796 VulkanIBLMapsGenerator
797 VulkanIMGTextureFilterCubic
798 VulkanIntroducingPVRShell
Nicolas Capens51b28002020-01-30 16:41:00 -0500799 VulkanIntroducingPVRVk
800 VulkanIntroducingUIRenderer
801 VulkanMultithreading
Nicolas Capens51b28002020-01-30 16:41:00 -0500802 VulkanNavigation3D
Nicolas Capens51b28002020-01-30 16:41:00 -0500803 VulkanPostProcessing
804 VulkanPVRScopeExample
805 VulkanPVRScopeRemote
806 )
807
Nicolas Capens3702e012020-03-30 09:08:47 -0400808 set(PVR_GLES_TARGET_OTHER
809 OpenGLESDeferredShading
810 OpenGLESGaussianBlur
811 OpenGLESImageBasedLighting
812 OpenGLESIMGFramebufferDownsample
813 OpenGLESIMGTextureFilterCubic
814 OpenGLESIntroducingPVRCamera
815 OpenGLESIntroducingPVRShell
816 OpenGLESIntroducingUIRenderer
817 OpenGLESMultiviewVR
818 OpenGLESNavigation3D
819 OpenGLESOpenCLExample
820 OpenGLESParticleSystem
821 OpenGLESPostProcessing
822 OpenGLESPVRScopeExample
823 OpenGLESPVRScopeRemote
824 OpenGLESSkinning
825 )
826
Nicolas Capens51b28002020-01-30 16:41:00 -0500827 set(PVR_TARGET_OTHER
828 glslang
829 glslangValidator
830 glslang-default-resource-limits
831 OGLCompiler
832 OSDependent
833 OpenCLMatrixMultiplication
Nicolas Capens51b28002020-01-30 16:41:00 -0500834 pugixml
835 PVRAssets
836 PVRCamera
837 PVRCore
838 PVRPfx
839 PVRShell
840 PVRUtilsGles
841 PVRUtilsVk
842 PVRVk
843 SPIRV
844 spirv-remap
845 SPVRemapper
846 uninstall
847 )
848
849 set(PVR_VULKAN_TARGET
850 ${PVR_VULKAN_TARGET_GOOD}
851 ${PVR_VULKAN_TARGET_OTHER}
852 )
853
Nicolas Capens3702e012020-03-30 09:08:47 -0400854 set(PVR_GLES_TARGET
855 ${PVR_GLES_TARGET_GOOD}
856 ${PVR_GLES_TARGET_OTHER}
857 )
858
Nicolas Capens51b28002020-01-30 16:41:00 -0500859 foreach(pvr_target ${PVR_VULKAN_TARGET})
860 add_dependencies(${pvr_target} vk_swiftshader)
861 endforeach()
862
Nicolas Capens3702e012020-03-30 09:08:47 -0400863 foreach(pvr_target ${PVR_GLES_TARGET})
864 add_dependencies(${pvr_target} libGLESv2)
865 add_dependencies(${pvr_target} libEGL)
866 endforeach()
867
868 foreach(pvr_target ${PVR_VULKAN_TARGET_GOOD} ${PVR_GLES_TARGET_GOOD})
Nicolas Capens51b28002020-01-30 16:41:00 -0500869 set_target_properties(${pvr_target} PROPERTIES FOLDER Samples)
870 endforeach()
871
Nicolas Capens3702e012020-03-30 09:08:47 -0400872 foreach(pvr_target ${PVR_TARGET_OTHER} ${PVR_VULKAN_TARGET_OTHER} ${PVR_GLES_TARGET_OTHER})
Nicolas Capens51b28002020-01-30 16:41:00 -0500873 set_target_properties(${pvr_target} PROPERTIES FOLDER Samples/PowerVR-Build)
874 endforeach()
Corentin Wallezcb586622020-03-27 17:38:29 +0100875endif()