Merge "Create sanitizer variants of APEX only when SANITIZE_TARGET is set"
diff --git a/apex/apex_test.go b/apex/apex_test.go
index dbc85d9..4ca5b14 100644
--- a/apex/apex_test.go
+++ b/apex/apex_test.go
@@ -41,6 +41,7 @@
 
 	ctx.RegisterModuleType("cc_library", android.ModuleFactoryAdaptor(cc.LibraryFactory))
 	ctx.RegisterModuleType("cc_library_shared", android.ModuleFactoryAdaptor(cc.LibrarySharedFactory))
+	ctx.RegisterModuleType("cc_library_headers", android.ModuleFactoryAdaptor(cc.LibraryHeaderFactory))
 	ctx.RegisterModuleType("cc_binary", android.ModuleFactoryAdaptor(cc.BinaryFactory))
 	ctx.RegisterModuleType("cc_object", android.ModuleFactoryAdaptor(cc.ObjectFactory))
 	ctx.RegisterModuleType("llndk_library", android.ModuleFactoryAdaptor(cc.LlndkLibraryFactory))
@@ -123,16 +124,18 @@
 	`
 
 	ctx.MockFileSystem(map[string][]byte{
-		"Android.bp":                                []byte(bp),
-		"build/target/product/security":             nil,
-		"apex_manifest.json":                        nil,
-		"system/sepolicy/apex/myapex-file_contexts": nil,
-		"mylib.cpp":                                 nil,
-		"myprebuilt":                                nil,
-		"vendor/foo/devkeys/test.x509.pem":          nil,
-		"vendor/foo/devkeys/test.pk8":               nil,
-		"vendor/foo/devkeys/testkey.avbpubkey":      nil,
-		"vendor/foo/devkeys/testkey.pem":            nil,
+		"Android.bp":                                   []byte(bp),
+		"build/target/product/security":                nil,
+		"apex_manifest.json":                           nil,
+		"system/sepolicy/apex/myapex-file_contexts":    nil,
+		"system/sepolicy/apex/otherapex-file_contexts": nil,
+		"mylib.cpp":                                    nil,
+		"myprebuilt":                                   nil,
+		"my_include":                                   nil,
+		"vendor/foo/devkeys/test.x509.pem":             nil,
+		"vendor/foo/devkeys/test.pk8":                  nil,
+		"vendor/foo/devkeys/testkey.avbpubkey":         nil,
+		"vendor/foo/devkeys/testkey.pem":               nil,
 	})
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	android.FailIfErrored(t, errs)
@@ -729,3 +732,95 @@
 			"vendor/foo/devkeys/test.x509.pem vendor/foo/devkeys/test.pk8")
 	}
 }
+
+func TestMacro(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex {
+			name: "otherapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+	`)
+
+	// non-APEX variant does not have __ANDROID__APEX__ defined
+	mylibCFlags := ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
+	ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
+	ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
+
+	// APEX variant has __ANDROID_APEX__=<apexname> defined
+	mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_myapex").Rule("cc").Args["cFlags"]
+	ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
+	ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
+
+	// APEX variant has __ANDROID_APEX__=<apexname> defined
+	mylibCFlags = ctx.ModuleForTests("mylib", "android_arm64_armv8-a_core_static_otherapex").Rule("cc").Args["cFlags"]
+	ensureNotContains(t, mylibCFlags, "-D__ANDROID_APEX__=myapex")
+	ensureContains(t, mylibCFlags, "-D__ANDROID_APEX__=otherapex")
+}
+
+func TestHeaderLibsDependency(t *testing.T) {
+	ctx := testApex(t, `
+		apex {
+			name: "myapex",
+			key: "myapex.key",
+			native_shared_libs: ["mylib"],
+		}
+
+		apex_key {
+			name: "myapex.key",
+			public_key: "testkey.avbpubkey",
+			private_key: "testkey.pem",
+		}
+
+		cc_library_headers {
+			name: "mylib_headers",
+			export_include_dirs: ["my_include"],
+			system_shared_libs: [],
+			stl: "none",
+		}
+
+		cc_library {
+			name: "mylib",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+			header_libs: ["mylib_headers"],
+			export_header_lib_headers: ["mylib_headers"],
+			stubs: {
+				versions: ["1", "2", "3"],
+			},
+		}
+
+		cc_library {
+			name: "otherlib",
+			srcs: ["mylib.cpp"],
+			system_shared_libs: [],
+			stl: "none",
+			shared_libs: ["mylib"],
+		}
+	`)
+
+	cFlags := ctx.ModuleForTests("otherlib", "android_arm64_armv8-a_core_static").Rule("cc").Args["cFlags"]
+
+	// Ensure that the include path of the header lib is exported to 'otherlib'
+	ensureContains(t, cFlags, "-Imy_include")
+}
diff --git a/cc/builder.go b/cc/builder.go
index 5dbd23e..b012d6f 100644
--- a/cc/builder.go
+++ b/cc/builder.go
@@ -199,7 +199,7 @@
 			// TODO(b/78139997): Add -check-all-apis back
 			commandStr := "($sAbiDiffer $allowFlags -lib $libName -arch $arch -o ${out} -new $in -old $referenceDump)"
 			commandStr += "|| (echo ' ---- Please update abi references by running $$ANDROID_BUILD_TOP/development/vndk/tools/header-checker/utils/create_reference_dumps.py -l ${libName} ----'"
-			commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiff/)"
+			commandStr += " && (mkdir -p $$DIST_DIR/abidiffs && cp ${out} $$DIST_DIR/abidiffs/)"
 			commandStr += " && exit 1)"
 			return blueprint.RuleParams{
 				Command:     commandStr,
diff --git a/cc/cc.go b/cc/cc.go
index baee70a..1f50976 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -258,7 +258,7 @@
 	getVndkExtendsModuleName() string
 	isPgoCompile() bool
 	useClangLld(actx ModuleContext) bool
-	isApex() bool
+	apexName() string
 	hasStubsVariants() bool
 	isStubs() bool
 }
@@ -720,9 +720,8 @@
 	return ctx.mod.getVndkExtendsModuleName()
 }
 
-// Tests if this module is built for APEX
-func (ctx *moduleContextImpl) isApex() bool {
-	return ctx.mod.ApexName() != ""
+func (ctx *moduleContextImpl) apexName() string {
+	return ctx.mod.ApexName()
 }
 
 func (ctx *moduleContextImpl) hasStubsVariants() bool {
@@ -1143,11 +1142,11 @@
 		deps.ReexportSharedLibHeaders, _ = rewriteNdkLibs(deps.ReexportSharedLibHeaders)
 	}
 
+	buildStubs := false
 	if c.linker != nil {
 		if library, ok := c.linker.(*libraryDecorator); ok {
 			if library.buildStubs() {
-				// Stubs lib does not have dependency to other libraries. Don't proceed.
-				return
+				buildStubs = true
 			}
 		}
 	}
@@ -1157,7 +1156,26 @@
 		if inList(lib, deps.ReexportHeaderLibHeaders) {
 			depTag = headerExportDepTag
 		}
-		actx.AddVariationDependencies(nil, depTag, lib)
+		if buildStubs {
+			imageVariation := "core"
+			if c.useVndk() {
+				imageVariation = "vendor"
+			} else if c.inRecovery() {
+				imageVariation = "recovery"
+			}
+			actx.AddFarVariationDependencies([]blueprint.Variation{
+				{Mutator: "arch", Variation: ctx.Target().String()},
+				{Mutator: "image", Variation: imageVariation},
+			}, depTag, lib)
+		} else {
+			actx.AddVariationDependencies(nil, depTag, lib)
+		}
+	}
+
+	if buildStubs {
+		// Stubs lib does not have dependency to other static/shared libraries.
+		// Don't proceed.
+		return
 	}
 
 	actx.AddVariationDependencies([]blueprint.Variation{
diff --git a/cc/compiler.go b/cc/compiler.go
index 63d2ade..fbe10b5 100644
--- a/cc/compiler.go
+++ b/cc/compiler.go
@@ -341,6 +341,10 @@
 		flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_RECOVERY__")
 	}
 
+	if ctx.apexName() != "" {
+		flags.GlobalFlags = append(flags.GlobalFlags, "-D__ANDROID_APEX__="+ctx.apexName())
+	}
+
 	instructionSet := String(compiler.Properties.Instruction_set)
 	if flags.RequiredInstructionSet != "" {
 		instructionSet = flags.RequiredInstructionSet
diff --git a/scripts/OWNERS b/scripts/OWNERS
new file mode 100644
index 0000000..076b3f5
--- /dev/null
+++ b/scripts/OWNERS
@@ -0,0 +1 @@
+per-file system-clang-format,system-clang-format-2 = enh@google.com,smoreland@google.com
diff --git a/scripts/system-clang-format b/scripts/system-clang-format
new file mode 100644
index 0000000..55773a2
--- /dev/null
+++ b/scripts/system-clang-format
@@ -0,0 +1,11 @@
+BasedOnStyle: Google
+AccessModifierOffset: -2
+AllowShortFunctionsOnASingleLine: Inline
+ColumnLimit: 100
+CommentPragmas: NOLINT:.*
+DerivePointerAlignment: false
+IndentWidth: 4
+ContinuationIndentWidth: 8
+PointerAlignment: Left
+TabWidth: 4
+UseTab: Never
diff --git a/scripts/system-clang-format-2 b/scripts/system-clang-format-2
new file mode 100644
index 0000000..ede5d7e
--- /dev/null
+++ b/scripts/system-clang-format-2
@@ -0,0 +1,9 @@
+BasedOnStyle: Google
+AllowShortFunctionsOnASingleLine: Inline
+ColumnLimit: 100
+CommentPragmas: NOLINT:.*
+DerivePointerAlignment: false
+IndentWidth: 2
+PointerAlignment: Left
+TabWidth: 2
+UseTab: Never