Loading android/bazel_paths.go +25 −5 Original line number Original line Diff line number Diff line Loading @@ -248,9 +248,29 @@ func transformSubpackagePath(ctx BazelConversionPathContext, path bazel.Label) b newPath.Label = path.Label newPath.Label = path.Label return newPath return newPath } } if strings.HasPrefix(path.Label, "./") { newLabel := "" // Drop "./" for consistent handling of paths. // Specifically, to not let "." be considered a package boundary. // Say `inputPath` is `x/Android.bp` and that file has some module // with `srcs=["y/a.c", "z/b.c"]`. // And say the directory tree is: // x // ├── Android.bp // ├── y // │ ├── a.c // │ └── Android.bp // └── z // └── b.c // Then bazel equivalent labels in srcs should be: // //x/y:a.c, x/z/b.c // The above should still be the case if `x/Android.bp` had // srcs=["./y/a.c", "./z/b.c"] // However, if we didn't strip "./", we'd get // //x/./y:a.c, //x/.:z/b.c path.Label = strings.TrimPrefix(path.Label, "./") } pathComponents := strings.Split(path.Label, "/") pathComponents := strings.Split(path.Label, "/") newLabel := "" foundPackageBoundary := false foundPackageBoundary := false // Check the deepest subdirectory first and work upwards // Check the deepest subdirectory first and work upwards for i := len(pathComponents) - 1; i >= 0; i-- { for i := len(pathComponents) - 1; i >= 0; i-- { Loading android/bazel_paths_test.go +76 −1 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,10 @@ package android import ( import ( "path/filepath" "path/filepath" "testing" "testing" "android/soong/bazel" "github.com/google/blueprint" "github.com/google/blueprint/pathtools" ) ) type TestBazelPathContext struct{} type TestBazelPathContext struct{} Loading @@ -29,7 +33,7 @@ func (*TestBazelPathContext) Config() Config { return cfg return cfg } } func (*TestBazelPathContext) AddNinjaFileDeps(deps ...string) { func (*TestBazelPathContext) AddNinjaFileDeps(...string) { panic("Unimplemented") panic("Unimplemented") } } Loading Loading @@ -106,3 +110,74 @@ func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) { t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) } } } } type TestBazelConversionPathContext struct { TestBazelConversionContext moduleDir string cfg Config } func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) Module() Module { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) Config() Config { return ctx.cfg } func (ctx *TestBazelConversionPathContext) ModuleDir() string { return ctx.moduleDir } func TestTransformSubpackagePath(t *testing.T) { cfg := NullConfig("out", "out/soong") cfg.fs = pathtools.MockFs(map[string][]byte{ "x/Android.bp": nil, "x/y/Android.bp": nil, }) var ctx BazelConversionPathContext = &TestBazelConversionPathContext{ moduleDir: "x", cfg: cfg, } pairs := map[string]string{ "y/a.c": "//x/y:a.c", "./y/a.c": "//x/y:a.c", "z/b.c": "z/b.c", "./z/b.c": "z/b.c", } for in, out := range pairs { actual := transformSubpackagePath(ctx, bazel.Label{Label: in}).Label if actual != out { t.Errorf("expected:\n%v\nactual:\n%v", out, actual) } } } bp2build/build_conversion_test.go +18 −0 Original line number Original line Diff line number Diff line Loading @@ -960,6 +960,24 @@ func TestModuleTypeBp2Build(t *testing.T) { name: "fg_foo", name: "fg_foo", srcs: ["a", "b"], srcs: ["a", "b"], bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true }, }`, ExpectedBazelTargets: []string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ "srcs": `[ "a", "b", ]`, }), }, }, { Description: "filegroup with dot-slash-prefixed srcs", ModuleTypeUnderTest: "filegroup", ModuleTypeUnderTestFactory: android.FileGroupFactory, Blueprint: `filegroup { name: "fg_foo", srcs: ["./a", "./b"], bazel_module: { bp2build_available: true }, }`, }`, ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ Loading Loading
android/bazel_paths.go +25 −5 Original line number Original line Diff line number Diff line Loading @@ -248,9 +248,29 @@ func transformSubpackagePath(ctx BazelConversionPathContext, path bazel.Label) b newPath.Label = path.Label newPath.Label = path.Label return newPath return newPath } } if strings.HasPrefix(path.Label, "./") { newLabel := "" // Drop "./" for consistent handling of paths. // Specifically, to not let "." be considered a package boundary. // Say `inputPath` is `x/Android.bp` and that file has some module // with `srcs=["y/a.c", "z/b.c"]`. // And say the directory tree is: // x // ├── Android.bp // ├── y // │ ├── a.c // │ └── Android.bp // └── z // └── b.c // Then bazel equivalent labels in srcs should be: // //x/y:a.c, x/z/b.c // The above should still be the case if `x/Android.bp` had // srcs=["./y/a.c", "./z/b.c"] // However, if we didn't strip "./", we'd get // //x/./y:a.c, //x/.:z/b.c path.Label = strings.TrimPrefix(path.Label, "./") } pathComponents := strings.Split(path.Label, "/") pathComponents := strings.Split(path.Label, "/") newLabel := "" foundPackageBoundary := false foundPackageBoundary := false // Check the deepest subdirectory first and work upwards // Check the deepest subdirectory first and work upwards for i := len(pathComponents) - 1; i >= 0; i-- { for i := len(pathComponents) - 1; i >= 0; i-- { Loading
android/bazel_paths_test.go +76 −1 Original line number Original line Diff line number Diff line Loading @@ -17,6 +17,10 @@ package android import ( import ( "path/filepath" "path/filepath" "testing" "testing" "android/soong/bazel" "github.com/google/blueprint" "github.com/google/blueprint/pathtools" ) ) type TestBazelPathContext struct{} type TestBazelPathContext struct{} Loading @@ -29,7 +33,7 @@ func (*TestBazelPathContext) Config() Config { return cfg return cfg } } func (*TestBazelPathContext) AddNinjaFileDeps(deps ...string) { func (*TestBazelPathContext) AddNinjaFileDeps(...string) { panic("Unimplemented") panic("Unimplemented") } } Loading Loading @@ -106,3 +110,74 @@ func TestPathForBazelOutRelativeWithParentDirectoryRoot(t *testing.T) { t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) t.Errorf("incorrect OutputPath.Rel(): expected %q, got %q", expectedRelPath, out.Rel()) } } } } type TestBazelConversionPathContext struct { TestBazelConversionContext moduleDir string cfg Config } func (ctx *TestBazelConversionPathContext) AddNinjaFileDeps(...string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) GlobWithDeps(string, []string) ([]string, error) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) PropertyErrorf(string, string, ...interface{}) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) GetDirectDep(string) (blueprint.Module, blueprint.DependencyTag) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) ModuleFromName(string) (blueprint.Module, bool) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) AddUnconvertedBp2buildDep(string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) AddMissingBp2buildDep(string) { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) Module() Module { panic("Unimplemented") } func (ctx *TestBazelConversionPathContext) Config() Config { return ctx.cfg } func (ctx *TestBazelConversionPathContext) ModuleDir() string { return ctx.moduleDir } func TestTransformSubpackagePath(t *testing.T) { cfg := NullConfig("out", "out/soong") cfg.fs = pathtools.MockFs(map[string][]byte{ "x/Android.bp": nil, "x/y/Android.bp": nil, }) var ctx BazelConversionPathContext = &TestBazelConversionPathContext{ moduleDir: "x", cfg: cfg, } pairs := map[string]string{ "y/a.c": "//x/y:a.c", "./y/a.c": "//x/y:a.c", "z/b.c": "z/b.c", "./z/b.c": "z/b.c", } for in, out := range pairs { actual := transformSubpackagePath(ctx, bazel.Label{Label: in}).Label if actual != out { t.Errorf("expected:\n%v\nactual:\n%v", out, actual) } } }
bp2build/build_conversion_test.go +18 −0 Original line number Original line Diff line number Diff line Loading @@ -960,6 +960,24 @@ func TestModuleTypeBp2Build(t *testing.T) { name: "fg_foo", name: "fg_foo", srcs: ["a", "b"], srcs: ["a", "b"], bazel_module: { bp2build_available: true }, bazel_module: { bp2build_available: true }, }`, ExpectedBazelTargets: []string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ "srcs": `[ "a", "b", ]`, }), }, }, { Description: "filegroup with dot-slash-prefixed srcs", ModuleTypeUnderTest: "filegroup", ModuleTypeUnderTestFactory: android.FileGroupFactory, Blueprint: `filegroup { name: "fg_foo", srcs: ["./a", "./b"], bazel_module: { bp2build_available: true }, }`, }`, ExpectedBazelTargets: []string{ ExpectedBazelTargets: []string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ MakeBazelTargetNoRestrictions("filegroup", "fg_foo", map[string]string{ Loading