Build Systems

  • Meta-Build Systems (Generators)
  • Build Tools (Low level Build engines)
  • All-in-One Build System

Meta-Build Systems (Generators)

  • These tools do not compile code themselves
  • They read project config and generate build files for lower level build engine

Autotools

  • It is native to Unix/Linux
  • Files
    • Input: configure.ac, Makefile.am
    • Output: ./configure script, Makefile.in templates, and finally Makefile
  • consists of
    • autoconf
      • Reads your configure.ac file and converts it into the giant, portable ./configure shell script.
    • automake
      • Reads your Makefile.am files and generates standard Makefile.in templates that comply with GNU standards.
    • libtool
      • Safely builds libraries for all operating systems.
    • autoreconf
      • Helper script to run autoconf and automake
  • Generate build files for Makefile only
# Run by the Developer (requires Autotools):
# Generates the './configure' script from configuration files
autoreconf -i
 
# Run by the End-User (does NOT require Autotools)
# Scans the system and generates the local 'Makefile'
./configure

CMake

  • It is cross-platform, and can run on Linux, macOS, Windows
  • Files
    • Input: CMakeLists.txt
    • Output: based on the build system
  • consists of
    • cmake
      • reads CMakeLists.txt and generate build files
      • can perform compilation and installation
    • ctest
      • runs unit tests
    • cpack
      • takes compiled program and automatically packages it into installers like .deb (Ubuntu), .dmg (macOS) or .msi (Windows)
  • Generate build files for:
    • GNU Make (Makefile)
    • Ninja (build.ninja)
    • Visual Studio solution (.sln)
    • Xcode project (.xcodeproj)
# Create a clean build folder
mkdir build && cd build
 
# Configure
cmake ..
 
# Compile (runs lower build tool automatically)
cmake --build .
 
# Install (runs lower build tool automatically)
cmake --install .

Meson

  • It is cross-platform, and can run on Linux, macOS, Windows
  • Generates build files for ninja only
  • Files
    • Input: meson.build
    • Output: build.ninja
  • Used in GNOME, systemd, Mesa (Graphics drivers)

Premake

  • It is cross-platform, and can run on Linux, macOS, Windows
  • Files
    • Input: premake5.lua
    • Output: based on build system
  • Generates build files for
    • GNU Make (Makefile)
    • Visual Studio solution (.sln)
    • Xcode project (.xcodeproj)
  • Used in game development

Build Tools (Low level Build engines)

  • These tools read the files generated by the meta-build tools and talk directly to the compiler (GCC, Clang, MSVC) to compile and link your code

GNU Make

  • It is native to Unix/Linux
  • uses Makefile
# compiles code
make
 
# install
sudo make install

Ninja

  • It is cross-platform, and can run on Linux, macOS, Windows
  • modern and blazing-fast replacement for GNU Make
  • uses build.ninja
  • automatically compiles in parallel using all your CPU cores by default
# compiles code
ninja
 
# install
sudo ninja install

All-in-One Build system

  • These tools handle both configuration and compilation directly without generating intermediate files

Bazel

  • It is cross-platform, and can run on Linux, macOS, Windows
  • Developed by Google for massive codebases (Monorepos)
  • Used by SpaceX, NVIDIA, Uber
  • Used in TensorFlow
  • Uses aggressive cloud caching for extreme speed
  • Files
    • Input Files: WORKSPACE (at project root), BUILD files (in code directories)
    • Output: Compiled binaries/libraries directly

Common Build Environment Variables

  • These are conventions used by build tools

Tool Selection

  • CC: selects the C compiler
    • example: gcc, clang
  • CXX: selects the C++ compiler
    • example: g++, clang++
  • CPP: selects the C preprocessor

Default Build Flags

  • CFLAGS: default flags for C compilation
    • example: -O2 -Wall
  • CXXFLAGS: default flags for C++ compilation
    • example: -std=c++20 -O2 -Wall
  • CPPFLAGS: preprocessor flags
    • example: -Iinclude -DMY_FEATURE=1
  • LDFLAGS: linker search paths and linker-specific flags
    • example: -L/usr/local/lib -Wl,-rpath,/usr/local/lib
  • LDLIBS: libraries to link
    • example: -lpthread -lm

Search Paths

  • CPATH
    • extra header search path for C and C++
  • C_INCLUDE_PATH
    • extra header search path for C
  • CPLUS_INCLUDE_PATH
    • extra header search path for C++
  • LIBRARY_PATH
    • extra library search path for the linker
  • PKG_CONFIG_PATH
    • helps pkg-config find .pc files