Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

makes: move common FLAGS to Makefile.common + rearrange makefiles #154

Merged
merged 3 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 50 additions & 28 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -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 :=

Expand All @@ -34,8 +35,7 @@ ifneq ($(filter-out -O%,$(OLVL)),)
endif

ifneq ($(DEBUG), 1)
CFLAGS += -DNDEBUG
CXXFLAGS += -DNDEBUG
CPPFLAGS += -DNDEBUG
endif

TOPDIR := $(CURDIR)
Expand All @@ -59,7 +59,7 @@ ALL_COMPONENTS :=
PREFIX_FS ?= $(abspath $(CURDIR)/../_fs/$(TARGET))
PREFIX_ROOTFS ?= $(PREFIX_FS)/root/

# Check TARGET and set building options
# Check TARGET, set TARGET_SUFF and include target building options
include $(MAKES_PATH)/check-target.mk

# setup tools
Expand All @@ -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)
Expand All @@ -104,24 +105,45 @@ 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
#
.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))
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions build.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env bash
# shellcheck source-path=SCRIPTDIR/..
#
# Shell script for building Phoenix-RTOS based firmware
#
Expand All @@ -16,7 +17,7 @@
ROOTFS_OVERLAYS=""

source ./phoenix-rtos-build/build.subr
source ./build.project

Check warning on line 20 in build.sh

View workflow job for this annotation

GitHub Actions / call-lint / shellcheck-pr

[shellcheck] reported by reviewdog 🐶 Not following: ./build.project: openBinaryFile: does not exist (No such file or directory) [SC1091](https://github.com/koalaman/shellcheck/wiki/SC1091) Raw Output: ./build.sh:20:8:info:Not following: ./build.project: openBinaryFile: does not exist (No such file or directory) [SC1091](https://github.com/koalaman/shellcheck/wiki/SC1091)

PREFIX_PROJECT="$(pwd)"

Expand Down Expand Up @@ -60,8 +61,8 @@

# 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

Expand Down
2 changes: 1 addition & 1 deletion makes/check-target.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 3 additions & 9 deletions Makefile.armv7a → target/armv7a.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ CC := $(CROSS)gcc
CXX := $(CROSS)g++

OLVL ?= -O2
CFLAGS += $(OLVL)

cpu := cortex-$(subst armv7,,$(TARGET_FAMILY))

Expand All @@ -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)
Expand Down
43 changes: 19 additions & 24 deletions Makefile.armv7m → target/armv7m.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 3 additions & 5 deletions Makefile.host → target/host.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
11 changes: 4 additions & 7 deletions Makefile.ia32 → target/ia32.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 2 additions & 5 deletions Makefile.riscv64 → target/riscv64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 4 additions & 7 deletions Makefile.sparcv8leon3 → target/sparcv8leon3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down
Loading