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  # Match the default GCC C++ extensions.
84  LOCAL_CPP_EXTENSION := .cc .cp .cxx .cpp .CPP .c++ .C
85else
86endif
87
88#
89# If LOCAL_ALLOW_UNDEFINED_SYMBOLS is not true, the linker will allow the generation
90# of a binary that uses undefined symbols.
91#
92ifneq ($(LOCAL_ALLOW_UNDEFINED_SYMBOLS),true)
93  LOCAL_LDFLAGS += $($(my)NO_UNDEFINED_LDFLAGS)
94endif
95
96# Toolchain by default disallows generated code running from the heap and stack.
97# If LOCAL_DISABLE_NO_EXECUTE is true, we allow that
98#
99ifeq ($(LOCAL_DISABLE_NO_EXECUTE),true)
100  LOCAL_CFLAGS += $($(my)DISABLE_NO_EXECUTE_CFLAGS)
101  LOCAL_LDFLAGS += $($(my)DISABLE_NO_EXECUTE_LDFLAGS)
102else
103  LOCAL_CFLAGS += $($(my)NO_EXECUTE_CFLAGS)
104  LOCAL_LDFLAGS += $($(my)NO_EXECUTE_LDFLAGS)
105endif
106
107# Toolchain by default provides relro and GOT protections.
108# If LOCAL_DISABLE_RELRO is true, we disable the protections.
109#
110ifeq ($(LOCAL_DISABLE_RELRO),true)
111  LOCAL_LDFLAGS += $($(my)DISABLE_RELRO_LDFLAGS)
112else
113  LOCAL_LDFLAGS += $($(my)RELRO_LDFLAGS)
114endif
115
116#
117# The original Android build system allows you to use the .arm prefix
118# to a source file name to indicate that it should be defined in either
119# 'thumb' or 'arm' mode, depending on the value of LOCAL_ARM_MODE
120#
121# First, check LOCAL_ARM_MODE, it should be empty, 'thumb' or 'arm'
122# We make the default 'thumb'
123#
124LOCAL_ARM_MODE := $(strip $(LOCAL_ARM_MODE))
125ifdef LOCAL_ARM_MODE
126  ifneq ($(words $(LOCAL_ARM_MODE)),1)
127      $(call __ndk_info,   LOCAL_ARM_MODE in $(LOCAL_MAKEFILE) must be one word, not '$(LOCAL_ARM_MODE)')
128      $(call __ndk_error, Aborting)
129  endif
130  # check that LOCAL_ARM_MODE is defined to either 'arm' or 'thumb'
131  $(if $(filter-out thumb arm, $(LOCAL_ARM_MODE)),\
132      $(call __ndk_info,   LOCAL_ARM_MODE must be defined to either 'arm' or 'thumb' in $(LOCAL_MAKEFILE) not '$(LOCAL_ARM_MODE)')\
133      $(call __ndk_error, Aborting)\
134  )
135endif
136
137# As a special case, the original Android build system
138# allows one to specify that certain source files can be
139# forced to build in ARM mode by using a '.arm' suffix
140# after the extension, e.g.
141#
142#  LOCAL_SRC_FILES := foo.c.arm
143#
144# to build source file $(LOCAL_PATH)/foo.c as ARM
145#
146
147$(call clear-all-src-tags)
148
149# As a special extension, the NDK also supports the .neon extension suffix
150# to indicate that a single file can be compiled with ARM NEON support
151# We must support both foo.c.neon and foo.c.arm.neon here
152#
153# Also, if LOCAL_ARM_NEON is set to 'true', force Neon mode for all source
154# files
155#
156
157neon_sources  := $(filter %.neon,$(LOCAL_SRC_FILES))
158neon_sources  := $(neon_sources:%.neon=%)
159
160LOCAL_ARM_NEON := $(strip $(LOCAL_ARM_NEON))
161ifdef LOCAL_ARM_NEON
162  $(if $(filter-out true false,$(LOCAL_ARM_NEON)),\
163    $(call __ndk_info,LOCAL_ARM_NEON must be defined either to 'true' or 'false' in $(LOCAL_MAKEFILE), not '$(LOCAL_ARM_NEON)')\
164    $(call __ndk_error,Aborting) \
165  )
166endif
167ifeq ($(LOCAL_ARM_NEON),true)
168  neon_sources += $(LOCAL_SRC_FILES:%.neon=%)
169endif
170
171neon_sources := $(strip $(neon_sources))
172ifdef neon_sources
173  ifneq ($(TARGET_ARCH_ABI),armeabi-v7a)
174    $(call __ndk_info,NEON support is only possible for armeabi-v7a ABI)
175    $(call __ndk_info,Please add checks against TARGET_ARCH_ABI in $(LOCAL_MAKEFILE))
176    $(call __ndk_error,Aborting)
177  endif
178  $(call tag-src-files,$(neon_sources:%.arm=%),neon)
179endif
180
181LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.neon=%)
182
183# strip the .arm suffix from LOCAL_SRC_FILES
184# and tag the relevant sources with the 'arm' tag
185#
186arm_sources     := $(filter %.arm,$(LOCAL_SRC_FILES))
187arm_sources     := $(arm_sources:%.arm=%)
188thumb_sources   := $(filter-out %.arm,$(LOCAL_SRC_FILES))
189LOCAL_SRC_FILES := $(LOCAL_SRC_FILES:%.arm=%)
190
191ifeq ($(LOCAL_ARM_MODE),arm)
192    arm_sources := $(LOCAL_SRC_FILES)
193endif
194ifeq ($(LOCAL_ARM_MODE),thumb)
195    arm_sources := $(empty)
196endif
197$(call tag-src-files,$(arm_sources),arm)
198
199# Process all source file tags to determine toolchain-specific
200# target compiler flags, and text.
201#
202$(call TARGET-process-src-files-tags)
203
204# only call dump-src-file-tags during debugging
205#$(dump-src-file-tags)
206
207LOCAL_DEPENDENCY_DIRS :=
208
209# all_source_patterns contains the list of filename patterns that correspond
210# to source files recognized by our build system
211all_source_extensions := .c .s .S $(LOCAL_CPP_EXTENSION)
212all_source_patterns   := $(foreach _ext,$(all_source_extensions),%$(_ext))
213all_cpp_patterns      := $(foreach _ext,$(LOCAL_CPP_EXTENSION),%$(_ext))
214
215unknown_sources := $(strip $(filter-out $(all_source_patterns),$(LOCAL_SRC_FILES)))
216ifdef unknown_sources
217    $(call __ndk_info,WARNING: Unsupported source file extensions in $(LOCAL_MAKEFILE) for module $(LOCAL_MODULE))
218    $(call __ndk_info,  $(unknown_sources))
219endif
220
221# LOCAL_OBJECTS will list all object files corresponding to the sources
222# listed in LOCAL_SRC_FILES, in the *same* order.
223#
224LOCAL_OBJECTS := $(LOCAL_SRC_FILES)
225$(foreach _ext,$(all_source_extensions),\
226    $(eval LOCAL_OBJECTS := $$(LOCAL_OBJECTS:%$(_ext)=%.o))\
227)
228LOCAL_OBJECTS := $(filter %.o,$(LOCAL_OBJECTS))
229LOCAL_OBJECTS := $(subst ../,__/,$(LOCAL_OBJECTS))
230LOCAL_OBJECTS := $(foreach _obj,$(LOCAL_OBJECTS),$(LOCAL_OBJS_DIR)/$(_obj))
231
232# If the module has any kind of C++ features, enable them in LOCAL_CPPFLAGS
233#
234ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti))
235    LOCAL_CPPFLAGS += -frtti
236endif
237ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),exceptions))
238    LOCAL_CPPFLAGS += -fexceptions
239endif
240
241# If we're using the 'system' STL and use rtti or exceptions, then
242# automatically link against the GNU libsupc++ for now.
243#
244ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti exceptions))
245    ifeq (system,$(NDK_APP_STL))
246      LOCAL_LDLIBS := $(LOCAL_LDLIBS) $(call host-path,$(NDK_ROOT)/sources/cxx-stl/gnu-libstdc++/$(TOOLCHAIN_VERSION)/libs/$(TARGET_ARCH_ABI)/libsupc++.a)
247    endif
248endif
249
250# Build the sources to object files
251#
252
253$(foreach src,$(filter %.c,$(LOCAL_SRC_FILES)), $(call compile-c-source,$(src),$(call get-object-name,$(src))))
254$(foreach src,$(filter %.S %.s,$(LOCAL_SRC_FILES)), $(call compile-s-source,$(src),$(call get-object-name,$(src))))
255
256$(foreach src,$(filter $(all_cpp_patterns),$(LOCAL_SRC_FILES)),\
257    $(call compile-cpp-source,$(src),$(call get-object-name,$(src)))\
258)
259
260#
261# The compile-xxx-source calls updated LOCAL_OBJECTS and LOCAL_DEPENDENCY_DIRS
262#
263ALL_DEPENDENCY_DIRS += $(sort $(LOCAL_DEPENDENCY_DIRS))
264CLEAN_OBJS_DIRS     += $(LOCAL_OBJS_DIR)
265
266#
267# Handle the static and shared libraries this module depends on
268#
269LOCAL_STATIC_LIBRARIES       := $(call strip-lib-prefix,$(LOCAL_STATIC_LIBRARIES))
270LOCAL_WHOLE_STATIC_LIBRARIES := $(call strip-lib-prefix,$(LOCAL_WHOLE_STATIC_LIBRARIES))
271LOCAL_SHARED_LIBRARIES       := $(call strip-lib-prefix,$(LOCAL_SHARED_LIBRARIES))
272
273# Transitive closure of static libraries
274LOCAL_STATIC_LIBRARIES       := $(call module-get-depends,$(LOCAL_STATIC_LIBRARIES),STATIC_LIBRARIES)
275LOCAL_WHOLE_STATIC_LIBRARIES := $(call module-get-depends,$(LOCAL_WHOLE_STATIC_LIBRARIES),WHOLE_STATIC_LIBRARIES)
276
277static_libraries       := $(call map,module-get-built,$(LOCAL_STATIC_LIBRARIES))
278whole_static_libraries := $(call map,module-get-built,$(LOCAL_WHOLE_STATIC_LIBRARIES))
279
280shared_libraries := $(call map,module-get-built,$(LOCAL_SHARED_LIBRARIES))\
281                    $(TARGET_PREBUILT_SHARED_LIBRARIES)
282
283$(LOCAL_BUILT_MODULE): $(static_libraries) $(whole_static_libraries) $(shared_libraries)
284
285# If LOCAL_LDLIBS contains anything like -l<library> then
286# prepend a -L$(SYSROOT)/usr/lib to it to ensure that the linker
287# looks in the right location
288#
289ifneq ($(filter -l%,$(LOCAL_LDLIBS)),)
290    LOCAL_LDLIBS := -L$(call host-path,$(SYSROOT)/usr/lib) $(LOCAL_LDLIBS)
291endif
292
293# The list of object/static/shared libraries passed to the linker when
294# building shared libraries and executables. order is important.
295#
296linker_objects_and_libraries := $(strip $(call TARGET-get-linker-objects-and-libraries,\
297    $(LOCAL_OBJECTS), \
298    $(static_libraries), \
299    $(whole_static_libraries), \
300    $(shared_libraries)))
301
302# The list of object files sent to 'ar' when building static libraries
303#
304ar_objects := $(call host-path,$(LOCAL_OBJECTS))
305
306# When LOCAL_SHORT_COMMANDS is defined to 'true' we are going to write the
307# list of all object files and/or static/shared libraries that appear on the
308# command line to a file, then use the @<listfile> syntax to invoke it.
309#
310# This allows us to link or archive a huge number of stuff even on Windows
311# with its puny 8192 max character limit on its command-line.
312#
313LOCAL_SHORT_COMMANDS := $(strip $(LOCAL_SHORT_COMMANDS))
314ifndef LOCAL_SHORT_COMMANDS
315    LOCAL_SHORT_COMMANDS := $(strip $(NDK_APP_SHORT_COMMANDS))
316endif
317ifeq ($(LOCAL_SHORT_COMMANDS),true)
318    # For static and whole static libraries
319    ifneq (,$(filter STATIC_LIBRARY WHOLE_STATIC_LIBRARY,$(call module-get-class,$(LOCAL_MODULE))))
320        $(call ndk_log,Building static library module '$(LOCAL_MODULE)' with linker list file)
321        ar_options   := $(ar_objects)
322        ar_list_file := $(LOCAL_OBJS_DIR)/archiver.list
323        ar_objects   := @$(call host-path,$(ar_list_file))
324        $(call generate-list-file,$(ar_options),$(ar_list_file))
325
326        $(LOCAL_BUILT_MODULE): $(ar_list_file)
327    endif
328
329    # For shared libraries and executables
330    ifneq (,$(filter SHARED_LIBRARY EXECUTABLE,$(call module-get-class,$(LOCAL_MODULE))))
331        $(call ndk_log,Building ELF binary module '$(LOCAL_MODULE)' with linker list file)
332        linker_options   := $(linker_objects_and_libraries)
333        linker_list_file := $(LOCAL_OBJS_DIR)/linker.list
334        linker_objects_and_libraries := @$(call host-path,$(linker_list_file))
335
336        $(call generate-list-file,$(linker_options),$(linker_list_file))
337
338        $(LOCAL_BUILT_MODULE): $(linker_list_file)
339    endif
340
341endif
342
343$(call generate-file-dir,$(LOCAL_BUILT_MODULE))
344
345$(LOCAL_BUILT_MODULE): PRIVATE_STATIC_LIBRARIES := $(static_libraries)
346$(LOCAL_BUILT_MODULE): PRIVATE_WHOLE_STATIC_LIBRARIES := $(whole_static_libraries)
347$(LOCAL_BUILT_MODULE): PRIVATE_SHARED_LIBRARIES := $(shared_libraries)
348$(LOCAL_BUILT_MODULE): PRIVATE_OBJECTS          := $(LOCAL_OBJECTS)
349$(LOCAL_BUILT_MODULE): PRIVATE_LINKER_OBJECTS_AND_LIBRARIES := $(linker_objects_and_libraries)
350$(LOCAL_BUILT_MODULE): PRIVATE_LIBGCC := $(TARGET_LIBGCC)
351
352$(LOCAL_BUILT_MODULE): PRIVATE_LD := $(TARGET_LD)
353$(LOCAL_BUILT_MODULE): PRIVATE_LDFLAGS := $(TARGET_LDFLAGS) $(LOCAL_LDFLAGS)
354$(LOCAL_BUILT_MODULE): PRIVATE_LDLIBS  := $(LOCAL_LDLIBS) $(TARGET_LDLIBS)
355
356$(LOCAL_BUILT_MODULE): PRIVATE_NAME := $(notdir $(LOCAL_BUILT_MODULE))
357$(LOCAL_BUILT_MODULE): PRIVATE_CXX := $(TARGET_CXX)
358$(LOCAL_BUILT_MODULE): PRIVATE_CC := $(TARGET_CC)
359$(LOCAL_BUILT_MODULE): PRIVATE_AR := $(TARGET_AR) $(TARGET_ARFLAGS)
360$(LOCAL_BUILT_MODULE): PRIVATE_AR_OBJECTS := $(ar_objects)
361$(LOCAL_BUILT_MODULE): PRIVATE_SYSROOT := $(SYSROOT)
362$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_SHARED_LIB := $(cmd-build-shared-library)
363$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_STATIC_LIB := $(cmd-build-static-library)
364$(LOCAL_BUILT_MODULE): PRIVATE_BUILD_EXECUTABLE := $(cmd-build-executable)
365
366#
367# If this is a static library module
368#
369ifeq ($(call module-get-class,$(LOCAL_MODULE)),STATIC_LIBRARY)
370$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
371	@ $(HOST_ECHO) "StaticLibrary  : $(PRIVATE_NAME)"
372	$(hide) $(call host-rm,$@)
373	$(hide) $(PRIVATE_BUILD_STATIC_LIB)
374
375ALL_STATIC_LIBRARIES += $(LOCAL_BUILT_MODULE)
376endif
377
378#
379# If this is a shared library module
380#
381ifeq ($(call module-get-class,$(LOCAL_MODULE)),SHARED_LIBRARY)
382$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
383	@ $(HOST_ECHO) "SharedLibrary  : $(PRIVATE_NAME)"
384	$(hide) $(PRIVATE_BUILD_SHARED_LIB)
385
386ALL_SHARED_LIBRARIES += $(LOCAL_BUILT_MODULE)
387endif
388
389#
390# If this is an executable module
391#
392ifeq ($(call module-get-class,$(LOCAL_MODULE)),EXECUTABLE)
393$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
394	@ $(HOST_ECHO) "Executable     : $(PRIVATE_NAME)"
395	$(hide) $(PRIVATE_BUILD_EXECUTABLE)
396
397ALL_EXECUTABLES += $(LOCAL_BUILT_MODULE)
398endif
399
400#
401# If this is a prebuilt module
402#
403ifeq ($(call module-is-prebuilt,$(LOCAL_MODULE)),$(true))
404$(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
405	@ $(HOST_ECHO) "Prebuilt       : $(PRIVATE_NAME) <= $(call pretty-dir,$(dir $<))"
406	$(hide) $(call host-cp,$<,$@)
407endif
408
409#
410# If this is an installable module
411#
412ifeq ($(call module-is-installable,$(LOCAL_MODULE)),$(true))
413$(LOCAL_INSTALLED): PRIVATE_NAME      := $(notdir $(LOCAL_BUILT_MODULE))
414$(LOCAL_INSTALLED): PRIVATE_SRC       := $(LOCAL_BUILT_MODULE)
415$(LOCAL_INSTALLED): PRIVATE_DST_DIR   := $(NDK_APP_DST_DIR)
416$(LOCAL_INSTALLED): PRIVATE_DST       := $(LOCAL_INSTALLED)
417$(LOCAL_INSTALLED): PRIVATE_STRIP     := $(TARGET_STRIP)
418$(LOCAL_INSTALLED): PRIVATE_STRIP_CMD := $(call cmd-strip, $(PRIVATE_DST))
419
420$(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
421	@$(HOST_ECHO) "Install        : $(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
422	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
423	$(hide) $(PRIVATE_STRIP_CMD)
424
425$(call generate-dir,$(NDK_APP_DST_DIR))
426$(LOCAL_INSTALLED): $(NDK_APP_DST_DIR)
427
428endif
429