Add $(build_number_file) support to genrules
Genrules that set uses_order_only_build_number_file: true will now
have the ability to reference a $(build_number_file) label that
will point to the build number file. It will also caused the build
number file to be added as an order-only dependency, which will make
it show up in the genrule sandbox.
This is needed for converting make code that references the build
number to soong, and for sandboxing the remaining unsandboxed genrules
that reference the build number.
Bug: 341873065
Test: m nothing --no-skip-soong-tests
Change-Id: I9092cbb0eb39c5449a79f0ee40a4202262cef206
diff --git a/genrule/genrule_test.go b/genrule/genrule_test.go
index 444aedb..9278f15 100644
--- a/genrule/genrule_test.go
+++ b/genrule/genrule_test.go
@@ -19,6 +19,7 @@
"os"
"regexp"
"strconv"
+ "strings"
"testing"
"android/soong/android"
@@ -1192,6 +1193,68 @@
}
}
+func TestGenruleUsesOrderOnlyBuildNumberFile(t *testing.T) {
+ testCases := []struct {
+ name string
+ bp string
+ fs android.MockFS
+ expectedError string
+ expectedCommand string
+ }{
+ {
+ name: "not allowed when not in allowlist",
+ fs: android.MockFS{
+ "foo/Android.bp": []byte(`
+genrule {
+ name: "gen",
+ uses_order_only_build_number_file: true,
+ cmd: "cp $(build_number_file) $(out)",
+ out: ["out.txt"],
+}
+`),
+ },
+ expectedError: `Only allowlisted modules may use uses_order_only_build_number_file: true`,
+ },
+ {
+ name: "normal",
+ fs: android.MockFS{
+ "build/soong/tests/Android.bp": []byte(`
+genrule {
+ name: "gen",
+ uses_order_only_build_number_file: true,
+ cmd: "cp $(build_number_file) $(out)",
+ out: ["out.txt"],
+}
+`),
+ },
+ expectedCommand: `cp BUILD_NUMBER_FILE __SBOX_SANDBOX_DIR__/out/out.txt`,
+ },
+ }
+
+ for _, tc := range testCases {
+ t.Run(tc.name, func(t *testing.T) {
+ fixtures := android.GroupFixturePreparers(
+ prepareForGenRuleTest,
+ android.PrepareForTestWithVisibility,
+ android.FixtureMergeMockFs(tc.fs),
+ android.FixtureModifyConfigAndContext(func(config android.Config, ctx *android.TestContext) {
+ config.TestProductVariables.BuildNumberFile = proptools.StringPtr("build_number.txt")
+ }),
+ )
+ if tc.expectedError != "" {
+ fixtures = fixtures.ExtendWithErrorHandler(android.FixtureExpectsOneErrorPattern(tc.expectedError))
+ }
+ result := fixtures.RunTest(t)
+
+ if tc.expectedError == "" {
+ tc.expectedCommand = strings.ReplaceAll(tc.expectedCommand, "BUILD_NUMBER_FILE", result.Config.SoongOutDir()+"/build_number.txt")
+ gen := result.Module("gen", "").(*Module)
+ android.AssertStringEquals(t, "raw commands", tc.expectedCommand, gen.rawCommands[0])
+ }
+ })
+ }
+}
+
type testTool struct {
android.ModuleBase
outputFile android.Path