@@ -19,11 +19,31 @@ function OK
1919
2020PARALLEL=" -j$( nproc) "
2121
22+ function safe_copy {
23+ local src=" $1 "
24+ local dst=" $2 "
25+
26+ if [ ! -f " $dst " ]; then
27+ echo " Copying $src -> $dst "
28+ cp -f " $src " " $dst "
29+ else
30+ echo " $dst already exists, skipping copy"
31+ fi
32+ }
33+
2234function do_buildroot
2335{
24- ASSERT git clone https://github.com/buildroot/buildroot -b 2024.11.1 --depth=1
25- cp -f configs/buildroot.config buildroot/.config
26- cp -f configs/busybox.config buildroot/busybox.config
36+ if [ ! -d buildroot ]; then
37+ echo " Cloning Buildroot..."
38+ ASSERT git clone https://github.com/buildroot/buildroot -b 2024.11.1 --depth=1
39+ else
40+ echo " buildroot/ already exists, skipping clone"
41+ fi
42+
43+ safe_copy configs/buildroot.config buildroot/.config
44+ safe_copy configs/busybox.config buildroot/busybox.config
45+ cp -f target/init buildroot/fs/cpio/init
46+
2747 # Otherwise, the error below raises:
2848 # You seem to have the current working directory in your
2949 # LD_LIBRARY_PATH environment variable. This doesn't work.
@@ -32,13 +52,28 @@ function do_buildroot
3252 ASSERT make olddefconfig
3353 ASSERT make $PARALLEL
3454 popd
35- cp -f buildroot/output/images/rootfs.cpio ./
55+
56+ if [[ $EXTERNAL_ROOT -eq 1 ]]; then
57+ echo " Copying rootfs.cpio to rootfs_full.cpio (external root mode)"
58+ cp -f buildroot/output/images/rootfs.cpio ./rootfs_full.cpio
59+ ASSERT ./scripts/rootfs_ext4.sh
60+ else
61+ echo " Copying rootfs.cpio to rootfs.cpio (initramfs mode)"
62+ cp -f buildroot/output/images/rootfs.cpio ./rootfs.cpio
63+ fi
3664}
3765
3866function do_linux
3967{
40- ASSERT git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git -b linux-6.12.y --depth=1
41- cp -f configs/linux.config linux/.config
68+ if [ ! -d linux ]; then
69+ echo " Cloning Linux kernel..."
70+ ASSERT git clone https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git -b linux-6.12.y --depth=1
71+ else
72+ echo " linux/ already exists, skipping clone"
73+ fi
74+
75+ safe_copy configs/linux.config linux/.config
76+
4277 export PATH=" $PWD /buildroot/output/host/bin:$PATH "
4378 export CROSS_COMPILE=riscv32-buildroot-linux-gnu-
4479 export ARCH=riscv
@@ -49,5 +84,74 @@ function do_linux
4984 popd
5085}
5186
52- do_buildroot && OK
53- do_linux && OK
87+ function show_help {
88+ cat << EOF
89+ Usage: $0 [--buildroot] [--linux] [--all] [--external-root] [--clean-build] [--help]
90+
91+ Options:
92+ --buildroot Build Buildroot rootfs
93+ --linux Build Linux kernel
94+ --all Build both Buildroot and Linux
95+ --external-root Use external rootfs instead of initramfs
96+ --clean-build Remove entire buildroot/ and/or linux/ directories before build
97+ --help Show this message
98+ EOF
99+ exit 1
100+ }
101+
102+ BUILD_BUILDROOT=0
103+ BUILD_LINUX=0
104+ EXTERNAL_ROOT=0
105+ CLEAN_BUILD=0
106+
107+ while [[ $# -gt 0 ]]; do
108+ case " $1 " in
109+ --buildroot)
110+ BUILD_BUILDROOT=1
111+ ;;
112+ --linux)
113+ BUILD_LINUX=1
114+ ;;
115+ --all)
116+ BUILD_BUILDROOT=1
117+ BUILD_LINUX=1
118+ ;;
119+ --external-root)
120+ EXTERNAL_ROOT=1
121+ ;;
122+ --clean-build)
123+ CLEAN_BUILD=1
124+ ;;
125+ --help|-h)
126+ show_help
127+ ;;
128+ * )
129+ echo " Unknown option: $1 "
130+ show_help
131+ ;;
132+ esac
133+ shift
134+ done
135+
136+ if [[ $BUILD_BUILDROOT -eq 0 && $BUILD_LINUX -eq 0 ]]; then
137+ echo " Error: No build target specified. Use --buildroot, --linux, or --all."
138+ show_help
139+ fi
140+
141+ if [[ $CLEAN_BUILD -eq 1 && $BUILD_BUILDROOT -eq 1 && -d buildroot ]]; then
142+ echo " Removing buildroot/ for clean build..."
143+ rm -rf buildroot
144+ fi
145+
146+ if [[ $CLEAN_BUILD -eq 1 && $BUILD_LINUX -eq 1 && -d linux ]]; then
147+ echo " Removing linux/ for clean build..."
148+ rm -rf linux
149+ fi
150+
151+ if [[ $BUILD_BUILDROOT -eq 1 ]]; then
152+ do_buildroot && OK
153+ fi
154+
155+ if [[ $BUILD_LINUX -eq 1 ]]; then
156+ do_linux && OK
157+ fi
0 commit comments