From 2c0327695cf475edd016fcc4dc9821c4fa8836be Mon Sep 17 00:00:00 2001 From: Yu Liu Date: Wed, 24 Jul 2024 19:27:51 +0000 Subject: [PATCH] Only add darwin specific rules/variables when building on mac. To support skipping ninja file writing during incremental build, we add all the global rules/variable/pools from static sources because when we restore from cache we don't have the build defs available to build the globals. This means all the global rules and variables are added blindly, so we need to exclude these darwin specific ones since they will fail due to they depends on the tool that only exists on MAC. Bug: 335718784 Test: local tests and CI Change-Id: Ife835545f0f76ffaec77e48bdd89ab7384a1b995 --- cc/builder.go | 32 +++++++++++++++++++--------- cc/config/darwin_host.go | 43 ++++++++++++++++++++------------------ rust/config/darwin_host.go | 15 +++++++------ 3 files changed, 54 insertions(+), 36 deletions(-) diff --git a/cc/builder.go b/cc/builder.go index 367bda380..a05a16dcb 100644 --- a/cc/builder.go +++ b/cc/builder.go @@ -156,11 +156,17 @@ var ( "args") // Rule to invoke `strip` (to discard symbols and data from object files) on darwin architecture. - darwinStrip = pctx.AndroidStaticRule("darwinStrip", - blueprint.RuleParams{ - Command: "${config.MacStripPath} -u -r -o $out $in", - CommandDeps: []string{"${config.MacStripPath}"}, - }) + darwinStrip = func() blueprint.Rule { + if runtime.GOOS == "darwin" { + return pctx.AndroidStaticRule("darwinStrip", + blueprint.RuleParams{ + Command: "${config.MacStripPath} -u -r -o $out $in", + CommandDeps: []string{"${config.MacStripPath}"}, + }) + } else { + return nil + } + }() // b/132822437: objcopy uses a file descriptor per .o file when called on .a files, which runs the system out of // file descriptors on darwin. Limit concurrent calls to 5 on darwin. @@ -174,11 +180,17 @@ var ( } }() - darwinLipo = pctx.AndroidStaticRule("darwinLipo", - blueprint.RuleParams{ - Command: "${config.MacLipoPath} -create -output $out $in", - CommandDeps: []string{"${config.MacLipoPath}"}, - }) + darwinLipo = func() blueprint.Rule { + if runtime.GOOS == "darwin" { + return pctx.AndroidStaticRule("darwinLipo", + blueprint.RuleParams{ + Command: "${config.MacLipoPath} -create -output $out $in", + CommandDeps: []string{"${config.MacLipoPath}"}, + }) + } else { + return nil + } + }() _ = pctx.SourcePathVariable("archiveRepackPath", "build/soong/scripts/archive_repack.sh") diff --git a/cc/config/darwin_host.go b/cc/config/darwin_host.go index 485666954..2ea607a9d 100644 --- a/cc/config/darwin_host.go +++ b/cc/config/darwin_host.go @@ -18,6 +18,7 @@ import ( "fmt" "os/exec" "path/filepath" + "runtime" "strings" "sync" @@ -73,31 +74,33 @@ var ( ) func init() { - pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { - return getMacTools(ctx).sdkRoot - }) - pctx.StaticVariable("macMinVersion", "10.14") - pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { - return getMacTools(ctx).arPath - }) + if runtime.GOOS == "darwin" { + pctx.VariableFunc("macSdkRoot", func(ctx android.PackageVarContext) string { + return getMacTools(ctx).sdkRoot + }) + pctx.StaticVariable("macMinVersion", "10.14") + pctx.VariableFunc("MacArPath", func(ctx android.PackageVarContext) string { + return getMacTools(ctx).arPath + }) - pctx.VariableFunc("MacLipoPath", func(ctx android.PackageVarContext) string { - return getMacTools(ctx).lipoPath - }) + pctx.VariableFunc("MacLipoPath", func(ctx android.PackageVarContext) string { + return getMacTools(ctx).lipoPath + }) - pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { - return getMacTools(ctx).stripPath - }) + pctx.VariableFunc("MacStripPath", func(ctx android.PackageVarContext) string { + return getMacTools(ctx).stripPath + }) - pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { - return getMacTools(ctx).toolPath - }) + pctx.VariableFunc("MacToolPath", func(ctx android.PackageVarContext) string { + return getMacTools(ctx).toolPath + }) - pctx.StaticVariable("DarwinCflags", strings.Join(darwinCflags, " ")) - pctx.StaticVariable("DarwinLdflags", strings.Join(darwinLdflags, " ")) - pctx.StaticVariable("DarwinLldflags", strings.Join(darwinLdflags, " ")) + pctx.StaticVariable("DarwinCflags", strings.Join(darwinCflags, " ")) + pctx.StaticVariable("DarwinLdflags", strings.Join(darwinLdflags, " ")) + pctx.StaticVariable("DarwinLldflags", strings.Join(darwinLdflags, " ")) - pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") + pctx.StaticVariable("DarwinYasmFlags", "-f macho -m amd64") + } } func MacStripPath(ctx android.PathContext) string { diff --git a/rust/config/darwin_host.go b/rust/config/darwin_host.go index a4bc1879c..df8c6ac5a 100644 --- a/rust/config/darwin_host.go +++ b/rust/config/darwin_host.go @@ -15,6 +15,7 @@ package config import ( + "runtime" "strings" "android/soong/android" @@ -35,13 +36,15 @@ func init() { registerToolchainFactory(android.Darwin, android.Arm64, darwinArm64ToolchainFactory) registerToolchainFactory(android.Darwin, android.X86_64, darwinX8664ToolchainFactory) - pctx.StaticVariable("DarwinToolchainRustFlags", strings.Join(DarwinRustFlags, " ")) - pctx.StaticVariable("DarwinToolchainLinkFlags", strings.Join(DarwinRustLinkFlags, " ")) + if runtime.GOOS == "darwin" { + pctx.StaticVariable("DarwinToolchainRustFlags", strings.Join(DarwinRustFlags, " ")) + pctx.StaticVariable("DarwinToolchainLinkFlags", strings.Join(DarwinRustLinkFlags, " ")) - pctx.StaticVariable("DarwinToolchainArm64RustFlags", strings.Join(darwinArm64Rustflags, " ")) - pctx.StaticVariable("DarwinToolchainArm64LinkFlags", strings.Join(darwinArm64Linkflags, " ")) - pctx.StaticVariable("DarwinToolchainX8664RustFlags", strings.Join(darwinX8664Rustflags, " ")) - pctx.StaticVariable("DarwinToolchainX8664LinkFlags", strings.Join(darwinX8664Linkflags, " ")) + pctx.StaticVariable("DarwinToolchainArm64RustFlags", strings.Join(darwinArm64Rustflags, " ")) + pctx.StaticVariable("DarwinToolchainArm64LinkFlags", strings.Join(darwinArm64Linkflags, " ")) + pctx.StaticVariable("DarwinToolchainX8664RustFlags", strings.Join(darwinX8664Rustflags, " ")) + pctx.StaticVariable("DarwinToolchainX8664LinkFlags", strings.Join(darwinX8664Linkflags, " ")) + } }