smart-os/01_build_src.sh

201 lines
6.5 KiB
Bash
Raw Normal View History

2022-07-28 23:43:44 +08:00
#!/bin/sh
#-----------------------------------------------
#
2022-07-31 18:36:44 +08:00
# 导入公共变量
2022-07-28 23:43:44 +08:00
#
2022-07-31 18:36:44 +08:00
#-----------------------------------------------
. ./common.sh
#LINUX_SRC_URL=https://kernel.org/pub/linux/kernel/v4.x/linux-4.14.9.tar.xz
LINUX_SRC_URL=https://mirror.bjtu.edu.cn/kernel/linux/kernel/v4.x/linux-4.14.9.tar.xz
#GLIBC_SRC_URL=https://ftp.gnu.org/gnu/glibc/glibc-2.32.tar.bz2
GLIBC_SRC_URL=https://mirrors.ustc.edu.cn/gnu/glibc/glibc-2.32.tar.bz2
BUSYBOX_SRC_URL=https://busybox.net/downloads/busybox-1.34.1.tar.bz2
#GCC_SRC_URL=https://ftpmirror.gnu.org/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
GCC_SRC_URL=https://mirrors.ustc.edu.cn/gnu/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
#BINUTILS_SRC_URL=https://ftp.gnu.org/gnu/binutils/binutils-2.36.tar.xz
BINUTILS_SRC_URL=https://mirrors.ustc.edu.cn/gnu/binutils/binutils-2.36.tar.xz
2022-07-28 23:43:44 +08:00
2022-07-31 18:36:44 +08:00
export CFLAGS="-Os -s -fno-stack-protector -fomit-frame-pointer -U_FORTIFY_SOURCE"
2022-07-28 23:43:44 +08:00
#----------------------------------------------
#
# 下载源码
#
#----------------------------------------------
mkdir -pv source
cd source
2022-07-31 18:36:44 +08:00
LINUX_SRC_NAME=$(file_name ${LINUX_SRC_URL})
if [ ! -f ${LINUX_SRC_NAME} ]; then
wget $LINUX_SRC_URL
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
GLIBC_SRC_NAME=$(file_name ${GLIBC_SRC_URL})
if [ ! -f ${GLIBC_SRC_NAME} ]; then
wget $GLIBC_SRC_URL
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
BUSYBOX_SRC_NAME=$(file_name ${BUSYBOX_SRC_URL})
if [ ! -f ${BUSYBOX_SRC_NAME} ]; then
wget $BUSYBOX_SRC_URL
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
GCC_SRC_NAME=$(file_name ${GCC_SRC_URL})
if [ ! -f ${GCC_SRC_NAME} ]; then
wget $GCC_SRC_URL
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
BINUTILS_SRC_NAME=$(file_name ${BINUTILS_SRC_URL})
if [ ! -f ${BINUTILS_SRC_NAME} ]; then
wget $BINUTILS_SRC_URL
2022-07-28 23:43:44 +08:00
fi
cd ..
#---------------------------------------------
#
# 解压源码
#
#---------------------------------------------
2022-07-31 18:36:44 +08:00
mkdir -pv ${build_dir}
2022-07-28 23:43:44 +08:00
2022-07-31 18:36:44 +08:00
LINUX_SRC_DIR=${build_dir}"/"$(file_dirname ${LINUX_SRC_NAME} .tar.xz)
if [ ! -d ${LINUX_SRC_DIR} ]; then
echo "unzip ${LINUX_SRC_NAME} source code"
tar xf source/${LINUX_SRC_NAME} -C ${build_dir}
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
GLIBC_SRC_DIR=${build_dir}"/"$(file_dirname ${GLIBC_SRC_NAME} .tar.bz2)
if [ ! -d ${GLIBC_SRC_DIR} ]; then
echo "unzip ${GLIBC_SRC_NAME} source code"
tar xf source/${GLIBC_SRC_NAME} -C ${build_dir}
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
BUSYBOX_SRC_DIR=${build_dir}"/"$(file_dirname ${BUSYBOX_SRC_NAME} .tar.bz2)
if [ ! -d ${BUSYBOX_SRC_DIR} ]; then
echo "unzip ${BUSYBOX_SRC_NAME} source code"
tar xf source/${BUSYBOX_SRC_NAME} -C ${build_dir}
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
GCC_SRC_DIR=${build_dir}"/"$(file_dirname ${GCC_SRC_NAME} .tar.xz)
if [ ! -d ${GCC_SRC_DIR} ]; then
echo "unzip ${GCC_SRC_NAME} source code"
tar xf source/${GCC_SRC_NAME} -C ${build_dir}
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
BINUTILS_SRC_DIR=${build_dir}"/"$(file_dirname ${BINUTILS_SRC_NAME} .tar.xz)
if [ ! -d ${BINUTILS_SRC_DIR} ]; then
echo "unzip ${BINUTILS_SRC_NAME} source code"
tar xf source/${BINUTILS_SRC_NAME} -C ${build_dir}
2022-07-28 23:43:44 +08:00
fi
2022-07-31 18:36:44 +08:00
#-----------------------------------------------
#
# 重新生成目标文件
#
#-----------------------------------------------
if [ "$1" != "" ]; then
if [ $1 != "rebuild" ]; then
exit
fi
echo "rebuild"
cd ${build_dir}
rm -rf linux_install glibc_install busybox_install gcc_install binutils_install
# 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9
if [ ! -d "linux_install" ]; then
mkdir -pv linux_install && cd ${LINUX_SRC_DIR}
make INSTALL_HDR_PATH=${linux_install} headers_install -j8 && cp arch/x86_64/boot/bzImage ${linux_install} && cd ..
fi
# 编译glibc
if [ ! -d "glibc_install" ]; then
mkdir -pv glibc_install && cd ${GLIBC_SRC_DIR}
mkdir -pv build && cd build
make install -j8 DESTDIR=${glibc_install} && cd .. && cd ..
fi
# 编译 busybox
if [ ! -d "busybox_install" ]; then
mkdir -pv busybox_install && cd ${BUSYBOX_SRC_DIR}
make CONFIG_PREFIX=${busybox_install} install && cd ..
fi
# 编译 libgcc
if [ ! -d "gcc_install" ]; then
mkdir -pv gcc_install && cd ${GCC_SRC_DIR}
make install -j8 DESTDIR=${gcc_install} && cd ..
fi
# 编译 binutils
if [ ! -d "binutils_install" ]; then
mkdir -pv binutils_install && cd ${BINUTILS_SRC_DIR}
make install -j8 DESTDIR=${binutils_install} && cd ..
fi
cd ..
exit
fi
2022-07-28 23:43:44 +08:00
#---------------------------------------------
#
# 编译源码
#
#---------------------------------------------
2022-07-31 18:36:44 +08:00
cd ${build_dir}
2022-07-28 23:43:44 +08:00
# 编译内核, 最终所有模块都装到目录 /lib/modules/4.14.9
2022-07-31 18:36:44 +08:00
if [ ! -d "linux_install" ]; then
mkdir -pv linux_install && cd ${LINUX_SRC_DIR} && make mrproper && make x86_64_defconfig
2022-07-28 23:43:44 +08:00
# Enable the VESA framebuffer for graphics support.
2022-07-31 00:01:00 +08:00
sed -i "s/.*CONFIG_FB_VESA.*/CONFIG_FB_VESA=y/" .config
2022-07-28 23:43:44 +08:00
# 网络需要 TUN/TAP 驱动 [ Device Drivers ] ---> [ Network device support ] ---> [ Universal TUN/TAP device driver support ]
2022-07-31 00:01:00 +08:00
make bzImage -j8
2022-07-28 23:43:44 +08:00
#cd linux-4.14.9 && make x86_64_defconfig && make bzImage -j8 && make modules && make modules_install && cd ..
2022-07-31 18:36:44 +08:00
make INSTALL_HDR_PATH=${linux_install} headers_install -j8 && cp arch/x86_64/boot/bzImage ${linux_install} && cd ..
2022-07-28 23:43:44 +08:00
fi
# 编译glibc
if [ ! -d "glibc_install" ]; then
2022-07-31 18:36:44 +08:00
mkdir -pv glibc_install && cd ${GLIBC_SRC_DIR}
2022-07-28 23:43:44 +08:00
mkdir -pv build && cd build && make distclean
2022-07-31 00:01:00 +08:00
../configure --prefix=/usr \
2022-07-31 18:36:44 +08:00
--with-headers=${linux_install}/include \
2022-07-31 00:01:00 +08:00
--enable-kernel=4.0.1 \
2022-07-28 23:43:44 +08:00
--without-selinux \
2022-07-31 00:01:00 +08:00
--disable-werror \
--disable-werror \
CFLAGS="$CFLAGS"
2022-07-28 23:43:44 +08:00
make -j8 && make install -j8 DESTDIR=${glibc_install} && cd .. && cd ..
fi
# 编译 busybox
if [ ! -d "busybox_install" ]; then
2022-07-31 18:36:44 +08:00
mkdir -pv busybox_install && cd ${BUSYBOX_SRC_DIR} && make distclean && make defconfig
2022-07-28 23:43:44 +08:00
# 静态编译 sed -i "s/# CONFIG_STATIC is not set/CONFIG_STATIC=y/g" .config
sed -i "s|.*CONFIG_SYSROOT.*|CONFIG_SYSROOT=\"${glibc_install}\"|" .config
2022-07-31 18:36:44 +08:00
sed -i "s|.*CONFIG_EXTRA_CFLAGS.*|CONFIG_EXTRA_CFLAGS=\"-I${linux_install}/include -I${glibc_install}/include -L${glibc_install}/usr/lib64 $CFLAGS\"|" .config
2022-07-28 23:43:44 +08:00
make busybox -j8 && make CONFIG_PREFIX=${busybox_install} install && cd ..
fi
2022-07-31 18:36:44 +08:00
# 编译 gcc
if [ ! -d "gcc_install" ]; then
mkdir -pv gcc_install && cd ${GCC_SRC_DIR} && make distclean && rm ./config.cache
2022-07-28 23:43:44 +08:00
./contrib/download_prerequisites
2022-07-31 00:01:00 +08:00
./configure --prefix=/usr --enable-languages=c,c++ --disable-multilib --disable-static --disable-libquadmath --enable-shared
2022-07-31 18:36:44 +08:00
CFLAGS="-L${glibc_install}/lib64 $CFLAGS" make -j8 && make install -j8 DESTDIR=${gcc_install} && cd ..
2022-07-28 23:43:44 +08:00
fi
# 编译 binutils
if [ ! -d "binutils_install" ]; then
2022-07-31 18:36:44 +08:00
mkdir -pv binutils_install && cd ${BINUTILS_SRC_DIR} && make distclean
2022-07-31 00:01:00 +08:00
./configure --prefix=/usr
CFLAGS="-L${glibc_install}/lib64 $CFLAGS" make -j8 && make install -j8 DESTDIR=${binutils_install} && cd ..
2022-07-28 23:43:44 +08:00
cd ..
fi
cd ..
echo "Run the next script: 02_build_img.sh"