-
Notifications
You must be signed in to change notification settings - Fork 17
/
CMakeLists.txt
448 lines (370 loc) · 16.7 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
cmake_minimum_required(VERSION 3.24)
set(CMAKE_POLICY_DEFAULT_CMP0075 NEW)
# Let cmake apply IPO flags for all compilers and do not output warnings.
set(CMAKE_POLICY_DEFAULT_CMP0069 NEW)
# When the option() command sees a normal variable of the given name,
# the NEW behavior for this policy is to do nothing when a normal variable of the same name exists.
# The normal variable is not removed. The cache entry is not created or updated and is ignored if it exists.
set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)
# Set a default build type if none was specified
if (NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
message(STATUS "Setting build type to 'Debug' as none was specified.")
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
# Set the possible values of build type for cmake-gui
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
"MinSizeRel" "RelWithDebInfo")
endif ()
# Generate version text and building timestamp
if (EXISTS "${CMAKE_SOURCE_DIR}/VERSION")
# For a release version, the version file should be provided
file(READ "${CMAKE_SOURCE_DIR}/VERSION" VERSION_CONTENT)
string(STRIP "${VERSION_CONTENT}" VERSION_CONTENT)
string(REGEX MATCH "^[0-9]+\\.[0-9]+\\.[0-9]+" CMAKE_PROJECT_VERSION "${VERSION_CONTENT}")
else ()
# Otherwise, use git hash as the version
message(WARNING "No VERSION file found. Use git hash as the version string.")
execute_process(
COMMAND git rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE VERSION_CONTENT
OUTPUT_STRIP_TRAILING_WHITESPACE
ERROR_QUIET
)
endif ()
if(VERSION_CONTENT STREQUAL "")
# Fallback version
set(VERSION_CONTENT "Unknown")
set(CMAKE_PROJECT_VERSION "0.0.0")
endif()
project(Crane VERSION ${CMAKE_PROJECT_VERSION} LANGUAGES C CXX)
# check and set compiler
set(REQUIRED_GNU_VERSION 13.0.0)
set(REQUIRED_CLANG_VERSION 19.0.0)
set(REQUIRED_BPF_CLANG_VERSION 17.0.0)
set(CLANG_VERSION "0.0.0")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL REQUIRED_GNU_VERSION)
if(CRANE_ENABLE_CGROUP_V2)
message(STATUS "Enabling Cgroup V2 will build cgroup_dev_bpf_object, which requires Clang ${REQUIRED_BPF_CLANG_VERSION}+. Use GNU ${CMAKE_CXX_COMPILER_VERSION} for other modules. ")
find_program(CLANG_EXECUTABLE NAMES clang)
if(CLANG_EXECUTABLE)
execute_process(
COMMAND ${CLANG_EXECUTABLE} --version
OUTPUT_VARIABLE CLANG_VERSION_OUTPUT
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX MATCH "[0-9]+\\.[0-9]+\\.[0-9]+" CLANG_VERSION ${CLANG_VERSION_OUTPUT})
if(CLANG_VERSION VERSION_GREATER_EQUAL REQUIRED_BPF_CLANG_VERSION)
set(ENABLE_BPF ON)
message(STATUS "Found Clang at ${CLANG_EXECUTABLE} with version ${CLANG_VERSION}; using this version for the cgroup_dev_bpf_object module.")
else()
message(FATAL_ERROR "Clang found at ${CLANG_EXECUTABLE} is version ${CLANG_VERSION}, but version ${REQUIRED_BPF_CLANG_VERSION} or higher is required for device management on Cgroup V2. You may use Cgroup V1 instead.")
endif()
else()
message(FATAL_ERROR "Clang ${REQUIRED_BPF_CLANG_VERSION} or higher is required for device management on Cgroup V2. You can use Cgroup V1.")
endif()
endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL REQUIRED_CLANG_VERSION)
if(CRANE_ENABLE_CGROUP_V2)
set(ENABLE_BPF ON)
endif()
message(STATUS "Using Clang for all module.")
else()
message(FATAL_ERROR "Clang ${REQUIRED_CLANG_VERSION} or higher is required.")
endif()
else()
message(FATAL_ERROR "Neither GNU ${REQUIRED_GNU_VERSION}+ nor Clang ${REQUIRED_CLANG_VERSION}+ found. Stop compiling crane")
endif()
# Options start here ----------------------------------------------------------------------------
option(ENABLE_BERKELEY_DB "Enable Berkeley DB as the embedded db backend" OFF)
option(ENABLE_UNQLITE "Enable Berkeley DB as the embedded db backend" ON)
option(CRANE_ENABLE_TESTS "Enable test targets" OFF)
option(CRANE_FORCE_COLORED_OUTPUT "Always produce ANSI-colored output (GNU/Clang only)." TRUE)
option(CRANE_NATIVE_ARCH_OPT "Enable -march=native compile option" ON)
option(CRANE_USE_GITEE_SOURCE "Enable the Gitee repository as the download source" OFF)
option(CRANE_USE_SYSTEM_LIBCGROUP "Use libcgroup from system instead of building from source" OFF)
option(CRANE_ENABLE_CGROUP_V2 "Enable Cgroup V2 support" OFF)
option(CRANE_FULL_DYNAMIC "Enable dynamic libs" OFF)
option(CRANE_ADDRESS_SANITIZER "Enable address sanitizer" OFF)
option(CRANE_THREAD_SANITIZER "Enable thread sanitizer" OFF)
option(CRANE_MIN_LOG_LEVEL "Set the minimal log level (INFO/DEBUG/TRACE)" OFF)
# Options end here -------------------------------------------------------------------------------
set(CMAKE_CXX_STANDARD 20)
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CRANE_ENABLE_TESTS ON)
endif ()
if (CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(CMAKE_INSTALL_PREFIX "/usr/local" CACHE PATH "Install path prefix, prepended onto install directories." FORCE)
endif ()
message(STATUS "Install prefix is set to ${CMAKE_INSTALL_PREFIX}")
# Set the minimal log level based on the build type if it has not been explicitly set by the user
if (CRANE_MIN_LOG_LEVEL STREQUAL "OFF")
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
set(CRANE_MIN_LOG_LEVEL "TRACE")
else ()
set(CRANE_MIN_LOG_LEVEL "DEBUG")
endif ()
endif ()
# Validate the user-provided log level
if (NOT CRANE_MIN_LOG_LEVEL MATCHES "INFO|DEBUG|TRACE")
message(FATAL_ERROR "Invalid log level: ${CRANE_MIN_LOG_LEVEL}. Must be INFO, DEBUG, or TRACE.")
endif ()
add_compile_definitions(CRANE_LOG_LEVEL=CRANE_LOG_LEVEL_${CRANE_MIN_LOG_LEVEL})
message(STATUS "Minimal log level is set to ${CRANE_MIN_LOG_LEVEL}")
# Generate the building timestamp
string(TIMESTAMP BUILD_TIMESTAMP "%a, %d %b %Y %H:%M:%S %z")
add_compile_definitions(CRANE_BUILD_TIMESTAMP="${BUILD_TIMESTAMP}")
add_compile_definitions(CRANE_VERSION_STRING="${VERSION_CONTENT}")
message(STATUS "Version: ${VERSION_CONTENT}")
message(STATUS "Building Time: ${BUILD_TIMESTAMP}")
# Set colorized output when ninja build system is used.
if (${CRANE_FORCE_COLORED_OUTPUT})
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU")
message(STATUS "Colorized output for gcc is enabled")
add_compile_options(-fdiagnostics-color=always)
elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
message(STATUS "Colorized output for clang is enabled")
add_compile_options(-fcolor-diagnostics)
endif ()
endif ()
if (${CRANE_NATIVE_ARCH_OPT})
message(STATUS "-march=native enabled")
add_compile_options(-march=native)
endif ()
# In CentOS 7, the following packages are required to enable sanitizers:
# 1. devtoolset-11-libasan-devel.x86_64
# 2. devtoolset-11-libtsan-devel.x86_64
if (${CRANE_ADDRESS_SANITIZER})
message(STATUS "address_sanitizer is enabled")
add_compile_options(-fsanitize=address -fno-omit-frame-pointer)
add_link_options(-fsanitize=address)
if (NOT ${CRANE_FULL_DYNAMIC})
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-static-libsan)
else ()
add_compile_options(-static-libasan)
endif ()
endif ()
endif ()
if (${CRANE_THREAD_SANITIZER})
message(STATUS "thread_sanitizer is enabled")
add_compile_options(-fsanitize=thread -fno-omit-frame-pointer)
add_link_options(-fsanitize=thread)
if (NOT ${CRANE_FULL_DYNAMIC})
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_compile_options(-static-libsan)
else ()
add_compile_options(-static-libtsan)
endif ()
endif ()
endif ()
# If Clang is used, select lld as the linker.
# Under clang, if lld is not selected as the linker and LLVMGold is not installed,
# CheckIpoSupported will failed.
# Normally we should pass "-fuse-ld=lld" to linker flags,
# but CheckIpoSupported will only use CMAKE_<LANG>_FLAGS to compile test source files.
# If we add "-fuse-ld=lld" to CMAKE_<LANG>_FLAGS, clang will give the warning:
# argument unused during compilation: '-fuse-ld=lld'.
# To solve this, we first set the linker flag in CMAKE_<LANG>_FLAGS and then move it
# to linker flags.
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(RAW_CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
set(RAW_CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fuse-ld=lld")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fuse-ld=lld")
message("CMAKE_C_FLAGS ${CMAKE_C_FLAGS}")
message("CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}")
endif ()
# Check LTO support
include(CheckIPOSupported)
check_ipo_supported(RESULT supported OUTPUT error)
# If Clang is used, select lld as the linker.
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(CMAKE_C_FLAGS "${RAW_CMAKE_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${RAW_CMAKE_CXX_FLAGS}")
message("CMAKE_C_FLAGS ${CMAKE_C_FLAGS}")
message("CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS}")
unset(RAW_CMAKE_C_FLAGS)
unset(RAW_CMAKE_CXX_FLAGS)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -fuse-ld=lld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
message("CMAKE_EXE_LINKER_FLAGS ${CMAKE_EXE_LINKER_FLAGS}")
message("CMAKE_SHARED_LINKER_FLAGS ${CMAKE_SHARED_LINKER_FLAGS}")
endif ()
if (supported)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_BUILD_TYPE STREQUAL "Release")
# IPO/LTO is disabled in g++ under Debug mode since it's quite slow.
message(STATUS "IPO / LTO enabled")
set(CMAKE_INTERPROCEDURAL_OPTIMIZATION TRUE)
endif ()
else ()
message(STATUS "IPO / LTO not supported: <${error}>")
endif ()
find_program(CCACHE_PROGRAM ccache)
if (CCACHE_PROGRAM)
message(STATUS "ccache found. Use ccache to launch compilers.")
set_property(GLOBAL PROPERTY CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set_property(GLOBAL PROPERTY CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif ()
if (${CRANE_FULL_DYNAMIC})
set(CMAKE_SKIP_BUILD_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
else ()
# static link on c++ libs
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-libstdc++")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-libstdc++")
endif ()
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
# search lib64 directory
set(FIND_LIBRARY_USE_LIB64_PATHS TRUE)
endif ()
# Add pre-included libraries files.
set(DEPENDENCIES_PRE_INSTALLED_DIR ${CMAKE_CURRENT_SOURCE_DIR}/dependencies/pre_installed)
add_subdirectory(${DEPENDENCIES_PRE_INSTALLED_DIR})
find_package(Threads REQUIRED)
# New in version cmake3.24:
# Set ZLIB_USE_STATIC_LIBS to ON to look for static libraries. Default is OFF.
# We set this directory variable here to OFF to make all find_package(ZLIB) in
# in this project to use dynamic zlib library file.
set(ZLIB_USE_STATIC_LIBS OFF)
# Some content are downloaded and built inside cmake folder.
# This line must be place before any find_package() command.
# Independently built projects are installed to ${DEPENDENCIES_ONLINE_DIR}
#
# Since find_package needs to be in the top scope, we append the paths of installed
# projects at top-level CMakeLists.txt
#
# EXCLUDE_FROM_ALL excludes all dependencies from being installed
add_subdirectory(dependencies/cmake EXCLUDE_FROM_ALL)
add_subdirectory(dependencies/CPM EXCLUDE_FROM_ALL)
# Notify CMake that we have module files to find packages/libs.
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModule/")
if (ENABLE_BERKELEY_DB)
message("Enable berkeley db as one of embedded db backend.")
find_package(BerkeleyDB REQUIRED)
if (BERKELEYDB_FOUND)
message("Berkeley DB found. Include: ${BERKELEY_DB_INCLUDE_DIR}; Libs: ${BERKELEY_DB_CXX_LIBRARIES}")
else ()
message(FATAL_ERROR "Berkeley DB was not found.")
endif ()
endif ()
if (ENABLE_UNQLITE)
message("Enable Unqlite as one of embedded db backend.")
endif ()
if (NOT (ENABLE_BERKELEY_DB AND BERKELEYDB_FOUND) AND NOT ENABLE_UNQLITE)
message(FATAL_ERROR "At least one of Berkeley DB and Unqlite should be enabled.")
endif ()
find_package(PAM REQUIRED)
if (PAM_FOUND)
message(STATUS "PAM library found. Include: ${PAM_INCLUDE_DIR}; Libs: ${PAM_LIBRARIES}")
else ()
message(FATAL_ERROR "PAM library was not found.")
endif ()
find_package(LibAIO REQUIRED)
if (LibAIO_FOUND)
message(STATUS "LibAIO found. Include: ${LIBAIO_INCLUDE_DIRS}; Libs: ${LIBAIO_LIBRARIES}")
else ()
message(FATAL_ERROR "LibAIO was not found.")
endif ()
# Needed by grpc
set(_PROTOBUF_LIBPROTOBUF libprotobuf)
set(_REFLECTION grpc++_reflection)
set(_PROTOBUF_PROTOC $<TARGET_FILE:protoc>)
set(_GRPC_GRPCPP grpc++)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:grpc_cpp_plugin>)
if(ENABLE_BPF)
find_package(PkgConfig REQUIRED)
pkg_check_modules(libbpf REQUIRED IMPORTED_TARGET libbpf>=1.4.6)
endif()
# @formatter:off
add_definitions(-DCRANE_BUILD_DIRECTORY=\("${CMAKE_BINARY_DIR}"\))
# @formatter:on
# Proto
add_subdirectory(protos)
# Source Code
add_subdirectory(src)
# Tests
if (CRANE_ENABLE_TESTS)
enable_testing()
# Test source files may include lots of warnings and errors.
# Exclude it from 'all' target
add_subdirectory(test EXCLUDE_FROM_ALL)
endif ()
# Generate the configuration file
configure_file(${CMAKE_SOURCE_DIR}/etc/cranectld.service.in ${CMAKE_BINARY_DIR}/etc/cranectld.service)
configure_file(${CMAKE_SOURCE_DIR}/etc/craned.service.in ${CMAKE_BINARY_DIR}/etc/craned.service)
# Add group and components
set(CPACK_COMPONENTS_GROUPING ONE_PER_GROUP)
set(CPACK_RPM_COMPONENT_INSTALL ON)
cpack_add_component(cranedc
DISPLAY_NAME "craned"
DESCRIPTION "craned - Execution Daemon of CraneSched"
GROUP craned)
cpack_add_component(cranectldc
DISPLAY_NAME "cranectld"
DESCRIPTION "cranectld - Control Daemon of CraneSched"
GROUP cranectld)
cpack_add_component_group(craned)
cpack_add_component_group(cranectld)
set(CPACK_COMPONENTS_ALL cranedc cranectldc)
# Install binaries
install(TARGETS cranectld
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
COMPONENT cranectldc
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_WRITE WORLD_EXECUTE
)
install(TARGETS craned
DESTINATION ${CMAKE_INSTALL_PREFIX}/bin
COMPONENT cranedc
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_WRITE GROUP_EXECUTE WORLD_READ WORLD_WRITE WORLD_EXECUTE
)
# Install unit files
install(FILES
${CMAKE_BINARY_DIR}/etc/cranectld.service
DESTINATION /usr/lib/systemd/system/
COMPONENT cranectldc
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
install(FILES
${CMAKE_BINARY_DIR}/etc/craned.service
DESTINATION /usr/lib/systemd/system/
COMPONENT cranedc
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
# Install configuration files
install(FILES ${CMAKE_SOURCE_DIR}/etc/config.yaml
DESTINATION /etc/crane/
COMPONENT cranectldc
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
install(FILES ${CMAKE_SOURCE_DIR}/etc/config.yaml
DESTINATION /etc/crane/
COMPONENT cranedc
PERMISSIONS OWNER_READ OWNER_WRITE GROUP_READ WORLD_READ)
# Only cranectld needs database.yaml and permission should be 600
install(FILES ${CMAKE_SOURCE_DIR}/etc/database.yaml
DESTINATION /etc/crane/
COMPONENT cranectldc
PERMISSIONS OWNER_READ OWNER_WRITE)
# Install PAM config
install(TARGETS pam_crane
DESTINATION /usr/lib64/security/
COMPONENT cranedc)
set(CPACK_GENERATOR "RPM;DEB")
set(CMAKE_PROJECT_HOMEPAGE_URL "https://github.com/PKUHPC/CraneSched")
set(CPACK_PACKAGE_NAME "CraneSched")
set(CPACK_PACKAGE_VENDOR "PKUHPC")
set(CPACK_PACKAGE_CONTACT "CraneSched <[email protected]>")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "An HPC and Cloud Computing Fused Job Scheduling System")
set(CPACK_PACKAGE_DESCRIPTION "An HPC and Cloud Computing Fused Job Scheduling System")
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
set(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.md")
set(CPACK_PACKAGE_FILE_NAME "${CPACK_PACKAGE_NAME}-${CMAKE_PROJECT_VERSION}-${CMAKE_SYSTEM_NAME}-${CMAKE_SYSTEM_PROCESSOR}")
# Do not allow the package to be relocated
set(CPACK_PACKAGE_RELOCATABLE OFF)
# RPM
set(CPACK_RPM_PACKAGE_GROUP "System Environment/Base")
set(CPACK_RPM_PACKAGE_LICENSE "AGPL v3")
set(CPACK_RPM_COMPRESSION_TYPE "xz")
#TODO CPACK_PACKAGE_DESCRIPTION
# DEB
set(CPACK_DEB_COMPONENT_INSTALL ON)
set(CPACK_DEBIAN_COMPRESSION_TYPE "xz")
include(CPack)