1###########################################################
2## Standard rules for building any target-side binaries
3## with dynamic linkage (dynamic libraries or executables
4## that link with dynamic libraries)
5##
6## Files including this file must define a rule to build
7## the target $(linked_module).
8###########################################################
9
10# This constraint means that we can hard-code any $(TARGET_*) variables.
11ifdef LOCAL_IS_HOST_MODULE
12$(error This file should not be used to build host binaries.  Included by (or near) $(lastword $(filter-out config/%,$(MAKEFILE_LIST))))
13endif
14
15# The name of the target file, without any path prepended.
16# This duplicates logic from base_rules.mk because we need to
17# know its results before base_rules.mk is included.
18include $(BUILD_SYSTEM)/configure_module_stem.mk
19
20intermediates := $(call local-intermediates-dir,,$(LOCAL_2ND_ARCH_VAR_PREFIX))
21
22# Define the target that is the unmodified output of the linker.
23# The basename of this target must be the same as the final output
24# binary name, because it's used to set the "soname" in the binary.
25# The includer of this file will define a rule to build this target.
26linked_module := $(intermediates)/LINKED/$(notdir $(my_installed_module_stem))
27
28# This tells binary.make to explicitly define the PRIVATE_ variables for
29# linked_module as well as for LOCAL_BUILT_MODULE.
30LOCAL_INTERMEDIATE_TARGETS := $(linked_module)
31
32###################################
33include $(BUILD_SYSTEM)/use_lld_setup.mk
34include $(BUILD_SYSTEM)/binary.mk
35###################################
36
37ifdef LOCAL_INJECT_BSSL_HASH
38inject_module := $(intermediates)/INJECT_BSSL_HASH/$(notdir $(my_installed_module_stem))
39LOCAL_INTERMEDIATE_TARGETS += $(inject_module)
40$(inject_module): $(SOONG_HOST_OUT)/bin/bssl_inject_hash
41$(inject_module): $(linked_module)
42	@echo "target inject BSSL hash: $(PRIVATE_MODULE) ($@)"
43	$(SOONG_HOST_OUT)/bin/bssl_inject_hash -in-object $< -o $@
44else
45inject_module := $(linked_module)
46endif
47
48###########################################################
49## Store a copy with symbols for symbolic debugging
50###########################################################
51ifeq ($(LOCAL_UNSTRIPPED_PATH),)
52my_unstripped_path := $(TARGET_OUT_UNSTRIPPED)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
53else
54my_unstripped_path := $(LOCAL_UNSTRIPPED_PATH)
55endif
56symbolic_input := $(inject_module)
57symbolic_output := $(my_unstripped_path)/$(my_installed_module_stem)
58$(eval $(call copy-unstripped-elf-file-with-mapping,$(symbolic_input),$(symbolic_output)))
59
60###########################################################
61## Store breakpad symbols
62###########################################################
63
64ifeq ($(BREAKPAD_GENERATE_SYMBOLS),true)
65my_breakpad_path := $(TARGET_OUT_BREAKPAD)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
66breakpad_input := $(inject_module)
67breakpad_output := $(my_breakpad_path)/$(my_installed_module_stem).sym
68$(breakpad_output) : $(breakpad_input) | $(BREAKPAD_DUMP_SYMS) $(PRIVATE_READELF)
69	@echo "target breakpad: $(PRIVATE_MODULE) ($@)"
70	@mkdir -p $(dir $@)
71	$(hide) if $(PRIVATE_READELF) -S $< > /dev/null 2>&1 ; then \
72	  $(BREAKPAD_DUMP_SYMS) -c $< > $@ ; \
73	else \
74	  echo "skipped for non-elf file."; \
75	  touch $@; \
76	fi
77$(LOCAL_BUILT_MODULE) : $(breakpad_output)
78endif
79
80###########################################################
81## Strip
82###########################################################
83strip_input := $(inject_module)
84strip_output := $(LOCAL_BUILT_MODULE)
85
86# Use an order-only dependency to ensure the unstripped file in the symbols
87# directory is copied when the module is built, but does not force the
88# module to be rebuilt when the symbols directory is cleaned by installclean.
89$(strip_output): | $(symbolic_output)
90
91my_strip_module := $(firstword \
92  $(LOCAL_STRIP_MODULE_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) \
93  $(LOCAL_STRIP_MODULE))
94ifeq ($(my_strip_module),)
95  my_strip_module := mini-debug-info
96endif
97
98ifeq ($(my_strip_module),false)
99  my_strip_module :=
100endif
101
102my_strip_args :=
103ifeq ($(my_strip_module),mini-debug-info)
104  my_strip_args += --keep-mini-debug-info
105else ifeq ($(my_strip_module),keep_symbols)
106  my_strip_args += --keep-symbols
107endif
108
109ifeq (,$(filter no_debuglink mini-debug-info,$(my_strip_module)))
110  ifneq ($(TARGET_BUILD_VARIANT),user)
111    my_strip_args += --add-gnu-debuglink
112  endif
113endif
114
115ifeq ($($(my_prefix)OS),darwin)
116  # llvm-strip does not support Darwin Mach-O yet.
117  my_strip_args += --use-gnu-strip
118endif
119
120valid_strip := mini-debug-info keep_symbols true no_debuglink
121ifneq (,$(filter-out $(valid_strip),$(my_strip_module)))
122  $(call pretty-error,Invalid strip value $(my_strip_module), only one of $(valid_strip) allowed)
123endif
124
125ifneq (,$(my_strip_module))
126  $(strip_output): PRIVATE_STRIP_ARGS := $(my_strip_args)
127  $(strip_output): PRIVATE_TOOLS_PREFIX := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)TOOLS_PREFIX)
128  $(strip_output): $(strip_input) $(SOONG_STRIP_PATH) $(XZ)
129	@echo "$($(PRIVATE_PREFIX)DISPLAY) Strip: $(PRIVATE_MODULE) ($@)"
130	CLANG_BIN=$(LLVM_PREBUILTS_PATH) \
131	CROSS_COMPILE=$(PRIVATE_TOOLS_PREFIX) \
132	XZ=$(XZ) \
133	CREATE_MINIDEBUGINFO=${CREATE_MINIDEBUGINFO} \
134	$(SOONG_STRIP_PATH) -i $< -o $@ -d $@.strip.d $(PRIVATE_STRIP_ARGS)
135  ifneq ($(HOST_OS),darwin)
136    $(strip_output): $(CREATE_MINIDEBUGINFO)
137  endif
138  $(call include-depfile,$(strip_output).strip.d,$(strip_output))
139else
140  # Don't strip the binary, just copy it.  We can't skip this step
141  # because a copy of the binary must appear at LOCAL_BUILT_MODULE.
142  $(strip_output): $(strip_input)
143	@echo "target Unstripped: $(PRIVATE_MODULE) ($@)"
144	$(copy-file-to-target)
145endif # my_strip_module
146
147$(cleantarget): PRIVATE_CLEAN_FILES += \
148    $(linked_module) \
149    $(inject_module) \
150    $(breakpad_output) \
151    $(symbolic_output) \
152    $(strip_output)
153