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. |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 93 | if lib.static() { |
| 94 | allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
| 95 | } else if lib.shared() { |
| 96 | allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 97 | } |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 98 | |
| 99 | // Deps in the target/arch nested static: { .. } and shared: { .. } props of a cc_library. |
| 100 | // target: { <target>: shared: { ... } } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 101 | for _, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 102 | for _, props := range configToProps { |
| 103 | if p, ok := props.(*SharedProperties); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 104 | allDeps = appendDeps(allDeps, p.Shared) |
| 105 | } |
| 106 | } |
| 107 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 108 | |
| 109 | for _, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 110 | for _, props := range configToProps { |
| 111 | if p, ok := props.(*StaticProperties); ok { |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 112 | allDeps = appendDeps(allDeps, p.Static) |
| 113 | } |
| 114 | } |
| 115 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 118 | // product variables only support a limited set of fields, this is the full list of field names |
| 119 | // related to cc module dependency management that are supported. |
| 120 | productVariableDepFields := [4]string{ |
| 121 | "Shared_libs", |
| 122 | "Static_libs", |
| 123 | "Exclude_static_libs", |
| 124 | "Whole_static_libs", |
| 125 | } |
| 126 | |
| 127 | productVariableProps := android.ProductVariableProperties(ctx) |
| 128 | for _, name := range productVariableDepFields { |
| 129 | props, exists := productVariableProps[name] |
| 130 | if !exists { |
| 131 | continue |
| 132 | } |
| 133 | for _, prop := range props { |
| 134 | if p, ok := prop.Property.([]string); !ok { |
| 135 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 136 | } else { |
| 137 | allDeps = append(allDeps, p...) |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 142 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 143 | } |
| 144 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 145 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 146 | // 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] | 147 | type staticOrSharedAttributes struct { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 148 | Srcs bazel.LabelListAttribute |
| 149 | Srcs_c bazel.LabelListAttribute |
| 150 | Srcs_as bazel.LabelListAttribute |
| 151 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 152 | |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 153 | Static_deps bazel.LabelListAttribute |
| 154 | Dynamic_deps bazel.LabelListAttribute |
| 155 | Whole_archive_deps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 156 | } |
| 157 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 158 | func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) { |
| 159 | // Branch srcs into three language-specific groups. |
| 160 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 161 | // know the language of these sources until the genrule is executed. |
| 162 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 163 | isCSrcOrFilegroup := func(s string) bool { |
| 164 | return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs") |
| 165 | } |
| 166 | |
| 167 | isAsmSrcOrFilegroup := func(s string) bool { |
| 168 | return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs") |
| 169 | } |
| 170 | |
| 171 | // Check that a module is a filegroup type named <label>. |
| 172 | isFilegroupNamed := func(m android.Module, fullLabel string) bool { |
| 173 | if ctx.OtherModuleType(m) != "filegroup" { |
| 174 | return false |
| 175 | } |
| 176 | labelParts := strings.Split(fullLabel, ":") |
| 177 | if len(labelParts) > 2 { |
| 178 | // There should not be more than one colon in a label. |
| 179 | panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel)) |
| 180 | } else { |
| 181 | return m.Name() == labelParts[len(labelParts)-1] |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | // Convert the filegroup dependencies into the extension-specific filegroups |
| 186 | // filtered in the filegroup.bzl macro. |
| 187 | cppFilegroup := func(label string) string { |
| 188 | ctx.VisitDirectDeps(func(m android.Module) { |
| 189 | if isFilegroupNamed(m, label) { |
| 190 | label = label + "_cpp_srcs" |
| 191 | return |
| 192 | } |
| 193 | }) |
| 194 | return label |
| 195 | } |
| 196 | cFilegroup := func(label string) string { |
| 197 | ctx.VisitDirectDeps(func(m android.Module) { |
| 198 | if isFilegroupNamed(m, label) { |
| 199 | label = label + "_c_srcs" |
| 200 | return |
| 201 | } |
| 202 | }) |
| 203 | return label |
| 204 | } |
| 205 | asFilegroup := func(label string) string { |
| 206 | ctx.VisitDirectDeps(func(m android.Module) { |
| 207 | if isFilegroupNamed(m, label) { |
| 208 | label = label + "_as_srcs" |
| 209 | return |
| 210 | } |
| 211 | }) |
| 212 | return label |
| 213 | } |
| 214 | |
| 215 | cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup) |
| 216 | cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup) |
| 217 | |
| 218 | asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup) |
| 219 | asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup) |
| 220 | |
| 221 | cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup) |
| 222 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs) |
| 223 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs) |
| 224 | return |
| 225 | } |
| 226 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 227 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 228 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 229 | lib, ok := module.compiler.(*libraryDecorator) |
| 230 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 231 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 234 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 238 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 239 | lib, ok := module.compiler.(*libraryDecorator) |
| 240 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 241 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 244 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 245 | } |
| 246 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 247 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
| 248 | var props StaticOrSharedProperties |
| 249 | if isStatic { |
| 250 | props = lib.StaticProperties.Static |
| 251 | } else { |
| 252 | props = lib.SharedProperties.Shared |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 253 | } |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 254 | |
| 255 | attrs := staticOrSharedAttributes{ |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 256 | Copts: bazel.StringListAttribute{Value: props.Cflags}, |
| 257 | Srcs: bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, props.Srcs)), |
| 258 | Static_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, props.Static_libs)), |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 259 | Dynamic_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, append(props.Shared_libs, props.System_shared_libs...))), |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 260 | Whole_archive_deps: bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)), |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 263 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 264 | attrs.Copts.SetSelectValue(axis, config, props.Cflags) |
| 265 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
| 266 | attrs.Static_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Static_libs)) |
| 267 | attrs.Dynamic_deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, props.Shared_libs)) |
| 268 | attrs.Whole_archive_deps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDeps(ctx, props.Whole_static_libs)) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | if isStatic { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 272 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 273 | for config, props := range configToProps { |
| 274 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 275 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | } else { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 280 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 281 | for config, props := range configToProps { |
| 282 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 283 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 284 | } |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 289 | cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 290 | attrs.Srcs = cppSrcs |
| 291 | attrs.Srcs_c = cSrcs |
| 292 | attrs.Srcs_as = asSrcs |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 293 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 294 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 297 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 298 | type prebuiltAttributes struct { |
| 299 | Src bazel.LabelAttribute |
| 300 | } |
| 301 | |
| 302 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes { |
| 303 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 304 | prebuiltLinker := prebuiltLibraryLinker.prebuiltLinker |
| 305 | |
| 306 | var srcLabelAttribute bazel.LabelAttribute |
| 307 | |
| 308 | if len(prebuiltLinker.properties.Srcs) > 1 { |
| 309 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file\n") |
| 310 | } |
| 311 | |
| 312 | if len(prebuiltLinker.properties.Srcs) == 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 313 | srcLabelAttribute.SetValue(android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinker.properties.Srcs[0])) |
| 314 | } |
| 315 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) { |
| 316 | for config, props := range configToProps { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 317 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 318 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 319 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config) |
| 320 | continue |
| 321 | } else if len(prebuiltLinkerProperties.Srcs) == 0 { |
| 322 | continue |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 323 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 324 | src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]) |
| 325 | srcLabelAttribute.SetSelectValue(axis, config, src) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 330 | return prebuiltAttributes{ |
| 331 | Src: srcLabelAttribute, |
| 332 | } |
| 333 | } |
| 334 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 335 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 336 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 337 | // Options for all languages |
| 338 | copts bazel.StringListAttribute |
| 339 | // Assembly options and sources |
| 340 | asFlags bazel.StringListAttribute |
| 341 | asSrcs bazel.LabelListAttribute |
| 342 | // C options and sources |
| 343 | conlyFlags bazel.StringListAttribute |
| 344 | cSrcs bazel.LabelListAttribute |
| 345 | // C++ options and sources |
| 346 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 347 | srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 350 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 351 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 352 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 353 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 354 | var asFlags bazel.StringListAttribute |
| 355 | var conlyFlags bazel.StringListAttribute |
| 356 | var cppFlags bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 357 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 358 | // Creates the -I flags for a directory, while making the directory relative |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 359 | // to the exec root for Bazel to work. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 360 | includeFlags := func(dir string) []string { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 361 | // 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] | 362 | moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir) |
| 363 | return []string{ |
| 364 | "-I" + moduleDirRootedPath, |
| 365 | // Include the bindir-rooted path (using make variable substitution). This most |
| 366 | // closely matches Bazel's native include path handling, which allows for dependency |
| 367 | // on generated headers in these directories. |
| 368 | // TODO(b/188084383): Handle local include directories in Bazel. |
| 369 | "-I$(BINDIR)/" + moduleDirRootedPath, |
| 370 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 373 | // Parse the list of module-relative include directories (-I). |
| 374 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 375 | // include_dirs are root-relative, not module-relative. |
| 376 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 377 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 378 | } |
| 379 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 380 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 381 | var result []string |
| 382 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 383 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 384 | // Bazel's copts, split them up to be compatible with the |
| 385 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 386 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 387 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 388 | return result |
| 389 | } |
| 390 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 391 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 392 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 393 | // Add srcs-like dependencies such as generated files. |
| 394 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 395 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 396 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 397 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 398 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 399 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 400 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 403 | for _, props := range module.compiler.compilerProps() { |
| 404 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 405 | srcs.SetValue(parseSrcs(baseCompilerProps)) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 406 | copts.Value = parseCommandLineFlags(baseCompilerProps.Cflags) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 407 | asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags) |
| 408 | conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags) |
| 409 | cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 410 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 411 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
| 412 | copts.Value = append(copts.Value, includeFlags(dir)...) |
| 413 | asFlags.Value = append(asFlags.Value, includeFlags(dir)...) |
| 414 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 415 | break |
| 416 | } |
| 417 | } |
| 418 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 419 | // Handle include_build_directory prop. If the property is true, then the |
| 420 | // target has access to all headers recursively in the package, and has |
| 421 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 422 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 423 | copts.Value = append(copts.Value, includeFlags(".")...) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 424 | asFlags.Value = append(asFlags.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 425 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 426 | copts.Value = append(copts.Value, includeFlags(".")...) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 427 | asFlags.Value = append(asFlags.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 430 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 431 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 432 | for axis, configToProps := range archVariantCompilerProps { |
| 433 | for config, props := range configToProps { |
| 434 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
| 435 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 436 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 437 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 438 | srcsList := parseSrcs(baseCompilerProps) |
| 439 | srcs.SetSelectValue(axis, config, srcsList) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 442 | archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags) |
| 443 | archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags) |
| 444 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
| 445 | archVariantCopts = append(archVariantCopts, includeFlags(dir)...) |
| 446 | archVariantAsflags = append(archVariantAsflags, includeFlags(dir)...) |
| 447 | } |
| 448 | |
| 449 | copts.SetSelectValue(axis, config, archVariantCopts) |
| 450 | asFlags.SetSelectValue(axis, config, archVariantAsflags) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 451 | conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 452 | cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 453 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 457 | srcs.ResolveExcludes() |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 458 | |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 459 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
| 460 | "Cflags": &copts, |
| 461 | "Asflags": &asFlags, |
| 462 | "CppFlags": &cppFlags, |
| 463 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 464 | productVariableProps := android.ProductVariableProperties(ctx) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 465 | for propName, attr := range productVarPropNameToAttribute { |
| 466 | if props, exists := productVariableProps[propName]; exists { |
| 467 | for _, prop := range props { |
| 468 | flags, ok := prop.Property.([]string) |
| 469 | if !ok { |
| 470 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 471 | } |
| 472 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 473 | attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 474 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 475 | } |
| 476 | } |
| 477 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 478 | srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs) |
| 479 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 480 | return compilerAttributes{ |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 481 | copts: copts, |
| 482 | srcs: srcs, |
| 483 | asFlags: asFlags, |
| 484 | asSrcs: asSrcs, |
| 485 | cSrcs: cSrcs, |
| 486 | conlyFlags: conlyFlags, |
| 487 | cppFlags: cppFlags, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | |
| 491 | // Convenience struct to hold all attributes parsed from linker properties. |
| 492 | type linkerAttributes struct { |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 493 | deps bazel.LabelListAttribute |
| 494 | dynamicDeps bazel.LabelListAttribute |
| 495 | wholeArchiveDeps bazel.LabelListAttribute |
| 496 | exportedDeps bazel.LabelListAttribute |
| 497 | useLibcrt bazel.BoolAttribute |
| 498 | linkopts bazel.StringListAttribute |
| 499 | versionScript bazel.LabelAttribute |
| 500 | stripKeepSymbols bazel.BoolAttribute |
| 501 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 502 | stripKeepSymbolsList bazel.StringListAttribute |
| 503 | stripAll bazel.BoolAttribute |
| 504 | stripNone bazel.BoolAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 505 | } |
| 506 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 507 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 508 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 509 | flags := linkerProperties.Ldflags |
| 510 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 511 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 512 | } |
| 513 | return flags |
| 514 | } |
| 515 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 516 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 517 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 518 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 519 | var headerDeps bazel.LabelListAttribute |
| 520 | var staticDeps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 521 | var exportedDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 522 | var dynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 523 | var wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 524 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 525 | var versionScript bazel.LabelAttribute |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 526 | var useLibcrt bazel.BoolAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 527 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 528 | var stripKeepSymbols bazel.BoolAttribute |
| 529 | var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 530 | var stripKeepSymbolsList bazel.StringListAttribute |
| 531 | var stripAll bazel.BoolAttribute |
| 532 | var stripNone bazel.BoolAttribute |
| 533 | |
| 534 | if libraryDecorator, ok := module.linker.(*libraryDecorator); ok { |
| 535 | stripProperties := libraryDecorator.stripper.StripProperties |
| 536 | stripKeepSymbols.Value = stripProperties.Strip.Keep_symbols |
| 537 | stripKeepSymbolsList.Value = stripProperties.Strip.Keep_symbols_list |
| 538 | stripKeepSymbolsAndDebugFrame.Value = stripProperties.Strip.Keep_symbols_and_debug_frame |
| 539 | stripAll.Value = stripProperties.Strip.All |
| 540 | stripNone.Value = stripProperties.Strip.None |
| 541 | } |
| 542 | |
| 543 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) { |
| 544 | for config, props := range configToProps { |
| 545 | if stripProperties, ok := props.(*StripProperties); ok { |
| 546 | stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 547 | stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 548 | stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 549 | stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 550 | stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
| 551 | } |
| 552 | } |
| 553 | } |
| 554 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 555 | for _, linkerProps := range module.linker.linkerProps() { |
| 556 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 557 | // Excludes to parallel Soong: |
| 558 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
| 559 | staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) |
| 560 | staticDeps.Value = android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs) |
| 561 | wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 562 | wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs)) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 563 | // TODO(b/186024507): Handle system_shared_libs as its own attribute, so that the appropriate default |
| 564 | // may be supported. |
| 565 | sharedLibs := android.FirstUniqueStrings(append(baseLinkerProps.Shared_libs, baseLinkerProps.System_shared_libs...)) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 566 | dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 567 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 568 | headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs) |
| 569 | headerDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, headerLibs)) |
| 570 | // TODO(b/188796939): also handle export_static_lib_headers, export_shared_lib_headers, |
| 571 | // export_generated_headers |
| 572 | exportedLibs := android.FirstUniqueStrings(baseLinkerProps.Export_header_lib_headers) |
| 573 | exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
| 574 | |
| 575 | linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 576 | if baseLinkerProps.Version_script != nil { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 577 | versionScript.SetValue(android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 578 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 579 | useLibcrt.Value = baseLinkerProps.libCrt() |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 580 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 581 | break |
| 582 | } |
| 583 | } |
| 584 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 585 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { |
| 586 | for config, props := range configToProps { |
| 587 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 588 | staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) |
| 589 | staticDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs)) |
| 590 | wholeArchiveLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 591 | wholeArchiveDeps.SetSelectValue(axis, config, android.BazelLabelForModuleWholeDepsExcludes(ctx, wholeArchiveLibs, baseLinkerProps.Exclude_static_libs)) |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame^] | 592 | sharedLibs := android.FirstUniqueStrings(append(baseLinkerProps.Shared_libs, baseLinkerProps.System_shared_libs...)) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 593 | dynamicDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs)) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 594 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 595 | headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs) |
| 596 | headerDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, headerLibs)) |
| 597 | exportedLibs := android.FirstUniqueStrings(baseLinkerProps.Export_header_lib_headers) |
| 598 | exportedDeps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
| 599 | |
| 600 | linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps)) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 601 | if baseLinkerProps.Version_script != nil { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 602 | versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 603 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 604 | useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt()) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 605 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 606 | } |
| 607 | } |
| 608 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 609 | type productVarDep struct { |
| 610 | // the name of the corresponding excludes field, if one exists |
| 611 | excludesField string |
| 612 | // reference to the bazel attribute that should be set for the given product variable config |
| 613 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 614 | |
| 615 | depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | productVarToDepFields := map[string]productVarDep{ |
| 619 | // product variables do not support exclude_shared_libs |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 620 | "Shared_libs": productVarDep{attribute: &dynamicDeps, depResolutionFunc: android.BazelLabelForModuleDepsExcludes}, |
| 621 | "Static_libs": productVarDep{"Exclude_static_libs", &staticDeps, android.BazelLabelForModuleDepsExcludes}, |
| 622 | "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, android.BazelLabelForModuleWholeDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | productVariableProps := android.ProductVariableProperties(ctx) |
| 626 | for name, dep := range productVarToDepFields { |
| 627 | props, exists := productVariableProps[name] |
| 628 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 629 | // if neither an include or excludes property exists, then skip it |
| 630 | if !exists && !excludesExists { |
| 631 | continue |
| 632 | } |
| 633 | // collect all the configurations that an include or exclude property exists for. |
| 634 | // we want to iterate all configurations rather than either the include or exclude because for a |
| 635 | // particular configuration we may have only and include or only an exclude to handle |
| 636 | configs := make(map[string]bool, len(props)+len(excludeProps)) |
| 637 | for config := range props { |
| 638 | configs[config] = true |
| 639 | } |
| 640 | for config := range excludeProps { |
| 641 | configs[config] = true |
| 642 | } |
| 643 | |
| 644 | for config := range configs { |
| 645 | prop, includesExists := props[config] |
| 646 | excludesProp, excludesExists := excludeProps[config] |
| 647 | var includes, excludes []string |
| 648 | var ok bool |
| 649 | // if there was no includes/excludes property, casting fails and that's expected |
| 650 | if includes, ok = prop.Property.([]string); includesExists && !ok { |
| 651 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 652 | } |
| 653 | if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok { |
| 654 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 655 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 656 | |
| 657 | dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes)) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | staticDeps.ResolveExcludes() |
| 662 | dynamicDeps.ResolveExcludes() |
| 663 | wholeArchiveDeps.ResolveExcludes() |
| 664 | |
| 665 | headerDeps.Append(staticDeps) |
| 666 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 667 | return linkerAttributes{ |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 668 | deps: headerDeps, |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 669 | exportedDeps: exportedDeps, |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 670 | dynamicDeps: dynamicDeps, |
| 671 | wholeArchiveDeps: wholeArchiveDeps, |
| 672 | linkopts: linkopts, |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 673 | useLibcrt: useLibcrt, |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 674 | versionScript: versionScript, |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 675 | |
| 676 | // Strip properties |
| 677 | stripKeepSymbols: stripKeepSymbols, |
| 678 | stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame, |
| 679 | stripKeepSymbolsList: stripKeepSymbolsList, |
| 680 | stripAll: stripAll, |
| 681 | stripNone: stripNone, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 682 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 683 | } |
| 684 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 685 | // Relativize a list of root-relative paths with respect to the module's |
| 686 | // directory. |
| 687 | // |
| 688 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 689 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 690 | // module dir relative. This function makes a list of paths entirely module dir |
| 691 | // relative. |
| 692 | // |
| 693 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 694 | // module. |
| 695 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 696 | var relativePaths []string |
| 697 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 698 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 699 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 700 | if err != nil { |
| 701 | panic(err) |
| 702 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 703 | relativePaths = append(relativePaths, relativePath) |
| 704 | } |
| 705 | return relativePaths |
| 706 | } |
| 707 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 708 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 709 | libraryDecorator := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 710 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 711 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 712 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 713 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
| 714 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 715 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
| 716 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 717 | } |
| 718 | |
| 719 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 720 | // exported included directories of a module. |
| 721 | func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) bazel.StringListAttribute { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 722 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 723 | // relative, so they don't need to be relativized like include_dirs, which |
| 724 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 725 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 726 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 727 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 728 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 729 | getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string { |
| 730 | variantIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 731 | variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 732 | |
| 733 | // To avoid duplicate includes when base includes + arch includes are combined |
| 734 | // TODO: This doesn't take conflicts between arch and os includes into account |
| 735 | variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs) |
| 736 | return variantIncludeDirs |
| 737 | } |
| 738 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 739 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { |
| 740 | for config, props := range configToProps { |
| 741 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 742 | archVariantIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 743 | if len(archVariantIncludeDirs) > 0 { |
| 744 | includeDirsAttribute.SetSelectValue(axis, config, archVariantIncludeDirs) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 745 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 746 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 750 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 751 | } |