Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 18 | "fmt" |
| 19 | "path/filepath" |
| 20 | "strings" |
| 21 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 22 | "android/soong/bazel" |
| 23 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint" |
| 25 | "github.com/google/blueprint/pathtools" |
| 26 | ) |
| 27 | |
| 28 | // bazel_paths contains methods to: |
| 29 | // * resolve Soong path and module references into bazel.LabelList |
| 30 | // * resolve Bazel path references into Soong-compatible paths |
| 31 | // |
| 32 | // There is often a similar method for Bazel as there is for Soong path handling and should be used |
| 33 | // in similar circumstances |
| 34 | // |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 35 | // Bazel Soong |
| 36 | // ============================================================== |
| 37 | // BazelLabelForModuleSrc PathForModuleSrc |
| 38 | // BazelLabelForModuleSrcExcludes PathForModuleSrcExcludes |
| 39 | // BazelLabelForModuleDeps n/a |
| 40 | // tbd PathForSource |
| 41 | // tbd ExistentPathsForSources |
| 42 | // PathForBazelOut PathForModuleOut |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 43 | // |
| 44 | // Use cases: |
| 45 | // * Module contains a property (often tagged `android:"path"`) that expects paths *relative to the |
| 46 | // module directory*: |
| 47 | // * BazelLabelForModuleSrcExcludes, if the module also contains an excludes_<propname> property |
| 48 | // * BazelLabelForModuleSrc, otherwise |
| 49 | // * Converting references to other modules to Bazel Labels: |
| 50 | // BazelLabelForModuleDeps |
| 51 | // * Converting a path obtained from bazel_handler cquery results: |
| 52 | // PathForBazelOut |
| 53 | // |
| 54 | // NOTE: all Soong globs are expanded within Soong rather than being converted to a Bazel glob |
| 55 | // syntax. This occurs because Soong does not have a concept of crossing package boundaries, |
| 56 | // so the glob as computed by Soong may contain paths that cross package-boundaries. These |
| 57 | // would be unknowingly omitted if the glob were handled by Bazel. By expanding globs within |
| 58 | // Soong, we support identification and detection (within Bazel) use of paths that cross |
| 59 | // package boundaries. |
| 60 | // |
| 61 | // Path resolution: |
| 62 | // * filepath/globs: resolves as itself or is converted to an absolute Bazel label (e.g. |
| 63 | // //path/to/dir:<filepath>) if path exists in a separate package or subpackage. |
| 64 | // * references to other modules (using the ":name{.tag}" syntax). These resolve as a Bazel label |
| 65 | // for a target. If the Bazel target is in the local module directory, it will be returned |
| 66 | // relative to the current package (e.g. ":<target>"). Otherwise, it will be returned as an |
| 67 | // absolute Bazel label (e.g. "//path/to/dir:<target>"). If the reference to another module |
| 68 | // cannot be resolved,the function will panic. This is often due to the dependency not being added |
| 69 | // via an AddDependency* method. |
| 70 | |
Usta Shrestha | db46a9b | 2022-07-11 11:29:56 -0400 | [diff] [blame] | 71 | // BazelConversionContext is a minimal context interface to check if a module should be converted by bp2build, |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 72 | // with functions containing information to match against allowlists and denylists. |
| 73 | // If a module is deemed to be convertible by bp2build, then it should rely on a |
| 74 | // BazelConversionPathContext for more functions for dep/path features. |
| 75 | type BazelConversionContext interface { |
| 76 | Config() Config |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 77 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 78 | Module() Module |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 79 | OtherModuleType(m blueprint.Module) string |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 80 | OtherModuleName(m blueprint.Module) string |
| 81 | OtherModuleDir(m blueprint.Module) string |
Sam Delmerico | 24c5603 | 2022-03-28 19:53:03 +0000 | [diff] [blame] | 82 | ModuleErrorf(format string, args ...interface{}) |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | // A subset of the ModuleContext methods which are sufficient to resolve references to paths/deps in |
| 86 | // order to form a Bazel-compatible label for conversion. |
| 87 | type BazelConversionPathContext interface { |
| 88 | EarlyModulePathContext |
| 89 | BazelConversionContext |
| 90 | |
| 91 | ModuleErrorf(fmt string, args ...interface{}) |
| 92 | PropertyErrorf(property, fmt string, args ...interface{}) |
| 93 | GetDirectDep(name string) (blueprint.Module, blueprint.DependencyTag) |
| 94 | ModuleFromName(name string) (blueprint.Module, bool) |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 95 | AddUnconvertedBp2buildDep(string) |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 96 | AddMissingBp2buildDep(dep string) |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | // BazelLabelForModuleDeps expects a list of reference to other modules, ("<module>" |
| 100 | // or ":<module>") and returns a Bazel-compatible label which corresponds to dependencies on the |
| 101 | // module within the given ctx. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 102 | func BazelLabelForModuleDeps(ctx BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 103 | return BazelLabelForModuleDepsWithFn(ctx, modules, BazelModuleLabel) |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // BazelLabelForModuleWholeDepsExcludes expects two lists: modules (containing modules to include in |
| 107 | // the list), and excludes (modules to exclude from the list). Both of these should contain |
| 108 | // references to other modules, ("<module>" or ":<module>"). It returns a Bazel-compatible label |
| 109 | // list which corresponds to dependencies on the module within the given ctx, and the excluded |
| 110 | // dependencies. Prebuilt dependencies will be appended with _alwayslink so they can be handled as |
| 111 | // whole static libraries. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 112 | func BazelLabelForModuleDepsExcludes(ctx BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 113 | return BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, BazelModuleLabel) |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 114 | } |
| 115 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 116 | // BazelLabelForModuleDepsWithFn expects a list of reference to other modules, ("<module>" |
| 117 | // or ":<module>") and applies moduleToLabelFn to determine and return a Bazel-compatible label |
| 118 | // which corresponds to dependencies on the module within the given ctx. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 119 | func BazelLabelForModuleDepsWithFn(ctx BazelConversionPathContext, modules []string, |
| 120 | moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 121 | var labels bazel.LabelList |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 122 | // In some cases, a nil string list is different than an explicitly empty list. |
| 123 | if len(modules) == 0 && modules != nil { |
| 124 | labels.Includes = []bazel.Label{} |
| 125 | return labels |
| 126 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 127 | for _, module := range modules { |
| 128 | bpText := module |
| 129 | if m := SrcIsModule(module); m == "" { |
| 130 | module = ":" + module |
| 131 | } |
| 132 | if m, t := SrcIsModuleWithTag(module); m != "" { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 133 | l := getOtherModuleLabel(ctx, m, t, moduleToLabelFn) |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 134 | if l != nil { |
| 135 | l.OriginalModuleName = bpText |
| 136 | labels.Includes = append(labels.Includes, *l) |
| 137 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 138 | } else { |
| 139 | ctx.ModuleErrorf("%q, is not a module reference", module) |
| 140 | } |
| 141 | } |
| 142 | return labels |
| 143 | } |
| 144 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 145 | // BazelLabelForModuleDepsExcludesWithFn expects two lists: modules (containing modules to include in the |
| 146 | // list), and excludes (modules to exclude from the list). Both of these should contain references |
| 147 | // to other modules, ("<module>" or ":<module>"). It applies moduleToLabelFn to determine and return a |
| 148 | // Bazel-compatible label list which corresponds to dependencies on the module within the given ctx, and |
| 149 | // the excluded dependencies. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 150 | func BazelLabelForModuleDepsExcludesWithFn(ctx BazelConversionPathContext, modules, excludes []string, |
| 151 | moduleToLabelFn func(BazelConversionPathContext, blueprint.Module) string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 152 | moduleLabels := BazelLabelForModuleDepsWithFn(ctx, RemoveListFromList(modules, excludes), moduleToLabelFn) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 153 | if len(excludes) == 0 { |
| 154 | return moduleLabels |
| 155 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 156 | excludeLabels := BazelLabelForModuleDepsWithFn(ctx, excludes, moduleToLabelFn) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 157 | return bazel.LabelList{ |
| 158 | Includes: moduleLabels.Includes, |
| 159 | Excludes: excludeLabels.Includes, |
| 160 | } |
| 161 | } |
| 162 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 163 | func BazelLabelForModuleSrcSingle(ctx BazelConversionPathContext, path string) bazel.Label { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 164 | if srcs := BazelLabelForModuleSrcExcludes(ctx, []string{path}, []string(nil)).Includes; len(srcs) > 0 { |
| 165 | return srcs[0] |
| 166 | } |
| 167 | return bazel.Label{} |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 168 | } |
| 169 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 170 | func BazelLabelForModuleDepSingle(ctx BazelConversionPathContext, path string) bazel.Label { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 171 | if srcs := BazelLabelForModuleDepsExcludes(ctx, []string{path}, []string(nil)).Includes; len(srcs) > 0 { |
| 172 | return srcs[0] |
| 173 | } |
| 174 | return bazel.Label{} |
Rupert Shuttleworth | 6e4950a | 2021-07-27 01:34:59 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 177 | // BazelLabelForModuleSrc expects a list of path (relative to local module directory) and module |
| 178 | // references (":<module>") and returns a bazel.LabelList{} containing the resolved references in |
| 179 | // paths, relative to the local module, or Bazel-labels (absolute if in a different package or |
| 180 | // relative if within the same package). |
| 181 | // Properties must have been annotated with struct tag `android:"path"` so that dependencies modules |
| 182 | // will have already been handled by the path_deps mutator. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 183 | func BazelLabelForModuleSrc(ctx BazelConversionPathContext, paths []string) bazel.LabelList { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 184 | return BazelLabelForModuleSrcExcludes(ctx, paths, []string(nil)) |
| 185 | } |
| 186 | |
| 187 | // BazelLabelForModuleSrc expects lists of path and excludes (relative to local module directory) |
| 188 | // and module references (":<module>") and returns a bazel.LabelList{} containing the resolved |
| 189 | // references in paths, minus those in excludes, relative to the local module, or Bazel-labels |
| 190 | // (absolute if in a different package or relative if within the same package). |
| 191 | // Properties must have been annotated with struct tag `android:"path"` so that dependencies modules |
| 192 | // will have already been handled by the path_deps mutator. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 193 | func BazelLabelForModuleSrcExcludes(ctx BazelConversionPathContext, paths, excludes []string) bazel.LabelList { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 194 | excludeLabels := expandSrcsForBazel(ctx, excludes, []string(nil)) |
| 195 | excluded := make([]string, 0, len(excludeLabels.Includes)) |
| 196 | for _, e := range excludeLabels.Includes { |
| 197 | excluded = append(excluded, e.Label) |
| 198 | } |
| 199 | labels := expandSrcsForBazel(ctx, paths, excluded) |
| 200 | labels.Excludes = excludeLabels.Includes |
| 201 | labels = transformSubpackagePaths(ctx, labels) |
| 202 | return labels |
| 203 | } |
| 204 | |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 205 | // Returns true if a prefix + components[:i] is a package boundary. |
| 206 | // |
| 207 | // A package boundary is determined by a BUILD file in the directory. This can happen in 2 cases: |
| 208 | // |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 209 | // 1. An Android.bp exists, which bp2build will always convert to a sibling BUILD file. |
| 210 | // 2. An Android.bp doesn't exist, but a checked-in BUILD/BUILD.bazel file exists, and that file |
| 211 | // is allowlisted by the bp2build configuration to be merged into the symlink forest workspace. |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 212 | func isPackageBoundary(config Config, prefix string, components []string, componentIndex int) bool { |
| 213 | prefix = filepath.Join(prefix, filepath.Join(components[:componentIndex+1]...)) |
| 214 | if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "Android.bp")); exists { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 215 | return true |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 216 | } else if config.Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(prefix) { |
| 217 | if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "BUILD")); exists { |
| 218 | return true |
| 219 | } else if exists, _, _ := config.fs.Exists(filepath.Join(prefix, "BUILD.bazel")); exists { |
| 220 | return true |
| 221 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 222 | } |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 223 | |
| 224 | return false |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | // Transform a path (if necessary) to acknowledge package boundaries |
| 228 | // |
| 229 | // e.g. something like |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 230 | // |
| 231 | // async_safe/include/async_safe/CHECK.h |
| 232 | // |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 233 | // might become |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 234 | // |
| 235 | // //bionic/libc/async_safe:include/async_safe/CHECK.h |
| 236 | // |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 237 | // if the "async_safe" directory is actually a package and not just a directory. |
| 238 | // |
| 239 | // In particular, paths that extend into packages are transformed into absolute labels beginning with //. |
| 240 | func transformSubpackagePath(ctx BazelConversionPathContext, path bazel.Label) bazel.Label { |
| 241 | var newPath bazel.Label |
| 242 | |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 243 | // Don't transform OriginalModuleName |
| 244 | newPath.OriginalModuleName = path.OriginalModuleName |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 245 | |
| 246 | if strings.HasPrefix(path.Label, "//") { |
| 247 | // Assume absolute labels are already correct (e.g. //path/to/some/package:foo.h) |
| 248 | newPath.Label = path.Label |
| 249 | return newPath |
| 250 | } |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 251 | if strings.HasPrefix(path.Label, "./") { |
| 252 | // Drop "./" for consistent handling of paths. |
| 253 | // Specifically, to not let "." be considered a package boundary. |
| 254 | // Say `inputPath` is `x/Android.bp` and that file has some module |
| 255 | // with `srcs=["y/a.c", "z/b.c"]`. |
| 256 | // And say the directory tree is: |
| 257 | // x |
| 258 | // ├── Android.bp |
| 259 | // ├── y |
| 260 | // │ ├── a.c |
| 261 | // │ └── Android.bp |
| 262 | // └── z |
| 263 | // └── b.c |
| 264 | // Then bazel equivalent labels in srcs should be: |
| 265 | // //x/y:a.c, x/z/b.c |
| 266 | // The above should still be the case if `x/Android.bp` had |
| 267 | // srcs=["./y/a.c", "./z/b.c"] |
| 268 | // However, if we didn't strip "./", we'd get |
| 269 | // //x/./y:a.c, //x/.:z/b.c |
| 270 | path.Label = strings.TrimPrefix(path.Label, "./") |
| 271 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 272 | pathComponents := strings.Split(path.Label, "/") |
Usta Shrestha | d558031 | 2022-09-23 16:46:38 -0400 | [diff] [blame] | 273 | newLabel := "" |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 274 | foundPackageBoundary := false |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 275 | // Check the deepest subdirectory first and work upwards |
| 276 | for i := len(pathComponents) - 1; i >= 0; i-- { |
| 277 | pathComponent := pathComponents[i] |
| 278 | var sep string |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 279 | if !foundPackageBoundary && isPackageBoundary(ctx.Config(), ctx.ModuleDir(), pathComponents, i) { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 280 | sep = ":" |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 281 | foundPackageBoundary = true |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 282 | } else { |
| 283 | sep = "/" |
| 284 | } |
| 285 | if newLabel == "" { |
| 286 | newLabel = pathComponent |
| 287 | } else { |
| 288 | newLabel = pathComponent + sep + newLabel |
| 289 | } |
| 290 | } |
Jingwen Chen | 0eeaeb8 | 2022-09-21 10:27:42 +0000 | [diff] [blame] | 291 | if foundPackageBoundary { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 292 | // Ensure paths end up looking like //bionic/... instead of //./bionic/... |
| 293 | moduleDir := ctx.ModuleDir() |
| 294 | if strings.HasPrefix(moduleDir, ".") { |
| 295 | moduleDir = moduleDir[1:] |
| 296 | } |
| 297 | // Make the path into an absolute label (e.g. //bionic/libc/foo:bar.h instead of just foo:bar.h) |
| 298 | if moduleDir == "" { |
| 299 | newLabel = "//" + newLabel |
| 300 | } else { |
| 301 | newLabel = "//" + moduleDir + "/" + newLabel |
| 302 | } |
| 303 | } |
| 304 | newPath.Label = newLabel |
| 305 | |
| 306 | return newPath |
| 307 | } |
| 308 | |
| 309 | // Transform paths to acknowledge package boundaries |
| 310 | // See transformSubpackagePath() for more information |
| 311 | func transformSubpackagePaths(ctx BazelConversionPathContext, paths bazel.LabelList) bazel.LabelList { |
| 312 | var newPaths bazel.LabelList |
| 313 | for _, include := range paths.Includes { |
| 314 | newPaths.Includes = append(newPaths.Includes, transformSubpackagePath(ctx, include)) |
| 315 | } |
| 316 | for _, exclude := range paths.Excludes { |
| 317 | newPaths.Excludes = append(newPaths.Excludes, transformSubpackagePath(ctx, exclude)) |
| 318 | } |
| 319 | return newPaths |
| 320 | } |
| 321 | |
Romain Jobredeaux | 1282c42 | 2021-10-29 10:52:59 -0400 | [diff] [blame] | 322 | // Converts root-relative Paths to a list of bazel.Label relative to the module in ctx. |
| 323 | func RootToModuleRelativePaths(ctx BazelConversionPathContext, paths Paths) []bazel.Label { |
| 324 | var newPaths []bazel.Label |
| 325 | for _, path := range PathsWithModuleSrcSubDir(ctx, paths, "") { |
| 326 | s := path.Rel() |
| 327 | newPaths = append(newPaths, bazel.Label{Label: s}) |
| 328 | } |
| 329 | return newPaths |
| 330 | } |
| 331 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 332 | // expandSrcsForBazel returns bazel.LabelList with paths rooted from the module's local source |
| 333 | // directory and Bazel target labels, excluding those included in the excludes argument (which |
| 334 | // should already be expanded to resolve references to Soong-modules). Valid elements of paths |
| 335 | // include: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 336 | // - filepath, relative to local module directory, resolves as a filepath relative to the local |
| 337 | // source directory |
| 338 | // - glob, relative to the local module directory, resolves as filepath(s), relative to the local |
| 339 | // module directory. Because Soong does not have a concept of crossing package boundaries, the |
| 340 | // glob as computed by Soong may contain paths that cross package-boundaries that would be |
| 341 | // unknowingly omitted if the glob were handled by Bazel. To allow identification and detect |
| 342 | // (within Bazel) use of paths that cross package boundaries, we expand globs within Soong rather |
| 343 | // than converting Soong glob syntax to Bazel glob syntax. **Invalid for excludes.** |
| 344 | // - other modules using the ":name{.tag}" syntax. These modules must implement SourceFileProducer |
| 345 | // or OutputFileProducer. These resolve as a Bazel label for a target. If the Bazel target is in |
| 346 | // the local module directory, it will be returned relative to the current package (e.g. |
| 347 | // ":<target>"). Otherwise, it will be returned as an absolute Bazel label (e.g. |
| 348 | // "//path/to/dir:<target>"). If the reference to another module cannot be resolved,the function |
| 349 | // will panic. |
| 350 | // |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 351 | // Properties passed as the paths or excludes argument must have been annotated with struct tag |
| 352 | // `android:"path"` so that dependencies on other modules will have already been handled by the |
| 353 | // path_deps mutator. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 354 | func expandSrcsForBazel(ctx BazelConversionPathContext, paths, expandedExcludes []string) bazel.LabelList { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 355 | if paths == nil { |
| 356 | return bazel.LabelList{} |
| 357 | } |
| 358 | labels := bazel.LabelList{ |
| 359 | Includes: []bazel.Label{}, |
| 360 | } |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 361 | |
| 362 | // expandedExcludes contain module-dir relative paths, but root-relative paths |
| 363 | // are needed for GlobFiles later. |
| 364 | var rootRelativeExpandedExcludes []string |
| 365 | for _, e := range expandedExcludes { |
| 366 | rootRelativeExpandedExcludes = append(rootRelativeExpandedExcludes, filepath.Join(ctx.ModuleDir(), e)) |
| 367 | } |
| 368 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 369 | for _, p := range paths { |
| 370 | if m, tag := SrcIsModuleWithTag(p); m != "" { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 371 | l := getOtherModuleLabel(ctx, m, tag, BazelModuleLabel) |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 372 | if l != nil && !InList(l.Label, expandedExcludes) { |
Jingwen Chen | 38e6264 | 2021-04-19 05:00:15 +0000 | [diff] [blame] | 373 | l.OriginalModuleName = fmt.Sprintf(":%s", m) |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 374 | labels.Includes = append(labels.Includes, *l) |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 375 | } |
| 376 | } else { |
| 377 | var expandedPaths []bazel.Label |
| 378 | if pathtools.IsGlob(p) { |
Jingwen Chen | 4ecc67d | 2021-04-27 09:47:02 +0000 | [diff] [blame] | 379 | // e.g. turn "math/*.c" in |
| 380 | // external/arm-optimized-routines to external/arm-optimized-routines/math/*.c |
| 381 | rootRelativeGlobPath := pathForModuleSrc(ctx, p).String() |
Romain Jobredeaux | 1282c42 | 2021-10-29 10:52:59 -0400 | [diff] [blame] | 382 | expandedPaths = RootToModuleRelativePaths(ctx, GlobFiles(ctx, rootRelativeGlobPath, rootRelativeExpandedExcludes)) |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 383 | } else { |
| 384 | if !InList(p, expandedExcludes) { |
| 385 | expandedPaths = append(expandedPaths, bazel.Label{Label: p}) |
| 386 | } |
| 387 | } |
| 388 | labels.Includes = append(labels.Includes, expandedPaths...) |
| 389 | } |
| 390 | } |
| 391 | return labels |
| 392 | } |
| 393 | |
| 394 | // getOtherModuleLabel returns a bazel.Label for the given dependency/tag combination for the |
| 395 | // module. The label will be relative to the current directory if appropriate. The dependency must |
| 396 | // already be resolved by either deps mutator or path deps mutator. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 397 | func getOtherModuleLabel(ctx BazelConversionPathContext, dep, tag string, |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 398 | labelFromModule func(BazelConversionPathContext, blueprint.Module) string) *bazel.Label { |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 399 | m, _ := ctx.ModuleFromName(dep) |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 400 | // The module was not found in an Android.bp file, this is often due to: |
| 401 | // * a limited manifest |
| 402 | // * a required module not being converted from Android.mk |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 403 | if m == nil { |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 404 | ctx.AddMissingBp2buildDep(dep) |
| 405 | return &bazel.Label{ |
| 406 | Label: ":" + dep + "__BP2BUILD__MISSING__DEP", |
| 407 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 408 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 409 | if !convertedToBazel(ctx, m) { |
| 410 | ctx.AddUnconvertedBp2buildDep(dep) |
| 411 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 412 | label := BazelModuleLabel(ctx, ctx.Module()) |
| 413 | otherLabel := labelFromModule(ctx, m) |
| 414 | |
| 415 | // TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets. |
| 416 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 417 | if samePackage(label, otherLabel) { |
| 418 | otherLabel = bazelShortLabel(otherLabel) |
| 419 | } |
| 420 | |
Liz Kammer | daa09ef | 2021-12-15 15:35:38 -0500 | [diff] [blame] | 421 | return &bazel.Label{ |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 422 | Label: otherLabel, |
| 423 | } |
| 424 | } |
| 425 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 426 | func BazelModuleLabel(ctx BazelConversionPathContext, module blueprint.Module) string { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 427 | // TODO(b/165114590): Convert tag (":name{.tag}") to corresponding Bazel implicit output targets. |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 428 | if !convertedToBazel(ctx, module) { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 429 | return bp2buildModuleLabel(ctx, module) |
| 430 | } |
Liz Kammer | 6eff323 | 2021-08-26 08:37:59 -0400 | [diff] [blame] | 431 | b, _ := module.(Bazelable) |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 432 | return b.GetBazelLabel(ctx, module) |
| 433 | } |
| 434 | |
| 435 | func bazelShortLabel(label string) string { |
| 436 | i := strings.Index(label, ":") |
Jingwen Chen | 80b6b64 | 2021-11-02 06:23:07 +0000 | [diff] [blame] | 437 | if i == -1 { |
| 438 | panic(fmt.Errorf("Could not find the ':' character in '%s', expected a fully qualified label.", label)) |
| 439 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 440 | return label[i:] |
| 441 | } |
| 442 | |
| 443 | func bazelPackage(label string) string { |
| 444 | i := strings.Index(label, ":") |
Jingwen Chen | 80b6b64 | 2021-11-02 06:23:07 +0000 | [diff] [blame] | 445 | if i == -1 { |
| 446 | panic(fmt.Errorf("Could not find the ':' character in '%s', expected a fully qualified label.", label)) |
| 447 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 448 | return label[0:i] |
| 449 | } |
| 450 | |
| 451 | func samePackage(label1, label2 string) bool { |
| 452 | return bazelPackage(label1) == bazelPackage(label2) |
| 453 | } |
| 454 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 455 | func bp2buildModuleLabel(ctx BazelConversionContext, module blueprint.Module) string { |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 456 | moduleName := ctx.OtherModuleName(module) |
| 457 | moduleDir := ctx.OtherModuleDir(module) |
| 458 | return fmt.Sprintf("//%s:%s", moduleDir, moduleName) |
| 459 | } |
| 460 | |
Vinh Tran | b6803a5 | 2022-12-14 11:34:54 -0500 | [diff] [blame^] | 461 | // ModuleFromBazelLabel reverses the logic in bp2buildModuleLabel |
| 462 | func ModuleFromBazelLabel(label string) string { |
| 463 | return strings.Split(label, ":")[1] |
| 464 | } |
| 465 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 466 | // BazelOutPath is a Bazel output path compatible to be used for mixed builds within Soong/Ninja. |
| 467 | type BazelOutPath struct { |
| 468 | OutputPath |
| 469 | } |
| 470 | |
Liz Kammer | 0f3b7d2 | 2021-09-28 13:48:21 -0400 | [diff] [blame] | 471 | // ensure BazelOutPath implements Path |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 472 | var _ Path = BazelOutPath{} |
Liz Kammer | 0f3b7d2 | 2021-09-28 13:48:21 -0400 | [diff] [blame] | 473 | |
| 474 | // ensure BazelOutPath implements genPathProvider |
| 475 | var _ genPathProvider = BazelOutPath{} |
| 476 | |
| 477 | // ensure BazelOutPath implements objPathProvider |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 478 | var _ objPathProvider = BazelOutPath{} |
| 479 | |
Liz Kammer | 0f3b7d2 | 2021-09-28 13:48:21 -0400 | [diff] [blame] | 480 | func (p BazelOutPath) genPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleGenPath { |
| 481 | return PathForModuleGen(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 482 | } |
| 483 | |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 484 | func (p BazelOutPath) objPathWithExt(ctx ModuleOutPathContext, subdir, ext string) ModuleObjPath { |
| 485 | return PathForModuleObj(ctx, subdir, pathtools.ReplaceExtension(p.path, ext)) |
| 486 | } |
| 487 | |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 488 | // PathForBazelOutRelative returns a BazelOutPath representing the path under an output directory dedicated to |
| 489 | // bazel-owned outputs. Calling .Rel() on the result will give the input path as relative to the given |
| 490 | // relativeRoot. |
| 491 | func PathForBazelOutRelative(ctx PathContext, relativeRoot string, path string) BazelOutPath { |
| 492 | validatedPath, err := validatePath(filepath.Join("execroot", "__main__", path)) |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 493 | if err != nil { |
| 494 | reportPathError(ctx, err) |
| 495 | } |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 496 | relativeRootPath := filepath.Join("execroot", "__main__", relativeRoot) |
| 497 | if pathComponents := strings.Split(path, "/"); len(pathComponents) >= 3 && |
| 498 | pathComponents[0] == "bazel-out" && pathComponents[2] == "bin" { |
| 499 | // If the path starts with something like: bazel-out/linux_x86_64-fastbuild-ST-b4ef1c4402f9/bin/ |
| 500 | // make it relative to that folder. bazel-out/volatile-status.txt is an example |
| 501 | // of something that starts with bazel-out but is not relative to the bin folder |
| 502 | relativeRootPath = filepath.Join("execroot", "__main__", pathComponents[0], pathComponents[1], pathComponents[2], relativeRoot) |
| 503 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 504 | |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 505 | var relPath string |
| 506 | if relPath, err = filepath.Rel(relativeRootPath, validatedPath); err != nil || strings.HasPrefix(relPath, "../") { |
| 507 | // We failed to make this path relative to execroot/__main__, fall back to a non-relative path |
| 508 | // One case where this happens is when path is ../bazel_tools/something |
| 509 | relativeRootPath = "" |
| 510 | relPath = validatedPath |
| 511 | } |
| 512 | |
| 513 | outputPath := OutputPath{ |
| 514 | basePath{"", ""}, |
Lukacs T. Berki | 9f6c24a | 2021-08-26 15:07:24 +0200 | [diff] [blame] | 515 | ctx.Config().soongOutDir, |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 516 | ctx.Config().BazelContext.OutputBase(), |
| 517 | } |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 518 | |
| 519 | return BazelOutPath{ |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 520 | // .withRel() appends its argument onto the current path, and only the most |
| 521 | // recently appended part is returned by outputPath.rel(). |
| 522 | // So outputPath.rel() will return relPath. |
| 523 | OutputPath: outputPath.withRel(relativeRootPath).withRel(relPath), |
Liz Kammer | 620dea6 | 2021-04-14 17:36:10 -0400 | [diff] [blame] | 524 | } |
| 525 | } |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 526 | |
Cole Faust | 9a06d25 | 2022-06-03 16:00:11 -0700 | [diff] [blame] | 527 | // PathForBazelOut returns a BazelOutPath representing the path under an output directory dedicated to |
| 528 | // bazel-owned outputs. |
| 529 | func PathForBazelOut(ctx PathContext, path string) BazelOutPath { |
| 530 | return PathForBazelOutRelative(ctx, "", path) |
| 531 | } |
| 532 | |
Liz Kammer | b6a55bf | 2021-04-12 15:42:51 -0400 | [diff] [blame] | 533 | // PathsForBazelOut returns a list of paths representing the paths under an output directory |
| 534 | // dedicated to Bazel-owned outputs. |
| 535 | func PathsForBazelOut(ctx PathContext, paths []string) Paths { |
| 536 | outs := make(Paths, 0, len(paths)) |
| 537 | for _, p := range paths { |
| 538 | outs = append(outs, PathForBazelOut(ctx, p)) |
| 539 | } |
| 540 | return outs |
| 541 | } |
Jingwen Chen | 6817bbb | 2022-10-14 09:56:07 +0000 | [diff] [blame] | 542 | |
| 543 | // BazelStringOrLabelFromProp splits a Soong module property that can be |
| 544 | // either a string literal, path (with android:path tag) or a module reference |
| 545 | // into separate bazel string or label attributes. Bazel treats string and label |
| 546 | // attributes as distinct types, so this function categorizes a string property |
| 547 | // into either one of them. |
| 548 | // |
| 549 | // e.g. apex.private_key = "foo.pem" can either refer to: |
| 550 | // |
| 551 | // 1. "foo.pem" in the current directory -> file target |
| 552 | // 2. "foo.pem" module -> rule target |
| 553 | // 3. "foo.pem" file in a different directory, prefixed by a product variable handled |
| 554 | // in a bazel macro. -> string literal |
| 555 | // |
| 556 | // For the first two cases, they are defined using the label attribute. For the third case, |
| 557 | // it's defined with the string attribute. |
| 558 | func BazelStringOrLabelFromProp( |
| 559 | ctx TopDownMutatorContext, |
| 560 | propToDistinguish *string) (bazel.LabelAttribute, bazel.StringAttribute) { |
| 561 | |
| 562 | var labelAttr bazel.LabelAttribute |
| 563 | var strAttr bazel.StringAttribute |
| 564 | |
| 565 | if propToDistinguish == nil { |
| 566 | // nil pointer |
| 567 | return labelAttr, strAttr |
| 568 | } |
| 569 | |
| 570 | prop := String(propToDistinguish) |
| 571 | if SrcIsModule(prop) != "" { |
| 572 | // If it's a module (SrcIsModule will return the module name), set the |
| 573 | // resolved label to the label attribute. |
| 574 | labelAttr.SetValue(BazelLabelForModuleDepSingle(ctx, prop)) |
| 575 | } else { |
| 576 | // Not a module name. This could be a string literal or a file target in |
| 577 | // the current dir. Check if the path exists: |
| 578 | path := ExistentPathForSource(ctx, ctx.ModuleDir(), prop) |
| 579 | |
| 580 | if path.Valid() && parentDir(path.String()) == ctx.ModuleDir() { |
| 581 | // If it exists and the path is relative to the current dir, resolve the bazel label |
| 582 | // for the _file target_ and set it to the label attribute. |
| 583 | // |
| 584 | // Resolution is necessary because this could be a file in a subpackage. |
| 585 | labelAttr.SetValue(BazelLabelForModuleSrcSingle(ctx, prop)) |
| 586 | } else { |
| 587 | // Otherwise, treat it as a string literal and assign to the string attribute. |
| 588 | strAttr.Value = propToDistinguish |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | return labelAttr, strAttr |
| 593 | } |