Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1 | // Copyright 2021 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 | package cc |
| 15 | |
| 16 | import ( |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 17 | "fmt" |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 18 | "path/filepath" |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 19 | "strings" |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/bazel" |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 23 | |
| 24 | "github.com/google/blueprint/proptools" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 28 | |
| 29 | func init() { |
| 30 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 31 | } |
| 32 | |
| 33 | func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) { |
| 34 | ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator) |
| 35 | } |
| 36 | |
| 37 | // A naive deps mutator to add deps on all modules across all combinations of |
| 38 | // target props for cc modules. This is needed to make module -> bazel label |
| 39 | // resolution work in the bp2build mutator later. This is probably |
| 40 | // the wrong way to do it, but it works. |
| 41 | // |
| 42 | // TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this? |
| 43 | func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { |
| 44 | module, ok := ctx.Module().(*Module) |
| 45 | if !ok { |
| 46 | // Not a cc module |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | if !module.ConvertWithBp2build(ctx) { |
| 51 | return |
| 52 | } |
| 53 | |
| 54 | var allDeps []string |
| 55 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 56 | for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) { |
| 57 | for _, props := range configToProps { |
| 58 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 59 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 60 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 61 | } |
| 62 | } |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 63 | } |
| 64 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 65 | for _, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { |
| 66 | for _, props := range configToProps { |
| 67 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 68 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 69 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 70 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 71 | allDeps = append(allDeps, baseLinkerProps.Exclude_static_libs...) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 72 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 73 | allDeps = append(allDeps, baseLinkerProps.Shared_libs...) |
| 74 | allDeps = append(allDeps, baseLinkerProps.Exclude_shared_libs...) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 75 | } |
| 76 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 79 | // Deps in the static: { .. } and shared: { .. } props of a cc_library. |
| 80 | if lib, ok := module.compiler.(*libraryDecorator); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 81 | appendDeps := func(deps []string, p StaticOrSharedProperties) []string { |
| 82 | deps = append(deps, p.Static_libs...) |
| 83 | deps = append(deps, p.Whole_static_libs...) |
| 84 | deps = append(deps, p.Shared_libs...) |
| 85 | return deps |
| 86 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 87 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 88 | allDeps = appendDeps(allDeps, lib.SharedProperties.Shared) |
| 89 | allDeps = appendDeps(allDeps, lib.StaticProperties.Static) |
Jingwen Chen | 45dec10 | 2021-05-19 10:30:29 +0000 | [diff] [blame] | 90 | |
| 91 | // TODO(b/186024507, b/186489250): Temporarily exclude adding |
| 92 | // system_shared_libs deps until libc and libm builds. |
| 93 | // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 94 | // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 95 | |
| 96 | // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library. |
| 97 | // target: { <target>: shared: { ... } } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 98 | for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 99 | for _, props := range configToProps { |
| 100 | if p, ok := props.(*SharedProperties); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 101 | allDeps = appendDeps(allDeps, p.Shared) |
| 102 | } |
| 103 | } |
| 104 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 105 | |
| 106 | for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 107 | for _, props := range configToProps { |
| 108 | if p, ok := props.(*StaticProperties); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 109 | allDeps = appendDeps(allDeps, p.Static) |
| 110 | } |
| 111 | } |
| 112 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 115 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 116 | } |
| 117 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 118 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 119 | // properties which apply to either the shared or static version of a cc_library module. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 120 | type staticOrSharedAttributes struct { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 121 | srcs bazel.LabelListAttribute |
| 122 | srcs_c bazel.LabelListAttribute |
| 123 | srcs_as bazel.LabelListAttribute |
| 124 | |
| 125 | copts bazel.StringListAttribute |
| 126 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 127 | staticDeps bazel.LabelListAttribute |
| 128 | dynamicDeps bazel.LabelListAttribute |
| 129 | wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 132 | func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) { |
| 133 | // Branch srcs into three language-specific groups. |
| 134 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 135 | // know the language of these sources until the genrule is executed. |
| 136 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 137 | isCSrcOrFilegroup := func(s string) bool { |
| 138 | return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs") |
| 139 | } |
| 140 | |
| 141 | isAsmSrcOrFilegroup := func(s string) bool { |
| 142 | return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs") |
| 143 | } |
| 144 | |
| 145 | // Check that a module is a filegroup type named <label>. |
| 146 | isFilegroupNamed := func(m android.Module, fullLabel string) bool { |
| 147 | if ctx.OtherModuleType(m) != "filegroup" { |
| 148 | return false |
| 149 | } |
| 150 | labelParts := strings.Split(fullLabel, ":") |
| 151 | if len(labelParts) > 2 { |
| 152 | // There should not be more than one colon in a label. |
| 153 | panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel)) |
| 154 | } else { |
| 155 | return m.Name() == labelParts[len(labelParts)-1] |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | // Convert the filegroup dependencies into the extension-specific filegroups |
| 160 | // filtered in the filegroup.bzl macro. |
| 161 | cppFilegroup := func(label string) string { |
| 162 | ctx.VisitDirectDeps(func(m android.Module) { |
| 163 | if isFilegroupNamed(m, label) { |
| 164 | label = label + "_cpp_srcs" |
| 165 | return |
| 166 | } |
| 167 | }) |
| 168 | return label |
| 169 | } |
| 170 | cFilegroup := func(label string) string { |
| 171 | ctx.VisitDirectDeps(func(m android.Module) { |
| 172 | if isFilegroupNamed(m, label) { |
| 173 | label = label + "_c_srcs" |
| 174 | return |
| 175 | } |
| 176 | }) |
| 177 | return label |
| 178 | } |
| 179 | asFilegroup := func(label string) string { |
| 180 | ctx.VisitDirectDeps(func(m android.Module) { |
| 181 | if isFilegroupNamed(m, label) { |
| 182 | label = label + "_as_srcs" |
| 183 | return |
| 184 | } |
| 185 | }) |
| 186 | return label |
| 187 | } |
| 188 | |
| 189 | cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup) |
| 190 | cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup) |
| 191 | |
| 192 | asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup) |
| 193 | asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup) |
| 194 | |
| 195 | cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup) |
| 196 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs) |
| 197 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs) |
| 198 | return |
| 199 | } |
| 200 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 201 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 202 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 203 | lib, ok := module.compiler.(*libraryDecorator) |
| 204 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 205 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 208 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 212 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 213 | lib, ok := module.compiler.(*libraryDecorator) |
| 214 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 215 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 218 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 219 | } |
| 220 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 221 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
| 222 | var props StaticOrSharedProperties |
| 223 | if isStatic { |
| 224 | props = lib.StaticProperties.Static |
| 225 | } else { |
| 226 | props = lib.SharedProperties.Shared |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 227 | } |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 228 | |
| 229 | attrs := staticOrSharedAttributes{ |
| 230 | copts: bazel.StringListAttribute{Value: props.Cflags}, |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 231 | srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)), |
| 232 | staticDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)), |
| 233 | dynamicDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Shared_libs)), |
| 234 | wholeArchiveDeps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)), |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 237 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
| 238 | attrs.copts.SetSelectValue(axis, config, props.Cflags) |
| 239 | attrs.srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
| 240 | attrs.staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) |
| 241 | attrs.dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) |
| 242 | attrs.wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | if isStatic { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 246 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 247 | for config, props := range configToProps { |
| 248 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 249 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | } else { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 254 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 255 | for config, props := range configToProps { |
| 256 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 257 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 263 | cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.srcs) |
| 264 | attrs.srcs = cppSrcs |
| 265 | attrs.srcs_c = cSrcs |
| 266 | attrs.srcs_as = asSrcs |
| 267 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 268 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 271 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 272 | type prebuiltAttributes struct { |
| 273 | Src bazel.LabelAttribute |
| 274 | } |
| 275 | |
| 276 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes { |
| 277 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 278 | prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker |
| 279 | |
| 280 | var srcLabelAttribute bazel.LabelAttribute |
| 281 | |
| 282 | if len(prebuiltLinker.properties.Srcs) > 1 { |
| 283 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n") |
| 284 | } |
| 285 | |
| 286 | if len(prebuiltLinker.properties.Srcs) == 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 287 | srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0])) |
| 288 | } |
| 289 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) { |
| 290 | for config, props := range configToProps { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 291 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 292 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 293 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config) |
| 294 | continue |
| 295 | } else if len(prebuiltLinkerProperties.Srcs) == 0 { |
| 296 | continue |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 297 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 298 | src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]) |
| 299 | srcLabelAttribute.SetSelectValue(axis, config, src) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | } |
| 303 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 304 | return prebuiltAttributes{ |
| 305 | Src: srcLabelAttribute, |
| 306 | } |
| 307 | } |
| 308 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 309 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 310 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 311 | // Options for all languages |
| 312 | copts bazel.StringListAttribute |
| 313 | // Assembly options and sources |
| 314 | asFlags bazel.StringListAttribute |
| 315 | asSrcs bazel.LabelListAttribute |
| 316 | // C options and sources |
| 317 | conlyFlags bazel.StringListAttribute |
| 318 | cSrcs bazel.LabelListAttribute |
| 319 | // C++ options and sources |
| 320 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 321 | srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 324 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 325 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 326 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 327 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 328 | var asFlags bazel.StringListAttribute |
| 329 | var conlyFlags bazel.StringListAttribute |
| 330 | var cppFlags bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 331 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 332 | // Creates the -I flags for a directory, while making the directory relative |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 333 | // to the exec root for Bazel to work. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 334 | includeFlags := func(dir string) []string { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 335 | // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 336 | moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir) |
| 337 | return []string{ |
| 338 | "-I" + moduleDirRootedPath, |
| 339 | // Include the bindir-rooted path (using make variable substitution). This most |
| 340 | // closely matches Bazel's native include path handling, which allows for dependency |
| 341 | // on generated headers in these directories. |
| 342 | // TODO(b/188084383): Handle local include directories in Bazel. |
| 343 | "-I$(BINDIR)/" + moduleDirRootedPath, |
| 344 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 347 | // Parse the list of module-relative include directories (-I). |
| 348 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 349 | // include_dirs are root-relative, not module-relative. |
| 350 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 351 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 352 | } |
| 353 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 354 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 355 | var result []string |
| 356 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 357 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 358 | // Bazel's copts, split them up to be compatible with the |
| 359 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 360 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 361 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 362 | return result |
| 363 | } |
| 364 | |
| 365 | // Parse the list of copts. |
| 366 | parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 367 | var copts []string |
| 368 | copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 369 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 370 | copts = append(copts, includeFlags(dir)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 371 | } |
| 372 | return copts |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 373 | } |
| 374 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame^] | 375 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 376 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 377 | // Add srcs-like dependencies such as generated files. |
| 378 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 379 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 380 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 381 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 382 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame^] | 383 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 384 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 387 | for _, props := range module.compiler.compilerProps() { |
| 388 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 389 | srcs.SetValue(parseSrcs(baseCompilerProps)) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 390 | copts.Value = parseCopts(baseCompilerProps) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 391 | asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags) |
| 392 | conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags) |
| 393 | cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 394 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 395 | break |
| 396 | } |
| 397 | } |
| 398 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 399 | // Handle include_build_directory prop. If the property is true, then the |
| 400 | // target has access to all headers recursively in the package, and has |
| 401 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 402 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 403 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 404 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 405 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 408 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 409 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 410 | for axis, configToProps := range archVariantCompilerProps { |
| 411 | for config, props := range configToProps { |
| 412 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
| 413 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 414 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 415 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 416 | srcsList := parseSrcs(baseCompilerProps) |
| 417 | srcs.SetSelectValue(axis, config, srcsList) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | copts.SetSelectValue(axis, config, parseCopts(baseCompilerProps)) |
| 421 | asFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 422 | conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 423 | cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 424 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame^] | 428 | srcs.ResolveExcludes() |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 429 | |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 430 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
| 431 | "Cflags": &copts, |
| 432 | "Asflags": &asFlags, |
| 433 | "CppFlags": &cppFlags, |
| 434 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 435 | productVariableProps := android.ProductVariableProperties(ctx) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 436 | for propName, attr := range productVarPropNameToAttribute { |
| 437 | if props, exists := productVariableProps[propName]; exists { |
| 438 | for _, prop := range props { |
| 439 | flags, ok := prop.Property.([]string) |
| 440 | if !ok { |
| 441 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 442 | } |
| 443 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 444 | attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.ProductConfigVariable), prop.ProductConfigVariable, newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 445 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 446 | } |
| 447 | } |
| 448 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 449 | srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs) |
| 450 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 451 | return compilerAttributes{ |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 452 | copts: copts, |
| 453 | srcs: srcs, |
| 454 | asFlags: asFlags, |
| 455 | asSrcs: asSrcs, |
| 456 | cSrcs: cSrcs, |
| 457 | conlyFlags: conlyFlags, |
| 458 | cppFlags: cppFlags, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
| 462 | // Convenience struct to hold all attributes parsed from linker properties. |
| 463 | type linkerAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 464 | deps bazel.LabelListAttribute |
| 465 | dynamicDeps bazel.LabelListAttribute |
| 466 | wholeArchiveDeps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 467 | exportedDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 468 | linkopts bazel.StringListAttribute |
| 469 | versionScript bazel.LabelAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 470 | } |
| 471 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 472 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 473 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 474 | flags := linkerProperties.Ldflags |
| 475 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 476 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 477 | } |
| 478 | return flags |
| 479 | } |
| 480 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 481 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 482 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 483 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 484 | var deps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 485 | var exportedDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 486 | var dynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 487 | var wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 488 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 489 | var versionScript bazel.LabelAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 490 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 491 | getLibs := func(baseLinkerProps *BaseLinkerProperties) []string { |
| 492 | libs := baseLinkerProps.Header_libs |
| 493 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 494 | libs = android.SortedUniqueStrings(libs) |
| 495 | return libs |
| 496 | } |
| 497 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 498 | for _, linkerProps := range module.linker.linkerProps() { |
| 499 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 500 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 501 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 502 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 503 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 504 | exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 505 | linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 506 | wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 507 | |
| 508 | if baseLinkerProps.Version_script != nil { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 509 | versionScript.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 510 | } |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 511 | |
| 512 | sharedLibs := baseLinkerProps.Shared_libs |
| 513 | dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 514 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 515 | break |
| 516 | } |
| 517 | } |
| 518 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 519 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { |
| 520 | for config, props := range configToProps { |
| 521 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 522 | libs := getLibs(baseLinkerProps) |
| 523 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
| 524 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 525 | deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, libs)) |
| 526 | exportedDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
| 527 | linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps)) |
| 528 | wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 529 | |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 530 | if baseLinkerProps.Version_script != nil { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 531 | versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 532 | } |
| 533 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 534 | sharedLibs := baseLinkerProps.Shared_libs |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 535 | dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 536 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 540 | return linkerAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 541 | deps: deps, |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 542 | exportedDeps: exportedDeps, |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 543 | dynamicDeps: dynamicDeps, |
| 544 | wholeArchiveDeps: wholeArchiveDeps, |
| 545 | linkopts: linkopts, |
| 546 | versionScript: versionScript, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 547 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 548 | } |
| 549 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 550 | // Relativize a list of root-relative paths with respect to the module's |
| 551 | // directory. |
| 552 | // |
| 553 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 554 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 555 | // module dir relative. This function makes a list of paths entirely module dir |
| 556 | // relative. |
| 557 | // |
| 558 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 559 | // module. |
| 560 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 561 | var relativePaths []string |
| 562 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 563 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 564 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 565 | if err != nil { |
| 566 | panic(err) |
| 567 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 568 | relativePaths = append(relativePaths, relativePath) |
| 569 | } |
| 570 | return relativePaths |
| 571 | } |
| 572 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 573 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 574 | libraryDecorator := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 575 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 576 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 577 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 578 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
| 579 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 580 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
| 581 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 582 | } |
| 583 | |
| 584 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 585 | // exported included directories of a module. |
| 586 | func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 587 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 588 | // relative, so they don't need to be relativized like include_dirs, which |
| 589 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 590 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 591 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 592 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 593 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 594 | getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string { |
| 595 | variantIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 596 | variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 597 | |
| 598 | // To avoid duplicate includes when base includes + arch includes are combined |
| 599 | // TODO: This doesn't take conflicts between arch and os includes into account |
| 600 | variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs) |
| 601 | return variantIncludeDirs |
| 602 | } |
| 603 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 604 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { |
| 605 | for config, props := range configToProps { |
| 606 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 607 | archVariantIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 608 | if len(archVariantIncludeDirs) > 0 { |
| 609 | includeDirsAttribute.SetSelectValue(axis, config, archVariantIncludeDirs) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 610 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 611 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 612 | } |
| 613 | } |
| 614 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 615 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 616 | } |