-
Notifications
You must be signed in to change notification settings - Fork 0
/
chbuild
executable file
·226 lines (183 loc) · 5.17 KB
/
chbuild
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
#!/bin/bash
set -e
self=$0
defconfig_command="$self defconfig"
clean_command="$self clean"
cmake_build_dir="build"
cmake_cache_file="$cmake_build_dir/CMakeCache.txt"
cmake_userland_build_dir="userland/_build"
cmake_kernel_build_dir="kernel/_build"
cmake_libchcore_build_dirs="libchcore/_build libchcore/_install"
cmake_script_dir="scripts/build/cmake"
cmake_init_cache_default="$cmake_script_dir/LoadConfigDefault.cmake"
cmake_init_cache_ask="$cmake_script_dir/LoadConfigAsk.cmake"
cmake_init_cache_abort="$cmake_script_dir/LoadConfigAbort.cmake"
cmake_init_cache_dump="$cmake_script_dir/DumpConfig.cmake"
config_file=".config"
defconfig_file="scripts/build/default.config"
RED='\033[0;31m'
BLUE='\033[0;34m'
GREEN='\033[0;32m'
ORANGE='\033[0;33m'
BOLD='\033[1m'
NONE='\033[0m'
_echo_info() {
echo -e "${BOLD}$@${NONE}"
}
_echo_succ() {
echo -e "${GREEN}${BOLD}$@${NONE}"
}
_echo_warn() {
echo -e "${ORANGE}${BOLD}$@${NONE}"
}
_echo_err() {
echo -e "${RED}${BOLD}$@${NONE}"
}
defconfig() {
if [ -d $cmake_build_dir ]; then
_echo_err "There exists a build directory, please run \`$clean_command\` first"
exit 1
fi
if [ -z "$1" ]; then
plat="raspi3"
else
plat="$1"
fi
_echo_info "Generating default config file..."
cp $defconfig_file $config_file
_config_default
_sync_config_with_cache
_echo_succ "Default config written to \`$config_file\` file."
}
_check_config_file() {
if [ ! -f $config_file ]; then
_echo_err "There is no \`.config\` file, please run \`$defconfig_command\` first"
exit 1
fi
}
_config_default() {
_echo_info "Configuring CMake..."
cmake -B $cmake_build_dir -C $cmake_init_cache_default
}
_config_ask() {
_echo_info "Configuring CMake..."
cmake -B $cmake_build_dir -C $cmake_init_cache_ask
}
_sync_config_with_cache() {
cmake -N -B $cmake_build_dir -C $cmake_init_cache_dump >/dev/null
}
config() {
_check_config_file
_config_ask
_sync_config_with_cache
_echo_succ "Config syned to \`$config_file\` file."
}
menuconfig() {
_check_config_file
_config_default
echo
_echo_warn "Note: In the menu config view, press C to save, Q to quit."
read -p "Now press Enter to continue..."
ccmake -B $cmake_build_dir
_sync_config_with_cache
_echo_succ "Config saved to \`$config_file\` file."
}
build() {
_check_config_file
_config_ask
_sync_config_with_cache
_echo_succ "Config syned to \`$config_file\` file."
_echo_info "Building..."
cmake --build $cmake_build_dir --target all --parallel $(nproc)
_echo_succ "Succeeded to build all targets"
}
clean() {
_echo_info "Cleaning..."
if [ -f $cmake_cache_file ]; then
cmake --build $cmake_build_dir --target clean-all
rm -rf $cmake_build_dir
_echo_succ "Succeeded to clean all targets"
elif [ -d $cmake_build_dir ]; then
rm -rf $cmake_build_dir $cmake_userland_build_dir $cmake_kernel_build_dir $cmake_libchcore_build_dirs
_echo_warn "Succeeded to clean all targets, but some object files may be left"
else
_echo_info "Nothing to clean"
fi
}
rebuild() {
clean
build
}
distclean() {
clean
_echo_info "Removing config file..."
rm -rf $config_file
_echo_succ "Succeeded to distclean"
}
_print_help() {
echo -e "\
${BOLD}USAGE:${NONE}
${BOLD}$self [options] [command]${NONE}
${BOLD}OPTIONS:${NONE}
${BOLD}--local, -l${NONE} run command in local environment (rather than docker)
${BOLD}COMMANDS:${NONE}
Local:
${BOLD}help, --help, -h${NONE} print this help text
Local or Docker:
${BOLD}defconfig${NONE} generate default config
${BOLD}config${NONE} run configuration step (interactively ask for config value if not set)
${BOLD}menuconfig${NONE} use TUI menu to edit config
${BOLD}build${NONE} build the project
${BOLD}clean${NONE} clean the project
${BOLD}rebuild${NONE} clean and build the project
${BOLD}distclean${NONE} clean the project and remove config file
"
}
_docker_run() {
if [ -f /.dockerenv ]; then
# we are already in docker container
$@
else
test -t 1 && use_tty="-t"
docker run -i $use_tty --rm \
-u $(id -u ${USER}):$(id -g ${USER}) \
-v $(pwd):/chos -w /chos \
ipads/chcore_builder:v1.3 \
$self $@
fi
}
_main() {
run_in_docker=true
while [ $# -gt 0 ]; do
case $1 in
help | --help | -h)
_print_help
exit
;;
--local | -l)
run_in_docker=false
;;
-*)
_echo_err "$self: invalid option \`$1\`\n"
break
;;
*)
if [[ "$1" == "_"* || $(type -t "$1") != function ]]; then
_echo_err "$self: invalid command \`$1\`\n"
break
fi
if [[ $run_in_docker == true ]]; then
_docker_run $@
else
$@
fi
exit
;;
esac
shift
done
# no command is run
_print_help
exit 1
}
_main $@