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