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" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 25 | |
| 26 | func init() { |
| 27 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 28 | } |
| 29 | |
| 30 | func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) { |
| 31 | ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator) |
| 32 | } |
| 33 | |
| 34 | // A naive deps mutator to add deps on all modules across all combinations of |
| 35 | // target props for cc modules. This is needed to make module -> bazel label |
| 36 | // resolution work in the bp2build mutator later. This is probably |
| 37 | // the wrong way to do it, but it works. |
| 38 | // |
| 39 | // TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this? |
| 40 | func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { |
| 41 | module, ok := ctx.Module().(*Module) |
| 42 | if !ok { |
| 43 | // Not a cc module |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | if !module.ConvertWithBp2build(ctx) { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | var allDeps []string |
| 52 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 53 | for _, p := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
| 54 | // base compiler props |
| 55 | if baseCompilerProps, ok := p.(*BaseCompilerProperties); ok { |
| 56 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 57 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | for _, p := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
| 62 | // arch specific compiler props |
| 63 | if baseCompilerProps, ok := p.(*BaseCompilerProperties); ok { |
| 64 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 65 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 66 | } |
| 67 | } |
| 68 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 69 | for _, p := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 70 | // arch specific linker props |
| 71 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 72 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 73 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 74 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 75 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 76 | } |
| 77 | } |
| 78 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 79 | for _, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 80 | // arch specific linker props |
| 81 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 82 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 83 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 84 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 85 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 89 | // Deps in the static: { .. } and shared: { .. } props of a cc_library. |
| 90 | if lib, ok := module.compiler.(*libraryDecorator); ok { |
| 91 | allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...) |
| 92 | allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...) |
| 93 | allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...) |
| 94 | allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 95 | |
| 96 | allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...) |
| 97 | allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...) |
| 98 | allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...) |
| 99 | allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
| 100 | } |
| 101 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 102 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 103 | } |
| 104 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 105 | type sharedAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 106 | copts bazel.StringListAttribute |
| 107 | srcs bazel.LabelListAttribute |
| 108 | staticDeps bazel.LabelListAttribute |
| 109 | dynamicDeps bazel.LabelListAttribute |
| 110 | wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
| 114 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) sharedAttributes { |
| 115 | lib, ok := module.compiler.(*libraryDecorator) |
| 116 | if !ok { |
| 117 | return sharedAttributes{} |
| 118 | } |
| 119 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 120 | copts := bazel.StringListAttribute{Value: lib.SharedProperties.Shared.Cflags} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 121 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 122 | srcs := bazel.LabelListAttribute{ |
| 123 | Value: android.BazelLabelForModuleSrc(ctx, lib.SharedProperties.Shared.Srcs)} |
| 124 | |
| 125 | staticDeps := bazel.LabelListAttribute{ |
| 126 | Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Static_libs)} |
| 127 | |
| 128 | dynamicDeps := bazel.LabelListAttribute{ |
| 129 | Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Shared_libs)} |
| 130 | |
| 131 | wholeArchiveDeps := bazel.LabelListAttribute{ |
| 132 | Value: android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs)} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 133 | |
| 134 | return sharedAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 135 | copts: copts, |
| 136 | srcs: srcs, |
| 137 | staticDeps: staticDeps, |
| 138 | dynamicDeps: dynamicDeps, |
| 139 | wholeArchiveDeps: wholeArchiveDeps, |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
| 143 | type staticAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 144 | copts bazel.StringListAttribute |
| 145 | srcs bazel.LabelListAttribute |
| 146 | staticDeps bazel.LabelListAttribute |
| 147 | dynamicDeps bazel.LabelListAttribute |
| 148 | wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
| 152 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticAttributes { |
| 153 | lib, ok := module.compiler.(*libraryDecorator) |
| 154 | if !ok { |
| 155 | return staticAttributes{} |
| 156 | } |
| 157 | |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 158 | copts := bazel.StringListAttribute{Value: lib.StaticProperties.Static.Cflags} |
| 159 | |
| 160 | srcs := bazel.LabelListAttribute{ |
| 161 | Value: android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs)} |
| 162 | |
| 163 | staticDeps := bazel.LabelListAttribute{ |
| 164 | Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Static_libs)} |
| 165 | |
| 166 | dynamicDeps := bazel.LabelListAttribute{ |
| 167 | Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Shared_libs)} |
| 168 | |
| 169 | wholeArchiveDeps := bazel.LabelListAttribute{ |
| 170 | Value: android.BazelLabelForModuleDeps(ctx, lib.StaticProperties.Static.Whole_static_libs)} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 171 | |
| 172 | return staticAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 173 | copts: copts, |
| 174 | srcs: srcs, |
| 175 | staticDeps: staticDeps, |
| 176 | dynamicDeps: dynamicDeps, |
| 177 | wholeArchiveDeps: wholeArchiveDeps, |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 181 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 182 | type compilerAttributes struct { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 183 | copts bazel.StringListAttribute |
| 184 | srcs bazel.LabelListAttribute |
| 185 | includes bazel.StringListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 186 | } |
| 187 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 188 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 189 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 190 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 191 | var copts bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 192 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 193 | // Creates the -I flags for a directory, while making the directory relative |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 194 | // to the exec root for Bazel to work. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 195 | includeFlags := func(dir string) []string { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 196 | // 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^] | 197 | moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir) |
| 198 | return []string{ |
| 199 | "-I" + moduleDirRootedPath, |
| 200 | // Include the bindir-rooted path (using make variable substitution). This most |
| 201 | // closely matches Bazel's native include path handling, which allows for dependency |
| 202 | // on generated headers in these directories. |
| 203 | // TODO(b/188084383): Handle local include directories in Bazel. |
| 204 | "-I$(BINDIR)/" + moduleDirRootedPath, |
| 205 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 208 | // Parse the list of module-relative include directories (-I). |
| 209 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 210 | // include_dirs are root-relative, not module-relative. |
| 211 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 212 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 213 | } |
| 214 | |
| 215 | // Parse the list of copts. |
| 216 | parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string { |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 217 | var copts []string |
Jingwen Chen | 75be1ca | 2021-05-12 05:04:58 +0000 | [diff] [blame] | 218 | for _, flag := range append(baseCompilerProps.Cflags, baseCompilerProps.Cppflags...) { |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 219 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 220 | // Bazel's copts, split them up to be compatible with the |
| 221 | // no_copts_tokenization feature. |
| 222 | copts = append(copts, strings.Split(flag, " ")...) |
| 223 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 224 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 225 | copts = append(copts, includeFlags(dir)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 226 | } |
| 227 | return copts |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 228 | } |
| 229 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 230 | // baseSrcs contain the list of src files that are used for every configuration. |
| 231 | var baseSrcs []string |
| 232 | // baseExcludeSrcs contain the list of src files that are excluded for every configuration. |
| 233 | var baseExcludeSrcs []string |
| 234 | // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the |
| 235 | // arch or os specific srcs later. |
| 236 | var baseSrcsLabelList bazel.LabelList |
| 237 | |
| 238 | // Parse srcs from an arch or OS's props value, taking the base srcs and |
| 239 | // exclude srcs into account. |
| 240 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
| 241 | // Combine the base srcs and arch-specific srcs |
| 242 | allSrcs := append(baseSrcs, baseCompilerProps.Srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 243 | // Add srcs-like dependencies such as generated files. |
| 244 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 245 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 246 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
| 247 | |
| 248 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 249 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 250 | // Combine the base exclude_srcs and configuration-specific exclude_srcs |
| 251 | allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 252 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs) |
| 253 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 256 | for _, props := range module.compiler.compilerProps() { |
| 257 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 258 | srcs.Value = parseSrcs(baseCompilerProps) |
| 259 | copts.Value = parseCopts(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 260 | |
| 261 | // Used for arch-specific srcs later. |
| 262 | baseSrcs = baseCompilerProps.Srcs |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 263 | baseSrcsLabelList = parseSrcs(baseCompilerProps) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 264 | baseExcludeSrcs = baseCompilerProps.Exclude_srcs |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 265 | break |
| 266 | } |
| 267 | } |
| 268 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 269 | // Handle include_build_directory prop. If the property is true, then the |
| 270 | // target has access to all headers recursively in the package, and has |
| 271 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 272 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 273 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 274 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame^] | 275 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 278 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 279 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 280 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 281 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 282 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 283 | srcsList := parseSrcs(baseCompilerProps) |
| 284 | srcs.SetValueForArch(arch.Name, srcsList) |
| 285 | // The base srcs value should not contain any arch-specific excludes. |
| 286 | srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes}) |
| 287 | } |
| 288 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 289 | copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 290 | } |
| 291 | } |
| 292 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 293 | // After going through all archs, delete the duplicate files in the arch |
| 294 | // values that are already in the base srcs.Value. |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 295 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 296 | if _, ok := props.(*BaseCompilerProperties); ok { |
| 297 | srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value)) |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | // Now that the srcs.Value list is finalized, compare it with the original |
| 302 | // list, and put the difference into the default condition for the arch |
| 303 | // select. |
| 304 | defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value) |
| 305 | // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used. |
| 306 | srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs) |
| 307 | |
| 308 | // Handle OS specific props. |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 309 | for os, props := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 310 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 311 | srcsList := parseSrcs(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 312 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
| 313 | srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 314 | copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 318 | return compilerAttributes{ |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 319 | srcs: srcs, |
| 320 | copts: copts, |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | // Convenience struct to hold all attributes parsed from linker properties. |
| 325 | type linkerAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 326 | deps bazel.LabelListAttribute |
| 327 | dynamicDeps bazel.LabelListAttribute |
| 328 | wholeArchiveDeps bazel.LabelListAttribute |
| 329 | linkopts bazel.StringListAttribute |
| 330 | versionScript bazel.LabelAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 333 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 334 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 335 | flags := linkerProperties.Ldflags |
| 336 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 337 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 338 | } |
| 339 | return flags |
| 340 | } |
| 341 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 342 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 343 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 344 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 345 | var deps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 346 | var dynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 347 | var wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 348 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 349 | var versionScript bazel.LabelAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 350 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 351 | for _, linkerProps := range module.linker.linkerProps() { |
| 352 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
| 353 | libs := baseLinkerProps.Header_libs |
| 354 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 355 | libs = append(libs, baseLinkerProps.Static_libs...) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 356 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 357 | libs = android.SortedUniqueStrings(libs) |
| 358 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 359 | linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 360 | wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 361 | |
| 362 | if baseLinkerProps.Version_script != nil { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 363 | versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 364 | } |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 365 | |
| 366 | sharedLibs := baseLinkerProps.Shared_libs |
| 367 | dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 368 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 369 | break |
| 370 | } |
| 371 | } |
| 372 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 373 | for arch, p := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 374 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 375 | libs := baseLinkerProps.Header_libs |
| 376 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 377 | libs = append(libs, baseLinkerProps.Static_libs...) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 378 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 379 | libs = android.SortedUniqueStrings(libs) |
| 380 | deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 381 | linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 382 | wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 383 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 384 | if baseLinkerProps.Version_script != nil { |
| 385 | versionScript.SetValueForArch(arch.Name, |
| 386 | android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
| 387 | } |
Rupert Shuttleworth | 3b413d3 | 2021-05-10 18:41:51 -0400 | [diff] [blame] | 388 | |
| 389 | sharedLibs := baseLinkerProps.Shared_libs |
| 390 | dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 391 | } |
| 392 | } |
| 393 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 394 | for os, p := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 395 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 396 | libs := baseLinkerProps.Header_libs |
| 397 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 398 | libs = append(libs, baseLinkerProps.Static_libs...) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 399 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 400 | libs = android.SortedUniqueStrings(libs) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 401 | wholeArchiveDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 402 | deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 403 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 404 | linkopts.SetValueForOS(os.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 405 | |
| 406 | sharedLibs := baseLinkerProps.Shared_libs |
| 407 | dynamicDeps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 411 | return linkerAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 412 | deps: deps, |
| 413 | dynamicDeps: dynamicDeps, |
| 414 | wholeArchiveDeps: wholeArchiveDeps, |
| 415 | linkopts: linkopts, |
| 416 | versionScript: versionScript, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 417 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 420 | // Relativize a list of root-relative paths with respect to the module's |
| 421 | // directory. |
| 422 | // |
| 423 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 424 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 425 | // module dir relative. This function makes a list of paths entirely module dir |
| 426 | // relative. |
| 427 | // |
| 428 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 429 | // module. |
| 430 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 431 | var relativePaths []string |
| 432 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 433 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 434 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 435 | if err != nil { |
| 436 | panic(err) |
| 437 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 438 | relativePaths = append(relativePaths, relativePath) |
| 439 | } |
| 440 | return relativePaths |
| 441 | } |
| 442 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 443 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 444 | // exported included directories of a module. |
| 445 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 446 | libraryDecorator := module.linker.(*libraryDecorator) |
| 447 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 448 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 449 | // relative, so they don't need to be relativized like include_dirs, which |
| 450 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 451 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 452 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 453 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 454 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 455 | for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 456 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 457 | archIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 458 | archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 459 | |
| 460 | // To avoid duplicate includes when base includes + arch includes are combined |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 461 | // FIXME: This doesn't take conflicts between arch and os includes into account |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 462 | archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs) |
| 463 | |
| 464 | if len(archIncludeDirs) > 0 { |
| 465 | includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs) |
| 466 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 467 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 468 | } |
| 469 | |
Lukacs T. Berki | 5f51839 | 2021-05-17 11:44:58 +0200 | [diff] [blame] | 470 | for os, props := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) { |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 471 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 472 | osIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 473 | osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 474 | |
| 475 | // To avoid duplicate includes when base includes + os includes are combined |
| 476 | // FIXME: This doesn't take conflicts between arch and os includes into account |
| 477 | osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs) |
| 478 | |
| 479 | if len(osIncludeDirs) > 0 { |
| 480 | includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs) |
| 481 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 485 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 486 | } |