Skip to content

Commit d49c885

Browse files
badochovHubert Badocha
authored andcommitted
Makefile: generate shared library
JIRA: RTOS-664
1 parent b84b1b0 commit d49c885

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

Makefile

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,19 @@ include ../phoenix-rtos-build/Makefile.common
1515
SYSROOT := $(shell $(CC) $(CFLAGS) -print-sysroot)
1616
MULTILIB_DIR := $(shell $(CC) $(CFLAGS) -print-multi-directory)
1717
LIBC_INSTALL_DIR := $(SYSROOT)/lib/$(MULTILIB_DIR)
18-
LIBC_INSTALL_NAMES := libc.a libm.a libg.a libpthread.a libubsan.a
18+
LIBC_INSTALL_NAMES_A := libc.a libm.a libg.a libpthread.a libubsan.a
1919
HEADERS_INSTALL_DIR := $(SYSROOT)/usr/include
20-
LIBNAME := libphoenix.a
2120

2221
ifeq (,$(filter-out /,$(SYSROOT)))
2322
$(error SYSROOT is not supported by the toolchain. Use cross-toolchain to compile.)
2423
endif
2524

2625
CFLAGS += -Iinclude -fno-builtin-malloc
2726

28-
ifeq ($(LIBPHOENIX_PIC), y)
29-
CFLAGS += $(TARGET_PIC_FLAG)
30-
endif
31-
3227
OBJS :=
3328
# crt0.o should have all necessary initialization + call to main()
3429
CRT0_OBJS := $(PREFIX_O)crt0-common.o
3530

36-
LIB_TARGETS := $(PREFIX_A)libphoenix.a $(PREFIX_A)crt0.o
37-
38-
all: $(LIB_TARGETS)
3931

4032
ifneq (,$(findstring arm,$(TARGET_SUFF)))
4133
include arch/arm/Makefile
@@ -67,7 +59,20 @@ include unistd/Makefile
6759
include wchar/Makefile
6860
include ubsan/Makefile
6961

70-
#include test/Makefile
62+
63+
LIB_TARGETS := $(PREFIX_A)libphoenix.a $(PREFIX_A)crt0.o
64+
INSTALL_TARGETS := install-headers install-libs
65+
66+
ifeq ($(LIBPHOENIX_PIC), y)
67+
CFLAGS += $(TARGET_PIC_FLAG)
68+
ifeq ($(LIBPHOENIX_SHARED), y)
69+
include shared.mk
70+
endif
71+
endif
72+
73+
74+
all: $(LIB_TARGETS)
75+
7176

7277
$(PREFIX_A)libphoenix.a: $(OBJS)
7378
$(ARCH)
@@ -78,7 +83,8 @@ $(PREFIX_A)crt0.o: $(CRT0_OBJS)
7883

7984
SRCHEADERS := $(shell find include -name \*.h)
8085

81-
install: install-headers install-libs
86+
87+
install: $(INSTALL_TARGETS)
8288

8389
install-headers: $(SRCHEADERS)
8490
@echo INSTALL "$(HEADERS_INSTALL_DIR)/*"; \
@@ -87,16 +93,16 @@ install-headers: $(SRCHEADERS)
8793

8894
# TODO: remove `rm crt0.o` when we will be sure it's not a symlink to libphoenix.a anymore
8995
install-libs: $(LIB_TARGETS)
90-
@echo INSTALL "$(LIBC_INSTALL_DIR)/*"; \
96+
$(SIL)echo INSTALL "$(LIBC_INSTALL_DIR)/*"; \
9197
mkdir -p "$(LIBC_INSTALL_DIR)"; \
9298
rm -rf "$(LIBC_INSTALL_DIR)/crt0.o"; \
9399
cp -a $^ "$(LIBC_INSTALL_DIR)"; \
94100
(cd $(LIBC_INSTALL_DIR) && \
95-
for lib in $(LIBC_INSTALL_NAMES); do \
101+
for lib in $(LIBC_INSTALL_NAMES_A); do \
96102
if [ ! -e "$$lib" ]; then \
97103
ln -sf "libphoenix.a" "$$lib"; \
98104
fi \
99-
done)
105+
done)
100106

101107
.PHONY: clean install install-headers install-libs
102108
clean:

shared.mk

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#
2+
# Makefile for compiling libphoenix as shared lib
3+
#
4+
# Copyright 2024 Phoenix Systems
5+
#
6+
# %LICENSE%
7+
#
8+
9+
# FIXME: In shared versions each library should only have needed parts of libphoenix.
10+
LIBC_INSTALL_NAMES_SO := libc.so libm.so libpthread.so libubsan.so
11+
12+
LIB_TARGETS += $(PREFIX_SO)libphoenix.so
13+
INSTALL_TARGETS += install-shared
14+
15+
# Tell dynamic linker to init libphoenix first.
16+
LIBPHOENIX_SO_FLAGS := $(LDFLAGS_PREFIX)-z,initfirst
17+
# Common shared lib flags
18+
SHARED_LIB_LD_FLAGS := $(TARGET_PIC_FLAG) -shared -nolibc -nostartfiles $(LDFLAGS_PREFIX)--warn-shared-textrel
19+
20+
$(PREFIX_SO)libphoenix.so: LDFLAGS:=$(LDFLAGS) $(SHARED_LIB_LD_FLAGS)
21+
22+
$(PREFIX_SO)libphoenix.so: $(OBJS)
23+
$(LINK)
24+
25+
26+
LOCAL_INSTALL_PATH := $(or $(LOCAL_INSTALL_PATH),$(DEFAULT_INSTALL_PATH_SO))
27+
28+
install-shared: $(PREFIX_SO)libphoenix.so $(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/libphoenix.so install-shared-libs
29+
30+
$(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH)/libphoenix.so: $(PREFIX_SO)libphoenix.so
31+
$(INSTALL_FS)
32+
$(SIL)(cd $(PREFIX_ROOTFS)$(LOCAL_INSTALL_PATH) && \
33+
for lib in $(LIBC_INSTALL_NAMES_SO); do \
34+
if [ ! -e "$$lib" ]; then \
35+
cp "libphoenix.so" "$$lib"; \
36+
fi \
37+
done \
38+
)
39+
40+
install-shared-libs: $(PREFIX_SO)libphoenix.so install-libs
41+
$(SIL)(cd $(LIBC_INSTALL_DIR) && \
42+
for lib in $(LIBC_INSTALL_NAMES_SO); do \
43+
if [ ! -e "$$lib" ]; then \
44+
ln -sf "libphoenix.so" "$$lib"; \
45+
fi \
46+
done \
47+
)

0 commit comments

Comments
 (0)