Merge changes from topic "libcxx-debug"

* changes:
  Mention libc++ not being stripped in the changelog.
  Add APP_STRIP_MODE and LOCAL_STRIP_MODE.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index b91dccd..09a7ce2 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -58,9 +58,22 @@
        `-DANDROID_ARM_NEON=false` to CMake.
      * Alternatively, use the Play Console to [blacklist CPUs] without NEON to
        disallow your app from being installed on those devices.
+ * Added `APP_STRIP_MODE` and `LOCAL_STRIP_MODE` to ndk-build.
+     * Allows the user to specify the strip mode used for their modules.
+     * The option is passed directly to the strip command. See the [strip
+       documentation](https://sourceware.org/binutils/docs/binutils/strip.html)
+       for details.
+     * If set to "none", strip will not be run.
+     * Defaults to "--strip-unneeded". This is the same behavior as previous
+       NDKs.
+     * `LOCAL_STRIP_MODE` always overrides `APP_STRIP_MODE` when set.
+ * [Issue 749]: The libc++_shared.so in the NDK is no longer stripped of debug
+   info. Debugging libc++ is now possible. Gradle will still strip the library
+   before packaging it in an APK.
 
 [Issue 490]: https://github.com/android-ndk/ndk/issues/490
 [Issue 573]: https://github.com/android-ndk/ndk/issues/573
+[Issue 749]: https://github.com/android-ndk/ndk/issues/749
 [blacklist CPUs]: https://support.google.com/googleplay/android-developer/answer/7353455?hl=en
 [clang-tidy]: http://clang.llvm.org/extra/clang-tidy/
 
diff --git a/build/core/add-application.mk b/build/core/add-application.mk
index 984585b..1e57e42 100644
--- a/build/core/add-application.mk
+++ b/build/core/add-application.mk
@@ -216,6 +216,20 @@
         $(eval APP_WRAP_SH_$(_abi) := $(APP_WRAP_SH)))
 endif
 
+# Stripping can be configured both at the app (APP_STRIP_MODE) and module level
+# (LOCAL_STRIP_MODE). The module setting always overrides the application
+# setting.
+#
+# This value is passed as-is as the flag to the strip command except when it is
+# set to the special value "none". If set to "none", the binary will not be
+# stripped at all.
+ifeq ($(APP_STRIP_MODE),)
+    # The strip command is only used for shared libraries and executables. It is
+    # thus safe to use --strip-unneeded, which is only dangerous when applied to
+    # static libraries or object files.
+    APP_STRIP_MODE := --strip-unneeded
+endif
+
 $(if $(call get,$(_map),defined),\
   $(call __ndk_info,Weird, the application $(_app) is already defined by $(call get,$(_map),defined))\
   $(call __ndk_error,Aborting)\
diff --git a/build/core/build-binary.mk b/build/core/build-binary.mk
index b78138f..8fb2b95 100644
--- a/build/core/build-binary.mk
+++ b/build/core/build-binary.mk
@@ -798,6 +798,12 @@
 	$(hide) $(call host-cp,$<,$@)
 endif
 
+ifeq ($(LOCAL_STRIP_MODE),)
+    NDK_STRIP_MODE := $(NDK_APP_STRIP_MODE)
+else
+    NDK_STRIP_MODE := $(LOCAL_STRIP_MODE)
+endif
+
 #
 # If this is an installable module
 #
@@ -808,6 +814,7 @@
 $(LOCAL_INSTALLED): PRIVATE_DST_DIR     := $(NDK_APP_DST_DIR)
 $(LOCAL_INSTALLED): PRIVATE_DST         := $(LOCAL_INSTALLED)
 $(LOCAL_INSTALLED): PRIVATE_STRIP       := $(TARGET_STRIP)
+$(LOCAL_INSTALLED): PRIVATE_STRIP_MODE  := $(NDK_STRIP_MODE)
 $(LOCAL_INSTALLED): PRIVATE_STRIP_CMD   := $(call cmd-strip, $(PRIVATE_DST))
 $(LOCAL_INSTALLED): PRIVATE_OBJCOPY     := $(TARGET_OBJCOPY)
 $(LOCAL_INSTALLED): PRIVATE_OBJCOPY_CMD := $(call cmd-add-gnu-debuglink, $(PRIVATE_DST), $(PRIVATE_SRC))
@@ -815,7 +822,7 @@
 $(LOCAL_INSTALLED): $(LOCAL_BUILT_MODULE) clean-installed-binaries
 	$(call host-echo-build-step,$(PRIVATE_ABI),Install) "$(PRIVATE_NAME) => $(call pretty-dir,$(PRIVATE_DST))"
 	$(hide) $(call host-install,$(PRIVATE_SRC),$(PRIVATE_DST))
-	$(hide) $(PRIVATE_STRIP_CMD)
+	$(if $(filter none,$(PRIVATE_STRIP_MODE)),,$(hide) $(PRIVATE_STRIP_CMD))
 
 #$(hide) $(PRIVATE_OBJCOPY_CMD)
 
diff --git a/build/core/default-build-commands.mk b/build/core/default-build-commands.mk
index dc16a2f..61d9910 100644
--- a/build/core/default-build-commands.mk
+++ b/build/core/default-build-commands.mk
@@ -106,10 +106,7 @@
 $(PRIVATE_AR) $(call host-path,$(LOCAL_BUILT_MODULE)) $(PRIVATE_AR_OBJECTS)
 endef
 
-# The strip command is only used for shared libraries and executables.
-# It is thus safe to use --strip-unneeded, which is only dangerous
-# when applied to static libraries or object files.
-cmd-strip = $(PRIVATE_STRIP) --strip-unneeded $(call host-path,$1)
+cmd-strip = $(PRIVATE_STRIP) $(PRIVATE_STRIP_MODE) $(call host-path,$1)
 
 # The command objcopy --add-gnu-debuglink= will be needed for Valgrind
 cmd-add-gnu-debuglink = $(PRIVATE_OBJCOPY) --add-gnu-debuglink=$(strip $(call host-path,$2)) $(call host-path,$1)
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index 96de24c..901750a 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -1351,6 +1351,7 @@
     APP_PROJECT_PATH \
     APP_SHORT_COMMANDS \
     APP_STL \
+    APP_STRIP_MODE \
     APP_THIN_ARCHIVE \
     APP_WRAP_SH \
 
diff --git a/tests/build/strip/project/jni/Android.mk b/tests/build/strip/project/jni/Android.mk
new file mode 100644
index 0000000..365bbad
--- /dev/null
+++ b/tests/build/strip/project/jni/Android.mk
@@ -0,0 +1,6 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foo
+LOCAL_SRC_FILES := foo.cpp
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/strip/project/jni/foo.cpp b/tests/build/strip/project/jni/foo.cpp
new file mode 100644
index 0000000..85e6cd8
--- /dev/null
+++ b/tests/build/strip/project/jni/foo.cpp
@@ -0,0 +1 @@
+void foo() {}
diff --git a/tests/build/strip/test.py b/tests/build/strip/test.py
new file mode 100644
index 0000000..a8e3fbe
--- /dev/null
+++ b/tests/build/strip/test.py
@@ -0,0 +1,44 @@
+#
+# Copyright (C) 2016 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.
+#
+"""Check for strip --strip-unneeded use."""
+import os
+import subprocess
+import sys
+
+
+def run_test(ndk_path, abi, api, build_flags):
+    """Checks ndk-build V=1 output for --strip-unneeded flag."""
+    if build_flags is None:
+        build_flags = []
+
+    ndk_build = os.path.join(ndk_path, 'ndk-build')
+    if sys.platform == 'win32':
+        ndk_build += '.cmd'
+    project_path = 'project'
+
+    ndk_args = build_flags + [
+        'APP_ABI=' + abi,
+        'APP_PLATFORM=android-{}'.format(api),
+        'V=1',
+    ]
+    proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args,
+                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    out, _ = proc.communicate()
+    if proc.returncode != 0:
+        return proc.returncode == 0, out
+
+    out_words = out.split(' ')
+    return '--strip-unneeded' in out_words, out
diff --git a/tests/build/strip_keep_symbols/project/jni/Android.mk b/tests/build/strip_keep_symbols/project/jni/Android.mk
new file mode 100644
index 0000000..f4b49ea
--- /dev/null
+++ b/tests/build/strip_keep_symbols/project/jni/Android.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foo
+LOCAL_SRC_FILES := foo.cpp
+LOCAL_STRIP_MODE := --strip-debug
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/strip_keep_symbols/project/jni/foo.cpp b/tests/build/strip_keep_symbols/project/jni/foo.cpp
new file mode 100644
index 0000000..85e6cd8
--- /dev/null
+++ b/tests/build/strip_keep_symbols/project/jni/foo.cpp
@@ -0,0 +1 @@
+void foo() {}
diff --git a/tests/build/strip_keep_symbols/test.py b/tests/build/strip_keep_symbols/test.py
new file mode 100644
index 0000000..24487bb
--- /dev/null
+++ b/tests/build/strip_keep_symbols/test.py
@@ -0,0 +1,47 @@
+#
+# Copyright (C) 2016 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.
+#
+"""Check for strip --strip-debug use."""
+import os
+import subprocess
+import sys
+
+
+def run_test(ndk_path, abi, api, build_flags):
+    """Checks ndk-build V=1 output for --strip-debug flag."""
+    if build_flags is None:
+        build_flags = []
+
+    ndk_build = os.path.join(ndk_path, 'ndk-build')
+    if sys.platform == 'win32':
+        ndk_build += '.cmd'
+    project_path = 'project'
+
+    ndk_args = build_flags + [
+        'APP_ABI=' + abi,
+        'APP_PLATFORM=android-{}'.format(api),
+        'V=1',
+    ]
+    proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args,
+                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    out, _ = proc.communicate()
+    if proc.returncode != 0:
+        return proc.returncode == 0, out
+
+    out_words = out.split(' ')
+    result = False
+    if '--strip-debug' in out_words and '--strip-unneeded' not in out_words:
+        result = True
+    return result, out
diff --git a/tests/build/strip_keep_symbols_app/project/jni/Android.mk b/tests/build/strip_keep_symbols_app/project/jni/Android.mk
new file mode 100644
index 0000000..365bbad
--- /dev/null
+++ b/tests/build/strip_keep_symbols_app/project/jni/Android.mk
@@ -0,0 +1,6 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foo
+LOCAL_SRC_FILES := foo.cpp
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/strip_keep_symbols_app/project/jni/Application.mk b/tests/build/strip_keep_symbols_app/project/jni/Application.mk
new file mode 100644
index 0000000..bb2dc76
--- /dev/null
+++ b/tests/build/strip_keep_symbols_app/project/jni/Application.mk
@@ -0,0 +1 @@
+APP_STRIP_MODE := --strip-debug
diff --git a/tests/build/strip_keep_symbols_app/project/jni/foo.cpp b/tests/build/strip_keep_symbols_app/project/jni/foo.cpp
new file mode 100644
index 0000000..85e6cd8
--- /dev/null
+++ b/tests/build/strip_keep_symbols_app/project/jni/foo.cpp
@@ -0,0 +1 @@
+void foo() {}
diff --git a/tests/build/strip_keep_symbols_app/test.py b/tests/build/strip_keep_symbols_app/test.py
new file mode 100644
index 0000000..24487bb
--- /dev/null
+++ b/tests/build/strip_keep_symbols_app/test.py
@@ -0,0 +1,47 @@
+#
+# Copyright (C) 2016 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.
+#
+"""Check for strip --strip-debug use."""
+import os
+import subprocess
+import sys
+
+
+def run_test(ndk_path, abi, api, build_flags):
+    """Checks ndk-build V=1 output for --strip-debug flag."""
+    if build_flags is None:
+        build_flags = []
+
+    ndk_build = os.path.join(ndk_path, 'ndk-build')
+    if sys.platform == 'win32':
+        ndk_build += '.cmd'
+    project_path = 'project'
+
+    ndk_args = build_flags + [
+        'APP_ABI=' + abi,
+        'APP_PLATFORM=android-{}'.format(api),
+        'V=1',
+    ]
+    proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args,
+                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    out, _ = proc.communicate()
+    if proc.returncode != 0:
+        return proc.returncode == 0, out
+
+    out_words = out.split(' ')
+    result = False
+    if '--strip-debug' in out_words and '--strip-unneeded' not in out_words:
+        result = True
+    return result, out
diff --git a/tests/build/strip_local_overrides_app/project/jni/Android.mk b/tests/build/strip_local_overrides_app/project/jni/Android.mk
new file mode 100644
index 0000000..eebee74
--- /dev/null
+++ b/tests/build/strip_local_overrides_app/project/jni/Android.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foo
+LOCAL_SRC_FILES := foo.cpp
+LOCAL_STRIP_MODE := --strip-unneeded
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/strip_local_overrides_app/project/jni/Application.mk b/tests/build/strip_local_overrides_app/project/jni/Application.mk
new file mode 100644
index 0000000..c88db27
--- /dev/null
+++ b/tests/build/strip_local_overrides_app/project/jni/Application.mk
@@ -0,0 +1 @@
+APP_STRIP_MODE := none
diff --git a/tests/build/strip_local_overrides_app/project/jni/foo.cpp b/tests/build/strip_local_overrides_app/project/jni/foo.cpp
new file mode 100644
index 0000000..85e6cd8
--- /dev/null
+++ b/tests/build/strip_local_overrides_app/project/jni/foo.cpp
@@ -0,0 +1 @@
+void foo() {}
diff --git a/tests/build/strip_local_overrides_app/test.py b/tests/build/strip_local_overrides_app/test.py
new file mode 100644
index 0000000..a8e3fbe
--- /dev/null
+++ b/tests/build/strip_local_overrides_app/test.py
@@ -0,0 +1,44 @@
+#
+# Copyright (C) 2016 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.
+#
+"""Check for strip --strip-unneeded use."""
+import os
+import subprocess
+import sys
+
+
+def run_test(ndk_path, abi, api, build_flags):
+    """Checks ndk-build V=1 output for --strip-unneeded flag."""
+    if build_flags is None:
+        build_flags = []
+
+    ndk_build = os.path.join(ndk_path, 'ndk-build')
+    if sys.platform == 'win32':
+        ndk_build += '.cmd'
+    project_path = 'project'
+
+    ndk_args = build_flags + [
+        'APP_ABI=' + abi,
+        'APP_PLATFORM=android-{}'.format(api),
+        'V=1',
+    ]
+    proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args,
+                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    out, _ = proc.communicate()
+    if proc.returncode != 0:
+        return proc.returncode == 0, out
+
+    out_words = out.split(' ')
+    return '--strip-unneeded' in out_words, out
diff --git a/tests/build/strip_none/project/jni/Android.mk b/tests/build/strip_none/project/jni/Android.mk
new file mode 100644
index 0000000..1a7c895
--- /dev/null
+++ b/tests/build/strip_none/project/jni/Android.mk
@@ -0,0 +1,7 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foo
+LOCAL_SRC_FILES := foo.cpp
+LOCAL_STRIP_MODE := none
+include $(BUILD_SHARED_LIBRARY)
diff --git a/tests/build/strip_none/project/jni/foo.cpp b/tests/build/strip_none/project/jni/foo.cpp
new file mode 100644
index 0000000..85e6cd8
--- /dev/null
+++ b/tests/build/strip_none/project/jni/foo.cpp
@@ -0,0 +1 @@
+void foo() {}
diff --git a/tests/build/strip_none/test.py b/tests/build/strip_none/test.py
new file mode 100644
index 0000000..474adef
--- /dev/null
+++ b/tests/build/strip_none/test.py
@@ -0,0 +1,44 @@
+#
+# Copyright (C) 2016 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.
+#
+"""Check for strip --strip-unneeded use."""
+import os
+import subprocess
+import sys
+
+
+def run_test(ndk_path, abi, api, build_flags):
+    """Checks ndk-build V=1 output for --strip-unneeded flag."""
+    if build_flags is None:
+        build_flags = []
+
+    ndk_build = os.path.join(ndk_path, 'ndk-build')
+    if sys.platform == 'win32':
+        ndk_build += '.cmd'
+    project_path = 'project'
+
+    ndk_args = build_flags + [
+        'APP_ABI=' + abi,
+        'APP_PLATFORM=android-{}'.format(api),
+        'V=1',
+    ]
+    proc = subprocess.Popen([ndk_build, '-C', project_path] + ndk_args,
+                            stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
+    out, _ = proc.communicate()
+    if proc.returncode != 0:
+        return proc.returncode == 0, out
+
+    out_words = out.split(' ')
+    return 'strip' not in out_words, out