Add support for LOCAL_MODULE_FILENAME, that allows renaming generated files.

Change-Id: I228732001af7786f31439a8462310e8ffe67dc19
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index 86887f1..f57012b 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -201,8 +201,8 @@
 LOCAL_STATIC_LIBRARIES := $(call strip-lib-prefix,$(LOCAL_STATIC_LIBRARIES))
 LOCAL_SHARED_LIBRARIES := $(call strip-lib-prefix,$(LOCAL_SHARED_LIBRARIES))
 
-static_libraries := $(call map,static-library-path,$(LOCAL_STATIC_LIBRARIES))
-shared_libraries := $(call map,shared-library-path,$(LOCAL_SHARED_LIBRARIES))\
+static_libraries := $(call map,module-get-built,$(LOCAL_STATIC_LIBRARIES))
+shared_libraries := $(call map,module-get-built,$(LOCAL_SHARED_LIBRARIES))\
                     $(call map,module-get-built,$(LOCAL_PREBUILTS))\
                     $(TARGET_PREBUILT_SHARED_LIBRARIES)
 
diff --git a/build/core/build-executable.mk b/build/core/build-executable.mk
index a43d8d0..e32d1ea 100644
--- a/build/core/build-executable.mk
+++ b/build/core/build-executable.mk
@@ -22,11 +22,12 @@
 
 $(call check-defined-LOCAL_MODULE,$(LOCAL_BUILD_SCRIPT))
 $(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
+$(call check-LOCAL_MODULE_FILENAME)
 
 # we are building target objects
 my := TARGET_
 
-LOCAL_BUILT_MODULE := $(call executable-path,$(LOCAL_MODULE))
-LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE)
+$(call handle-module-filename,,)
+$(call handle-module-built)
 
 $(call module-add-executable,$(LOCAL_MODULE))
diff --git a/build/core/build-shared-library.mk b/build/core/build-shared-library.mk
index 65de48e..df5b58a 100644
--- a/build/core/build-shared-library.mk
+++ b/build/core/build-shared-library.mk
@@ -22,11 +22,12 @@
 
 $(call check-defined-LOCAL_MODULE,$(LOCAL_BUILD_SCRIPT))
 $(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
+$(call check-LOCAL_MODULE_FILENAME)
 
 # we are building target objects
 my := TARGET_
 
-LOCAL_BUILT_MODULE := $(call shared-library-path,$(LOCAL_MODULE))
-LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE)
+$(call handle-module-filename,lib,.so)
+$(call handle-module-built)
 
 $(call module-add-shared-library,$(LOCAL_MODULE))
diff --git a/build/core/build-static-library.mk b/build/core/build-static-library.mk
index c6c4b8b..e6e452e 100644
--- a/build/core/build-static-library.mk
+++ b/build/core/build-static-library.mk
@@ -26,8 +26,8 @@
 # we are building target objects
 my := TARGET_
 
-LOCAL_BUILT_MODULE := $(call static-library-path,$(LOCAL_MODULE))
-LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE)
+$(call handle-module-filename,lib,.a)
+$(call handle-module-built)
 
 $(call module-add-static-library,$(LOCAL_MODULE))
 
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index 4fea6da..f648d84 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -154,6 +154,7 @@
 #
 modules-LOCALS := \
     MODULE \
+    MODULE_FILENAME \
     PATH \
     SRC_FILES \
     CPP_EXTENSION \
@@ -468,6 +469,66 @@
   )
 
 # -----------------------------------------------------------------------------
+# This is used to check that LOCAL_MODULE_FILENAME, if defined, is correct.
+#
+# Function : check-user-LOCAL_MODULE_FILENAME
+# Returns  : None
+# Usage    : $(call check-user-LOCAL_MODULE_FILENAME)
+# -----------------------------------------------------------------------------
+check-LOCAL_MODULE_FILENAME = \
+  $(if $(strip $(LOCAL_MODULE_FILENAME)),\
+    $(if $(call seq,$(words $(LOCAL_MODULE_FILENAME)),1),,\
+        $(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain spaces)\
+        $(call __ndk_error,Plase correct error. Aborting)\
+    )\
+    $(if $(filter %.a %.so,$(LOCAL_MODULE_FILENAME)),\
+        $(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_FILENAME should not include file extensions)\
+    )\
+  )
+
+# -----------------------------------------------------------------------------
+# Function  : handle-module-filename
+# Arguments : 1: default file prefix
+#             2: file suffix
+# Returns   : None
+# Usage     : $(call handle-module-filename,<prefix>,<suffix>)
+# Rationale : To be used to check and or set the module's filename through
+#             the LOCAL_MODULE_FILENAME variable.
+# -----------------------------------------------------------------------------
+handle-module-filename = $(eval $(call ev-handle-module-filename,$1,$2))
+
+
+# $1: default file prefix
+# $2: default file suffix
+define ev-handle-module-filename
+LOCAL_MODULE_FILENAME := $$(strip $$(LOCAL_MODULE_FILENAME))
+ifdef LOCAL_MODULE_FILENAME
+    ifneq ($$(words $$(LOCAL_MODULE_FILENAME)),1)
+        $$(call __ndk_info,$$(LOCAL_MAKEFILE):$$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain any space)
+        $$(call __ndk_error,Aborting)
+    endif
+    ifneq ($$(filter %.a %.so,$$(LOCAL_MODULE_FILENAME)),)
+        $$(call __ndk_info,$$(LOCAL_MAKEFILE):$$(LOCAL_MODULE): LOCAL_MODULE_FILENAME must not contain a file extension)
+        $$(call __ndk_error,Aborting)
+    endif
+else
+    LOCAL_MODULE_FILENAME := $1$(LOCAL_MODULE)
+endif
+LOCAL_MODULE_FILENAME := $$(LOCAL_MODULE_FILENAME)$2
+endef
+
+# -----------------------------------------------------------------------------
+# Function  : handle-module-built
+# Returns   : None
+# Usage     : $(call handle-module-built)
+# Rationale : To be used to automatically compute the location of the generated
+#             binary file, and the directory where to place its object files.
+# -----------------------------------------------------------------------------
+handle-module-built = \
+    $(eval LOCAL_BUILT_MODULE := $(TARGET_OUT)/$(LOCAL_MODULE_FILENAME))\
+    $(eval LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE))
+
+# -----------------------------------------------------------------------------
 # Strip any 'lib' prefix in front of a given string.
 #
 # Function : strip-lib-prefix
@@ -690,54 +751,6 @@
 #
 # =============================================================================
 
-
-# =============================================================================
-#
-# Generated files support
-#
-# =============================================================================
-
-
-# -----------------------------------------------------------------------------
-# Function  : host-static-library-path
-# Arguments : 1: library module name (e.g. 'foo')
-# Returns   : location of generated host library name (e.g. '..../libfoo.a)
-# Usage     : $(call host-static-library-path,<modulename>)
-# -----------------------------------------------------------------------------
-host-static-library-path = $(HOST_OUT)/lib$1.a
-
-# -----------------------------------------------------------------------------
-# Function  : host-executable-path
-# Arguments : 1: executable module name (e.g. 'foo')
-# Returns   : location of generated host executable name (e.g. '..../foo)
-# Usage     : $(call host-executable-path,<modulename>)
-# -----------------------------------------------------------------------------
-host-executable-path = $(HOST_OUT)/$1$(HOST_EXE)
-
-# -----------------------------------------------------------------------------
-# Function  : static-library-path
-# Arguments : 1: library module name (e.g. 'foo')
-# Returns   : location of generated static library name (e.g. '..../libfoo.a)
-# Usage     : $(call static-library-path,<modulename>)
-# -----------------------------------------------------------------------------
-static-library-path = $(TARGET_OUT)/lib$1.a
-
-# -----------------------------------------------------------------------------
-# Function  : shared-library-path
-# Arguments : 1: library module name (e.g. 'foo')
-# Returns   : location of generated shared library name (e.g. '..../libfoo.so)
-# Usage     : $(call shared-library-path,<modulename>)
-# -----------------------------------------------------------------------------
-shared-library-path = $(TARGET_OUT)/lib$1.so
-
-# -----------------------------------------------------------------------------
-# Function  : executable-path
-# Arguments : 1: executable module name (e.g. 'foo')
-# Returns   : location of generated exectuable name (e.g. '..../foo)
-# Usage     : $(call executable-path,<modulename>)
-# -----------------------------------------------------------------------------
-executable-path = $(TARGET_OUT)/$1
-
 # =============================================================================
 #
 # Build commands support
diff --git a/build/core/prebuilt-shared-library.mk b/build/core/prebuilt-shared-library.mk
index 2c7f5ff..ab61472 100644
--- a/build/core/prebuilt-shared-library.mk
+++ b/build/core/prebuilt-shared-library.mk
@@ -22,6 +22,7 @@
 
 $(call check-defined-LOCAL_MODULE,$(LOCAL_BUILD_SCRIPT))
 $(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
+$(call check-LOCAL_MODULE_FILENAME)
 
 # Check that LOCAL_SRC_FILES contains only paths to shared libraries
 ifneq ($(words $(LOCAL_SRC_FILES)),1)
@@ -43,6 +44,11 @@
 $(call __ndk_error,Aborting)
 endif
 
+ifdef LOCAL_MODULE_FILENAME
+$(call __ndk_info,ERROR:$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_FILENAME cannot be used for a prebuilt shared library)
+$(call __ndk_error,Aborting)
+endif
+
 LOCAL_BUILT_MODULE := $(LOCAL_SRC_FILES)
 LOCAL_SRC_FILES    :=
 LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE)
diff --git a/docs/ANDROID-MK.TXT b/docs/ANDROID-MK.TXT
index 67098aa..b90125c 100644
--- a/docs/ANDROID-MK.TXT
+++ b/docs/ANDROID-MK.TXT
@@ -374,12 +374,29 @@
     module names, and shall not contain any space. You MUST define
     it before including any $(BUILD_XXXX) script.
 
-    The module name determines the name of generated files, e.g.
-    lib<foo>.so for a shared library module named <foo>. However
+    By default, the module name determines the name of generated files,
+    e.g. lib<foo>.so for a shared library module named <foo>. However
     you should only refer to other modules with their 'normal'
     name (e.g. <foo>) in your NDK build files (either Android.mk
     or Application.mk)
 
+    You can override this default with LOCAL_MODULE_FILENAME (see below)
+
+LOCAL_MODULE_FILENAME
+    This variable is optional, and allows you to redefine the name of
+    generated files. By default, module <foo> will always generate a
+    static library named lib<foo>.a or a shared library named lib<foo>.so,
+    which are standard Unix conventions.
+
+    You can override this by defining LOCAL_MODULE_FILENAME, For example:
+
+        LOCAL_MODULE := foo-version-1
+        LOCAL_MODULE_FILENAME := libfoo
+
+    NOTE: You should not put a path or file extension in your
+    LOCAL_MODULE_FILENAME, these will be handled automatically by the
+    build system.
+
 LOCAL_SRC_FILES
     This is a list of source files that will be built for your module.
     Only list the files that will be passed to a compiler, since the
diff --git a/docs/CHANGES.TXT b/docs/CHANGES.TXT
index 9e14324..9014d7d 100644
--- a/docs/CHANGES.TXT
+++ b/docs/CHANGES.TXT
@@ -39,6 +39,10 @@
   build script (instead of BUILD_SHARED_LIBRARY). See the new documentation
   file named docs/PREBUILTS.TXT for explanations and usage examples.
 
+- Add support to specify a different file name for generated files, through
+  the new LOCAL_MODULE_FILENAME variable. See docs/ANDROID-MK.TXT for an
+  example.
+
 OTHER FIXES & CHANGES:
 
 - Update documentation for 'my-dir' function to explain that, due to the