blob: 3bfc9898e1a8fa2d13956907a1ec69b0c766455a [file] [log] [blame]
David Neto22f144c2017-06-12 14:26:21 -04001# Copyright 2017 The Clspv 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
15
Marco Antognini858ff312020-08-26 20:47:40 +010016cmake_minimum_required(VERSION 3.13.4)
David Neto22f144c2017-06-12 14:26:21 -040017
alan-bakerb27a3332019-07-03 15:26:35 -040018if(NOT COMMAND find_host_package)
19 macro(find_host_package)
20 find_package(${ARGN})
21 endmacro()
22endif()
23
24# Tests require Python3
25find_host_package(PythonInterp 3 REQUIRED)
26
David Neto22f144c2017-06-12 14:26:21 -040027# If we are the parent CMakeLists.txt, need to declare a project
28if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
29 project(clspv)
30endif()
31
alan-baker9a19de82019-08-21 09:58:25 -040032set(CMAKE_CXX_STANDARD 14)
33
alan-bakera5ff28e2018-11-21 16:27:20 -050034# Make sure CMAKE_BUILD_TYPE gets a value because we rely on it for
35# running unit tests correctly on Windows.
36if (NOT DEFINED CMAKE_BUILD_TYPE)
37 message(STATUS "CMAKE_BUILD_TYPE was not specified, defaulting to Debug")
38 set(CMAKE_BUILD_TYPE Debug)
39endif()
40
David Neto22f144c2017-06-12 14:26:21 -040041# Check if required third-party dependencies exist.
42macro(use_component path)
Neil Henning39672102017-09-29 14:33:13 +010043if(NOT IS_DIRECTORY ${path})
44 message(FATAL_ERROR "Required component '${path}' does not exist! Please run 'python utils/fetch_sources.py' in the '${CMAKE_CURRENT_SOURCE_DIR}' folder.")
David Neto22f144c2017-06-12 14:26:21 -040045endif()
46endmacro()
47
Robert Fossfb1d6912017-10-05 21:03:38 +020048option(SKIP_CLSPV_TOOLS_INSTALL "Skip installation" ${SKIP_CLSPV_TOOLS_INSTALL})
49if(NOT ${SKIP_CLSPV_TOOLS_INSTALL})
50 set(ENABLE_CLSPV_TOOLS_INSTALL ON)
51endif()
52
David Netoe37c3442018-03-15 14:43:53 -040053if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
alan-baker4a757f62020-04-22 08:17:49 -040054 # GCC 7.3 complains about LLVM code: RetryAfterSignal in lib/Support/Process.cpp
55 # Silence that warning for now.
56 # TODO(dneto): Upgrade to newer LLVM to avoid this issue.
57 add_compile_options("-Wno-noexcept-type")
David Netoe37c3442018-03-15 14:43:53 -040058endif()
59
Kévin Petit0d5ffb92018-09-26 21:21:33 +010060if (NOT DEFINED SPIRV_HEADERS_SOURCE_DIR)
61 set(SPIRV_HEADERS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/SPIRV-Headers)
62 use_component(${SPIRV_HEADERS_SOURCE_DIR})
63endif()
64
65if (NOT DEFINED SPIRV_TOOLS_SOURCE_DIR)
Kévin Petit0d5ffb92018-09-26 21:21:33 +010066 set(SPIRV_TOOLS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/SPIRV-Tools)
Kévin Petit0d5ffb92018-09-26 21:21:33 +010067 use_component(${SPIRV_TOOLS_SOURCE_DIR})
Kévin Petit3a327922019-03-18 15:44:11 +000068endif()
Kévin Petit0d5ffb92018-09-26 21:21:33 +010069
Kévin Petit3a327922019-03-18 15:44:11 +000070if (NOT TARGET spirv-dis)
Kévin Petit0d5ffb92018-09-26 21:21:33 +010071 # First tell SPIR-V Tools where to find SPIR-V Headers
72 set(SPIRV-Headers_SOURCE_DIR ${SPIRV_HEADERS_SOURCE_DIR})
73
74 # Bring in the SPIR-V Tools repository which we'll use for testing
Kévin Petit3a327922019-03-18 15:44:11 +000075 add_subdirectory(${SPIRV_TOOLS_SOURCE_DIR}
76 ${CMAKE_CURRENT_BINARY_DIR}/third_party/SPIRV-Tools EXCLUDE_FROM_ALL)
Kévin Petit0d5ffb92018-09-26 21:21:33 +010077endif()
78
alan-bakerf5e5f692018-11-27 08:33:24 -050079set(CMAKE_POSITION_INDEPENDENT_CODE ON)
80
Kévin Petit0d5ffb92018-09-26 21:21:33 +010081set(SPIRV_TOOLS_BINARY_DIR "$<TARGET_FILE_DIR:spirv-dis>")
David Neto22f144c2017-06-12 14:26:21 -040082
Diego Novilloa4c44fa2019-04-11 10:56:15 -040083option(ENABLE_CLSPV_OPT "Enable the clspv-opt driver." ON)
Diego Novilloc4cbaa72019-03-29 17:31:58 -040084
alan-bakerde5497c2019-01-02 09:51:37 -050085if (NOT DEFINED EXTERNAL_LLVM)
86 set(EXTERNAL_LLVM 0)
87endif()
88if (${EXTERNAL_LLVM} EQUAL 1)
89 if (NOT DEFINED CLSPV_LLVM_SOURCE_DIR)
90 message(FATAL_ERROR "External LLVM requires CLSPV_LLVM_SOURCE_DIR to be specified")
91 endif()
92 if (NOT DEFINED CLSPV_CLANG_SOURCE_DIR)
93 message(FATAL_ERROR "External LLVM requires CLSPV_CLANG_SOURCE_DIR to be specified")
94 endif()
95 if (NOT DEFINED CLSPV_LLVM_BINARY_DIR)
96 message(FATAL_ERROR "External LLVM requires CLSPV_LLVM_BINARY_DIR to be specified")
97 endif()
98else()
alan-baker171c5492019-08-09 13:28:48 -040099 # Setup to use the LLVM monorepo.
dan sinclair05c145e2019-05-29 13:46:56 -0400100 if (NOT DEFINED CLSPV_LLVM_SOURCE_DIR)
alan-baker171c5492019-08-09 13:28:48 -0400101 set(CLSPV_LLVM_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/llvm)
dan sinclair05c145e2019-05-29 13:46:56 -0400102 endif()
103
104 if (NOT DEFINED CLSPV_CLANG_SOURCE_DIR)
alan-baker171c5492019-08-09 13:28:48 -0400105 set(CLSPV_CLANG_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/clang)
dan sinclair05c145e2019-05-29 13:46:56 -0400106 endif()
107
108 use_component(${CLSPV_LLVM_SOURCE_DIR})
109 use_component(${CLSPV_CLANG_SOURCE_DIR})
alan-bakerde5497c2019-01-02 09:51:37 -0500110
alan-bakerbccf62c2019-03-29 10:32:41 -0400111 # Kokoro bots have older toolchains so make that LLVM check a warning.
112 option(LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN "" ON)
alan-bakerde5497c2019-01-02 09:51:37 -0500113 set(CLSPV_LLVM_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm)
114
115 # First tell LLVM where to find clang.
116 set(LLVM_EXTERNAL_CLANG_SOURCE_DIR ${CLSPV_CLANG_SOURCE_DIR})
117
Diego Novilloa4c44fa2019-04-11 10:56:15 -0400118 # Tell LLVM not to build any targets.
119 set(LLVM_TARGETS_TO_BUILD ""
120 CACHE STRING "Semicolon-separated list of targets to build, or \"all\".")
121
alan-bakerde5497c2019-01-02 09:51:37 -0500122 # Then pull in LLVM for building.
dan sinclair05c145e2019-05-29 13:46:56 -0400123 add_subdirectory(${CLSPV_LLVM_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/third_party/llvm EXCLUDE_FROM_ALL)
Marco Antognini7e338402021-03-15 12:48:37 +0000124
125 # Ensure clspv and LLVM use the same build options (e.g. -D_FILE_OFFSET_BITS=64).
126 list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/third_party/llvm/llvm/cmake/modules/)
127 include(HandleLLVMOptions)
alan-bakerde5497c2019-01-02 09:51:37 -0500128endif()
129
David Neto22f144c2017-06-12 14:26:21 -0400130set(CLSPV_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
131
132set(CLSPV_INCLUDE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/include
133 ${CLSPV_BINARY_DIR}/include
134)
135
David Neto22f144c2017-06-12 14:26:21 -0400136set(SPIRV_HEADERS_INCLUDE_DIRS
137 ${SPIRV_HEADERS_SOURCE_DIR}/include/
138)
139
alan-bakerde5497c2019-01-02 09:51:37 -0500140set(LLVM_SOURCE_DIR ${CLSPV_LLVM_SOURCE_DIR})
141set(LLVM_BINARY_DIR ${CLSPV_LLVM_BINARY_DIR})
David Neto22f144c2017-06-12 14:26:21 -0400142set(LLVM_INCLUDE_DIRS
143 ${LLVM_SOURCE_DIR}/include
alan-bakerde5497c2019-01-02 09:51:37 -0500144 ${CLSPV_LLVM_BINARY_DIR}/include
David Neto22f144c2017-06-12 14:26:21 -0400145)
146
alan-bakerde5497c2019-01-02 09:51:37 -0500147set(CLANG_SOURCE_DIR ${CLSPV_CLANG_SOURCE_DIR})
David Neto22f144c2017-06-12 14:26:21 -0400148set(CLANG_INCLUDE_DIRS
149 ${CLANG_SOURCE_DIR}/include
alan-bakerde5497c2019-01-02 09:51:37 -0500150 ${CLSPV_LLVM_BINARY_DIR}/tools/clang/include
David Neto22f144c2017-06-12 14:26:21 -0400151)
152
David Neto22f144c2017-06-12 14:26:21 -0400153if(NOT WIN32)
154 # Disable RTTI and exceptions (to match LLVM)
155 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions")
156endif()
157
alan-baker4a757f62020-04-22 08:17:49 -0400158if(${CMAKE_CXX_COMPILER_ID} MATCHES "GNU")
alan-baker5be685c2020-06-10 11:18:08 -0400159 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror=unused-variable -Werror=switch")
alan-baker4a757f62020-04-22 08:17:49 -0400160elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
alan-baker5be685c2020-06-10 11:18:08 -0400161 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror-unused-variable -Werror-switch")
alan-baker4a757f62020-04-22 08:17:49 -0400162endif()
163
David Neto22f144c2017-06-12 14:26:21 -0400164# Bring in our cmake folder
165add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/cmake)
166
167# Bring in our lib folder
168add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/lib)
169
170# Bring in our tools folder
171add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tools)
172
173# Bring in our test folder
174add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/test)
Robert Fossfb1d6912017-10-05 21:03:38 +0200175
176
177if(ENABLE_CLSPV_TOOLS_INSTALL)
178 install(
179 FILES
180 ${CMAKE_CURRENT_SOURCE_DIR}/include/clspv/AddressSpace.h
181 ${CMAKE_CURRENT_SOURCE_DIR}/include/clspv/Passes.h
182 DESTINATION
183 ${CMAKE_INSTALL_INCLUDEDIR}/clspv/)
184endif(ENABLE_CLSPV_TOOLS_INSTALL)