From ba0f154cba55096a2342375df3375f4626b15866 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Mon, 11 Nov 2024 19:08:27 +0800 Subject: [PATCH 01/10] remove audio and gpu init from ioe_init --- am/src/platform/nemu/ioe/ioe.c | 52 ++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 25 deletions(-) diff --git a/am/src/platform/nemu/ioe/ioe.c b/am/src/platform/nemu/ioe/ioe.c index cdf35d1..5970e20 100644 --- a/am/src/platform/nemu/ioe/ioe.c +++ b/am/src/platform/nemu/ioe/ioe.c @@ -18,42 +18,44 @@ void __am_disk_config(AM_DISK_CONFIG_T *cfg); void __am_disk_status(AM_DISK_STATUS_T *stat); void __am_disk_blkio(AM_DISK_BLKIO_T *io); -static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) { cfg->present = true; cfg->has_rtc = true; } -static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; } -static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; } -static void __am_net_config (AM_NET_CONFIG_T *cfg) { cfg->present = false; } +static void __am_timer_config(AM_TIMER_CONFIG_T *cfg) { + cfg->present = true; + cfg->has_rtc = true; +} +static void __am_input_config(AM_INPUT_CONFIG_T *cfg) { cfg->present = true; } +static void __am_uart_config(AM_UART_CONFIG_T *cfg) { cfg->present = false; } +static void __am_net_config(AM_NET_CONFIG_T *cfg) { cfg->present = false; } typedef void (*handler_t)(void *buf); static void *lut[128] = { - [AM_TIMER_CONFIG] = __am_timer_config, - [AM_TIMER_RTC ] = __am_timer_rtc, - [AM_TIMER_UPTIME] = __am_timer_uptime, - [AM_INPUT_CONFIG] = __am_input_config, - [AM_INPUT_KEYBRD] = __am_input_keybrd, - [AM_GPU_CONFIG ] = __am_gpu_config, - [AM_GPU_FBDRAW ] = __am_gpu_fbdraw, - [AM_GPU_STATUS ] = __am_gpu_status, - [AM_UART_CONFIG ] = __am_uart_config, - [AM_AUDIO_CONFIG] = __am_audio_config, - [AM_AUDIO_CTRL ] = __am_audio_ctrl, - [AM_AUDIO_STATUS] = __am_audio_status, - [AM_AUDIO_PLAY ] = __am_audio_play, - [AM_DISK_CONFIG ] = __am_disk_config, - [AM_DISK_STATUS ] = __am_disk_status, - [AM_DISK_BLKIO ] = __am_disk_blkio, - [AM_NET_CONFIG ] = __am_net_config, + [AM_TIMER_CONFIG] = __am_timer_config, + [AM_TIMER_RTC] = __am_timer_rtc, + [AM_TIMER_UPTIME] = __am_timer_uptime, + [AM_INPUT_CONFIG] = __am_input_config, + [AM_INPUT_KEYBRD] = __am_input_keybrd, + [AM_GPU_CONFIG] = __am_gpu_config, + [AM_GPU_FBDRAW] = __am_gpu_fbdraw, + [AM_GPU_STATUS] = __am_gpu_status, + [AM_UART_CONFIG] = __am_uart_config, + [AM_AUDIO_CONFIG] = __am_audio_config, + [AM_AUDIO_CTRL] = __am_audio_ctrl, + [AM_AUDIO_STATUS] = __am_audio_status, + [AM_AUDIO_PLAY] = __am_audio_play, + [AM_DISK_CONFIG] = __am_disk_config, + [AM_DISK_STATUS] = __am_disk_status, + [AM_DISK_BLKIO] = __am_disk_blkio, + [AM_NET_CONFIG] = __am_net_config, }; static void fail(void *buf) { panic("access nonexist register"); } bool ioe_init() { for (int i = 0; i < LENGTH(lut); i++) - if (!lut[i]) lut[i] = fail; - __am_gpu_init(); + if (!lut[i]) + lut[i] = fail; __am_timer_init(); - __am_audio_init(); return true; } -void ioe_read (int reg, void *buf) { ((handler_t)lut[reg])(buf); } +void ioe_read(int reg, void *buf) { ((handler_t)lut[reg])(buf); } void ioe_write(int reg, void *buf) { ((handler_t)lut[reg])(buf); } From 1e69b237562857dbb2c830685d6f47e94957e3d1 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 10 Dec 2024 17:24:14 +0800 Subject: [PATCH 02/10] Makefile: add install target which exports flags for dependents. --- Makefile | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1d55214..01e3183 100644 --- a/Makefile +++ b/Makefile @@ -149,7 +149,16 @@ image: image-dep archive: $(ARCHIVE) image-dep: $(OBJS) $(LIBS) @echo \# Creating image [$(ARCH)] -.PHONY: image image-dep archive run $(LIBS) +.PHONY: image image-dep archive run $(LIBS) install + +install: $(INSTALL_DIR)/flags.mk + +### Install rules +$(addprefix $(INSTALL_DIR)/, $(LIBS)): %: + +$(INSTALL_DIR)/flags.mk: + @echo "CFLAGS += " $(INTERFACE_CFLAGS) > $(DST_DIR)/flags.mk + @echo "LDFLAGS += " $(INTERFACE_LDFLAGS) >> $(DST_DIR)/flags.mk ### Clean a single project (remove `build/`) clean: From 54ee5d6c31031d6dbd7473985de1573f3fa71439 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 10 Dec 2024 17:45:52 +0800 Subject: [PATCH 03/10] Makefile: start moving to macro based Makefile target generation - libam-riscv32-nemu is working. - libam on platforms other than riscv32-nemu has not been worked on yet. - klib has not been worked on yet. --- Makefile | 87 ++++++++++------------------------------ scripts/helpers/rules.mk | 47 ++++++++++++++++++++++ scripts/isa/riscv.mk | 7 ++-- scripts/platform/nemu.mk | 31 +++++++------- scripts/riscv32-nemu.mk | 13 +++--- 5 files changed, 93 insertions(+), 92 deletions(-) create mode 100644 scripts/helpers/rules.mk diff --git a/Makefile b/Makefile index 01e3183..027cfb0 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ # Makefile for AbstractMachine Kernels and Libraries +include scripts/helpers/rules.mk ### *Get a more readable version of this Makefile* by `make html` (requires python-markdown) html: @@ -17,9 +18,6 @@ endif ### Override checks when `make clean/clean-all/html` ifeq ($(findstring $(MAKECMDGOALS),clean|clean-all|html),) -### Print build info message -$(info # Building $(NAME)-$(MAKECMDGOALS) [$(ARCH)]) - ### Check: environment variable `$AM_HOME` looks sane ifeq ($(wildcard $(AM_HOME)/am/include/am.h),) $(error $$AM_HOME must be an AbstractMachine repo) @@ -36,33 +34,16 @@ ARCH_SPLIT = $(subst -, ,$(ARCH)) ISA = $(word 1,$(ARCH_SPLIT)) PLATFORM = $(word 2,$(ARCH_SPLIT)) -### Check if there is something to build -ifeq ($(flavor SRCS), undefined) - $(error Nothing to build) -endif - ### Checks end here endif ## 2. General Compilation Targets ### Create the destination directory (`build/$ARCH`) -WORK_DIR = $(shell pwd) -DST_DIR = $(WORK_DIR)/build/$(ARCH) -$(shell mkdir -p $(DST_DIR)) - -### Compilation targets (a binary image or archive) -IMAGE_REL = build/$(NAME)-$(ARCH) -IMAGE = $(abspath $(IMAGE_REL)) -ARCHIVE = $(WORK_DIR)/build/$(NAME)-$(ARCH).a - -### Collect the files to be linked: object files (`.o`) and libraries (`.a`) -OBJS = $(addprefix $(DST_DIR)/, $(addsuffix .o, $(basename $(SRCS)))) -LIBS := $(sort $(LIBS) am klib) # lazy evaluation ("=") causes infinite recursions -LINKAGE = $(OBJS) \ - $(addsuffix -$(ARCH).a, $(join \ - $(addsuffix /build/, $(addprefix $(AM_HOME)/, $(LIBS))), \ - $(LIBS) )) +WORK_DIR ?= $(shell pwd) +DST_DIR ?= $(WORK_DIR)/build/$(ARCH) +LIB_BUILDDIR ?= $(DST_DIR)/lib +INSTALLDIR ?= $(WORK_DIR)/build/install/$(ARCH) ## 3. General Compilation Flags @@ -76,27 +57,13 @@ OBJDUMP ?= $(CROSS_COMPILE)objdump OBJCOPY ?= $(CROSS_COMPILE)objcopy READELF ?= $(CROSS_COMPILE)readelf -### Compilation flags -INC_PATH += $(WORK_DIR)/include $(addsuffix /include/, $(addprefix $(AM_HOME)/, $(LIBS))) -INCFLAGS += $(addprefix -I, $(INC_PATH)) - -ARCH_H := arch/$(ARCH).h -CFLAGS += -lm -g -O3 -MMD -Wall $(INCFLAGS) \ - -D__ISA__=\"$(ISA)\" -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ - -D__ARCH__=$(ARCH) -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ - -D__PLATFORM__=$(PLATFORM) -D__PLATFORM_$(shell echo $(PLATFORM) | tr a-z A-Z | tr - _) \ - -DARCH_H=\"$(ARCH_H)\" \ - -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ - -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden CXXFLAGS += $(CFLAGS) -ffreestanding -fno-rtti -fno-exceptions ASFLAGS += $(INCFLAGS) LDFLAGS += -z noexecstack +INTERFACE_LDFLAGS += -z noexecstack ## 4. Arch-Specific Configurations -### Paste in arch-specific configurations (e.g., from `scripts/x86_64-qemu.mk`) --include $(AM_HOME)/scripts/$(ARCH).mk - ### Fall back to native gcc/binutils if there is no cross compiler ifeq ($(wildcard $(shell which $(CC))),) $(info # $(CC) not found; fall back to default gcc and binutils) @@ -105,43 +72,31 @@ endif ## 5. Compilation Rules -### Rule (compile): a single `.c` -> `.o` (gcc) -$(DST_DIR)/%.o: %.c - @mkdir -p $(dir $@) && echo + CC $< - @$(CC) -std=gnu11 $(CFLAGS) -c -o $@ $(realpath $<) +### Build libam -### Rule (compile): a single `.cc` -> `.o` (g++) -$(DST_DIR)/%.o: %.cc - @mkdir -p $(dir $@) && echo + CXX $< - @$(CXX) -std=c++17 $(CXXFLAGS) -c -o $@ $(realpath $<) +#### Include archetecture specific build flags +include $(AM_HOME)/scripts/$(ARCH).mk -### Rule (compile): a single `.cpp` -> `.o` (g++) -$(DST_DIR)/%.o: %.cpp - @mkdir -p $(dir $@) && echo + CXX $< - @$(CXX) -std=c++17 $(CXXFLAGS) -c -o $@ $(realpath $<) +#### Generating build rules with ADD_LIBRARY call. Target specific build flags can be tuned via changing prefixed variables (AM_ here) +AM_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/am/src $(AM_HOME)/klib/include +AM_CFLAGS += -lm -g -O3 -MMD -Wall $(addprefix -I, $(AM_INCPATH)) \ + -D__ISA__=\"$(ISA)\" -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ + -D__ARCH__=$(ARCH) -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ + -D__PLATFORM__=$(PLATFORM) -D__PLATFORM_$(shell echo $(PLATFORM) | tr a-z A-Z | tr - _) \ + -DARCH_H=\"$(ARCH_H)\" \ + -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ + -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden -### Rule (compile): a single `.S` -> `.o` (gcc, which preprocesses and calls as) -$(DST_DIR)/%.o: %.S - @mkdir -p $(dir $@) && echo + AS $< - @$(AS) $(ASFLAGS) -c -o $@ $(realpath $<) +$(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libam-$(ARCH).a,AM_)) -### Rule (recursive make): build a dependent library (am, klib, ...) -$(LIBS): %: - @$(MAKE) -s -C $(AM_HOME)/$* archive +ALL := am +all: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(ALL))) ### Rule (link): objects (`*.o`) and libraries (`*.a`) -> `IMAGE.elf`, the final ELF binary to be packed into image (ld) $(IMAGE).elf: $(OBJS) $(LIBS) @echo + LD "->" $(IMAGE_REL).elf @$(LD) $(LDFLAGS) -o $(IMAGE).elf --start-group $(LINKAGE) --end-group -### Rule (archive): objects (`*.o`) -> `ARCHIVE.a` (ar) -$(ARCHIVE): $(OBJS) - @echo + AR "->" $(shell realpath $@ --relative-to .) - @$(AR) rcs $(ARCHIVE) $(OBJS) - -### Rule (`#include` dependencies): paste in `.d` files generated by gcc on `-MMD` --include $(addprefix $(DST_DIR)/, $(addsuffix .d, $(basename $(SRCS)))) - ## 6. Miscellaneous ### Build order control diff --git a/scripts/helpers/rules.mk b/scripts/helpers/rules.mk new file mode 100644 index 0000000..379d370 --- /dev/null +++ b/scripts/helpers/rules.mk @@ -0,0 +1,47 @@ + +# Usage: +# $(1): Target prefix +# +# Generate build rules for files in variable _SRCS. Use _CFLAGS, +# _CXXFLAGS, _ASFLAGS to control build flags. +# E.g: +# $(eval $(call COMPILE_RULES,AM_)) +define COMPILE_RULES +AM_OBJS := $(addprefix $(DST_DIR)/, $(addsuffix .o, $(AM_SRCS))) +### Rule (compile): a single `.c` -> `.c.o` (gcc) +$$(filter %.c.o, $$($(1)OBJS)): $$(DST_DIR)/%.c.o: %.c + @mkdir -p $$(dir $$@) && echo + CC $$< + @$$(CC) $$($(1)CFLAGS) -c -o $$@ $$(realpath $$<) + +### Rule (compile): a single `.cc` -> `.cc.o` (g++) +$$(filter %.cc.o, $$($(1)OBJS)): $$(DST_DIR)/%.cc.o: %.cc + @mkdir -p $$(dir $$@) && echo + CXX $$< + @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) + +### Rule (compile): a single `.cpp` -> `.cpp.o` (g++) +$$(filter %.cpp.o, $$($(1)OBJS)): $$(DST_DIR)/%.cpp.o: %.cpp + @mkdir -p $$(dir $$@) && echo + CXX $$< + @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) + +### Rule (compile): a single `.S` -> `.S.o` (gcc, which preprocesses and calls as) +$$(filter %.S.o, $$($(1)OBJS)): $$(DST_DIR)/%.S.o: %.S + @mkdir -p $$(dir $$@) && echo + AS $$< + @$$(AS) $$($(1)ASFLAGS) -c -o $$@ $$(realpath $$<) + +### Rule (`#include` dependencies): paste in `.d` files generated by gcc on `-MMD` +-include $$(addprefix $$(DST_DIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$(AM_SRCS)))) +endef + +# Usage: +# $(1): Library name +# $(2): Library variable prefix +# E.g: +# $(eval $(call ADD_LIBRARY,am-riscv,AM_)) +define ADD_LIBRARY +$(eval $(call COMPILE_RULES,$(2))) +$(1): $$($(2)OBJS) + @mkdir -p $$(dir $$@) + @echo + AR "->" $$(patsubst $$(CURDIR)/%,%,$(1)) + @$$(AR) rcs $$@ $$($(2)OBJS) +endef + diff --git a/scripts/isa/riscv.mk b/scripts/isa/riscv.mk index 1315be9..89d3653 100644 --- a/scripts/isa/riscv.mk +++ b/scripts/isa/riscv.mk @@ -1,8 +1,7 @@ CROSS_COMPILE := riscv64-linux-gnu- -COMMON_CFLAGS := -fno-pic -march=rv64g -mcmodel=medany -mstrict-align -CFLAGS += $(COMMON_CFLAGS) -static -ASFLAGS += $(COMMON_CFLAGS) -O0 -LDFLAGS += -melf64lriscv -O2 +AM_CFLAGS += -static -fno-pic -march=rv64g -mcmodel=medany -mstrict-align +AM_ASFLAGS += -static -fno-pic -march=rv32g_zicsr -mcmodel=medany -O0 +AM_LDFLAGS += -melf64lriscv -O2 # overwrite ARCH_H defined in $(AM_HOME)/Makefile ARCH_H := arch/riscv.h diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index dd554bd..5f4e578 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -1,21 +1,19 @@ -AM_SRCS := platform/nemu/trm.c \ - platform/nemu/ioe/ioe.c \ - platform/nemu/ioe/timer.c \ - platform/nemu/ioe/input.c \ - platform/nemu/ioe/gpu.c \ - platform/nemu/ioe/audio.c \ - platform/nemu/ioe/disk.c \ - platform/nemu/mpe.c +AM_SRCS := am/src/platform/nemu/trm.c \ + am/src/platform/nemu/ioe/ioe.c \ + am/src/platform/nemu/ioe/timer.c \ + am/src/platform/nemu/ioe/input.c \ + am/src/platform/nemu/ioe/gpu.c \ + am/src/platform/nemu/ioe/audio.c \ + am/src/platform/nemu/ioe/disk.c \ + am/src/platform/nemu/mpe.c -CFLAGS += -fdata-sections -ffunction-sections -LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ +AM_CFLAGS += -fdata-sections -ffunction-sections +AM_LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 -LDFLAGS += --gc-sections -e _start -NEMUFLAGS += -b -#-l $(shell dirname $(IMAGE).elf)/nemu-log.txt +AM_LDFLAGS += --gc-sections -e _start -CFLAGS += -DMAINARGS=\"$(mainargs)\" -CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include +AM_CFLAGS += -DMAINARGS=\"$(mainargs)\" +AM_INCPATH += $(AM_HOME)/am/src/platform/nemu/include .PHONY: $(AM_HOME)/am/src/platform/nemu/trm.c image: $(IMAGE).elf @@ -23,6 +21,9 @@ image: $(IMAGE).elf @echo + OBJCOPY "->" $(IMAGE_REL).bin @$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(IMAGE).elf $(IMAGE).bin +NEMUFLAGS += -b +#-l $(shell dirname $(IMAGE).elf)/nemu-log.txt + run: image $(MAKE) -C $(NEMU_HOME) ISA=$(ISA) run ARGS="$(NEMUFLAGS)" IMG=$(IMAGE).bin diff --git a/scripts/riscv32-nemu.mk b/scripts/riscv32-nemu.mk index 7b3a274..eb7c24f 100644 --- a/scripts/riscv32-nemu.mk +++ b/scripts/riscv32-nemu.mk @@ -1,10 +1,9 @@ include $(AM_HOME)/scripts/isa/riscv.mk include $(AM_HOME)/scripts/platform/nemu.mk -CFLAGS += -DISA_H=\"riscv/riscv.h\" -COMMON_CFLAGS += -march=rv32im_zicsr -mabi=ilp32 # overwrite -LDFLAGS += -melf32lriscv # overwrite +AM_CFLAGS += -DISA_H=\"riscv/riscv.h\" -march=rv32im_zicsr -mabi=ilp32 # overwrite +AM_LDFLAGS += -melf32lriscv # overwrite -AM_SRCS += riscv/nemu/start.S \ - riscv/nemu/cte.c \ - riscv/nemu/trap.S \ - riscv/nemu/vme.c +AM_SRCS += am/src/riscv/nemu/start.S \ + am/src/riscv/nemu/cte.c \ + am/src/riscv/nemu/trap.S \ + am/src/riscv/nemu/vme.c From fa7d9c5eb0e16bb6162b4f22bbe5ddeb2ca7da86 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 10 Dec 2024 18:12:00 +0800 Subject: [PATCH 04/10] fixup! Makefile: start moving to macro based Makefile target generation --- scripts/helpers/rules.mk | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/scripts/helpers/rules.mk b/scripts/helpers/rules.mk index 379d370..ad8e187 100644 --- a/scripts/helpers/rules.mk +++ b/scripts/helpers/rules.mk @@ -4,32 +4,33 @@ # # Generate build rules for files in variable _SRCS. Use _CFLAGS, # _CXXFLAGS, _ASFLAGS to control build flags. +# Object files will be put into $BUILDDIR # E.g: # $(eval $(call COMPILE_RULES,AM_)) define COMPILE_RULES -AM_OBJS := $(addprefix $(DST_DIR)/, $(addsuffix .o, $(AM_SRCS))) +$(1)OBJS := $$(addprefix $$(BUILDDIR)/, $$(addsuffix .o, $$($(1)SRCS))) ### Rule (compile): a single `.c` -> `.c.o` (gcc) -$$(filter %.c.o, $$($(1)OBJS)): $$(DST_DIR)/%.c.o: %.c +$$(filter %.c.o, $$($(1)OBJS)): $$(BUILDDIR)/%.c.o: %.c @mkdir -p $$(dir $$@) && echo + CC $$< @$$(CC) $$($(1)CFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.cc` -> `.cc.o` (g++) -$$(filter %.cc.o, $$($(1)OBJS)): $$(DST_DIR)/%.cc.o: %.cc +$$(filter %.cc.o, $$($(1)OBJS)): $$(BUILDDIR)/%.cc.o: %.cc @mkdir -p $$(dir $$@) && echo + CXX $$< @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.cpp` -> `.cpp.o` (g++) -$$(filter %.cpp.o, $$($(1)OBJS)): $$(DST_DIR)/%.cpp.o: %.cpp +$$(filter %.cpp.o, $$($(1)OBJS)): $$(BUILDDIR)/%.cpp.o: %.cpp @mkdir -p $$(dir $$@) && echo + CXX $$< @$$(CXX) $$($(1)CXXFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (compile): a single `.S` -> `.S.o` (gcc, which preprocesses and calls as) -$$(filter %.S.o, $$($(1)OBJS)): $$(DST_DIR)/%.S.o: %.S +$$(filter %.S.o, $$($(1)OBJS)): $$(BUILDDIR)/%.S.o: %.S @mkdir -p $$(dir $$@) && echo + AS $$< @$$(AS) $$($(1)ASFLAGS) -c -o $$@ $$(realpath $$<) ### Rule (`#include` dependencies): paste in `.d` files generated by gcc on `-MMD` --include $$(addprefix $$(DST_DIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$(AM_SRCS)))) +-include $$(addprefix $$(BUILDDIR)/, $$(addsuffix .d, $$(filter %.c %.cc, $$($(1)SRCS)))) endef # Usage: From 0c6f5e078992bf53d32672d32817a0324cb44416 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 10 Dec 2024 18:12:40 +0800 Subject: [PATCH 05/10] Makefile: move klib to new approach --- Makefile | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 027cfb0..4c0f0be 100644 --- a/Makefile +++ b/Makefile @@ -72,6 +72,7 @@ endif ## 5. Compilation Rules +BUILDDIR := $(DST_DIR) ### Build libam #### Include archetecture specific build flags @@ -89,8 +90,19 @@ AM_CFLAGS += -lm -g -O3 -MMD -Wall $(addprefix -I, $(AM_INCPATH)) \ $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libam-$(ARCH).a,AM_)) -ALL := am +### Build klib + +KLIB_SRCS := $(shell find klib/src/ -name "*.c") + +KLIB_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include +KLIB_CFLAGS := -MMD -Wall $(addprefix -I, $(KLIB_INCPATH)) \ + -DARCH_H=\"$(ARCH_H)\" \ + +$(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libklib-$(ARCH).a,KLIB_)) + +ALL := am klib all: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(ALL))) +$(ALL): %: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, %)) ### Rule (link): objects (`*.o`) and libraries (`*.a`) -> `IMAGE.elf`, the final ELF binary to be packed into image (ld) $(IMAGE).elf: $(OBJS) $(LIBS) From f0bac361e6be6f10df859c674812c7e9db68f6a8 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Tue, 10 Dec 2024 19:56:07 +0800 Subject: [PATCH 06/10] Makefile: Add install, exports interface flags, libs, and includes --- Makefile | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 4c0f0be..7db5c43 100644 --- a/Makefile +++ b/Makefile @@ -44,6 +44,8 @@ WORK_DIR ?= $(shell pwd) DST_DIR ?= $(WORK_DIR)/build/$(ARCH) LIB_BUILDDIR ?= $(DST_DIR)/lib INSTALLDIR ?= $(WORK_DIR)/build/install/$(ARCH) +LIB_INSTALLDIR ?= $(INSTALLDIR)/lib +INC_INSTALLDIR ?= $(INSTALLDIR)/include ## 3. General Compilation Flags @@ -84,9 +86,11 @@ AM_CFLAGS += -lm -g -O3 -MMD -Wall $(addprefix -I, $(AM_INCPATH)) \ -D__ISA__=\"$(ISA)\" -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ -D__ARCH__=$(ARCH) -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ -D__PLATFORM__=$(PLATFORM) -D__PLATFORM_$(shell echo $(PLATFORM) | tr a-z A-Z | tr - _) \ - -DARCH_H=\"$(ARCH_H)\" \ - -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ - -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden + -DARCH_H=\"$(ARCH_H)\" +AM_INTERFACE_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include +AM_INTERFACE_CFLAGS += $(addprefix -I, $(AM_INTERFACE_INCPATH)) \ + -lm -DARCH_H=\"$(ARCH_H)\" -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ + -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libam-$(ARCH).a,AM_)) @@ -95,14 +99,16 @@ $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libam-$(ARCH).a,AM_)) KLIB_SRCS := $(shell find klib/src/ -name "*.c") KLIB_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include -KLIB_CFLAGS := -MMD -Wall $(addprefix -I, $(KLIB_INCPATH)) \ - -DARCH_H=\"$(ARCH_H)\" \ +KLIB_CFLAGS += -MMD -Wall $(addprefix -I, $(KLIB_INCPATH)) \ + -DARCH_H=\"$(ARCH_H)\" +KLIB_INTERFACE_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include +KLIB_INTERFACE_CFLAGS += -DARCH_H=\"$(ARCH_H)\" $(addprefix -I, $(KLIB_INTERFACE_INCPATH)) $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libklib-$(ARCH).a,KLIB_)) -ALL := am klib -all: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(ALL))) -$(ALL): %: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, %)) +LIBS := am klib +libs: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(ALL))) +$(LIBS): %: $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, %)) ### Rule (link): objects (`*.o`) and libraries (`*.a`) -> `IMAGE.elf`, the final ELF binary to be packed into image (ld) $(IMAGE).elf: $(OBJS) $(LIBS) @@ -118,14 +124,22 @@ image-dep: $(OBJS) $(LIBS) @echo \# Creating image [$(ARCH)] .PHONY: image image-dep archive run $(LIBS) install -install: $(INSTALL_DIR)/flags.mk - ### Install rules -$(addprefix $(INSTALL_DIR)/, $(LIBS)): %: -$(INSTALL_DIR)/flags.mk: - @echo "CFLAGS += " $(INTERFACE_CFLAGS) > $(DST_DIR)/flags.mk - @echo "LDFLAGS += " $(INTERFACE_LDFLAGS) >> $(DST_DIR)/flags.mk +INTERFACE_INCPATH += $(sort $(KLIB_INTERFACE_INCPATH) $(AM_INTERFACE_INCPATH)) +INTERFACE_CFLAGS += $(sort $(KLIB_INTERFACE_CFLAGS) $(AM_INTERFACE_CFLAGS)) +INTERFACE_LDFLAGS += $(sort $(KLIB_LDFLAGS) $(AM_LDFLAGS)) + +EXPORT_FLAGS_FILE := $(INSTALLDIR)/flags-$(ARCH).mk +$(EXPORT_FLAGS_FILE): + @mkdir -p $(INSTALLDIR) + @echo "CFLAGS += " $(INTERFACE_CFLAGS) > $(EXPORT_FLAGS_FILE) + @echo "LDFLAGS += " $(INTERFACE_LDFLAGS) >> $(EXPORT_FLAGS_FILE) + +install: $(EXPORT_FLAGS_FILE) $(LIBS) + @mkdir -p $(LIB_INSTALLDIR) $(INC_INSTALLDIR) + @cp $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(LIBS))) $(LIB_INSTALLDIR) + @cp -r $(addsuffix /*, $(INTERFACE_INCPATH)) $(INC_INSTALLDIR)/ ### Clean a single project (remove `build/`) clean: From 8c6f9bb716a0b736cd43dbe279f1231904aa461f Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 12:41:55 +0800 Subject: [PATCH 07/10] Makefile: export helpers, improve install target --- Makefile | 33 ++++++++++++++++++++++----------- scripts/templates/flags.tmpl | 4 ++++ 2 files changed, 26 insertions(+), 11 deletions(-) create mode 100644 scripts/templates/flags.tmpl diff --git a/Makefile b/Makefile index 7db5c43..0be5307 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,6 @@ OBJCOPY ?= $(CROSS_COMPILE)objcopy READELF ?= $(CROSS_COMPILE)readelf CXXFLAGS += $(CFLAGS) -ffreestanding -fno-rtti -fno-exceptions -ASFLAGS += $(INCFLAGS) LDFLAGS += -z noexecstack INTERFACE_LDFLAGS += -z noexecstack @@ -122,7 +121,7 @@ image: image-dep archive: $(ARCHIVE) image-dep: $(OBJS) $(LIBS) @echo \# Creating image [$(ARCH)] -.PHONY: image image-dep archive run $(LIBS) install +.PHONY: image image-dep archive run libs $(LIBS) install ### Install rules @@ -130,16 +129,28 @@ INTERFACE_INCPATH += $(sort $(KLIB_INTERFACE_INCPATH) $(AM_INTERFACE_INCPATH)) INTERFACE_CFLAGS += $(sort $(KLIB_INTERFACE_CFLAGS) $(AM_INTERFACE_CFLAGS)) INTERFACE_LDFLAGS += $(sort $(KLIB_LDFLAGS) $(AM_LDFLAGS)) -EXPORT_FLAGS_FILE := $(INSTALLDIR)/flags-$(ARCH).mk -$(EXPORT_FLAGS_FILE): - @mkdir -p $(INSTALLDIR) - @echo "CFLAGS += " $(INTERFACE_CFLAGS) > $(EXPORT_FLAGS_FILE) - @echo "LDFLAGS += " $(INTERFACE_LDFLAGS) >> $(EXPORT_FLAGS_FILE) +EXPORT_FLAGS_FILE := $(LIB_INSTALLDIR)/make/flags-$(ARCH).mk +EXPORT_FLAGS_TEMPLATE := $(file < $(AM_HOME)/scripts/templates/flags.tmpl) +HELPERS := $(wildcard find scripts/helpers/*.mk) +EXPORT_HELPERS := $(HELPERS:scripts/helpers/%=$(LIB_INSTALLDIR)/make/%) -install: $(EXPORT_FLAGS_FILE) $(LIBS) - @mkdir -p $(LIB_INSTALLDIR) $(INC_INSTALLDIR) - @cp $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(LIBS))) $(LIB_INSTALLDIR) - @cp -r $(addsuffix /*, $(INTERFACE_INCPATH)) $(INC_INSTALLDIR)/ +test: + @echo $(EXPORT_HELPERS) + @echo $(LIB_INSTALLDIR) + +EXPORTS := $(EXPORT_FLAGS_FILE) $(EXPORT_HELPERS) + +$(EXPORT_HELPERS): $(LIB_INSTALLDIR)/make/%: scripts/helpers/% + @install -Dm644 $< $(dir $@) + +export INTERFACE_CFLAGS INTERFACE_INCPATH INTERFACE_LDFLAGS +$(EXPORT_FLAGS_FILE): + @install -Dm644 <(printf $(EXPORT_FLAGS_TEMPLATE)) $(EXPORT_FLAGS_FILE) + +install: $(EXPORTS) $(LIBS) + @install -dm755 $(LIB_INSTALLDIR) $(INC_INSTALLDIR) + @install -Dm644 $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(LIBS))) $(LIB_INSTALLDIR) + @install -Dm644 $(shell find $(INTERFACE_INCPATH) -name '*.h') $(INC_INSTALLDIR)/ ### Clean a single project (remove `build/`) clean: diff --git a/scripts/templates/flags.tmpl b/scripts/templates/flags.tmpl new file mode 100644 index 0000000..0746074 --- /dev/null +++ b/scripts/templates/flags.tmpl @@ -0,0 +1,4 @@ +"CFLAGS += %s \n\ +LDFLAGS += %s" \ +"$INTERFACE_CFLAGS" \ +"$INTERFACE_LDFLAGS" From 99fd46526c531adf9de6ec0ba780c5b7a9932094 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 14:26:24 +0800 Subject: [PATCH 08/10] Makefile: split header and lib install --- Makefile | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 0be5307..fb8d59f 100644 --- a/Makefile +++ b/Makefile @@ -141,16 +141,27 @@ test: EXPORTS := $(EXPORT_FLAGS_FILE) $(EXPORT_HELPERS) $(EXPORT_HELPERS): $(LIB_INSTALLDIR)/make/%: scripts/helpers/% + @echo + INSTALL $(patsubst $(INSTALLDIR)/%,%,$@) + @install -dm755 $(dir $@) @install -Dm644 $< $(dir $@) export INTERFACE_CFLAGS INTERFACE_INCPATH INTERFACE_LDFLAGS $(EXPORT_FLAGS_FILE): + @echo + INSTALL $(patsubst $(INSTALLDIR)/%,%,$@) @install -Dm644 <(printf $(EXPORT_FLAGS_TEMPLATE)) $(EXPORT_FLAGS_FILE) -install: $(EXPORTS) $(LIBS) - @install -dm755 $(LIB_INSTALLDIR) $(INC_INSTALLDIR) +install-libs: $(LIBS) + @echo + INSTALL LIBS: $(LIBS) + @install -dm755 $(LIB_INSTALLDIR) @install -Dm644 $(addsuffix -$(ARCH).a, $(addprefix $(LIB_BUILDDIR)/lib, $(LIBS))) $(LIB_INSTALLDIR) - @install -Dm644 $(shell find $(INTERFACE_INCPATH) -name '*.h') $(INC_INSTALLDIR)/ + +install-headers: HEADERS := $(shell find $(INTERFACE_INCPATH) -name '*.h') +install-headers: + @echo + INSTALL HEADERS: $(patsubst $(AM_HOME)/%,%,$(HEADERS)) + @install -dm755 $(INC_INSTALLDIR) + @install -Dm644 $(HEADERS) $(INC_INSTALLDIR) + +install: $(EXPORTS) install-libs install-headers ### Clean a single project (remove `build/`) clean: From f86387b16314d1a359c67dad87a590fc71dd2f29 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 14:27:20 +0800 Subject: [PATCH 09/10] fixup! Makefile: export helpers, improve install target --- scripts/templates/flags.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/templates/flags.tmpl b/scripts/templates/flags.tmpl index 0746074..cbb7b11 100644 --- a/scripts/templates/flags.tmpl +++ b/scripts/templates/flags.tmpl @@ -1,4 +1,4 @@ -"CFLAGS += %s \n\ -LDFLAGS += %s" \ +"AM_CFLAGS += %s \n\ +AM_LDFLAGS += %s" \ "$INTERFACE_CFLAGS" \ "$INTERFACE_LDFLAGS" From 4be0c3018a2af32207398c08635d634c502af736 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 14:28:28 +0800 Subject: [PATCH 10/10] Makefile: add ADD_IMAGE helper --- scripts/helpers/rules.mk | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/scripts/helpers/rules.mk b/scripts/helpers/rules.mk index ad8e187..7ce4255 100644 --- a/scripts/helpers/rules.mk +++ b/scripts/helpers/rules.mk @@ -34,7 +34,7 @@ $$(filter %.S.o, $$($(1)OBJS)): $$(BUILDDIR)/%.S.o: %.S endef # Usage: -# $(1): Library name +# $(1): Library build path # $(2): Library variable prefix # E.g: # $(eval $(call ADD_LIBRARY,am-riscv,AM_)) @@ -46,3 +46,19 @@ $(1): $$($(2)OBJS) @$$(AR) rcs $$@ $$($(2)OBJS) endef +# Usage: +# $(1): Image build path +# $(2): Image variable prefix +# E.g: +# $(eval $(call ADD_IMAGE,dummy,DUMMY_)) +define ADD_IMAGE +$(eval $(call COMPILE_RULES,$(2))) +$(1).elf: $$($(2)OBJS) + @mkdir -p $$(dir $$@) + @echo + LD "->" $$(patsubst $$(CURDIR)/%,%,$(1).elf) + @$$(LD) $$($(2)_LDFLAGS) -o $$@ --start-group $$($(2)OBJS) --end-group +$(1).bin: $(1).elf + @echo + OBJCOPY "->" $$(patsubst $$(CURDIR)/%,%,$(1)) + @$$(OBJCOPY) -S --set-section-flags .bss=alloc,contents -O binary $(1).elf $(1).bin +endef +