DEV Community

arinak1017
arinak1017

Posted on

Building GCC from Source on AArch64

Before we start...

If you have just stumbled upon my SPO600 series of blog posts, it has been created to document and share my learnings as I progress through my Software Portability and Optimization college course.

In this post, I’ll share my experience of building the GCC compiler from source on my college's AArch64 server. This setup is an important first step of our class project: adding a new experimental feature to the GNU Compiler Collection (GCC).

A little bit about the project

One of the cool things about Seneca’s Software Portability and Optimization course is that each semester, students get a chance to gain hands-on experience by working with real-world projects.

Our Fall 2024 batch will attempt to create a working prototype for function pruning (which is a component of the yet-to-be-implemented AFMV (automatic function multi-versioning)) for the GCC. I’ll dive deeper into the specifics of the project in future posts, but to put it simply, our focus will be on figuring out how to detect when two different versions of a function are "fundamentally the same".

Useful Links:

Building GCC on AArch64

Wait, why would we even build GCC from source?

Being able to build a local copy of GCC is crucial for any contributor, as it allows you to test the changes you make directly on your build. (You can think of it as setting up the dev environment for the project.)

How I built GCC on the school's AArch64 server

To perform the build I have followed the instructions on the guide page of our class wiki while also referring to the guides linked above for extra details.

1. Obtaining the Source Code

I cloned the source code from the official git repo into an empty gcc/ directory (created specifically for storing the source code).

git clone git://gcc.gnu.org/git/gcc.git gcc
Enter fullscreen mode Exit fullscreen mode

Notably, cloning took longer the usual due to the size of the repo.

2. Configuring the build

I created the gcc-build-001/ build directory (to separate source files and build files), cd'd into it and ran the configure script (located in the source directory), specifying the installation directory with the --prefix flag.

Running the configure script checks the system requirements
and creates a Makefile with build instructions for GCC in the current directory. (In my case, the college server already had all the necessary dependencies installed. You can find the info about the prerequisites in the docs.)

Note: Building inside the source directories is not recommended!

mkdir ~/gcc-build-001
cd ~/gcc-build-001
$ ~/gcc/configure --prefix=$HOME/gcc-test-build
Enter fullscreen mode Exit fullscreen mode

3. Performing the build

I built the compiler with the following command:

time make -j 5 |& tee build.log
Enter fullscreen mode Exit fullscreen mode

Building the compiler is a complex process that can take anywhere from 20 minutes to several hours depending on your system's specs.

  • To speed up the build, I used the -j flag to specify the maximum number of parallel jobs for make. The wiki recommends setting this value between (number of cores + 1) and (number of cores * 2 + 1), using the higher end for systems with hyper-threading or slower disks and the lower end for those without. Since our college server has 4 cores and does not support hyper-threading, I decided to stick to the lower end of the recommended range by running up to 5 jobs.

  • time is used to measure how long the make process takes to complete. The tee command captures the output of make and writes it to the file build.log while also displaying it in the terminal.

In total, the build process took: 124 minutes and 3.647 seconds

By default, the compiler is built in three stages, following a bootstrapped build process to ensure reliability and consistency in the build. If you are performing a 3-stage bootstrapped build, you can quickly check the current build stage by looking at the stage_current file.
stage_current

4. Installing GCC

I installed the build in the directory specified in the configuration step by running the following command in the build directory:

make install
Enter fullscreen mode Exit fullscreen mode

5. Using the local copy of the compiler

To use the newly installed compiler instead of the system's default GCC, I modified the $PATH to point to the local build directory:

.bashrc

Then, I ran this command to apply the changes:

source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

After that, I was finally able to use my local copy of GCC 😃 🎉

result

Afterthoughts

This experience helped me get familiar with the build process: I learned about various configuration options, the three stages of the bootstrapped build, and got a peek at the codebase. Going through the related documentation also gave me a better understanding of the contribution process.

While this has been a solid start, I still have a lot of work ahead. To start contributing, I'll need to get comfortable with navigating the codebase and adapting to GCC’s coding style, so there’s a lot more documentation reading and code experimenting to come!

Top comments (0)