Merge "Generate a libc++ linker script per API level."
diff --git a/build/tools/make_standalone_toolchain.py b/build/tools/make_standalone_toolchain.py
index 6ac7585..d700428 100755
--- a/build/tools/make_standalone_toolchain.py
+++ b/build/tools/make_standalone_toolchain.py
@@ -305,19 +305,6 @@
     return dst_libdir
 
 
-def fix_linker_script(path):
-    """Remove libandroid_support from the given linker script.
-
-    See https://github.com/android-ndk/ndk/issues/672 or the comment in
-    copy_libcxx_libs for more details.
-    """
-    with open(path, 'r+') as script:
-        contents = script.read()
-        script.seek(0)
-        script.write(contents.replace('-landroid_support', ''))
-        script.truncate()
-
-
 def copy_libcxx_libs(src_dir, dst_dir, abi, api):
     shutil.copy2(os.path.join(src_dir, 'libc++_shared.so'), dst_dir)
     shutil.copy2(os.path.join(src_dir, 'libc++_static.a'), dst_dir)
@@ -339,22 +326,11 @@
     # Unlike the other STLs, also copy libc++.so (another linker script) over
     # as libstdc++.so.  Since it's a linker script, the linker will still get
     # the right DT_NEEDED from the SONAME of the actual linked object.
-    shutil.copy2(os.path.join(src_dir, 'libc++.a'),
+    shutil.copy2(os.path.join(src_dir, 'libc++.a.{}'.format(api)),
                  os.path.join(dst_dir, 'libstdc++.a'))
-    shutil.copy2(os.path.join(src_dir, 'libc++.so'),
+    shutil.copy2(os.path.join(src_dir, 'libc++.so.{}'.format(api)),
                  os.path.join(dst_dir, 'libstdc++.so'))
 
-    # TODO: Find a better fix for r18.
-    # https://github.com/android-ndk/ndk/issues/672
-    # The linker scripts in the NDK distribution are not correct for LP32 API
-    # 21+. In this case, rewrite the linker script to not link
-    # libandroid_support. We do this rather than generating our own linker
-    # scripts to avoid issues of updating one template and forgetting the
-    # other.
-    if '64' not in abi and api >= 21:
-        fix_linker_script(os.path.join(dst_dir, 'libstdc++.a'))
-        fix_linker_script(os.path.join(dst_dir, 'libstdc++.so'))
-
 
 def create_toolchain(install_path, arch, api, gcc_path, clang_path,
                      platforms_path, host_tag):
diff --git a/checkbuild.py b/checkbuild.py
index e785049..7f67c35 100755
--- a/checkbuild.py
+++ b/checkbuild.py
@@ -672,19 +672,19 @@
 
 
 def make_linker_script(path, libs):
-    with open(path, 'w') as linker_script:
-        linker_script.write('INPUT({})\n'.format(' '.join(libs)))
+    ndk.file.write_file(path, 'INPUT({})\n'.format(' '.join(libs)))
 
 
-def create_libcxx_linker_scripts(lib_dir, abi):
+def create_libcxx_linker_scripts(lib_dir, abi, api):
     static_libs = ['-lc++_static', '-lc++abi']
     is_arm = abi == 'armeabi-v7a'
-    needs_android_support = abi in ndk.abis.LP32_ABIS
+    needs_android_support = api < 21
     if needs_android_support:
         static_libs.append('-landroid_support')
     if is_arm:
         static_libs.extend(['-lunwind', '-ldl', '-latomic'])
-    make_linker_script(os.path.join(lib_dir, 'libc++.a'), static_libs)
+    make_linker_script(
+        os.path.join(lib_dir, 'libc++.a.{}'.format(api)), static_libs)
 
     shared_libs = []
     if needs_android_support:
@@ -692,7 +692,8 @@
     if is_arm:
         shared_libs.extend(['-lunwind', '-latomic'])
     shared_libs.append('-lc++_shared')
-    make_linker_script(os.path.join(lib_dir, 'libc++.so'), shared_libs)
+    make_linker_script(
+        os.path.join(lib_dir, 'libc++.so.{}'.format(api)), shared_libs)
 
 
 class Libcxx(ndk.builds.Module):
@@ -805,7 +806,13 @@
             # things properly even when we're not using ndk-build. The linker
             # will read the script in place of the library so that we link the
             # unwinder and other support libraries appropriately.
-            create_libcxx_linker_scripts(lib_dir, abi)
+            platforms_meta = json.loads(
+                ndk.file.read_file(ndk.paths.ndk_path('meta/platforms.json')))
+            for api in range(platforms_meta['min'], platforms_meta['max'] + 1):
+                if api < ndk.abis.min_api_for_abi(abi):
+                    continue
+
+                create_libcxx_linker_scripts(lib_dir, abi, api)
 
     def install_static_libs(self, lib_dir, abi):
         static_lib_dir = os.path.join(self.obj_out, 'local', abi)