forked from Demon000/work-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·421 lines (338 loc) · 8.42 KB
/
build.sh
File metadata and controls
executable file
·421 lines (338 loc) · 8.42 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
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
#!/bin/bash
POSITIONAL_ARGS=()
print_usage() {
echo "usage: $0 <board> [options] [target]"
echo "board:"
echo " rpi3: Raspberry Pi 3 32bit"
echo " rpi4: Raspberry Pi 4 32bit"
echo " rpi4-64: Raspberry Pi 4 64bit"
echo " nv: Nvidia"
echo " arm64: Basic ARM64 build"
echo "options:"
echo " -d|--dtbs: build dtbs"
echo " -m|--modules <modules_path>: build dtbs and install them at the given path"
echo " -h|--headers <headers_path>: build headers and install them at the given path"
echo " -s|--source <source_path>: kernel source path, use current directory if not given"
echo " -o|--out <out_path>: kernel output path, use source path if not given"
echo " -l|--localversion <localversion>: kernel local version"
exit 1
}
while [[ $# -gt 0 ]]; do
case $1 in
-d|--dtbs)
DTBS=1
shift
;;
-m|--modules)
MODULES_PATH="$2"
shift
shift
;;
-h|--headers)
HEADERS_PATH="$2"
shift
shift
;;
-s|--source)
SOURCE_PATH="$2"
shift
shift
;;
-o|--out)
KERNEL_OUT_PATH="$2"
shift
shift
;;
-l|--localversion)
KERNEL_LOCALVERSION="$2"
shift
shift
;;
-*|--*)
echo "Unknown option $1"
print_usage
exit 1
;;
*)
POSITIONAL_ARGS+=("$1")
shift
;;
esac
done
set -- "${POSITIONAL_ARGS[@]}"
if [[ $# -lt 1 ]]; then
print_usage
fi
BOARD_TYPE="$1"
shift
IMAGE_TARGET="Image"
ZIMAGE_TARGET="zImage"
DTBS_TARGET="dtbs"
NV_DISPLAY_TARGET="nv_display"
NV_SUPPLEMENTS_TARGET="nv_supplements"
MODULES_TARGET="modules"
HEADERS_TARGET="headers"
MODULES_INSTALL_TARGET="modules_install"
HEADERS_INSTALL_TARGET="headers_install"
TARGETS=("$@")
if [[ -z "$TARGETS" ]]; then
AUTO_TARGETS="1"
fi
if [[ -n "$AUTO_TARGETS" ]]; then
if [[ "$BOARD_TYPE" = "rpi3" ]]; then
TARGETS+=("$ZIMAGE_TARGET")
elif [[ "$BOARD_TYPE" = "rpi4" ]]; then
TARGETS+=("$ZIMAGE_TARGET")
elif [[ "$BOARD_TYPE" = "rpi4-64" ]]; then
TARGETS+=("$IMAGE_TARGET")
elif [[ "$BOARD_TYPE" = "nv" ]]; then
TARGETS+=("$IMAGE_TARGET")
TARGETS+=("$NV_DISPLAY_TARGET")
if [[ -z "$KERNEL_LOCALVERSION" ]]; then
KERNEL_LOCALVERSION="-tegra"
fi
elif [[ "$BOARD_TYPE" = "arm64" ]]; then
TARGETS+=("$IMAGE_TARGET")
fi
if [[ -n "$MODULES_PATH" ]]; then
TARGETS+=("$MODULES_TARGET")
TARGETS+=("$MODULES_INSTALL_TARGET")
fi
if [[ -n "$HEADERS_PATH" ]]; then
TARGETS+=("$HEADERS_TARGET")
TARGETS+=("$HEADERS_INSTALL_TARGET")
fi
if [[ -n "$DTBS" ]]; then
TARGETS+=("$DTBS_TARGET")
fi
fi
O_OPT=()
if [[ "$BOARD_TYPE" = "rpi3" ]]; then
O_OPT+=(ARCH="arm")
O_OPT+=(KERNEL="kernel7")
elif [[ "$BOARD_TYPE" = "rpi4" ]]; then
O_OPT+=(ARCH="arm")
O_OPT+=(KERNEL="kernel7l")
elif [[ "$BOARD_TYPE" = "rpi4-64" ]]; then
O_OPT+=(ARCH="arm64")
O_OPT+=(KERNEL="kernel8")
elif [[ "$BOARD_TYPE" = "nv" ]]; then
O_OPT+=(ARCH="arm64")
elif [[ "$BOARD_TYPE" = "arm64" ]]; then
O_OPT+=(ARCH="arm64")
fi
if [[ -n "$KERNEL_LOCALVERSION" ]]; then
O_OPT+=(LOCALVERSION="$KERNEL_LOCALVERSION")
fi
if [[ -n "$SOURCE_PATH" ]]; then
O_OPT+=(-C "${SOURCE_PATH}")
fi
if [[ -z "$SOURCE_PATH" ]]; then
SOURCE_PATH="."
fi
if [[ -n "$KERNEL_OUT_PATH" ]]; then
O_OPT+=(O="${KERNEL_OUT_PATH}")
fi
if [[ -z "$KERNEL_OUT_PATH" ]]; then
KERNEL_OUT_PATH="$SOURCE_PATH"
fi
if [[ -n "$CROSS_COMPILE" ]]; then
O_OPT+=(CROSS_COMPILE="$CROSS_COMPILE")
fi
O_OPT+=(-j$(nproc))
echo "Targets: ${TARGETS[@]}"
echo "Options: ${O_OPT[@]}"
INITIAL_TARGETS=()
for TARGET in "${TARGETS[@]}"
do
case "$TARGET" in
"$MODULES_TARGET")
BUILD_MODULES=1
;;
"$HEADERS_TARGET")
BUILD_HEADERS=1
;;
"$NV_DISPLAY_TARGET")
BUILD_NV_DISPLAY=1
;;
"$NV_SUPPLEMENTS_TARGET")
BUILD_NV_SUPPLEMENTS=1
;;
"$MODULES_INSTALL_TARGET")
INSTALL_MODULES=1
;;
"$HEADERS_INSTALL_TARGET")
INSTALL_HEADERS=1
;;
*)
INITIAL_TARGETS+=("$TARGET")
;;
esac
done
TARGETS=("${INITIAL_TARGETS[@]}")
START_TIME=$(date +%s.%N)
if [[ ${#TARGETS[@]} -ne 0 ]]; then
make "${O_OPT[@]}" "${TARGETS[@]}"
if [[ $? -ne 0 ]]; then
exit
fi
fi
NV_DISPLAY_PATH="../../tegra/kernel-src/nv-kernel-display-driver/NVIDIA-kernel-module-source-TempVersion"
KERNEL_SRC_PATH_ABS=$(realpath "$SOURCE_PATH")
KERNEL_OUT_PATH_ABS=$(realpath "$KERNEL_OUT_PATH")
NV_DISPLAY_O_OPT=(
"SYSSRC=$KERNEL_SRC_PATH_ABS"
"SYSOUT=$KERNEL_OUT_PATH_ABS"
"CC=${CROSS_COMPILE}gcc"
"LD=${CROSS_COMPILE}ld"
"AR=${CROSS_COMPILE}ar"
"CXX=${CROSS_COMPILE}g++"
"OBJCOPY=${CROSS_COMPILE}objcopy"
"TARGET_ARCH=aarch64"
)
build_modules() {
local dir_path="$1"
shift
local opts=("$@")
if [[ -n "$dir_path" ]]; then
pushd "$dir_path"
fi
make "${O_OPT[@]}" "${opts[@]}" "$MODULES_TARGET"
if [[ $? -ne 0 ]]; then
exit
fi
if [[ -n "$dir_path" ]]; then
popd
fi
}
build_nv_display_modules() {
build_modules "$NV_DISPLAY_PATH" "${NV_DISPLAY_O_OPT[@]}"
}
build_headers() {
make "${O_OPT[@]}" "$HEADERS_TARGET"
if [[ $? -ne 0 ]]; then
exit
fi
}
install_modules() {
local modules_path="$1"
shift
local clean_dir="$1"
shift
local dir_path="$1"
shift
local opts=("$@")
local mod_path_arg
local mod_strip_arg
if [[ -n "$modules_path" ]]; then
if [[ -n "$clean_dir" ]]; then
rm -rf "$modules_path"
fi
mkdir -p "$modules_path"
modules_path_abs=$(realpath "$modules_path")
mod_path_arg="INSTALL_MOD_PATH=${modules_path_abs}"
mod_strip_arg="INSTALL_MOD_STRIP=1"
fi
if [[ -n "$dir_path" ]]; then
pushd "$dir_path"
fi
make "${O_OPT[@]}" "${opts[@]}" "$mod_path_arg" "$mod_strip_arg" "$MODULES_INSTALL_TARGET"
if [[ $? -ne 0 ]]; then
exit
fi
if [[ -n "$dir_path" ]]; then
popd
fi
}
install_kernel_modules() {
local modules_path="$1"
shift
install_modules "$modules_path" 1
}
install_nv_display_modules() {
local modules_path="$1"
shift
# To get the same output as Nvidia does when installing the display
# module, set INSTALL_MOD_DIR line in kernel-open/Makefile to
# KBUILD_PARAMS += INSTALL_MOD_DIR=extra/opensrc-disp
install_modules "$modules_path" "" "$NV_DISPLAY_PATH" "${NV_DISPLAY_O_OPT[@]}"
}
install_headers() {
local headers_path="$1"
shift
local hdr_path_arg
if [[ -n "$headers_path" ]]; then
rm -rf "$headers_path"
mkdir -p "$headers_path"
headers_path_abs=$(realpath "$headers_path")
hdr_path_arg="INSTALL_HDR_PATH=${headers_path_abs}"
fi
make "${O_OPT[@]}" "$hdr_path_arg" "$HEADERS_INSTALL_TARGET"
if [[ $? -ne 0 ]]; then
exit
fi
}
package_supplements() {
local supplements_path="$1"
shift
local name="$1"
shift
local subpath="$1"
shift
pushd "$supplements_path"
tar --owner root --group root -cjf "$name" "$subpath"
popd
}
build_nv_supplements() {
local supplements_path="../../../kernel/"
local image_src_path="$KERNEL_OUT_PATH/arch/arm64/boot/Image"
local image_dst_path="$supplements_path/Image"
cp "$image_src_path" "$image_dst_path"
local dtb_src_path="$KERNEL_OUT_PATH/arch/arm64/boot/dts/nvidia"
local dtb_dst_path="$supplements_path/dtb"
mkdir -p "$dtb_dst_path"
cp -r "$dtb_src_path"/* "$dtb_dst_path"
local kernel_supplements_dir_path=$(mktemp -d)
local kernel_supplements_name="kernel_supplements.tbz2"
install_kernel_modules "$kernel_supplements_dir_path"
package_supplements "$kernel_supplements_dir_path" \
"$kernel_supplements_name" "lib/modules"
cp "$kernel_supplements_dir_path/$kernel_supplements_name" \
"$supplements_path/$kernel_supplements_name"
rm -rf "$kernel_supplements_dir_path"
local kernel_display_supplements_dir_path=$(mktemp -d)
local kernel_display_supplements_name="kernel_display_supplements.tbz2"
install_nv_display_modules "$kernel_display_supplements_dir_path"
package_supplements "$kernel_display_supplements_dir_path" \
"$kernel_display_supplements_name" "lib/modules"
cp "$kernel_display_supplements_dir_path/$kernel_display_supplements_name" \
"$supplements_path/$kernel_display_supplements_name"
rm -rf "$kernel_display_supplements_dir_path"
}
if [[ -n "$BUILD_MODULES" ]]; then
build_modules
if [[ -n "$BUILD_NV_DISPLAY" ]]; then
build_nv_display_modules
fi
fi
if [[ -n "$BUILD_HEADERS" ]]; then
build_headers
fi
if [[ -n "$INSTALL_MODULES" ]]; then
install_kernel_modules "$MODULES_PATH"
if [[ -n "$BUILD_NV_DISPLAY" ]]; then
# To get the same output as Nvidia does when installing the display
# module, set INSTALL_MOD_DIR line in kernel-open/Makefile to
# KBUILD_PARAMS += INSTALL_MOD_DIR=extra/opensrc-disp
install_nv_display_modules "$MODULES_PATH"
fi
fi
if [[ -n "$INSTALL_HEADERS" ]]; then
install_headers "$HEADERS_PATH"
fi
if [[ -n "$BUILD_NV_SUPPLEMENTS" ]]; then
build_nv_supplements
fi
END_TIME=$(date +%s.%N)
RUN_TIME=$(echo "$END_TIME - $START_TIME" | bc -l)
echo "Run time: $RUN_TIME"