Merge "Make more scripts supporting the unknown archs."
diff --git a/build/core/add-application.mk b/build/core/add-application.mk
index da6fabe..a513d6e 100644
--- a/build/core/add-application.mk
+++ b/build/core/add-application.mk
@@ -243,6 +243,7 @@
 endif
 
 APP_CFLAGS := $(strip $(APP_CFLAGS))
+APP_CONLYFLAGS := $(strip $(APP_CONLYFLAGS))
 APP_LDFLAGS := $(strip $(APP_LDFLAGS))
 
 # Check that APP_STL is defined. If not, use the default value (system)
diff --git a/build/core/default-build-commands.mk b/build/core/default-build-commands.mk
index 679dc3f..cf2e89e 100644
--- a/build/core/default-build-commands.mk
+++ b/build/core/default-build-commands.mk
@@ -115,6 +115,7 @@
 TARGET_CC       = $(TOOLCHAIN_PREFIX)gcc
 endif
 TARGET_CFLAGS   =
+TARGET_CONLYFLAGS =
 
 ifneq ($(findstring c++-analyzer,$(CXX)),)
 TARGET_CXX      = $(CXX)
diff --git a/build/core/definitions.mk b/build/core/definitions.mk
index fd36470..d27fed6 100644
--- a/build/core/definitions.mk
+++ b/build/core/definitions.mk
@@ -378,6 +378,7 @@
     CPP_EXTENSION \
     C_INCLUDES \
     CFLAGS \
+    CONLYFLAGS \
     CXXFLAGS \
     CPPFLAGS \
     STATIC_LIBRARIES \
@@ -391,6 +392,7 @@
     DISABLE_RELRO \
     DISABLE_FORMAT_STRING_CHECKS \
     EXPORT_CFLAGS \
+    EXPORT_CONLYFLAGS \
     EXPORT_CPPFLAGS \
     EXPORT_LDLIBS \
     EXPORT_C_INCLUDES \
@@ -820,9 +822,10 @@
 # $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)))
+    $(eval __ndk_modules.$1.CFLAGS     := $(filter-out $2,$(__ndk_modules.$1.CFLAGS)))\
+    $(eval __ndk_modules.$1.CONLYFLAGS := $(filter-out $2,$(__ndk_modules.$1.CONLYFLAGS)))\
+    $(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
@@ -1247,8 +1250,8 @@
 NDK_APP_VARS_REQUIRED :=
 
 # the list of variables that *may* be defined in Application.mk files
-NDK_APP_VARS_OPTIONAL := APP_OPTIM APP_CPPFLAGS APP_CFLAGS APP_CXXFLAGS APP_LDFLAGS \
-                         APP_PLATFORM APP_BUILD_SCRIPT APP_ABI APP_MODULES \
+NDK_APP_VARS_OPTIONAL := APP_OPTIM APP_CPPFLAGS APP_CFLAGS APP_CONLY_FLAGS APP_CXXFLAGS \
+                         APP_LDFLAGS APP_PLATFORM APP_BUILD_SCRIPT APP_ABI APP_MODULES \
                          APP_PROJECT_PATH APP_STL APP_SHORT_COMMANDS \
                          APP_PIE APP_THIN_ARCHIVE
 
@@ -1478,7 +1481,9 @@
           $$(call get-src-file-target-cflags,$(1)) \
           $$(call host-c-includes,$$(LOCAL_C_INCLUDES) $$(LOCAL_PATH)) \
           $$(LOCAL_CFLAGS) \
+          $$(LOCAL_CONLYFLAGS) \
           $$(NDK_APP_CFLAGS) \
+          $$(NDK_APP_CONLYFLAGS) \
           $$(call host-c-includes,$$($(my)C_INCLUDES)) \
           -c \
 
diff --git a/build/core/import-locals.mk b/build/core/import-locals.mk
index 9b6abb9..cb18480 100644
--- a/build/core/import-locals.mk
+++ b/build/core/import-locals.mk
@@ -17,12 +17,12 @@
 
 $(call assert-defined,LOCAL_MODULE)
 
-# For LOCAL_CFLAGS, LOCAL_CPPFLAGS and LOCAL_C_INCLUDES, we need
-# to use the exported definitions of the closure of all modules
+# For LOCAL_CFLAGS, LOCAL_CONLYFLAGS, LOCAL_CPPFLAGS and LOCAL_C_INCLUDES,
+# we need to use the exported definitions of the closure of all modules
 # we depend on.
 #
 # I.e. If module 'foo' depends on 'bar' which depends on 'zoo',
-# then 'foo' will get the CFLAGS/CPPFLAGS/C_INCLUDES of both 'bar'
+# then 'foo' will get the CFLAGS/CONLYFLAGS/CPPFLAGS/C_INCLUDES of both 'bar'
 # and 'zoo'
 #
 
@@ -30,12 +30,14 @@
 all_depends := $(filter-out $(LOCAL_MODULE),$(all_depends))
 
 imported_CFLAGS     := $(call module-get-listed-export,$(all_depends),CFLAGS)
+imported_CONLYFLAGS := $(call module-get-listed-export,$(all_depends),CONLYFLAGS)
 imported_CPPFLAGS   := $(call module-get-listed-export,$(all_depends),CPPFLAGS)
 imported_C_INCLUDES := $(call module-get-listed-export,$(all_depends),C_INCLUDES)
 
 ifdef NDK_DEBUG_IMPORTS
     $(info Imports for module $(LOCAL_MODULE):)
     $(info   CFLAGS='$(imported_CFLAGS)')
+    $(info   CONLYFLAGS='$(imported_CONLYFLAGS)')
     $(info   CPPFLAGS='$(imported_CPPFLAGS)')
     $(info   C_INCLUDES='$(imported_C_INCLUDES)')
     $(info All depends='$(all_depends)')
@@ -46,6 +48,7 @@
 # (this allows the module to override them).
 #
 LOCAL_CFLAGS     := $(strip $(imported_CFLAGS) $(LOCAL_CFLAGS))
+LOCAL_CONLYFLAGS := $(strip $(imported_CONLYFLAGS) $(LOCAL_CONLYFLAGS))
 LOCAL_CPPFLAGS   := $(strip $(imported_CPPFLAGS) $(LOCAL_CPPFLAGS))
 
 #
diff --git a/build/tools/build-device-llvm.sh b/build/tools/build-device-llvm.sh
index 6f8862a..93aedf1 100755
--- a/build/tools/build-device-llvm.sh
+++ b/build/tools/build-device-llvm.sh
@@ -153,7 +153,7 @@
   if [ -n "$OPTION_GCC_VERSION" ]; then
     toolchain_name="${toolchain_name//${DEFAULT_GCC_VERSION}/${OPTION_GCC_VERSION}}"
   fi
-  CFLAGS="-fomit-frame-pointer -fstrict-aliasing"
+  CFLAGS="-fomit-frame-pointer -fstrict-aliasing -ffunction-sections -fdata-sections"
   case $abi in
     armeabi)
       # No -mthumb, because ./llvm/lib/Target/ARM/ARMJITInfo.cpp now contain inline assembly
@@ -243,6 +243,9 @@
   # Strip
   STRIP=$BUILD_OUT/ndk-standalone-$arch/bin/$toolchain_prefix-strip
   find $TOOLCHAIN_BUILD_PREFIX/$abi -maxdepth 1 -type f -exec $STRIP --strip-all {} \;
+
+  # copy SOURCES
+  run cp "$SRC_DIR/SOURCES" $TOOLCHAIN_BUILD_PREFIX/$abi
 done
 
 TOOLCHAIN_BUILD_PREFIX="`cd $TOOLCHAIN_BUILD_PREFIX; pwd`"
diff --git a/build/tools/build-on-device-toolchain.sh b/build/tools/build-on-device-toolchain.sh
index c06a388..e3ac336 100755
--- a/build/tools/build-on-device-toolchain.sh
+++ b/build/tools/build-on-device-toolchain.sh
@@ -104,10 +104,14 @@
   run cp -f $NDK_DIR/$GABIXX_SUBDIR/libs/$ABI/libgabi++_shared.so $OUT_SYSROOT/usr/lib
 fi
 
+dump "Strip $ABI binaries"
+STRIP=$NDK_DIR/$(get_default_toolchain_binprefix_for_arch $ARCH)strip
+run find $OUT_SYSROOT/usr/lib \( -name "*\.so" \) -exec $STRIP --strip-all {} \;
 
 dump "Build $ABI LLVM toolchain from $SRC_DIR ..."
 run $BUILDTOOLS/build-device-llvm.sh $FLAGS --abis=$ABI --gcc-version=$GCC_TOOLCHAIN_VERSION $SRC_DIR $NDK_DIR
 fail_panic "Could not build le32 LLVM toolchain!"
+run mv -f $TMP_OUT_DIR/$ABI/SOURCES $OUT_SYSROOT/usr
 run mv -f $TMP_OUT_DIR/$ABI/lib*.so $OUT_SYSROOT/usr/lib
 run mv -f $TMP_OUT_DIR/$ABI/* $OUT_SYSROOT/usr/bin
 run rmdir $TMP_OUT_DIR/$ABI
diff --git a/build/tools/cleanup-headers.sh b/build/tools/cleanup-headers.sh
new file mode 100755
index 0000000..fd84484
--- /dev/null
+++ b/build/tools/cleanup-headers.sh
@@ -0,0 +1,165 @@
+#!/bin/sh
+#
+# Copyright (C) 2013 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.
+#
+# cleanup-headers.sh
+#
+# This tool is used when we want to generate NDK arch headers.
+# It will populate $NDK_DIR/../development/ndk/platforms/android-*/arch-$NEW_ARCH by default.
+#
+
+PROGDIR=`dirname $0`
+PROGDIR=`cd $PROGDIR && pwd`
+. "$PROGDIR/prebuilt-common.sh"
+NDK_DIR=$ANDROID_NDK_ROOT
+
+DEV_DIR="$NDK_DIR/../development"
+register_var_option "--development-dir=<path>" DEV_DIR "Specify where the development dir is"
+
+PROGRAM_PARAMETERS="<old-arch-name> <new-arch-name>"
+PROGRAM_DESCRIPTION=\
+"This tool is used when we want to generate NDK arch headers.
+It will populate $NDK_DIR/../development/ndk/platforms/android-*/arch-$NEW_ARCH by default."
+
+extract_parameters "$@"
+
+set_parameters() {
+  OLD_ARCH=$1
+  NEW_ARCH=$2
+  DEV_NDK_PLATFORM_DIR=$DEV_DIR/ndk/platforms
+}
+
+set_parameters $PARAMETERS
+
+if [ -z "$OLD_ARCH" ] || [ -z "$NEW_ARCH" ]; then
+  dump "[Error] Must specify old and new arch names"
+  exit 1
+fi
+
+if [ -n "`find $DEV_NDK_PLATFORM_DIR -type d | grep arch-$NEW_ARCH`" ]; then
+  log "[Warning] Already exist new arch headers. Remove it first."
+  for API in $API_LEVELS; do
+    rm -rf $DEV_NDK_PLATFORM_DIR/android-$API/arch-$NEW_ARCH
+  done
+fi
+
+replace() {
+  local HEADERS_DIR=$1
+  local OLD=$2
+  local NEW=$3
+  FILES="`find $HEADERS_DIR | xargs grep $OLD | awk -F ':' '{print $1}' | sort -u | xargs`"
+  for FILE in $FILES; do
+    TMP_FILE=`mktemp`
+    cp -a $FILE $TMP_FILE
+    cat $TMP_FILE | sed -e "s/$OLD/$NEW/g" > $FILE
+    rm -f $TMP_FILE
+  done
+}
+
+remove() {
+  local HEADERS_DIR=$1
+  local PATTERN=$2
+  local NAME=$3
+  FILES="`find $HEADERS_DIR | xargs grep $NAME | awk -F ':' '{print $1}' | sort -u | xargs`"
+  for FILE in $FILES; do
+    TMP_FILE=`mktemp`
+    cp -a $FILE $TMP_FILE
+    cat $TMP_FILE | sed -e "s/.*${PATTERN}.*${NAME}.*//g" > $FILE
+    rm -f $TMP_FILE
+  done
+}
+
+OLD_PATTERN=ARM
+NEW_PATTERN=MACHINE
+TMP_PATTERN=NDKTMPMACHINE
+KEEP_NO_RENAME_STUB=
+replace_stub_to_temp() {
+  local COMMAND=$1
+  if [ "$COMMAND" = "add" ]; then
+    local NAME=$2
+    KEEP_NO_RENAME_STUB="$KEEP_NO_RENAME_STUB $NAME"
+  elif [ "$COMMAND" = "do" ] || [ "$COMMAND" = "revert" ]; then
+    local DIR=$2
+    for stub in $KEEP_NO_RENAME_STUB; do
+      local new_stub=`echo $stub | sed -e "s/$OLD_PATTERN/$TMP_PATTERN/g"`
+      if [ "$COMMAND" = "do" ]; then
+        replace $DIR $stub $new_stub
+      else
+        replace $DIR $new_stub $stub
+      fi
+    done
+  fi
+}
+
+for API in $API_LEVELS; do
+  log2 "API: $API"
+  OLD_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/arch-$OLD_ARCH
+  NEW_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/arch-$NEW_ARCH
+  if [ -d "$OLD_PLATFORM_DIR/symbols" ]; then
+    run copy_directory $OLD_PLATFORM_DIR/symbols $NEW_PLATFORM_DIR/symbols
+  fi
+  if [ -d "$OLD_PLATFORM_DIR/include" ]; then
+    run copy_directory $OLD_PLATFORM_DIR/include $NEW_PLATFORM_DIR/include
+  fi
+
+  PATCH_PLATFORM_DIR=$DEV_NDK_PLATFORM_DIR/android-$API/header-patches
+  if [ -d "$PATCH_PLATFORM_DIR" ]; then
+    run rm -rf $NEW_PLATFORM_DIR/include/asm/arch
+    for asm_header_stem in domain dyntick fpstate glue hardware ide mtd-xip pgtable-hwdef proc-fns procinfo suspend topology vga; do
+      run rm -f $NEW_PLATFORM_DIR/include/asm/${asm_header_stem}.h
+    done
+    for machine_header_stem in cpu-features; do
+      run rm -f $NEW_PLATFORM_DIR/include/machine/${machine_header_stem}.h
+    done
+
+    # Some keyword should not be replaced nor droped
+    replace_stub_to_temp add ARMv6
+    replace_stub_to_temp add PARMRK
+    replace_stub_to_temp add __ARM_EABI__
+    replace_stub_to_temp add __ARMEB__
+    replace_stub_to_temp add __LINUX_ARM_ARCH__
+    replace_stub_to_temp do $NEW_PLATFORM_DIR/include
+
+    remove $NEW_PLATFORM_DIR/include "#include" glue.h
+    remove $NEW_PLATFORM_DIR/include "#include" arch
+    remove $NEW_PLATFORM_DIR/include "#include" mach
+    remove $NEW_PLATFORM_DIR/include "#include" domain.h
+    remove $NEW_PLATFORM_DIR/include "#include" pgtable-hwdef.h
+    remove $NEW_PLATFORM_DIR/include "#include" proc-fns.h
+
+    remove $NEW_PLATFORM_DIR/include "#define" EM_ARM
+    remove $NEW_PLATFORM_DIR/include "#define" MODULE_ARCH_VERMAGIC
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_BASE
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_breakpoint
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_cacheflush
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_usr26
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_usr32
+    remove $NEW_PLATFORM_DIR/include "#define" __ARM_NR_set_tls
+
+    replace $NEW_PLATFORM_DIR $OLD_PATTERN $NEW_PATTERN
+    replace_stub_to_temp revert $NEW_PLATFORM_DIR/include
+
+    FILES="`cd $PATCH_PLATFORM_DIR && find include -type f | xargs`"
+    for FILE in $FILES; do
+      run mkdir -p $NEW_PLATFORM_DIR/`dirname $FILE`
+      run cp -a $PATCH_PLATFORM_DIR/$FILE $NEW_PLATFORM_DIR/$FILE
+    done
+
+    PATCH_FILE=$PATCH_PLATFORM_DIR/headers.patch
+    if [ -f "$PATCH_FILE" ]; then
+      cd $NEW_PLATFORM_DIR && patch -p1 -s < $PATCH_FILE
+    fi
+  fi
+done
diff --git a/build/tools/download-toolchain-sources.sh b/build/tools/download-toolchain-sources.sh
index 281f5ef..5822554 100755
--- a/build/tools/download-toolchain-sources.sh
+++ b/build/tools/download-toolchain-sources.sh
@@ -214,7 +214,7 @@
 toolchain_checkout "" $BRANCH isl .
 toolchain_checkout "" $BRANCH ppl .
 toolchain_checkout "" $BRANCH expat .
-toolchain_checkout "" $BRANCH binutils binutils-2.19 binutils-2.21 binutils-2.22 binutils-2.23
+toolchain_checkout "" $BRANCH binutils binutils-2.21 binutils-2.22 binutils-2.23
 toolchain_checkout "" $BRANCH gcc gcc-4.6 gcc-4.7 gcc-4.8
 toolchain_checkout "" $BRANCH gdb gdb-7.3.x gdb-7.6
 toolchain_checkout "" $BRANCH python Python-2.7.5
diff --git a/build/tools/toolchain-patches/mclinker/0005-Fixed-darwin-ld.mcld-GNU-m-emulation-fail.patch b/build/tools/toolchain-patches/mclinker/0005-Fixed-darwin-ld.mcld-GNU-m-emulation-fail.patch
index aff63e9..5d5c91d 100644
--- a/build/tools/toolchain-patches/mclinker/0005-Fixed-darwin-ld.mcld-GNU-m-emulation-fail.patch
+++ b/build/tools/toolchain-patches/mclinker/0005-Fixed-darwin-ld.mcld-GNU-m-emulation-fail.patch
@@ -15,7 +15,7 @@
 index b5152e6..f7ddd90 100644
 --- a/tools/llvm-mcld/llvm-mcld.cpp
 +++ b/tools/llvm-mcld/llvm-mcld.cpp
-@@ -1018,12 +1018,12 @@ static Triple ParseEmulation(const std::string& pEmulation)
+@@ -1019,12 +1019,12 @@ static Triple ParseEmulation(const std::string& pEmulation)
  {
    Triple result = StringSwitch<Triple>(pEmulation)
      .Case("armelf_linux_eabi", Triple("arm", "", "linux", "gnueabi"))
diff --git a/tests/abcc/build-abcc.sh b/tests/abcc/build-abcc.sh
index 178a5e0..bfddf53 100755
--- a/tests/abcc/build-abcc.sh
+++ b/tests/abcc/build-abcc.sh
@@ -138,7 +138,7 @@
 
 for ABI in $ABIS; do
   run rm -f obj libs
-  run $NDK_DIR/ndk-build -B APP_ABI=$ABI APP_STL=stlport_static SYSTEM_PREBUILT_PACKAGE=false -C jni
+  run $NDK_DIR/ndk-build -B APP_ABI=$ABI -C jni
   if [ "$DEBUG" = "yes" ]; then
     run rm -f AndroidManifest.xml
     run cp -a AndroidManifest.xml.debug AndroidManifest.xml
diff --git a/tests/abcc/jni/Application.mk b/tests/abcc/jni/Application.mk
new file mode 100644
index 0000000..abf2e06
--- /dev/null
+++ b/tests/abcc/jni/Application.mk
@@ -0,0 +1,3 @@
+APP_ABI := all
+APP_STL := stlport_static
+SYSTEM_PREBUILT_PACKAGE := false
diff --git a/tests/build/test-c-only-flags/jni/Android.mk b/tests/build/test-c-only-flags/jni/Android.mk
new file mode 100644
index 0000000..f413313
--- /dev/null
+++ b/tests/build/test-c-only-flags/jni/Android.mk
@@ -0,0 +1,14 @@
+LOCAL_PATH := $(call my-dir)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := test-c-only-flags
+LOCAL_SRC_FILES := test-c-only-flags.cpp
+LOCAL_CFLAGS += -Werror
+
+# LOCAL_CFLAGS is passed for C++ compilation as well.  This is by design
+# to save developers from the trouble of specifying flags twice for both
+# LOCAL_CFLAGS and LOCAL_CPPFLAGS.  Unfortunately for flags like -std=gnu99
+# is harmful to g++ (warning) and clang++ (error)
+LOCAL_CONLYFLAGS += -std=gnu99
+
+include $(BUILD_EXECUTABLE)
diff --git a/tests/build/test-c-only-flags/jni/Application.mk b/tests/build/test-c-only-flags/jni/Application.mk
new file mode 100644
index 0000000..a252a72
--- /dev/null
+++ b/tests/build/test-c-only-flags/jni/Application.mk
@@ -0,0 +1 @@
+APP_ABI := all
diff --git a/tests/build/test-c-only-flags/jni/test-c-only-flags.cpp b/tests/build/test-c-only-flags/jni/test-c-only-flags.cpp
new file mode 100644
index 0000000..da73754
--- /dev/null
+++ b/tests/build/test-c-only-flags/jni/test-c-only-flags.cpp
@@ -0,0 +1,20 @@
+/*
+ * 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.
+ */
+#include <cstdlib>
+
+int main()
+{
+}