smol-gilbraltar/GNUmakefile

207 lines
6.3 KiB
Text
Raw Permalink Normal View History

2024-12-20 22:45:12 +00:00
RM= rm
CC= $(CONFIG_TARGET_CC)
AS= $(CONFIG_TARGET_CC)
# NOTE(dinosaure): we must use [aarch64-none-elf-gcc] to compile assembly code
# to be able to include some files and define some macros.
LD= $(CONFIG_TARGET_LD)
AR= $(CONFIG_TARGET_AR)
OBJDUMP= $(CONFIG_TARGET_OBJDUMP)
2024-12-20 22:45:12 +00:00
OBJCOPY= $(CONFIG_TARGET_OBJCOPY)
export TOPDIR := $(abspath .)
$(TOPDIR)/Makeconf:
$(error Makeconf not found, please run ./configure.sh)
include $(TOPDIR)/Makeconf
include/$(CONFIG_TARGET_TRIPLE):
@echo "GEN $@"
@./gen-headers.sh $@
CPU ?= -mcpu=cortex-a76 -mlittle-endian
ARCH += -DAARCH=64 $(CPU)
TARGET ?= kernel_2712
DEFINE += -DSSP_GUARD_SYMBOL=__stack_chk_guard \
-DSSP_FAIL_SYMBOL=__stack_chk_fail
INCLUDE += -I $(TOPDIR)/include -I $(TOPDIR)/include/$(CONFIG_TARGET_TRIPLE) \
-I $(TOPDIR)/nolibc/include -I $(TOPDIR)/openlibm/include -I $(TOPDIR)/openlibm/src
2024-12-20 22:45:12 +00:00
AFLAGS += $(ARCH) $(DEFINE) $(INCLUDE)
# CFLAGS & LDFLAGS are used with $(CC). Link arguments must be passed with -Wl
# Also, CONFIG_TARGET_LD_LDFLAGS are specific when we use CONFIG_TARGET_LD
CFLAGS += -ffreestanding -fstack-protector-strong -nostdlib \
2024-12-20 22:45:12 +00:00
-nostartfiles -mstrict-align -Wall \
$(ARCH) $(DEFINE) $(INCLUDE) $(CONFIG_TARGET_CC_CFLAGS)
comma := ,
empty:=
space := $(empty) $(empty)
LDFLAGS += -static -Wl,--no-warn-rwx-segments \
-Wl,--section-start=.init=0x80000 \
-Wl,--start-group \
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
-Wl,--end-group \
-Wl,$(subst $(space),$(comma),${CONFIG_TARGET_CC_LDFLAGS})
LD_LDFLAGS += -static --no-warn-rwx-segments \
--section-start=.init=0x80000 \
--start-group \
-L lib/$(CONFIG_TARGET_TRIPLE) -lgilbraltar -lopenlibm -lnolibc -lgcc \
--end-group \
${CONFIG_TARGET_CC_LDFLAGS}
2024-12-20 22:45:12 +00:00
SRCS= kernel.c timer.c led.c interrupt_handler.c exception_handler.c \
2024-12-23 00:46:17 +00:00
mbox.c clock.c crt.c serial.c log.c power.c tag.c coherent_page.c dtb.c \
synchronize.c spinlock.c memory.c pager.c translation_table.c
2024-12-20 22:45:12 +00:00
ASMS= startup.S exception_stub.S
2024-12-23 22:44:47 +00:00
SRCS:=$(addprefix kernel/,$(SRCS))
ASMS:=$(addprefix kernel/,$(ASMS))
2024-12-20 22:45:12 +00:00
OBJS= $(SRCS:c=o) $(ASMS:S=o)
ALLTOOLS := gcc cc ar as ld nm objcopy objdump ranlib readelf strip
ALLTOOLS := $(foreach tool,$(ALLTOOLS),\
$(CONFIG_TARGET_TRIPLE)-$(tool))
TOOLCHAIN_FOR_BUILD := $(addprefix bin/build/,$(ALLTOOLS))
bin/build:
2025-01-02 11:16:39 +00:00
@test ! -d $@
@mkdir -p $@
bin/build/$(CONFIG_TARGET_TRIPLE)-%: toolchain/%.in | bin/build
2025-01-02 11:16:39 +00:00
@echo "SUBST $@"
@sed -e 's!@@CONFIG_TARGET_ARCH@@!$(CONFIG_TARGET_ARCH)!g' \
-e 's!@@CONFIG_TARGET_TRIPLE@@!$(CONFIG_TARGET_TRIPLE)!g' \
-e 's!@@CONFIG_TARGET_CC@@!$(CONFIG_TARGET_CC)!g' \
-e 's!@@CONFIG_TARGET_CC_CFLAGS@@!$(CFLAGS)!g' \
-e 's!@@CONFIG_TARGET_CC_LDFLAGS@@!$(LDFLAGS)!g' \
2025-01-02 11:16:39 +00:00
-e 's!@@CONFIG_TARGET_LD@@!$(CONFIG_TARGET_LD)!g' \
-e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(LD_LDFLAGS)!g' \
2025-01-02 11:16:39 +00:00
-e 's!@@CONFIG_TARGET_OBJCOPY@@!$(CONFIG_TARGET_OBJCOPY)!g' \
-e 's!@@CONFIG_HOST_CC@@!$(CONFIG_HOST_CC)!g' \
-e 's!@@CONFIG_TARGET_NM@@!$(CONFIG_TARGET_NM)!g' \
-e 's!@@CONFIG_TARGET_OBJDUMP@@!$(CONFIG_TARGET_OBJDUMP)!g' \
-e 's!@@CONFIG_TARGET_AR@@!$(CONFIG_TARGET_AR)!g' \
-e 's!@@CONFIG_TARGET_RANLIB@@!$(CONFIG_TARGET_RANLIB)!g' \
-e 's!@@CONFIG_TARGET_STRIP@@!$(CONFIG_TARGET_STRIP)!g' \
2025-01-02 11:16:39 +00:00
$< >$@
@chmod +x $@
.PHONY: toolchains
toolchains: bin/build
2024-12-20 22:45:12 +00:00
%.o: %.S
@echo "AS $@"
@$(AS) $(AFLAGS) -c -o $@ $<
%.o: %.c include/$(CONFIG_TARGET_TRIPLE)
@echo "CC $@"
@$(CC) $(CFLAGS) -c -o $@ $<
2025-01-02 11:16:39 +00:00
lib/$(CONFIG_TARGET_TRIPLE):
@test ! -d $@
@mkdir -p $@
lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a: $(OBJS) | lib/$(CONFIG_TARGET_TRIPLE)
2024-12-20 22:45:12 +00:00
@echo "AR $@"
@rm -f $@
@$(AR) cr $@ $^
lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a: nolibc/libnolibc.a | lib/$(CONFIG_TARGET_TRIPLE)
@echo "COPY $@"
@cp $^ $@
lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a: openlibm/libopenlibm.a | lib/$(CONFIG_TARGET_TRIPLE)
@echo "COPY $@"
@cp $^ $@
2025-01-02 11:16:39 +00:00
2024-12-20 22:45:12 +00:00
.PHONY: phony-openlibm
phony-openlibm: include/$(CONFIG_TARGET_TRIPLE)
@$(MAKE) -C openlibm \
"CC=$(CC)" "CPPFLAGS=$(CFLAGS)" libopenlibm.a
openlibm/libopenlibm.a: phony-openlibm
2024-12-23 22:44:47 +00:00
ocaml:
test ! -d $@
cp -r "$$(ocamlfind query ocaml-src)" $@
VERSION="$$(head -n1 ocaml/VERSION)" ; \
if test -d "patches/$$VERSION" ; then \
git apply --directory=$@ "patches/$$VERSION"/*; \
fi
ocaml/Makefile.config: $(TOOLCHAIN_FOR_BUILD) | ocaml
PATH="$$PWD/bin/build:$$PATH" ; \
2024-12-23 22:44:47 +00:00
cd ocaml && \
./configure \
2025-01-02 11:16:39 +00:00
--target="$(CONFIG_TARGET_TRIPLE)" \
--prefix="$(CONFIG_SYSROOT)" \
2024-12-23 22:44:47 +00:00
--disable-shared \
--disable-systhreads \
--disable-unix-lib \
--disable-instrumented-runtime \
--disable-debug-runtime \
--disable-ocamltest \
--disable-ocamldoc \
2025-01-02 11:16:39 +00:00
--without-zstd \
2024-12-23 22:44:47 +00:00
$(MAKECONF_OCAML_CONFIGURE_OPTIONS)
build:
mkdir -p $@
OCAML_IS_BUILT := build/ocaml_is_built
$(OCAML_IS_BUILT): ocaml/Makefile.config | build
PATH="$$PWD/bin/build/$(TOOLDIR_FOR_BUILD):$$PATH" $(MAKE) -C ocaml cross.opt
2024-12-23 22:44:47 +00:00
cd ocaml && ocamlrun tools/stripdebug ocamlc ocamlc.tmp
cd ocaml && ocamlrun tools/stripdebug ocamlopt ocamlopt.tmp
2024-12-23 22:44:47 +00:00
touch $@
NOLIBC_CFLAGS= $(CFLAGS) \
-I $(TOPDIR)/nolibc/include \
-I $(TOPDIR)/openlibm/src \
-I $(TOPDIR)/openlibm/include
2024-12-20 22:45:12 +00:00
.PHONY: phony-nolibc
phony-nolibc: include/$(CONFIG_TARGET_TRIPLE)
$(MAKE) -C nolibc libnolibc.a \
"CC=$(CC)" "FREESTANDING_CFLAGS=$(NOLIBC_CFLAGS)"
nolibc/libnolibc.a: phony-nolibc
.PHONY: all
all: kernel/libgilbraltar.a
2024-12-20 22:45:12 +00:00
.PHONY: clean
clean:
2024-12-23 22:44:47 +00:00
$(RM) -f kernel/*.o kernel/*.a *.elf *.bin *.img *.map
2025-01-02 11:16:39 +00:00
$(RM) -rf include/$(CONFIG_TARGET_TRIPLE)/
2024-12-23 22:44:47 +00:00
$(RM) -rf _build
2025-01-02 11:16:39 +00:00
$(RM) -rf bin/
$(RM) -rf lib/$(CONFIG_TARGET_TRIPLE)/
2024-12-23 22:44:47 +00:00
if [ -d ocaml ] ; then $(MAKE) -C ocaml clean ; fi
2024-12-20 22:45:12 +00:00
$(RM) -f test/*.o
$(MAKE) -C openlibm clean
$(MAKE) -C nolibc clean FREESTANDING_CFLAGS=_
.PHONY: distclean
distclean: clean
2024-12-23 22:44:47 +00:00
$(RM) -f Makeconf Makeconf.sh
if [ -d ocaml ] ; then $(MAKE) -C ocaml distclean ; fi
2024-12-20 22:45:12 +00:00
test/%.o: test/%.c
@echo "CC $@"
@$(CC) $(CFLAGS) -c -o $@ $<
kernel_2712.img: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a test/test.o
2024-12-20 22:45:12 +00:00
@echo "LD ${@:img=elf}"
@$(CC) test/test.o -o ${@:img=elf} $(CFLAGS) $(LDFLAGS) -Wl,-Map,${@:img=map} -T gilbraltar.ld
2024-12-20 22:45:12 +00:00
@$(OBJCOPY) ${@:img=elf} -O binary $@