Compilation Example

Hello World Example

In this section, we will show to compile source code with compiler and compiler flags. To get started, let’s start with a simple hello world example we have available in C and C++ as shown below

#include <stdio.h>

int main() {
    printf("Hello, World in C\n");
    return 0;
}
#include <iostream>

int main() {
    std::cout << "Hello, World in C++" << std::endl;
    return 0;
}

Shown below is an example buildspec file that will compile the above source code with the gcc compiler. The compilers section is used to specify the compiler to use that is selected via the name property which applies a regular expression to search for compiler. The compilers are defined in buildtest configuration file see Defining Compilers for more details.

buildspecs:
  hello_world_c_cpp:
    type: script
    executor: generic.local.bash
    description: Hello world compilation in C and C++
    compilers:
      name: ['builtin_gcc']
    run: |
      $BUILDTEST_CC hello.c -o hello_c
      $BUILDTEST_CXX hello.cpp -o hello_cpp
      ./hello_c
      ./hello_cpp

Buildtest will define environment variables like BUILDTEST_CC and BUILDTEST_CXX that point to the C and C++ compiler wrapper for the selected compiler.

Let’s try to run the code and inspect the test output and test file.

buildtest build -b tutorials/compilation/hello_world_compilation.yml
$ buildtest build -b tutorials/compilation/hello_world_compilation.yml
╭───────────────────────────── buildtest summary ──────────────────────────────╮
│                                                                              │
│ User:               docs                                                     │
│ Hostname:           build-24186293-project-280831-buildtest                  │
│ Platform:           Linux                                                    │
│ Current Time:       2024/04/25 15:44:37                                      │
│ buildtest path:     /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│ buildtest version:  2.0                                                      │
│ python path:        /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│ python version:     3.8.18                                                   │
│ Configuration File: /tmp/tmp0ns2hqkt/config.yml                              │
│ Test Directory:     /tmp/tmp0ns2hqkt/var/tests                               │
│ Report File:        /tmp/tmp0ns2hqkt/var/report.json                         │
│ Command:            /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
───────────────────────────  Discovering Buildspecs ────────────────────────────
                             Discovered buildspecs                              
╔══════════════════════════════════════════════════════════════════════════════╗
║ buildspec                                                                    ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ /home/docs/checkouts/readthedocs.org/user_builds/buildtest/checkouts/devel/t ║
║ utorials/compilation/hello_world_compilation.yml                             ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ Total: 1                                                                     ║
╚══════════════════════════════════════════════════════════════════════════════╝


Total Discovered Buildspecs:  1
Total Excluded Buildspecs:  0
Detected Buildspecs after exclusion:  1
────────────────────────────── Parsing Buildspecs ──────────────────────────────
Valid Buildspecs: 1
Invalid Buildspecs: 0
/home/docs/checkouts/readthedocs.org/user_builds/buildtest/checkouts/devel/tutorials/compilation/hello_world_compilation.yml: VALID
Total builder objects created: 1
                            Builders by type=script                             
┏━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
┃          ┃        ┃          ┃          ┃       ┃       ┃ descrip ┃ buildspe ┃
┃ builder  ┃ type   ┃ executor ┃ compiler ┃ nodes ┃ procs ┃ tion    ┃ cs       ┃
┡━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
│ hello_wo │ script │ generic. │ builtin_ │ None  │ None  │ Hello   │ /home/do │
│ rld_c_cp │        │ local.ba │ gcc      │       │       │ world   │ cs/check │
│ p/f3bb1c │        │ sh       │          │       │       │ compila │ outs/rea │
│ 30       │        │          │          │       │       │ tion in │ dthedocs │
│          │        │          │          │       │       │ C and   │ .org/use │
│          │        │          │          │       │       │ C++     │ r_builds │
│          │        │          │          │       │       │         │ /buildte │
│          │        │          │          │       │       │         │ st/check │
│          │        │          │          │       │       │         │ outs/dev │
│          │        │          │          │       │       │         │ el/tutor │
│          │        │          │          │       │       │         │ ials/com │
│          │        │          │          │       │       │         │ pilation │
│          │        │          │          │       │       │         │ /hello_w │
│          │        │          │          │       │       │         │ orld_com │
│          │        │          │          │       │       │         │ pilation │
│          │        │          │          │       │       │         │ .yml     │
└──────────┴────────┴──────────┴──────────┴───────┴───────┴─────────┴──────────┘
──────────────────────────────── Building Test ─────────────────────────────────
hello_world_c_cpp/f3bb1c30: Creating Test Directory: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30
──────────────────────────────── Running Tests ─────────────────────────────────
Spawning 1 processes for processing builders
───────────────────────────────── Iteration 1 ──────────────────────────────────
hello_world_c_cpp/f3bb1c30 does not have any dependencies adding test to queue
   Builders Eligible to Run   
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Builder                    ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ hello_world_c_cpp/f3bb1c30 │
└────────────────────────────┘
hello_world_c_cpp/f3bb1c30: Current Working Directory : /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/stage
hello_world_c_cpp/f3bb1c30: Running Test via command: bash hello_world_c_cpp_build.sh
hello_world_c_cpp/f3bb1c30: Test completed in 3.526645 seconds with returncode: 0
hello_world_c_cpp/f3bb1c30: Writing output file -  /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp.out
hello_world_c_cpp/f3bb1c30: Writing error file - /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp.err
                                  Test Summary                                  
┏━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━┓
┃ builder                 ┃ executor           ┃ status ┃ returncode ┃ runtime ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━┩
│ hello_world_c_cpp/f3bb1 │ generic.local.bash │ PASS   │ 0          │ 3.527   │
│ c30                     │                    │        │            │         │
└─────────────────────────┴────────────────────┴────────┴────────────┴─────────┘



Passed Tests: 1/1 Percentage: 100.000%
Failed Tests: 0/1 Percentage: 0.000%


Adding 1 test results to report file: /tmp/tmp0ns2hqkt/var/report.json
Writing Logfile to /tmp/tmp0ns2hqkt/var/logs/buildtest_xf6_uwhg.log
$ buildtest inspect query -o -t hello_world_c_cpp
──────────── hello_world_c_cpp/f3bb1c30-32d6-455b-8548-387a6a1c6b49 ────────────
Executor: generic.local.bash
Description: Hello world compilation in C and C++
State: PASS
Returncode: 0
Runtime: 3.526645 sec
Starttime: 2024/04/25 15:44:37
Endtime: 2024/04/25 15:44:40
Command: bash hello_world_c_cpp_build.sh
Test Script: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp.sh
Build Script: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp_build.sh
Output File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp.out
Error File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compilation/hello_world_c_cpp/f3bb1c30/hello_world_c_cpp.err
Log File: /tmp/tmp0ns2hqkt/var/logs/buildtest_xf6_uwhg.log
─ Output File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_comp… ─
Hello, World in C                                                               
Hello, World in C++                                                             
                                                                                
─ Test File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/hello_world_compil… ─
#!/usr/bin/bash                                                                 
set -eo pipefail                                                                
BUILDTEST_CC="gcc"                                                              
BUILDTEST_CXX="g++"                                                             
BUILDTEST_FC="gfortran"                                                         
BUILDTEST_CFLAGS=                                                               
BUILDTEST_CXXFLAGS=                                                             
BUILDTEST_FFLAGS=                                                               
BUILDTEST_CPPFLAGS=                                                             
BUILDTEST_LDFLAGS=                                                              
# Content of run section                                                        
$BUILDTEST_CC hello.c -o hello_c                                                
$BUILDTEST_CXX hello.cpp -o hello_cpp                                           
./hello_c                                                                       
./hello_cpp

Please note that the compiler definition for builtin_gcc is a canonical name to reference to system GNU compiler that is set to /usr/bin/gcc, /usr/bin/g++. The compiler details can be extracted from the configuration file via buildtest config compilers list command. Shown below is the YAML output of the compiler details.

$ buildtest config compilers list --yaml
gcc:
  builtin_gcc:
    cc: gcc
    cxx: g++
    fc: gfortran

STREAM Benchmark Example

In this next section, we will run the STREAM memory benchmark. In order to run this example, we will need to download the source code and compile the source code. We will use the curl command to download the source code. The default section is used to specify default compiler settings for compiler, this may include compiler options and environment variables. The cflags option is responsible for setting C compiler flags which is set to environment variable BUILDTEST_CFLAGS that we can access in the run section. The env section is used to set environment variables that are used in the run section.

buildspecs:
  stream_openmp_c:
    executor: generic.local.bash
    type: script
    description: "STREAM Microbenchmark C Test with OpenMP"
    tags: ["benchmark"]
    compilers:
      name: ['(builtin_gcc)']
      default:
        gcc:
          cflags: -fopenmp -O2
          env:
           OMP_NUM_THREADS: 8
    run: |
      curl https://www.cs.virginia.edu/stream/FTP/Code/stream.c -o stream.c 
      $BUILDTEST_CC $BUILDTEST_CFLAGS stream.c -o stream
      ./stream
buildtest build -b tutorials/compilation/stream.yml
$ buildtest build -b tutorials/compilation/stream.yml
╭───────────────────────────── buildtest summary ──────────────────────────────╮
│                                                                              │
│ User:               docs                                                     │
│ Hostname:           build-24186293-project-280831-buildtest                  │
│ Platform:           Linux                                                    │
│ Current Time:       2024/04/25 15:44:42                                      │
│ buildtest path:     /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│ buildtest version:  2.0                                                      │
│ python path:        /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│ python version:     3.8.18                                                   │
│ Configuration File: /tmp/tmp0ns2hqkt/config.yml                              │
│ Test Directory:     /tmp/tmp0ns2hqkt/var/tests                               │
│ Report File:        /tmp/tmp0ns2hqkt/var/report.json                         │
│ Command:            /home/docs/checkouts/readthedocs.org/user_builds/buildte │
│                                                                              │
╰──────────────────────────────────────────────────────────────────────────────╯
───────────────────────────  Discovering Buildspecs ────────────────────────────
                             Discovered buildspecs                              
╔══════════════════════════════════════════════════════════════════════════════╗
║ buildspec                                                                    ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ /home/docs/checkouts/readthedocs.org/user_builds/buildtest/checkouts/devel/t ║
║ utorials/compilation/stream.yml                                              ║
╟──────────────────────────────────────────────────────────────────────────────╢
║ Total: 1                                                                     ║
╚══════════════════════════════════════════════════════════════════════════════╝


Total Discovered Buildspecs:  1
Total Excluded Buildspecs:  0
Detected Buildspecs after exclusion:  1
────────────────────────────── Parsing Buildspecs ──────────────────────────────
Valid Buildspecs: 1
Invalid Buildspecs: 0
/home/docs/checkouts/readthedocs.org/user_builds/buildtest/checkouts/devel/tutorials/compilation/stream.yml: VALID
Total builder objects created: 1
                            Builders by type=script                             
┏━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━┳━━━━━━━━━━┳━━━━━━━┳━━━━━━━┳━━━━━━━━━┳━━━━━━━━━━┓
┃          ┃        ┃          ┃          ┃       ┃       ┃ descrip ┃ buildspe ┃
┃ builder  ┃ type   ┃ executor ┃ compiler ┃ nodes ┃ procs ┃ tion    ┃ cs       ┃
┡━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━╇━━━━━━━━━━╇━━━━━━━╇━━━━━━━╇━━━━━━━━━╇━━━━━━━━━━┩
│ stream_o │ script │ generic. │ builtin_ │ None  │ None  │ STREAM  │ /home/do │
│ penmp_c/ │        │ local.ba │ gcc      │       │       │ Microbe │ cs/check │
│ 4022da87 │        │ sh       │          │       │       │ nchmark │ outs/rea │
│          │        │          │          │       │       │  C Test │ dthedocs │
│          │        │          │          │       │       │ with    │ .org/use │
│          │        │          │          │       │       │ OpenMP  │ r_builds │
│          │        │          │          │       │       │         │ /buildte │
│          │        │          │          │       │       │         │ st/check │
│          │        │          │          │       │       │         │ outs/dev │
│          │        │          │          │       │       │         │ el/tutor │
│          │        │          │          │       │       │         │ ials/com │
│          │        │          │          │       │       │         │ pilation │
│          │        │          │          │       │       │         │ /stream. │
│          │        │          │          │       │       │         │ yml      │
└──────────┴────────┴──────────┴──────────┴───────┴───────┴─────────┴──────────┘
──────────────────────────────── Building Test ─────────────────────────────────
stream_openmp_c/4022da87: Creating Test Directory: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87
──────────────────────────────── Running Tests ─────────────────────────────────
Spawning 1 processes for processing builders
───────────────────────────────── Iteration 1 ──────────────────────────────────
stream_openmp_c/4022da87 does not have any dependencies adding test to queue
  Builders Eligible to Run  
┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ Builder                  ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━┩
│ stream_openmp_c/4022da87 │
└──────────────────────────┘
stream_openmp_c/4022da87: Current Working Directory : /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stage
stream_openmp_c/4022da87: Running Test via command: bash stream_openmp_c_build.sh
stream_openmp_c/4022da87: Test completed in 1.233734 seconds with returncode: 0
stream_openmp_c/4022da87: Writing output file -  /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c.out
stream_openmp_c/4022da87: Writing error file - /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c.err
                                  Test Summary                                  
┏━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━┳━━━━━━━━━━━━┳━━━━━━━━━┓
┃ builder                 ┃ executor           ┃ status ┃ returncode ┃ runtime ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━╇━━━━━━━━━━━━╇━━━━━━━━━┩
│ stream_openmp_c/4022da8 │ generic.local.bash │ PASS   │ 0          │ 1.234   │
│ 7                       │                    │        │            │         │
└─────────────────────────┴────────────────────┴────────┴────────────┴─────────┘



Passed Tests: 1/1 Percentage: 100.000%
Failed Tests: 0/1 Percentage: 0.000%


Adding 1 test results to report file: /tmp/tmp0ns2hqkt/var/report.json
Writing Logfile to /tmp/tmp0ns2hqkt/var/logs/buildtest_vno7da_h.log
$ buildtest inspect query -o -t stream_openmp_c
───────────── stream_openmp_c/4022da87-1638-4f00-b437-7c8a293b3b2b ─────────────
Executor: generic.local.bash
Description: STREAM Microbenchmark C Test with OpenMP
State: PASS
Returncode: 0
Runtime: 1.233734 sec
Starttime: 2024/04/25 15:44:42
Endtime: 2024/04/25 15:44:43
Command: bash stream_openmp_c_build.sh
Test Script: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c.sh
Build Script: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c_build.sh
Output File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c.out
Error File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_openmp_c/4022da87/stream_openmp_c.err
Log File: /tmp/tmp0ns2hqkt/var/logs/buildtest_vno7da_h.log
─ Output File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_op… ─
-------------------------------------------------------------                   
STREAM version $Revision: 5.10 $                                                
-------------------------------------------------------------                   
This system uses 8 bytes per array element.                                     
-------------------------------------------------------------                   
Array size = 10000000 (elements), Offset = 0 (elements)                         
Memory per array = 76.3 MiB (= 0.1 GiB).                                        
Total memory required = 228.9 MiB (= 0.2 GiB).                                  
Each kernel will be executed 10 times.                                          
 The *best* time for each kernel (excluding the first iteration)                
 will be used to compute the reported bandwidth.                                
-------------------------------------------------------------                   
Number of Threads requested = 8                                                 
Number of Threads counted = 8                                                   
-------------------------------------------------------------                   
Your clock granularity/precision appears to be 1 microseconds.                  
Each test below will take on the order of 7141 microseconds.                    
   (= 7141 clock ticks)                                                         
Increase the size of the arrays if this shows that                              
you are not getting at least 20 clock ticks per test.                           
-------------------------------------------------------------                   
WARNING -- The above is only a rough guideline.                                 
For best results, please be sure you know the                                   
precision of your system timer.                                                 
-------------------------------------------------------------                   
Function    Best Rate MB/s  Avg time     Min time     Max time                  
Copy:           13614.6     0.011875     0.011752     0.012183                  
Scale:          13633.4     0.011864     0.011736     0.012139                  
Add:            14252.8     0.017107     0.016839     0.017411                  
Triad:          14138.3     0.017134     0.016975     0.017373                  
-------------------------------------------------------------                   
Solution Validates: avg error less than 1.000000e-13 on all three arrays        
-------------------------------------------------------------                   
                                                                                
─ Test File: /tmp/tmp0ns2hqkt/var/tests/generic.local.bash/stream/stream_open… ─
#!/usr/bin/bash                                                                 
set -eo pipefail                                                                
BUILDTEST_CC="gcc"                                                              
BUILDTEST_CXX="g++"                                                             
BUILDTEST_FC="gfortran"                                                         
BUILDTEST_CFLAGS="-fopenmp -O2"                                                 
BUILDTEST_CXXFLAGS=                                                             
BUILDTEST_FFLAGS=                                                               
BUILDTEST_CPPFLAGS=                                                             
BUILDTEST_LDFLAGS=                                                              
export OMP_NUM_THREADS="8"                                                      
# Content of run section                                                        
curl https://www.cs.virginia.edu/stream/FTP/Code/stream.c -o stream.c           
$BUILDTEST_CC $BUILDTEST_CFLAGS stream.c -o stream                              
./stream