给树莓派 3B+ 构建 Linux 时想顺便添加 gcc,反正都是玩儿么。
不过根据 Buildroot 官方文档,从 Buildroot-2012.11 开始,对 native gcc 的支持就停止了。yocto 又太复杂了,只能在网上找添加目标 gcc 包的方法。
可能因为没什么用,很难找,找了一圈,最终在一个国外学校的网站上搜到了:Buildroot and compiler on target。
最终过程如下:
打补丁
git clone https://gitee.com/mirrors/buildroot.git
wget https://luplab.cs.ucdavis.edu/assets/buildroot-gcc/gcc-target.patch
git apply --stat ../gcc-target.patch
git apply --check ../gcc-target.patch
git apply ../gcc-target.patch
似乎应用不了,reset 到 2021.08-rc1 也不行。
算了,直接添加好了。
添加 hash 列表
添加文件 hash 值,用于下载校验
cd package/gcc/gcc-target/
ln -s ../gcc.hash gcc-target.hash
添加 mk 文件
添加 mk 文件,用于编译
nano package/gcc/gcc-target/gcc-target.mk
内容如下
################################################################################
#
# gcc-target
#
################################################################################
GCC_TARGET_VERSION = $(GCC_VERSION)
GCC_TARGET_SITE = $(GCC_SITE)
GCC_TARGET_SOURCE = $(GCC_SOURCE)
# Use the same archive as gcc-initial and gcc-final
GCC_TARGET_DL_SUBDIR = gcc
GCC_TARGET_DEPENDENCIES = gmp mpfr mpc
# First, we use HOST_GCC_COMMON_MAKE_OPTS to get a lot of correct flags (such as
# the arch, abi, float support, etc.) which are based on the config used to
# build the internal toolchain
GCC_TARGET_CONF_OPTS = $(HOST_GCC_COMMON_CONF_OPTS)
# Then, we modify incorrect flags from HOST_GCC_COMMON_CONF_OPTS
GCC_TARGET_CONF_OPTS += \
--with-sysroot=/ \
--with-build-sysroot=$(STAGING_DIR) \
--disable-__cxa_atexit \
--with-gmp=$(STAGING_DIR) \
--with-mpc=$(STAGING_DIR) \
--with-mpfr=$(STAGING_DIR)
# Then, we force certain flags that may appear in HOST_GCC_COMMON_CONF_OPTS
GCC_TARGET_CONF_OPTS += \
--disable-libquadmath \
--disable-libsanitizer \
--disable-plugin \
--disable-lto
# Finally, we add some of our own flags
GCC_TARGET_CONF_OPTS += \
--enable-languages=c \
--disable-boostrap \
--disable-libgomp \
--disable-nls \
--disable-libmpx \
--disable-gcov \
$(EXTRA_TARGET_GCC_CONFIG_OPTIONS)
GCC_TARGET_CONF_ENV = $(HOST_GCC_COMMON_CONF_ENV)
GCC_TARGET_MAKE_OPTS += $(HOST_GCC_COMMON_MAKE_OPTS)
# Install standard C headers (from glibc)
define GCC_TARGET_INSTALL_HEADERS
cp -r $(STAGING_DIR)/usr/include $(TARGET_DIR)/usr
endef
GCC_TARGET_POST_INSTALL_TARGET_HOOKS += GCC_TARGET_INSTALL_HEADERS
# Install standard C libraries (from glibc)
GCC_TARGET_GLIBC_LIBS = \
*crt*.o *_nonshared.a \
libBrokenLocale.so libanl.so libbfd.so libc.so libcrypt.so libdl.so \
libm.so libnss_compat.so libnss_db.so libnss_files.so libnss_hesiod.so \
libpthread.so libresolv.so librt.so libthread_db.so libutil.so
define GCC_TARGET_INSTALL_LIBS
for libpattern in $(GCC_TARGET_GLIBC_LIBS); do \
$(call copy_toolchain_lib_root,$$libpattern) ; \
done
endef
GCC_TARGET_POST_INSTALL_TARGET_HOOKS += GCC_TARGET_INSTALL_LIBS
# Remove unnecessary files (extra links to gcc binaries, and libgcc which is
# already in `/lib`)
define GCC_TARGET_RM_FILES
rm -f $(TARGET_DIR)/usr/bin/$(ARCH)-buildroot-linux-gnu-gcc*
rm -f $(TARGET_DIR)/usr/lib/libgcc_s*.so*
rm -f $(TARGET_DIR)/usr/$(ARCH)-buildroot-linux-gnu/lib/ldscripts/elf32*
rm -f $(TARGET_DIR)/usr/$(ARCH)-buildroot-linux-gnu/lib/ldscripts/elf64b*
endef
GCC_TARGET_POST_INSTALL_TARGET_HOOKS += GCC_TARGET_RM_FILES
$(eval $(autotools-package))
添加目标包配置脚本
nano package/gcc/Config.in
内容如下
config BR2_PACKAGE_GCC_TARGET
bool "gcc"
depends on BR2_TOOLCHAIN_BUILDROOT
select BR2_PACKAGE_BINUTILS
select BR2_PACKAGE_BINUTILS_TARGET
select BR2_PACKAGE_GMP
select BR2_PACKAGE_MPFR
select BR2_PACKAGE_MPC
help
If you want the target system to be able to run
binutils/gcc and compile native code, say Y here.
config BR2_EXTRA_TARGET_GCC_CONFIG_OPTIONS
string "Additional target gcc options"
default ""
depends on BR2_PACKAGE_GCC_TARGET
help
Any additional target gcc options you may want to include....
Including, but not limited to --disable-checking etc.
Refer to */configure in your gcc sources.
添加目标包引用
nano package/Config.in
在 Develope tools 下面添加
source "package/gcc/Config.in"
包含头文件
安装到目标时将头文件包含进去
nano Makefile
修改如下
rm -rf $(TARGET_DIR)/usr/include $(TARGET_DIR)/usr/share/aclocal \
替换为
rm -rf $(TARGET_DIR)/usr/share/aclocal \
\( -name '*.a' -o -name '*.la' -o -name '*.prl' \) -print0 | xargs -0 rm -f
替换为
\( -name '*.la' -o -name '*.prl' \) -print0 | xargs -0 rm -f
重新配置
make menuconfig
修改如下
Target packages ->Development tools,添加 gcc
Filesystem ->images exact size,改为 500M
Build options ->strip target binaries,取消
重新编译
make -j8