Files
bsb2/tools/build.mk
Niklas Gollenstede 4245798955 fixes for A2
mostly include some things that should and remove some things that shouldn't have been included in the handout (yes this does hint at some places that need to be touched for A2)
2025-11-24 15:04:26 +01:00

158 lines
5.6 KiB
Makefile

# Build the kernel
# Top Level Folder all generated files will be placed in.
ROOTBUILDDIR = build
# Folder the generated files will be placed in.
BUILDDIR ?= $(ROOTBUILDDIR)
# Build folder suffixes
OPTTAG = opt
NOOPTTAG = noopt
DBGTAG = dbg
VERBOSETAG = verbose
# C++
CXX ?= $(PREFIX)clang++
# on macos the default compiler is called c++, switch to clang++
ifeq ($(shell uname),Darwin)
ifeq ($(CXX),c++)
CXX := clang++
endif
endif
CXXFLAGS_ARCH = -m64
CXXFLAGS_DEFAULT = -std=c++23 -ffreestanding -fno-pic -nodefaultlibs -nostdlib -nostdinc -I. -fno-rtti -fno-exceptions -Wno-write-strings -fno-stack-protector -mno-red-zone -g -gdwarf-2 -fno-use-cxa-atexit -no-pie -nostartfiles
# Enable 16-byte compare-and-exchange instruction for debugging purposes (stack alignment)
CXXFLAGS_DEFAULT += -mcx16
CXXFLAGS_OPT = -O3 -fomit-frame-pointer
CXXFLAGS_WARNING = -Wall -Wextra -Werror -Wno-error=unused-parameter -Wno-error=unused-variable -Wno-non-virtual-dtor
CXXFLAGS_CLANG = -Wno-error=unused-private-field -Wno-implicit-exception-spec-mismatch -Wno-error=unused-const-variable -Wno-unused-command-line-argument -Wno-unused-const-variable -fno-strict-aliasing
ifeq ($(shell uname),Darwin)
CXXFLAGS_CLANG += -target x86_64-pc-linux-gnu -fuse-ld=lld
endif
CXXFLAGS_GCC = -fno-tree-loop-distribute-patterns -Wstack-usage=4096 -Wno-error=stack-usage=
CXXFLAGS_NOFPU = -mno-mmx -mno-sse -mgeneral-regs-only
CXXFLAGS = $(CXXFLAGS_ARCH) $(CXXFLAGS_DEFAULT) $(CXXFLAGS_OPT) $(CXXFLAGS_NOFPU) $(CXXFLAGS_WARNING)
# Compiler specific flags
ifneq (,$(findstring clang,$(CXX)))
COMPILER := CLANG
CXXFLAGS += $(CXXFLAGS_CLANG)
else ifneq (,$(findstring g++,$(CXX)))
COMPILER := GCC
# Configures -Warray-bounds warning to allow accessing in the NULL page
# Necessary to read the Extended BIOS Data Area at 0x40e
ifeq "$(shell expr `$(CXX) -dumpversion | cut -f1 -d.` \>= 12)" "1"
CXXFLAGS += --param=min-pagesize=0x40e
endif
CXXFLAGS += $(CXXFLAGS_GCC)
else
COMPILER :=
endif
# Assembly
ASM = nasm
ASMFLAGS = -f elf64
# Additional build utilities
# If llvm tools are available, use them
ifneq (,$(shell which llvm-objcopy 2>/dev/null))
PREFIX = llvm-
endif
OBJCOPY = $(PREFIX)objcopy
STRIP = $(PREFIX)strip
AR = $(PREFIX)ar
# Subdirectories with sources
VPATH = $(sort $(dir $(CC_SOURCES) $(ASM_SOURCES)))
# Lists of object files that are generated by compilation:
# Note that the variables containing the input files are to be defined by
# the Makefiles prior to including this common.mk.
CC_OBJECTS = $(addprefix $(BUILDDIR)/,$(CC_SOURCES:.cc=.o))
DEP_FILES = $(addprefix $(BUILDDIR)/,$(CC_SOURCES:.cc=.d) $(addsuffix .d,$(ASM_SOURCES)))
ASM_OBJECTS = $(addprefix $(BUILDDIR)/,$(addsuffix .o,$(ASM_SOURCES)))
# Dependency files
$(BUILDDIR)/%.d : %.cc $(MAKEFILE_LIST)
@echo "DEP $<"
@mkdir -p $(@D)
$(VERBOSE) $(CXX) $(CXXFLAGS) -MM -MT $(BUILDDIR)/$*.o -MF $@ $<
$(BUILDDIR)/%.asm.d : %.asm $(MAKEFILE_LIST)
@echo "DEP $<"
@mkdir -p $(@D)
$(VERBOSE) $(ASM) $(ASMFLAGS) -M -MT $(BUILDDIR)/$*.asm.o -MF $@ $<
# Object files
$(BUILDDIR)/%.o : %.cc $(MAKEFILE_LIST)
@echo "CXX $<"
@mkdir -p $(@D)
$(VERBOSE) $(CXX) -c $(CXXFLAGS) -o $@ $<
$(BUILDDIR)/%.asm.o : %.asm $(MAKEFILE_LIST)
@echo "ASM $<"
@mkdir -p $(@D)
$(VERBOSE) $(ASM) $(ASMFLAGS) -o $@ $<
# The standard target 'clean' removes the whole generated system, the object files, and the dependency files.
clean::
rm -rf "$(BUILDDIR)"
rm -rf doc
# Target issuing a nested call to make generating a fully optimized systems without assertions.
%-$(OPTTAG):
$(VERBOSE) $(MAKE) BUILDDIR="$(BUILDDIR)/$(OPTTAG)" ISODIR="$(ISODIR)/$(OPTTAG)" CXXFLAGS_OPT="-Ofast -fomit-frame-pointer -flto -march=westmere -DNDEBUG" $*
# Target issuing a nested call to make generating a non-optimized system.
%-$(NOOPTTAG):
$(VERBOSE) $(MAKE) BUILDDIR="$(BUILDDIR)/$(NOOPTTAG)" ISODIR="$(ISODIR)/$(NOOPTTAG)" CXXFLAGS_OPT="-O0" $*
# Target issuing a nested call to make generating a system optimized for debugging.
%-$(DBGTAG):
$(VERBOSE) $(MAKE) BUILDDIR="$(BUILDDIR)/$(DBGTAG)" ISODIR="$(ISODIR)/$(DBGTAG)" CXXFLAGS_OPT="-Og -fno-omit-frame-pointer" $*
# Target issuing a nested call to make generating a system with verbose output.
%-$(VERBOSETAG):
$(VERBOSE) $(MAKE) BUILDDIR="$(BUILDDIR)/$(VERBOSETAG)" ISODIR="$(ISODIR)/$(VERBOSETAG)" CXXFLAGS_OPT="-DVERBOSE" $*
# Build the doxygen docs
doc:
PROJECT_NAME=StuBS doxygen ../doxygen/doxy-sra.cfg
# Documentation
help::
@$(echo) "" \
"All targets exist in different flavours in addition to \e[2;3m<name>\e[0m:\n" \
"\e[2;3m<name>\e[0;3m-noopt\e[0m, \e[2;3m<name>\e[0;3m-opt\e[0m, \e[2;3m<name>\e[0;3m-dbg\e[0m, and \e[2;3m<name>\e[0;3m-verbose\e[0m.\n" \
"Targets suffixed with \e[3m-noopt\e[0m are compiled without optimizations,\n" \
"\e[3m-opt\e[0m targets produce a highly optimized binary, while\n" \
"\e[3m-dbg\e[0m targets only use optimizations not hindering debugging.\n" \
"Targets suffixed with \e[3m-verbose\e[0m generate binaries including\n" \
"verbose output (via \e[3mDBG_VERBOSE\e[0m), making such targets useful for debugging.\n" \
"To get a verbose make output, clear VERBOSE, e.g. \e[3mmake VERBOSE=\e[0m.\n" \
"The following targets are available (each target can be suffixed by \e[3m-noopt\e[0m\n" \
"and \e[3m-verbose\e[0m):\n\n" \
" \e[3mall\e[0m Builds $(PROJECT), generating an ELF binary\n\n" \
" \e[3mdoc\e[0m Builds the documention (doc/html/index.html)\n" \
# Print warnings, if appropriate
ifeq (,$(COMPILER))
$(warning Unknown (and potentially unsupported) compiler "$(CXX)"!)
endif
# Include dependency files (generated via gcc flag -MM)
ifneq ($(MAKECMDGOALS),clean)
-include $(DEP_FILES)
endif
# Phony targets
.PHONY: clean doc help