Merge "pom2bp: clean up templates"
diff --git a/android/mutator.go b/android/mutator.go
index 64d9fdd..b9c44e8 100644
--- a/android/mutator.go
+++ b/android/mutator.go
@@ -205,7 +205,7 @@
 }
 
 func depsMutator(ctx BottomUpMutatorContext) {
-	if m, ok := ctx.Module().(Module); ok {
+	if m, ok := ctx.Module().(Module); ok && m.Enabled() {
 		m.DepsMutator(ctx)
 	}
 }
diff --git a/cc/cc.go b/cc/cc.go
index befd683..2d967ed 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -585,6 +585,9 @@
 	if inList(ctx.baseModuleName(), llndkLibraries) {
 		return true
 	}
+	if inList(ctx.baseModuleName(), ndkMigratedLibs) {
+		return true
+	}
 	if ctx.useVndk() && ctx.isVndk() {
 		// Return true if this is VNDK-core, VNDK-SP, or VNDK-Ext and this is not
 		// VNDK-private.
@@ -922,10 +925,6 @@
 }
 
 func (c *Module) DepsMutator(actx android.BottomUpMutatorContext) {
-	if !c.Enabled() {
-		return
-	}
-
 	ctx := &depsContext{
 		BottomUpMutatorContext: actx,
 		moduleContextImpl: moduleContextImpl{
diff --git a/cc/config/arm64_device.go b/cc/config/arm64_device.go
index f412583..6fdd524 100644
--- a/cc/config/arm64_device.go
+++ b/cc/config/arm64_device.go
@@ -238,7 +238,7 @@
 	return t.toolchainClangCflags
 }
 
-func (toolchainArm64) SanitizerRuntimeLibraryArch() string {
+func (toolchainArm64) LibclangRuntimeLibraryArch() string {
 	return "aarch64"
 }
 
diff --git a/cc/config/arm_device.go b/cc/config/arm_device.go
index 4719fb7..4135179 100644
--- a/cc/config/arm_device.go
+++ b/cc/config/arm_device.go
@@ -397,7 +397,7 @@
 	}
 }
 
-func (toolchainArm) SanitizerRuntimeLibraryArch() string {
+func (toolchainArm) LibclangRuntimeLibraryArch() string {
 	return "arm"
 }
 
diff --git a/cc/config/mips64_device.go b/cc/config/mips64_device.go
index 6fa2571..0c4640b 100644
--- a/cc/config/mips64_device.go
+++ b/cc/config/mips64_device.go
@@ -160,7 +160,7 @@
 	return "${config.Mips64ClangLdflags}"
 }
 
-func (toolchainMips64) SanitizerRuntimeLibraryArch() string {
+func (toolchainMips64) LibclangRuntimeLibraryArch() string {
 	return "mips64"
 }
 
diff --git a/cc/config/mips_device.go b/cc/config/mips_device.go
index b815fc6..eb44fd5 100644
--- a/cc/config/mips_device.go
+++ b/cc/config/mips_device.go
@@ -210,7 +210,7 @@
 	return "${config.MipsClangLdflags}"
 }
 
-func (toolchainMips) SanitizerRuntimeLibraryArch() string {
+func (toolchainMips) LibclangRuntimeLibraryArch() string {
 	return "mips"
 }
 
diff --git a/cc/config/toolchain.go b/cc/config/toolchain.go
index 39b5df4..fefe7c2 100644
--- a/cc/config/toolchain.go
+++ b/cc/config/toolchain.go
@@ -79,7 +79,7 @@
 	ShlibSuffix() string
 	ExecutableSuffix() string
 
-	SanitizerRuntimeLibraryArch() string
+	LibclangRuntimeLibraryArch() string
 
 	AvailableLibraries() []string
 
@@ -156,7 +156,7 @@
 	return ""
 }
 
-func (toolchainBase) SanitizerRuntimeLibraryArch() string {
+func (toolchainBase) LibclangRuntimeLibraryArch() string {
 	return ""
 }
 
@@ -214,44 +214,48 @@
 	return list
 }
 
-func SanitizerRuntimeLibrary(t Toolchain, sanitizer string) string {
-	arch := t.SanitizerRuntimeLibraryArch()
+func LibclangRuntimeLibrary(t Toolchain, library string) string {
+	arch := t.LibclangRuntimeLibraryArch()
 	if arch == "" {
 		return ""
 	}
-	return "libclang_rt." + sanitizer + "-" + arch + "-android"
+	return "libclang_rt." + library + "-" + arch + "-android"
+}
+
+func BuiltinsRuntimeLibrary(t Toolchain) string {
+	return LibclangRuntimeLibrary(t, "builtins")
 }
 
 func AddressSanitizerRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "asan")
+	return LibclangRuntimeLibrary(t, "asan")
 }
 
 func HWAddressSanitizerRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "hwasan")
+	return LibclangRuntimeLibrary(t, "hwasan")
 }
 
 func HWAddressSanitizerStaticLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "hwasan_static")
+	return LibclangRuntimeLibrary(t, "hwasan_static")
 }
 
 func UndefinedBehaviorSanitizerRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "ubsan_standalone")
+	return LibclangRuntimeLibrary(t, "ubsan_standalone")
 }
 
 func UndefinedBehaviorSanitizerMinimalRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "ubsan_minimal")
+	return LibclangRuntimeLibrary(t, "ubsan_minimal")
 }
 
 func ThreadSanitizerRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "tsan")
+	return LibclangRuntimeLibrary(t, "tsan")
 }
 
 func ProfileRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "profile")
+	return LibclangRuntimeLibrary(t, "profile")
 }
 
 func ScudoRuntimeLibrary(t Toolchain) string {
-	return SanitizerRuntimeLibrary(t, "scudo")
+	return LibclangRuntimeLibrary(t, "scudo")
 }
 
 func ToolPath(t Toolchain) string {
diff --git a/cc/config/x86_64_device.go b/cc/config/x86_64_device.go
index ba03094..5e2dc49 100644
--- a/cc/config/x86_64_device.go
+++ b/cc/config/x86_64_device.go
@@ -227,7 +227,7 @@
 	return "${config.X86_64YasmFlags}"
 }
 
-func (toolchainX86_64) SanitizerRuntimeLibraryArch() string {
+func (toolchainX86_64) LibclangRuntimeLibraryArch() string {
 	return "x86_64"
 }
 
diff --git a/cc/config/x86_device.go b/cc/config/x86_device.go
index 15188cf..ffdf8ea 100644
--- a/cc/config/x86_device.go
+++ b/cc/config/x86_device.go
@@ -251,7 +251,7 @@
 	return "${config.X86YasmFlags}"
 }
 
-func (toolchainX86) SanitizerRuntimeLibraryArch() string {
+func (toolchainX86) LibclangRuntimeLibraryArch() string {
 	return "i686"
 }
 
diff --git a/cc/config/x86_windows_host.go b/cc/config/x86_windows_host.go
index 4cb8fa4..9003e85 100644
--- a/cc/config/x86_windows_host.go
+++ b/cc/config/x86_windows_host.go
@@ -62,6 +62,8 @@
 
 	windowsLdflags = []string{
 		"--enable-stdcall-fixup",
+		"-Wl,--dynamicbase",
+		"-Wl,--nxcompat",
 	}
 	windowsClangLdflags  = append(ClangFilterUnknownCflags(windowsLdflags), []string{}...)
 	windowsClangLldflags = ClangFilterUnknownLldflags(windowsClangLdflags)
@@ -96,6 +98,7 @@
 		"-m64",
 		"-L${WindowsGccRoot}/${WindowsGccTriple}/lib64",
 		"-static-libgcc",
+		"-Wl,--high-entropy-va",
 	}
 	windowsX8664ClangLdflags = append(ClangFilterUnknownCflags(windowsX8664Ldflags), []string{
 		"-B${WindowsGccRoot}/${WindowsGccTriple}/bin",
diff --git a/cc/library.go b/cc/library.go
index 147dd8e..7ff7885 100644
--- a/cc/library.go
+++ b/cc/library.go
@@ -604,7 +604,7 @@
 }
 
 func getRefAbiDumpFile(ctx ModuleContext, vndkVersion, fileName string) android.Path {
-	isLlndk := inList(ctx.baseModuleName(), llndkLibraries)
+	isLlndk := inList(ctx.baseModuleName(), llndkLibraries) || inList(ctx.baseModuleName(), ndkMigratedLibs)
 
 	refAbiDumpTextFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, false)
 	refAbiDumpGzipFile := android.PathForVndkRefAbiDump(ctx, vndkVersion, fileName, isLlndk, true)
diff --git a/cc/prebuilt.go b/cc/prebuilt.go
index 3f277aa..3dd4d11 100644
--- a/cc/prebuilt.go
+++ b/cc/prebuilt.go
@@ -51,6 +51,24 @@
 
 var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
 
+func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {}
+
+func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
+	// export_header_lib_headers needs to be passed along
+	return Deps{
+		HeaderLibs:               p.baseLinker.Properties.Header_libs,
+		ReexportHeaderLibHeaders: p.baseLinker.Properties.Export_header_lib_headers,
+	}
+}
+
+func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
+	return Flags{}
+}
+
+func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
+	return p.libraryDecorator.linkerProps()
+}
+
 func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
 	flags Flags, deps PathDeps, objs Objects) android.Path {
 	// TODO(ccross): verify shared library dependencies
diff --git a/scripts/manifest_fixer.py b/scripts/manifest_fixer.py
index 25f96cd..db35c8d 100755
--- a/scripts/manifest_fixer.py
+++ b/scripts/manifest_fixer.py
@@ -53,6 +53,8 @@
                       help='manifest is for a static library')
   parser.add_argument('--uses-library', dest='uses_libraries', action='append',
                       help='specify additional <uses-library> tag to add')
+  parser.add_argument('--uses-non-sdk-api', dest='uses_non_sdk_api', action='store_true',
+                      help='manifest is for a package built against the platform')
   parser.add_argument('input', help='input AndroidManifest.xml file')
   parser.add_argument('output', help='output AndroidManifest.xml file')
   return parser.parse_args()
@@ -226,6 +228,33 @@
     indent = get_indent(application.previousSibling, 1)
     application.appendChild(doc.createTextNode(indent))
 
+def add_uses_non_sdk_api(doc):
+  """Add android:usesNonSdkApi=true attribute to <application>.
+
+  Args:
+    doc: The XML document. May be modified by this function.
+  Raises:
+    RuntimeError: Invalid manifest
+  """
+
+  manifest = parse_manifest(doc)
+  elems = get_children_with_tag(manifest, 'application')
+  application = elems[0] if len(elems) == 1 else None
+  if len(elems) > 1:
+    raise RuntimeError('found multiple <application> tags')
+  elif not elems:
+    application = doc.createElement('application')
+    indent = get_indent(manifest.firstChild, 1)
+    first = manifest.firstChild
+    manifest.insertBefore(doc.createTextNode(indent), first)
+    manifest.insertBefore(application, first)
+
+  attr = application.getAttributeNodeNS(android_ns, 'usesNonSdkApi')
+  if attr is None:
+    attr = doc.createAttributeNS(android_ns, 'android:usesNonSdkApi')
+    attr.value = 'true'
+    application.setAttributeNode(attr)
+
 
 def write_xml(f, doc):
   f.write('<?xml version="1.0" encoding="utf-8"?>\n')
@@ -248,6 +277,9 @@
     if args.uses_libraries:
       add_uses_libraries(doc, args.uses_libraries)
 
+    if args.uses_non_sdk_api:
+      add_uses_non_sdk_api(doc)
+
     with open(args.output, 'wb') as f:
       write_xml(f, doc)
 
diff --git a/scripts/manifest_fixer_test.py b/scripts/manifest_fixer_test.py
index 5908997..ac72e6d 100755
--- a/scripts/manifest_fixer_test.py
+++ b/scripts/manifest_fixer_test.py
@@ -310,5 +310,39 @@
     self.assertEqual(output, expected)
 
 
+class AddUsesNonSdkApiTest(unittest.TestCase):
+  """Unit tests for add_uses_libraries function."""
+
+  def run_test(self, input_manifest):
+    doc = minidom.parseString(input_manifest)
+    manifest_fixer.add_uses_non_sdk_api(doc)
+    output = StringIO.StringIO()
+    manifest_fixer.write_xml(output, doc)
+    return output.getvalue()
+
+  manifest_tmpl = (
+      '<?xml version="1.0" encoding="utf-8"?>\n'
+      '<manifest xmlns:android="http://schemas.android.com/apk/res/android">\n'
+      '    <application%s/>\n'
+      '</manifest>\n')
+
+  def uses_non_sdk_api(self, value):
+    return ' android:usesNonSdkApi="true"' if value else ''
+
+  def test_set_true(self):
+    """Empty new_uses_libraries must not touch the manifest."""
+    manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(False)
+    expected = self.manifest_tmpl % self.uses_non_sdk_api(True)
+    output = self.run_test(manifest_input)
+    self.assertEqual(output, expected)
+
+  def test_already_set(self):
+    """new_uses_libraries must not overwrite existing tags."""
+    manifest_input = self.manifest_tmpl % self.uses_non_sdk_api(True)
+    expected = manifest_input
+    output = self.run_test(manifest_input)
+    self.assertEqual(output, expected)
+
+
 if __name__ == '__main__':
   unittest.main()
diff --git a/ui/build/paths/config.go b/ui/build/paths/config.go
index d6adc66..7886466 100644
--- a/ui/build/paths/config.go
+++ b/ui/build/paths/config.go
@@ -40,12 +40,13 @@
 }
 
 // The configuration used if the tool is not listed in the config below.
-// Currently this will create the symlink, but log a warning. In the future,
-// I expect this to move closer to Forbidden.
+// Currently this will create the symlink, but log and error when it's used. In
+// the future, I expect the symlink to be removed, and this will be equivalent
+// to Forbidden.
 var Missing = PathConfig{
 	Symlink: true,
 	Log:     true,
-	Error:   false,
+	Error:   true,
 }
 
 func GetConfig(name string) PathConfig {
@@ -99,6 +100,7 @@
 	"mkdir":     Allowed,
 	"mktemp":    Allowed,
 	"mv":        Allowed,
+	"od":        Allowed,
 	"openssl":   Allowed,
 	"paste":     Allowed,
 	"patch":     Allowed,
@@ -129,6 +131,7 @@
 	"sum":       Allowed,
 	"tar":       Allowed,
 	"tail":      Allowed,
+	"tee":       Allowed,
 	"todos":     Allowed,
 	"touch":     Allowed,
 	"tr":        Allowed,