Merge "don't touch `bp2build_workspace_marker` file if it exists"
diff --git a/android/paths.go b/android/paths.go
index dbcdb23..375297f 100644
--- a/android/paths.go
+++ b/android/paths.go
@@ -1139,6 +1139,21 @@
 	return path
 }
 
+// MaybeExistentPathForSource joins the provided path components and validates that the result
+// neither escapes the source dir nor is in the out dir.
+// It does not validate whether the path exists.
+func MaybeExistentPathForSource(ctx PathContext, pathComponents ...string) SourcePath {
+	path, err := pathForSource(ctx, pathComponents...)
+	if err != nil {
+		reportPathError(ctx, err)
+	}
+
+	if pathtools.IsGlob(path.String()) {
+		ReportPathErrorf(ctx, "path may not contain a glob: %s", path.String())
+	}
+	return path
+}
+
 // ExistentPathForSource returns an OptionalPath with the SourcePath, rooted from SrcDir, *not*
 // rooted from the module's local source directory, if the path exists, or an empty OptionalPath if
 // it doesn't exist. Dependencies are added so that the ninja file will be regenerated if the state
diff --git a/cc/cc.go b/cc/cc.go
index eb7c639..a8011b8 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -2335,9 +2335,11 @@
 	ctx.ctx = ctx
 
 	deps := c.deps(ctx)
-
 	apiImportInfo := GetApiImports(c, actx)
-	deps = updateDepsWithApiImports(deps, apiImportInfo)
+
+	if ctx.Os() == android.Android && c.Target().NativeBridge != android.NativeBridgeEnabled {
+		deps = updateDepsWithApiImports(deps, apiImportInfo)
+	}
 
 	c.Properties.AndroidMkSystemSharedLibs = deps.SystemSharedLibs
 
@@ -2362,7 +2364,9 @@
 		}
 
 		// Check header lib replacement from API surface first, and then check again with VSDK
-		lib = GetReplaceModuleName(lib, apiImportInfo.HeaderLibs)
+		if ctx.Os() == android.Android && c.Target().NativeBridge != android.NativeBridgeEnabled {
+			lib = GetReplaceModuleName(lib, apiImportInfo.HeaderLibs)
+		}
 		lib = GetReplaceModuleName(lib, GetSnapshot(c, &snapshotInfo, actx).HeaderLibs)
 
 		if c.isNDKStubLibrary() {
diff --git a/cc/library_stub.go b/cc/library_stub.go
index 1283d76..1b02330 100644
--- a/cc/library_stub.go
+++ b/cc/library_stub.go
@@ -77,6 +77,19 @@
 	return basename + multitree.GetApiImportSuffix()
 }
 
+// Export include dirs without checking for existence.
+// The directories are not guaranteed to exist during Soong analysis.
+func (d *apiLibraryDecorator) exportIncludes(ctx ModuleContext) {
+	exporterProps := d.flagExporter.Properties
+	for _, dir := range exporterProps.Export_include_dirs {
+		d.dirs = append(d.dirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir))
+	}
+	// system headers
+	for _, dir := range exporterProps.Export_system_include_dirs {
+		d.systemDirs = append(d.systemDirs, android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), dir))
+	}
+}
+
 func (d *apiLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objects Objects) android.Path {
 	// Export headers as system include dirs if specified. Mostly for libc
 	if Bool(d.libraryDecorator.Properties.Llndk.Export_headers_as_system) {
@@ -87,7 +100,7 @@
 	}
 
 	// Flags reexported from dependencies. (e.g. vndk_prebuilt_shared)
-	d.libraryDecorator.flagExporter.exportIncludes(ctx)
+	d.exportIncludes(ctx)
 	d.libraryDecorator.reexportDirs(deps.ReexportedDirs...)
 	d.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...)
 	d.libraryDecorator.reexportFlags(deps.ReexportedFlags...)
@@ -95,7 +108,13 @@
 	d.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...)
 	d.libraryDecorator.flagExporter.setProvider(ctx)
 
-	in := android.PathForModuleSrc(ctx, *d.properties.Src)
+	if d.properties.Src == nil {
+		ctx.PropertyErrorf("src", "src is a required property")
+	}
+	// Skip the existence check of the stub prebuilt file.
+	// The file is not guaranteed to exist during Soong analysis.
+	// Build orchestrator will be responsible for creating a connected ninja graph.
+	in := android.MaybeExistentPathForSource(ctx, ctx.ModuleDir(), *d.properties.Src)
 
 	d.unstrippedOutputFile = in
 	libName := d.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix()
diff --git a/cc/library_stub_test.go b/cc/library_stub_test.go
index 288a34c..5cb4e61 100644
--- a/cc/library_stub_test.go
+++ b/cc/library_stub_test.go
@@ -241,3 +241,41 @@
 	android.AssertBoolEquals(t, "original header should be used for original library", true, hasDirectDependency(t, ctx, libfoo, libfooHeader))
 	android.AssertBoolEquals(t, "Header from API surface should not be used for original library", false, hasDirectDependency(t, ctx, libfoo, libfooHeaderApiImport))
 }
+
+func TestExportDirFromStubLibrary(t *testing.T) {
+	bp := `
+		cc_library {
+			name: "libfoo",
+			export_include_dirs: ["source_include_dir"],
+			export_system_include_dirs: ["source_system_include_dir"],
+			vendor_available: true,
+		}
+		cc_api_library {
+			name: "libfoo",
+			export_include_dirs: ["stub_include_dir"],
+			export_system_include_dirs: ["stub_system_include_dir"],
+			vendor_available: true,
+			src: "libfoo.so",
+		}
+		api_imports {
+			name: "api_imports",
+			shared_libs: [
+				"libfoo",
+			],
+			header_libs: [],
+		}
+		// vendor binary
+		cc_binary {
+			name: "vendorbin",
+			vendor: true,
+			srcs: ["vendor.cc"],
+			shared_libs: ["libfoo"],
+		}
+	`
+	ctx := prepareForCcTest.RunTestWithBp(t, bp)
+	vendorCFlags := ctx.ModuleForTests("vendorbin", "android_vendor.29_arm64_armv8-a").Rule("cc").Args["cFlags"]
+	android.AssertStringDoesContain(t, "Vendor binary should compile using headers provided by stub", vendorCFlags, "-Istub_include_dir")
+	android.AssertStringDoesNotContain(t, "Vendor binary should not compile using headers of source", vendorCFlags, "-Isource_include_dir")
+	android.AssertStringDoesContain(t, "Vendor binary should compile using system headers provided by stub", vendorCFlags, "-isystem stub_system_include_dir")
+	android.AssertStringDoesNotContain(t, "Vendor binary should not compile using system headers of source", vendorCFlags, "-isystem source_system_include_dir")
+}
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 517b80f..87710c0 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -622,21 +622,11 @@
 		bp2buildCtx.SetAllowMissingDependencies(configuration.AllowMissingDependencies())
 		bp2buildCtx.SetNameInterface(newNameResolver(configuration))
 		bp2buildCtx.RegisterForBazelConversion()
+		bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile)
 
 		var ninjaDeps []string
 		ninjaDeps = append(ninjaDeps, extraNinjaDeps...)
 
-		// The bp2build process is a purely functional process that only depends on
-		// Android.bp files. It must not depend on the values of per-build product
-		// configurations or variables, since those will generate different BUILD
-		// files based on how the user has configured their tree.
-		bp2buildCtx.SetModuleListFile(cmdlineArgs.ModuleListFile)
-		if modulePaths, err := bp2buildCtx.ListModulePaths("."); err != nil {
-			panic(err)
-		} else {
-			ninjaDeps = append(ninjaDeps, modulePaths...)
-		}
-
 		// Run the loading and analysis pipeline to prepare the graph of regular
 		// Modules parsed from Android.bp files, and the BazelTargetModules mapped
 		// from the regular Modules.