Skip to content

Commit

Permalink
Adapt build env to shared libs
Browse files Browse the repository at this point in the history
JIRA: RTOS-664
  • Loading branch information
badochov committed Jan 31, 2024
1 parent 14a4a73 commit 55e17e4
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ CURR_SUFFIX := $(patsubst $(TOPDIR)/%,%,$(abspath $(CURDIR))/)
PREFIX_O := $(BUILD_DIR)/$(CURR_SUFFIX)

# target install paths, can be provided exterally
PREFIX_SO ?= $(PREFIX_BUILD)/libexec/
PREFIX_A ?= $(PREFIX_BUILD)/lib/
PREFIX_H ?= $(PREFIX_BUILD)/include/
PREFIX_PROG ?= $(PREFIX_BUILD)/prog/
Expand All @@ -74,11 +75,16 @@ include $(MAKES_PATH)/funcs.mk

# provide template files' paths to external makes
binary.mk := $(MAKES_PATH)/binary.mk
binary-dyn.mk := $(MAKES_PATH)/binary-dyn.mk
static-lib.mk := $(MAKES_PATH)/static-lib.mk
shared-lib.mk := $(MAKES_PATH)/shared-lib.mk

# default path for the programs to be installed in rootfs
DEFAULT_INSTALL_PATH := /bin

# default path for the shared-libraries to be installed in rootfs
DEFAULT_INSTALL_PATH_SO := /usr/lib

# do not clean and build in parallel
ifneq ($(filter %clean,$(MAKECMDGOALS)),)
$(info cleaning targets, make parallelism disabled)
Expand All @@ -90,6 +96,7 @@ endif
#
# common include/lib paths
LDFLAGS += -L$(PREFIX_A)

ifneq ($(KERNEL), 1)
CFLAGS += -I$(PREFIX_H)
CXXFLAGS += -I$(PREFIX_H)
Expand Down
21 changes: 21 additions & 0 deletions makes/binary-dyn.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Makefile rules for compiling and linking dynamically linked binary file
# supported external variables, besides those stated in binary.mk:
# - DEP_LIBS_SHARED - shared libraries from current repo needed to be compiled/installed before this component (shortcut for putting something in LIBS and DEPS)
# - LIBS_SHARED - names of the shared libs to link the binary against (without .so suffix)

RESOLVED_LIBS_SHARED := $(patsubst lib%,-l%, $(DEP_LIBS_SHARED) $(LIBS_SHARED))

DEPS += $(DEP_LIBS_SHARED)

# Add shared libraries directory search path
LD_FLAGS_DYN := -L$(PREFIX_SO)

LOCAL_LDFLAGS += $(LD_FLAGS_DYN)
LOCAL_LDLIBS += $(RESOLVED_LIBS_SHARED)

DYNAMIC_BINARY := y

include $(binary.mk)

DEP_LIBS_SHARED :=
LIBS_SHARED :=
8 changes: 8 additions & 0 deletions makes/binary.mk
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ RESOLVED_LIBS += $(patsubst %,$(PREFIX_A)%.a, $(LIBS))
DEPS += $(DEP_LIBS)
$(OBJS.$(NAME)): | $(DEPS)

ifneq ($(DYNAMIC_BINARY), y)
# FIXME: remove once hostutils use binary-dyn.mk
ifneq ($(TARGET_FAMILY), host)
LOCAL_LDFLAGS += -static
endif
endif

# potentially custom CFLAGS/CXXFLAGS/LDFLAGS for compilation and linking
# add ABS_HEADERS_DIR to CFLAGS/CXXFLAGS as a first -I path to build always using local headers instead of installed ones
$(OBJS.$(NAME)): CFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CFLAGS) $(LOCAL_CFLAGS)
Expand Down Expand Up @@ -126,3 +133,4 @@ LOCAL_CXXFLAGS :=
LOCAL_LDFLAGS :=
LOCAL_LDLIBS :=
LOCAL_INSTALL_PATH :=
undefine DYNAMIC_BINARY
123 changes: 123 additions & 0 deletions makes/shared-lib.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Makefile rules for compiling a shared library
# supported external variables:
# - NAME - component/target binary name
# - LOCAL_SRCS - list of source files relative to current makefile
# - SRCS - list of source files relative to project root
# - LOCAL_HEADERS - headers to be installed (relative to current makefile)
# - LOCAL_HEADERS_DIR - headers tree be installed (relative to current makefile) - default "include"
# - HEADERS - headers to be installed (relative to project root)
#
# - DEPS - list of components from current repo to be completed before starting this one
#
# - LOCAL_CFLAGS - additional CFLAGS for current component compilation
# - LOCAL_CXXFLAGS - additional CXXFLAGS for current component compilation
#
# - LOCAL_INSTALL_PATH - custom rootfs dir for the shared library to be installed (if not provided - DEFAULT_INSTALL_PATH_SO)




# directory with current Makefile - relative to the repository root
# filter-out all Makefiles outside of TOPDIR
# WARNING: LOCAL_DIR computation would fail if any Makefile include would be done before including this file
# if necessary set LOCAL_DIR := $(call my-dir) at the beginning of the Makefile
ifeq ($(origin LOCAL_DIR), undefined)
CLIENT_MAKES := $(filter $(TOPDIR)/%,$(abspath $(MAKEFILE_LIST)))
LOCAL_DIR := $(patsubst $(TOPDIR)/%,%,$(dir $(lastword $(CLIENT_MAKES))))
endif

# binary.mk clears all variables it uses so we should expect that they are not set here. Leaving them set would
# influence next binary.mk call leading to unexpected errors
ifneq ($(DEP_LIB)$(LIBS)$(LOCAL_LDFLAGS)$(LOCAL_LDLIBS),)
$(warning $(NAME): DEP_LIB=$(DEP_LIB))
$(warning $(NAME): LIBS=$(LIBS))
$(warning $(NAME): LOCAL_LDFLAGS=$(LOCAL_LDFLAGS))
$(warning $(NAME): LOCAL_LDLIBS=$(LOCAL_LDLIBS))
$(error $(NAME): shared-lib.mk invoked with args reserved for binary.mk)
endif

# external headers - by default "include" dir - to disable functionality set "LOCAL_HEADERS_DIR := nothing"
LOCAL_HEADERS_DIR ?= include
ABS_HEADERS_DIR := $(abspath ./$(LOCAL_DIR)/$(LOCAL_HEADERS_DIR))

SRCS += $(addprefix $(LOCAL_DIR), $(LOCAL_SRCS))
HEADERS += $(addprefix $(LOCAL_DIR), $(LOCAL_HEADERS))

# removing all files with unsupported extensions
SRCS := $(filter $(LANGUAGE_EXTENSIONS), $(SRCS))

# linking prerequisites
OBJS.$(NAME) := $(patsubst %,$(PREFIX_O)%.o,$(basename $(SRCS)))

# compilation prerequisites - component order-only dependency
$(OBJS.$(NAME)): | $(DEPS)

# Shared lib flags
SHARED_LIB_LD_FLAGS := $(TARGET_PIC_FLAG) -shared -nostartfiles $(LDFLAGS_PREFIX)--warn-shared-textrel

# potentially custom CFLAGS/CXXFLAGS/LDFLAGS for compilation and linking
# add ABS_HEADERS_DIR to CFLAGS/CXXFLAGS to build always using local headers instead of installed ones
$(OBJS.$(NAME)): CFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CFLAGS) $(LOCAL_CFLAGS) $(TARGET_PIC_FLAG)
$(OBJS.$(NAME)): CXXFLAGS:=-I"$(ABS_HEADERS_DIR)" $(CXXFLAGS) $(LOCAL_CXXFLAGS) $(TARGET_PIC_FLAG)
$(PREFIX_SO)$(NAME).so: LDFLAGS:=$(LDFLAGS) $(SHARED_LIB_LD_FLAGS)

# dynamically generated dependencies (file-to-file dependencies)
DEPS.$(NAME) := $(patsubst %,$(PREFIX_O)%.d,$(SRCS))
-include $(DEPS.$(NAME))

# rule for installing headers
INSTALLED_HEADERS.$(NAME) := $(patsubst $(LOCAL_DIR)%.h, $(PREFIX_H)%.h, $(HEADERS))

# external headers dir support (install whole subtree)
INSTALLED_HEADERS_TREE.$(NAME) := $(patsubst $(ABS_HEADERS_DIR)/%,$(PREFIX_H)%,$(shell find $(ABS_HEADERS_DIR) -type f -name '*.h' 2>/dev/null))

ifneq ($(filter-out $(PREFIX_H)%, $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME))),)
$(error $(NAME): Installing headers outside of PREFIX_H, check Your makefile: $(INSTALLED_HEADERS.$(NAME) $(INSTALLED_HEADERS_TREE.$(NAME))))
endif

$(INSTALLED_HEADERS.$(NAME)): $(PREFIX_H)%.h: $(LOCAL_DIR)%.h
$(HEADER)

$(INSTALLED_HEADERS_TREE.$(NAME)): $(PREFIX_H)%.h: $(ABS_HEADERS_DIR)/%.h
$(HEADER)

# rule for linking shared lib
$(PREFIX_SO)$(NAME).so: $(OBJS.$(NAME))
$(LINK)

# create component phony targets
.PHONY: $(NAME) $(NAME)-headers $(NAME)-clean

$(NAME)-headers: $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME))

$(NAME): $(NAME)-headers $(PREFIX_SO)$(NAME).so

$(NAME)-clean:
@echo "cleaning $(NAME)"
@rm -rf $(OBJS.$(NAME)) $(DEPS.$(NAME)) $(INSTALLED_HEADERS.$(NAME)) $(INSTALLED_HEADERS_TREE.$(NAME)) $(PREFIX_SO)$(NAME).so

# install into the root filesystem
LOCAL_INSTALL_PATH := $(or $(LOCAL_INSTALL_PATH),$(DEFAULT_INSTALL_PATH_SO))

$(NAME)-install: $(NAME) $(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/$(NAME).so
$(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/$(NAME).so: $(PREFIX_SO)$(NAME).so
$(INSTALL_FS)

# necessary for NAME variable to be correctly set in recepies
$(NAME) $(NAME)-clean: NAME:=$(NAME)

ALL_COMPONENTS += $(NAME)

# cleaning vars to avoid strange errors
NAME :=
LOCAL_SRCS :=
undefine LOCAL_DIR # need to treat LOCAL_DIR="" as a valid (set-extenally) value
LOCAL_HEADERS :=
undefine LOCAL_HEADERS_DIR # undefine needed for default value to work in next component
DEPS :=
SRCS :=
HEADERS :=
LOCAL_CFLAGS :=
LOCAL_CXXFLAGS :=
undefine LOCAL_INSTALL_PATH

3 changes: 2 additions & 1 deletion makes/static-lib.mk
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Makefile rules for compiling and static library
# Makefile rules for compiling a static library
# supported external variables:
# - NAME - component/target binary name
# - LOCAL_SRCS - list of source files relative to current makefile
Expand Down Expand Up @@ -32,6 +32,7 @@ ifneq ($(DEP_LIB)$(LIBS)$(LOCAL_LDFLAGS)$(LOCAL_LDLIBS)$(LOCAL_INSTALL_PATH),)
$(warning $(NAME): LOCAL_LDFLAGS=$(LOCAL_LDFLAGS))
$(warning $(NAME): LOCAL_LDLIBS=$(LOCAL_LDLIBS))
$(warning $(NAME): LOCAL_INSTALL_PATH=$(LOCAL_INSTALL_PATH))
$(warning $(NAME): LOCAL_INSTALL_PATH=$(LOCAL_INSTALL_PATH))
$(error $(NAME): static-lib.mk invoked with args reserved for binary.mk)
endif

Expand Down
3 changes: 3 additions & 0 deletions target/armv7a.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ else
$(error Incorrect TARGET.)
endif

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

CFLAGS += -mcpu=$(cpu) -mtune=$(cpu) -mthumb -fomit-frame-pointer -mno-unaligned-access
CXXFLAGS := $(CFLAGS)

Expand Down
4 changes: 3 additions & 1 deletion target/armv7m.mk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ else
$(error Incorrect TARGET: $(TARGET))
endif

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

LDFLAGS := -Wl,-z,max-page-size=0x10

Expand All @@ -48,7 +50,7 @@ ifeq ($(KERNEL), 1)
LDFLAGS += -Tbss=20000000 -Tdata=20000000
STRIP := $(CROSS)strip
else
CFLAGS += -fpic -fpie -msingle-pic-base -mno-pic-data-is-text-relative
CFLAGS += $(TARGET_PIC_FLAG) $(TARGET_PIE_FLAG) -msingle-pic-base -mno-pic-data-is-text-relative
# output .rel.* sections to make ELF position-independent
LDFLAGS += -Wl,-q
STRIP := $(PREFIX_PROJECT)/phoenix-rtos-build/scripts/strip.py $(CROSS)strip --strip-unneeded -R .rel.text
Expand Down
3 changes: 3 additions & 0 deletions target/host.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ ARFLAGS = -r
LD := $(CROSS)gcc
LDFLAGS_PREFIX := -Wl,

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump
STRIP := $(CROSS)strip
Expand Down
6 changes: 6 additions & 0 deletions target/ia32.mk
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ LDFLAGS_PREFIX := -Wl,

LDFLAGS :=

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump

STRIP := $(CROSS)strip

VADDR_KERNEL_BASE=0xc0000000
VADDR_KERNEL_INIT=$(shell printf "0x%x" $$(($(VADDR_KERNEL_BASE) + 0x110000)))

LIBPHOENIX_PIC ?= y
LIBPHOENIX_SHARED ?= y
3 changes: 3 additions & 0 deletions target/riscv64.mk
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ LDFLAGS_PREFIX := -Wl,

LDFLAGS :=

TARGET_PIC_FLAG = -fpic
TARGET_PIE_FLAG = -fpie

OBJCOPY := $(CROSS)objcopy
OBJDUMP := $(CROSS)objdump

Expand Down
5 changes: 4 additions & 1 deletion target/sparcv8leon3.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ CFLAGS += -mcpu=leon3

LDFLAGS :=

TARGET_PIC_FLAG = -fPIC
TARGET_PIE_FLAG = -fPIE

ifeq ($(TARGET_SUBFAMILY), gr716)
VADDR_KERNEL_INIT := 31000000
CPPFLAGS := -DNOMMU
Expand All @@ -27,7 +30,7 @@ ifeq ($(TARGET_SUBFAMILY), gr716)
LDFLAGS += -Wl,-z,max-page-size=0x200 -Tbss=40001800 -Tdata=40001800 -Wl,--section-start=.rodata=40000000
STRIP := $(CROSS)strip
else
CFLAGS += -fPIC -fPIE -mno-pic-data-is-text-relative -mpic-register=g6
CFLAGS += $(TARGET_PIC_FLAG) $(TARGET_PIE_FLAG) -mno-pic-data-is-text-relative -mpic-register=g6
LDFLAGS += -Wl,-q
STRIP := $(CROSS)strip --strip-unneeded -R .rela.text
endif
Expand Down
2 changes: 1 addition & 1 deletion toolchain/build-toolchain.sh
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ download;
build_binutils;
build_gcc_stage1;

build_libc;
build_gcc_stage2;
build_libc;
build_libstdcpp;

strip_binaries;
Expand Down
3 changes: 1 addition & 2 deletions toolchain/gcc-9.5.0-10-fix-libc-spec.patch
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
} while (0)

-#define STD_LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
+/* use gcc default for libc (re-define as it might have been overridden by previous imports) */
+#undef LIB_SPEC
+#define LIB_SPEC "%{!shared:%{g*:-lg} %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"
+#define LIB_SPEC "%{!shared: %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"

/* This will prevent selecting 'unsigned long int' instead of 'unsigned int' as 'uint32_t' in stdint-newlib.h. */
#undef STDINT_LONG32
23 changes: 23 additions & 0 deletions toolchain/gcc-9.5.0-11-fix-link-spec.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
diff --git a/gcc/config/phoenix.h b/gcc/config/phoenix.h
index 787af6e31..70ef165b4 100644
--- a/gcc/config/phoenix.h
+++ b/gcc/config/phoenix.h
@@ -30,6 +30,18 @@ along with GCC; see the file COPYING3. If not see
#undef LIB_SPEC
#define LIB_SPEC "%{!shared: %{!p:%{!pg:-lc}}%{p:-lc_p}%{pg:-lc_p}}"

+#undef LINK_SPEC
+#define LINK_SPEC \
+ "%{assert*} %{R*} %{rpath*} \
+ %{shared:-shared} \
+ %{symbolic:-Bsymbolic} \
+ %{!shared: \
+ -dc -dp \
+ %{!static: \
+ %{rdynamic:-export-dynamic} \
+ -dynamic-linker /usr/libexec/ld.elf_so} \
+ %{static:-static}}"
+
/* This will prevent selecting 'unsigned long int' instead of 'unsigned int' as 'uint32_t' in stdint-newlib.h. */
#undef STDINT_LONG32
#define STDINT_LONG32 0

0 comments on commit 55e17e4

Please sign in to comment.