forked from SJTU-IPADS/OS-Course-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMakeLists.txt
258 lines (217 loc) · 9.41 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
# Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
# Licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
# http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
cmake_minimum_required(VERSION 3.14)
project(ChCore)
set(_cmake_script_dir ${CMAKE_CURRENT_SOURCE_DIR}/scripts/build/cmake)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${_cmake_script_dir}/Modules)
set(CMAKE_PREFIX_PATH ${CMAKE_CURRENT_BINARY_DIR}/user)
include(SubProject)
include(CommonTools)
chcore_dump_chcore_vars()
chcore_collect_pkg_path(${CMAKE_CURRENT_SOURCE_DIR}/user)
chcore_get_nproc(_nproc)
if(CHCORE_VERBOSE_BUILD)
set(CMAKE_VERBOSE_MAKEFILE ON)
endif()
set(_libc_install_dir ${CMAKE_CURRENT_BINARY_DIR}/chcore-libc)
list(APPEND CMAKE_PREFIX_PATH ${_libc_install_dir})
string(REPLACE ";" ":" _colon_prefix_path "${CMAKE_PREFIX_PATH}")
set(_common_args
-DCMAKE_MODULE_PATH=${CMAKE_MODULE_PATH}
-DCMAKE_PREFIX_PATH=${_colon_prefix_path}
-DCHCORE_PROJECT_DIR=${CMAKE_CURRENT_SOURCE_DIR}
-DCMAKE_VERBOSE_MAKEFILE=${CMAKE_VERBOSE_MAKEFILE}
-DCMAKE_EXPORT_COMPILE_COMMANDS=ON)
# Construct cache args list for subprojects (kernel, user)
macro(chcore_config _config_name _config_type _default _description)
if(NOT DEFINED ${_config_name})
message(
FATAL_ERROR
"Do not run CMake command directly, use `./chbuild` instead")
endif()
list(APPEND _cache_args
-D${_config_name}:${_config_type}=${${_config_name}})
endmacro()
include(${CMAKE_CURRENT_SOURCE_DIR}/config.cmake)
# --- LibC ---
# set(_libc_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/user/chcore-libc)
# set(_libc_build_dir ${CMAKE_CURRENT_BINARY_DIR}/user/chcore-libc)
# Main targets for Libc
# chcore_add_subproject(
# libc
# SOURCE_DIR ${_libc_source_dir}
# BINARY_DIR ${_libc_build_dir}
# INSTALL_DIR ${_libc_install_dir}
# CMAKE_ARGS
# ${_common_args}
# -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
# CMAKE_CACHE_ARGS ${_cache_args}
# INSTALL_COMMAND echo "Performed in CMake"
# BUILD_ALWAYS TRUE)
# Clean target for LibC
# add_custom_target(
# libc-clean-trigger
# WORKING_DIRECTORY ${_libc_build_dir}
# COMMAND [ -d ${_libc_build_dir}/CMakeFiles ] && ${CMAKE_COMMAND} --build . --target libc-clean || true
# COMMAND /bin/rm -rf ${_libc_install_dir})
# --- Clean Ramdisk ---
# Target to re-create ramdisk, must be run before build user-level stuffs
# set(build_ramdisk_dir ${CMAKE_CURRENT_BINARY_DIR}/ramdisk)
# file(REMOVE_RECURSE ${build_ramdisk_dir})
# file(MAKE_DIRECTORY ${build_ramdisk_dir})
# --- Ramdisk ---
# if(CHCORE_CHPM_INSTALL_TO_RAMDISK AND CHCORE_CHPM_INSTALL_PREFIX)
# get_filename_component(_chpm_install_prefix ${CHCORE_CHPM_INSTALL_PREFIX}
# REALPATH BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
# set(_copy_chpm_install_cmd
# "cp -rL `find ${_chpm_install_prefix} -mindepth 1 -maxdepth 1 -type d -not -name include -and -not -name 'chpm-*'` ."
# )
# string(REPLACE " " ";" _copy_chpm_install_cmd ${_copy_chpm_install_cmd})
# else()
# set(_copy_chpm_install_cmd true)
# endif()
# --- User ---
# set(_libs_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/user/chcore-libs)
# set(_libs_build_dir ${CMAKE_CURRENT_BINARY_DIR}/user/chcore-libs)
# set(_libs_install_dir ${_libs_build_dir})
# chcore_add_subproject(
# libs
# SOURCE_DIR ${_libs_source_dir}
# BINARY_DIR ${_libs_build_dir}
# INSTALL_DIR ${_libs_install_dir}
# CMAKE_ARGS
# ${_common_args}
# -DCHCORE_MUSL_LIBC_INSTALL_DIR=${_libc_install_dir} # used by user.cmake toolchain to find `musl-gcc`
# -DCHCORE_RAMDISK_DIR=${build_ramdisk_dir}
# -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
# -DCMAKE_TOOLCHAIN_FILE=${_cmake_script_dir}/Toolchains/user.cmake
# CMAKE_CACHE_ARGS ${_cache_args}
# INSTALL_COMMAND make install
# DEPENDS libc
# BUILD_ALWAYS TRUE)
# set(_apps_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/user/apps)
# set(_apps_build_dir ${CMAKE_CURRENT_BINARY_DIR}/user/apps)
# set(_apps_install_dir ${_apps_build_dir})
# chcore_add_subproject(
# apps
# SOURCE_DIR ${_apps_source_dir}
# BINARY_DIR ${_apps_build_dir}
# INSTALL_DIR ${_apps_install_dir}
# CMAKE_ARGS
# ${_common_args}
# -DCHCORE_MUSL_LIBC_INSTALL_DIR=${_libc_install_dir} # used by user.cmake toolchain to find `musl-gcc`
# -DCHCORE_RAMDISK_DIR=${build_ramdisk_dir}
# -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
# -DCMAKE_TOOLCHAIN_FILE=${_cmake_script_dir}/Toolchains/user.cmake
# CMAKE_CACHE_ARGS ${_cache_args}
# INSTALL_COMMAND echo "Nothing to install"
# DEPENDS libs
# BUILD_ALWAYS TRUE)
# set(_system_services_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/user/system-services)
# set(_system_services_build_dir ${CMAKE_CURRENT_BINARY_DIR}/user/system-services)
# set(_system_services_install_dir ${_system_services_build_dir})
# set(_ci_config_dir ${CMAKE_CURRENT_SOURCE_DIR}/scripts/ci)
# We embed a binary into other binaries by creating a source file named incbin_*.S, such files
# need to be re-generated per compilation. So we have to remove them from build dir every time. And that's why it is not included in system-services-clean-trigger
# add_custom_target(
# system-services-clean-incbin
# COMMAND
# [ -d ${_system_services_build_dir} ]
# && find ${_system_services_build_dir} -type f -name "incbin_*.S.*" | xargs rm -f
# || true)
# Clean target for system services
# add_custom_target(
# system-services-clean-trigger
# WORKING_DIRECTORY ${_system_services_build_dir}
# COMMAND [ -d ${_system_services_build_dir}/CMakeFiles ] && ${CMAKE_COMMAND} --build . --target system-services-clean || true
# )
# chcore_add_subproject(
# system-services
# SOURCE_DIR ${_system_services_source_dir}
# BINARY_DIR ${_system_services_build_dir}
# INSTALL_DIR ${_system_services_install_dir}
# CMAKE_ARGS
# ${_common_args}
# -DCHCORE_MUSL_LIBC_INSTALL_DIR=${_libc_install_dir} # used by user.cmake toolchain to find `musl-gcc`
# -DCHCORE_RAMDISK_DIR=${build_ramdisk_dir}
# -DCHCORE_CI_CONFIG_DIR=${_ci_config_dir}
# -DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
# -DCMAKE_TOOLCHAIN_FILE=${_cmake_script_dir}/Toolchains/user.cmake
# CMAKE_CACHE_ARGS ${_cache_args}
# INSTALL_COMMAND echo "Nothing to install"
# DEPENDS apps system-services-clean-incbin
# BUILD_ALWAYS TRUE)
# --- Kernel ---
set(_kernel_source_dir ${CMAKE_CURRENT_SOURCE_DIR}/kernel)
set(_kernel_build_dir ${CMAKE_CURRENT_BINARY_DIR}/kernel)
set(_kernel_install_dir ${CMAKE_CURRENT_BINARY_DIR})
# Target to force re-including cpio binaries
add_custom_target(
kernel-clean-incbin
COMMAND
[ -d ${_kernel_build_dir}/CMakeFiles ] && find
${_kernel_build_dir}/CMakeFiles -type f -name "incbin_*.S.*" | xargs rm
-f || true)
# Main targets for kernel
chcore_add_subproject(
kernel
SOURCE_DIR ${_kernel_source_dir}
BINARY_DIR ${_kernel_build_dir}
INSTALL_DIR ${_kernel_install_dir}
CMAKE_ARGS
${_common_args}
-DCHCORE_USER_INSTALL_DIR=${_system_services_install_dir} # used by kernel/CMakeLists.txt to incbin cpio files
-DCMAKE_INSTALL_PREFIX=<INSTALL_DIR>
-DCMAKE_TOOLCHAIN_FILE=${_cmake_script_dir}/Toolchains/kernel.cmake
CMAKE_CACHE_ARGS ${_cache_args}
DEPENDS kernel-clean-incbin
BUILD_ALWAYS TRUE)
# Generate toolchain file
configure_file(${_cmake_script_dir}/Toolchains/toolchain_tpl.cmake
toolchain.cmake
@ONLY)
# Re-generate ramdisk
# add_custom_target(
# rebuildproc
# WORKING_DIRECTORY ${_system_services_build_dir}
# COMMAND [ -d ${_system_services_build_dir}/CMakeFiles ] && ${CMAKE_COMMAND} --build . --target buildram || true
# )
# add_custom_target(
# rambuild
# WORKING_DIRECTORY ${_kernel_build_dir}
# COMMAND [ -d ${_kernel_build_dir}/CMakeFiles ] && ${CMAKE_COMMAND} --build . --target rebuildkernel || true
# DEPENDS rebuildproc
# )
# --- Clean All ---
add_custom_target(
clean-all
)
# add_dependencies(clean-all libc-clean-trigger system-services-clean-trigger)
# --- Update Submodules (always run in local env) ---
add_custom_target(update-submodules)
macro(_update_submodule _target _subproject_path _module_rel_path)
add_custom_target(
${_target}
WORKING_DIRECTORY ${_subproject_path}
COMMAND echo "Updating ${_subproject_path}/${_module_rel_path}..."
COMMAND git submodule update --init
--recursive --depth=1 ${_module_rel_path})
add_dependencies(update-submodules ${_target})
endmacro()
# _update_submodule(update-musl-libc ${_system_services_source_dir} chcore-libc/musl-libc)
if(CHCORE_DRIVER_FWK_LINUX)
_update_submodule(update-linux-port ${_system_services_source_dir} system-servers/drivers/linux-port)
endif()
if(CHCORE_DRIVER_FWK_CIRCLE)
_update_submodule(update-circle ${_system_services_source_dir} system-servers/drivers/raspi/circle)
endif()
if(CHCORE_CROSS_COMPILE MATCHES "riscv64")
_update_submodule(opensbi ${_kernel_source_dir} arch/riscv64/boot/opensbi)
endif()