aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid 'Digit' Turner <digit@google.com>2011-09-15 12:29:09 +0200
committerDavid 'Digit' Turner <digit@google.com>2011-09-21 17:14:27 +0200
commitc55bfcf671107b1b678f5a66a3ec74cbeb3927cb (patch)
treedd24305dc0d467dac2668e3d45e4e108dcc9443c
parent2364cdd5479617ea676179e2f408a47e7ce1c893 (diff)
downloadndk-c55bfcf671107b1b678f5a66a3ec74cbeb3927cb.tar.gz
Add LOCAL_CPP_FEATURES support to Android.mk
This is a new variable that can be used to specify that a given module relies on C++ exceptions or RTTI. This is done for several reasons: - First, we want to avoid always linking GNU libsupc++ to every final binary, because we're soon going to provide an alternative. - Second, it is useful to declare prebuilt binaries that have been compiled with these features. It allows the final link to work correctly. Note that the change is backwards-compatible. You can still use -frtti or -fexceptions in your compiler flags to enable the features too. Change-Id: I24b28935e4446c55b169d35990ec2a379ac08500
-rw-r--r--build/core/build-binary.mk18
-rw-r--r--build/core/definitions.mk79
-rw-r--r--docs/ANDROID-MK.html23
-rw-r--r--docs/CHANGES.html6
-rw-r--r--toolchains/arm-linux-androideabi-4.4.3/setup.mk2
-rw-r--r--toolchains/x86-4.4.3/setup.mk2
6 files changed, 123 insertions, 7 deletions
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index c0b78940a..3f881cb70 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -211,6 +211,24 @@ $(foreach _ext,$(all_source_extensions),\
LOCAL_OBJECTS := $(filter %.o,$(LOCAL_OBJECTS))
LOCAL_OBJECTS := $(foreach _obj,$(LOCAL_OBJECTS),$(LOCAL_OBJS_DIR)/$(_obj))
+# If the module has any kind of C++ features, enable them in LOCAL_CPPFLAGS
+#
+ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),rtti))
+ LOCAL_CPPFLAGS += -frtti
+endif
+ifneq (,$(call module-has-c++-features,$(LOCAL_MODULE),exceptions))
+ LOCAL_CPPFLAGS += -fexceptions
+endif
+
+# If we're using the 'system' STL and use rtti or exceptions, then
+# automatically link against the GNU libsupc++ for now.
+#
+ifneq (,$(call module-has-c++-features),$(LOCAL_MODULE),rtti exceptions)
+ ifeq (system,$(NDK_APP_STL))
+ LOCAL_LDLIBS := $(LOCAL_LDLIBS) -lsupc++
+ endif
+endif
+
# Build the sources to object files
#
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index e708da15a..bc821d993 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -241,6 +241,7 @@ modules-LOCALS := \
EXPORT_LDLIBS \
EXPORT_C_INCLUDES \
FILTER_ASM \
+ CPP_FEATURES \
# The following are generated by the build scripts themselves
@@ -334,7 +335,8 @@ module-add = \
)\
$(foreach __local,$(modules-LOCALS),\
$(eval __ndk_modules.$1.$(__local) := $(LOCAL_$(__local)))\
- )
+ )\
+ $(call module-handle-c++-features,$1)
# Retrieve the class of module $1
@@ -523,7 +525,8 @@ module-get-c++-sources = \
# Returns true if a module has C++ sources
#
-module-has-c++ = $(strip $(call module-get-c++-sources,$1))
+module-has-c++-sources = $(strip $(call module-get-c++-sources,$1))
+
# Add C++ dependencies to any module that has C++ sources.
# $1: list of C++ runtime static libraries (if any)
@@ -531,12 +534,82 @@ module-has-c++ = $(strip $(call module-get-c++-sources,$1))
#
modules-add-c++-dependencies = \
$(foreach __module,$(__ndk_modules),\
- $(if $(call module-has-c++,$(__module)),\
+ $(if $(call module-has-c++-sources,$(__module)),\
$(call ndk_log,Module '$(__module)' has C++ sources)\
$(call module-add-c++-deps,$(__module),$1,$2),\
)\
)
+
+# Return the compiler flags used to compile a C++ module
+# Order matters and should match the one used by the build command
+module-get-c++-flags = $(strip \
+ $(__ndk_modules.$1.CFLAGS) \
+ $(__ndk_modules.$1.CPPFLAGS) \
+ $(__ndk_modules.$1.CXXFLAGS))
+
+# This function is used to remove certain flags from a module compiler flags
+# $1: Module name
+# $2: List of flags to remove
+#
+module-filter-out-compiler-flags = \
+ $(eval __ndk_modules.$1.CFLAGS := $(filter-out $2,$(__ndk_modules.$1.CFLAGS)))\
+ $(eval __ndk_modules.$1.CPPFLAGS := $(filter-out $2,$(__ndk_modules.$1.CPPFLAGS)))\
+ $(eval __ndk_modules.$1.CXXFLAGS := $(filter-out $2,$(__ndk_modules.$1.CXXFLAGS)))
+
+# Return true if a module's compiler flags enable rtti
+# We just look at -frtti and -fno-rtti on the command-line
+# and keep the last one of these flags.
+module-flags-have-rtti = $(strip \
+ $(filter -frtti,\
+ $(lastword $(filter -frtti -fno-rtti,$(call module-get-c++-flags,$1)))\
+ )\
+ )
+
+# Same with C++ exception support (i.e. -fexceptions and -fno-exceptions)
+#
+module-flags-have-exceptions = $(strip \
+ $(filter -fexceptions,\
+ $(lastword $(filter -fexceptions -fno-execeptions,$(call module-get-c++-flags,$1)))\
+ )\
+ )
+
+# Handle the definition of LOCAL_CPP_FEATURES, i.e.:
+#
+# - If it is defined, check that it only contains valid values
+# - If it is undefined, try to compute its value automatically by
+# looking at the C++ compiler flags used to build the module
+#
+# After this, we remove all features flags from the module's command-line
+# And add only the correct ones back in LOCAL_CPP_FLAGS
+#
+module-handle-c++-features = \
+ $(if $(strip $(__ndk_modules.$1.CPP_FEATURES)),\
+ $(eval __cxxbad := $(filter-out rtti exceptions,$(__ndk_modules.$1.CPP_FEATURES)))\
+ $(if $(__cxxbad),\
+ $(call __ndk_info,WARNING: Ignoring invalid values in LOCAL_CPP_FEATURES definition in $(__ndk_modules.$1.MAKEFILE): $(__cxxbad))\
+ $(eval __ndk_modules.$1.CPP_FEATURES := $(strip $(filter-out $(__cxxbad),$(__ndk_modules.$1.CPP_FEATURES))))\
+ )\
+ ,\
+ $(eval __ndk_modules.$1.CPP_FEATURES := $(strip \
+ $(if $(call module-flags-have-rtti,$1),rtti) \
+ $(if $(call module-flags-have-exceptions,$1),exceptions) \
+ )) \
+ )\
+ $(call module-filter-out-compiler-flags,$1,-frtti -fno-rtti -fexceptions -fno-exceptions)\
+
+# Returns true if a module or its dependencies have specific C++ features
+# (i.e. RTTI or Exceptions)
+#
+# $1: module name
+# $2: list of features (e.g. 'rtti' or 'exceptions')
+#
+module-has-c++-features = $(strip \
+ $(eval __cxxdeps := $(call module-get-all-dependencies,$1))\
+ $(eval __cxxflags := $(foreach __cxxdep,$(__cxxdeps),$(__ndk_modules.$(__cxxdep).CPP_FEATURES)))\
+ $(if $(filter $2,$(__cxxflags)),true,)\
+ )
+
# Add standard C++ dependencies to a given module
#
# $1: module name
diff --git a/docs/ANDROID-MK.html b/docs/ANDROID-MK.html
index 3dc5c99c2..0a35c50a7 100644
--- a/docs/ANDROID-MK.html
+++ b/docs/ANDROID-MK.html
@@ -433,6 +433,29 @@ LOCAL_CPP_EXTENSION
LOCAL_CPP_EXTENSION := .cxx
+LOCAL_CPP_FEATURES
+ This is an optional variable that can be defined to indicate
+ that your code relies on specific C++ features. To indicate that
+ your code uses RTTI (RunTime Type Information), use the following:
+
+ LOCAL_CPP_FEATURES := rtti
+
+ To indicate that your code uses C++ exceptions, use:
+
+ LOCAL_CPP_FEATURES := exceptions
+
+ You can also use both of them with (order is not important):
+
+ LOCAL_CPP_FEATURES := rtti features
+
+ The effect of this variable is to enable the right compiler/linker
+ flags when building your modules from sources. For prebuilt binaries,
+ this also helps declare which features the binary relies on to ensure
+ the final link works correctly.
+
+ It is recommended to use this variable instead of enabling -frtti and
+ -fexceptions directly in your LOCAL_CPPFLAGS definition.
+
LOCAL_C_INCLUDES
An optional list of paths, relative to the NDK *root* directory,
which will be appended to the include search path when compiling
diff --git a/docs/CHANGES.html b/docs/CHANGES.html
index 41f770df0..423486a92 100644
--- a/docs/CHANGES.html
+++ b/docs/CHANGES.html
@@ -43,6 +43,12 @@ IMPORTANT CHANGES:
of your project tree, or if you define NDK_PROJECT_PATH to point to a
specific directory.
+- New LOCAL_CPP_FEATURES variable in Android.mk, used to declare which C++
+ features (RTTI or Exceptions) your module uses. This is especially handy
+ if you have prebuilt modules that depend on them since this will ensure
+ the final link will work correctly.
+
+ See docs/ANDROID-MK.html and docs/CPLUSPLUS-SUPPORT.html for more details
IMPORTANT BUG FIXES:
diff --git a/toolchains/arm-linux-androideabi-4.4.3/setup.mk b/toolchains/arm-linux-androideabi-4.4.3/setup.mk
index 37b470227..3fa1d60ae 100644
--- a/toolchains/arm-linux-androideabi-4.4.3/setup.mk
+++ b/toolchains/arm-linux-androideabi-4.4.3/setup.mk
@@ -130,7 +130,6 @@ $(PRIVATE_CXX) \
$(PRIVATE_SHARED_LIBRARIES)) \
$(PRIVATE_LDFLAGS) \
$(PRIVATE_LDLIBS) \
- -lsupc++ \
-o $(call host-path,$@)
endef
@@ -147,6 +146,5 @@ $(PRIVATE_CXX) \
$(PRIVATE_SHARED_LIBRARIES)) \
$(PRIVATE_LDFLAGS) \
$(PRIVATE_LDLIBS) \
- -lsupc++ \
-o $(call host-path,$@)
endef
diff --git a/toolchains/x86-4.4.3/setup.mk b/toolchains/x86-4.4.3/setup.mk
index 8a81dc5a1..3d55ada67 100644
--- a/toolchains/x86-4.4.3/setup.mk
+++ b/toolchains/x86-4.4.3/setup.mk
@@ -86,7 +86,6 @@ $(PRIVATE_CXX) \
$(PRIVATE_SHARED_LIBRARIES)) \
$(PRIVATE_LDFLAGS) \
$(PRIVATE_LDLIBS) \
- -lsupc++ \
-o $(call host-path,$@)
endef
@@ -103,6 +102,5 @@ $(PRIVATE_CXX) \
$(PRIVATE_SHARED_LIBRARIES)) \
$(PRIVATE_LDFLAGS) \
$(PRIVATE_LDLIBS) \
- -lsupc++ \
-o $(call host-path,$@)
endef