####################################################################################################
#
#  Makefile for bootloader.
#  External vars my be:
#    arch      - target arch
#    dbg       - debug flag
#    cfgdir    - path to dir that contents sys-config.h
#    blddir    - path to dir that will content build result
#    target    - target file name, like bootloader.img
#
####################################################################################################

objs        := $(arch)/crt0.o $(arch)/crt1.o main.o ramfs.generate.o
baseflags   := -U$(arch) # undefine built-in macro (least for sparc undef 'sparc=1')
baseflags   += -O2 -Wall -Werror
cxxflags    := -std=c++11
cxxflags    += -fno-rtti # no-rtti to avoid undefined reference to `vtable ...'
cxxflags    += -fno-exceptions
incflags    := -I$(cfgdir)
incflags    += -I$(arch)
incflags    += -I$(wrmdir)/lib/uart/$(uart)
incflags    += -I$(wrmdir)/lib/intc/$(intc)
incflags    += -I$(wrmdir)/lib/elfloader
incflags    += -I$(wrmdir)/lib/containers
incflags    += -I$(wrmdir)/lib/l4/inc
incflags    += -I$(wrmdir)/lib/sys
incflags    += -I$(wrmdir)/lib/sys/$(arch)
incflags    += -I$(wrmdir)/lib/wlibc/inc
libs        := $(rtblddir)/lib/elfloader/libelfloader.a
libs        += $(rtblddir)/lib/wlibc/libwlibc.a
libs        += $(rtblddir)/lib/wstdc++/libwstdc++.a
libs        += $(rtblddir)/lib/sys/libsys.a
ldflags     :=
asflags     :=
cflags      :=
ldaddr_file := $(cfgdir)/bootloader-load-addr.h
cc_deps     := $(ldaddr_file) # add cc-dependence because crt1.cc include "bootloader-load-addr.h"
cpp_deps    := $(ramfs_files) # add cpp-dependence because main.cpp needs to update build timestamp

ifeq ($(dbg),1)
  baseflags += -g
  baseflags += -DDEBUG
else
  baseflags += -DNDEBUG
endif

include $(wrmdir)/mk/base.mk

# generate ramfs.generate.c
# format of 'ldr_ramfs':  name1:path1  name2:path2 ...
$(blddir)/ramfs.generate.c:  $(ramfs_files)
	@$(echo) "$(color_compile)[GEN] $(notdir $(target)):  $(notdir $(ramfs_files)) --> $(notdir $@)$(color_off)"
	@mkdir -p $(blddir);
	@# print files info
	@echo -e "      Generate ramfs:"; \
	cnt=0; \
	for item in $(ldr_ramfs); do \
		arr=($$(echo $$item | tr ":" " ")); \
		name=$${arr[0]}; \
		path=$${arr[1]}; \
		size=$$(stat -c%s $$path); \
		printf "      file%02d:  %16s  %8u    .../%s\n" $$cnt $$name $$size $$(basename $$path); \
		((cnt += 1)); \
	done
	@# put binaries to the c-file
	@file=$@; \
	echo -e "// Generated by ldr/Makefile, don't edit me.\n"  > $$file; \
	echo -e "#include \"sys_ramfs.h\"\n"                     >> $$file; \
	cnt=0; \
	for item in $(ldr_ramfs); do \
		arr=($$(echo $$item | tr ":" " ")); \
		name=$${arr[0]}; \
		path=$${arr[1]}; \
		algn="   0x1"; \
		if [ "$(arch)" == "arm" ]; then \
			algn=" 0"; \
		fi; \
		if [[ "$$name" != "kernel.elf"   && "$$name" != "sigma0.elf"  && \
			  "$$name" != "roottask.elf" && "$$name" != "config.alph" ]]; then \
			algn=$(page_sz); \
			if [ "$(arch)" == "arm" ]; then \
				pgsz=$(page_sz); \
				algn=$$(printf "%.0f" $$(echo "ibase=16; l($${pgsz:2})/l(2);" | bc -l)); \
			fi; \
		fi; \
		echo -e "asm (\".align $$algn; file$$cnt: .incbin \\\"$$path\\\"\");" >> $$file; \
		((cnt += 1)); \
	done; \
	echo -e ""                                               >> $$file;
	@# put ramfs-header to the c-file
	@file=$@; \
	cnt=0; \
	for item in $(ldr_ramfs); do \
		echo -e "extern int file$$cnt;"                      >> $$file; \
		((cnt += 1)); \
	done; \
	echo -e ""                                               >> $$file; \
	echo -e "#define SECTION __attribute__((section(\".first\")))\n" >> $$file; \
	echo -e "const Ramfs_file_header_t ramfs[] SECTION ="    >> $$file; \
	echo -e "{"                                              >> $$file; \
	cnt=0; \
	for item in $(ldr_ramfs); do \
		arr=($$(echo $$item | tr ":" " ")); \
		name=$${arr[0]}; \
		path=$${arr[1]}; \
		size=$$(stat -c%s $$path); \
		echo -e "    {"                                      >> $$file; \
		echo -e "        .name = \"$$name\","                >> $$file; \
		echo -e "        .size = $$size,"                    >> $$file; \
		echo -e "        .data = &file$$cnt"                 >> $$file; \
		echo -e "    },"                                     >> $$file; \
		((cnt += 1)); \
	done; \
	echo -e "    {"                                          >> $$file; \
	echo -e "        .name = 0,"                             >> $$file; \
	echo -e "        .size = 0,"                             >> $$file; \
	echo -e "        .data = 0"                              >> $$file; \
	echo -e "    },"                                         >> $$file; \
	echo -e "};"                                             >> $$file; \

# special rule to build generated c-file
$(blddir)/ramfs.generate.o:  $(blddir)/ramfs.generate.c
	@$(echo) "$(color_compile)[CC]  $(notdir $(target)):  ramfs.generate.c --> $(notdir $@)$(color_off)"
	$(v)$(gccprefix)gcc $(cflags) -c $(blddir)/ramfs.generate.c -o $(blddir)/ramfs.generate.o
