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
31# A list of LOCAL_XXX variables that are ignored for static libraries.
32# Print a warning if they are present inside a module definition to let
33# the user know this won't do what he/she expects.
34not_in_static_libs := \
35    LOCAL_LDFLAGS \
36    LOCAL_LDLIBS \
37    LOCAL_ALLOW_UNDEFINED_SYMBOLS
38
39ifeq ($(call module-get-class,$(LOCAL_MODULE)),STATIC_LIBRARY)
40$(foreach _notvar,$(not_in_static_libs),\
41    $(if $(strip $($(_notvar))),\
42        $(call __ndk_info,WARNING:$(LOCAL_MAKEFILE):$(LOCAL_MODULE): $(_notvar) is always ignored for static libraries)\
43    )\
44)
45endif
46
47# Some developers like to add library names (e.g. -lfoo) to LOCAL_LDLIBS
48# and LOCAL_LDFLAGS directly. This is very fragile and can lead to broken
49# builds and other nasty surprises, because it doesn't tell ndk-build
50# that the corresponding module depends on these files. Emit a warning
51# when we detect this case.
52libs_in_ldflags := $(filter -l% %.so %.a,$(LOCAL_LDLIBS) $(LOCAL_LDFLAGS))
53
54# Remove the system libraries we know about from the warning, it's ok
55# (and actually expected) to link them with -l<name>.
56system_libs := \
57    android \
58    c \
59    dl \
60    jnigraphics \
61    log \
62    m \
63    m_hard \
64    stdc++ \
65    z \
66    EGL \
67    GLESv1_CM \
68    GLESv2 \
69    GLESv3 \
70    OpenSLES \
71    OpenMAXAL \
72    mediandk \
73    atomic
74
75libs_in_ldflags := $(filter-out $(addprefix -l,$(system_libs)), $(libs_in_ldflags))
76
77ifneq (,$(strip $(libs_in_ldflags)))
78  $(call __ndk_info,WARNING:$(LOCAL_MAKEFILE):$(LOCAL_MODULE): non-system libraries in linker flags: $(libs_in_ldflags))
79  $(call __ndk_info,    This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES)
80  $(call __ndk_info,    or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the)
81  $(call __ndk_info,    current module)
82endif
83
84include $(BUILD_SYSTEM)/import-locals.mk
85
86# Check for LOCAL_THIN_ARCHIVE / APP_THIN_ARCHIVE and print a warning if
87# it is defined for non-static library modules.
88thin_archive := $(strip $(LOCAL_THIN_ARCHIVE))
89ifdef thin_archive
90ifneq (STATIC_LIBRARY,$(call module-get-class,$(LOCAL_MODULE)))
91    $(call __ndk_info,WARNING:$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_THIN_ARCHIVE is for building static libraries)
92endif
93endif
94
95ifndef thin_archive
96    thin_archive := $(strip $(NDK_APP_THIN_ARCHIVE))
97endif
98# Print a warning if the value is not 'true', 'false' or empty.
99ifneq (,$(filter-out true false,$(thin_archive)))
100    $(call __ndk_info,WARNING:$(LOCAL_MAKEFILE):$(LOCAL_MODULE): Invalid LOCAL_THIN_ARCHIVE value '$(thin_archive)' ignored!)
101    thin_archive :=
102endif
103
104#
105# Ensure that 'make <module>' and 'make clean-<module>' work
106#
107.PHONY: $(LOCAL_MODULE)
108$(LOCAL_MODULE): $(LOCAL_BUILT_MODULE)
109
110cleantarget := clean-$(LOCAL_MODULE)-$(TARGET_ARCH_ABI)
111.PHONY: $(cleantarget)
112clean: $(cleantarget)
113
114$(cleantarget): PRIVATE_ABI         := $(TARGET_ARCH_ABI)
115$(cleantarget): PRIVATE_MODULE      := $(LOCAL_MODULE)
116ifneq ($(LOCAL_BUILT_MODULE_NOT_COPIED),true)
117$(cleantarget): PRIVATE_CLEAN_FILES := $(LOCAL_BUILT_MODULE) \
118                                       $($(my)OBJS)
119else
120$(cleantarget): PRIVATE_CLEAN_FILES := $($(my)OBJS)
121endif
122$(cleantarget)::
123	$(call host-echo-build-step,$(PRIVATE_ABI),Clean) "$(PRIVATE_MODULE) [$(PRIVATE_ABI)]"
124	$(hide) $(call host-rmdir,$(PRIVATE_CLEAN_FILES))
125
126ifeq ($(NDK_APP_DEBUGGABLE),true)
127$(NDK_APP_GDBSETUP): PRIVATE_SRC_DIRS += $(LOCAL_C_INCLUDES) $(LOCAL_PATH)
128endif
129
130# list of generated object files
131LOCAL_OBJECTS :=
132
133# list of generated object files from RS files, subset of LOCAL_OBJECTS
134LOCAL_RS_OBJECTS :=
135
136# always define ANDROID when building binaries
137#
138LOCAL_CFLAGS := -DANDROID $(LOCAL_CFLAGS)
139
140#
141# Add the default system shared libraries to the build
142#
143ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
144  LOCAL_SHARED_LIBRARIES += $(TARGET_DEFAULT_SYSTEM_SHARED_LIBRARIES)
145else
146  LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
147endif
148
149#
150# Check LOCAL_CPP_EXTENSION
151#
152bad_cpp_extensions := $(strip $(filter-out .%,$(LOCAL_CPP_EXTENSION)))
153ifdef bad_cpp_extensions
154    $(call __ndk_info,WARNING: Invalid LOCAL_CPP_EXTENSION values: $(bad_cpp_extensions))
155    LOCAL_CPP_EXTENSION := $(filter $(bad_cpp_extensions),$(LOCAL_CPP_EXTENSIONS))
156endif
157LOCAL_CPP_EXTENSION := $(strip $(LOCAL_CPP_EXTENSION))
158ifeq ($(LOCAL_CPP_EXTENSION),)
159  # Match the default GCC C++ extensions.
160  LOCAL_CPP_EXTENSION := $(default-c++-extensions)
161endif
162LOCAL_RS_EXTENSION := $(default-rs-extensions)
163
164#
165# If LOCAL_ALLOW_UNDEFINED_SYMBOLS is not true, the linker will allow the generation
166# of a binary that uses undefined symbols.
167#
168ifneq ($(LOCAL_ALLOW_UNDEFINED_SYMBOLS),true)
169  LOCAL_LDFLAGS += $($(my)NO_UNDEFINED_LDFLAGS)
170endif
171
172# Toolchain by default disallows generated code running from the heap and stack.
173# If LOCAL_DISABLE_NO_EXECUTE is true, we allow that
174#
175ifeq ($(LOCAL_DISABLE_NO_EXECUTE),true)
176  LOCAL_CFLAGS += $($(my)DISABLE_NO_EXECUTE_CFLAGS)
177  LOCAL_LDFLAGS += $($(my)DISABLE_NO_EXECUTE_LDFLAGS)
178else
179  LOCAL_CFLAGS += $($(my)NO_EXECUTE_CFLAGS)
180  LOCAL_LDFLAGS += $($(my)NO_EXECUTE_LDFLAGS)
181endif
182
183# Toolchain by default provides relro and GOT protections.
184# If LOCAL_DISABLE_RELRO is true, we disable the protections.
185#
186ifeq ($(LOCAL_DISABLE_RELRO),true)
187  LOCAL_LDFLAGS += $($(my)DISABLE_RELRO_LDFLAGS)
188else
189  LOCAL_LDFLAGS += $($(my)RELRO_LDFLAGS)
190endif
191
192# We enable shared text relocation warnings by default. These are not allowed in
193# current versions of Android (android-21 for LP64 ABIs, android-23 for LP32
194# ABIs).
195LOCAL_LDFLAGS += -Wl,--warn-shared-textrel
196
197# We enable fatal linker warnings by default.
198# If LOCAL_DISABLE_FATAL_LINKER_WARNINGS is true, we don't enable this check.
199ifneq ($(LOCAL_DISABLE_FATAL_LINKER_WARNINGS),true)
200  LOCAL_LDFLAGS += -Wl,--fatal-warnings
201endif
202
203# By default, we protect against format string vulnerabilities
204# If LOCAL_DISABLE_FORMAT_STRING_CHECKS is true, we disable the protections.
205ifeq ($(LOCAL_DISABLE_FORMAT_STRING_CHECKS),true)
206  LOCAL_CFLAGS += $($(my)DISABLE_FORMAT_STRING_CFLAGS)
207else
208  LOCAL_CFLAGS += $($(my)FORMAT_STRING_CFLAGS)
209endif
210
211# enable PIE for executable beyond certain API level, unless "-static"
212ifneq (,$(filter true,$(NDK_APP_PIE) $(TARGET_PIE)))
213  ifeq ($(call module-get-class,$(LOCAL_MODULE)),EXECUTABLE)
214    ifeq (,$(filter -static,$(TARGET_LDFLAGS) $(LOCAL_LDFLAGS) $(NDK_APP_LDFLAGS)))
215      LOCAL_CFLAGS += -fPIE
216      LOCAL_LDFLAGS += -fPIE -pie
217    endif
218  endif
219endif
220
221#
222# The original Android build system allows you to use the .arm prefix
223# to a source file name to indicate that it should be defined in either
224# 'thumb' or 'arm' mode, depending on the value of LOCAL_ARM_MODE
225#
226# First, check LOCAL_ARM_MODE, it should be empty, 'thumb' or 'arm'
227# We make the default 'thumb'
228#
229LOCAL_ARM_MODE := $(strip $(LOCAL_ARM_MODE))
230ifdef LOCAL_ARM_MODE
231  ifneq ($(words $(LOCAL_ARM_MODE)),1)
232      $(call __ndk_info,   LOCAL_ARM_MODE in $(LOCAL_MAKEFILE) must be one word, not '$(LOCAL_ARM_MODE)')
233      $(call __ndk_error, Aborting)
234  endif
235  # check that LOCAL_ARM_MODE is defined to either 'arm' or 'thumb'
236  $(if $(filter-out thumb arm, $(LOCAL_ARM_MODE)),\
237      $(call __ndk_info,   LOCAL_ARM_MODE must be defined to either 'arm' or 'thumb' in $(LOCAL_MAKEFILE) not '$(LOCAL_ARM_MODE)')\
238      $(call __ndk_error, Aborting)\
239  )
240  my_link_arm_mode := $(LOCAL_ARM_MODE)
241else
242  my_link_arm_mode := thumb
243endif
244
245# As a special case, the original Android build system
246# allows one to specify that certain source files can be
247# forced to build in ARM mode by using a '.arm' suffix
248# after the extension, e.g.
249#
250#  LOCAL_SRC_FILES := foo.c.arm
251#
252# to build source file $(LOCAL_PATH)/foo.c as ARM
253#
254
255$(call clear-all-src-tags)
256
257# As a special extension, the NDK also supports the .neon extension suffix
258# to indicate that a single file can be compiled with ARM NEON support
259# We must support both foo.c.neon and foo.c.arm.neon here
260#
261# Also, if LOCAL_ARM_NEON is set to 'true', force Neon mode for all source
262# files
263#
264
265neon_sources  := $(filter %.neon,$(LOCAL_SRC_FILES))
266neon_sources  := $(neon_sources:%.neon=%)
267
268LOCAL_ARM_NEON := $(strip $(LOCAL_ARM_NEON))
269ifdef LOCAL_ARM_NEON
270  $(if $(filter-out true false,$(LOCAL_ARM_NEON)),\
271    $(call __ndk_info,LOCAL_ARM_NEON must be defined either to 'true' or 'false' in $(LOCAL_MAKEFILE), not '$(LOCAL_ARM_NEON)')\
272    $(call __ndk_error,Aborting) \
273  )
274endif
275ifeq ($(LOCAL_ARM_NEON),true)
276  neon_sources += $(LOCAL_SRC_FILES:%.neon=%)
277  # tag the precompiled header with 'neon' tag if it exists
278  ifneq (,$(LOCAL_PCH))
279    $(call tag-src-files,$(LOCAL_PCH),neon)
280  endif
281endif
282
283neon_sources := $(strip $(neon_sources))
284ifdef neon_sources
285  ifeq ($(filter $(TARGET_ARCH_ABI), armeabi-v7a armeabi-v7a-hard arm64-v8a x86 x86_64),)
286    $(call __ndk_info,NEON support is only possible for armeabi-v7a ABI, its variant armeabi-v7a-hard, and arm64-v8a, x86 and x86_64 ABIs)
287    $(call __ndk_info,Please add checks against TARGET_ARCH_ABI in $(LOCAL_MAKEFILE))
288    $(call __ndk_error,Aborting)
289  endif
290  $(call tag-src-files,$(neon_sources:%.arm=%),neon)
291endif
292
293LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.neon=%)
294
295# strip the .arm suffix from LOCAL_SRC_FILES
296# and tag the relevant sources with the 'arm' tag
297#
298arm_sources     := $(filter %.arm,$(LOCAL_SRC_FILES))
299arm_sources     := $(arm_sources:%.arm=%)
300thumb_sources   := $(filter-out %.arm,$(LOCAL_SRC_FILES))
301LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.arm=%)
302
303ifeq ($(LOCAL_ARM_MODE),arm)
304    arm_sources := $(LOCAL_SRC_FILES)
305    # tag the precompiled header with 'arm' tag if it exists
306    ifneq (,$(LOCAL_PCH))
307        $(call tag-src-files,$(LOCAL_PCH),arm)
308    endif
309else
310# For arm, all sources are compiled in thumb mode by default in release mode.
311# Linker should behave similarly
312ifneq ($(filter armeabi%, $(TARGET_ARCH_ABI)),)
313ifneq ($(APP_OPTIM),debug)
314    LOCAL_LDFLAGS += -mthumb
315endif
316endif
317endif
318ifeq ($(LOCAL_ARM_MODE),thumb)
319    arm_sources := $(empty)
320endif
321$(call tag-src-files,$(arm_sources),arm)
322
323# tag debug if APP_OPTIM is 'debug'
324#
325ifeq ($(APP_OPTIM),debug)
326    $(call tag-src-files,$(LOCAL_SRC_FILES),debug)
327    ifneq (,$(LOCAL_PCH))
328        $(call tag-src-files,$(LOCAL_PCH),debug)
329    endif
330endif
331
332# add PCH to LOCAL_SRC_FILES so that TARGET-process-src-files-tags could process it
333ifneq (,$(LOCAL_PCH))
334    LOCAL_SRC_FILES += $(LOCAL_PCH)
335endif
336
337# Process all source file tags to determine toolchain-specific
338# target compiler flags, and text.
339#
340$(call TARGET-process-src-files-tags)
341
342# now remove PCH from LOCAL_SRC_FILES to prevent getting NDK warning about
343# unsupported source file extensions
344ifneq (,$(LOCAL_PCH))
345    LOCAL_SRC_FILES := $(filter-out $(LOCAL_PCH),$(LOCAL_SRC_FILES))
346endif
347
348# only call dump-src-file-tags during debugging
349#$(dump-src-file-tags)
350
351LOCAL_DEPENDENCY_DIRS :=
352
353# all_source_patterns contains the list of filename patterns that correspond
354# to source files recognized by our build system
355ifneq ($(filter x86 x86_64, $(TARGET_ARCH_ABI)),)
356all_source_extensions := .c .s .S .asm $(LOCAL_CPP_EXTENSION) $(LOCAL_RS_EXTENSION)
357else
358all_source_extensions := .c .s .S $(LOCAL_CPP_EXTENSION) $(LOCAL_RS_EXTENSION)
359endif
360all_source_patterns   := $(foreach _ext,$(all_source_extensions),%$(_ext))
361all_cpp_patterns      := $(foreach _ext,$(LOCAL_CPP_EXTENSION),%$(_ext))
362all_rs_patterns       := $(foreach _ext,$(LOCAL_RS_EXTENSION),%$(_ext))
363
364unknown_sources := $(strip $(filter-out $(all_source_patterns),$(LOCAL_SRC_FILES)))
365ifdef unknown_sources
366    $(call __ndk_info,WARNING: Unsupported source file extensions in $(LOCAL_MAKEFILE) for module $(LOCAL_MODULE))
367    $(call __ndk_info,  $(unknown_sources))
368endif
369
370# LOCAL_OBJECTS will list all object files corresponding to the sources
371# listed in LOCAL_SRC_FILES, in the *same* order.
372#
373LOCAL_OBJECTS := $(LOCAL_SRC_FILES)
374$(foreach _ext,$(all_source_extensions),\
375    $(eval LOCAL_OBJECTS := $$(LOCAL_OBJECTS:%$(_ext)=%$$(TARGET_OBJ_EXTENSION)))\
376)
377LOCAL_OBJECTS := $(filter %$(TARGET_OBJ_EXTENSION),$(LOCAL_OBJECTS))
378LOCAL_OBJECTS := $(subst ../,__/,$(LOCAL_OBJECTS))
379LOCAL_OBJECTS := $(subst :,_,$(LOCAL_OBJECTS))
380LOCAL_OBJECTS := $(foreach _obj,$(LOCAL_OBJECTS),$(LOCAL_OBJS_DIR)/$(_obj))
381
382LOCAL_RS_OBJECTS := $(filter $(all_rs_patterns),$(LOCAL_SRC_FILES))
383$(foreach _ext,$(LOCAL_RS_EXTENSION),\
384    $(eval LOCAL_RS_OBJECTS := $$(LOCAL_RS_OBJECTS:%$(_ext)=%$$(TARGET_OBJ_EXTENSION)))\
385)
386LOCAL_RS_OBJECTS := $(filter %$(TARGET_OBJ_EXTENSION),$(LOCAL_RS_OBJECTS))
387LOCAL_RS_OBJECTS := $(subst ../,__/,$(LOCAL_RS_OBJECTS))
388LOCAL_RS_OBJECTS := $(subst :,_,$(LOCAL_RS_OBJECTS))
389LOCAL_RS_OBJECTS := $(foreach _obj,$(LOCAL_RS_OBJECTS),$(LOCAL_OBJS_DIR)/$(_obj))
390
391# If the module has any kind of C++ features, enable them in LOCAL_CPPFLAGS
392#
393ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti))
394    LOCAL_CPPFLAGS += -frtti
395endif
396ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),exceptions))
397    LOCAL_CPPFLAGS += -fexceptions
398endif
399
400# If we're using the 'system' STL and use rtti or exceptions, then
401# automatically link against the GNU libsupc++ for now.
402#
403ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti exceptions))
404    ifeq (system,$(NDK_APP_STL))
405      LOCAL_LDLIBS := $(LOCAL_LDLIBS) $(call host-path,$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/4.9/libs/$(TARGET_ARCH_ABI)/libsupc++$(TARGET_LIB_EXTENSION))
406    endif
407endif
408
409# Set include patch for renderscript
410
411
412ifneq ($(LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE),)
413    LOCAL_RENDERSCRIPT_INCLUDES := $(LOCAL_RENDERSCRIPT_INCLUDES_OVERRIDE)
414else
415    LOCAL_RENDERSCRIPT_INCLUDES := \
416        $(RENDERSCRIPT_TOOLCHAIN_HEADER) \
417        $(LOCAL_RENDERSCRIPT_INCLUDES)
418endif
419
420RS_COMPAT :=
421ifneq ($(call module-is-shared-library,$(LOCAL_MODULE)),)
422    RS_COMPAT := true
423endif
424
425
426# Build PCH
427
428get-pch-name = $(strip \
429    $(subst ../,__/,\
430        $(eval __pch := $1)\
431        $(eval __pch := $(__pch:%.h=%.precompiled.h))\
432        $(__pch)\
433    ))
434
435ifneq (,$(LOCAL_PCH))
436    # Build PCH into obj directory
437    LOCAL_BUILT_PCH := $(call get-pch-name,$(LOCAL_PCH))
438
439    # Build PCH
440    $(call compile-cpp-source,$(LOCAL_PCH),$(LOCAL_BUILT_PCH).gch)
441
442    # All obj files are dependent on the PCH
443    $(foreach src,$(filter $(all_cpp_patterns),$(LOCAL_SRC_FILES)),\
444        $(eval $(LOCAL_OBJS_DIR)/$(call get-object-name,$(src)) : $(LOCAL_OBJS_DIR)/$(LOCAL_BUILT_PCH).gch)\
445    )
446
447    # Files from now on build with PCH
448    LOCAL_CPPFLAGS += -Winvalid-pch -include $(LOCAL_OBJS_DIR)/$(LOCAL_BUILT_PCH)
449
450    # Insert PCH dir at beginning of include search path
451    LOCAL_C_INCLUDES := \
452        $(LOCAL_OBJS_DIR) \
453        $(LOCAL_C_INCLUDES)
454endif
455
456# Build the sources to object files
457#
458
459$(foreach src,$(filter %.c,$(LOCAL_SRC_FILES)), $(call compile-c-source,$(src),$(call get-object-name,$(src))))
460$(foreach src,$(filter %.S %.s,$(LOCAL_SRC_FILES)), $(call compile-s-source,$(src),$(call get-object-name,$(src))))
461$(foreach src,$(filter $(all_cpp_patterns),$(LOCAL_SRC_FILES)),\
462    $(call compile-cpp-source,$(src),$(call get-object-name,$(src)))\
463)
464
465$(foreach src,$(filter $(all_rs_patterns),$(LOCAL_SRC_FILES)),\
466    $(call compile-rs-source,$(src),$(call get-rs-scriptc-name,$(src)),$(call get-rs-bc-name,$(src)),$(call get-rs-so-name,$(src)),$(call get-object-name,$(src)),$(RS_COMPAT))\
467)
468
469ifneq ($(filter x86 x86_64, $(TARGET_ARCH_ABI)),)
470$(foreach src,$(filter %.asm,$(LOCAL_SRC_FILES)), $(call compile-asm-source,$(src),$(call get-object-name,$(src))))
471endif
472
473#
474# The compile-xxx-source calls updated LOCAL_OBJECTS and LOCAL_DEPENDENCY_DIRS
475#
476ALL_DEPENDENCY_DIRS += $(sort $(LOCAL_DEPENDENCY_DIRS))
477CLEAN_OBJS_DIRS     += $(LOCAL_OBJS_DIR)
478
479#
480# Handle the static and shared libraries this module depends on
481#
482
483# If LOCAL_LDLIBS contains anything like -l<library> then
484# prepend a -L$(SYSROOT_LINK)/usr/lib to it to ensure that the linker
485# looks in the right location
486#
487ifneq ($(filter -l%,$(LOCAL_LDLIBS)),)
488    LOCAL_LDLIBS := -L$(call host-path,$(SYSROOT_LINK)/usr/lib) $(LOCAL_LDLIBS)
489    ifneq ($(filter x86_64 mips64,$(TARGET_ARCH_ABI)),)
490        LOCAL_LDLIBS := -L$(call host-path,$(SYSROOT_LINK)/usr/lib64) $(LOCAL_LDLIBS)
491    endif
492endif
493
494my_ldflags := $(TARGET_LDFLAGS) $(LOCAL_LDFLAGS) $(NDK_APP_LDFLAGS)
495ifneq ($(filter armeabi%,$(TARGET_ARCH_ABI)),)
496    my_ldflags += $(TARGET_$(my_link_arm_mode)_LDFLAGS)
497endif
498
499# When LOCAL_SHORT_COMMANDS is defined to 'true' we are going to write the
500# list of all object files and/or static/shared libraries that appear on the
501# command line to a file, then use the @<listfile> syntax to invoke it.
502#
503# This allows us to link or archive a huge number of stuff even on Windows
504# with its puny 8192 max character limit on its command-line.
505#
506LOCAL_SHORT_COMMANDS := $(strip $(LOCAL_SHORT_COMMANDS))
507ifndef LOCAL_SHORT_COMMANDS
508    LOCAL_SHORT_COMMANDS := $(strip $(NDK_APP_SHORT_COMMANDS))
509endif
510
511$(call generate-file-dir,$(LOCAL_BUILT_MODULE))
512
513$(LOCAL_BUILT_MODULE): PRIVATE_OBJECTS := $(LOCAL_OBJECTS)
514$(LOCAL_BUILT_MODULE): PRIVATE_LIBGCC := $(TARGET_LIBGCC)
515
516$(LOCAL_BUILT_MODULE): PRIVATE_LD := $(TARGET_LD)
517$(LOCAL_BUILT_MODULE): PRIVATE_LDFLAGS := $(my_ldflags)
518$(LOCAL_BUILT_MODULE): PRIVATE_LDLIBS  := $(LOCAL_LDLIBS) $(TARGET_LDLIBS)
519
520$(LOCAL_BUILT_MODULE): PRIVATE_NAME := $(notdir $(LOCAL_BUILT_MODULE))
521$(LOCAL_BUILT_MODULE): PRIVATE_CXX := $(TARGET_CXX)
522$(LOCAL_BUILT_MODULE): PRIVATE_CC := $(TARGET_CC)
523$(LOCAL_BUILT_MODULE): PRIVATE_SYSROOT_LINK := $(SYSROOT_LINK)
524
525ifeq ($(call module-get-class,$(LOCAL_MODULE)),STATIC_LIBRARY)
526
527#
528# This is a static library module, things are very easy. We only need
529# to build the object files and archive them with 'ar'. Note that module
530# dependencies can be ignored here, i.e. if the module depends on other
531# static or shared libraries, there is no need to actually build them
532# before, so don't add Make dependencies to them.
533#
534# In other words, consider the following graph:
535#
536#     libfoo.so -> libA.a ->libB.a
537#
538# then libA.a and libB.a can be built in parallel, only linking libfoo.so
539# depends on their completion.
540#
541
542ar_objects := $(call host-path,$(LOCAL_OBJECTS))
543
544ifeq ($(LOCAL_SHORT_COMMANDS),true)
545    $(call ndk_log,Building static library module '$(LOCAL_MODULE)' with linker list file)
546    ar_list_file := $(LOCAL_OBJS_DIR)/archiver.list
547    $(call generate-list-file,$(ar_objects),$(ar_list_file))
548    ar_objects   := @$(call host-path,$(ar_list_file))
549    $(LOCAL_BUILT_MODULE): $(ar_list_file)
550endif
551
552# Compute 'ar' flags. Thin archives simply require 'T' here.
553ar_flags := $(TARGET_ARFLAGS)
554ifeq (true,$(thin_archive))
555    $(call ndk_log,$(TARGET_ARCH_ABI):Building static library '$(LOCAL_MODULE)' as thin archive)
556    ar_flags := $(ar_flags)T
557endif
558
559$(LOCAL_BUILT_MODULE): PRIVATE_ABI := $(TARGET_ARCH_ABI)
560$(LOCAL_BUILT_MODULE): PRIVATE_AR := $(TARGET_AR) $(ar_flags)
561$(LOCAL_BUILT_MODULE): PRIVATE_AR_OBJECTS := $(ar_objects)
562$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_STATIC_LIB := $(cmd-build-static-library)
563
564$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
565	$(call host-echo-build-step,$(PRIVATE_ABI),StaticLibrary) "$(PRIVATE_NAME)"
566	$(hide) $(call host-rm,$@)
567	$(hide) $(PRIVATE_BUILD_STATIC_LIB)
568
569ALL_STATIC_LIBRARIES += $(LOCAL_BUILT_MODULE)
570
571endif
572
573ifneq (,$(filter SHARED_LIBRARY EXECUTABLE,$(call module-get-class,$(LOCAL_MODULE))))
574
575#
576# This is a shared library or an executable, so computing dependencies properly is
577# crucial. The general rule to apply is the following:
578#
579#   - collect the list of all static libraries that need to be part
580#     of the link, and in the right order. To do so, get the transitive
581#     closure of LOCAL_STATIC_LIBRARIES and LOCAL_WHOLE_STATIC_LIBRARIES
582#     and ensure they are ordered topologically.
583#
584#  - collect the list of all shared libraries that need to be part of
585#    the link. This is the transitive closure of the list of
586#    LOCAL_SHARED_LIBRARIES for the module and all its dependent static
587#    libraries identified in the step above. Of course, need to be
588#    ordered topologically too.
589#
590#  - add Make dependencies to ensure that all these libs are built
591#    before the module itself too.
592#
593# A few quick examples:
594#
595#    main.exe -> libA.a -> libB.a -> libfoo.so -> libC.a
596#
597#      static_libs(main.exe) = libA.a libB.a  (i.e. no libC.a)
598#      shared_libs(main.exe) = libfoo.so
599#      static_libs(libfoo.so) = libC.a
600#
601#    main.exe -> libA.a ---> libB.a
602#                  |           ^
603#                  v           |
604#                libC.a  ------
605#
606#      static_libs(main.exe) = libA.a libC.a libB.a
607#             (i.e. libB.a must appear after all libraries that depend on it).
608#
609all_libs := $(call module-get-link-libs,$(LOCAL_MODULE))
610shared_libs := $(call module-filter-shared-libraries,$(all_libs))
611static_libs := $(call module-filter-static-libraries,$(all_libs))
612whole_static_libs := $(call module-extract-whole-static-libs,$(LOCAL_MODULE),$(static_libs))
613static_libs := $(filter-out $(whole_static_libs),$(static_libs))
614
615$(call -ndk-mod-debug,module $(LOCAL_MODULE) [$(LOCAL_BUILT_MODULE)])
616$(call -ndk-mod-debug,.  all_libs='$(all_libs)')
617$(call -ndk-mod-debug,.  shared_libs='$(shared_libs)')
618$(call -ndk-mod-debug,.  static_libs='$(static_libs)')
619$(call -ndk-mod-debug,.  whole_static_libs='$(whole_static_libs)')
620
621shared_libs       := $(call map,module-get-built,$(shared_libs))\
622                     $(TARGET_PREBUILT_SHARED_LIBRARIES)
623static_libs       := $(call map,module-get-built,$(static_libs))
624whole_static_libs := $(call map,module-get-built,$(whole_static_libs))
625
626$(call -ndk-mod-debug,.  built_shared_libs='$(shared_libs)')
627$(call -ndk-mod-debug,.  built_static_libs='$(static_libs)')
628$(call -ndk-mod-debug,.  built_whole_static_libs='$(whole_static_libs)')
629
630# The list of object/static/shared libraries passed to the linker when
631# building shared libraries and executables. order is important.
632#
633# Cannot use immediate evaluation because PRIVATE_LIBGCC may not be defined at this point.
634linker_objects_and_libraries = $(strip $(call TARGET-get-linker-objects-and-libraries,\
635    $(LOCAL_OBJECTS), \
636    $(static_libs), \
637    $(whole_static_libs), \
638    $(shared_libs)))
639
640ifeq ($(LOCAL_SHORT_COMMANDS),true)
641    $(call ndk_log,Building ELF binary module '$(LOCAL_MODULE)' with linker list file)
642    linker_options   := $(linker_objects_and_libraries)
643    linker_list_file := $(LOCAL_OBJS_DIR)/linker.list
644    linker_objects_and_libraries := @$(call host-path,$(linker_list_file))
645    $(call generate-list-file,$(linker_options),$(linker_list_file))
646    $(LOCAL_BUILT_MODULE): $(linker_list_file)
647endif
648
649$(LOCAL_BUILT_MODULE): $(shared_libs) $(static_libs) $(whole_static_libs)
650$(LOCAL_BUILT_MODULE): PRIVATE_ABI := $(TARGET_ARCH_ABI)
651$(LOCAL_BUILT_MODULE): PRIVATE_LINKER_OBJECTS_AND_LIBRARIES := $(linker_objects_and_libraries)
652$(LOCAL_BUILT_MODULE): PRIVATE_STATIC_LIBRARIES := $(static_libs)
653$(LOCAL_BUILT_MODULE): PRIVATE_WHOLE_STATIC_LIBRARIES := $(whole_static_libs)
654$(LOCAL_BUILT_MODULE): PRIVATE_SHARED_LIBRARIES := $(shared_libs)
655
656endif
657
658#
659# If this is a shared library module
660#
661ifeq ($(call module-get-class,$(LOCAL_MODULE)),SHARED_LIBRARY)
662$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_SHARED_LIB := $(cmd-build-shared-library)
663$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
664	$(call host-echo-build-step,$(PRIVATE_ABI),SharedLibrary) "$(PRIVATE_NAME)"
665	$(hide) $(PRIVATE_BUILD_SHARED_LIB)
666
667ALL_SHARED_LIBRARIES += $(LOCAL_BUILT_MODULE)
668endif
669
670#
671# If this is an executable module
672#
673ifeq ($(call module-get-class,$(LOCAL_MODULE)),EXECUTABLE)
674$(LOCAL_BUILT_MODULE): PRIVATE_ABI := $(TARGET_ARCH_ABI)
675$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_EXECUTABLE := $(cmd-build-executable)
676$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
677	$(call host-echo-build-step,$(PRIVATE_ABI),Executable) "$(PRIVATE_NAME)"
678	$(hide) $(PRIVATE_BUILD_EXECUTABLE)
679
680ALL_EXECUTABLES += $(LOCAL_BUILT_MODULE)
681endif
682
683#
684# If this is a copyable prebuilt module
685#
686ifeq ($(call module-is-copyable,$(LOCAL_MODULE)),$(true))
687$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
688	$(call host-echo-build-step,$(PRIVATE_ABI),Prebuilt) "$(PRIVATE_NAME) <= $(call pretty-dir,$(dir $<))"
689	$(hide) $(call host-cp,$<,$@)
690endif
691
692#
693# If this is an installable module
694#
695ifeq ($(call module-is-installable,$(LOCAL_MODULE)),$(true))
696$(LOCAL_INSTALLED): PRIVATE_ABI         := $(TARGET_ARCH_ABI)
697$(LOCAL_INSTALLED): PRIVATE_NAME        := $(notdir $(LOCAL_BUILT_MODULE))
698$(LOCAL_INSTALLED): PRIVATE_SRC         := $(LOCAL_BUILT_MODULE)
699$(LOCAL_INSTALLED): PRIVATE_DST_DIR     := $(NDK_APP_DST_DIR)
700$(LOCAL_INSTALLED): PRIVATE_DST         := $(LOCAL_INSTALLED)
701$(LOCAL_INSTALLED): PRIVATE_STRIP       := $(TARGET_STRIP)
702$(LOCAL_INSTALLED): PRIVATE_STRIP_CMD   := $(call cmd-strip, $(PRIVATE_DST))
703$(LOCAL_INSTALLED): PRIVATE_OBJCOPY     := $(TARGET_OBJCOPY)
704$(LOCAL_INSTALLED): PRIVATE_OBJCOPY_CMD := $(call cmd-add-gnu-debuglink, $(PRIVATE_DST), $(PRIVATE_SRC))
705
706$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
707	$(call host-echo-build-step,$(PRIVATE_ABI),Install) "$(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
708	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
709	$(hide) $(PRIVATE_STRIP_CMD)
710
711#$(hide) $(PRIVATE_OBJCOPY_CMD)
712
713$(call generate-file-dir,$(LOCAL_INSTALLED))
714
715endif
716