Try Embedded

Make and CMake for embedded

Rules, dependencies, toolchain files and a sane firmware build.

Why a build system

A firmware project has dozens of source files, several compiler flags that must stay consistent, a linker script and post-build steps such as producing a .bin and printing the size. A build system records all of that, rebuilds only what changed, and makes the build identical on every developer's machine and in CI.

Make

Make reads rules of the form target: prerequisites, followed by a recipe (indented with a tab). If any prerequisite is newer than the target, the recipe runs.

TARGET  := firmware
CC      := arm-none-eabi-gcc
OBJCOPY := arm-none-eabi-objcopy
SIZE    := arm-none-eabi-size

CPU     := -mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard
CFLAGS  := $(CPU) -Os -g3 -Wall -Wextra -ffunction-sections -fdata-sections -MMD -MP
CFLAGS  += -Iinc -Idrivers -DSTM32F411xE
LDFLAGS := $(CPU) -T stm32f411.ld -Wl,--gc-sections -Wl,-Map=build/$(TARGET).map --specs=nano.specs

SRCS := $(wildcard src/*.c drivers/*.c) startup/startup_stm32f411.s
OBJS := $(patsubst %,build/%.o,$(basename $(SRCS)))

all: build/$(TARGET).bin

build/$(TARGET).elf: $(OBJS)
	$(CC) $(LDFLAGS) $^ -o $@
	$(SIZE) $@

build/%.bin: build/%.elf
	$(OBJCOPY) -O binary $< $@

build/%.o: %.c
	@mkdir -p $(dir $@)
	$(CC) $(CFLAGS) -c $< -o $@

build/%.o: %.s
	@mkdir -p $(dir $@)
	$(CC) $(CPU) -c $< -o $@

clean:
	rm -rf build

-include $(OBJS:.o=.d)
.PHONY: all clean

Pieces worth knowing:

  • $@ is the target, $< the first prerequisite, $^ all prerequisites.
  • % pattern rules describe how to build any .o from its .c.
  • -MMD -MP makes the compiler write a .d file listing every header each source includes; -include reads them. Without this, editing a header does not rebuild the files that use it, which is the most common Make bug.
  • make -j8 builds in parallel; correct prerequisites are what make that safe.

CMake

CMake does not build anything itself. It generates a build (Makefiles or, more commonly now, Ninja files) from a higher-level description. It is the standard for larger projects, the Zephyr RTOS, the Raspberry Pi Pico SDK and most vendor IDEs' export formats.

cmake_minimum_required(VERSION 3.20)
project(firmware C ASM)

add_executable(firmware
  src/main.c
  drivers/uart.c
  drivers/gpio.c
  startup/startup_stm32f411.s
)

target_include_directories(firmware PRIVATE inc drivers)
target_compile_definitions(firmware PRIVATE STM32F411xE)
target_compile_options(firmware PRIVATE -Os -g3 -Wall -Wextra -ffunction-sections -fdata-sections)
target_link_options(firmware PRIVATE
  -T${CMAKE_SOURCE_DIR}/stm32f411.ld
  -Wl,--gc-sections
  -Wl,-Map=firmware.map
  --specs=nano.specs
)
set_target_properties(firmware PROPERTIES SUFFIX ".elf")

add_custom_command(TARGET firmware POST_BUILD
  COMMAND ${CMAKE_OBJCOPY} -O binary $<TARGET_FILE:firmware> firmware.bin
  COMMAND ${CMAKE_SIZE} $<TARGET_FILE:firmware>
)

The toolchain file

The compiler and CPU flags belong in a separate toolchain file, so the same CMakeLists.txt can build for the target and for the host (for unit tests):

# arm-gcc.cmake
set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER arm-none-eabi-gcc)
set(CMAKE_ASM_COMPILER arm-none-eabi-gcc)
set(CMAKE_OBJCOPY arm-none-eabi-objcopy)
set(CMAKE_SIZE arm-none-eabi-size)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)   # test programs cannot link without a linker script
set(CMAKE_C_FLAGS_INIT "-mcpu=cortex-m4 -mthumb -mfpu=fpv4-sp-d16 -mfloat-abi=hard")
cmake -B build -G Ninja -DCMAKE_TOOLCHAIN_FILE=arm-gcc.cmake -DCMAKE_BUILD_TYPE=MinSizeRel
cmake --build build

Targets, not variables

Modern CMake attaches flags to targets with target_* commands and uses PRIVATE, PUBLIC and INTERFACE to say whether a setting also applies to whoever links the target. Organise drivers as libraries:

add_library(drivers STATIC drivers/uart.c drivers/gpio.c)
target_include_directories(drivers PUBLIC drivers)    # users of drivers get the include path too
target_link_libraries(firmware PRIVATE drivers)

Make or CMake?

MakeCMake
Learning curveSmall, but subtleLarger
Header dependenciesManual (-MMD)Automatic
Host and target buildsTwo Makefiles or many variablesTwo toolchain files
IDE supportLimitedVS Code, CLion and others read it directly
Typical useSmall projects, vendor examplesMedium to large projects, SDKs, Zephyr

Either works. What matters is that the build is one command, reproducible, and runs in CI.

Interview question: How would you set up a build so the same code is compiled for the target and for host unit tests?

Answer: Use CMake with two toolchain files: one for arm-none-eabi-gcc with the CPU flags and linker script, one for the host compiler. Split the code into libraries: hardware-independent logic in one library, drivers and startup code in another. The firmware target links both; the test executable links the logic library with a test framework such as Unity or GoogleTest and fakes for the drivers. CI runs both builds.

Interview question: What belongs in a CMake toolchain file and what belongs in CMakeLists.txt?

Answer: The toolchain file describes the compiler and target: CMAKE_SYSTEM_NAME (Generic for bare metal), the compiler, objcopy and size paths, CPU and FPU flags, and CMAKE_TRY_COMPILE_TARGET_TYPE. CMakeLists.txt describes the project: sources, targets, include directories, definitions, the linker script and post-build steps. Keeping them separate lets one project build for several targets.

Going deeper

Interview question: How would you build debug, release and factory-test firmware from one source tree?

Answer: Use CMake (or Make) with one target per variant and build types for debug and release, selecting variant-specific sources and defines through options, never through copied files. Run all variants in CI from a clean checkout with a pinned toolchain, and embed the variant and version in each binary.

Interview question: Why use Ninja rather than Make as the CMake generator?

Answer: Ninja is designed to be generated rather than hand-written: it starts faster, tracks dependencies precisely, parallelises well by default and rebuilds only what changed, making incremental builds noticeably quicker on large projects. CMake generates it with -G Ninja; the CMakeLists.txt stays the same.

Interview question: How do you make a firmware build reproducible on another machine or years later?

Answer: Pin the toolchain version and run builds in a container or documented environment, keep all dependencies in the repository or pinned by version and hash, avoid embedding timestamps and absolute paths (or use SOURCE_DATE_EPOCH and -ffile-prefix-map), make the build a single command, and run it in CI. Compare binary hashes between two builds to verify.

Explain it: In your own words, explain why editing a header sometimes fails to trigger a rebuild in a hand-written Makefile and how to fix it.

Answer: Make only rebuilds a target when one of its listed prerequisites is newer, and a hand-written rule usually lists only the .c file, so Make does not know which headers each object depends on. Editing a header therefore leaves stale objects. Compiling with -MMD -MP makes the compiler write a .d file listing every header each object included, and including those files in the Makefile gives Make the complete dependency list.