forked from lucianovk/jetson-gs_usb-kernel-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjetson-kernel-builder.sh
More file actions
executable file
·741 lines (636 loc) · 23.9 KB
/
Copy pathjetson-kernel-builder.sh
File metadata and controls
executable file
·741 lines (636 loc) · 23.9 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
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
#!/bin/bash
set -euo pipefail
# ==============================================================================
# Jetson kernel module builder
#
# Supports:
# 1) Native build on Jetson
# 2) Cross-compile on PC
#
# Features:
# - Native Jetson: auto-discovers L4T version unless --kernel-version is given
# - Cross-compile: requires --kernel-version
# - Builds multiple modules from a modules.txt file
# - Prints parsed module entries during validation
# - Supports dry-run mode
# ==============================================================================
# Colors for formatting
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m'
# ------------------------------------------------------------------------------
# Formatting helpers
# ------------------------------------------------------------------------------
print_header() {
echo -e "${YELLOW}\n===== $1 =====${NC}"
}
print_step() {
echo -e "${GREEN}[STEP]${NC} $1"
}
print_info() {
echo -e "[INFO] $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
usage() {
cat <<EOF
Usage:
$0 [--kernel-version VERSION] [--modules-file FILE] [--dry-run]
Options:
--kernel-version VERSION Target L4T version (example: 36.4.3)
--modules-file FILE Module definition file (default: modules.txt)
--dry-run Print what would be done without making changes
-h, --help Show this help
Behavior:
Native Jetson:
- defaults to discovered Jetson L4T version
- can be overridden with --kernel-version
Cross-compile on PC:
- requires --kernel-version
Module file format:
One module per line:
module_name|config_symbol|module_dir
Example:
gs_usb|CONFIG_CAN_GS_USB|drivers/net/can/usb
peak_usb|CONFIG_CAN_PEAK_USB|drivers/net/can/usb
Notes:
- Lines starting with # are ignored
- Blank lines are ignored
EOF
}
# ------------------------------------------------------------------------------
# Globals / defaults
# ------------------------------------------------------------------------------
ARCHITECTURE="$(uname -m)"
IS_ARM64=0
if [[ "$ARCHITECTURE" == "aarch64" || "$ARCHITECTURE" == "arm64" ]]; then
IS_ARM64=1
fi
KERNEL_VERSION=""
MODULES_FILE="modules.txt"
SCRIPT_DIR="$(pwd)"
SOURCES_DIR="$SCRIPT_DIR/sources"
DRY_RUN=0
# Parsed module entries: "module_name|config_symbol|module_dir"
declare -a MODULE_ENTRIES=()
# Resolved later by lookup
NVIDIA_RELEASE_PATH=""
PUBLIC_SOURCES_URL=""
TOOLCHAIN_URL=""
TOOLCHAIN_ARCHIVE_NAME="aarch64--glibc--stable-2022.08-1.tar.bz2"
TOOLCHAIN_DIR_NAME="aarch64--glibc--stable-2022.08-1"
# Build paths resolved later
SRC_PATH=""
OUT_PATH=""
BUILD_ROOT=""
MODULE_INSTALL_BASE=""
TARGET_UNAME_R=""
# ------------------------------------------------------------------------------
# Command helpers
# ------------------------------------------------------------------------------
run_cmd() {
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] $*"
else
"$@"
fi
}
# ------------------------------------------------------------------------------
# Argument parsing
# ------------------------------------------------------------------------------
while [[ $# -gt 0 ]]; do
case "$1" in
--kernel-version)
if [[ $# -lt 2 ]]; then
print_error "--kernel-version requires a value"
exit 1
fi
KERNEL_VERSION="$2"
shift 2
;;
--modules-file)
if [[ $# -lt 2 ]]; then
print_error "--modules-file requires a value"
exit 1
fi
MODULES_FILE="$2"
shift 2
;;
--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
usage
exit 0
;;
*)
print_error "Unknown argument: $1"
usage
exit 1
;;
esac
done
# ------------------------------------------------------------------------------
# Helpers
# ------------------------------------------------------------------------------
discover_l4t_version() {
# Preferred source on Jetson
if [[ -f /etc/nv_tegra_release ]]; then
sed -n 's/.*R\([0-9]\+\) (release), REVISION: \([0-9]\+\)\.\([0-9]\+\).*/\1.\2.\3/p' /etc/nv_tegra_release | head -n1
return 0
fi
return 1
}
validate_kernel_version_format() {
local version="$1"
if [[ ! "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
print_error "Invalid kernel/L4T version format: $version"
print_error "Expected format: MAJOR.MINOR.PATCH (example: 36.4.3)"
exit 1
fi
}
resolve_release_info() {
local version="$1"
# Explicit mapping is safer than generating URLs blindly.
case "$version" in
36.4.3)
NVIDIA_RELEASE_PATH="r36_release_v4.3"
PUBLIC_SOURCES_URL="https://developer.nvidia.com/downloads/embedded/l4t/r36_release_v4.3/sources/public_sources.tbz2"
TOOLCHAIN_URL="https://developer.nvidia.com/downloads/embedded/l4t/r36_release_v3.0/toolchain/aarch64--glibc--stable-2022.08-1.tar.bz2"
;;
36.4.4)
NVIDIA_RELEASE_PATH="r36_release_v4.4"
PUBLIC_SOURCES_URL="https://developer.nvidia.com/downloads/embedded/l4t/r36_release_v4.4/sources/public_sources.tbz2"
TOOLCHAIN_URL="https://developer.nvidia.com/downloads/embedded/l4t/r36_release_v3.0/toolchain/aarch64--glibc--stable-2022.08-1.tar.bz2"
;;
*)
print_error "Unsupported kernel/L4T version: $version"
print_error "Add it to resolve_release_info() after verifying the NVIDIA public sources and toolchain paths."
exit 1
;;
esac
}
check_url_exists() {
local url="$1"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] wget --spider -q \"$url\""
return 0
fi
wget --spider -q "$url"
}
validate_modules_file() {
local file="$1"
if [[ ! -f "$file" ]]; then
print_error "Modules file not found: $file"
exit 1
fi
local line_no=0
local valid_count=0
MODULE_ENTRIES=()
while IFS= read -r line || [[ -n "$line" ]]; do
line_no=$((line_no + 1))
# Trim leading/trailing whitespace
local trimmed
trimmed="$(echo "$line" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
[[ -z "$trimmed" ]] && continue
[[ "$trimmed" =~ ^# ]] && continue
IFS='|' read -r module_name config_symbol module_dir extra <<< "$trimmed"
# Trim each field
module_name="$(echo "${module_name:-}" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
config_symbol="$(echo "${config_symbol:-}" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
module_dir="$(echo "${module_dir:-}" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
extra="$(echo "${extra:-}" | sed 's/^[[:space:]]*//; s/[[:space:]]*$//')"
# Remove CR if file uses CRLF line endings
module_name="${module_name%$'\r'}"
config_symbol="${config_symbol%$'\r'}"
module_dir="${module_dir%$'\r'}"
extra="${extra%$'\r'}"
if [[ -n "$extra" || -z "$module_name" || -z "$config_symbol" || -z "$module_dir" ]]; then
print_error "Invalid modules file entry at line $line_no: $line"
print_error "Expected format: module_name|config_symbol|module_dir"
exit 1
fi
MODULE_ENTRIES+=("${module_name}|${config_symbol}|${module_dir}")
valid_count=$((valid_count + 1))
done < "$file"
if [[ $valid_count -eq 0 ]]; then
print_error "No valid module entries found in $file"
exit 1
fi
print_info "Modules found in $file:"
local idx=0
local entry
for entry in "${MODULE_ENTRIES[@]}"; do
idx=$((idx + 1))
local module_name config_symbol module_dir
IFS='|' read -r module_name config_symbol module_dir <<< "$entry"
print_info " [$idx] module_name=$module_name | config_symbol=$config_symbol | module_dir=$module_dir"
done
}
enable_module_config() {
local config_file="$1"
local config_symbol="$2"
if grep -q "^${config_symbol}=m$" "$config_file"; then
print_info "${config_symbol} already set to module"
return 0
fi
if grep -q "^${config_symbol}=y$" "$config_file"; then
print_step "Changing ${config_symbol}=y to ${config_symbol}=m"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sed -i \"s/^${config_symbol}=y$/${config_symbol}=m/\" \"$config_file\""
else
sed -i "s/^${config_symbol}=y$/${config_symbol}=m/" "$config_file"
fi
return 0
fi
if grep -q "^# ${config_symbol} is not set$" "$config_file"; then
print_step "Enabling ${config_symbol}=m"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sed -i \"s/^# ${config_symbol} is not set$/${config_symbol}=m/\" \"$config_file\""
else
sed -i "s/^# ${config_symbol} is not set$/${config_symbol}=m/" "$config_file"
fi
return 0
fi
if grep -q "^${config_symbol}=" "$config_file"; then
print_step "Updating ${config_symbol} to module"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sed -i \"s/^${config_symbol}=.*/${config_symbol}=m/\" \"$config_file\""
else
sed -i "s/^${config_symbol}=.*/${config_symbol}=m/" "$config_file"
fi
return 0
fi
print_step "Appending ${config_symbol}=m"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] echo \"${config_symbol}=m\" >> \"$config_file\""
else
echo "${config_symbol}=m" >> "$config_file"
fi
}
install_dependencies() {
print_header "INSTALLING DEPENDENCIES"
if ! command -v apt-get >/dev/null 2>&1; then
print_error "Non-apt based system detected. Install dependencies manually."
echo "Required packages:"
echo " build-essential bc libssl-dev flex bison wget git pv kmod ca-certificates libelf-dev"
exit 1
fi
print_step "Updating package list..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sudo apt-get update -qq"
else
sudo apt-get update -qq
fi
print_step "Installing required packages..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sudo apt-get install -qq -y build-essential bc libssl-dev flex bison wget git pv kmod ca-certificates libelf-dev"
else
sudo apt-get install -qq -y \
build-essential \
bc \
libssl-dev \
flex \
bison \
wget \
git \
pv \
kmod \
ca-certificates \
libelf-dev
fi
print_success "Dependencies installed"
}
prepare_kernel_config() {
print_header "KERNEL CONFIGURATION"
if [[ $IS_ARM64 -eq 1 ]]; then
if [[ ! -f "$SCRIPT_DIR/config" ]]; then
print_step "Generating kernel config from running Jetson..."
if [[ -f /proc/config.gz ]]; then
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] zcat /proc/config.gz > \"$SCRIPT_DIR/config\""
else
zcat /proc/config.gz > "$SCRIPT_DIR/config"
fi
else
print_error "/proc/config.gz not found on this Jetson"
print_error "Provide a config file manually at: $SCRIPT_DIR/config"
exit 1
fi
print_success "Configuration generated: $SCRIPT_DIR/config"
else
print_info "Using existing config file: $SCRIPT_DIR/config"
fi
else
if [[ ! -f "$SCRIPT_DIR/config" ]]; then
print_error "Cross-compile mode requires a config file copied from target Jetson:"
print_error "Expected: $SCRIPT_DIR/config"
exit 1
fi
print_info "Using provided target config file: $SCRIPT_DIR/config"
fi
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] cp \"$SCRIPT_DIR/config\" \"$OUT_PATH/.config\""
else
cp "$SCRIPT_DIR/config" "$OUT_PATH/.config"
fi
print_success "Copied config to $OUT_PATH/.config"
print_step "Enabling requested module configs..."
local entry module_name config_symbol module_dir
for entry in "${MODULE_ENTRIES[@]}"; do
IFS='|' read -r module_name config_symbol module_dir <<< "$entry"
enable_module_config "$OUT_PATH/.config" "$config_symbol"
done
}
download_and_extract_sources() {
print_header "DOWNLOADING PUBLIC SOURCES"
print_info "Resolved NVIDIA release path: $NVIDIA_RELEASE_PATH"
print_info "Public sources URL: $PUBLIC_SOURCES_URL"
if ! check_url_exists "$PUBLIC_SOURCES_URL"; then
print_error "Public sources URL does not exist or is not reachable:"
print_error "$PUBLIC_SOURCES_URL"
exit 1
fi
if [[ ! -f "$SRC_PATH/public_sources.tbz2" ]]; then
print_step "Downloading public_sources.tbz2..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] wget --show-progress -q -O \"$SRC_PATH/public_sources.tbz2\" \"$PUBLIC_SOURCES_URL\""
else
wget --show-progress -q -O "$SRC_PATH/public_sources.tbz2" "$PUBLIC_SOURCES_URL"
fi
print_success "Downloaded public_sources.tbz2"
else
print_info "public_sources.tbz2 already exists, skipping download"
fi
print_header "EXTRACTING PUBLIC SOURCES"
if [[ ! -d "$SRC_PATH/Linux_for_Tegra/source" ]]; then
print_step "Extracting public_sources.tbz2..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] (cd \"$SRC_PATH\" && pv public_sources.tbz2 | tar xjf -)"
else
(
cd "$SRC_PATH"
pv public_sources.tbz2 | tar xjf -
)
fi
print_success "Extraction complete"
else
print_info "Linux_for_Tegra/source already extracted"
fi
if [[ $DRY_RUN -eq 0 && ! -f "$SRC_PATH/Linux_for_Tegra/source/kernel_src.tbz2" ]]; then
print_error "kernel_src.tbz2 not found after extracting public sources"
exit 1
fi
if [[ ! -d "$SRC_PATH/Linux_for_Tegra/source/kernel/kernel-jammy-src" ]]; then
print_step "Extracting kernel_src.tbz2..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] (cd \"$SRC_PATH/Linux_for_Tegra/source\" && tar xf kernel_src.tbz2)"
else
(
cd "$SRC_PATH/Linux_for_Tegra/source"
tar xf kernel_src.tbz2
)
fi
print_success "Kernel sources extracted"
else
print_info "kernel-jammy-src already extracted"
fi
}
setup_toolchain_if_needed() {
print_header "TOOLCHAIN CONFIGURATION"
if [[ $IS_ARM64 -eq 1 ]]; then
print_info "Native Jetson build: toolchain setup not required"
return 0
fi
print_info "Toolchain URL: $TOOLCHAIN_URL"
if ! check_url_exists "$TOOLCHAIN_URL"; then
print_error "Toolchain URL does not exist or is not reachable:"
print_error "$TOOLCHAIN_URL"
exit 1
fi
if [[ ! -f "$SRC_PATH/$TOOLCHAIN_ARCHIVE_NAME" ]]; then
print_step "Downloading cross-compilation toolchain..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] wget --show-progress -q -O \"$SRC_PATH/$TOOLCHAIN_ARCHIVE_NAME\" \"$TOOLCHAIN_URL\""
else
wget --show-progress -q -O "$SRC_PATH/$TOOLCHAIN_ARCHIVE_NAME" "$TOOLCHAIN_URL"
fi
print_success "Toolchain downloaded"
else
print_info "Toolchain archive already exists"
fi
if [[ ! -d "$SRC_PATH/$TOOLCHAIN_DIR_NAME" ]]; then
print_step "Extracting toolchain..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] (cd \"$SRC_PATH\" && tar xf \"$TOOLCHAIN_ARCHIVE_NAME\")"
else
(
cd "$SRC_PATH"
tar xf "$TOOLCHAIN_ARCHIVE_NAME"
)
fi
print_success "Toolchain extracted"
else
print_info "Toolchain already extracted"
fi
export CROSS_COMPILE="$SRC_PATH/$TOOLCHAIN_DIR_NAME/bin/aarch64-buildroot-linux-gnu-"
if [[ $DRY_RUN -eq 0 && ! -x "${CROSS_COMPILE}gcc" ]]; then
print_error "Cross compiler not found at expected path: ${CROSS_COMPILE}gcc"
exit 1
fi
print_success "Toolchain configured: $CROSS_COMPILE"
}
prepare_build_tree() {
print_header "INITIAL SETUP"
run_cmd mkdir -p "$SOURCES_DIR/$KERNEL_VERSION"
cd "$SOURCES_DIR/$KERNEL_VERSION"
SRC_PATH="$PWD"
OUT_PATH="$SRC_PATH/kernel_out"
run_cmd mkdir -p "$OUT_PATH"
print_info "Script directory: $SCRIPT_DIR"
print_info "Build root: $SRC_PATH"
print_info "Output path: $OUT_PATH"
print_info "Sources directory: $SOURCES_DIR"
print_info "Target L4T version: $KERNEL_VERSION"
if [[ $IS_ARM64 -eq 1 ]]; then
TARGET_UNAME_R="$(uname -r)"
MODULE_INSTALL_BASE="/lib/modules/$TARGET_UNAME_R/kernel"
else
TARGET_UNAME_R="<target-jetson-uname-r>" # Not used
MODULE_INSTALL_BASE="/lib/modules/$TARGET_UNAME_R/kernel"
fi
}
run_modules_prepare() {
print_header "PREPARING BUILD ENVIRONMENT"
local kernel_src_dir="$SRC_PATH/Linux_for_Tegra/source/kernel/kernel-jammy-src"
cd "$kernel_src_dir"
if [[ $DRY_RUN -eq 1 ]]; then
if [[ $IS_ARM64 -eq 1 ]]; then
echo "[DRY-RUN] make O=\"$OUT_PATH\" modules_prepare"
else
echo "[DRY-RUN] make O=\"$OUT_PATH\" ARCH=arm64 CROSS_COMPILE=\"$CROSS_COMPILE\" modules_prepare"
fi
print_success "Dry run: modules_prepare skipped"
return 0
fi
if [[ $IS_ARM64 -eq 1 ]]; then
make O="$OUT_PATH" modules_prepare </dev/null |& tee "$OUT_PATH/prepare.log"
else
make O="$OUT_PATH" ARCH=arm64 CROSS_COMPILE="$CROSS_COMPILE" modules_prepare </dev/null |& tee "$OUT_PATH/prepare.log"
fi
print_success "modules_prepare completed"
}
build_modules() {
print_header "MODULE COMPILATION"
local kernel_src_dir="$SRC_PATH/Linux_for_Tegra/source/kernel/kernel-jammy-src"
cd "$kernel_src_dir"
if [[ $DRY_RUN -eq 0 ]]; then
: > "$OUT_PATH/build.log"
fi
local entry module_name config_symbol module_dir
for entry in "${MODULE_ENTRIES[@]}"; do
IFS='|' read -r module_name config_symbol module_dir <<< "$entry"
print_step "Compiling module: $module_name"
print_info " - Config: $config_symbol"
print_info " - Directory: $module_dir"
if [[ $DRY_RUN -eq 1 ]]; then
if [[ $IS_ARM64 -eq 1 ]]; then
echo "[DRY-RUN] make O=\"$OUT_PATH\" M=\"$module_dir\" modules"
else
echo "[DRY-RUN] make O=\"$OUT_PATH\" ARCH=arm64 CROSS_COMPILE=\"$CROSS_COMPILE\" M=\"$module_dir\" modules"
fi
continue
fi
if [[ $IS_ARM64 -eq 1 ]]; then
make O="$OUT_PATH" M="$module_dir" modules </dev/null |& tee -a "$OUT_PATH/build.log"
else
make O="$OUT_PATH" ARCH=arm64 CROSS_COMPILE="$CROSS_COMPILE" M="$module_dir" modules </dev/null |& tee -a "$OUT_PATH/build.log"
fi
done
print_success "All requested modules compiled"
print_info "Preparation log: $OUT_PATH/prepare.log"
print_info "Build log: $OUT_PATH/build.log"
}
install_or_export_modules() {
print_header "INSTALLATION / POST-PROCESSING"
local entry module_name config_symbol module_dir
for entry in "${MODULE_ENTRIES[@]}"; do
IFS='|' read -r module_name config_symbol module_dir <<< "$entry"
local module_source_root="$OUT_PATH/$module_dir/"
local module_dest_root="$MODULE_INSTALL_BASE/${module_dir#drivers/}/"
if [[ $DRY_RUN -eq 0 && ! -d "$module_source_root" ]]; then
print_error "Built module directory not found: $module_source_root"
exit 1
fi
if [[ $IS_ARM64 -eq 1 ]]; then
print_step "Installing modules from $module_dir to Jetson"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sudo mkdir -p \"$module_dest_root\""
echo "[DRY-RUN] sudo rsync -av --include='*/' --include='*.ko' --exclude='*' \"$module_source_root\" \"$module_dest_root\""
echo "[DRY-RUN] ensure requested module names are present in /etc/modules"
else
sudo mkdir -p "$module_dest_root"
sudo rsync -av \
--include='*/' \
--include='*.ko' \
--exclude='*' \
"$module_source_root" \
"$module_dest_root"
if ! grep -q "^$module_name$" /etc/modules; then
echo "$module_name" | sudo tee -a /etc/modules >/dev/null
print_success "$module_name added to /etc/modules"
else
print_info "$module_name already present in /etc/modules"
fi
fi
else
local export_root="$SCRIPT_DIR/exported_modules/${module_dir#drivers/}/"
print_step "Exporting built modules from $module_dir with preserved subtree"
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] mkdir -p \"$export_root\""
echo "[DRY-RUN] rsync -av --include='*/' --include='*.ko' --exclude='*' \"$module_source_root\" \"$export_root\""
else
mkdir -p "$export_root"
rsync -av \
--include='*/' \
--include='*.ko' \
--exclude='*' \
"$module_source_root" \
"$export_root"
print_success "Exported modules under: $export_root"
fi
fi
done
if [[ $IS_ARM64 -eq 1 ]]; then
print_step "Updating module dependencies..."
if [[ $DRY_RUN -eq 1 ]]; then
echo "[DRY-RUN] sudo depmod -a"
print_success "Dry run complete. No installation changes were made."
else
sudo depmod -a
print_success "Installation complete. Reboot required."
fi
else
print_header "JETSON INSTALLATION INSTRUCTIONS"
echo -e "Copy the generated exported_modules tree to the target Jetson."
echo
echo -e "Then run on the Jetson:"
echo -e " ${YELLOW}sudo rsync -av exported_modules/ /lib/modules/\$(uname -r)/kernel/${NC}"
echo -e " ${YELLOW}sudo depmod -a${NC}"
echo -e " ${YELLOW}sudo reboot${NC}"
fi
}
# ------------------------------------------------------------------------------
# Main flow
# ------------------------------------------------------------------------------
print_header "INITIAL CHECK"
if [[ $IS_ARM64 -eq 1 ]]; then
print_success "Running on ARM64 (Jetson - Native Mode)"
else
print_info "Running on $ARCHITECTURE (Cross-Compile Mode)"
fi
if [[ $DRY_RUN -eq 1 ]]; then
print_info "Dry run mode enabled: no changes will be made"
fi
print_header "KERNEL VERSION RESOLUTION"
if [[ $IS_ARM64 -eq 1 ]]; then
if [[ -z "$KERNEL_VERSION" ]]; then
print_step "Discovering Jetson L4T version..."
if ! KERNEL_VERSION="$(discover_l4t_version)"; then
print_error "Failed to discover Jetson L4T version automatically."
print_error "Use --kernel-version VERSION"
exit 1
fi
print_success "Discovered Jetson L4T version: $KERNEL_VERSION"
else
print_info "Using user-specified kernel version: $KERNEL_VERSION"
fi
else
if [[ -z "$KERNEL_VERSION" ]]; then
print_error "Cross-compile mode requires --kernel-version"
exit 1
fi
print_info "Using cross-compile target L4T version: $KERNEL_VERSION"
fi
validate_kernel_version_format "$KERNEL_VERSION"
resolve_release_info "$KERNEL_VERSION"
print_header "MODULE FILE CHECK"
print_info "Modules file: $MODULES_FILE"
validate_modules_file "$MODULES_FILE"
print_success "Modules file is valid"
install_dependencies
prepare_build_tree
prepare_kernel_config
download_and_extract_sources
setup_toolchain_if_needed
run_modules_prepare
build_modules
install_or_export_modules
print_header "PROCESS COMPLETED"