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
28ALL_ORIGINAL_DYNAMIC_BINARIES += $(linked_module)
29
30# Because TARGET_SYMBOL_FILTER_FILE depends on ALL_ORIGINAL_DYNAMIC_BINARIES,
31# the linked_module rules won't necessarily inherit the PRIVATE_
32# variables from LOCAL_BUILT_MODULE.  This tells binary.make to explicitly
33# define the PRIVATE_ variables for linked_module as well as for
34# LOCAL_BUILT_MODULE.
35LOCAL_INTERMEDIATE_TARGETS := $(linked_module)
36
37###################################
38include $(BUILD_SYSTEM)/use_lld_setup.mk
39include $(BUILD_SYSTEM)/binary.mk
40###################################
41
42ifdef LOCAL_INJECT_BSSL_HASH
43inject_module := $(intermediates)/INJECT_BSSL_HASH/$(notdir $(my_installed_module_stem))
44LOCAL_INTERMEDIATE_TARGETS += $(inject_module)
45$(inject_module): $(SOONG_HOST_OUT)/bin/bssl_inject_hash
46$(inject_module): $(linked_module)
47	@echo "target inject BSSL hash: $(PRIVATE_MODULE) ($@)"
48	$(SOONG_HOST_OUT)/bin/bssl_inject_hash -in-object $< -o $@
49else
50inject_module := $(linked_module)
51endif
52
53###########################################################
54## Store a copy with symbols for symbolic debugging
55###########################################################
56ifeq ($(LOCAL_UNSTRIPPED_PATH),)
57my_unstripped_path := $(TARGET_OUT_UNSTRIPPED)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
58else
59my_unstripped_path := $(LOCAL_UNSTRIPPED_PATH)
60endif
61symbolic_input := $(inject_module)
62symbolic_output := $(my_unstripped_path)/$(my_installed_module_stem)
63$(symbolic_output) : $(symbolic_input)
64	@echo "target Symbolic: $(PRIVATE_MODULE) ($@)"
65	$(copy-file-to-target)
66
67###########################################################
68## Store breakpad symbols
69###########################################################
70
71ifeq ($(BREAKPAD_GENERATE_SYMBOLS),true)
72my_breakpad_path := $(TARGET_OUT_BREAKPAD)/$(patsubst $(PRODUCT_OUT)/%,%,$(my_module_path))
73breakpad_input := $(inject_module)
74breakpad_output := $(my_breakpad_path)/$(my_installed_module_stem).sym
75$(breakpad_output) : $(breakpad_input) | $(BREAKPAD_DUMP_SYMS) $(PRIVATE_READELF)
76	@echo "target breakpad: $(PRIVATE_MODULE) ($@)"
77	@mkdir -p $(dir $@)
78	$(hide) if $(PRIVATE_READELF) -S $< > /dev/null 2>&1 ; then \
79	  $(BREAKPAD_DUMP_SYMS) -c $< > $@ ; \
80	else \
81	  echo "skipped for non-elf file."; \
82	  touch $@; \
83	fi
84$(LOCAL_BUILT_MODULE) : $(breakpad_output)
85endif
86
87###########################################################
88## Strip
89###########################################################
90strip_input := $(inject_module)
91strip_output := $(LOCAL_BUILT_MODULE)
92
93# Use an order-only dependency to ensure the unstripped file in the symbols
94# directory is copied when the module is built, but does not force the
95# module to be rebuilt when the symbols directory is cleaned by installclean.
96$(strip_output): | $(symbolic_output)
97
98my_strip_module := $(firstword \
99  $(LOCAL_STRIP_MODULE_$($(my_prefix)$(LOCAL_2ND_ARCH_VAR_PREFIX)ARCH)) \
100  $(LOCAL_STRIP_MODULE))
101ifeq ($(my_strip_module),)
102  my_strip_module := mini-debug-info
103endif
104
105ifeq ($(my_strip_module),false)
106  my_strip_module :=
107endif
108
109my_strip_args :=
110ifeq ($(my_strip_module),mini-debug-info)
111  my_strip_args += --keep-mini-debug-info
112else ifeq ($(my_strip_module),keep_symbols)
113  my_strip_args += --keep-symbols
114endif
115
116ifeq (,$(filter no_debuglink mini-debug-info,$(my_strip_module)))
117  ifneq ($(TARGET_BUILD_VARIANT),user)
118    my_strip_args += --add-gnu-debuglink
119  endif
120endif
121
122ifeq ($($(my_prefix)OS),darwin)
123  # llvm-strip does not support Darwin Mach-O yet.
124  my_strip_args += --use-gnu-strip
125endif
126
127valid_strip := mini-debug-info keep_symbols true no_debuglink
128ifneq (,$(filter-out $(valid_strip),$(my_strip_module)))
129  $(call pretty-error,Invalid strip value $(my_strip_module), only one of $(valid_strip) allowed)
130endif
131
132ifneq (,$(my_strip_module))
133  $(strip_output): PRIVATE_STRIP_ARGS := $(my_strip_args)
134  $(strip_output): PRIVATE_TOOLS_PREFIX := $($(LOCAL_2ND_ARCH_VAR_PREFIX)$(my_prefix)TOOLS_PREFIX)
135  $(strip_output): $(strip_input) $(SOONG_STRIP_PATH) $(XZ)
136	@echo "$($(PRIVATE_PREFIX)DISPLAY) Strip: $(PRIVATE_MODULE) ($@)"
137	CLANG_BIN=$(LLVM_PREBUILTS_PATH) \
138	CROSS_COMPILE=$(PRIVATE_TOOLS_PREFIX) \
139	XZ=$(XZ) \
140	CREATE_MINIDEBUGINFO=${CREATE_MINIDEBUGINFO} \
141	$(SOONG_STRIP_PATH) -i $< -o $@ -d $@.strip.d $(PRIVATE_STRIP_ARGS)
142  ifneq ($(HOST_OS),darwin)
143    $(strip_output): $(CREATE_MINIDEBUGINFO)
144  endif
145  $(call include-depfile,$(strip_output).strip.d,$(strip_output))
146else
147  # Don't strip the binary, just copy it.  We can't skip this step
148  # because a copy of the binary must appear at LOCAL_BUILT_MODULE.
149  $(strip_output): $(strip_input)
150	@echo "target Unstripped: $(PRIVATE_MODULE) ($@)"
151	$(copy-file-to-target)
152endif # my_strip_module
153
154$(cleantarget): PRIVATE_CLEAN_FILES += \
155    $(linked_module) \
156    $(inject_module) \
157    $(breakpad_output) \
158    $(symbolic_output) \
159    $(strip_output)
160