Add default_shared_libs property

Building against a locally-built libc besides bionic requires setting
system_shared_libs to avoid circular dependencies, but modules all
over the tree assume that system_shared_libs only affects modules
building against bionic.  Add a new default_shared_libs property
that applies to all modules, which will generally be set in
arch-specific clauses to only affect the desired set of modules.

Bug: 190084016
Test: m checkbuild
Change-Id: Ia2349d84c70e503916f90a5d2702e135248f73df
diff --git a/cc/linker.go b/cc/linker.go
index 7b16b40..13df232 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -45,11 +45,18 @@
 	// list of module-specific flags that will be used for all link steps
 	Ldflags []string `android:"arch_variant"`
 
-	// list of system libraries that will be dynamically linked to
-	// shared library and executable modules.  If unset, generally defaults to libc,
-	// libm, and libdl.  Set to [] to prevent linking against the defaults.
+	// list of system libraries that will be dynamically linked to shared library and executable
+	// modules that build against bionic (device or Linux bionic modules).  If unset, generally
+	// defaults to libc, libm, and libdl.  Set to [] to prevent linking against the defaults.
+	// Equivalent to default_shared_libs for modules that build against bionic, and ignored on
+	// modules that do not build against bionic.
 	System_shared_libs []string `android:"arch_variant"`
 
+	// list of system libraries that will be dynamically linked to shared library and executable
+	// modules.  If unset, generally defaults to libc, libm, and libdl.  Set to [] to prevent
+	// linking against the defaults.  Equivalent to system_shared_libs, but applies to all modules.
+	Default_shared_libs []string `android:"arch_variant"`
+
 	// allow the module to contain undefined symbols.  By default,
 	// modules cannot contain undefined symbols that are not satisified by their immediate
 	// dependencies.  Set this flag to true to remove --no-undefined from the linker flags.
@@ -231,6 +238,19 @@
 	linker.Properties.Ldflags = append(linker.Properties.Ldflags, flags...)
 }
 
+// overrideDefaultSharedLibraries returns the contents of the default_shared_libs or
+// system_shared_libs properties, and records an error if both are set.
+func (linker *baseLinker) overrideDefaultSharedLibraries(ctx BaseModuleContext) []string {
+	if linker.Properties.System_shared_libs != nil && linker.Properties.Default_shared_libs != nil {
+		ctx.PropertyErrorf("system_shared_libs", "cannot be specified if default_shared_libs is also specified")
+	}
+	if ctx.toolchain().Bionic() && linker.Properties.System_shared_libs != nil {
+		// system_shared_libs is only honored when building against bionic.
+		return linker.Properties.System_shared_libs
+	}
+	return linker.Properties.Default_shared_libs
+}
+
 // linkerInit initializes dynamic properties of the linker (such as runpath).
 func (linker *baseLinker) linkerInit(ctx BaseModuleContext) {
 	if ctx.toolchain().Is64Bit() {
@@ -331,22 +351,22 @@
 		deps.SharedLibs = append(deps.SharedLibs, linker.Properties.Target.Platform.Shared_libs...)
 	}
 
+	deps.SystemSharedLibs = linker.overrideDefaultSharedLibraries(ctx)
+	// In Bazel conversion mode, variations have not been specified, so SystemSharedLibs may
+	// inaccuarately appear unset, which can cause issues with circular dependencies.
+	if deps.SystemSharedLibs == nil && !ctx.BazelConversionMode() {
+		// Provide a default set of shared libraries if default_shared_libs and system_shared_libs
+		// are unspecified.  Note: If an empty list [] is specified, it implies that the module
+		// declines the default shared libraries.
+		deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
+	}
+
 	if ctx.toolchain().Bionic() {
 		// libclang_rt.builtins has to be last on the command line
 		if !Bool(linker.Properties.No_libcrt) && !ctx.header() {
 			deps.LateStaticLibs = append(deps.LateStaticLibs, config.BuiltinsRuntimeLibrary(ctx.toolchain()))
 		}
 
-		deps.SystemSharedLibs = linker.Properties.System_shared_libs
-		// In Bazel conversion mode, variations have not been specified, so SystemSharedLibs may
-		// inaccuarately appear unset, which can cause issues with circular dependencies.
-		if deps.SystemSharedLibs == nil && !ctx.BazelConversionMode() {
-			// Provide a default system_shared_libs if it is unspecified. Note: If an
-			// empty list [] is specified, it implies that the module declines the
-			// default system_shared_libs.
-			deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
-		}
-
 		if inList("libdl", deps.SharedLibs) {
 			// If system_shared_libs has libc but not libdl, make sure shared_libs does not
 			// have libdl to avoid loading libdl before libc.
@@ -573,6 +593,11 @@
 	} else {
 		specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, linker.Properties.System_shared_libs...)
 	}
+	if specifiedDeps.defaultSharedLibs == nil {
+		specifiedDeps.defaultSharedLibs = linker.Properties.Default_shared_libs
+	} else {
+		specifiedDeps.defaultSharedLibs = append(specifiedDeps.defaultSharedLibs, linker.Properties.Default_shared_libs...)
+	}
 
 	return specifiedDeps
 }