diff --git a/Makefile b/Makefile index 139c53e..23a8afe 100644 --- a/Makefile +++ b/Makefile @@ -123,14 +123,19 @@ $(LIBS): %: @$(MAKE) -s -C $(AM_HOME)/$* archive ### Rule (link): objects (`*.o`) and libraries (`*.a`) -> `IMAGE.elf`, the final ELF binary to be packed into image (ld) -$(IMAGE).elf: $(OBJS) $(LIBS) +$(IMAGE).elf: $(LINKAGE) + @echo \# Creating image [$(ARCH)] @echo + LD "->" $(IMAGE_REL).elf - @$(LD) $(LDFLAGS) -o $(IMAGE).elf --start-group $(LINKAGE) --end-group +ifneq ($(filter $(ARCH),native),) + $(CXX) -o $@ -Wl,--whole-archive $^ -Wl,-no-whole-archive $(LDFLAGS_CXX) +else + @$(LD) $(LDFLAGS) -o $@ --start-group $^ --end-group +endif ### Rule (archive): objects (`*.o`) -> `ARCHIVE.a` (ar) $(ARCHIVE): $(OBJS) @echo + AR "->" $(shell realpath $@ --relative-to .) - @$(AR) rcs $(ARCHIVE) $(OBJS) + @$(AR) rcs $@ $^ ### Rule (`#include` dependencies): paste in `.d` files generated by gcc on `-MMD` -include $(addprefix $(DST_DIR)/, $(addsuffix .d, $(basename $(SRCS)))) @@ -140,8 +145,8 @@ $(ARCHIVE): $(OBJS) ### Build order control image: image-dep archive: $(ARCHIVE) -image-dep: $(OBJS) $(LIBS) - @echo \# Creating image [$(ARCH)] +image-dep: $(LIBS) $(IMAGE).elf +.NOTPARALLEL: image-dep .PHONY: image image-dep archive run $(LIBS) ### Clean a single project (remove `build/`) diff --git a/scripts/native.mk b/scripts/native.mk index e6ebe04..af7ec10 100644 --- a/scripts/native.mk +++ b/scripts/native.mk @@ -15,14 +15,10 @@ AM_SRCS := native/trm.c \ CFLAGS += -fpie ASFLAGS += -fpie -pie comma = , -LDFLAGS_CXX = $(addprefix -Wl$(comma), $(LDFLAGS)) - -image: - @echo + LD "->" $(IMAGE_REL) - @g++ -pie -o $(IMAGE) -Wl,--whole-archive $(LINKAGE) -Wl,-no-whole-archive $(LDFLAGS_CXX) -lSDL2 -ldl +LDFLAGS_CXX = $(addprefix -Wl$(comma), $(LDFLAGS)) -pie -lSDL2 -ldl run: image - $(IMAGE) + $(IMAGE).elf gdb: image - gdb -ex "handle SIGUSR1 SIGUSR2 SIGSEGV noprint nostop" $(IMAGE) + gdb -ex "handle SIGUSR1 SIGUSR2 SIGSEGV noprint nostop" $(IMAGE).elf diff --git a/scripts/platform/nemu.mk b/scripts/platform/nemu.mk index 8367675..5536816 100644 --- a/scripts/platform/nemu.mk +++ b/scripts/platform/nemu.mk @@ -17,7 +17,7 @@ CFLAGS += -DMAINARGS=\"$(mainargs)\" CFLAGS += -I$(AM_HOME)/am/src/platform/nemu/include .PHONY: $(AM_HOME)/am/src/platform/nemu/trm.c -image: $(IMAGE).elf +image: image-dep @$(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 diff --git a/scripts/platform/npc.mk b/scripts/platform/npc.mk index 3136fec..6cbc7f8 100644 --- a/scripts/platform/npc.mk +++ b/scripts/platform/npc.mk @@ -15,7 +15,7 @@ LDFLAGS += --gc-sections -e _start CFLAGS += -DMAINARGS=\"$(mainargs)\" .PHONY: $(AM_HOME)/am/src/riscv/npc/trm.c -image: $(IMAGE).elf +image: image-dep @$(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 diff --git a/scripts/platform/qemu.mk b/scripts/platform/qemu.mk index 67bcd67..776d91a 100644 --- a/scripts/platform/qemu.mk +++ b/scripts/platform/qemu.mk @@ -11,7 +11,7 @@ build-arg: image BOOT_HOME := $(AM_HOME)/am/src/x86/qemu/boot -image: $(IMAGE).elf +image: image-dep @$(MAKE) -s -C $(BOOT_HOME) @echo + CREATE "->" $(IMAGE_REL) @( cat $(BOOT_HOME)/bootblock.o; head -c 1024 /dev/zero; cat $(IMAGE).elf ) > $(IMAGE) diff --git a/scripts/spike.mk b/scripts/spike.mk index 4ea391c..6147b99 100644 --- a/scripts/spike.mk +++ b/scripts/spike.mk @@ -16,4 +16,4 @@ LDFLAGS += --gc-sections -e _start CFLAGS += -DMAINARGS=\"$(mainargs)\" .PHONY: $(AM_HOME)/am/src/riscv/spike/trm.c -image: $(IMAGE).elf +image: image-dep