Lab 4 - GCC Build

In this post, I will build a GCC compiler that is different from the system one. Let's try to build our custom GCC compiler!

As required, I have to build it on both the x86-001 and aarch64-022 servers.
To access the servers, I need to use the commands:
x86_64 server
ssh username@x86-001.spo600.cdot.systems
aarch64-002 server
ssh username@aarch64-002.spo600.cdot.systems
After logging in, I first checked if Git was installed because I had to obtain the GCC source code from some Git repositories.  To confirm it, I run the code:
git --version
It means Git is installed when you see the Git version.

Then, I obtained the GCC source code from https://gcc.gnu.org/git.html. However, this is not the only way to get the source code. You can also find it from other sources.
To get a copy of the source code, I run the command:
git clone git://gcc.gnu.org/git/gcc.git SomeLocalDir

After the repository is cloned, we can check the file.
First, I run the command ls to check the folder. Then, I go into the folder with the command cd folderName. Last, I run the same ls command to check all the files.

Next, I configured my GCC build to install into a custom directory ($HOME/gcc-test-001) instead of the system directories. This allows me to use GCC without affecting the system's default compiler.
mkdir ~/gcc-build-001
cd ~/gcc-build-001
~/gcc/configure --prefix=$HOME/gcc-test-001

After all the configuration, it is time to perform the build by running the following command:
time make -j 24 |& tee build.log
It uses the Makefile that is generated during the configuration. The build will take around an hour. The runtime depends on your computer's performance, especially the number of CPU threads and overall system capability.


After the build process finishes successfully, I install the compiled GCC by running:
make install
Then, you will see a folder named 'gcc-test-001' generated in our home directory. This is the directory I specified with --prefix during configuration.

The last step is to check the build!
We can run make check to check the build. However, it will take a long time to fully test the build.
make check
To make things easier, I set the custom GCC binary directory to my system's PATH. This allows me to use the custom version of GCC.
PATH=$HOME/gcc-test-001/bin:$PATH
To check which GCC we are using, we can run:
which gcc
When it is using the system GCC, it will show below:

If it is using the custom GCC, it will show the path of our custom GCC binary folder. Also, the GCC version may be different.

After confirming that I was using the custom GCC, I created a simple C program file and tried to compile it.

Simple C program:
#include <stdio.h>

int main(){
        printf("Hello, I am Wing Ho Chau\n");
        return 0;
}
The program will print out my name.

It does what it was supposed to do! I built a custom GCC!
To do it on both servers, I just repeat the same steps on another server.

Some experiment:

I went into the GCC subdirectory to update the timestamp of the file "passes.cc" and rebuild the software in the gcc-build-001 directory.

The directory path should be:
/home/whchau1/gcc/gcc
Update the timestamp by running:
touch passes.cc
Then, we go back directory gcc-build-001 to run:
time make -j 24 |& tee build.log
Since only 1 file is affected, it only less time to build the GCC. It took 3m13.173s to rebuild, but user time is longer. Why? 
The real time is shorter than the user time because the rebuild process was performed using 24 threads. The total time taken for the entire process was 3 minutes and 13.17 seconds, which represents the real time. However, since we used 24 threads for parallel processing, the user time accumulates from the CPU time of each individual thread. This causes the user time to exceed the real time, as each of the 24 threads contributes to the overall CPU time, running in parallel.


Next, I performed a "null rebuild," where I rebuilt the software without making any changes to the source code.
The rebuild only took 22.375 seconds to complete. Since no source code was modified, the process primarily involved traversing the directory trees to check if any updates were required, rather than recompiling any files.

Reflection:

It is fun to know how to build a custom GCC compiler, however, building a custom GCC took a lot of time. Makefile is really useful, as it allows us to run a long command easier once the Makefile is set up.

Comments

Popular posts from this blog

Project - Stage 1: Create a Basic GCC Pass