1# Input
2#
3# libname	the name of the lib
4# libdir	directory of lib which also is used as input to
5#		mk/subdir.mk
6# conf-file     [optional] if set, all objects will depend on $(conf-file)
7# [if CFG_ULIBS_SHARED==y]
8#   libuuid	the UUID of the shared lib
9#   libl	other libraries this library depends on; used to generate the
10#               proper link arguments (-Lxxx -lyyy) and to add dependencies
11#               on the needed .so files
12# [endif]
13#
14# Output
15#
16# updated cleanfiles and
17# updated libfiles, libdirs, libnames and libdeps
18
19
20subdirs = $(libdir)
21include mk/subdir.mk
22ifeq ($(filter $(sm), core ldelf),) # TA
23ifeq ($(CFG_ULIBS_MCOUNT),y)
24cflags-lib$(libname)-$(sm) += -pg
25endif
26endif
27include mk/compile.mk
28
29lib-libfile	:= $(out-dir)/$(base-prefix)$(libdir)/lib$(libname).a
30ifeq ($(CFG_ULIBS_SHARED),y)
31lib-shlibfile	:= $(out-dir)/$(base-prefix)$(libdir)/lib$(libname).so
32lib-shlibstrippedfile := $(out-dir)/$(base-prefix)$(libdir)/lib$(libname).stripped.so
33lib-shlibtafile	:= $(out-dir)/$(base-prefix)$(libdir)/$(libuuid).ta
34lib-libuuidln	:= $(out-dir)/$(base-prefix)$(libdir)/$(libuuid).elf
35lib-shlibfile-$(libname)-$(sm) := $(lib-shlibfile)
36lib-libdir-$(libname)-$(sm) := $(out-dir)/$(base-prefix)$(libdir)
37lib-needed-so-files := $(foreach l,$(libl),$(lib-shlibfile-$(l)-$(sm)))
38lib-Ll-args := $(foreach l,$(libl),-L$(lib-libdir-$(l)-$(sm)) -l$(l))
39endif
40cleanfiles	:= $(lib-libfile) $(lib-shlibfile) $(lib-shlibstrippedfile) $(lib-shlibtafile) $(lib-libuuidln) $(cleanfiles)
41libfiles	:= $(lib-libfile) $(lib-shlibfile) $(lib-shlibstrippedfile) $(lib-shlibtafile) $(lib-libuuidln) $(libfiles)
42libdirs 	:= $(out-dir)/$(base-prefix)$(libdir) $(libdirs)
43libnames	:= $(libname) $(libnames)
44libdeps		:= $(lib-libfile) $(libdeps)
45
46SIGN = scripts/sign_encrypt.py
47TA_SIGN_KEY ?= keys/default_ta.pem
48
49define process-lib
50ifeq ($(lib-use-ld), y)
51$(lib-libfile): $(objs)
52	@echo '  LD      $$@'
53	@mkdir -p $$(dir $$@)
54	$$(q)$$(LD$(sm)) $(lib-ldflags) -o $$@ $$^
55else
56$(lib-libfile): $(objs)
57	@$(cmd-echo-silent) '  AR      $$@'
58	@mkdir -p $$(dir $$@)
59	$$(q)rm -f $$@ && $$(AR$(sm)) rcs $$@ $$^
60endif
61ifeq ($(CFG_ULIBS_SHARED),y)
62ifeq ($(sm)-$(CFG_TA_BTI),ta_arm64-y)
63lib-ldflags$(libuuid) += $$(call ld-option,-z force-bti) --fatal-warnings
64endif
65$(lib-shlibfile): $(objs) $(lib-needed-so-files)
66	@$(cmd-echo-silent) '  LD      $$@'
67	@mkdir -p $$(dir $$@)
68	$$(q)$$(LD$(sm)) $(lib-ldflags) -shared -z max-page-size=4096 \
69		$(call ld-option,-z separate-loadable-segments) \
70		$$(lib-ldflags$(libuuid)) \
71		--soname=$(libuuid) -o $$@ $$(filter-out %.so,$$^) $(lib-Ll-args)
72
73$(lib-shlibstrippedfile): $(lib-shlibfile)
74	@$(cmd-echo-silent) '  OBJCOPY $$@'
75	$$(q)$$(OBJCOPY$(sm)) --strip-unneeded $$< $$@
76
77$(lib-shlibtafile): $(lib-shlibstrippedfile) $(TA_SIGN_KEY)
78	@$(cmd-echo-silent) '  SIGN    $$@'
79	$$(q)$$(PYTHON3) $$(SIGN) --key $(TA_SIGN_KEY) --uuid $(libuuid) --in $$< --out $$@
80
81$(lib-libuuidln): $(lib-shlibfile)
82	@$(cmd-echo-silent) '  LN      $$@'
83	$$(q)ln -sf lib$(libname).so $$@
84endif
85endef #process-lib
86
87$(eval $(call process-lib))
88
89$(objs): $(conf-file)
90
91# Clean residues from processing
92objs		:=
93libname		:=
94libuuid		:=
95lib-use-ld	:=
96lib-shlibfile	:=
97lib-shlibstrippedfile :=
98lib-shlibtafile	:=
99lib-libuuidln	:=
100lib-needed-so-files :=
101libl :=
102