Install data_libs using relative_install_path property

Test: Added to data_libs tests
Test: Manually verified with bionic-unit-tests
Test: Treehugger
Change-Id: I28a8e08e3409f1e7c7bb72f4351310b57f35f513
This commit is contained in:
Chris Parsons
2020-07-09 17:12:52 -04:00
parent 3044394329
commit 216e10a0f6
8 changed files with 100 additions and 25 deletions

View File

@@ -1555,3 +1555,15 @@ func absolutePath(path string) string {
} }
return filepath.Join(absSrcDir, path) return filepath.Join(absSrcDir, path)
} }
// A DataPath represents the path of a file to be used as data, for example
// a test library to be installed alongside a test.
// The data file should be installed (copied from `<SrcPath>`) to
// `<install_root>/<RelativeInstallPath>/<filename>`, or
// `<install_root>/<filename>` if RelativeInstallPath is empty.
type DataPath struct {
// The path of the data file that should be copied into the data directory
SrcPath Path
// The install path of the data file, relative to the install root.
RelativeInstallPath string
}

View File

@@ -116,9 +116,9 @@ func (a *apexBundle) androidMkForFiles(w io.Writer, apexBundleName, apexName, mo
if len(fi.symlinks) > 0 { if len(fi.symlinks) > 0 {
fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " ")) fmt.Fprintln(w, "LOCAL_MODULE_SYMLINKS :=", strings.Join(fi.symlinks, " "))
} }
newDataPaths := []android.Path{} newDataPaths := []android.DataPath{}
for _, path := range fi.dataPaths { for _, path := range fi.dataPaths {
dataOutPath := modulePath + ":" + path.Rel() dataOutPath := modulePath + ":" + path.SrcPath.Rel()
if ok := seenDataOutPaths[dataOutPath]; !ok { if ok := seenDataOutPaths[dataOutPath]; !ok {
newDataPaths = append(newDataPaths, path) newDataPaths = append(newDataPaths, path)
seenDataOutPaths[dataOutPath] = true seenDataOutPaths[dataOutPath] = true

View File

@@ -1145,7 +1145,7 @@ type apexFile struct {
module android.Module module android.Module
// list of symlinks that will be created in installDir that point to this apexFile // list of symlinks that will be created in installDir that point to this apexFile
symlinks []string symlinks []string
dataPaths android.Paths dataPaths []android.DataPath
transitiveDep bool transitiveDep bool
moduleDir string moduleDir string

View File

@@ -403,16 +403,16 @@ func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
} }
for _, d := range fi.dataPaths { for _, d := range fi.dataPaths {
// TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible // TODO(eakammer): This is now the third repetition of ~this logic for test paths, refactoring should be possible
relPath := d.Rel() relPath := d.SrcPath.Rel()
dataPath := d.String() dataPath := d.SrcPath.String()
if !strings.HasSuffix(dataPath, relPath) { if !strings.HasSuffix(dataPath, relPath) {
panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath)) panic(fmt.Errorf("path %q does not end with %q", dataPath, relPath))
} }
dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath)).String() dataDest := android.PathForModuleOut(ctx, "image"+suffix, fi.apexRelativePath(relPath)).String()
copyCommands = append(copyCommands, "cp -f "+d.String()+" "+dataDest) copyCommands = append(copyCommands, "cp -f "+d.SrcPath.String()+" "+dataDest)
implicitInputs = append(implicitInputs, d) implicitInputs = append(implicitInputs, d.SrcPath)
} }
} }
@@ -473,7 +473,7 @@ func (a *apexBundle) buildUnflattenedApex(ctx android.ModuleContext) {
if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") { if f.installDir == "bin" || strings.HasPrefix(f.installDir, "bin/") {
executablePaths = append(executablePaths, pathInApex) executablePaths = append(executablePaths, pathInApex)
for _, d := range f.dataPaths { for _, d := range f.dataPaths {
readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.Rel())) readOnlyPaths = append(readOnlyPaths, filepath.Join(f.installDir, d.SrcPath.Rel()))
} }
for _, s := range f.symlinks { for _, s := range f.symlinks {
executablePaths = append(executablePaths, filepath.Join(f.installDir, s)) executablePaths = append(executablePaths, filepath.Join(f.installDir, s))

View File

@@ -149,21 +149,25 @@ func (c *Module) AndroidMkEntries() []android.AndroidMkEntries {
return []android.AndroidMkEntries{entries} return []android.AndroidMkEntries{entries}
} }
func AndroidMkDataPaths(data android.Paths) []string { func AndroidMkDataPaths(data []android.DataPath) []string {
var testFiles []string var testFiles []string
for _, d := range data { for _, d := range data {
rel := d.Rel() rel := d.SrcPath.Rel()
path := d.String() path := d.SrcPath.String()
if !strings.HasSuffix(path, rel) { if !strings.HasSuffix(path, rel) {
panic(fmt.Errorf("path %q does not end with %q", path, rel)) panic(fmt.Errorf("path %q does not end with %q", path, rel))
} }
path = strings.TrimSuffix(path, rel) path = strings.TrimSuffix(path, rel)
testFiles = append(testFiles, path+":"+rel) testFileString := path + ":" + rel
if len(d.RelativeInstallPath) > 0 {
testFileString += ":" + d.RelativeInstallPath
}
testFiles = append(testFiles, testFileString)
} }
return testFiles return testFiles
} }
func androidMkWriteTestData(data android.Paths, ctx AndroidMkContext, entries *android.AndroidMkEntries) { func androidMkWriteTestData(data []android.DataPath, ctx AndroidMkContext, entries *android.AndroidMkEntries) {
testFiles := AndroidMkDataPaths(data) testFiles := AndroidMkDataPaths(data)
if len(testFiles) > 0 { if len(testFiles) > 0 {
entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) { entries.ExtraEntries = append(entries.ExtraEntries, func(entries *android.AndroidMkEntries) {
@@ -357,8 +361,11 @@ func (benchmark *benchmarkDecorator) AndroidMkEntries(ctx AndroidMkContext, entr
entries.SetBool("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", true) entries.SetBool("LOCAL_DISABLE_AUTO_GENERATE_TEST_CONFIG", true)
} }
}) })
dataPaths := []android.DataPath{}
androidMkWriteTestData(benchmark.data, ctx, entries) for _, srcPath := range benchmark.data {
dataPaths = append(dataPaths, android.DataPath{SrcPath: srcPath})
}
androidMkWriteTestData(dataPaths, ctx, entries)
} }
func (test *testBinary) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) { func (test *testBinary) AndroidMkEntries(ctx AndroidMkContext, entries *android.AndroidMkEntries) {

View File

@@ -1401,9 +1401,9 @@ func (c *Module) IsTestPerSrcAllTestsVariation() bool {
return ok && test.isAllTestsVariation() return ok && test.isAllTestsVariation()
} }
func (c *Module) DataPaths() android.Paths { func (c *Module) DataPaths() []android.DataPath {
if p, ok := c.installer.(interface { if p, ok := c.installer.(interface {
dataPaths() android.Paths dataPaths() []android.DataPath
}); ok { }); ok {
return p.dataPaths() return p.dataPaths()
} }

View File

@@ -539,7 +539,7 @@ func TestDataLibs(t *testing.T) {
data_libs: ["test_lib"], data_libs: ["test_lib"],
gtest: false, gtest: false,
} }
` `
config := TestConfig(buildDir, android.Android, nil, bp, nil) config := TestConfig(buildDir, android.Android, nil, bp, nil)
config.TestProductVariables.DeviceVndkVersion = StringPtr("current") config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
@@ -564,7 +564,7 @@ func TestDataLibs(t *testing.T) {
} }
outputPath := outputFiles[0].String() outputPath := outputFiles[0].String()
testBinaryPath := testBinary.dataPaths()[0].String() testBinaryPath := testBinary.dataPaths()[0].SrcPath.String()
if !strings.HasSuffix(outputPath, "/main_test") { if !strings.HasSuffix(outputPath, "/main_test") {
t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath) t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
@@ -576,6 +576,54 @@ func TestDataLibs(t *testing.T) {
} }
} }
func TestDataLibsRelativeInstallPath(t *testing.T) {
bp := `
cc_test_library {
name: "test_lib",
srcs: ["test_lib.cpp"],
relative_install_path: "foo/bar/baz",
gtest: false,
}
cc_test {
name: "main_test",
data_libs: ["test_lib"],
gtest: false,
}
`
config := TestConfig(buildDir, android.Android, nil, bp, nil)
config.TestProductVariables.DeviceVndkVersion = StringPtr("current")
config.TestProductVariables.Platform_vndk_version = StringPtr("VER")
config.TestProductVariables.VndkUseCoreVariant = BoolPtr(true)
ctx := testCcWithConfig(t, config)
module := ctx.ModuleForTests("main_test", "android_arm_armv7-a-neon").Module()
testBinary := module.(*Module).linker.(*testBinary)
outputFiles, err := module.(android.OutputFileProducer).OutputFiles("")
if err != nil {
t.Fatalf("Expected cc_test to produce output files, error: %s", err)
}
if len(outputFiles) != 1 {
t.Errorf("expected exactly one output file. output files: [%s]", outputFiles)
}
if len(testBinary.dataPaths()) != 1 {
t.Errorf("expected exactly one test data file. test data files: [%s]", testBinary.dataPaths())
}
outputPath := outputFiles[0].String()
testBinaryPath := testBinary.dataPaths()[0]
if !strings.HasSuffix(outputPath, "/main_test") {
t.Errorf("expected test output file to be 'main_test', but was '%s'", outputPath)
}
entries := android.AndroidMkEntriesForTest(t, config, "", module)[0]
if !strings.HasSuffix(entries.EntryMap["LOCAL_TEST_DATA"][0], ":test_lib.so:foo/bar/baz") {
t.Errorf("expected LOCAL_TEST_DATA to end with `:test_lib.so:foo/bar/baz`,"+
" but was '%s'", testBinaryPath)
}
}
func TestVndkWhenVndkVersionIsNotSet(t *testing.T) { func TestVndkWhenVndkVersionIsNotSet(t *testing.T) {
ctx := testCcNoVndk(t, ` ctx := testCcNoVndk(t, `
cc_library { cc_library {

View File

@@ -165,7 +165,7 @@ func (test *testBinary) srcs() []string {
return test.baseCompiler.Properties.Srcs return test.baseCompiler.Properties.Srcs
} }
func (test *testBinary) dataPaths() android.Paths { func (test *testBinary) dataPaths() []android.DataPath {
return test.data return test.data
} }
@@ -310,7 +310,7 @@ type testBinary struct {
*binaryDecorator *binaryDecorator
*baseCompiler *baseCompiler
Properties TestBinaryProperties Properties TestBinaryProperties
data android.Paths data []android.DataPath
testConfig android.Path testConfig android.Path
} }
@@ -339,7 +339,11 @@ func (test *testBinary) linkerFlags(ctx ModuleContext, flags Flags) Flags {
} }
func (test *testBinary) install(ctx ModuleContext, file android.Path) { func (test *testBinary) install(ctx ModuleContext, file android.Path) {
test.data = android.PathsForModuleSrc(ctx, test.Properties.Data) dataSrcPaths := android.PathsForModuleSrc(ctx, test.Properties.Data)
for _, dataSrcPath := range dataSrcPaths {
test.data = append(test.data, android.DataPath{SrcPath: dataSrcPath})
}
ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) { ctx.VisitDirectDepsWithTag(dataLibDepTag, func(dep android.Module) {
depName := ctx.OtherModuleName(dep) depName := ctx.OtherModuleName(dep)
@@ -348,10 +352,14 @@ func (test *testBinary) install(ctx ModuleContext, file android.Path) {
if !ok { if !ok {
ctx.ModuleErrorf("data_lib %q is not a linkable cc module", depName) ctx.ModuleErrorf("data_lib %q is not a linkable cc module", depName)
} }
ccModule, ok := dep.(*Module)
if !ok {
ctx.ModuleErrorf("data_lib %q is not a cc module", depName)
}
if ccDep.OutputFile().Valid() { if ccDep.OutputFile().Valid() {
test.data = append(test.data, ccDep.OutputFile().Path()) test.data = append(test.data,
} else { android.DataPath{SrcPath: ccDep.OutputFile().Path(),
ctx.ModuleErrorf("data_lib %q has no output file", depName) RelativeInstallPath: ccModule.installer.relativeInstallPath()})
} }
}) })