Skip to content

Instantly share code, notes, and snippets.

@theunkn0wn1
Last active September 10, 2021 06:31
Show Gist options
  • Save theunkn0wn1/81f3a5fee1abdb47768ab088832227c8 to your computer and use it in GitHub Desktop.
Save theunkn0wn1/81f3a5fee1abdb47768ab088832227c8 to your computer and use it in GitHub Desktop.
A64 makefile for assembly projects
# Unknown's V1-A64 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 20.04 aarch64's cross-compilers 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 = lab4
# Assembler to use.
AS=aarch64-linux-gnu-as
# Path to GCC, used to detect libc and optionally as the linker
GCC = aarch64-linux-gnu-gcc
# Linker to use.
LD=aarch64-linux-gnu-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-aarch64.so.1)
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)
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) $(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)
# 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