[REF: https://wiki.linaro.org/WorkingGroups/ToolChain/BuildingGNUToolchains]

This page is a work in progress.

The following topics are not yet explained (highest priority items at the top)

  • building using a pre-existing sysroot
  • setting default target architecture and default cpu tuning
  • multilib
  • multiarch
  • *-unknown-elf-* configurations (newlib)
  • building canadian cross

Introduction

Currently, only linux cross compilers are explained. Building a bare metal cross compiler is similar, but glibc is not used, and another C library (eg newlib) is used in its place.

To build a cross compiler without a pre-existing sysroot, the following process is followed:

  1. build binutils
  2. build a minimal GCC C compiler, which is suitable for building the C library
  3. install kernel header files
  4. build C library
  5. build a full GCC

Environment variables

Shell commands in following sections assume the following environment variables have been set up. These have no special meaning other than that they are used in the commands in this document.

Example values are shown

    # configure the build
TRIPLET=arm-unknown-linux-gnueabi # or aarch64-unknown-linux-gnu
LINUX_ARCH=arm # use arm64 if building for an aarch64 target
CPUS=8 # set the number of CPUs for parallel builds
# set paths
WORK=$HOME/work
TOOLS_PATH=$HOME/tools
SYSROOT_PATH="$HOME/sysroot-$TRIPLET" # or path of existing sysroot if you are using a prebuilt one

GCC Configure options

Other gcc configure options may be selected. If building arm-unknown-linux-gnueabihf, then additional options are required to set the default behaviour of the compiler to use the hard float ABI:

The commands below use the GCC_CONF shell variable to set the additional configuration options

    GCC_CONF="--with-arch=armv7-a --with-tune=cortex-a9 --with-fpu=vfpv3-d16 --with-float=hard"

This sets the default target architecture and tuning and hard floating point ABI.

Build dependencies

!!! TODO explain how to get and build gmp etc

Install gcc build dependencies, including but not limited to: gmp-devel libmpc-devel mpfr-devel flex byacc bison. With a recent Linux distribution, installing the provided packages may be sufficient (eg apt-get build-dep gcc)

Downloading sources

Source code for gcc, binutils and glibc may be obtained from your favourite place. I used:

!!! TODO link to tarballs of released versions here

It helps to download each of these into their own subdirectory, eg

    WORK=$HOME/work
mkdir -p "$WORK/binutils" "$WORK/gcc" "$WORK/glibc/" "$WORK/kernel"
cd $WORK/binutils; git clone git://git.linaro.org/toolchain/binutils.git
cd $WORK/gcc; svn co svn://gcc.gnu.org/svn/gcc/branches/linaro/gcc-4_8-branch
cd "$WORK/glibc"; git clone git://git.linaro.org/toolchain/glibc.git)&
cd "$WORK/kernel"; git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git

Building binutils

Binutils is configured for the target platform, and we specify the path to the sysroot, even though we haven't build the sysroot yet. This doesn't matter, as we won't do anything which needs the sysroot until we build the 2nd stage gcc.

    cd "$WORK/binutils"
rm -Rf build
mkdir build
cd build
../binutils/configure --prefix=$TOOLS_PATH --target=$TRIPLET --with-sysroot=$SYSROOT_PATH
make -j${CPUS:-1}
make -j${CPUS:-1} install

Preparing a sysroot (eglibc or glibc)

If you have an existing sysroot, then you should extract it and set SYSROOT_PATH to point to it. You can then skip the remainder of this section.

If you don't have an existing sysroot, then you must build one. This is done with the following steps:

BUILDING 1ST STAGE GCC

The purpose of the 1st stage gcc is to create a minimal compiler which is capable of building glibc. A full build of gcc depends on the C library, but we can't build the C library until we have a C compiler, so we have to build this simplified compiler to break the circular dependency.

The important parts of the configure line are --enable-languages=c (we just need a C compiler for now, and will build other languages at stage 2) and --without-headers which configures the compiler so that it does not need the C library. --disable-threads is needed to work around a bug/problem which means the build process still looks for pthread.h even if --without-headers is used. A variety of other --disable-* options are used to disable other features which are not appropriate for a stage 1 compiler.

The configure and make steps have to be run with the binutils tools on the path, hence the PATH=... part of the command lines below.

    cd "$WORK/gcc"
rm -Rf build
mkdir build
cd build PATH=$TOOLS_PATH/bin:$PATH ../gcc-4_8-branch/configure --prefix=$TOOLS_PATH --enable-languages=c --without-headers --target=$TRIPLET --disable-libmudflap -disable-libatomic --disable-threads --disable-shared --enable-static --disable-decimal-float --disable-libgomp --disable-libitm --disable-libmudflap --disable-libquadmath --disable-libsanitizer --disable-libssp $GCC_CONF
PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1}
PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1} install

INSTALLING KERNEL HEADERS

Before we can build glibc, we need to install the Linux kernel headers. These define the ABI for the Linux syscalls which glibc uses. This step only needs to know which target platform and where to put the header files.

    cd "$WORK/kernel/linux-2.6"
make headers_install ARCH=$LINUX_ARCH INSTALL_HDR_PATH=$SYSROOT_PATH/usr/

BUILDING GLIBC

When configuring glibc it is required to use --prefix=/usr, as this is handled specially in the build system, so that the correct linux filesystem layout is produced. (reference: http://www.eglibc.org/archives/patches/msg00078.html)

Other options are required so that the build finds the kernel headers we installed. !!! TODO explain difference between --host and --target

    cd "$WORK/glibc"
rm -Rf build
mkdir build
cd build
# http://www.eglibc.org/archives/patches/msg00078.html
PATH=$TOOLS_PATH/bin:$PATH ../glibc/configure --prefix=/usr --build=x86_64-unknown-linux-gnu --host=$TRIPLET --target=$TRIPLET --with-headers=$SYSROOT_PATH/usr/include --includedir=/usr/include

Then we build and install glibc. Note that install_root must be set, as we set the prefix to /usr in the configure step, but we want to install into our new sysroot not into the system /usr directory.

    PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1}
PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1} install install_root="$SYSROOT_PATH"

On aarch64, the make install step doesn't create a directory called /usr/lib inside the sysroot. However, if this directory doesn't exist, then gcc won't look for libraries in /usr/lib64, which means no program can be linked. (This causes libgcc to fail to build during the 2nd stage gcc compile, with "cannot find crti.o: No such file or directory")

    mkdir -p "$SYSROOT_PATH/usr/lib"

Building 2nd stage gcc

Now, we can build the final gcc.

    cd "$WORK/gcc"
rm -Rf build2
mkdir build2
cd build2 PATH=$TOOLS_PATH/bin:$PATH ../gcc-4_8-branch/configure --prefix=$TOOLS_PATH --enable-languages=c,c++,fortran --target=$TRIPLET --with-sysroot=$SYSROOT_PATH $GCC_CONF
PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1}
PATH=$TOOLS_PATH/bin:$PATH make -j${CPUS:-1} install

Testing

You should now be able to use $TOOLS_PATH/bin/$TRIPLET-gcc to create new binaries.

!!! TODO how do we run make check on a cross compiler?

WorkingGroups/ToolChain/BuildingGNUToolchains (最后修改时间 2013-09-12 20:50:27)

Howto Building GNU Toolchains的更多相关文章

  1. CentOS 6.7 中安装Emacs 24.5

    Emacs 版本:http://mirror.bjtu.edu.cn/gnu/emacs/emacs-24.5.tar.gz CentOS 内核版本:2.6.32-573.el6.x86_64 参考资 ...

  2. [转]Whirlwind Tour of ARM Assembly

    ref:http://www.coranac.com/tonc/text/asm.htm 23.1. Introduction Very broadly speaking, you can divid ...

  3. CentOS 中安装和卸载 Emacs

    日志更新记录: [1] 增加 CentOS7.2 (内核版本:3.10.0-514.16.1.el7.x86_64)下Emacs25.2 的安装过程,它与本文的步骤完全一样. Emacs 版本:htt ...

  4. How-to Install VMware Tools on Debian Stretch 9 32/64bit Linux+GNU

    在虚拟机VMWARE上安装debian9 安装vmwaretools时候遇到问题 询问我IFCONFIG安装在哪里? 新版的debian不知道是用户权限问题还是使用了其他网络配置工具 vmwareto ...

  5. OpenSSL Command-Line HOWTO

    OpenSSL Command-Line HOWTO The openssl application that ships with the OpenSSL libraries can perform ...

  6. http://wiki.apache.org/tomcat/HowTo

    http://wiki.apache.org/tomcat/HowTo Contents Meta How do I add a question to this page? How do I con ...

  7. Networked Graphics: Building Networked Games and Virtual Environments (Anthony Steed / Manuel Fradinho Oliveira 著)

    PART I GROUNDWORK CHAPTER 1 Introduction CHAPTER 2 One on One (101) CHAPTER 3 Overview of the Intern ...

  8. 再次编译 arm toolchains

    为什么说再呢,因为已经好多次了.每次失败,都再从失败的地方开始.今天这篇呢,主要是记录今天的进展. 1. 编译要分三步走 之前学习的时候就有印象,要三步走.但是因为没有实践过,所以,忘差不多了.所谓三 ...

  9. 【译】GNU Radio How to write a block 【如何开发用户模块及编写功能块】

    本文讲解如何在GNU Radio中添加用户开发的信号处理模块,译文如有不当之处可参考原文地址:http://gnuradio.microembedded.com/outoftreemodules Ou ...

随机推荐

  1. flask中利用from来进行对修改修改时旧密码的验证

    在flask中,肯定是post提交个from进行密码验证.还有一定就是修改密码肯定是登录之后才能进行对密码的修改,这么说,在浏览器中的session中一定会有用户的信息,可以通过相对应的信息去获取到相 ...

  2. Unity游戏开发常用的一些函数用法

    Unity游戏开发常用函数 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享. ...

  3. URL diff URI

    很多人会混淆这两个名词. URL:(Uniform/Universal Resource Locator 的缩写,统一资源定位符). URI:(Uniform Resource Identifier ...

  4. Javascript中的Bind,Call和Apply

    http://www.html-js.com/article/JavaScript-functional-programming-in-Javascript-Bind-Call-and-Apply?s ...

  5. 两道dp

    链接:https://ac.nowcoder.com/acm/contest/186/C?&headNav=www 来源:牛客网终于Alice走出了大魔王的陷阱,可是现在傻傻的她忘了带武器了, ...

  6. 炫龙笔记本的gtx965m显卡玩游戏很卡

    这是我遇到的问题,我2016年10月份这样买了一款笔记本,主要看的是性价比吧!神舟.炫龙都是性价比,所以买了炫龙笔记本 配置如下 cpu:i7 4870hq 显卡:gtx965m 内存条:16G 固态 ...

  7. 【BZOJ4817】【SDOI2017】树点染色

    不算学会lct...... 原题: Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种 ...

  8. C++编译器报错汇总

    1.error: ‘Person’ was not declared in this scope(1)若是一个类或函数的命名空间对使用者不可见(2)成员(静态)函数没有通过对象名或类名进行调用(3)虽 ...

  9. Singer 学习四 可视化数据操作工具

    knots 是一款基于electron 开发的可视化UI界面,我们可以此工具进行方便的数据处理,注意工具使用了 docker 运行,需要安装docker 下载地址   https://github.c ...

  10. 转载:扒一扒Profiler中这几个“占坑鬼”

    https://blog.uwa4d.com/archives/presentandsync.html WaitForTargetFPS.Gfx.WaitForPresent 和 Graphics.P ...