binary.mk revision d033d57f409d8df2ddda319e191da7ee71009362
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_CFLAGS or
51# TARGET_thumb_CFLAGS.  HOST_(arm|thumb)_CFLAGS values aren't
52# actually used (although they are usually empty).
53arm_objects_cflags := $($(my_prefix)$(arm_objects_mode)_CFLAGS)
54normal_objects_cflags := $($(my_prefix)$(normal_objects_mode)_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) $(LOCAL_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		$(LOCAL_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) $(LOCAL_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) $(LOCAL_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 $(LOCAL_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 $(LOCAL_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) $(LOCAL_ADDITIONAL_DEPENDENCIES)
210	$(transform-$(PRIVATE_HOST)c-to-o)
211-include $(c_objects:%.o=%.P)
212endif
213
214###########################################################
215## C: Compile generated .c files to .o.
216###########################################################
217
218gen_c_sources := $(filter %.c,$(LOCAL_GENERATED_SOURCES))
219gen_c_objects := $(gen_c_sources:%.c=%.o)
220
221ifneq ($(strip $(gen_c_objects)),)
222# Compile all generated files as thumb.
223# TODO: support compiling certain generated files as arm.
224$(gen_c_objects): PRIVATE_ARM_MODE := $(normal_objects_mode)
225$(gen_c_objects): PRIVATE_ARM_CFLAGS := $(normal_objects_cflags)
226$(gen_c_objects): $(intermediates)/%.o: $(intermediates)/%.c $(yacc_cpps) $(LOCAL_ADDITIONAL_DEPENDENCIES)
227	$(transform-$(PRIVATE_HOST)c-to-o)
228-include $(gen_c_objects:%.o=%.P)
229endif
230
231###########################################################
232## AS: Compile .S files to .o.
233###########################################################
234
235asm_sources_S := $(filter %.S,$(LOCAL_SRC_FILES))
236asm_objects_S := $(addprefix $(intermediates)/,$(asm_sources_S:.S=.o))
237
238ifneq ($(strip $(asm_objects_S)),)
239$(asm_objects_S): $(intermediates)/%.o: $(TOPDIR)$(LOCAL_PATH)/%.S $(LOCAL_ADDITIONAL_DEPENDENCIES)
240	$(transform-$(PRIVATE_HOST)s-to-o)
241-include $(asm_objects_S:%.o=%.P)
242endif
243
244asm_sources_s := $(filter %.s,$(LOCAL_SRC_FILES))
245asm_objects_s := $(addprefix $(intermediates)/,$(asm_sources_s:.s=.o))
246
247ifneq ($(strip $(asm_objects_s)),)
248$(asm_objects_s): $(intermediates)/%.o: $(TOPDIR)$(LOCAL_PATH)/%.s $(LOCAL_ADDITIONAL_DEPENDENCIES)
249	$(transform-$(PRIVATE_HOST)s-to-o-no-deps)
250-include $(asm_objects_s:%.o=%.P)
251endif
252
253asm_objects := $(asm_objects_S) $(asm_objects_s)
254
255
256###########################################################
257## Common object handling.
258###########################################################
259
260# some rules depend on asm_objects being first.  If your code depends on
261# being first, it's reasonable to require it to be assembly
262all_objects := \
263	$(asm_objects) \
264	$(cpp_objects) \
265	$(gen_cpp_objects) \
266	$(gen_asm_objects) \
267	$(c_objects) \
268	$(gen_c_objects) \
269	$(yacc_objects) \
270	$(lex_objects) \
271	$(addprefix $(TOPDIR)$(LOCAL_PATH)/,$(LOCAL_PREBUILT_OBJ_FILES))
272
273LOCAL_C_INCLUDES += $(TOPDIR)$(LOCAL_PATH) $(intermediates) $(base_intermediates)
274
275$(all_objects) : | $(LOCAL_GENERATED_SOURCES)
276ALL_C_CPP_ETC_OBJECTS += $(all_objects)
277
278###########################################################
279## Copy headers to the install tree
280###########################################################
281include $(BUILD_COPY_HEADERS)
282
283###########################################################
284# Standard library handling.
285#
286# On the target, we compile with -nostdlib, so we must add in the
287# default system shared libraries, unless they have requested not
288# to by supplying a LOCAL_SYSTEM_SHARED_LIBRARIES value.  One would
289# supply that, for example, when building libc itself.
290###########################################################
291ifndef LOCAL_IS_HOST_MODULE
292  ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
293    LOCAL_SHARED_LIBRARIES += $($(my_prefix)DEFAULT_SYSTEM_SHARED_LIBRARIES)
294  else
295    LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
296  endif
297endif
298
299# Logging used to be part of libcutils (target) and libutils (sim);
300# hack modules that use those other libs to also include liblog.
301# All of this complexity is to make sure that liblog only appears
302# once, and appears just before libcutils or libutils on the link
303# line.
304# TODO: remove this hack and change all modules to use liblog
305# when necessary.
306define insert-liblog
307  $(if $(filter liblog,$(1)),$(1), \
308    $(if $(filter libcutils,$(1)), \
309      $(patsubst libcutils,liblog libcutils,$(1)) \
310     , \
311      $(patsubst libutils,liblog libutils,$(1)) \
312     ) \
313   )
314endef
315ifneq (,$(filter libcutils libutils,$(LOCAL_SHARED_LIBRARIES)))
316  LOCAL_SHARED_LIBRARIES := $(call insert-liblog,$(LOCAL_SHARED_LIBRARIES))
317endif
318ifneq (,$(filter libcutils libutils,$(LOCAL_STATIC_LIBRARIES)))
319  LOCAL_STATIC_LIBRARIES := $(call insert-liblog,$(LOCAL_STATIC_LIBRARIES))
320endif
321ifneq (,$(filter libcutils libutils,$(LOCAL_WHOLE_STATIC_LIBRARIES)))
322  LOCAL_WHOLE_STATIC_LIBRARIES := $(call insert-liblog,$(LOCAL_WHOLE_STATIC_LIBRARIES))
323endif
324
325###########################################################
326# The list of libraries that this module will link against are in
327# these variables.  Each is a list of bare module names like "libc libm".
328#
329# LOCAL_SHARED_LIBRARIES
330# LOCAL_STATIC_LIBRARIES
331# LOCAL_WHOLE_STATIC_LIBRARIES
332#
333# We need to convert the bare names into the dependencies that
334# we'll use for LOCAL_BUILT_MODULE and LOCAL_INSTALLED_MODULE.
335# LOCAL_BUILT_MODULE should depend on the BUILT versions of the
336# libraries, so that simply building this module doesn't force
337# an install of a library.  Similarly, LOCAL_INSTALLED_MODULE
338# should depend on the INSTALLED versions of the libraries so
339# that they get installed when this module does.
340###########################################################
341# NOTE:
342# WHOLE_STATIC_LIBRARIES are libraries that are pulled into the
343# module without leaving anything out, which is useful for turning
344# a collection of .a files into a .so file.  Linking against a
345# normal STATIC_LIBRARY will only pull in code/symbols that are
346# referenced by the module. (see gcc/ld's --whole-archive option)
347###########################################################
348
349# Get the list of BUILT libraries, which are under
350# various intermediates directories.
351so_suffix := $($(my_prefix)SHLIB_SUFFIX)
352a_suffix := $($(my_prefix)STATIC_LIB_SUFFIX)
353
354built_shared_libraries := \
355    $(addprefix $($(my_prefix)OUT_INTERMEDIATE_LIBRARIES)/, \
356      $(addsuffix $(so_suffix), \
357        $(LOCAL_SHARED_LIBRARIES)))
358
359built_static_libraries := \
360    $(foreach lib,$(LOCAL_STATIC_LIBRARIES), \
361      $(call intermediates-dir-for, \
362        STATIC_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE))/$(lib)$(a_suffix))
363
364built_whole_libraries := \
365    $(foreach lib,$(LOCAL_WHOLE_STATIC_LIBRARIES), \
366      $(call intermediates-dir-for, \
367        STATIC_LIBRARIES,$(lib),$(LOCAL_IS_HOST_MODULE))/$(lib)$(a_suffix))
368
369# Get the list of INSTALLED libraries.  Strip off the various
370# intermediates directories and point to the common lib dirs.
371installed_shared_libraries := \
372    $(addprefix $($(my_prefix)OUT_SHARED_LIBRARIES)/, \
373      $(notdir $(built_shared_libraries)))
374
375# We don't care about installed static libraries, since the
376# libraries have already been linked into the module at that point.
377# We do, however, care about the NOTICE files for any static
378# libraries that we use. (see notice_files.make)
379
380installed_static_library_notice_file_targets := \
381    $(foreach lib,$(LOCAL_STATIC_LIBRARIES) $(LOCAL_WHOLE_STATIC_LIBRARIES), \
382      NOTICE-$(if $(LOCAL_IS_HOST_MODULE),HOST,TARGET)-STATIC_LIBRARIES-$(lib))
383
384###########################################################
385# Rule-specific variable definitions
386###########################################################
387$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_YACCFLAGS := $(LOCAL_YACCFLAGS)
388$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ASFLAGS := $(LOCAL_ASFLAGS)
389$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CFLAGS := $(LOCAL_CFLAGS)
390$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_CPPFLAGS := $(LOCAL_CPPFLAGS)
391$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_DEBUG_CFLAGS := $(debug_cflags)
392$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_C_INCLUDES := $(LOCAL_C_INCLUDES)
393$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDFLAGS := $(LOCAL_LDFLAGS)
394$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_LDLIBS := $(LOCAL_LDLIBS)
395
396# this is really the way to get the files onto the command line instead
397# of using $^, because then LOCAL_ADDITIONAL_DEPENDENCIES doesn't work
398$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_SHARED_LIBRARIES := $(built_shared_libraries)
399$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_STATIC_LIBRARIES := $(built_static_libraries)
400$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_WHOLE_STATIC_LIBRARIES := $(built_whole_libraries)
401$(LOCAL_INTERMEDIATE_TARGETS): PRIVATE_ALL_OBJECTS := $(all_objects)
402
403###########################################################
404# Define library dependencies.
405###########################################################
406# all_libraries is used for the dependencies on LOCAL_BUILT_MODULE.
407all_libraries := \
408    $(built_shared_libraries) \
409    $(built_static_libraries) \
410    $(built_whole_libraries)
411
412# Make LOCAL_INSTALLED_MODULE depend on the installed versions of the
413# libraries so they get installed along with it.  We don't need to
414# rebuild it when installing it, though, so this can be an order-only
415# dependency.
416$(LOCAL_INSTALLED_MODULE): | $(installed_shared_libraries)
417
418# Also depend on the notice files for any static libraries that
419# are linked into this module.  This will force them to be installed
420# when this module is.
421$(LOCAL_INSTALLED_MODULE): | $(installed_static_library_notice_file_targets)
422