From 9b698b68c913d691d40f3e79128c3f120abf2138 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Wed, 22 Dec 2021 09:55:32 -0800 Subject: [PATCH 1/3] Add libc_musl as a dependency of RuleBuilderCommand.BuiltTool Assume calls to RuleBuilderCommand.BuiltTool may refer to a tool that was built against musl libc, and add it as a dependency so that it is copied into the sandbox. This emulates the behavior of compiling against glibc, which is available from the host sysroot when running in the sandbox. Bug: 190084016 Test: m USE_HOST_MUSL=true sdk-repo-build-tools Change-Id: Ieafdcceb818f9c31595487aab3ffbafba1412b3a --- android/config.go | 5 +++++ android/rule_builder.go | 17 +++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/android/config.go b/android/config.go index f10732bd7..877800a72 100644 --- a/android/config.go +++ b/android/config.go @@ -1982,3 +1982,8 @@ func (c *config) ApexBootJars() ConfiguredJarList { func (c *config) RBEWrapper() string { return c.GetenvWithDefault("RBE_WRAPPER", remoteexec.DefaultWrapperPath) } + +// UseHostMusl returns true if the host target has been configured to build against musl libc. +func (c *config) UseHostMusl() bool { + return Bool(c.productVariables.HostMusl) +} diff --git a/android/rule_builder.go b/android/rule_builder.go index 1c6b1c086..098c1fcc4 100644 --- a/android/rule_builder.go +++ b/android/rule_builder.go @@ -470,7 +470,7 @@ var _ BuilderContext = SingletonContext(nil) func (r *RuleBuilder) depFileMergerCmd(depFiles WritablePaths) *RuleBuilderCommand { return r.Command(). - BuiltTool("dep_fixer"). + builtToolWithoutDeps("dep_fixer"). Inputs(depFiles.Paths()) } @@ -638,7 +638,7 @@ func (r *RuleBuilder) Build(name string, desc string) { } sboxCmd.Text("rm -rf").Output(r.outDir) sboxCmd.Text("&&") - sboxCmd.BuiltTool("sbox"). + sboxCmd.builtToolWithoutDeps("sbox"). Flag("--sandbox-path").Text(shared.TempDirForOutDir(PathForOutput(r.ctx).String())). Flag("--manifest").Input(r.sboxManifestPath) @@ -1040,6 +1040,19 @@ func (c *RuleBuilderCommand) ImplicitTools(paths Paths) *RuleBuilderCommand { // It is equivalent to: // cmd.Tool(ctx.Config().HostToolPath(ctx, tool)) func (c *RuleBuilderCommand) BuiltTool(tool string) *RuleBuilderCommand { + if c.rule.ctx.Config().UseHostMusl() { + // If the host is using musl, assume that the tool was built against musl libc and include + // libc_musl.so in the sandbox. + // TODO(ccross): if we supported adding new dependencies during GenerateAndroidBuildActions + // this could be a dependency + TransitivePackagingSpecs. + c.ImplicitTool(c.rule.ctx.Config().HostJNIToolPath(c.rule.ctx, "libc_musl")) + } + return c.builtToolWithoutDeps(tool) +} + +// builtToolWithoutDeps is similar to BuiltTool, but doesn't add any dependencies. It is used +// internally by RuleBuilder for helper tools that are known to be compiled statically. +func (c *RuleBuilderCommand) builtToolWithoutDeps(tool string) *RuleBuilderCommand { return c.Tool(c.rule.ctx.Config().HostToolPath(c.rule.ctx, tool)) } From 1aa45b083e4a456d893183ce1a645fdad821fd8f Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 10 Feb 2022 10:33:10 -0800 Subject: [PATCH 2/3] Add musl_ and glibc_ properties Add musl_ and glibc_ properties similar to bionic_. Bug: 215802826 Test: m checkbuild Change-Id: Icfc42ad7b54ee1052f84a46b7c0acffb0a27b236 --- android/arch.go | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) diff --git a/android/arch.go b/android/arch.go index 96a4cbf09..a719cf3f5 100644 --- a/android/arch.go +++ b/android/arch.go @@ -917,7 +917,8 @@ func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc { for _, archType := range osArchTypeMap[os] { targets = append(targets, GetCompoundTargetField(os, archType)) - // Also add the special "linux_" and "bionic_" property structs. + // Also add the special "linux_", "bionic_" , "glibc_", and + // "musl_" property structs. if os.Linux() { target := "Linux_" + archType.Name if !InList(target, targets) { @@ -930,6 +931,18 @@ func createArchPropTypeDesc(props reflect.Type) []archPropTypeDesc { targets = append(targets, target) } } + if os == Linux { + target := "Glibc_" + archType.Name + if !InList(target, targets) { + targets = append(targets, target) + } + } + if os == LinuxMusl { + target := "Musl_" + archType.Name + if !InList(target, targets) { + targets = append(targets, target) + } + } } } @@ -1379,11 +1392,25 @@ func getArchProperties(ctx BaseMutatorContext, archProperties interface{}, arch result = append(result, osArchProperties) } + if os == Linux { + field := "Glibc_" + archType.Name + userFriendlyField := "target.glibc_" + "_" + archType.Name + if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { + result = append(result, osArchProperties) + } + } + if os == LinuxMusl { + field := "Musl_" + archType.Name + userFriendlyField := "target.musl_" + "_" + archType.Name + if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { + result = append(result, osArchProperties) + } + // Special case: to ease the transition from glibc to musl, apply linux_glibc // properties (which has historically mean host linux) to musl variants. - field := "Linux_glibc_" + archType.Name - userFriendlyField := "target.linux_glibc_" + archType.Name + field = "Linux_glibc_" + archType.Name + userFriendlyField = "target.linux_glibc_" + archType.Name if osArchProperties, ok := getChildPropertyStruct(ctx, targetProp, field, userFriendlyField); ok { result = append(result, osArchProperties) } From 72ee67659d4a59bf544a748a75c640f7a01116f5 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Fri, 28 Jan 2022 14:05:22 -0800 Subject: [PATCH 3/3] Fix musl clang triple Use x86_64-linux-musl and i686-linux-musl as the clang triple when targeting musl. Bug: 190084016 Test: m USE_HOST_MUSL=true host-native Change-Id: Ibd19d9a5fbf4b67950745480d1e0ed8e02eeeba1 --- cc/config/x86_linux_host.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/cc/config/x86_linux_host.go b/cc/config/x86_linux_host.go index 43333fada..60f03c2c9 100644 --- a/cc/config/x86_linux_host.go +++ b/cc/config/x86_linux_host.go @@ -194,10 +194,6 @@ func (t *toolchainLinux) IncludeFlags() string { return "" } -func (t *toolchainLinuxX86) ClangTriple() string { - return "i686-linux-gnu" -} - func (t *toolchainLinuxX86) Cflags() string { return "${config.LinuxCflags} ${config.LinuxX86Cflags}" } @@ -206,10 +202,6 @@ func (t *toolchainLinuxX86) Cppflags() string { return "" } -func (t *toolchainLinuxX8664) ClangTriple() string { - return "x86_64-linux-gnu" -} - func (t *toolchainLinuxX8664) Cflags() string { return "${config.LinuxCflags} ${config.LinuxX8664Cflags}" } @@ -283,6 +275,10 @@ type toolchainLinuxGlibcX8664 struct { toolchainGlibc } +func (t *toolchainLinuxGlibcX86) ClangTriple() string { + return "i686-linux-gnu" +} + func (t *toolchainLinuxGlibcX86) Cflags() string { return t.toolchainLinuxX86.Cflags() + " " + t.toolchainGlibc.Cflags() } @@ -295,6 +291,10 @@ func (t *toolchainLinuxGlibcX86) Lldflags() string { return t.toolchainLinuxX86.Lldflags() + " " + t.toolchainGlibc.Lldflags() } +func (t *toolchainLinuxGlibcX8664) ClangTriple() string { + return "x86_64-linux-gnu" +} + func (t *toolchainLinuxGlibcX8664) Cflags() string { return t.toolchainLinuxX8664.Cflags() + " " + t.toolchainGlibc.Cflags() } @@ -356,6 +356,10 @@ type toolchainLinuxMuslX8664 struct { toolchainMusl } +func (t *toolchainLinuxMuslX86) ClangTriple() string { + return "i686-linux-musl" +} + func (t *toolchainLinuxMuslX86) Cflags() string { return t.toolchainLinuxX86.Cflags() + " " + t.toolchainMusl.Cflags() } @@ -368,6 +372,10 @@ func (t *toolchainLinuxMuslX86) Lldflags() string { return t.toolchainLinuxX86.Lldflags() + " " + t.toolchainMusl.Lldflags() } +func (t *toolchainLinuxMuslX8664) ClangTriple() string { + return "x86_64-linux-musl" +} + func (t *toolchainLinuxMuslX8664) Cflags() string { return t.toolchainLinuxX8664.Cflags() + " " + t.toolchainMusl.Cflags() }