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