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