@@ -23,32 +23,140 @@ PARALLEL="-j$(nproc)"
2323
2424OUTPUT_DIR=./build/linux-image/
2525
26+ BR_PKG_DIR=./tests/system/br_pkgs
27+
28+ function create_br_pkg_config()
29+ {
30+ local pkg_name=$1
31+ local output_path=$2
32+
33+ cat << EOF > "${output_path} "
34+ config BR2_PACKAGE_${pkg_name^^}
35+ bool "${pkg_name} "
36+ help
37+ ${pkg_name} package.
38+ EOF
39+ }
40+
41+ function create_br_pkg_makefile()
42+ {
43+ local pkg_name=$1
44+ local output_path=$2
45+
46+ cat << EOF > "${output_path} "
47+ ################################################################################
48+ #
49+ # ${pkg_name} package
50+ #
51+ ################################################################################
52+
53+ ${pkg_name^^} _VERSION = 1.0
54+ ${pkg_name^^} _SITE = package/${pkg_name} /src
55+ ${pkg_name^^} _SITE_METHOD = local
56+
57+ define ${pkg_name^^} _BUILD_CMDS
58+ \$ (MAKE) CC="\$ (TARGET_CC)" LD="\$ (TARGET_LD)" -C \$ (@D)
59+ endef
60+
61+ define ${pkg_name^^} _INSTALL_TARGET_CMDS
62+ \$ (INSTALL) -D -m 0755 \$ (@D)/${pkg_name} \$ (TARGET_DIR)/usr/bin
63+ endef
64+
65+ \$ (eval \$ (generic-package))
66+ EOF
67+ }
68+
69+ function create_br_pkg_src()
70+ {
71+ local pkg_name=$1
72+ local src_c_file=$2
73+ local output_path=$3
74+ local mk_output_path=$3 /Makefile
75+ local src_output_path=$3 /$pkg_name .c
76+
77+ # Create src directory
78+ mkdir -p ${output_path}
79+
80+ # Create makefile
81+ # the output binary is in lowercase
82+ cat << EOF > "${mk_output_path} "
83+ all:
84+ \$ (CC) ${pkg_name} .c -o ${pkg_name}
85+ EOF
86+
87+ # moving C source file
88+ cp -f ${src_c_file} ${src_output_path}
89+ }
90+
91+ function update_br_pkg_config()
92+ {
93+ local pkg_name=$1
94+ local br_pkg_config_file=" ${SRC_DIR} /buildroot/package/Config.in"
95+ local source_line=" source \" package/${pkg_name} /Config.in\" "
96+
97+ # Only append if this package's isn't already present in the menu
98+ if ! grep -q " ${pkg_name} " " ${br_pkg_config_file} " ; then
99+ sed -i ' /^menu "Custom packages"/,/^endmenu$/{
100+ /^endmenu$/i\
101+ ' " ${source_line} " '
102+ }' " ${br_pkg_config_file} "
103+ fi
104+ }
105+
106+ # This function patches the packages when building the rootfs.cpio from scratch
107+ function do_patch_buildroot
108+ {
109+ local br_pkg_config_file=" ${SRC_DIR} /buildroot/package/Config.in"
110+
111+ # Only append if the custom packages menu block isn't already present in the menu
112+ if ! grep -q " Custom packages" " ${br_pkg_config_file} " ; then
113+ cat << EOF >> "${br_pkg_config_file} "
114+ menu "Custom packages"
115+ endmenu
116+ EOF
117+ fi
118+
119+ for c in $( find ${BR_PKG_DIR} -type f) ; do
120+ local basename=" $( basename ${c} ) "
121+ local pkg_name=" ${basename% .* } "
122+
123+ mkdir -p ${SRC_DIR} /buildroot/package/${pkg_name}
124+
125+ create_br_pkg_config ${pkg_name} ${SRC_DIR} /buildroot/package/${pkg_name} /Config.in
126+ create_br_pkg_makefile ${pkg_name} ${SRC_DIR} /buildroot/package/${pkg_name} /${pkg_name} .mk
127+ create_br_pkg_src ${pkg_name} ${c} ${SRC_DIR} /buildroot/package/${pkg_name} /src
128+
129+ update_br_pkg_config ${pkg_name}
130+ done
131+ }
132+
26133function do_buildroot
27134{
28- cp -f assets/system/configs/buildroot.config $SRC_DIR /buildroot/.config
29- cp -f assets/system/configs/busybox.config $SRC_DIR /buildroot/busybox.config
135+ cp -f assets/system/configs/buildroot.config ${ SRC_DIR} /buildroot/.config
136+ cp -f assets/system/configs/busybox.config ${ SRC_DIR} /buildroot/busybox.config
30137 # Otherwise, the error below raises:
31138 # You seem to have the current working directory in your
32139 # LD_LIBRARY_PATH environment variable. This doesn't work.
33140 unset LD_LIBRARY_PATH
34- pushd $SRC_DIR /buildroot
141+ do_patch_buildroot
142+ pushd ${SRC_DIR} /buildroot
35143 ASSERT make olddefconfig
36- ASSERT make $PARALLEL
144+ ASSERT make ${ PARALLEL}
37145 popd
38- cp -f $SRC_DIR /buildroot/output/images/rootfs.cpio $OUTPUT_DIR
146+ cp -f ${ SRC_DIR} /buildroot/output/images/rootfs.cpio ${ OUTPUT_DIR}
39147}
40148
41149function do_linux
42150{
43- cp -f assets/system/configs/linux.config $SRC_DIR /linux/.config
44- export PATH=" $SRC_DIR /buildroot/output/host/bin:$PATH "
151+ cp -f assets/system/configs/linux.config ${ SRC_DIR} /linux/.config
152+ export PATH=" ${ SRC_DIR} /buildroot/output/host/bin:${ PATH} "
45153 export CROSS_COMPILE=riscv32-buildroot-linux-gnu-
46154 export ARCH=riscv
47- pushd $SRC_DIR /linux
155+ pushd ${ SRC_DIR} /linux
48156 ASSERT make olddefconfig
49- ASSERT make $PARALLEL
157+ ASSERT make ${ PARALLEL}
50158 popd
51- cp -f $SRC_DIR /linux/arch/riscv/boot/Image $OUTPUT_DIR
159+ cp -f ${ SRC_DIR} /linux/arch/riscv/boot/Image ${ OUTPUT_DIR}
52160}
53161
54162do_buildroot && OK
0 commit comments