build-binary.mk revision 488e22a37f47c49be48a0f2f877298dd5fd0d1c9
1# Copyright (C) 2008 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#      http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14#
15
16# Check that LOCAL_MODULE is defined, then restore its LOCAL_XXXX values
17$(call assert-defined,LOCAL_MODULE)
18$(call module-restore-locals,$(LOCAL_MODULE))
19
20# For now, only support target (device-specific modules).
21# We may want to introduce support for host modules in the future
22# but that is too experimental for now.
23#
24my := TARGET_
25
26# LOCAL_MAKEFILE must also exist and name the Android.mk that
27# included the module build script.
28#
29$(call assert-defined,LOCAL_MAKEFILE LOCAL_BUILD_SCRIPT LOCAL_BUILT_MODULE)
30
31include $(BUILD_SYSTEM)/import-locals.mk
32
33#
34# Ensure that 'make <module>' and 'make clean-<module>' work
35#
36.PHONY: $(LOCAL_MODULE)
37$(LOCAL_MODULE): $(LOCAL_BUILT_MODULE)
38
39cleantarget := clean-$(LOCAL_MODULE)-$(TARGET_ARCH_ABI)
40.PHONY: $(cleantarget)
41clean: $(cleantarget)
42
43$(cleantarget): PRIVATE_MODULE      := $(LOCAL_MODULE)
44$(cleantarget): PRIVATE_TEXT        := [$(TARGET_ARCH_ABI)]
45$(cleantarget): PRIVATE_CLEAN_FILES := $(LOCAL_BUILT_MODULE) \
46                                       $($(my)OBJS)
47
48$(cleantarget)::
49	@$(HOST_ECHO) "Clean: $(PRIVATE_MODULE) $(PRIVATE_TEXT)"
50	$(hide) $(call host-rmdir,$(PRIVATE_CLEAN_FILES))
51
52ifeq ($(NDK_APP_DEBUGGABLE),true)
53$(NDK_APP_GDBSETUP): PRIVATE_SRC_DIRS += $(LOCAL_C_INCLUDES) $(LOCAL_PATH)
54endif
55
56# list of generated object files
57LOCAL_OBJECTS :=
58
59# always define ANDROID when building binaries
60#
61LOCAL_CFLAGS := -DANDROID $(LOCAL_CFLAGS)
62
63#
64# Add the default system shared libraries to the build
65#
66ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
67  LOCAL_SHARED_LIBRARIES += $(TARGET_DEFAULT_SYSTEM_SHARED_LIBRARIES)
68else
69  LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
70endif
71
72
73#
74# Check LOCAL_CPP_EXTENSION, use '.cpp' by default
75#
76bad_cpp_extensions := $(strip $(filter-out .%,$(LOCAL_CPP_EXTENSION)))
77ifdef bad_cpp_extensions
78    $(call __ndk_info,WARNING: Invalid LOCAL_CPP_EXTENSION values: $(bad_cpp_extensions))
79    LOCAL_CPP_EXTENSION := $(filter $(bad_cpp_extensions),$(LOCAL_CPP_EXTENSIONS))
80endif
81LOCAL_CPP_EXTENSION := $(strip $(LOCAL_CPP_EXTENSION))
82ifeq ($(LOCAL_CPP_EXTENSION),)
83  LOCAL_CPP_EXTENSION := .cpp
84else
85endif
86
87#
88# If LOCAL_ALLOW_UNDEFINED_SYMBOLS is not true, the linker will allow the generation
89# of a binary that uses undefined symbols.
90#
91ifneq ($(LOCAL_ALLOW_UNDEFINED_SYMBOLS),true)
92  LOCAL_LDFLAGS += $($(my)NO_UNDEFINED_LDFLAGS)
93endif
94
95# If LOCAL_DISABLE_NO_EXECUTE is not true, we disable generated code from running from
96# the heap and stack by default.
97#
98ifndef ($(LOCAL_DISABLE_NO_EXECUTE),true)
99  LOCAL_CFLAGS += $($(my)NO_EXECUTE_CFLAGS)
100  LOCAL_LDFLAGS += $($(my)NO_EXECUTE_LDFLAGS)
101endif
102
103#
104# The original Android build system allows you to use the .arm prefix
105# to a source file name to indicate that it should be defined in either
106# 'thumb' or 'arm' mode, depending on the value of LOCAL_ARM_MODE
107#
108# First, check LOCAL_ARM_MODE, it should be empty, 'thumb' or 'arm'
109# We make the default 'thumb'
110#
111LOCAL_ARM_MODE := $(strip $(LOCAL_ARM_MODE))
112ifdef LOCAL_ARM_MODE
113  ifneq ($(words $(LOCAL_ARM_MODE)),1)
114      $(call __ndk_info,   LOCAL_ARM_MODE in $(LOCAL_MAKEFILE) must be one word, not '$(LOCAL_ARM_MODE)')
115      $(call __ndk_error, Aborting)
116  endif
117  # check that LOCAL_ARM_MODE is defined to either 'arm' or 'thumb'
118  $(if $(filter-out thumb arm, $(LOCAL_ARM_MODE)),\
119      $(call __ndk_info,   LOCAL_ARM_MODE must be defined to either 'arm' or 'thumb' in $(LOCAL_MAKEFILE) not '$(LOCAL_ARM_MODE)')\
120      $(call __ndk_error, Aborting)\
121  )
122endif
123
124# As a special case, the original Android build system
125# allows one to specify that certain source files can be
126# forced to build in ARM mode by using a '.arm' suffix
127# after the extension, e.g.
128#
129#  LOCAL_SRC_FILES := foo.c.arm
130#
131# to build source file $(LOCAL_PATH)/foo.c as ARM
132#
133
134$(call clear-all-src-tags)
135
136# As a special extension, the NDK also supports the .neon extension suffix
137# to indicate that a single file can be compiled with ARM NEON support
138# We must support both foo.c.neon and foo.c.arm.neon here
139#
140# Also, if LOCAL_ARM_NEON is set to 'true', force Neon mode for all source
141# files
142#
143
144neon_sources  := $(filter %.neon,$(LOCAL_SRC_FILES))
145neon_sources  := $(neon_sources:%.neon=%)
146
147LOCAL_ARM_NEON := $(strip $(LOCAL_ARM_NEON))
148ifdef LOCAL_ARM_NEON
149  $(if $(filter-out true false,$(LOCAL_ARM_NEON)),\
150    $(call __ndk_info,LOCAL_ARM_NEON must be defined either to 'true' or 'false' in $(LOCAL_MAKEFILE), not '$(LOCAL_ARM_NEON)')\
151    $(call __ndk_error,Aborting) \
152  )
153endif
154ifeq ($(LOCAL_ARM_NEON),true)
155  neon_sources += $(LOCAL_SRC_FILES:%.neon=%)
156endif
157
158neon_sources := $(strip $(neon_sources))
159ifdef neon_sources
160  ifneq ($(TARGET_ARCH_ABI),armeabi-v7a)
161    $(call __ndk_info,NEON support is only possible for armeabi-v7a ABI)
162    $(call __ndk_info,Please add checks against TARGET_ARCH_ABI in $(LOCAL_MAKEFILE))
163    $(call __ndk_error,Aborting)
164  endif
165  $(call tag-src-files,$(neon_sources:%.arm=%),neon)
166endif
167
168LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.neon=%)
169
170# strip the .arm suffix from LOCAL_SRC_FILES
171# and tag the relevant sources with the 'arm' tag
172#
173arm_sources     := $(filter %.arm,$(LOCAL_SRC_FILES))
174arm_sources     := $(arm_sources:%.arm=%)
175thumb_sources   := $(filter-out %.arm,$(LOCAL_SRC_FILES))
176LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.arm=%)
177
178ifeq ($(LOCAL_ARM_MODE),arm)
179    arm_sources := $(LOCAL_SRC_FILES)
180endif
181ifeq ($(LOCAL_ARM_MODE),thumb)
182    arm_sources := $(empty)
183endif
184$(call tag-src-files,$(arm_sources),arm)
185
186# Process all source file tags to determine toolchain-specific
187# target compiler flags, and text.
188#
189$(call TARGET-process-src-files-tags)
190
191# only call dump-src-file-tags during debugging
192#$(dump-src-file-tags)
193
194LOCAL_DEPENDENCY_DIRS :=
195
196# all_source_patterns contains the list of filename patterns that correspond
197# to source files recognized by our build system
198all_source_extensions := .c .s .S $(LOCAL_CPP_EXTENSION)
199all_source_patterns   := $(foreach _ext,$(all_source_extensions),%$(_ext))
200all_cpp_patterns      := $(foreach _ext,$(LOCAL_CPP_EXTENSION),%$(_ext))
201
202unknown_sources := $(strip $(filter-out $(all_source_patterns),$(LOCAL_SRC_FILES)))
203ifdef unknown_sources
204    $(call __ndk_info,WARNING: Unsupported source file extensions in $(LOCAL_MAKEFILE) for module $(LOCAL_MODULE))
205    $(call __ndk_info,  $(unknown_sources))
206endif
207
208# LOCAL_OBJECTS will list all object files corresponding to the sources
209# listed in LOCAL_SRC_FILES, in the *same* order.
210#
211LOCAL_OBJECTS := $(LOCAL_SRC_FILES)
212$(foreach _ext,$(all_source_extensions),\
213    $(eval LOCAL_OBJECTS := $$(LOCAL_OBJECTS:%$(_ext)=%.o))\
214)
215LOCAL_OBJECTS := $(filter %.o,$(LOCAL_OBJECTS))
216LOCAL_OBJECTS := $(subst ../,__/,$(LOCAL_OBJECTS))
217LOCAL_OBJECTS := $(foreach _obj,$(LOCAL_OBJECTS),$(LOCAL_OBJS_DIR)/$(_obj))
218
219# If the module has any kind of C++ features, enable them in LOCAL_CPPFLAGS
220#
221ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti))
222    LOCAL_CPPFLAGS += -frtti
223endif
224ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),exceptions))
225    LOCAL_CPPFLAGS += -fexceptions
226endif
227
228# If we're using the 'system' STL and use rtti or exceptions, then
229# automatically link against the GNU libsupc++ for now.
230#
231ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti exceptions))
232    ifeq (system,$(NDK_APP_STL))
233      LOCAL_LDLIBS := $(LOCAL_LDLIBS) $(call host-path,$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/libs/$(TARGET_ARCH_ABI)/libsupc++.a)
234    endif
235endif
236
237# Build the sources to object files
238#
239
240$(foreach src,$(filter %.c,$(LOCAL_SRC_FILES)), $(call compile-c-source,$(src),$(call get-object-name,$(src))))
241$(foreach src,$(filter %.S %.s,$(LOCAL_SRC_FILES)), $(call compile-s-source,$(src),$(call get-object-name,$(src))))
242
243$(foreach src,$(filter $(all_cpp_patterns),$(LOCAL_SRC_FILES)),\
244    $(call compile-cpp-source,$(src),$(call get-object-name,$(src)))\
245)
246
247#
248# The compile-xxx-source calls updated LOCAL_OBJECTS and LOCAL_DEPENDENCY_DIRS
249#
250ALL_DEPENDENCY_DIRS += $(sort $(LOCAL_DEPENDENCY_DIRS))
251CLEAN_OBJS_DIRS     += $(LOCAL_OBJS_DIR)
252
253#
254# Handle the static and shared libraries this module depends on
255#
256LOCAL_STATIC_LIBRARIES       := $(call strip-lib-prefix,$(LOCAL_STATIC_LIBRARIES))
257LOCAL_WHOLE_STATIC_LIBRARIES := $(call strip-lib-prefix,$(LOCAL_WHOLE_STATIC_LIBRARIES))
258LOCAL_SHARED_LIBRARIES       := $(call strip-lib-prefix,$(LOCAL_SHARED_LIBRARIES))
259
260# Transitive closure of static libraries
261LOCAL_STATIC_LIBRARIES       := $(call module-get-depends,$(LOCAL_STATIC_LIBRARIES),STATIC_LIBRARIES)
262LOCAL_WHOLE_STATIC_LIBRARIES := $(call module-get-depends,$(LOCAL_WHOLE_STATIC_LIBRARIES),WHOLE_STATIC_LIBRARIES)
263
264static_libraries       := $(call map,module-get-built,$(LOCAL_STATIC_LIBRARIES))
265whole_static_libraries := $(call map,module-get-built,$(LOCAL_WHOLE_STATIC_LIBRARIES))
266
267shared_libraries := $(call map,module-get-built,$(LOCAL_SHARED_LIBRARIES))\
268                    $(TARGET_PREBUILT_SHARED_LIBRARIES)
269
270$(LOCAL_BUILT_MODULE): $(static_libraries) $(whole_static_libraries) $(shared_libraries)
271
272# If LOCAL_LDLIBS contains anything like -l<library> then
273# prepend a -L$(SYSROOT)/usr/lib to it to ensure that the linker
274# looks in the right location
275#
276ifneq ($(filter -l%,$(LOCAL_LDLIBS)),)
277    LOCAL_LDLIBS := -L$(call host-path,$(SYSROOT)/usr/lib) $(LOCAL_LDLIBS)
278endif
279
280# The list of object/static/shared libraries passed to the linker when
281# building shared libraries and executables. order is important.
282#
283linker_objects_and_libraries := $(strip $(call TARGET-get-linker-objects-and-libraries,\
284    $(LOCAL_OBJECTS), \
285    $(static_libraries), \
286    $(whole_static_libraries), \
287    $(shared_libraries)))
288
289# The list of object files sent to 'ar' when building static libraries
290#
291ar_objects := $(call host-path,$(LOCAL_OBJECTS))
292
293# When LOCAL_SHORT_COMMANDS is defined to 'true' we are going to write the
294# list of all object files and/or static/shared libraries that appear on the
295# command line to a file, then use the @<listfile> syntax to invoke it.
296#
297# This allows us to link or archive a huge number of stuff even on Windows
298# with its puny 8192 max character limit on its command-line.
299#
300LOCAL_SHORT_COMMANDS := $(strip $(LOCAL_SHORT_COMMANDS))
301ifndef LOCAL_SHORT_COMMANDS
302    LOCAL_SHORT_COMMANDS := $(strip $(NDK_APP_SHORT_COMMANDS))
303endif
304ifeq ($(LOCAL_SHORT_COMMANDS),true)
305    # For static and whole static libraries
306    ifneq (,$(filter STATIC_LIBRARY WHOLE_STATIC_LIBRARY,$(call module-get-class,$(LOCAL_MODULE))))
307        $(call ndk_log,Building static library module '$(LOCAL_MODULE)' with linker list file)
308        ar_options   := $(ar_objects)
309        ar_list_file := $(LOCAL_OBJS_DIR)/archiver.list
310        ar_objects   := @$(call host-path,$(ar_list_file))
311        $(call generate-list-file,$(ar_options),$(ar_list_file))
312
313        $(LOCAL_BUILT_MODULE): $(ar_list_file)
314    endif
315
316    # For shared libraries and executables
317    ifneq (,$(filter SHARED_LIBRARY EXECUTABLE,$(call module-get-class,$(LOCAL_MODULE))))
318        $(call ndk_log,Building ELF binary module '$(LOCAL_MODULE)' with linker list file)
319        linker_options   := $(linker_objects_and_libraries)
320        linker_list_file := $(LOCAL_OBJS_DIR)/linker.list
321        linker_objects_and_libraries := @$(call host-path,$(linker_list_file))
322
323        $(call generate-list-file,$(linker_options),$(linker_list_file))
324
325        $(LOCAL_BUILT_MODULE): $(linker_list_file)
326    endif
327
328endif
329
330$(LOCAL_BUILT_MODULE): PRIVATE_STATIC_LIBRARIES := $(static_libraries)
331$(LOCAL_BUILT_MODULE): PRIVATE_WHOLE_STATIC_LIBRARIES := $(whole_static_libraries)
332$(LOCAL_BUILT_MODULE): PRIVATE_SHARED_LIBRARIES := $(shared_libraries)
333$(LOCAL_BUILT_MODULE): PRIVATE_OBJECTS          := $(LOCAL_OBJECTS)
334$(LOCAL_BUILT_MODULE): PRIVATE_LINKER_OBJECTS_AND_LIBRARIES := $(linker_objects_and_libraries)
335$(LOCAL_BUILT_MODULE): PRIVATE_LIBGCC := $(TARGET_LIBGCC)
336
337$(LOCAL_BUILT_MODULE): PRIVATE_LD := $(TARGET_LD)
338$(LOCAL_BUILT_MODULE): PRIVATE_LDFLAGS := $(TARGET_LDFLAGS) $(LOCAL_LDFLAGS)
339$(LOCAL_BUILT_MODULE): PRIVATE_LDLIBS  := $(LOCAL_LDLIBS) $(TARGET_LDLIBS)
340
341$(LOCAL_BUILT_MODULE): PRIVATE_NAME := $(notdir $(LOCAL_BUILT_MODULE))
342$(LOCAL_BUILT_MODULE): PRIVATE_CXX := $(TARGET_CXX)
343$(LOCAL_BUILT_MODULE): PRIVATE_CC := $(TARGET_CC)
344$(LOCAL_BUILT_MODULE): PRIVATE_AR := $(TARGET_AR) $(TARGET_ARFLAGS)
345$(LOCAL_BUILT_MODULE): PRIVATE_AR_OBJECTS := $(ar_objects)
346$(LOCAL_BUILT_MODULE): PRIVATE_SYSROOT := $(SYSROOT)
347
348$(LOCAL_BUILT_MODULE): PRIVATE_LDSCRIPT_X := $(TARGET_LDSCRIPT_X)
349$(LOCAL_BUILT_MODULE): PRIVATE_LDSCRIPT_XSC := $(TARGET_LDSCRIPT_XSC)
350
351#
352# If this is a static library module
353#
354ifeq ($(call module-get-class,$(LOCAL_MODULE)),STATIC_LIBRARY)
355$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
356	@ $(call host-mkdir,$(dir $@))
357	@ $(HOST_ECHO) "StaticLibrary  : $(PRIVATE_NAME)"
358	$(hide) $(call host-rm,$@)
359	$(hide) $(cmd-build-static-library)
360
361ALL_STATIC_LIBRARIES += $(LOCAL_BUILT_MODULE)
362endif
363
364#
365# If this is a shared library module
366#
367ifeq ($(call module-get-class,$(LOCAL_MODULE)),SHARED_LIBRARY)
368$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
369	@ $(call host-mkdir,$(dir $@))
370	@ $(HOST_ECHO) "SharedLibrary  : $(PRIVATE_NAME)"
371	$(hide) $(cmd-build-shared-library)
372
373ALL_SHARED_LIBRARIES += $(LOCAL_BUILT_MODULE)
374endif
375
376#
377# If this is an executable module
378#
379ifeq ($(call module-get-class,$(LOCAL_MODULE)),EXECUTABLE)
380$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
381	@ $(call host-mkdir,$(dir $@))
382	@ $(HOST_ECHO) "Executable     : $(PRIVATE_NAME)"
383	$(hide) $(cmd-build-executable)
384
385ALL_EXECUTABLES += $(LOCAL_BUILT_MODULE)
386endif
387
388#
389# If this is a prebuilt module
390#
391ifeq ($(call module-is-prebuilt,$(LOCAL_MODULE)),$(true))
392$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
393	@ $(call host-mkdir,$(dir $@))
394	@ $(HOST_ECHO) "Prebuilt       : $(PRIVATE_NAME) <= $(call pretty-dir,$(dir $<))"
395	$(hide) $(call host-cp,$<,$@)
396endif
397
398#
399# If this is an installable module
400#
401ifeq ($(call module-is-installable,$(LOCAL_MODULE)),$(true))
402$(LOCAL_INSTALLED): PRIVATE_NAME    := $(notdir $(LOCAL_BUILT_MODULE))
403$(LOCAL_INSTALLED): PRIVATE_SRC     := $(LOCAL_BUILT_MODULE)
404$(LOCAL_INSTALLED): PRIVATE_DST_DIR := $(NDK_APP_DST_DIR)
405$(LOCAL_INSTALLED): PRIVATE_DST     := $(LOCAL_INSTALLED)
406$(LOCAL_INSTALLED): PRIVATE_STRIP   := $(TARGET_STRIP)
407
408$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
409	@$(HOST_ECHO) "Install        : $(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
410	$(hide) $(call host-mkdir,$(PRIVATE_DST_DIR))
411	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
412	$(hide) $(call cmd-strip, $(PRIVATE_DST))
413endif
414