binary.mk revision 88b607994a148f4af5bffee163e39ce8296750c6
1###########################################################
2## Standard rules for building binary object files from
3## asm/c/cpp/yacc/lex source files.
4##
5## The list of object files is exported in $(all_objects).
6###########################################################
7
8#######################################
9include $(BUILD_SYSTEM)/base_rules.mk
10#######################################
11
12###########################################################
13## Define PRIVATE_ variables used by multiple module types
14###########################################################
15$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_NO_DEFAULT_COMPILER_FLAGS := \
16	$(strip $(LOCAL_NO_DEFAULT_COMPILER_FLAGS))
17
18ifeq ($(strip $(LOCAL_CC)),)
19  LOCAL_CC := $($(my_prefix)CC)
20endif
21$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CC := $(LOCAL_CC)
22
23ifeq ($(strip $(LOCAL_CXX)),)
24  LOCAL_CXX := $($(my_prefix)CXX)
25endif
26$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CXX := $(LOCAL_CXX)
27
28# TODO: support a mix of standard extensions so that this isn't necessary
29LOCAL_CPP_EXTENSION := $(strip $(LOCAL_CPP_EXTENSION))
30ifeq ($(LOCAL_CPP_EXTENSION),)
31  LOCAL_CPP_EXTENSION := .cpp
32endif
33$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CPP_EXTENSION := $(LOCAL_CPP_EXTENSION)
34
35# Certain modules like libdl have to have symbols resolved at runtime and blow
36# up if --no-undefined is passed to the linker.
37ifeq ($(strip $(LOCAL_NO_DEFAULT_COMPILER_FLAGS)),)
38ifeq ($(strip $(LOCAL_ALLOW_UNDEFINED_SYMBOLS)),)
39  LOCAL_LDFLAGS := $(LOCAL_LDFLAGS) $($(my_prefix)NO_UNDEFINED_LDFLAGS)
40endif
41endif
42
43###########################################################
44## Define arm-vs-thumb-mode flags.
45###########################################################
46LOCAL_ARM_MODE := $(strip $(LOCAL_ARM_MODE))
47arm_objects_mode := $(if $(LOCAL_ARM_MODE),$(LOCAL_ARM_MODE),arm)
48normal_objects_mode := $(if $(LOCAL_ARM_MODE),$(LOCAL_ARM_MODE),thumb)
49
50# Read the values from something like TARGET_arm_release_CFLAGS or
51# TARGET_thumb_debug_CFLAGS.  HOST_(arm|thumb)_(release|debug)_CFLAGS
52# values aren't actually used (although they are usually empty).
53arm_objects_cflags := $($(my_prefix)$(arm_objects_mode)_$($(my_prefix)BUILD_TYPE)_CFLAGS)
54normal_objects_cflags := $($(my_prefix)$(normal_objects_mode)_$($(my_prefix)BUILD_TYPE)_CFLAGS)
55
56###########################################################
57## Define per-module debugging flags.  Users can turn on
58## debugging for a particular module by setting DEBUG_MODULE_ModuleName
59## to a non-empty value in their environment or buildspec.mk,
60## and setting HOST_/TARGET_CUSTOM_DEBUG_CFLAGS to the
61## debug flags that they want to use.
62###########################################################
63ifdef DEBUG_MODULE_$(strip $(LOCAL_MODULE))
64  debug_cflags := $($(my_prefix)CUSTOM_DEBUG_CFLAGS)
65else
66  debug_cflags :=
67endif
68
69###########################################################
70## Stuff source generated from one-off tools
71###########################################################
72$(LOCAL_GENERATED_SOURCES): PRIVATE_MODULE := $(LOCAL_MODULE)
73
74ALL_GENERATED_SOURCES += $(LOCAL_GENERATED_SOURCES)
75
76
77###########################################################
78## YACC: Compile .y files to .cpp and the to .o.
79###########################################################
80
81yacc_sources := $(filter %.y,$(LOCAL_SRC_FILES))
82yacc_cpps := $(addprefix \
83	$(intermediates)/,$(yacc_sources:.y=$(LOCAL_CPP_EXTENSION)))
84yacc_headers := $(yacc_cpps:$(LOCAL_CPP_EXTENSION)=.h)
85yacc_objects := $(yacc_cpps:$(LOCAL_CPP_EXTENSION)=.o)
86
87ifneq ($(strip $(yacc_cpps)),)
88$(yacc_cpps): $(intermediates)/%$(LOCAL_CPP_EXTENSION): \
89		$(TOPDIR)$(LOCAL_PATH)/%.y \
90		$(lex_cpps) $(PRIVATE_ADDITIONAL_DEPENDENCIES)
91	$(call transform-y-to-cpp,$(PRIVATE_CPP_EXTENSION))
92$(yacc_headers): $(intermediates)/%.h: $(intermediates)/%$(LOCAL_CPP_EXTENSION)
93
94$(yacc_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
95$(yacc_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
96$(yacc_objects): $(intermediates)/%.o: $(intermediates)/%$(LOCAL_CPP_EXTENSION)
97	$(transform-$(PRIVATE_HOST)cpp-to-o)
98endif
99
100###########################################################
101## LEX: Compile .l files to .cpp and then to .o.
102###########################################################
103
104lex_sources := $(filter %.l,$(LOCAL_SRC_FILES))
105lex_cpps := $(addprefix \
106	$(intermediates)/,$(lex_sources:.l=$(LOCAL_CPP_EXTENSION)))
107lex_objects := $(lex_cpps:$(LOCAL_CPP_EXTENSION)=.o)
108
109ifneq ($(strip $(lex_cpps)),)
110$(lex_cpps): $(intermediates)/%$(LOCAL_CPP_EXTENSION): \
111		$(TOPDIR)$(LOCAL_PATH)/%.l
112	$(transform-l-to-cpp)
113
114$(lex_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
115$(lex_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
116$(lex_objects): $(intermediates)/%.o: \
117		$(intermediates)/%$(LOCAL_CPP_EXTENSION) \
118		$(PRIVATE_ADDITIONAL_DEPENDENCIES) \
119		$(yacc_headers)
120	$(transform-$(PRIVATE_HOST)cpp-to-o)
121endif
122
123###########################################################
124## C++: Compile .cpp files to .o.
125###########################################################
126
127# we also do this on host modules and sim builds, even though
128# it's not really arm, because there are files that are shared.
129cpp_arm_sources    := $(patsubst %$(LOCAL_CPP_EXTENSION).arm,%$(LOCAL_CPP_EXTENSION),$(filter %$(LOCAL_CPP_EXTENSION).arm,$(LOCAL_SRC_FILES)))
130cpp_arm_objects    := $(addprefix $(intermediates)/,$(cpp_arm_sources:$(LOCAL_CPP_EXTENSION)=.o))
131
132cpp_normal_sources := $(filter %$(LOCAL_CPP_EXTENSION),$(LOCAL_SRC_FILES))
133cpp_normal_objects := $(addprefix $(intermediates)/,$(cpp_normal_sources:$(LOCAL_CPP_EXTENSION)=.o))
134
135$(cpp_arm_objects):    PRIVATE_ARM_MODE := $(arm_objects_mode)
136$(cpp_arm_objects):    PRIVATE_ARM_CFLAGS := $(arm_objects_cflags)
137$(cpp_normal_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
138$(cpp_normal_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
139
140cpp_objects        := $(cpp_arm_objects) $(cpp_normal_objects)
141
142ifneq ($(strip $(cpp_objects)),)
143$(cpp_objects): $(intermediates)/%.o: \
144		$(TOPDIR)$(LOCAL_PATH)/%$(LOCAL_CPP_EXTENSION) \
145		$(yacc_cpps) $(PRIVATE_ADDITIONAL_DEPENDENCIES)
146	$(transform-$(PRIVATE_HOST)cpp-to-o)
147-include $(cpp_objects:%.o=%.P)
148endif
149
150###########################################################
151## C++: Compile generated .cpp files to .o.
152###########################################################
153
154gen_cpp_sources := $(filter %$(LOCAL_CPP_EXTENSION),$(LOCAL_GENERATED_SOURCES))
155gen_cpp_objects := $(gen_cpp_sources:%$(LOCAL_CPP_EXTENSION)=%.o)
156
157ifneq ($(strip $(gen_cpp_objects)),)
158# Compile all generated files as thumb.
159# TODO: support compiling certain generated files as arm.
160$(gen_cpp_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
161$(gen_cpp_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
162$(gen_cpp_objects): $(intermediates)/%.o: $(intermediates)/%$(LOCAL_CPP_EXTENSION) $(yacc_cpps) $(PRIVATE_ADDITIONAL_DEPENDENCIES)
163	$(transform-$(PRIVATE_HOST)cpp-to-o)
164-include $(gen_cpp_objects:%.o=%.P)
165endif
166
167###########################################################
168## S: Compile generated .S and .s files to .o.
169###########################################################
170
171gen_S_sources := $(filter %.S,$(LOCAL_GENERATED_SOURCES))
172gen_S_objects := $(gen_S_sources:%.S=%.o)
173
174ifneq ($(strip $(gen_S_sources)),)
175$(gen_S_objects): $(intermediates)/%.o: $(intermediates)/%.S $(PRIVATE_ADDITIONAL_DEPENDENCIES)
176	$(transform-$(PRIVATE_HOST)s-to-o)
177-include $(gen_S_objects:%.o=%.P)
178endif
179
180gen_s_sources := $(filter %.s,$(LOCAL_GENERATED_SOURCES))
181gen_s_objects := $(gen_s_sources:%.s=%.o)
182
183ifneq ($(strip $(gen_s_objects)),)
184$(gen_s_objects): $(intermediates)/%.o: $(intermediates)/%.s $(PRIVATE_ADDITIONAL_DEPENDENCIES)
185	$(transform-$(PRIVATE_HOST)s-to-o-no-deps)
186-include $(gen_s_objects:%.o=%.P)
187endif
188
189gen_asm_objects := $(gen_S_objects) $(gen_s_objects)
190
191###########################################################
192## C: Compile .c files to .o.
193###########################################################
194
195c_arm_sources    := $(patsubst %.c.arm,%.c,$(filter %.c.arm,$(LOCAL_SRC_FILES)))
196c_arm_objects    := $(addprefix $(intermediates)/,$(c_arm_sources:.c=.o))
197
198c_normal_sources := $(filter %.c,$(LOCAL_SRC_FILES))
199c_normal_objects := $(addprefix $(intermediates)/,$(c_normal_sources:.c=.o))
200
201$(c_arm_objects):    PRIVATE_ARM_MODE := $(arm_objects_mode)
202$(c_arm_objects):    PRIVATE_ARM_CFLAGS := $(arm_objects_cflags)
203$(c_normal_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
204$(c_normal_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
205
206c_objects        := $(c_arm_objects) $(c_normal_objects)
207
208ifneq ($(strip $(c_objects)),)
209$(c_objects): $(intermediates)/%.o: $(TOPDIR)$(LOCAL_PATH)/%.c $(yacc_cpps) $(PRIVATE_ADDITIONAL_DEPENDENCIES)
210	$(transform-$(PRIVATE_HOST)c-to-o)
211-include $(c_objects:%.o=%.P)
212endif
213
214###########################################################
215## AS: Compile .S files to .o.
216###########################################################
217
218asm_sources_S := $(filter %.S,$(LOCAL_SRC_FILES))
219asm_objects_S := $(addprefix $(intermediates)/,$(asm_sources_S:.S=.o))
220
221ifneq ($(strip $(asm_objects_S)),)
222$(asm_objects_S): $(intermediates)/%.o: $(TOPDIR)$(LOCAL_PATH)/%.S $(PRIVATE_ADDITIONAL_DEPENDENCIES)
223	$(transform-$(PRIVATE_HOST)s-to-o)
224-include $(asm_objects_S:%.o=%.P)
225endif
226
227asm_sources_s := $(filter %.s,$(LOCAL_SRC_FILES))
228asm_objects_s := $(addprefix $(intermediates)/,$(asm_sources_s:.s=.o))
229
230ifneq ($(strip $(asm_objects_s)),)
231$(asm_objects_s): $(intermediates)/%.o: $(TOPDIR)$(LOCAL_PATH)/%.s $(PRIVATE_ADDITIONAL_DEPENDENCIES)
232	$(transform-$(PRIVATE_HOST)s-to-o-no-deps)
233-include $(asm_objects_s:%.o=%.P)
234endif
235
236asm_objects := $(asm_objects_S) $(asm_objects_s)
237
238
239###########################################################
240## Common object handling.
241###########################################################
242
243# some rules depend on asm_objects being first.  If your code depends on
244# being first, it's reasonable to require it to be assembly
245all_objects := \
246	$(asm_objects) \
247	$(cpp_objects) \
248	$(gen_cpp_objects) \
249	$(gen_asm_objects) \
250	$(c_objects) \
251	$(yacc_objects) \
252	$(lex_objects) \
253	$(addprefix $(TOPDIR)$(LOCAL_PATH)/,$(LOCAL_PREBUILT_OBJ_FILES))
254
255LOCAL_C_INCLUDES += $(TOPDIR)$(LOCAL_PATH) $(intermediates) $(base_intermediates)
256
257$(all_objects) : | $(LOCAL_GENERATED_SOURCES)
258ALL_C_CPP_ETC_OBJECTS += $(all_objects)
259
260###########################################################
261## Copy headers to the install tree
262###########################################################
263include $(BUILD_COPY_HEADERS)
264
265###########################################################
266# Standard library handling.
267#
268# On the target, we compile with -nostdlib, so we must add in the
269# default system shared libraries, unless they have requested not
270# to by supplying a LOCAL_SYSTEM_SHARED_LIBRARIES value.  One would
271# supply that, for example, when building libc itself.
272###########################################################
273ifndef LOCAL_IS_HOST_MODULE
274  ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
275    LOCAL_SHARED_LIBRARIES += $($(my_prefix)DEFAULT_SYSTEM_SHARED_LIBRARIES)
276  else
277    LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
278  endif
279endif
280
281# Logging used to be part of libcutils (target) and libutils (sim);
282# hack modules that use those other libs to also include liblog.
283# All of this complexity is to make sure that liblog only appears
284# once, and appears just before libcutils or libutils on the link
285# line.
286# TODO: remove this hack and change all modules to use liblog
287# when necessary.
288define insert-liblog
289  $(if $(filter liblog,$(1)),$(1), \
290    $(if $(filter libcutils,$(1)), \
291      $(patsubst libcutils,liblog libcutils,$(1)) \
292     , \
293      $(patsubst libutils,liblog libutils,$(1)) \
294     ) \
295   )
296endef
297ifneq (,$(filter libcutils libutils,$(LOCAL_SHARED_LIBRARIES)))
298  LOCAL_SHARED_LIBRARIES := $(call insert-liblog,$(LOCAL_SHARED_LIBRARIES))
299endif
300ifneq (,$(filter libcutils libutils,$(LOCAL_STATIC_LIBRARIES)))
301  LOCAL_STATIC_LIBRARIES := $(call insert-liblog,$(LOCAL_STATIC_LIBRARIES))
302endif
303ifneq (,$(filter libcutils libutils,$(LOCAL_WHOLE_STATIC_LIBRARIES)))
304  LOCAL_WHOLE_STATIC_LIBRARIES := $(call insert-liblog,$(LOCAL_WHOLE_STATIC_LIBRARIES))
305endif
306
307###########################################################
308# The list of libraries that this module will link against are in
309# these variables.  Each is a list of bare module names like "libc libm".
310#
311# LOCAL_SHARED_LIBRARIES
312# LOCAL_STATIC_LIBRARIES
313# LOCAL_WHOLE_STATIC_LIBRARIES
314#
315# We need to convert the bare names into the dependencies that
316# we'll use for LOCAL_BUILT_MODULE and LOCAL_INSTALLED_MODULE.
317# LOCAL_BUILT_MODULE should depend on the BUILT versions of the
318# libraries, so that simply building this module doesn't force
319# an install of a library.  Similarly, LOCAL_INSTALLED_MODULE
320# should depend on the INSTALLED versions of the libraries so
321# that they get installed when this module does.
322###########################################################
323# NOTE:
324# WHOLE_STATIC_LIBRARIES are libraries that are pulled into the
325# module without leaving anything out, which is useful for turning
326# a collection of .a files into a .so file.  Linking against a
327# normal STATIC_LIBRARY will only pull in code/symbols that are
328# referenced by the module. (see gcc/ld's --whole-archive option)
329###########################################################
330
331# Get the list of BUILT libraries, which are under
332# various intermediates directories.
333so_suffix := $($(my_prefix)SHLIB_SUFFIX)
334a_suffix := $($(my_prefix)STATIC_LIB_SUFFIX)
335
336built_shared_libraries := \
337    $(addprefix $($(my_prefix)OUT_INTERMEDIATE_LIBRARIES)/, \
338      $(addsuffix $(so_suffix), \
339        $(LOCAL_SHARED_LIBRARIES)))
340
341built_static_libraries := \
342    $(foreach lib,$(LOCAL_STATIC_LIBRARIES), \
343      $(call intermediates-dir-for, \
344        STATIC_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE))/$(lib)$(a_suffix))
345
346built_whole_libraries := \
347    $(foreach lib,$(LOCAL_WHOLE_STATIC_LIBRARIES), \
348      $(call intermediates-dir-for, \
349        STATIC_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE))/$(lib)$(a_suffix))
350
351# Get the list of INSTALLED libraries.  Strip off the various
352# intermediates directories and point to the common lib dirs.
353installed_shared_libraries := \
354    $(addprefix $($(my_prefix)OUT_SHARED_LIBRARIES)/, \
355      $(notdir $(built_shared_libraries)))
356
357# We don't care about installed static libraries, since the
358# libraries have already been linked into the module at that point.
359# We do, however, care about the NOTICE files for any static
360# libraries that we use. (see notice_files.make)
361
362installed_static_library_notice_file_targets := \
363    $(foreach lib,$(LOCAL_STATIC_LIBRARIES) $(LOCAL_WHOLE_STATIC_LIBRARIES), \
364      NOTICE-$(if $(LOCAL_IS_HOST_MODULE),HOST,TARGET)-STATIC_LIBRARIES-$(lib))
365
366###########################################################
367# Rule-specific variable definitions
368###########################################################
369$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_YACCFLAGS := $(LOCAL_YACCFLAGS)
370$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASFLAGS := $(LOCAL_ASFLAGS)
371$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CFLAGS := $(LOCAL_CFLAGS)
372$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CPPFLAGS := $(LOCAL_CPPFLAGS)
373$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEBUG_CFLAGS := $(debug_cflags)
374$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_C_INCLUDES := $(LOCAL_C_INCLUDES)
375$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDFLAGS := $(LOCAL_LDFLAGS)
376$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDLIBS := $(LOCAL_LDLIBS)
377
378# this is really the way to get the files onto the command line instead
379# of using $^, because then LOCAL_ADDITIONAL_DEPENDENCIES doesn't work
380$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_SHARED_LIBRARIES := $(built_shared_libraries)
381$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_STATIC_LIBRARIES := $(built_static_libraries)
382$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_WHOLE_STATIC_LIBRARIES := $(built_whole_libraries)
383$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_OBJECTS := $(all_objects)
384
385###########################################################
386# Define library dependencies.
387###########################################################
388# all_libraries is used for the dependencies on LOCAL_BUILT_MODULE.
389all_libraries := \
390    $(built_shared_libraries) \
391    $(built_static_libraries) \
392    $(built_whole_libraries)
393
394# Make LOCAL_INSTALLED_MODULE depend on the installed versions of the
395# libraries so they get installed along with it.  We don't need to
396# rebuild it when installing it, though, so this can be an order-only
397# dependency.
398$(LOCAL_INSTALLED_MODULE): | $(installed_shared_libraries)
399
400# Also depend on the notice files for any static libraries that
401# are linked into this module.  This will force them to be installed
402# when this module is.
403$(LOCAL_INSTALLED_MODULE): | $(installed_static_library_notice_file_targets)
404