Skip to content

Instantly share code, notes, and snippets.

@theunkn0wn1
Created November 9, 2020 20:24
Show Gist options
  • Save theunkn0wn1/75a48aef82701b9cf2aa7d63c320cbe8 to your computer and use it in GitHub Desktop.
Save theunkn0wn1/75a48aef82701b9cf2aa7d63c320cbe8 to your computer and use it in GitHub Desktop.
A random makefile
# Unknown's asm makefile.
# This makefile will detect all *.asm and *.S asm sources and build corresponding .o's
# in the BUILD_DIR.
# once all assembly files are generated / up to date, PROJECT will be linked in the CWD
# from these assembled artefacts in BUILD_DIR.
#
# NOTE:
# This makefile works both in cross-compile situations and native builds
# It has been tested tested against Ubuntu 18.04's armhf crossbuild and on a RPi 4b.
#
# If your system uses a different cross-compiler, you will need to adjust AS, GCC, LD
# # # #
# CONFIGURATION
# User configurable values live in here, in no specific order
# # #
# name of output binary
PROJECT = rasm4
# Assembler to use.
AS=arm-linux-gnueabihf-as
# Path to GCC, used to detect libc and optionally as the linker
GCC = arm-linux-gnueabihf-gcc
# Linker to use.
LD=arm-linux-gnueabihf-ld
# Extra arguments to pass to the assembler.
AS_EXTRA_ARGS = -g
# Extra arguments to pass to the linker
LD_EXTRA_ARGS = -g
# Directory to store build artefacts in.
BUILD_DIR=./build
# # END CONFIGUREABLES
# #
# LIBC path computation, since this varies based on compiler setup.
#
PATH_LIBC = $(shell $(GCC) --print-file-name=libc.so )
PATH_LIBC_LD = $(shell $(GCC) --print-file-name=ld-linux-armhf.so.3)
LD_LIBC_ARGS = $(PATH_LIBC) -dynamic-linker $(PATH_LIBC_LD)
# Compute final additional arguments to pass to the linker
LD_ARGS = $(LD_LIBC_ARGS) $(LD_EXTRA_ARGS)
# Compute final additional arguments to pass to the assembler
AS_ARGS = $(AS_EXTRA_ARGS)
# Instruct make that build artefacts exist in $(BUILD_DIR) and should consult there
# before regenerating them.
VPATH = $(BUILD_DIR)
# specify the default goal explicitly to avoid confusion.
.DEFAULT_GOAL: $(PROJECT)
# Specific object files for this project
#
# Sources ending with .asm
SRCS_asm = $(wildcard *.asm)
# sources anding with .S
SRCS_s = $(wildcard *.S)
# Compute the object file names for the provided sources
# (foo.asm -> foo.o)
OBJS = $(SRCS_asm:.asm=.o) $(SRCS_s:.S=.o)
# as well as their paths in the build directory.
# (foo.asm -> foo.o -> $(BUILD_DIR)/foo.o )
OBJS_paths = $(foreach file, $(OBJS), $(BUILD_DIR)/$(file) )
# Primary build target
$(PROJECT): $(OBJS)
$(LD) $(LD_ARGS) -o $@ $(OBJS_paths) libbarnett.a
# Ensure the build directory exists.
$(BUILD_DIR):
-mkdir $(BUILD_DIR)
# Pattern build rule for *.asm -> *.o
%.o: %.asm
$(AS) $(AS_ARGS) -o $(BUILD_DIR)/$@ $<
# Pattern build rule for *.S -> *.o
%.o: %.S
$(AS) $(AS_ARGS) -o $(BUILD_DIR)/$@ $<
# Pattern build rule for *.s -> *.o
%.o: %.s
$(AS) $(AS_ARGS) -o $(BUILD_DIR)/$@ $<
# Clean up build artefacts present in BUILD_DIR
clean:
rm -f $(wildcard $(BUILD_DIR)/*.o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment