smol-gilbraltar/GNUmakefile
2024-12-23 23:44:47 +01:00

138 lines
3.9 KiB
Makefile

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= aarch64-none-elf-ar # TODO
OBJDUMP= aarch64-none-elf-objdump # TODO
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
AFLAGS += $(ARCH) $(DEFINE) $(INCLUDE)
CFLAGS += -std=c11 -ffreestanding -fstack-protector-strong -nostdlib \
-nostartfiles -mstrict-align -Wall \
$(ARCH) $(DEFINE) $(INCLUDE) $(CONFIG_TARGET_CC_CFLAGS)
LDFLAGS += -nostdlib -static -Wl,--no-warn-rwx-segments \
-Wl,--section-start=.init=0x80000 -Wl,--build-id=none \
-Wl,--start-group -L . -lgilbraltar -L nolibc -lnolibc -L openlibm -lopenlibm -lgcc -Wl,--end-group
SRCS= kernel.c timer.c led.c interrupt_handler.c exception_handler.c \
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
ASMS= startup.S exception_stub.S
SRCS:=$(addprefix kernel/,$(SRCS))
ASMS:=$(addprefix kernel/,$(ASMS))
OBJS= $(SRCS:c=o) $(ASMS:S=o)
%.o: %.S
@echo "AS $@"
@$(AS) $(AFLAGS) -c -o $@ $<
%.o: %.c include/$(CONFIG_TARGET_TRIPLE)
@echo "CC $@"
@$(CC) $(CFLAGS) -c -o $@ $<
kernel/libgilbraltar.a: $(OBJS)
@echo "AR $@"
@rm -f $@
@$(AR) cr $@ $^
.PHONY: phony-openlibm
phony-openlibm: include/$(CONFIG_TARGET_TRIPLE)
@$(MAKE) -C openlibm \
"CC=$(CC)" "CPPFLAGS=$(CFLAGS)" libopenlibm.a
openlibm/libopenlibm.a: phony-openlibm
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: | ocaml
PATH="$$PWD/$(TOOLDIR_FOR_BUILD):$$PATH" ;
cd ocaml && \
./configure \
--target="$(MAKECONF_TARGET_ARCH)-gilbraltar-ocaml" \
--prefix="$(MAKECONF_SYSROOT)" \
--disable-shared \
--disable-systhreads \
--disable-unix-lib \
--disable-instrumented-runtime \
--disable-debug-runtime \
--disable-ocamltest \
--disable-ocamldoc \
--disable-zstd \
$(MAKECONF_OCAML_CONFIGURE_OPTIONS)
OCAML_IS_BUILT := _build/ocaml_is_built
$(OCAML_IS_BUILT): ocaml/Makefile.config | _build
PATH="$$PWD/$(TOOLDIR_FOR_BUILD):$$PATH" $(MAKE) -C ocaml cross.opt
cd ocaml && ocamlrun tools/stripdebug ocamlc ocamlc.tmp
cd ocaml && ocamlrun tools/stripdebug ocalmopt ocamlopt.tmp
touch $@
NOLIBC_CFLAGS= $(CFLAGS) -I $(TOPDIR)/nolibc/include -I $(TOPDIR)/openlibm/src -I $(TOPDIR)/openlibm/include
.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
.PHONY: clean
clean:
$(RM) -f kernel/*.o kernel/*.a *.elf *.bin *.img *.map
$(RM) -rf include/$(CONFIG_TARGET_TRIPLE)
$(RM) -rf _build
if [ -d ocaml ] ; then $(MAKE) -C ocaml clean ; fi
$(RM) -f test/*.o
$(MAKE) -C openlibm clean
$(MAKE) -C nolibc clean FREESTANDING_CFLAGS=_
.PHONY: distclean
distclean: clean
$(RM) -f Makeconf Makeconf.sh
if [ -d ocaml ] ; then $(MAKE) -C ocaml distclean ; fi
test/%.o: test/%.c
@echo "CC $@"
@$(CC) $(CFLAGS) -c -o $@ $<
kernel_2712.img: kernel/libgilbraltar.a nolibc/libnolibc.a openlibm/libopenlibm.a test/test.o
@echo "LD ${@:img=elf}"
@$(LD) test/test.o -o ${@:img=elf} $(LDFLAGS) -Wl,-Map ${@:img=map} -Wl,-T gilbraltar.ld
@$(OBJCOPY) ${@:img=elf} -O binary $@