forked from oneapi-src/unified-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
282 lines (240 loc) · 10 KB
/
CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# Copyright (C) 2022-2023 Intel Corporation
# Part of the Unified-Runtime Project, under the Apache License v2.0 with LLVM Exceptions.
# See LICENSE.TXT
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
cmake_minimum_required(VERSION 3.14.0 FATAL_ERROR)
project(unified-runtime VERSION 0.7.0)
include(GNUInstallDirs)
include(CheckCXXSourceCompiles)
include(CMakePackageConfigHelpers)
include(CTest)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(helpers)
if(CMAKE_SYSTEM_NAME STREQUAL Darwin)
set(CMAKE_FIND_FRAMEWORK NEVER)
endif()
find_package(Python3 COMPONENTS Interpreter REQUIRED)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
# Build Options
option(UR_BUILD_TESTS "Build unit tests." ON)
option(UR_BUILD_TOOLS "build ur tools" ON)
option(UR_FORMAT_CPP_STYLE "format code style of C++ sources" OFF)
option(UR_DEVELOPER_MODE "enable developer checks, treats warnings as errors" OFF)
option(UR_USE_ASAN "enable AddressSanitizer" OFF)
option(UR_USE_UBSAN "enable UndefinedBehaviorSanitizer" OFF)
option(UR_USE_MSAN "enable MemorySanitizer" OFF)
option(UR_USE_TSAN "enable ThreadSanitizer" OFF)
option(UR_ENABLE_TRACING "enable api tracing through xpti" OFF)
option(UMF_BUILD_SHARED_LIBRARY "Build UMF as shared library" OFF)
option(UMF_ENABLE_POOL_TRACKING "Build UMF with pool tracking" OFF)
option(UR_BUILD_ADAPTER_L0 "build level 0 adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_OPENCL "build opencl adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_CUDA "build cuda adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_HIP "build hip adapter from SYCL" OFF)
option(UR_BUILD_ADAPTER_NATIVE_CPU "build native_cpu adapter from SYCL" OFF)
option(UR_BUILD_EXAMPLE_CODEGEN "Build the codegen example." OFF)
option(VAL_USE_LIBBACKTRACE_BACKTRACE "enable libbacktrace validation backtrace for linux" OFF)
set(UR_DPCXX "" CACHE FILEPATH "Path of the DPC++ compiler executable")
set(UR_SYCL_LIBRARY_DIR "" CACHE PATH
"Path of the SYCL runtime library directory")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
if(MSVC)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin/$<CONFIG>)
endif()
# Define rpath for libraries so that adapters can be found automatically
set(CMAKE_BUILD_RPATH "${CMAKE_LIBRARY_OUTPUT_DIRECTORY}")
# Define a path for custom commands to work around MSVC
set(CUSTOM_COMMAND_BINARY_DIR ${CMAKE_RUNTIME_OUTPUT_DIRECTORY})
if(MSVC)
# MSVC implicitly adds $<CONFIG> to the output path
set(CUSTOM_COMMAND_BINARY_DIR ${CUSTOM_COMMAND_BINARY_DIR}/$<CONFIG>)
endif()
if(NOT MSVC)
# Determine if libstdc++ is being used.
check_cxx_source_compiles("
#include <array>
#ifndef __GLIBCXX__
#error not using libstdc++
#endif
int main() {}"
USING_LIBSTDCXX)
if(USING_LIBSTDCXX)
# Support older versions of GCC where the <filesystem> header is not
# available and <experimental/filesystem> must be used instead. This
# requires linking against libstdc++fs.a, on systems where <filesystem>
# is available we still need to link this library.
link_libraries(stdc++fs)
endif()
endif()
if(UR_ENABLE_TRACING)
add_compile_definitions(UR_ENABLE_TRACING)
# fetch xpti proxy library for the tracing layer
FetchContentSparse_Declare(xpti https://github.com/intel/llvm.git "sycl-nightly/20230703" "xpti")
FetchContent_MakeAvailable(xpti)
# set -fPIC for xpti since we are linking it with a shared library
set_target_properties(xpti PROPERTIES POSITION_INDEPENDENT_CODE ON)
# fetch the xptifw dispatcher, mostly used for testing
# these variables need to be set for xptifw to compile
set(XPTI_SOURCE_DIR ${xpti_SOURCE_DIR})
set(XPTI_DIR ${xpti_SOURCE_DIR})
set(XPTI_ENABLE_TESTS OFF CACHE INTERNAL "Turn off xptifw tests")
FetchContentSparse_Declare(xptifw https://github.com/intel/llvm.git "sycl-nightly/20230703" "xptifw")
FetchContent_MakeAvailable(xptifw)
check_cxx_compiler_flag("-Wno-error=maybe-uninitialized" HAS_MAYBE_UNINIT)
if (HAS_MAYBE_UNINIT)
target_compile_options(xptifw PRIVATE -Wno-error=maybe-uninitialized)
endif()
set_target_properties(xptifw PROPERTIES
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}
)
if (MSVC)
set(TARGET_XPTI $<IF:$<CONFIG:Release>,xpti,xptid>)
# disable warning C4267: The compiler detected a conversion from size_t to a smaller type.
target_compile_options(xptifw PRIVATE /wd4267)
else()
set(TARGET_XPTI xpti)
endif()
endif()
if(UR_USE_ASAN)
add_sanitizer_flag(address)
endif()
if(UR_USE_UBSAN)
add_sanitizer_flag(undefined)
endif()
if(UR_USE_TSAN)
add_sanitizer_flag(thread)
endif()
if(UR_USE_MSAN)
message(WARNING "MemorySanitizer requires that all code is built with
its instrumentation, otherwise false positives are possible.
See https://github.com/google/sanitizers/wiki/MemorySanitizerLibcxxHowTo#instrumented-libc
for details")
add_sanitizer_flag(memory)
endif()
# Check if clang-format (in correct version) is available for Cpp code formatting.
if(UR_FORMAT_CPP_STYLE)
find_program(CLANG_FORMAT NAMES clang-format-15 clang-format-15.0 clang-format)
if(CLANG_FORMAT)
get_program_version_major_minor(${CLANG_FORMAT} CLANG_FORMAT_VERSION)
message(STATUS "Found clang-format: ${CLANG_FORMAT} (version: ${CLANG_FORMAT_VERSION})")
set(CLANG_FORMAT_REQUIRED "15.0")
if(NOT (CLANG_FORMAT_VERSION VERSION_EQUAL CLANG_FORMAT_REQUIRED))
message(FATAL_ERROR "required clang-format version is ${CLANG_FORMAT_REQUIRED}")
endif()
else()
message(FATAL_ERROR "UR_FORMAT_CPP_STYLE=ON, but clang-format not found (required version: ${CLANG_FORMAT_REQUIRED})")
endif()
endif()
# Obtain files for clang-format and license check
set(format_glob)
set(license_glob)
foreach(dir examples include source test tools)
list(APPEND format_glob
"${dir}/*.h"
"${dir}/*.hpp"
"${dir}/*.c"
"${dir}/*.cpp"
"${dir}/**/*.h"
"${dir}/**/*.hpp"
"${dir}/**/*.c"
"${dir}/**/*.cpp")
list(APPEND license_glob
"${dir}/*.yml"
"${dir}/**/*.yml"
"${dir}/*.py"
"${dir}/**/*.py"
"${dir}/**/CMakeLists.txt"
"${dir}/CMakeLists.txt"
)
endforeach()
file(GLOB_RECURSE format_src ${format_glob})
file(GLOB_RECURSE license_src ${license_glob})
# Add license check target
list(FILTER license_src EXCLUDE REGEX "registry.yml")
add_custom_target(verify-licenses
COMMAND ${Python3_EXECUTABLE}
"${PROJECT_SOURCE_DIR}/scripts/verify_license.py"
"--files" ${format_src} ${license_src}
COMMENT "Verify all files contain a license."
)
# Add code formatter target
add_custom_target(cppformat)
# ... and all source files to the formatter
add_cppformat(all-sources ${format_src})
# Allow custom third_party folder
if(NOT DEFINED THIRD_PARTY_DIR)
set(THIRD_PARTY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party)
endif()
add_subdirectory(${THIRD_PARTY_DIR})
# A header only library to specify include directories in transitive
# dependencies.
add_library(ur_headers INTERFACE)
# Alias target to support FetchContent.
add_library(${PROJECT_NAME}::headers ALIAS ur_headers)
target_include_directories(ur_headers INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>)
# Add the include directory and the headers target to the install.
install(
DIRECTORY "${PROJECT_SOURCE_DIR}/include/"
DESTINATION include COMPONENT ur_headers)
install(
TARGETS ur_headers
EXPORT ${PROJECT_NAME}-targets)
add_subdirectory(source)
add_subdirectory(examples)
if(UR_BUILD_TESTS)
add_subdirectory(test)
endif()
if(UR_BUILD_TOOLS)
add_subdirectory(tools)
endif()
# Add the list of installed targets to the install. This includes the namespace
# which all installed targets will be prefixed with, e.g. for the headers
# target users will depend on ${PROJECT_NAME}::headers.
install(
EXPORT ${PROJECT_NAME}-targets
FILE ${PROJECT_NAME}-targets.cmake
NAMESPACE ${PROJECT_NAME}::
DESTINATION lib/cmake/${PROJECT_NAME})
# Configure the package versions file for use in find_package when installed.
write_basic_package_version_file(
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config-version.cmake
COMPATIBILITY SameMajorVersion)
# Configure the package file that is searched for by find_package when
# installed.
configure_package_config_file(
${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config.cmake
INSTALL_DESTINATION lib/cmake/${PROJECT_NAME})
# Add the package files to the install.
install(
FILES
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config.cmake
${PROJECT_BINARY_DIR}/cmake/${PROJECT_NAME}-config-version.cmake
DESTINATION lib/cmake/${PROJECT_NAME})
set(API_JSON_FILE ${PROJECT_BINARY_DIR}/unified_runtime.json)
if(UR_FORMAT_CPP_STYLE)
# Generate source from the specification
add_custom_target(generate-code USES_TERMINAL
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/scripts
COMMAND ${Python3_EXECUTABLE} run.py --api-json ${API_JSON_FILE} --clang-format=${CLANG_FORMAT}
COMMAND ${Python3_EXECUTABLE} json2src.py --api-json ${API_JSON_FILE} ${PROJECT_SOURCE_DIR}
)
# Generate and format source from the specification
add_custom_target(generate USES_TERMINAL
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target generate-code
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target cppformat
)
# Generate source and check for uncommitted diffs
add_custom_target(check-generated
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
COMMAND git diff --exit-code
DEPENDS generate
)
else()
message(STATUS " UR_FORMAT_CPP_STYLE not set. Targets: 'generate' and 'check-generated' are not available")
endif()