-
Notifications
You must be signed in to change notification settings - Fork 772
Expand file tree
/
Copy pathCMakeLists.txt
More file actions
373 lines (311 loc) · 13.2 KB
/
CMakeLists.txt
File metadata and controls
373 lines (311 loc) · 13.2 KB
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
# Copyright (C) Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
cmake_minimum_required(VERSION 3.25)
cmake_policy(SET CMP0079 NEW) # Allow target_link_libraries() in subdirs
include(feature-flags.cmake)
include(src/cmake/environment-utils.cmake)
is_running_in_ci(IS_RUNNING_IN_CI)
option(VCPKG_BUILD_DEFAULT "Enable or disable building the default vcpkg triplet, which includes both debug and release variants." OFF)
option(MULTIPASS_ENABLE_TESTS "Build tests" ON)
message(STATUS "Running in CI environment? ${IS_RUNNING_IN_CI}")
if(NOT ${IS_RUNNING_IN_CI})
include(src/cmake/configure-linker.cmake)
configure_linker()
endif()
cmake_host_system_information(RESULT HOST_OS_NAME QUERY OS_NAME)
cmake_host_system_information(RESULT HOST_ARCH QUERY OS_PLATFORM)
if ("${HOST_ARCH}" STREQUAL "arm64" OR "${HOST_ARCH}" STREQUAL "aarch64")
set(VCPKG_HOST_ARCH "arm64")
elseif ("${HOST_ARCH}" STREQUAL "s390x")
set(VCPKG_HOST_ARCH "s390x")
elseif ("${HOST_ARCH}" STREQUAL "ppc64le")
set(VCPKG_HOST_ARCH "ppc64le")
else()
set(VCPKG_HOST_ARCH "x64")
endif()
include(CMakeDependentOption)
cmake_dependent_option(FORCE_ENABLE_VIRTUALBOX "Enable VirtualBox" ON UNIX OFF)
if("${HOST_OS_NAME}" STREQUAL "macOS")
# needs to be set before "project"
set(VCPKG_HOST_OS "osx")
set(CMAKE_OSX_DEPLOYMENT_TARGET "13.3" CACHE STRING "macOS Deployment Target")
elseif("${HOST_OS_NAME}" STREQUAL "Windows")
set(VCPKG_HOST_OS "windows-static-md")
else()
set(VCPKG_HOST_OS "linux")
endif()
# Use caching option if it's installed
find_program(SCCACHE_PROGRAM sccache)
find_program(CCACHE_PROGRAM ccache)
if(SCCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER "${SCCACHE_PROGRAM}")
if("${HOST_OS_NAME}" STREQUAL "Windows")
set(CMAKE_MSVC_DEBUG_INFORMATION_FORMAT "$<$<CONFIG:Debug,RelWithDebInfo>:Embedded>")
cmake_policy(SET CMP0141 NEW)
endif()
elseif(CCACHE_PROGRAM)
set(CMAKE_CXX_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
set(CMAKE_C_COMPILER_LAUNCHER "${CCACHE_PROGRAM}")
endif()
if(NOT VCPKG_BUILD_DEFAULT)
message(STATUS "Will build `vcpkg` dependencies in `release-only` mode.")
# Propagate macOS deployment target to vcpkg triplet
if(APPLE AND DEFINED CMAKE_OSX_DEPLOYMENT_TARGET)
set(VCPKG_TRIPLETS_DIR "${CMAKE_CURRENT_BINARY_DIR}/vcpkg-triplets")
configure_file(
"${CMAKE_CURRENT_SOURCE_DIR}/3rd-party/vcpkg-triplets/osx-release.cmake.in"
"${VCPKG_TRIPLETS_DIR}/${VCPKG_HOST_ARCH}-${VCPKG_HOST_OS}-release.cmake"
@ONLY
)
set(VCPKG_OVERLAY_TRIPLETS "${VCPKG_TRIPLETS_DIR}" CACHE STRING "Custom vcpkg triplets directory")
message(STATUS "Generated vcpkg triplet: ${VCPKG_HOST_ARCH}-${VCPKG_HOST_OS}-release.cmake with deployment target ${CMAKE_OSX_DEPLOYMENT_TARGET}")
endif()
set(VCPKG_HOST_TRIPLET "${VCPKG_HOST_ARCH}-${VCPKG_HOST_OS}-release")
set(VCPKG_TARGET_TRIPLET "${VCPKG_HOST_ARCH}-${VCPKG_HOST_OS}-release")
else()
message(NOTICE "Will build `vcpkg` deps in both `debug` and `release` configurations. Be aware that it will take around twice the time to build the `vcpkg` deps.")
endif()
message(STATUS "Bootstrapping vcpkg...")
set(MULTIPASS_VCPKG_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}/3rd-party/vcpkg" CACHE PATH
"Root vcpkg location, where the top bootstrap scripts are located")
set(VCPKG_CMD "${MULTIPASS_VCPKG_LOCATION}/bootstrap-vcpkg.")
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Windows")
string(APPEND VCPKG_CMD "bat")
list(PREPEND VCPKG_CMD "cmd" "/c") # run in cmd; otherwise, we'd fail when called from PowerShell
else ()
string(APPEND VCPKG_CMD "sh")
endif ()
# GCC is unreliable on ppc64le and can cause internal compiler errors during vcpkg bootstrap
set(VCPKG_BOOTSTRAP_ENV)
if(${VCPKG_HOST_ARCH} STREQUAL "ppc64le")
set(VCPKG_BOOTSTRAP_ENV
"CC=clang"
"CXX=clang++"
"CMAKE_C_COMPILER=clang"
"CMAKE_CXX_COMPILER=clang++")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} -E env ${VCPKG_BOOTSTRAP_ENV} ${VCPKG_CMD} RESULT_VARIABLE VCPKG_BOOTSTRAP_RESULT)
if(NOT VCPKG_BOOTSTRAP_RESULT EQUAL 0)
message(FATAL_ERROR "Bootstrapping vcpkg failed with exit code: ${VCPKG_BOOTSTRAP_RESULT}")
else()
message(STATUS "Bootstrapping vcpkg completed successfully.")
endif()
if(MULTIPASS_ENABLE_TESTS)
set(VCPKG_MANIFEST_FEATURES "tests" CACHE STRING "Enabled vcpkg features")
endif()
set(CMAKE_TOOLCHAIN_FILE "${MULTIPASS_VCPKG_LOCATION}/scripts/buildsystems/vcpkg.cmake"
CACHE STRING "Vcpkg toolchain file")
project(Multipass)
cmake_dependent_option(
MULTIPASS_ENABLE_FLUTTER_GUI
"Build Flutter GUI"
ON
"NOT (CMAKE_SYSTEM_PROCESSOR STREQUAL s390x OR CMAKE_SYSTEM_PROCESSOR STREQUAL ppc64le)"
OFF
)
include(GNUInstallDirs)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}")
if(MULTIPASS_ENABLE_FLUTTER_GUI)
if(MSVC)
set(FLUTTER_TOOLS_EXTENSION ".bat")
endif()
set(FLUTTER_EXECUTABLE ${CMAKE_SOURCE_DIR}/3rd-party/flutter/bin/flutter${FLUTTER_TOOLS_EXTENSION})
set(DART_EXECUTABLE ${CMAKE_SOURCE_DIR}/3rd-party/flutter/bin/dart${FLUTTER_TOOLS_EXTENSION})
if(MSVC)
set(FLUTTER_CONFIG_ARGS --no-enable-linux-desktop --no-enable-macos-desktop --enable-windows-desktop)
elseif(APPLE)
set(FLUTTER_CONFIG_ARGS --no-enable-linux-desktop --enable-macos-desktop --no-enable-windows-desktop)
elseif(UNIX AND NOT APPLE)
set(FLUTTER_CONFIG_ARGS --enable-linux-desktop --no-enable-macos-desktop --no-enable-windows-desktop)
endif()
execute_process(COMMAND ${FLUTTER_EXECUTABLE} config ${FLUTTER_CONFIG_ARGS} --suppress-analytics --no-enable-web --no-enable-android --no-enable-ios --no-enable-fuchsia --no-enable-custom-devices
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${FLUTTER_EXECUTABLE} precache ${FLUTTER_PRECACHE_OS}
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${DART_EXECUTABLE} pub get
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/3rd-party/protobuf.dart/protoc_plugin
COMMAND_ERROR_IS_FATAL ANY)
execute_process(COMMAND ${DART_EXECUTABLE} compile exe bin/protoc_plugin.dart
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/3rd-party/protobuf.dart/protoc_plugin
COMMAND_ERROR_IS_FATAL ANY)
set(DART_PROTOC_PLUGIN ${CMAKE_SOURCE_DIR}/3rd-party/protobuf.dart/protoc_plugin/bin/protoc_plugin.exe)
endif(MULTIPASS_ENABLE_FLUTTER_GUI)
string(TOLOWER "${CMAKE_BUILD_TYPE}" cmake_build_type_lower)
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
add_definitions(-DMULTIPASS_COMPILER_CLANG)
if(cmake_build_type_lower MATCHES "asan")
add_compile_options(-fno-omit-frame-pointer -fsanitize=address)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=address")
elseif(cmake_build_type_lower MATCHES "ubsan")
add_compile_options(-fno-omit-frame-pointer -fsanitize=undefined)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=undefined")
elseif(cmake_build_type_lower MATCHES "tsan")
add_compile_options(-fno-omit-frame-pointer -fsanitize=thread)
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fno-omit-frame-pointer -fsanitize=thread")
endif()
endif()
# these we want to apply even to 3rd-party
if(cmake_build_type_lower MATCHES "coverage")
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_compile_options(--coverage)
if(COMMAND add_link_options)
add_link_options(--coverage)
else()
string(APPEND CMAKE_SHARED_LINKER_FLAGS " --coverage")
string(APPEND CMAKE_EXE_LINKER_FLAGS " --coverage")
endif()
else()
message(FATAL_ERROR "Coverage build only supported with GCC")
endif()
endif()
if(UNIX)
set(HOST_ARCH ${CMAKE_HOST_SYSTEM_PROCESSOR})
if (${HOST_ARCH} STREQUAL "arm64")
set(HOST_ARCH "aarch64")
endif ()
if (${HOST_ARCH} STREQUAL "ppc64le")
set(HOST_ARCH "ppc64")
endif ()
if (FORCE_ENABLE_VIRTUALBOX OR (APPLE AND ${HOST_ARCH} MATCHES "x86_64"))
list(APPEND MULTIPASS_BACKENDS virtualbox)
endif()
list(APPEND MULTIPASS_BACKENDS qemu)
if(APPLEVZ_ENABLED)
list(APPEND MULTIPASS_BACKENDS applevz)
endif()
endif()
# Boost config
find_package(Boost CONFIG REQUIRED COMPONENTS json)
# OpenSSL config
find_package(OpenSSL CONFIG REQUIRED)
# gRPC config
find_package(gRPC CONFIG REQUIRED)
# SG_REQUIRE_NOEXCEPT_IN_CPP17
# fmt config
find_package(fmt CONFIG REQUIRED)
# targets: fmt::fmt, fmt::fmt-header-only
# scope_guard config
find_path(SCOPE_GUARD_INCLUDE_DIRS "scope_guard.hpp")
add_library(scope_guard INTERFACE)
target_include_directories(scope_guard INTERFACE ${SCOPE_GUARD_INCLUDE_DIRS})
target_compile_definitions(scope_guard INTERFACE SG_REQUIRE_NOEXCEPT_IN_CPP17)
# yaml-cpp config
find_package(yaml-cpp CONFIG REQUIRED)
# neargye-semver config
find_package(semver CONFIG REQUIRED)
# Needs to be here before we set further compilation options
add_subdirectory(3rd-party)
# Qt config
find_package(Qt6 COMPONENTS Core Concurrent Network REQUIRED)
include(src/cmake/versioning.cmake)
determine_version(MULTIPASS_VERSION)
set(MULTIPASS_VERSION ${MULTIPASS_VERSION})
message(STATUS "Setting version to: ${MULTIPASS_VERSION}")
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(MSVC)
add_definitions(-DMULTIPASS_COMPILER_MSVC)
add_definitions(-D_WIN32_WINNT=0xA00) # A gprc header requires this (specifies Windows 10)
add_definitions(-DNOMINMAX) # otherwise windows defines macros which interfere with std::min, std::max
add_definitions(-DMULTIPASS_PLATFORM_WINDOWS)
add_definitions(-DLIBSSH_STATIC) # otherwise adds declspec specifiers to libssh apis
add_definitions(-D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS)
add_definitions(-DWIN32_LEAN_AND_MEAN)
set(MULTIPASS_BACKENDS hyperv virtualbox)
set(MULTIPASS_PLATFORM windows)
else()
add_compile_options(-Werror -Wall -pedantic -fPIC)
if(APPLE)
add_definitions(-DMULTIPASS_PLATFORM_APPLE)
set(MULTIPASS_PLATFORM macos)
add_definitions(-DMULTIPASS_COMPILER_APPLE_CLANG)
else()
# Linux
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
add_definitions(-DMULTIPASS_COMPILER_GCC)
endif()
add_compile_options(-Wextra -Wempty-body -Wformat-security -Winit-self -Warray-bounds $<$<COMPILE_LANGUAGE:CXX>:-Wnon-virtual-dtor>)
if(NOT ${CMAKE_HOST_SYSTEM_PROCESSOR} MATCHES "^arm")
add_compile_options(-Wcast-align)
endif()
CHECK_CXX_COMPILER_FLAG("-Wno-expansion-to-defined" COMPILER_SUPPORTS_NO_EXP_TO_DEFINED)
if(COMPILER_SUPPORTS_NO_EXP_TO_DEFINED)
add_compile_options(-Wno-expansion-to-defined)
endif()
add_definitions(-DMULTIPASS_PLATFORM_LINUX)
set(MULTIPASS_PLATFORM linux)
set(LINUX TRUE)
endif()
endif()
if(cmake_build_type_lower MATCHES "coverage")
find_program(GCOV gcov)
find_program(LCOV lcov)
find_program(GENHTML genhtml)
if(NOT (GCOV AND LCOV AND GENHTML))
message(AUTHOR_WARNING
"gcov, lcov and genhtml required for coverage reports. Disabling."
)
else()
message(STATUS
"Coverage enabled, use the 'covreport' target."
)
execute_process(COMMAND ${LCOV} --version
OUTPUT_VARIABLE LCOV_VERSION_RAW)
string(REGEX REPLACE "lcov: LCOV version " "" LCOV_VERSION_STR "${LCOV_VERSION_RAW}")
string(REGEX REPLACE "\\..*" "" LCOV_VERSION_MAJOR "${LCOV_VERSION_STR}")
if(${LCOV_VERSION_MAJOR} GREATER_EQUAL 2)
set(LCOV_IGNORE_ERRORS --ignore-errors mismatch --ignore-errors negative)
endif()
add_custom_target(covreport
DEPENDS multipass_tests
WORKING_DIRECTORY ${CMAKE_BUILD_DIR}
COMMAND ${LCOV} --directory . --zerocounters
COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target test
COMMAND ${LCOV} --directory . --capture --output-file coverage.info ${LCOV_IGNORE_ERRORS}
COMMAND ${LCOV}
--remove coverage.info
'/usr/*'
${CMAKE_SOURCE_DIR}'/3rd-party/*'
${CMAKE_SOURCE_DIR}'/tests/*'
${CMAKE_BINARY_DIR}'/*'
--output-file coverage.cleaned
COMMAND ${CMAKE_COMMAND} -E remove coverage.info
COMMAND ${GENHTML} -o coverage coverage.cleaned
)
endif()
endif()
set(MULTIPASS_GENERATED_SOURCE_DIR ${CMAKE_BINARY_DIR}/gen)
file(MAKE_DIRECTORY ${MULTIPASS_GENERATED_SOURCE_DIR})
include_directories(
include
${MULTIPASS_GENERATED_SOURCE_DIR})
add_subdirectory(data)
add_subdirectory(src)
if(MULTIPASS_ENABLE_TESTS)
enable_testing()
add_subdirectory(tests/unit)
endif()
include(packaging/cpack.cmake OPTIONAL)
# Disable .clang-tidy in build folder
file(WRITE "${CMAKE_BINARY_DIR}/.clang-tidy"
"---
Checks: >
-*,
")