Lab 4 - GCC Build Lab (Part 2)

 Continuing from Part 1

In [Part 1], I covered the preparation and initial setup for building GCC from source. After successfully setting up the build environment, I proceeded with compiling and testing the compiler. However, I encountered an unexpected issue that made me restart the build process from scratch.


Unexpected Issue: Log File Mistake & Missing Build Time

After running make, I needed to record the build time for analysis. However, while checking my logs, I realized two problems:


1️⃣ Wrong Log File Name

I initially used the command:

cd ~/git/gcc

git log -1

Later, when trying to find the build time, I searched in build.log instead of guild.log, assuming that was the correct file.

Since the logs were stored in the wrong filename (guild.log instead of build.log), I couldn’t find the time records when searching.


2️⃣ Make Command Had Failed in the First Attempt

Even after realizing the log filename mistake, I still couldn’t find the build time inside guild.log.

After further investigation, I discovered that my initial make command had failed due to missing targets, meaning the build never actually ran properly in the first place!

This meant I had no valid build time recorded at all and needed to redo everything from scratch.


Rebuilding GCC from Scratch (Properly This Time)

To fix these mistakes, I deleted everything and started fresh with the correct logging process:

1. Clean up previous build attempts:

rm -rf ~/gcc-build-001 ~/gcc-test-001 build.log guild.log


2. Reconfigure the build properly:

~/git/gcc/configure --prefix=$HOME/gcc-test-001


3. Run make again, ensuring the build log is correctly saved:

time make -j 24 |& tee build.log





Now, with the correct file name and a successful build.


4. Run `make install` to complete the installation:

After the build process was successfully completed, I needed to install the newly built GCC to the specified directory. 

make install


Verifying the Newly Built GCC Compiler

After successfully installing the newly built GCC, the next step was to verify that it could compile and run a simple C program. To do this, I created a basic test file and compiled it using the newly built GCC.


1️⃣ Confirm That My GCC is Different from the System GCC

To check whether I was using the newly built GCC instead of the system default, I ran:

which gcc

Then, I explicitly called my custom-built GCC:

$HOME/gcc-test-001/bin/gcc --version






This confirmed that the correct version was running!


2️⃣ Compile a Simple C Program to Test GCC

To ensure the code was properly formatted, I used the following command to create test.c:

cat > test.c <<EOF #include <stdio.h> int main() { printf("Hello, GCC!\n"); return 0; } EOF

Once the source code was ready, I compiled it using the newly built GCC:

$HOME/gcc-test-001/bin/gcc test.c -o test

This command generates an executable file named test, allowing me to test whether the new compiler is functioning correctly.

To confirm that the compilation was successful, I executed the program:

./test

If everything was set up correctly, the output should be:







This confirmed that the newly built GCC was working as expected and could successfully compile and execute C programs.


Rebuilding After Modifying a Single File

After successfully building GCC, I needed to test how the build process reacts when modifying just one source file. This step helps demonstrate how make efficiently recompiles only the changed parts of the code instead of rebuilding everything from scratch.


1️⃣ Updating a Single File

To simulate a small change in the source code, I updated the timestamp of passes.cc. Instead of modifying the content, I used the touch command:

touch ~/git/gcc/gcc/passes.cc

This command updates the file’s last modified timestamp without making any actual changes to the content.


2️⃣ Rebuilding GCC

After modifying the file, I triggered the rebuild process:

time make -j 24 |& tee rebuild.log

This rebuild should be significantly faster than the first full build because only the modified file (and any dependencies) needs to be recompiled.


3️⃣ Rebuild Time Comparison





The initial build took 47 minutes, but this rebuild was completed in under 1 minute, proving that only the necessary parts were recompiled.


Null Rebuild - Ensuring No Changes Trigger a Rebuild

After completing the incremental rebuild in the previous step, the next task was to perform a null rebuild. This step ensures that when no source files have changed, make does not unnecessarily recompile anything.


Running a Null Rebuild

To test this, I executed the following command:

time make -j 24 |& tee null-rebuild.log

This command captures the build time and logs the output to null-rebuild.log for later reference.

A null rebuild should complete much faster than a regular rebuild because make only checks dependencies and confirms that everything is already up to date.

Checking the last few lines of null-rebuild.log with:

tail -n 10 null-rebuild.log


Here’s what I observed in my output:








This confirms that the process finished in just 13 seconds, significantly faster than both the full build (47 minutes) and the incremental rebuild (48 seconds).

The output showed multiple "Leaving directory" messages, confirming that make simply traversed the directories without recompiling anything.


Final Verification: Testing the Newly Built GCC

After successfully rebuilding GCC and confirming that incremental builds worked as expected, it was time for the final step: verifying that my compiled GCC could actually compile and run a program.

Instead of creating a new test file, I reused the test.c file I had created earlier when first attempting to compile a program. This ensured consistency in the verification process.







And the output confirmed success.



To be continued in next posting!



Comments

Popular posts from this blog

SPO600 2025 Winter Project - Stage 1: Create a Basic GCC Pass (part1)

SPO600 2025 Winter Project - Stage 2: GIMPLE Level Clone Analysis and Pruning (part4)

Lab 1 - 6502 Assembly Language