Merge "Remove unused "product_services_specific" prop"
diff --git a/Android.bp b/Android.bp
index 8d0c1ea..c73c3da 100644
--- a/Android.bp
+++ b/Android.bp
@@ -206,6 +206,7 @@
     ],
     testSrcs: [
         "cc/cc_test.go",
+        "cc/compiler_test.go",
         "cc/gen_test.go",
         "cc/genrule_test.go",
         "cc/library_test.go",
diff --git a/apex/apex.go b/apex/apex.go
index f03a8f9..13e4fb7 100644
--- a/apex/apex.go
+++ b/apex/apex.go
@@ -1217,7 +1217,7 @@
 			if am, ok := child.(android.ApexModule); ok {
 				// We cannot use a switch statement on `depTag` here as the checked
 				// tags used below are private (e.g. `cc.sharedDepTag`).
-				if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) || java.IsJniDepTag(depTag) {
+				if cc.IsSharedDepTag(depTag) || cc.IsRuntimeDepTag(depTag) {
 					if cc, ok := child.(*cc.Module); ok {
 						if android.InList(cc.Name(), providedNativeSharedLibs) {
 							// If we're using a shared library which is provided from other APEX,
@@ -1254,6 +1254,8 @@
 						filesInfo = append(filesInfo, apexFile{fileToCopy, moduleName, dirInApex, nativeTest, cc, nil})
 						return true
 					}
+				} else if java.IsJniDepTag(depTag) {
+					// Do nothing for JNI dep. JNI libraries are always embedded in APK-in-APEX.
 				} else if am.CanHaveApexVariants() && am.IsInstallableToApex() {
 					ctx.ModuleErrorf("unexpected tag %q for indirect dependency %q", depTag, depName)
 				}
diff --git a/apex/apex_test.go b/apex/apex_test.go
index 614164d..330dc3c 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -2538,7 +2538,17 @@
 
 	ensureContains(t, copyCmds, "image.apex/app/AppFoo/AppFoo.apk")
 	ensureContains(t, copyCmds, "image.apex/priv-app/AppFooPriv/AppFooPriv.apk")
-	ensureContains(t, copyCmds, "image.apex/lib64/libjni.so")
+
+	// JNI libraries are embedded inside APK
+	appZipRule := ctx.ModuleForTests("AppFoo", "android_common_myapex").Rule("zip")
+	libjniOutput := ctx.ModuleForTests("libjni", "android_arm64_armv8-a_core_shared_myapex").Module().(*cc.Module).OutputFile()
+	ensureListContains(t, appZipRule.Implicits.Strings(), libjniOutput.String())
+	// ... uncompressed
+	if args := appZipRule.Args["jarArgs"]; !strings.Contains(args, "-L 0") {
+		t.Errorf("jni lib is not uncompressed for AppFoo")
+	}
+	// ... and not directly inside the APEX
+	ensureNotContains(t, copyCmds, "image.apex/lib64/libjni.so")
 }
 
 func TestApexWithAppImports(t *testing.T) {
diff --git a/bpfix/Android.bp b/bpfix/Android.bp
index aec9ff9..e291578 100644
--- a/bpfix/Android.bp
+++ b/bpfix/Android.bp
@@ -28,9 +28,9 @@
 
 bootstrap_go_package {
     name: "bpfix-cmd",
-    pkgPath: "android/soong/bpfix/bpfix/cmd",
+    pkgPath: "android/soong/bpfix/cmd_lib",
     srcs: [
-        "cmd-lib/bpfix.go",
+        "cmd_lib/bpfix.go",
     ],
     deps: [
         "bpfix-lib",
diff --git a/bpfix/cmd/main.go b/bpfix/cmd/main.go
index 8ca16b4..ad68144 100644
--- a/bpfix/cmd/main.go
+++ b/bpfix/cmd/main.go
@@ -16,10 +16,8 @@
 
 package main
 
-import (
-	"android/soong/bpfix/bpfix/cmd"
-)
+import "android/soong/bpfix/cmd_lib"
 
 func main() {
-	cmd.Run()
+	cmd_lib.Run()
 }
diff --git a/bpfix/cmd-lib/bpfix.go b/bpfix/cmd_lib/bpfix.go
similarity index 99%
rename from bpfix/cmd-lib/bpfix.go
rename to bpfix/cmd_lib/bpfix.go
index 98122f2..f90f65b 100644
--- a/bpfix/cmd-lib/bpfix.go
+++ b/bpfix/cmd_lib/bpfix.go
@@ -16,7 +16,7 @@
 
 // TODO(jeffrygaston) should this file be consolidated with bpfmt.go?
 
-package cmd
+package cmd_lib
 
 import (
 	"bytes"
diff --git a/cc/compiler.go b/cc/compiler.go
index bb40a5b..671861b 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -17,6 +17,7 @@
 import (
 	"fmt"
 	"path/filepath"
+	"regexp"
 	"strconv"
 	"strings"
 
@@ -252,6 +253,7 @@
 // per-target values, module type values, and per-module Blueprints properties
 func (compiler *baseCompiler) compilerFlags(ctx ModuleContext, flags Flags, deps PathDeps) Flags {
 	tc := ctx.toolchain()
+	modulePath := android.PathForModuleSrc(ctx).String()
 
 	compiler.srcsBeforeGen = android.PathsForModuleSrcExcludes(ctx, compiler.Properties.Srcs, compiler.Properties.Exclude_srcs)
 	compiler.srcsBeforeGen = append(compiler.srcsBeforeGen, deps.GeneratedSources...)
@@ -289,8 +291,8 @@
 
 	if compiler.Properties.Include_build_directory == nil ||
 		*compiler.Properties.Include_build_directory {
-		flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+android.PathForModuleSrc(ctx).String())
-		flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+android.PathForModuleSrc(ctx).String())
+		flags.Local.CommonFlags = append(flags.Local.CommonFlags, "-I"+modulePath)
+		flags.Local.YasmFlags = append(flags.Local.YasmFlags, "-I"+modulePath)
 	}
 
 	if !(ctx.useSdk() || ctx.useVndk()) || ctx.Host() {
@@ -381,7 +383,7 @@
 		"${config.CommonClangGlobalCflags}",
 		fmt.Sprintf("${config.%sClangGlobalCflags}", hod))
 
-	if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
+	if isThirdParty(modulePath) {
 		flags.Global.CommonFlags = append([]string{"${config.ClangExternalCflags}"}, flags.Global.CommonFlags...)
 	}
 
@@ -438,7 +440,7 @@
 	// vendor/device specific things), we could extend this to be a ternary
 	// value.
 	strict := true
-	if strings.HasPrefix(android.PathForModuleSrc(ctx).String(), "external/") {
+	if strings.HasPrefix(modulePath, "external/") {
 		strict = false
 	}
 
@@ -580,3 +582,28 @@
 
 	return TransformSourceToObj(ctx, subdir, srcFiles, flags, pathDeps, cFlagsDeps)
 }
+
+var thirdPartyDirPrefixExceptions = []*regexp.Regexp{
+	regexp.MustCompile("^vendor/[^/]*google[^/]*/"),
+	regexp.MustCompile("^hardware/google/"),
+	regexp.MustCompile("^hardware/interfaces/"),
+	regexp.MustCompile("^hardware/libhardware[^/]*/"),
+	regexp.MustCompile("^hardware/ril/"),
+}
+
+func isThirdParty(path string) bool {
+	thirdPartyDirPrefixes := []string{"external/", "vendor/", "hardware/"}
+
+	for _, prefix := range thirdPartyDirPrefixes {
+		if strings.HasPrefix(path, prefix) {
+			for _, prefix := range thirdPartyDirPrefixExceptions {
+				if prefix.MatchString(path) {
+					return false
+				}
+			}
+			break
+		}
+	}
+
+	return true
+}
diff --git a/cc/compiler_test.go b/cc/compiler_test.go
new file mode 100644
index 0000000..c301388
--- /dev/null
+++ b/cc/compiler_test.go
@@ -0,0 +1,43 @@
+// Copyright 2019 Google Inc. All rights reserved.
+//
+// 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.
+
+package cc
+
+import (
+	"testing"
+)
+
+func TestIsThirdParty(t *testing.T) {
+	shouldFail := []string{
+		"external/foo/",
+		"vendor/bar/",
+		"hardware/underwater_jaguar/",
+	}
+	shouldPass := []string{
+		"vendor/google/cts/",
+		"hardware/google/pixel",
+		"hardware/interfaces/camera",
+		"hardware/ril/supa_ril",
+	}
+	for _, path := range shouldFail {
+		if !isThirdParty(path) {
+			t.Errorf("Expected %s to be considered third party", path)
+		}
+	}
+	for _, path := range shouldPass {
+		if isThirdParty(path) {
+			t.Errorf("Expected %s to *not* be considered third party", path)
+		}
+	}
+}
diff --git a/cc/config/clang.go b/cc/config/clang.go
index 030a076..eddc341 100644
--- a/cc/config/clang.go
+++ b/cc/config/clang.go
@@ -163,8 +163,8 @@
 		"-Wno-tautological-type-limit-compare",
 	}, " "))
 
-	// Extra cflags for projects under external/ directory to disable warnings that are infeasible
-	// to fix in all the external projects and their upstream repos.
+	// Extra cflags for external third-party projects to disable warnings that
+	// are infeasible to fix in all the external projects and their upstream repos.
 	pctx.StaticVariable("ClangExtraExternalCflags", strings.Join([]string{
 		"-Wno-enum-compare",
 		"-Wno-enum-compare-switch",
diff --git a/cc/fuzz.go b/cc/fuzz.go
index 11e6413..4b537c0 100644
--- a/cc/fuzz.go
+++ b/cc/fuzz.go
@@ -338,6 +338,9 @@
 		collectAllSharedDependencies(module, sharedLibraries, ctx)
 
 		for _, library := range sharedLibraries {
+			archDirs[archDir] = append(archDirs[archDir],
+				fileToZip{library, ccModule.Name() + "/lib"})
+
 			if _, exists := archSharedLibraryDeps[archAndLibraryKey{archDir, library}]; exists {
 				continue
 			}
@@ -379,12 +382,6 @@
 		}
 	})
 
-	// Add the shared library deps for packaging.
-	for key, _ := range archSharedLibraryDeps {
-		archDirs[key.ArchDir] = append(archDirs[key.ArchDir],
-			fileToZip{key.Library, "lib"})
-	}
-
 	for archDir, filesToZip := range archDirs {
 		arch := archDir.Base()
 		hostOrTarget := filepath.Base(filepath.Dir(archDir.String()))
diff --git a/java/androidmk.go b/java/androidmk.go
index 0510680..c973739 100644
--- a/java/androidmk.go
+++ b/java/androidmk.go
@@ -262,6 +262,11 @@
 }
 
 func (app *AndroidApp) AndroidMkEntries() android.AndroidMkEntries {
+	if !app.IsForPlatform() {
+		return android.AndroidMkEntries{
+			Disabled: true,
+		}
+	}
 	return android.AndroidMkEntries{
 		Class:      "APPS",
 		OutputFile: android.OptionalPathForPath(app.outputFile),
diff --git a/java/app.go b/java/app.go
index d53d626..e9ef9eb 100644
--- a/java/app.go
+++ b/java/app.go
@@ -78,8 +78,9 @@
 
 	// Store native libraries uncompressed in the APK and set the android:extractNativeLibs="false" manifest
 	// flag so that they are used from inside the APK at runtime.  Defaults to true for android_test modules unless
-	// sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to false for other
-	// module types where the native libraries are generally preinstalled outside the APK.
+	// sdk_version or min_sdk_version is set to a version that doesn't support it (<23), defaults to true for
+	// android_app modules that are embedded to APEXes, defaults to false for other module types where the native
+	// libraries are generally preinstalled outside the APK.
 	Use_embedded_native_libs *bool
 
 	// Store dex files uncompressed in the APK and set the android:useEmbeddedDex="true" manifest attribute so that
@@ -217,7 +218,8 @@
 		ctx.PropertyErrorf("min_sdk_version", "invalid value %q: %s", a.minSdkVersion(), err)
 	}
 
-	return minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)
+	return (minSdkVersion >= 23 && Bool(a.appProperties.Use_embedded_native_libs)) ||
+		!a.IsForPlatform()
 }
 
 // Returns whether this module should have the dex file stored uncompressed in the APK.
@@ -241,7 +243,7 @@
 
 func (a *AndroidApp) shouldEmbedJnis(ctx android.BaseModuleContext) bool {
 	return ctx.Config().UnbundledBuild() || Bool(a.appProperties.Use_embedded_native_libs) ||
-		a.appProperties.AlwaysPackageNativeLibs
+		!a.IsForPlatform() || a.appProperties.AlwaysPackageNativeLibs
 }
 
 func (a *AndroidApp) aaptBuildActions(ctx android.ModuleContext) {
@@ -585,6 +587,7 @@
 	android.InitAndroidMultiTargetsArchModule(module, android.DeviceSupported, android.MultilibCommon)
 	android.InitDefaultableModule(module)
 	android.InitOverridableModule(module, &module.appProperties.Overrides)
+	android.InitApexModule(module)
 
 	return module
 }
diff --git a/androidmk/partner_androidmk/Android.bp b/partner/Android.bp
similarity index 71%
rename from androidmk/partner_androidmk/Android.bp
rename to partner/Android.bp
index 532116a..f2ced8d 100644
--- a/androidmk/partner_androidmk/Android.bp
+++ b/partner/Android.bp
@@ -1,4 +1,4 @@
-// Copyright 2015 Google Inc. All rights reserved.
+// Copyright 2019 Google Inc. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -19,31 +19,31 @@
 blueprint_go_binary {
     name: "partner_androidmk",
     srcs: [
-        "partner_androidmk/androidmk.go",
+        "androidmk/androidmk.go",
     ],
     testSrcs: [
-        "partner_androidmk/androidmk_test.go",
+        "androidmk/androidmk_test.go",
     ],
     deps: [
         "androidmk-lib",
-        "partner_bpfix_extensions",
+        "partner-bpfix-extensions",
     ],
 }
 
 blueprint_go_binary {
     name: "partner_bpfix",
     srcs: [
-        "partner_bpfix/bpfix.go",
+        "bpfix/bpfix.go",
     ],
     deps: [
         "bpfix-cmd",
-        "partner_bpfix_extensions",
+        "partner-bpfix-extensions",
     ],
 }
 
 bootstrap_go_package {
-    name: "partner_bpfix_extensions",
-    pkgPath: "partner/android/bpfix/extensions",
-    srcs: ["fixes/headers.go"],
+    name: "partner-bpfix-extensions",
+    pkgPath: "android/soong/partner/bpfix/extensions",
+    srcs: ["bpfix/extensions/headers.go"],
     deps: ["bpfix-lib"],
 }
diff --git a/androidmk/partner_androidmk/partner_androidmk/androidmk.go b/partner/androidmk/androidmk.go
similarity index 96%
rename from androidmk/partner_androidmk/partner_androidmk/androidmk.go
rename to partner/androidmk/androidmk.go
index af8cdf3..f49981b 100644
--- a/androidmk/partner_androidmk/partner_androidmk/androidmk.go
+++ b/partner/androidmk/androidmk.go
@@ -23,7 +23,7 @@
 
 	"android/soong/androidmk/androidmk"
 
-	_ "partner/android/bpfix/extensions"
+	_ "android/soong/partner/bpfix/extensions"
 )
 
 var usage = func() {
diff --git a/androidmk/partner_androidmk/partner_androidmk/androidmk_test.go b/partner/androidmk/androidmk_test.go
similarity index 97%
rename from androidmk/partner_androidmk/partner_androidmk/androidmk_test.go
rename to partner/androidmk/androidmk_test.go
index ff04e88..6bae836 100644
--- a/androidmk/partner_androidmk/partner_androidmk/androidmk_test.go
+++ b/partner/androidmk/androidmk_test.go
@@ -23,7 +23,7 @@
 	"android/soong/androidmk/androidmk"
 	"android/soong/bpfix/bpfix"
 
-	_ "partner/android/bpfix/extensions"
+	_ "android/soong/partner/bpfix/extensions"
 )
 
 var testCases = []struct {
diff --git a/androidmk/partner_androidmk/partner_bpfix/bpfix.go b/partner/bpfix/bpfix.go
similarity index 88%
rename from androidmk/partner_androidmk/partner_bpfix/bpfix.go
rename to partner/bpfix/bpfix.go
index 2c8e0a8..687fe1c 100644
--- a/androidmk/partner_androidmk/partner_bpfix/bpfix.go
+++ b/partner/bpfix/bpfix.go
@@ -17,11 +17,11 @@
 package main
 
 import (
-	"android/soong/bpfix/bpfix/cmd"
+	"android/soong/bpfix/cmd_lib"
 
-	_ "partner/android/bpfix/extensions"
+	_ "android/soong/partner/bpfix/extensions"
 )
 
 func main() {
-	cmd.Run()
+	cmd_lib.Run()
 }
diff --git a/androidmk/partner_androidmk/fixes/headers.go b/partner/bpfix/extensions/headers.go
similarity index 100%
rename from androidmk/partner_androidmk/fixes/headers.go
rename to partner/bpfix/extensions/headers.go