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 ( |
| 17 | "android/soong/android" |
| 18 | "android/soong/bazel" |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 19 | "path/filepath" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 23 | |
| 24 | func init() { |
| 25 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 26 | } |
| 27 | |
| 28 | func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) { |
| 29 | ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator) |
| 30 | } |
| 31 | |
| 32 | // A naive deps mutator to add deps on all modules across all combinations of |
| 33 | // target props for cc modules. This is needed to make module -> bazel label |
| 34 | // resolution work in the bp2build mutator later. This is probably |
| 35 | // the wrong way to do it, but it works. |
| 36 | // |
| 37 | // TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this? |
| 38 | func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { |
| 39 | module, ok := ctx.Module().(*Module) |
| 40 | if !ok { |
| 41 | // Not a cc module |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | if !module.ConvertWithBp2build(ctx) { |
| 46 | return |
| 47 | } |
| 48 | |
| 49 | var allDeps []string |
| 50 | |
| 51 | for _, p := range module.GetTargetProperties(&BaseLinkerProperties{}) { |
| 52 | // arch specific linker props |
| 53 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 54 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 55 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 56 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 57 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | for _, p := range module.GetArchProperties(&BaseLinkerProperties{}) { |
| 62 | // arch specific linker props |
| 63 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 64 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 65 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 66 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 67 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 71 | // Deps in the static: { .. } and shared: { .. } props of a cc_library. |
| 72 | if lib, ok := module.compiler.(*libraryDecorator); ok { |
| 73 | allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...) |
| 74 | allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...) |
| 75 | allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...) |
| 76 | allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 77 | |
| 78 | allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...) |
| 79 | allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...) |
| 80 | allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...) |
| 81 | allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
| 82 | } |
| 83 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 84 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 85 | } |
| 86 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 87 | type sharedAttributes struct { |
| 88 | staticDeps bazel.LabelListAttribute |
| 89 | } |
| 90 | |
| 91 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
| 92 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) sharedAttributes { |
| 93 | lib, ok := module.compiler.(*libraryDecorator) |
| 94 | if !ok { |
| 95 | return sharedAttributes{} |
| 96 | } |
| 97 | |
| 98 | var staticDeps bazel.LabelListAttribute |
| 99 | |
| 100 | staticDeps.Value = android.BazelLabelForModuleDeps(ctx, lib.SharedProperties.Shared.Whole_static_libs) |
| 101 | |
| 102 | return sharedAttributes{ |
| 103 | staticDeps: staticDeps, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | type staticAttributes struct { |
| 108 | srcs bazel.LabelListAttribute |
| 109 | } |
| 110 | |
| 111 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
| 112 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticAttributes { |
| 113 | lib, ok := module.compiler.(*libraryDecorator) |
| 114 | if !ok { |
| 115 | return staticAttributes{} |
| 116 | } |
| 117 | |
| 118 | var srcs bazel.LabelListAttribute |
| 119 | srcs.Value = android.BazelLabelForModuleSrc(ctx, lib.StaticProperties.Static.Srcs) |
| 120 | |
| 121 | return staticAttributes{ |
| 122 | srcs: srcs, |
| 123 | } |
| 124 | } |
| 125 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 126 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 127 | type compilerAttributes struct { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 128 | copts bazel.StringListAttribute |
| 129 | srcs bazel.LabelListAttribute |
| 130 | includes bazel.StringListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 133 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 134 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 135 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 136 | var copts bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 137 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 138 | // Creates the -I flag for a directory, while making the directory relative |
| 139 | // to the exec root for Bazel to work. |
| 140 | includeFlag := func(dir string) string { |
| 141 | // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements. |
| 142 | return "-I" + filepath.Join(ctx.ModuleDir(), dir) |
| 143 | } |
| 144 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 145 | // Parse the list of module-relative include directories (-I). |
| 146 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 147 | // include_dirs are root-relative, not module-relative. |
| 148 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 149 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 150 | } |
| 151 | |
| 152 | // Parse the list of copts. |
| 153 | parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 154 | copts := append([]string{}, baseCompilerProps.Cflags...) |
| 155 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
| 156 | copts = append(copts, includeFlag(dir)) |
| 157 | } |
| 158 | return copts |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 159 | } |
| 160 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 161 | // baseSrcs contain the list of src files that are used for every configuration. |
| 162 | var baseSrcs []string |
| 163 | // baseExcludeSrcs contain the list of src files that are excluded for every configuration. |
| 164 | var baseExcludeSrcs []string |
| 165 | // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the |
| 166 | // arch or os specific srcs later. |
| 167 | var baseSrcsLabelList bazel.LabelList |
| 168 | |
| 169 | // Parse srcs from an arch or OS's props value, taking the base srcs and |
| 170 | // exclude srcs into account. |
| 171 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
| 172 | // Combine the base srcs and arch-specific srcs |
| 173 | allSrcs := append(baseSrcs, baseCompilerProps.Srcs...) |
| 174 | // Combine the base exclude_srcs and configuration-specific exclude_srcs |
| 175 | allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...) |
| 176 | return android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs) |
| 177 | } |
| 178 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 179 | for _, props := range module.compiler.compilerProps() { |
| 180 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 181 | srcs.Value = parseSrcs(baseCompilerProps) |
| 182 | copts.Value = parseCopts(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 183 | |
| 184 | // Used for arch-specific srcs later. |
| 185 | baseSrcs = baseCompilerProps.Srcs |
| 186 | baseExcludeSrcs = baseCompilerProps.Exclude_srcs |
| 187 | baseSrcsLabelList = parseSrcs(baseCompilerProps) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 188 | break |
| 189 | } |
| 190 | } |
| 191 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 192 | // Handle include_build_directory prop. If the property is true, then the |
| 193 | // target has access to all headers recursively in the package, and has |
| 194 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 195 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
| 196 | copts.Value = append(copts.Value, includeFlag(".")) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 197 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
| 198 | copts.Value = append(copts.Value, includeFlag(".")) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 201 | for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) { |
| 202 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 203 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 204 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 205 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 206 | srcsList := parseSrcs(baseCompilerProps) |
| 207 | srcs.SetValueForArch(arch.Name, srcsList) |
| 208 | // The base srcs value should not contain any arch-specific excludes. |
| 209 | srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes}) |
| 210 | } |
| 211 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 212 | copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 213 | } |
| 214 | } |
| 215 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 216 | // After going through all archs, delete the duplicate files in the arch |
| 217 | // values that are already in the base srcs.Value. |
| 218 | for arch, props := range module.GetArchProperties(&BaseCompilerProperties{}) { |
| 219 | if _, ok := props.(*BaseCompilerProperties); ok { |
| 220 | srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value)) |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | // Now that the srcs.Value list is finalized, compare it with the original |
| 225 | // list, and put the difference into the default condition for the arch |
| 226 | // select. |
| 227 | defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value) |
| 228 | // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used. |
| 229 | srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs) |
| 230 | |
| 231 | // Handle OS specific props. |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 232 | for os, props := range module.GetTargetProperties(&BaseCompilerProperties{}) { |
| 233 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 234 | srcsList := parseSrcs(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 235 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
| 236 | srcs.SetValueForOS(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 237 | copts.SetValueForOS(os.Name, parseCopts(baseCompilerProps)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 238 | } |
| 239 | } |
| 240 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 241 | return compilerAttributes{ |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 242 | srcs: srcs, |
| 243 | copts: copts, |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // Convenience struct to hold all attributes parsed from linker properties. |
| 248 | type linkerAttributes struct { |
| 249 | deps bazel.LabelListAttribute |
| 250 | linkopts bazel.StringListAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 253 | // bp2BuildParseLinkerProps creates a label list attribute containing the header library deps of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 254 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 255 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 256 | var deps bazel.LabelListAttribute |
| 257 | var linkopts bazel.StringListAttribute |
| 258 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 259 | for _, linkerProps := range module.linker.linkerProps() { |
| 260 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
| 261 | libs := baseLinkerProps.Header_libs |
| 262 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 263 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 264 | libs = append(libs, baseLinkerProps.Whole_static_libs...) |
| 265 | libs = android.SortedUniqueStrings(libs) |
| 266 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 267 | linkopts.Value = baseLinkerProps.Ldflags |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 268 | break |
| 269 | } |
| 270 | } |
| 271 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 272 | for arch, p := range module.GetArchProperties(&BaseLinkerProperties{}) { |
| 273 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 274 | libs := baseLinkerProps.Header_libs |
| 275 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 276 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 277 | libs = append(libs, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 278 | libs = android.SortedUniqueStrings(libs) |
| 279 | deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 280 | linkopts.SetValueForArch(arch.Name, baseLinkerProps.Ldflags) |
| 281 | } |
| 282 | } |
| 283 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 284 | for os, p := range module.GetTargetProperties(&BaseLinkerProperties{}) { |
| 285 | if baseLinkerProps, ok := p.(*BaseLinkerProperties); ok { |
| 286 | libs := baseLinkerProps.Header_libs |
| 287 | libs = append(libs, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 288 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 289 | libs = append(libs, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 290 | libs = android.SortedUniqueStrings(libs) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 291 | deps.SetValueForOS(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 292 | linkopts.SetValueForOS(os.Name, baseLinkerProps.Ldflags) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 296 | return linkerAttributes{ |
| 297 | deps: deps, |
| 298 | linkopts: linkopts, |
| 299 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 302 | // Relativize a list of root-relative paths with respect to the module's |
| 303 | // directory. |
| 304 | // |
| 305 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 306 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 307 | // module dir relative. This function makes a list of paths entirely module dir |
| 308 | // relative. |
| 309 | // |
| 310 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 311 | // module. |
| 312 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 313 | var relativePaths []string |
| 314 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 315 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 316 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 317 | if err != nil { |
| 318 | panic(err) |
| 319 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 320 | relativePaths = append(relativePaths, relativePath) |
| 321 | } |
| 322 | return relativePaths |
| 323 | } |
| 324 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 325 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 326 | // exported included directories of a module. |
| 327 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 328 | libraryDecorator := module.linker.(*libraryDecorator) |
| 329 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 330 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 331 | // relative, so they don't need to be relativized like include_dirs, which |
| 332 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 333 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 334 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 335 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 336 | |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 337 | for arch, props := range module.GetArchProperties(&FlagExporterProperties{}) { |
| 338 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 339 | archIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 340 | archIncludeDirs = append(archIncludeDirs, flagExporterProperties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 341 | |
| 342 | // To avoid duplicate includes when base includes + arch includes are combined |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 343 | // 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] | 344 | archIncludeDirs = bazel.SubtractStrings(archIncludeDirs, includeDirs) |
| 345 | |
| 346 | if len(archIncludeDirs) > 0 { |
| 347 | includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs) |
| 348 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 349 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 350 | } |
| 351 | |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 352 | for os, props := range module.GetTargetProperties(&FlagExporterProperties{}) { |
| 353 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 354 | osIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 355 | osIncludeDirs = append(osIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 356 | |
| 357 | // To avoid duplicate includes when base includes + os includes are combined |
| 358 | // FIXME: This doesn't take conflicts between arch and os includes into account |
| 359 | osIncludeDirs = bazel.SubtractStrings(osIncludeDirs, includeDirs) |
| 360 | |
| 361 | if len(osIncludeDirs) > 0 { |
| 362 | includeDirsAttribute.SetValueForOS(os.Name, osIncludeDirs) |
| 363 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 367 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 368 | } |