-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·222 lines (194 loc) · 5.9 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·222 lines (194 loc) · 5.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
#!/bin/bash
# build.sh
# A utility to build Jetson Linux kernel module drivers for specified peripherals.
# Currently supports PWM PCA9685.
# Usage:
# ./build.sh [--pwm-pca9685] [--ads1015] [--sc16is7xx] [--output-dir DIR] <public_sources.tbz2>
set -euo pipefail
# Supported peripherals
SUPPORTED=(pwm-pca9685 ads1015 sc16is7xx)
# Selected peripherals
declare -a SELECTED=()
# Default output directory
OUTDIR="$PWD"
# Print usage information
usage() {
echo "Usage: $0 [options] <public_sources.tbz2>"
echo
echo "Options:"
for periph in "${SUPPORTED[@]}"; do
echo " --${periph} Compile driver for ${periph^^} peripheral"
done
echo " --output-dir DIR Specify output directory (defaults to current directory)"
echo " --help Show this help message and exit"
exit 1
}
# Ensure at least one argument
if [[ $# -lt 1 ]]; then
usage
fi
# Parse options until only the tar file remains
while [[ $# -gt 1 ]]; do
case "$1" in
--help)
usage
;;
--output-dir)
if [[ -n "${2:-}" && -d "$2" ]]; then
OUTDIR="$2"
shift 2
else
echo "Error: --output-dir requires a valid directory argument"
exit 1
fi
;;
--*)
opt="${1#--}"
# Exact match against supported peripherals
for s in "${SUPPORTED[@]}"; do
if [[ "$s" == "$opt" ]]; then
SELECTED+=("$opt")
shift
continue 2
fi
done
echo "Error: Unsupported option '$1'"
usage
;;
*)
echo "Error: Unexpected option or argument '$1'"
usage
;;
esac
done
# Remaining argument is the tar file
TARFILE="$1"
if [[ ! -f "$TARFILE" ]]; then
echo "Error: File '$TARFILE' not found"
exit 1
fi
# Validate tar file
if ! tar -tjf "$TARFILE" >/dev/null 2>&1; then
echo "Error: '$TARFILE' is not a valid bzip2 tar archive"
exit 1
fi
# Ensure at least one peripheral was selected
if [[ ${#SELECTED[@]} -eq 0 ]]; then
echo "Error: No peripheral specified"
usage
fi
# Show configuration summary
echo "Selected peripherals: ${SELECTED[*]}"
echo "Using output directory: $OUTDIR"
# Detect host architecture and configure cross-compilation if needed
HOST_ARCH=$(uname -m)
MAKE_OPTS=""
if [[ "$HOST_ARCH" == "x86_64" ]]; then
echo "Detected x86_64 host - enabling cross-compilation for ARM64 (aarch64)"
MAKE_OPTS="ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-"
CROSS_COMPILE_PKGS="gcc-aarch64-linux-gnu"
elif [[ "$HOST_ARCH" == "aarch64" ]]; then
echo "Detected ARM64 host - native compilation"
CROSS_COMPILE_PKGS=""
else
echo "Error: Unsupported host architecture '$HOST_ARCH'. Only x86_64 and aarch64 are supported."
exit 1
fi
# Install dependencies
sudo apt install -y tar build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison $CROSS_COMPILE_PKGS
# Create temporary workspace
echo "Creating temporary workspace..."
TMPDIR=$(mktemp -d)
trap 'rm -rf "$TMPDIR"' EXIT
# Extract public sources
echo "Extracting public sources to '$TMPDIR'..."
tar -xjf "$TARFILE" -C "$TMPDIR"
# Extract kernel sources
echo "Extracting kernel sources..."
pushd "$TMPDIR/Linux_for_Tegra/source/public" > /dev/null
tar -xjf kernel_src.tbz2
popd > /dev/null
# Locate exact kernel source directory
KERNEL_SRC_DIR=$(find "$TMPDIR/Linux_for_Tegra/source/public/kernel" -maxdepth 1 -type d -name 'kernel-*' | head -n 1)
if [[ -z "$KERNEL_SRC_DIR" ]]; then
echo "Error: Kernel source directory not found"
exit 1
fi
# Enter kernel source directory
echo "Configuring kernel at $KERNEL_SRC_DIR..."
pushd "$KERNEL_SRC_DIR" > /dev/null
# Initial default config
make $MAKE_OPTS olddefconfig
# Set version prefix
scripts/config --set-str CONFIG_LOCALVERSION "-tegra"
# Define configuration functions for peripherals
configure_pwm_pca9685() {
echo "Configuring I2C and PCA9685 support..."
scripts/config --enable CONFIG_I2C
scripts/config --enable CONFIG_I2C_CHARDEV
scripts/config --module CONFIG_PWM_PCA9685
}
configure_ads1015() {
echo "Configuring IIO and ADS1015 support..."
scripts/config --enable CONFIG_IIO
scripts/config --enable CONFIG_IIO_CHARDEV
scripts/config --module CONFIG_IIO_ADS1015
}
configure_sc16is7xx() {
echo "Configuring SC16IS7XX UART support..."
scripts/config --enable CONFIG_SERIAL_CORE
scripts/config --enable CONFIG_I2C
scripts/config --enable CONFIG_SPI
scripts/config --enable CONFIG_SERIAL_SC16IS7XX
scripts/config --module CONFIG_SERIAL_SC16IS7XX_I2C
scripts/config --module CONFIG_SERIAL_SC16IS7XX_SPI
}
# Invoke configuration for each selected peripheral
for periph in "${SELECTED[@]}"; do
cfg_fn="configure_${periph//-/_}"
if declare -f "$cfg_fn" >/dev/null; then
$cfg_fn
else
echo "Warning: No configure function defined for $periph"
fi
done
# Re-run default config to apply changes
make $MAKE_OPTS olddefconfig
make $MAKE_OPTS prepare
make $MAKE_OPTS modules_prepare
popd > /dev/null
# Define compile functions for peripherals
compile_pwm_pca9685() {
echo "Compiling PWM PCA9685 module..."
pushd "$KERNEL_SRC_DIR" > /dev/null
make $MAKE_OPTS drivers/pwm/pwm-pca9685.ko
cp drivers/pwm/pwm-pca9685.ko "$OUTDIR"
popd > /dev/null
echo "pwm-pca9685.ko has been copied to $OUTDIR"
}
compile_ads1015() {
echo "Compiling ADS1015 ADC module..."
pushd "$KERNEL_SRC_DIR" > /dev/null
make $MAKE_OPTS drivers/iio/adc/ti-ads1015.ko
cp drivers/iio/adc/ti-ads1015.ko "$OUTDIR"
popd > /dev/null
echo "ads1015.ko has been copied to $OUTDIR"
}
compile_sc16is7xx() {
echo "Compiling SC16IS7XX UART module..."
pushd "$KERNEL_SRC_DIR" > /dev/null
make $MAKE_OPTS drivers/tty/serial/sc16is7xx.ko
cp drivers/tty/serial/sc16is7xx.ko "$OUTDIR"
popd > /dev/null
echo "sc16is7xx.ko has been copied to $OUTDIR"
}
# Build each selected peripheral
for periph in "${SELECTED[@]}"; do
build_fn="compile_${periph//-/_}"
if declare -f "$build_fn" >/dev/null; then
$build_fn
else
echo "Warning: No compile function defined for $periph"
fi
done
echo "All done."