Skip to content

Instantly share code, notes, and snippets.

@diffstorm
Created May 25, 2024 16:49
Show Gist options
  • Save diffstorm/1230092de1bfa2d9b7f8ec4994f72754 to your computer and use it in GitHub Desktop.
Save diffstorm/1230092de1bfa2d9b7f8ec4994f72754 to your computer and use it in GitHub Desktop.
Example makefile to compile applications and bootloaders with various toolchains
# Purpose:
# This makefile is designed to compile applications and bootloaders with various toolchains.
#
# Features:
# - Supports compiling applications and bootloaders separately or together.
# - Allows specifying different compilers for applications and bootloaders within the same configuration.
# - Supports skipping compilation for specific applications or bootloaders using the "none" keyword.
#
# Configuration Format:
# Configurations are specified in the format:
# app:compiler;bootloader:compiler
# - "app" and "bootloader" represent the application and bootloader respectively.
# - "compiler" represents the compiler/toolchain to be used for compilation.
# - Use "none" to skip compilation for the application or bootloader.
#
# Example Configuration:
# app1:gcc;bootloader1:iar
# - This configuration compiles "app1" with GCC and "bootloader1" with IAR.
#
# app2:none;bootloader2:arm-none-eabi-gcc
# - This configuration skips compiling "app2" and compiles "bootloader2" with arm-none-eabi-gcc.
# Configurations
CONFIGURATIONS := \
app1:gcc;bootloader1:iar \
app2:none;bootloader2:arm-none-eabi-gcc
# Extract components from the configuration
define extract_config
$(word 1,$(subst ;, ,$(1)))
endef
define extract_app_toolchain
$(word 2,$(subst :, ,$(word 1,$(subst ;, ,$(1)))))
endef
define extract_bootloader_toolchain
$(word 2,$(subst :, ,$(word 2,$(subst ;, ,$(1)))))
endef
# Set up generic targets
all: $(foreach config, $(CONFIGURATIONS), $(call build_target,$(config)))
# Target-specific variables
define build_target
$(if $(filter none,$(call extract_config,$(1))), \
$(info Skipping target: $(1)), \
$(call build_target_app,$(1)) \
$(call build_target_bootloader,$(1)))
endef
define build_target_app
$(call build_target_impl,$(call extract_config,$(1)), \
$(call extract_app_toolchain,$(1)), \
$(call extract_config,$(1)))
endef
define build_target_bootloader
$(call build_target_impl,$(call extract_config,$(1)), \
$(call extract_bootloader_toolchain,$(1)), \
$(call extract_config,$(1)))
endef
define build_target_impl
$(2) -o $@_$(3) $<
endef
# Set up targets with specific compilers and configurations
$(foreach config, $(CONFIGURATIONS), \
$(eval $(call build_target,$(config))))
.PHONY: clean
clean:
rm -f $(addsuffix _*, $(foreach config, $(CONFIGURATIONS), $(call build_target,$(config))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment