Simplify module database definitions.

Introduce the LOCAL_MODULE_CLASS field, and use it.

Change-Id: I7103a78f801fa4fdd2080088a4ae81249ce011cf
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index d34386f..12accd1 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -17,17 +17,11 @@
 $(call assert-defined,LOCAL_MODULE)
 $(call module-restore-locals,$(LOCAL_MODULE))
 
-# Check LOCAL_IS_HOST_MODULE and define 'my' as either HOST_ or TARGET_
+# For now, only support target (device-specific modules).
+# We may want to introduce support for host modules in the future
+# but that is too experimental for now.
 #
-LOCAL_IS_HOST_MODULE := $(strip $(LOCAL_IS_HOST_MODULE))
-ifdef LOCAL_IS_HOST_MODULE
-  ifneq ($(LOCAL_IS_HOST_MODULE),true)
-    $(call __ndk_log,$(LOCAL_PATH): LOCAL_IS_HOST_MODULE must be "true" or empty, not "$(LOCAL_IS_HOST_MODULE)")
-  endif
-  my := HOST_
-else
-  my := TARGET_
-endif
+my := TARGET_
 
 # LOCAL_MAKEFILE must also exist and name the Android.mk that
 # included the module build script.
@@ -69,12 +63,10 @@
 #
 # Add the default system shared libraries to the build
 #
-ifndef LOCAL_IS_HOST_MODULE
-  ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
-    LOCAL_SHARED_LIBRARIES += $(TARGET_DEFAULT_SYSTEM_SHARED_LIBRARIES)
-  else
-    LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
-  endif
+ifeq ($(LOCAL_SYSTEM_SHARED_LIBRARIES),none)
+  LOCAL_SHARED_LIBRARIES += $(TARGET_DEFAULT_SYSTEM_SHARED_LIBRARIES)
+else
+  LOCAL_SHARED_LIBRARIES += $(LOCAL_SYSTEM_SHARED_LIBRARIES)
 endif
 
 
@@ -250,7 +242,7 @@
 #
 # If this is a static library module
 #
-ifeq ($(call module-get-type,$(LOCAL_MODULE)),static-library)
+ifeq ($(call module-get-class,$(LOCAL_MODULE)),STATIC_LIBRARY)
 $(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
 	@ mkdir -p $(dir $@)
 	@ echo "StaticLibrary  : $(PRIVATE_NAME)"
@@ -263,7 +255,7 @@
 #
 # If this is a shared library module
 #
-ifeq ($(call module-get-type,$(LOCAL_MODULE)),shared-library)
+ifeq ($(call module-get-class,$(LOCAL_MODULE)),SHARED_LIBRARY)
 $(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
 	@ mkdir -p $(dir $@)
 	@ echo "SharedLibrary  : $(PRIVATE_NAME)"
@@ -275,7 +267,7 @@
 #
 # If this is an executable module
 #
-ifeq ($(call module-get-type,$(LOCAL_MODULE)),executable)
+ifeq ($(call module-get-class,$(LOCAL_MODULE)),EXECUTABLE)
 $(LOCAL_BUILT_MODULE): $(LOCAL_OBJECTS)
 	@ mkdir -p $(dir $@)
 	@ echo "Executable     : $(PRIVATE_NAME)"
diff --git a/build/core/build-executable.mk b/build/core/build-executable.mk
index e32d1ea..dda5592 100644
--- a/build/core/build-executable.mk
+++ b/build/core/build-executable.mk
@@ -30,4 +30,5 @@
 $(call handle-module-filename,,)
 $(call handle-module-built)
 
-$(call module-add-executable,$(LOCAL_MODULE))
+LOCAL_MODULE_CLASS := EXECUTABLE
+include $(BUILD_SYSTEM)/build-module.mk
diff --git a/build/core/build-module.mk b/build/core/build-module.mk
new file mode 100644
index 0000000..d0ab95d
--- /dev/null
+++ b/build/core/build-module.mk
@@ -0,0 +1,34 @@
+# Copyright (C) 2010 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+$(call check-defined-LOCAL_MODULE,$(LOCAL_BUILD_SCRIPT))
+$(call check-LOCAL_MODULE,$(LOCAL_MAKEFILE))
+
+# This file is used to record the LOCAL_XXX definitions of a given
+# module. It is included by BUILD_STATIC_LIBRARY, BUILD_SHARED_LIBRARY
+# and others.
+#
+LOCAL_MODULE_CLASS := $(strip $(LOCAL_MODULE_CLASS))
+ifndef LOCAL_MODULE_CLASS
+$(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): LOCAL_MODULE_CLASS definition is missing !)
+$(call __ndk_error,Aborting)
+endif
+
+$(if $(call module-class-check,$(LOCAL_MODULE_CLASS)),,\
+$(call __ndk_info,$(LOCAL_MAKEFILE):$(LOCAL_MODULE): Unknown LOCAL_MODULE_CLASS value: $(LOCAL_MODULE_CLASS))\
+$(call __ndk_error,Aborting)\
+)
+
+$(call module-add,$(LOCAL_MODULE))
diff --git a/build/core/build-shared-library.mk b/build/core/build-shared-library.mk
index df5b58a..159aebb 100644
--- a/build/core/build-shared-library.mk
+++ b/build/core/build-shared-library.mk
@@ -30,4 +30,5 @@
 $(call handle-module-filename,lib,.so)
 $(call handle-module-built)
 
-$(call module-add-shared-library,$(LOCAL_MODULE))
+LOCAL_MODULE_CLASS := SHARED_LIBRARY
+include $(BUILD_SYSTEM)/build-module.mk
diff --git a/build/core/build-static-library.mk b/build/core/build-static-library.mk
index e6e452e..cab3758 100644
--- a/build/core/build-static-library.mk
+++ b/build/core/build-static-library.mk
@@ -29,5 +29,6 @@
 $(call handle-module-filename,lib,.a)
 $(call handle-module-built)
 
-$(call module-add-static-library,$(LOCAL_MODULE))
+LOCAL_MODULE_CLASS := STATIC_LIBRARY
+include $(BUILD_SYSTEM)/build-module.mk
 
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index d693ba4..b52c999 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -176,17 +176,37 @@
     EXPORT_C_INCLUDES \
 
 # The following are generated by the build scripts themselves
-modules-LOCALS += \
-    MAKEFILE \
-    LDFLAGS \
-    OBJECTS \
-    BUILT_MODULE \
-    OBJS_DIR \
-    IS_HOST_MODULE \
-    INSTALLED
+
+# LOCAL_MAKEFILE will contain the path to the Android.mk defining the module
+modules-LOCALS += MAKEFILE
+
+# LOCAL_LDFLAGS will contain the set of final linker flags for the module
+modules-LOCALS += LDFLAGS
+
+# LOCAL_OBJECTS will contain the list of object files generated from the
+# module's sources, if any.
+modules-LOCALS += OBJECTS
+
+# LOCAL_BUILT_MODULE will contain the location of the symbolic version of
+# the generated module (i.e. the one containing all symbols used during
+# native debugging). It is generally under $PROJECT/obj/local/
+modules-LOCALS += BUILT_MODULE
+
+# LOCAL_OBJS_DIR will contain the location where the object files for
+# this module will be stored. Usually $PROJECT/obj/local/<module>/obj
+modules-LOCALS += OBJS_DIR
+
+# LOCAL_INSTALLED will contain the location of the installed version
+# of the module. Usually $PROJECT/libs/<abi>/<prefix><module><suffix>
+# where <prefix> and <suffix> depend on the module class.
+modules-LOCALS += INSTALLED
+
+# LOCAL_MODULE_CLASS will contain the type of the module
+# (e.g. STATIC_LIBRARY, SHARED_LIBRARY, etc...)
+modules-LOCALS += MODULE_CLASS
 
 # the list of managed fields per module
-modules-fields = type depends \
+modules-fields = depends \
                  $(modules-LOCALS)
 
 # -----------------------------------------------------------------------------
@@ -213,54 +233,40 @@
 modules-get-list = $(__ndk_modules)
 
 # -----------------------------------------------------------------------------
-# Function : modules-add
+# Function : module-add
 # Arguments: 1: module name
-#            2: type of module (e.g. 'static')
 # Returns  : None
-# Usage    : $(call modules-add,<modulename>,<Android.mk path>)
+# Usage    : $(call module-add,<modulename>)
 # Rationale: add a new module. If it is already defined, print an error message
 #            and abort. This will record all LOCAL_XXX variables for the module.
 # -----------------------------------------------------------------------------
 module-add = \
-  $(call assert-defined,LOCAL_MAKEFILE LOCAL_BUILT_MODULE LOCAL_OBJS_DIR)\
+  $(call assert-defined,LOCAL_MAKEFILE LOCAL_BUILT_MODULE LOCAL_OBJS_DIR LOCAL_MODULE_CLASS)\
   $(if $(call set_is_member,$(__ndk_modules),$1),\
-       $(call __ndk_info,Trying to define local module '$1' in $(LOCAL_MAKEFILE).)\
-       $(call __ndk_info,But this module was already defined by $(__ndk_modules.$1.MAKEFILE).)\
-       $(call __ndk_error,Aborting.)\
+    $(call __ndk_info,Trying to define local module '$1' in $(LOCAL_MAKEFILE).)\
+    $(call __ndk_info,But this module was already defined by $(__ndk_modules.$1.MAKEFILE).)\
+    $(call __ndk_error,Aborting.)\
   )\
   $(eval __ndk_modules := $(call set_insert,$(__ndk_modules),$1))\
-  $(eval __ndk_modules.$1.type := $2)\
+  $(if $(call module-class-is-installable,$(LOCAL_MODULE_CLASS)),\
+    $(eval LOCAL_INSTALLED := $(NDK_APP_DST_DIR)/$(notdir $(LOCAL_BUILT_MODULE)))\
+  )\
   $(foreach __local,$(modules-LOCALS),\
     $(eval __ndk_modules.$1.$(__local) := $(LOCAL_$(__local)))\
   )
 
-module-add-static-library = $(call module-add,$1,static-library)
 
-module-add-shared-library = \
-    $(eval LOCAL_INSTALLED := $(NDK_APP_DST_DIR)/$(notdir $(LOCAL_BUILT_MODULE)))\
-    $(call module-add,$1,shared-library)
-
-module-add-executable = \
-    $(eval LOCAL_INSTALLED := $(NDK_APP_DST_DIR)/$(notdir $(LOCAL_BUILT_MODULE)))\
-    $(call module-add,$1,executable)
-
-module-add-prebuilt-shared-library = \
-    $(eval LOCAL_INSTALLED := $(NDK_APP_DST_DIR)/$(notdir $(LOCAL_BUILT_MODULE)))\
-    $(call module-add,$1,prebuilt-shared-library)
-
-# Returns $(true) iff module $1 is of type $2
-module-is-type = $(call seq,$(__ndk_modules.$1.type),$2)
-
-module-get-type = $(__ndk_modules.$1.type)
+# Retrieve the class of module $1
+module-get-class = $(__ndk_modules.$1.MODULE_CLASS)
 
 # Retrieve built location of module $1
 module-get-built = $(__ndk_modules.$1.BUILT_MODULE)
 
-# Returns $(true) is module $1 is of a given type
-module-is-static-library = $(call module-is-type,$1,static-library)
-module-is-shared-library = $(call module-is-type,$1,shared-library)
-module-is-executable     = $(call module-is-type,$1,executable)
-module-is-installable    = $(if $(filter shared-library executable prebuilt-shared-library,$(call module-get-type,$1)),$(true),$(false))
+# Returns $(true) is module $1 is installable
+# An installable module is one that will be copied to $PROJECT/libs/<abi>/
+# (e.g. shared libraries).
+#
+module-is-installable    = $(call module-class-is-installable,$(call module-get-class,$1))
 
 # -----------------------------------------------------------------------------
 # Function : module-get-export
@@ -307,7 +313,7 @@
             $(info $(space)$(space)$(space)$(space)$(__field): $(__ndk_modules.$(__mod).$(__field)))\
         )\
     )\
-    $(info --- end of list)
+    $(info --- end of modules list)
 
 
 # -----------------------------------------------------------------------------
@@ -964,3 +970,64 @@
     $(foreach __dir,$(__ndk_import_dirs),\
       $(info -- $(__dir))\
     )\
+
+#
+#  Module classes
+#
+NDK_MODULE_CLASSES :=
+
+# Register a new module class
+# $1: class name (e.g. STATIC_LIBRARY)
+# $2: optional file prefix (e.g. 'lib')
+# $3: optional file suffix (e.g. '.so')
+#
+module-class-register = \
+    $(eval NDK_MODULE_CLASSES += $1) \
+    $(eval NDK_MODULE_CLASS.$1.FILE_PREFIX := $2) \
+    $(eval NDK_MODULE_CLASS.$1.FILE_SUFFIX := $3) \
+    $(eval NDK_MODULE_CLASS.$1.INSTALLABLE := $(false)) \
+
+# Same a module-class-register, for installable modules
+#
+# An installable module is one that will be copied to $PROJECT/libs/<abi>/
+# during the NDK build.
+#
+# $1: class name
+# $2: optional file prefix
+# $3: optional file suffix
+#
+module-class-register-installable = \
+    $(call module-class-register,$1,$2,$3) \
+    $(eval NDK_MODULE_CLASS.$1.INSTALLABLE := $(true))
+
+# Returns $(true) if $1 is a valid/registered LOCAL_MODULE_CLASS value
+#
+module-class-check = $(call set_is_member,$(NDK_MODULE_CLASSES),$1)
+
+# Returns $(true) if $1 corresponds to an installable module class
+#
+module-class-is-installable = $(if $(NDK_MODULE_CLASS.$1.INSTALLABLE),$(true),$(false))
+
+
+#
+# Register valid module classes
+#
+
+# static libraries:
+# <foo> -> lib<foo>.a by default
+$(call module-class-register,STATIC_LIBRARY,lib,.a)
+
+# shared libraries:
+# <foo> -> lib<foo>.so
+# a shared library is installable.
+$(call module-class-register-installable,SHARED_LIBRARY,lib,.so)
+
+# executable
+# <foo> -> <foo>
+# an executable is installable.
+$(call module-class-register-installable,EXECUTABLE,,)
+
+# prebuild shared library
+# <foo> -> <foo>  (we assume it is already well-named)
+# it is installable
+$(call module-class-register-installable,PREBUILT_SHARED_LIBRARY,,)
diff --git a/build/core/prebuilt-shared-library.mk b/build/core/prebuilt-shared-library.mk
index ab61472..3c07a1d 100644
--- a/build/core/prebuilt-shared-library.mk
+++ b/build/core/prebuilt-shared-library.mk
@@ -53,4 +53,5 @@
 LOCAL_SRC_FILES    :=
 LOCAL_OBJS_DIR     := $(TARGET_OBJS)/$(LOCAL_MODULE)
 
-$(call module-add-prebuilt-shared-library,$(LOCAL_MODULE))
+LOCAL_MODULE_CLASS := PREBUILT_SHARED_LIBRARY
+include $(BUILD_SYSTEM)/build-module.mk