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) 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 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 \ -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} 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) 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: @test ! -d $@ @mkdir -p $@ bin/build/$(CONFIG_TARGET_TRIPLE)-%: toolchain/%.in | bin/build @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' \ -e 's!@@CONFIG_TARGET_LD@@!$(CONFIG_TARGET_LD)!g' \ -e 's!@@CONFIG_TARGET_LD_LDFLAGS@@!$(LD_LDFLAGS)!g' \ -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' \ $< >$@ @chmod +x $@ .PHONY: toolchains toolchains: bin/build %.o: %.S @echo "AS $@" @$(AS) $(AFLAGS) -c -o $@ $< %.o: %.c include/$(CONFIG_TARGET_TRIPLE) @echo "CC $@" @$(CC) $(CFLAGS) -c -o $@ $< lib/$(CONFIG_TARGET_TRIPLE): @test ! -d $@ @mkdir -p $@ lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a: $(OBJS) | lib/$(CONFIG_TARGET_TRIPLE) @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 $^ $@ .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: $(TOOLCHAIN_FOR_BUILD) | ocaml PATH="$$PWD/bin/build:$$PATH" ; \ cd ocaml && \ ./configure \ --target="$(CONFIG_TARGET_TRIPLE)" \ --prefix="$(CONFIG_SYSROOT)" \ --disable-shared \ --disable-systhreads \ --disable-unix-lib \ --disable-instrumented-runtime \ --disable-debug-runtime \ --disable-ocamltest \ --disable-ocamldoc \ --without-zstd \ $(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 cd ocaml && ocamlrun tools/stripdebug ocamlc ocamlc.tmp cd ocaml && ocamlrun tools/stripdebug ocamlopt 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 $(RM) -rf bin/ $(RM) -rf lib/$(CONFIG_TARGET_TRIPLE)/ 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: lib/$(CONFIG_TARGET_TRIPLE)/libgilbraltar.a lib/$(CONFIG_TARGET_TRIPLE)/libnolibc.a lib/$(CONFIG_TARGET_TRIPLE)/libopenlibm.a test/test.o @echo "LD ${@:img=elf}" @$(CC) test/test.o -o ${@:img=elf} $(CFLAGS) $(LDFLAGS) -Wl,-Map,${@:img=map} -T gilbraltar.ld @$(OBJCOPY) ${@:img=elf} -O binary $@