From 5ca5e6972b5e3956b3a3710685fdc4c9f366b10a Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 15:34:30 +0800 Subject: [PATCH 1/3] fixup! Makefile: export helpers, improve install target --- Makefile | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Makefile b/Makefile index f994fe8..06c9235 100644 --- a/Makefile +++ b/Makefile @@ -134,10 +134,6 @@ EXPORT_FLAGS_TEMPLATE := $(file < $(AM_HOME)/scripts/templates/flags.tmpl) HELPERS := $(wildcard find scripts/helpers/*.mk) EXPORT_HELPERS := $(HELPERS:scripts/helpers/%=$(LIB_INSTALLDIR)/make/%) -test: - @echo $(EXPORT_HELPERS) - @echo $(LIB_INSTALLDIR) - EXPORTS := $(EXPORT_FLAGS_FILE) $(EXPORT_HELPERS) $(EXPORT_HELPERS): $(LIB_INSTALLDIR)/make/%: scripts/helpers/% From 3e33f2e0f1fc72f53ac75393433066a8c95b3349 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 17:01:38 +0800 Subject: [PATCH 2/3] Makefile: sort out CFLAGS and LDFLAGS --- Makefile | 44 ++++++++++++++++++++++------------ am/src/platform/nemu/ioe/ioe.c | 1 + scripts/helpers/rules.mk | 2 +- scripts/platform/nemu.mk | 12 ++++++---- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Makefile b/Makefile index 06c9235..c20b7ec 100644 --- a/Makefile +++ b/Makefile @@ -74,22 +74,27 @@ endif ## 5. Compilation Rules BUILDDIR := $(DST_DIR) +COMMON_CFLAGS := $(CFLAGS) -g -O3 -MMD -Wall \ + -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ + -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden ### Build libam - #### Include archetecture specific build flags include $(AM_HOME)/scripts/$(ARCH).mk +COMMON_CFLAGS += -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ + -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ + -DARCH_H=\"$(ARCH_H)\" +INTERFACE_CFLAGS += -DARCH_H=\"$(ARCH_H)\" \ + -fno-asynchronous-unwind-tables \ + -fno-builtin -fno-stack-protector \ + -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden #### 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)\" +AM_CFLAGS += $(COMMON_CFLAGS) $(addprefix -I, $(AM_INCPATH)) AM_INTERFACE_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include -AM_INTERFACE_CFLAGS += $(addprefix -I, $(AM_INTERFACE_INCPATH:%=$(INC_INSTALLDIR))) \ - -lm -DARCH_H=\"$(ARCH_H)\" -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ - -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden +AM_INTERFACE_CFLAGS += + +AM_INTERFACE_LDFLAGS += -lm -lam-$(ARCH) $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libam-$(ARCH).a,AM_)) @@ -98,10 +103,10 @@ $(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 += $(COMMON_CFLAGS) $(addprefix -I, $(KLIB_INCPATH)) KLIB_INTERFACE_INCPATH += $(AM_HOME)/am/include $(AM_HOME)/klib/include -KLIB_INTERFACE_CFLAGS += -DARCH_H=\"$(ARCH_H)\" $(addprefix -I, $(KLIB_INTERFACE_INCPATH:%=$(INC_INSTALLDIR))) +KLIB_INTERFACE_CFLAGS += +KLIB_INTERFACE_LDFLAGS += -lklib-$(ARCH) $(eval $(call ADD_LIBRARY,$(LIB_BUILDDIR)/libklib-$(ARCH).a,KLIB_)) @@ -126,8 +131,10 @@ image-dep: $(OBJS) $(LIBS) ### Install rules 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)) +# TODO: Use sort here will cause error on seperated flags, such as: -e _start +# but without sort, duplicated flags will not be removed. +INTERFACE_CFLAGS += $(addprefix -I, $(INTERFACE_INCPATH:%=$(INC_INSTALLDIR))) $(sort $(KLIB_INTERFACE_CFLAGS) $(AM_INTERFACE_CFLAGS)) +INTERFACE_LDFLAGS += -L$(LIB_INSTALLDIR) $(sort $(KLIB_INTERFACE_LDFLAGS) $(AM_INTERFACE_LDFLAGS)) EXPORT_FLAGS_FILE := $(LIB_INSTALLDIR)/make/flags-$(ARCH).mk EXPORT_FLAGS_TEMPLATE := $(file < $(AM_HOME)/scripts/templates/flags.tmpl) @@ -146,6 +153,13 @@ $(EXPORT_FLAGS_FILE): @echo + INSTALL $(patsubst $(INSTALLDIR)/%,%,$@) @install -Dm644 <(printf $(EXPORT_FLAGS_TEMPLATE)) $(EXPORT_FLAGS_FILE) +LDSCRIPTS := $(patsubst $(AM_HOME)/scripts/%, $(LIB_INSTALLDIR)/ldscripts/%, $(shell find $(AM_HOME)/scripts -name "*.ld")) + +$(LDSCRIPTS): $(LIB_INSTALLDIR)/ldscripts/%: $(AM_HOME)/scripts/% + @echo + INSTALL $(patsubst $(INSTALLDIR)/%,%,$@) + @mkdir -p $(LIB_INSTALLDIR)/ldscripts + @install -Dm644 $< $(dir $@) + install-libs: $(LIBS) @echo + INSTALL LIBS: $(LIBS) @install -dm755 $(LIB_INSTALLDIR) @@ -157,7 +171,7 @@ install-headers: $(HEADERS) # Headers needs to be reinstalled if they are change @install -dm755 $(INC_INSTALLDIR) @cp -r $(addsuffix /*, $(INTERFACE_INCPATH)) $(INC_INSTALLDIR) -install: $(EXPORTS) install-libs install-headers +install: $(EXPORTS) install-libs install-headers $(LDSCRIPTS) ### Clean a single project (remove `build/`) clean: diff --git a/am/src/platform/nemu/ioe/ioe.c b/am/src/platform/nemu/ioe/ioe.c index 5970e20..b0883a9 100644 --- a/am/src/platform/nemu/ioe/ioe.c +++ b/am/src/platform/nemu/ioe/ioe.c @@ -59,3 +59,4 @@ bool ioe_init() { void ioe_read(int reg, void *buf) { ((handler_t)lut[reg])(buf); } void ioe_write(int reg, void *buf) { ((handler_t)lut[reg])(buf); } + diff --git a/scripts/helpers/rules.mk b/scripts/helpers/rules.mk index 7ce4255..6c37be6 100644 --- a/scripts/helpers/rules.mk +++ b/scripts/helpers/rules.mk @@ -56,7 +56,7 @@ $(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 + @$$(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 diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index 5f4e578..927f8bc 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -7,10 +7,14 @@ AM_SRCS := am/src/platform/nemu/trm.c \ am/src/platform/nemu/ioe/disk.c \ am/src/platform/nemu/mpe.c -AM_CFLAGS += -fdata-sections -ffunction-sections -AM_LDFLAGS += -T $(AM_HOME)/scripts/linker.ld \ - --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 -AM_LDFLAGS += --gc-sections -e _start +AM_PUBLIC_CFLAGS := -fdata-sections -ffunction-sections +AM_PUBLIC_LDFLAGS := --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 \ + --gc-sections --entry=_start +AM_CFLAGS += $(AM_PUBLIC_CFLAGS) +AM_LDFLAGS += -T$(AM_HOME)/scripts/linker.ld $(AM_PUBLIC_LDFLAGS) + +AM_INTERFACE_CFLAGS += $(AM_PUBLIC_CFLAGS) +AM_INTERFACE_LDFLAGS += -T$(LIB_INSTALLDIR)/ldscripts/linker.ld $(AM_PUBLIC_LDFLAGS) AM_CFLAGS += -DMAINARGS=\"$(mainargs)\" AM_INCPATH += $(AM_HOME)/am/src/platform/nemu/include From 5f096b8805878e117550f21c086731d143e41af9 Mon Sep 17 00:00:00 2001 From: xinyangli Date: Wed, 11 Dec 2024 17:06:02 +0800 Subject: [PATCH 3/3] Makefile: remove unused variables and targets --- Makefile | 53 ++++++++++++++-------------------------- scripts/isa/riscv.mk | 1 - scripts/platform/nemu.mk | 11 ++------- scripts/riscv32-nemu.mk | 4 +-- 4 files changed, 22 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index c20b7ec..d3fa917 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,10 @@ html: ## 1. Basic Setup and Checks -### Default to create a bare-metal kernel image +### Default to create all static libraries ifeq ($(MAKECMDGOALS),) - MAKECMDGOALS = image - .DEFAULT_GOAL = image + MAKECMDGOALS = libs + .DEFAULT_GOAL = libs endif ### Override checks when `make clean/clean-all/html` @@ -37,7 +37,7 @@ PLATFORM = $(word 2,$(ARCH_SPLIT)) ### Checks end here endif -## 2. General Compilation Targets +## 2. Setup variables pointing to build and install directory ### Create the destination directory (`build/$ARCH`) WORK_DIR ?= $(shell pwd) @@ -47,7 +47,7 @@ INSTALLDIR ?= $(WORK_DIR)/build/install/$(ARCH) LIB_INSTALLDIR ?= $(INSTALLDIR)/lib INC_INSTALLDIR ?= $(INSTALLDIR)/include -## 3. General Compilation Flags +## 3. Toolchain setup ### (Cross) compilers, e.g., mips-linux-gnu-g++ CC ?= $(CROSS_COMPILE)gcc @@ -59,17 +59,13 @@ OBJDUMP ?= $(CROSS_COMPILE)objdump OBJCOPY ?= $(CROSS_COMPILE)objcopy READELF ?= $(CROSS_COMPILE)readelf -CXXFLAGS += $(CFLAGS) -ffreestanding -fno-rtti -fno-exceptions -LDFLAGS += -z noexecstack -INTERFACE_LDFLAGS += -z noexecstack - ## 4. Arch-Specific Configurations -### 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) - CROSS_COMPILE := riscv64-unknown-linux-gnu- -endif +# TODO: Removed CROSS_COMPILE toolchain setup as it's too complicated +# for Makefile to do right. Force the user to provide a CROSS_COMPILE +# prefix for now. They can also specify CFLAGS and LDFLAGS through +# environment variable. +include $(AM_HOME)/scripts/$(ARCH).mk ## 5. Compilation Rules @@ -77,16 +73,16 @@ BUILDDIR := $(DST_DIR) COMMON_CFLAGS := $(CFLAGS) -g -O3 -MMD -Wall \ -fno-asynchronous-unwind-tables -fno-builtin -fno-stack-protector \ -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden +INTERFACE_LDFLAGS += -z noexecstack +INTERFACE_CFLAGS += -fno-asynchronous-unwind-tables \ + -fno-builtin -fno-stack-protector \ + -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden ### Build libam #### Include archetecture specific build flags -include $(AM_HOME)/scripts/$(ARCH).mk COMMON_CFLAGS += -D__ARCH_$(shell echo $(ARCH) | tr a-z A-Z | tr - _) \ -D__ISA_$(shell echo $(ISA) | tr a-z A-Z)__ \ -DARCH_H=\"$(ARCH_H)\" -INTERFACE_CFLAGS += -DARCH_H=\"$(ARCH_H)\" \ - -fno-asynchronous-unwind-tables \ - -fno-builtin -fno-stack-protector \ - -Wno-main -U_FORTIFY_SOURCE -fvisibility=hidden +INTERFACE_CFLAGS += -DARCH_H=\"$(ARCH_H)\" #### 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 @@ -114,22 +110,7 @@ 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) - @echo + LD "->" $(IMAGE_REL).elf - @$(LD) $(LDFLAGS) -o $(IMAGE).elf --start-group $(LINKAGE) --end-group - -## 6. Miscellaneous - -### Build order control -image: image-dep -archive: $(ARCHIVE) -image-dep: $(OBJS) $(LIBS) - @echo \# Creating image [$(ARCH)] -.PHONY: image image-dep archive run libs $(LIBS) install - -### Install rules - +## 6. Install rules INTERFACE_INCPATH += $(sort $(KLIB_INTERFACE_INCPATH) $(AM_INTERFACE_INCPATH)) # TODO: Use sort here will cause error on seperated flags, such as: -e _start # but without sort, duplicated flags will not be removed. @@ -173,6 +154,8 @@ install-headers: $(HEADERS) # Headers needs to be reinstalled if they are change install: $(EXPORTS) install-libs install-headers $(LDSCRIPTS) +.PHONY: libs $(LIBS) install + ### Clean a single project (remove `build/`) clean: rm -rf Makefile.html $(WORK_DIR)/build/ diff --git a/scripts/isa/riscv.mk b/scripts/isa/riscv.mk index 89d3653..a8e7b17 100644 --- a/scripts/isa/riscv.mk +++ b/scripts/isa/riscv.mk @@ -1,4 +1,3 @@ -CROSS_COMPILE := riscv64-linux-gnu- 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 diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index 927f8bc..0e95484 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -10,21 +10,14 @@ AM_SRCS := am/src/platform/nemu/trm.c \ AM_PUBLIC_CFLAGS := -fdata-sections -ffunction-sections AM_PUBLIC_LDFLAGS := --defsym=_pmem_start=0x80000000 --defsym=_entry_offset=0x0 \ --gc-sections --entry=_start -AM_CFLAGS += $(AM_PUBLIC_CFLAGS) +AM_CFLAGS += $(AM_PUBLIC_CFLAGS) -DMAINARGS=\"$(mainargs)\" AM_LDFLAGS += -T$(AM_HOME)/scripts/linker.ld $(AM_PUBLIC_LDFLAGS) +AM_INCPATH += $(AM_HOME)/am/src/platform/nemu/include AM_INTERFACE_CFLAGS += $(AM_PUBLIC_CFLAGS) AM_INTERFACE_LDFLAGS += -T$(LIB_INSTALLDIR)/ldscripts/linker.ld $(AM_PUBLIC_LDFLAGS) - -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 - @$(OBJDUMP) -d $(IMAGE).elf > $(IMAGE).txt - @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 diff --git a/scripts/riscv32-nemu.mk b/scripts/riscv32-nemu.mk index eb7c24f..bf3dc58 100644 --- a/scripts/riscv32-nemu.mk +++ b/scripts/riscv32-nemu.mk @@ -1,7 +1,7 @@ include $(AM_HOME)/scripts/isa/riscv.mk include $(AM_HOME)/scripts/platform/nemu.mk -AM_CFLAGS += -DISA_H=\"riscv/riscv.h\" -march=rv32im_zicsr -mabi=ilp32 # overwrite -AM_LDFLAGS += -melf32lriscv # overwrite +AM_CFLAGS += -DISA_H=\"riscv/riscv.h\" -march=rv32im_zicsr -mabi=ilp32 +AM_LDFLAGS += -melf32lriscv AM_SRCS += am/src/riscv/nemu/start.S \ am/src/riscv/nemu/cte.c \