From 1c14a21994da900427d9d2a26ac9ef3a3ea4c066 Mon Sep 17 00:00:00 2001 From: Liz Kammer Date: Tue, 12 May 2020 15:26:55 -0700 Subject: [PATCH] Add test data dependencies to APEX. Test: soong go tests Bug: 155820504 Change-Id: If96d82c27f19953e34efb31f2111f1643c0c4008 --- apex/androidmk.go | 3 +++ apex/apex.go | 10 ++++++++-- apex/apex_test.go | 15 +++++++++++++++ apex/builder.go | 16 ++++++++++++++++ cc/androidmk.go | 7 ++++++- cc/cc.go | 9 +++++++++ cc/test.go | 4 ++++ 7 files changed, 61 insertions(+), 3 deletions(-) diff --git a/apex/androidmk.go b/apex/androidmk.go index 2c5407c9e..5774809aa 100644 --- a/apex/androidmk.go +++ b/apex/androidmk.go @@ -120,6 +120,9 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo if len(fi.symlinks) > 0 { fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " ")) } + if len(fi.dataPaths) > 0 { + fmt.Println(w, "LOCAL_TEST_DATA :=", strings.Join(cc.AndroidMkDataPaths(fi.dataPaths), " ")) + } if fi.module != nil && len(fi.module.NoticeFiles()) > 0 { fmt.Fprintln(w, "LOCAL_NOTICE_FILE :=", strings.Join(fi.module.NoticeFiles().Strings(), " ")) diff --git a/apex/apex.go b/apex/apex.go index fa71ffa5c..c3f73ee9a 100644 --- a/apex/apex.go +++ b/apex/apex.go @@ -1214,6 +1214,7 @@ type apexFile struct { module android.Module // list of symlinks that will be created in installDir that point to this apexFile symlinks []string + dataPaths android.Paths transitiveDep bool moduleDir string @@ -1249,16 +1250,20 @@ func (af *apexFile) Ok() bool { return af.builtFile != nil && af.builtFile.String() != "" } +func (af *apexFile) apexRelativePath(path string) string { + return filepath.Join(af.installDir, path) +} + // Path() returns path of this apex file relative to the APEX root func (af *apexFile) Path() string { - return filepath.Join(af.installDir, af.builtFile.Base()) + return af.apexRelativePath(af.builtFile.Base()) } // SymlinkPaths() returns paths of the symlinks (if any) relative to the APEX root func (af *apexFile) SymlinkPaths() []string { var ret []string for _, symlink := range af.symlinks { - ret = append(ret, filepath.Join(af.installDir, symlink)) + ret = append(ret, af.apexRelativePath(symlink)) } return ret } @@ -1664,6 +1669,7 @@ func apexFileForExecutable(ctx android.BaseModuleContext, cc *cc.Module) apexFil fileToCopy := cc.OutputFile().Path() af := newApexFile(ctx, fileToCopy, cc.Name(), dirInApex, nativeExecutable, cc) af.symlinks = cc.Symlinks() + af.dataPaths = cc.DataPaths() return af } diff --git a/apex/apex_test.go b/apex/apex_test.go index dc6986244..021c3e22a 100644 --- a/apex/apex_test.go +++ b/apex/apex_test.go @@ -180,6 +180,8 @@ func testApexContext(t *testing.T, bp string, handlers ...testCustomizer) (*andr "build/make/core/proguard.flags": nil, "build/make/core/proguard_basic_keeps.flags": nil, "dummy.txt": nil, + "baz": nil, + "bar/baz": nil, } cc.GatherRequiredFilesForTest(fs) @@ -3267,6 +3269,14 @@ func TestApexWithTests(t *testing.T) { private_key: "testkey.pem", } + filegroup { + name: "fg", + srcs: [ + "baz", + "bar/baz" + ], + } + cc_test { name: "mytest", gtest: false, @@ -3276,6 +3286,7 @@ func TestApexWithTests(t *testing.T) { system_shared_libs: [], static_executable: true, stl: "none", + data: [":fg"], } cc_library { @@ -3308,6 +3319,10 @@ func TestApexWithTests(t *testing.T) { ensureContains(t, copyCmds, "image.apex/bin/test/mytest") ensureContains(t, copyCmds, "image.apex/lib64/mylib.so") + //Ensure that test data are copied into apex. + ensureContains(t, copyCmds, "image.apex/bin/test/baz") + ensureContains(t, copyCmds, "image.apex/bin/test/bar/baz") + // Ensure that test deps built with `test_per_src` are copied into apex. ensureContains(t, copyCmds, "image.apex/bin/test/mytest1") ensureContains(t, copyCmds, "image.apex/bin/test/mytest2") diff --git a/apex/builder.go b/apex/builder.go index ca24f2cff..47ae501f9 100644 --- a/apex/builder.go +++ b/apex/builder.go @@ -350,6 +350,19 @@ func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) { symlinkDest := android.PathForModuleOut(ctx, "image"+suffix, symlinkPath).String() copyCommands = append(copyCommands, "ln -sfn "+filepath.Base(destPath)+" "+symlinkDest) } + for _, d := range fi.dataPaths { + // TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible + relPath := d.Rel() + dataPath := d.String() + if !strings.HasSuffix(dataPath, relPath) { + panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath)) + } + + dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath)).String() + + copyCommands = append(copyCommands, "cp -f "+d.String()+" "+dataDest) + implicitInputs = append(implicitInputs, d) + } } // TODO(jiyong): use RuleBuilder @@ -406,6 +419,9 @@ func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) { pathInApex := filepath.Join(f.installDir, f.builtFile.Base()) if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") { executablePaths = append(executablePaths, pathInApex) + for _, d := range f.dataPaths { + readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.Rel())) + } for _, s := range f.symlinks { executablePaths = append(executablePaths, filepath.Join(f.installDir, s)) } diff --git a/cc/androidmk.go b/cc/androidmk.go index 671adb55f..3ce1a8955 100644 --- a/cc/androidmk.go +++ b/cc/androidmk.go @@ -149,7 +149,7 @@ func (c *Module) AndroidMkEntries() []android.AndroidMkEntries { return []android.AndroidMkEntries{entries} } -func androidMkWriteTestData(data android.Paths, ctx AndroidMkContext, entries *android.AndroidMkEntries) { +func AndroidMkDataPaths(data android.Paths) []string { var testFiles []string for _, d := range data { rel := d.Rel() @@ -160,6 +160,11 @@ func androidMkWriteTestData(data android.Paths, ctx AndroidMkContext, entries *a path = strings.TrimSuffix(path, rel) testFiles = append(testFiles, path+":"+rel) } + return testFiles +} + +func androidMkWriteTestData(data android.Paths, ctx AndroidMkContext, entries *android.AndroidMkEntries) { + testFiles := AndroidMkDataPaths(data) if len(testFiles) > 0 { entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) { entries.AddStrings("LOCAL_TEST_DATA", testFiles...) diff --git a/cc/cc.go b/cc/cc.go index 02c487902..fbd5bdfb8 100644 --- a/cc/cc.go +++ b/cc/cc.go @@ -1395,6 +1395,15 @@ func (c *Module) IsTestPerSrcAllTestsVariation() bool { return ok && test.isAllTestsVariation() } +func (c *Module) DataPaths() android.Paths { + if p, ok := c.installer.(interface { + dataPaths() android.Paths + }); ok { + return p.dataPaths() + } + return nil +} + func (c *Module) getNameSuffixWithVndkVersion(ctx android.ModuleContext) string { // Returns the name suffix for product and vendor variants. If the VNDK version is not // "current", it will append the VNDK version to the name suffix. diff --git a/cc/test.go b/cc/test.go index b1f8eec53..254eae61f 100644 --- a/cc/test.go +++ b/cc/test.go @@ -160,6 +160,10 @@ func (test *testBinary) srcs() []string { return test.baseCompiler.Properties.Srcs } +func (test *testBinary) dataPaths() android.Paths { + return test.data +} + func (test *testBinary) isAllTestsVariation() bool { stem := test.binaryDecorator.Properties.Stem return stem != nil && *stem == ""