diff --git a/Makefile.common b/Makefile.common index ff01f4f5..4bd555d0 100644 --- a/Makefile.common +++ b/Makefile.common @@ -20,8 +20,9 @@ TARGET_FAMILY ?= $(firstword $(subst -, ,$(TARGET)-)) TARGET_SUBFAMILY ?= $(word 2,$(subst -, ,$(TARGET)-)) # reset env variables -CFLAGS := -CXXFLAGS := +CFLAGS := # flags related to C files compilation +CXXFLAGS := # flags related to C++ files compilation +CPPFLAGS := # flags for C preprocessor (C & C++) LDFLAGS := LDFLAGS_PREFIX := @@ -34,8 +35,7 @@ ifneq ($(filter-out -O%,$(OLVL)),) endif ifneq ($(DEBUG), 1) - CFLAGS += -DNDEBUG - CXXFLAGS += -DNDEBUG + CPPFLAGS += -DNDEBUG endif TOPDIR := $(CURDIR) @@ -59,8 +59,8 @@ ALL_COMPONENTS := PREFIX_FS ?= $(abspath $(CURDIR)/../_fs/$(TARGET)) PREFIX_ROOTFS ?= $(PREFIX_FS)/root/ -# Check TARGET and set building options -include $(MAKES_PATH)/check-target.mk +# Check TARGET, set TARGET_SUFF and include target building options +include $(MAKES_PATH)/include-target.mk # setup tools include $(MAKES_PATH)/setup-tools.mk @@ -81,18 +81,19 @@ $(info cleaning targets, make parallelism disabled) .NOTPARALLEL: endif -# Distribute the __TARGET and __CPU defines -CFLAGS += -D__TARGET_$(call uppercase,$(TARGET_FAMILY)) -CXXFLAGS += -D__TARGET_$(call uppercase,$(TARGET_FAMILY)) - -CFLAGS += -D__CPU_$(call uppercase,$(TARGET_SUBFAMILY)) -CXXFLAGS += -D__CPU_$(call uppercase,$(TARGET_SUBFAMILY)) - -# generic CFLAGS/LDFLAGS +# +# generic *FLAGS options +# +# common include/lib paths LDFLAGS += -L$(PREFIX_A) +ifneq ($(KERNEL), 1) + CFLAGS += -I$(PREFIX_H) + CXXFLAGS += -I$(PREFIX_H) +endif # make PROJECT_PATH the first search dir to allow project customizations/monkey-patching CFLAGS := -I$(PROJECT_PATH) $(CFLAGS) +CXXFLAGS := -I$(PROJECT_PATH) $(CXXFLAGS) # add multilib directory to library search path SYSROOT := $(shell $(CC) $(CFLAGS) -print-sysroot) @@ -104,10 +105,31 @@ LDFLAGS += -L$(LIBC_INSTALL_DIR) CFLAGS += -fmacro-prefix-map=$(dir $(TOPDIR))= CXXFLAGS += -fmacro-prefix-map=$(dir $(TOPDIR))= -ifneq ($(KERNEL), 1) - CFLAGS += -I$(PREFIX_H) - CXXFLAGS += -I$(PREFIX_H) -endif +# garbage-collect unused code/data +# NOTE: exported to ports also as it reduces binaries size greatly +CFLAGS += -ffunction-sections -fdata-sections +LDFLAGS += $(LDFLAGS_PREFIX)--gc-sections + +### right now we should have only target-necessary flags, save them for exporting (for building ports) ### +EXPORT_CFLAGS := $(CFLAGS) +EXPORT_CXXFLAGS := $(CXXFLAGS) +EXPORT_LDFLAGS := $(LDFLAGS) + +# add our coding-style related options +CFLAGS += -Wall -Wstrict-prototypes +CXXFLAGS += -Wall + +# always produce binaries with debug information +CFLAGS += -ggdb3 +CXXFLAGS += -ggdb3 + +# set optimization level (target/project-dependant) +CFLAGS += $(OLVL) +CXXFLAGS += $(OLVL) + +# Distribute the __TARGET and __CPU defines +CPPFLAGS += -D__TARGET_$(call uppercase,$(TARGET_FAMILY)) +CPPFLAGS += -D__CPU_$(call uppercase,$(TARGET_SUBFAMILY)) # # Generic rules @@ -115,13 +137,13 @@ endif .PHONY: help export-cflags export-cxxflags export-ldflags export-cflags: - @echo $(CFLAGS) + @echo $(EXPORT_CFLAGS) export-cxxflags: - @echo $(CXXFLAGS) + @echo $(EXPORT_CXXFLAGS) export-ldflags: - @echo $(LDFLAGS) + @echo $(EXPORT_LDFLAGS) help: $(info DEFAULT_COMPONENTS := $(DEFAULT_COMPONENTS)) @@ -168,26 +190,26 @@ INSTALL_FS = $(SIL)printf "INSTALL %s\n" "$(@:$(PREFIX_ROOTFS)%=%)"; \ $(PREFIX_O)%.o: %.c @mkdir -p $(@D) @printf "CC %-24s\n" "$<" - $(SIL)$(CC) -c $(CFLAGS) "$(abspath $<)" -o "$@" - $(SIL)$(CC) -M -MD -MP -MF $(PREFIX_O)$*.c.d -MT "$@" $(CFLAGS) $< + $(SIL)$(CC) -c $(CPPFLAGS) $(CFLAGS) "$(abspath $<)" -o "$@" + $(SIL)$(CC) -M -MD -MP -MF $(PREFIX_O)$*.c.d -MT "$@" $(CPPFLAGS) $(CFLAGS) $< $(PREFIX_O)%.o: %.cc @mkdir -p $(@D) @printf "CXX %-24s\n" "$<" - $(SIL)$(CXX) -c $(CXXFLAGS) "$(abspath $<)" -o "$@" - $(SIL)$(CXX) -M -MD -MP -MF $(PREFIX_O)$*.cc.d -MT "$@" $(CXXFLAGS) $< + $(SIL)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) "$(abspath $<)" -o "$@" + $(SIL)$(CXX) -M -MD -MP -MF $(PREFIX_O)$*.cc.d -MT "$@" $(CPPFLAGS) $(CXXFLAGS) $< $(PREFIX_O)%.o: %.cpp @mkdir -p $(@D) @printf "CXX %-24s\n" "$<" - $(SIL)$(CXX) -c $(CXXFLAGS) "$(abspath $<)" -o "$@" - $(SIL)$(CXX) -M -MD -MP -MF $(PREFIX_O)$*.cpp.d -MT "$@" $(CXXFLAGS) $< + $(SIL)$(CXX) -c $(CPPFLAGS) $(CXXFLAGS) "$(abspath $<)" -o "$@" + $(SIL)$(CXX) -M -MD -MP -MF $(PREFIX_O)$*.cpp.d -MT "$@" $(CPPFLAGS) $(CXXFLAGS) $< $(PREFIX_O)%.o: %.S @mkdir -p $(@D) @printf "ASM %-24s\n" "$<" - $(SIL)$(CC) -c $(CFLAGS) "$<" -o "$@" - $(SIL)$(CC) -M -MD -MP -MF $(PREFIX_O)$*.S.d -MT "$@" $(CFLAGS) $< + $(SIL)$(CC) -c $(CPPFLAGS) $(CFLAGS) "$<" -o "$@" + $(SIL)$(CC) -M -MD -MP -MF $(PREFIX_O)$*.S.d -MT "$@" $(CPPFLAGS) $(CFLAGS) $< $(PREFIX_PROG_STRIPPED)%: $(PREFIX_PROG)% @mkdir -p $(@D) diff --git a/build.sh b/build.sh index 1a480ac8..4eef8432 100755 --- a/build.sh +++ b/build.sh @@ -1,4 +1,5 @@ #!/usr/bin/env bash +# shellcheck source-path=SCRIPTDIR/.. # # Shell script for building Phoenix-RTOS based firmware # @@ -60,8 +61,8 @@ export TARGET TARGET_FAMILY TARGET_SUBFAMILY TARGET_PROJECT PROJECT_PATH PREFIX_ # export flags for ports - call make only after all necessary env variables are already set EXPORT_CFLAGS="$(make -f phoenix-rtos-build/Makefile.common export-cflags)" -# export only generic flags: "-z xxx", "-Lxxx", "-q" -EXPORT_LDFLAGS="$(make -f phoenix-rtos-build/Makefile.common export-ldflags | grep -E -o "(-z [^ ]+)|(-L[^ ]+)|(-q)" | xargs)" +# Convert ldflags to format recognizable by gcc, for example -q -> -Wl,-q +EXPORT_LDFLAGS="$(make -f phoenix-rtos-build/Makefile.common export-ldflags | sed "s/\s/,/g" | sed 's/^-\|,-/ -Wl,-/g')" export EXPORT_CFLAGS EXPORT_LDFLAGS diff --git a/makes/check-target.mk b/makes/include-target.mk similarity index 96% rename from makes/check-target.mk rename to makes/include-target.mk index bf2923bd..09f3ea36 100644 --- a/makes/check-target.mk +++ b/makes/include-target.mk @@ -77,4 +77,4 @@ ifeq (,$(filter $(TARGETS),$(TARGET_FAMILY)-$(TARGET_SUBFAMILY))) $(error $(MESSAGE)$(LF)Available targets:$(LF)$(subst $(SPACE),$(LF),$(sort $(TARGETS))$(LF))) endif -include $(MAKES_PATH)/../Makefile.$(TARGET_SUFF) +include $(MAKES_PATH)/../target/$(TARGET_SUFF).mk diff --git a/Makefile.armv7a b/target/armv7a.mk similarity index 79% rename from Makefile.armv7a rename to target/armv7a.mk index 00334004..94bbcbb5 100644 --- a/Makefile.armv7a +++ b/target/armv7a.mk @@ -12,7 +12,6 @@ CC := $(CROSS)gcc CXX := $(CROSS)g++ OLVL ?= -O2 -CFLAGS += $(OLVL) cpu := cortex-$(subst armv7,,$(TARGET_FAMILY)) @@ -24,19 +23,14 @@ else $(error Incorrect TARGET.) endif -CFLAGS += -Wall -Wstrict-prototypes -g\ - -mcpu=$(cpu) -mtune=$(cpu) -mthumb\ - -fomit-frame-pointer -mno-unaligned-access -fdata-sections -ffunction-sections - -CXXFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) - -VADDR_KERNEL_INIT = 0xc0000000 +CFLAGS += -mcpu=$(cpu) -mtune=$(cpu) -mthumb -fomit-frame-pointer -mno-unaligned-access +CXXFLAGS := $(CFLAGS) AR = $(CROSS)ar ARFLAGS = -r LD = $(CROSS)ld -LDFLAGS = -z max-page-size=0x1000 --gc-sections +LDFLAGS = -z max-page-size=0x1000 GCCLIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) CRTBEGIN := $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o) CRTEND := $(shell $(CC) $(CFLAGS) -print-file-name=crtend.o) diff --git a/Makefile.armv7m b/target/armv7m.mk similarity index 71% rename from Makefile.armv7m rename to target/armv7m.mk index 7ab7aaa0..77f5bbbf 100644 --- a/Makefile.armv7m +++ b/target/armv7m.mk @@ -13,11 +13,8 @@ CXX := $(CROSS)g++ # common Cortex-M CFLAGS OLVL ?= -O2 -CFLAGS += $(OLVL) -CFLAGS += -Wall -Wstrict-prototypes -g CFLAGS += -mthumb -fomit-frame-pointer -mno-unaligned-access -CFLAGS += -DNOMMU -CFLAGS += -fdata-sections -ffunction-sections +CPPFLAGS += -DNOMMU ifeq ($(TARGET_FAMILY), armv7m7) CFLAGS += -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-d16 @@ -28,45 +25,43 @@ else ifeq ($(TARGET_FAMILY), armv7m3) endif ifeq ($(TARGET_SUBFAMILY), stm32l152xd) - RAM_SIZE=48 - VADDR_KERNEL_INIT=0800d000 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0800d000 + KERNEL_TARGET_DEFINE=-DNOMMU else ifeq ($(TARGET_SUBFAMILY), stm32l152xe) - RAM_SIZE=80 - VADDR_KERNEL_INIT=0800d000 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0800d000 + KERNEL_TARGET_DEFINE=-DNOMMU else ifeq ($(TARGET_SUBFAMILY), stm32l4x6) - RAM_SIZE=320 - VADDR_KERNEL_INIT=0800d000 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0800d000 + KERNEL_TARGET_DEFINE=-DNOMMU else ifeq ($(TARGET_SUBFAMILY), imxrt105x) - VADDR_KERNEL_INIT=0 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0 + KERNEL_TARGET_DEFINE=-DNOMMU else ifeq ($(TARGET_SUBFAMILY), imxrt106x) - VADDR_KERNEL_INIT=0 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0 + KERNEL_TARGET_DEFINE=-DNOMMU else ifeq ($(TARGET_SUBFAMILY), imxrt117x) - VADDR_KERNEL_INIT=0 - KERNEL_TARGET_DEFINE=-DNOMMU + VADDR_KERNEL_INIT=0 + KERNEL_TARGET_DEFINE=-DNOMMU else - $(error Incorrect TARGET.) + $(error Incorrect TARGET: $(TARGET)) endif -LDFLAGS := -z max-page-size=0x10 --gc-sections +LDFLAGS := -z max-page-size=0x10 ifeq ($(KERNEL), 1) - CFLAGS += -DRAM_SIZE=$(RAM_SIZE) $(KERNEL_TARGET_DEFINE) -ffixed-r9 + CPPFLAGS += $(KERNEL_TARGET_DEFINE) + CFLAGS += -ffixed-r9 LDFLAGS += -Tbss=20000000 -Tdata=20000000 STRIP = $(CROSS)strip else CFLAGS += -fpic -fpie -msingle-pic-base -mno-pic-data-is-text-relative - # output .rel.* sections to make ELF position-independent + # output .rel.* sections to make ELF position-independent LDFLAGS += -q STRIP = $(PREFIX_PROJECT)/phoenix-rtos-build/scripts/strip.py $(CROSS)strip --strip-unneeded -R .rel.text endif -CXXFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) +CXXFLAGS := $(CFLAGS) AR = $(CROSS)ar ARFLAGS = -r diff --git a/Makefile.host b/target/host.mk similarity index 74% rename from Makefile.host rename to target/host.mk index 7ca0bbe7..1f0c6ac0 100644 --- a/Makefile.host +++ b/target/host.mk @@ -12,14 +12,12 @@ CC := $(CROSS)gcc CXX := $(CROSS)g++ OLVL ?= -O2 -CFLAGS += $(OLVL) -CFLAGS += -Wall -Wstrict-prototypes -g -fomit-frame-pointer -ffunction-sections -fdata-sections +CFLAGS += -fomit-frame-pointer AR := $(CROSS)ar ARFLAGS = -r LD := $(CROSS)gcc -LDFLAGS += -Wl,--gc-sections LDFLAGS_PREFIX := -Wl, OBJCOPY := $(CROSS)objcopy @@ -32,8 +30,8 @@ ifneq ($(NOSAN), 1) LDFLAGS += -fsanitize=address,undefined endif +CXXFLAGS := $(CFLAGS) + # install unstripped binaries in rootfs # (cruicial for tests binaries with debug info for meaningful sanitizers info) ROOTFS_INSTALL_UNSTRIPPED := y - -CXXFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) diff --git a/Makefile.ia32 b/target/ia32.mk similarity index 75% rename from Makefile.ia32 rename to target/ia32.mk index 123be669..d3242989 100644 --- a/Makefile.ia32 +++ b/target/ia32.mk @@ -12,19 +12,16 @@ CC := $(CROSS)gcc CXX := $(CROSS)g++ OLVL ?= -O2 -CFLAGS += $(OLVL) -CFLAGS += -g -Wall -Wstrict-prototypes\ - -m32 -march=i586 -mtune=generic -mno-mmx -mno-sse -fno-pic -fno-pie\ - -fomit-frame-pointer -fno-builtin-malloc\ - -fdata-sections -ffunction-sections +CFLAGS += -m32 -march=i586 -mtune=generic -mno-mmx -mno-sse -fno-pic -fno-pie\ + -fomit-frame-pointer -fno-builtin-malloc -CXXFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) +CXXFLAGS := $(CFLAGS) AR = $(CROSS)ar ARFLAGS = -r LD = $(CROSS)ld -LDFLAGS := --gc-sections +LDFLAGS := GCCLIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) CRTBEGIN := $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o) CRTEND := $(shell $(CC) $(CFLAGS) -print-file-name=crtend.o) diff --git a/Makefile.riscv64 b/target/riscv64.mk similarity index 78% rename from Makefile.riscv64 rename to target/riscv64.mk index 98f697db..7ed3c166 100644 --- a/Makefile.riscv64 +++ b/target/riscv64.mk @@ -14,12 +14,9 @@ CC = $(CROSS)gcc CXX := $(CROSS)g++ OLVL ?= -O2 -CFLAGS += $(OLVL) -# FIXME: -ffunction-sections and -fdata-sections are missing -CFLAGS += -Wall -Wstrict-prototypes -g\ - -fomit-frame-pointer -mcmodel=medany -fno-builtin -DTARGET_RISCV64 +CFLAGS += -fomit-frame-pointer -mcmodel=medany -fno-builtin -CFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) +CXXFLAGS := $(CFLAGS) AR = $(CROSS)ar ARFLAGS = -r diff --git a/Makefile.sparcv8leon3 b/target/sparcv8leon3.mk similarity index 82% rename from Makefile.sparcv8leon3 rename to target/sparcv8leon3.mk index 52dc45e5..249acc7a 100644 --- a/Makefile.sparcv8leon3 +++ b/target/sparcv8leon3.mk @@ -14,17 +14,14 @@ CC := $(CROSS)gcc CXX := $(CROSS)g++ OLVL ?= -O2 -CFLAGS += $(OLVL) -CFLAGS += -g -Wall -Wstrict-prototypes \ - -mcpu=leon3 -msoft-float \ - -DNOMMU \ - -fdata-sections -ffunction-sections +CFLAGS += -mcpu=leon3 -msoft-float +CPPFLAGS := -DNOMMU AR = $(CROSS)ar ARFLAGS = -r LD = $(CROSS)ld -LDFLAGS := -z max-page-size=0x200 --gc-sections +LDFLAGS := -z max-page-size=0x200 VADDR_KERNEL_INIT=31000000 @@ -37,7 +34,7 @@ else STRIP = $(CROSS)strip --strip-unneeded -R .rela.text endif -CXXFLAGS += $(filter-out -Wstrict-prototypes, $(CFLAGS)) +CXXFLAGS := $(CFLAGS) GCCLIB := $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) CRTBEGIN := $(shell $(CC) $(CFLAGS) -print-file-name=crtbegin.o)