Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package cc |
| 15 | |
| 16 | import ( |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 17 | "fmt" |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 18 | "path/filepath" |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 19 | "strings" |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 20 | |
| 21 | "android/soong/android" |
| 22 | "android/soong/bazel" |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 23 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 24 | "github.com/google/blueprint" |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 25 | |
| 26 | "github.com/google/blueprint/proptools" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 27 | ) |
| 28 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 29 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 30 | // 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] | 31 | type staticOrSharedAttributes struct { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 32 | Srcs bazel.LabelListAttribute |
| 33 | Srcs_c bazel.LabelListAttribute |
| 34 | Srcs_as bazel.LabelListAttribute |
| 35 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 36 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 37 | Deps bazel.LabelListAttribute |
| 38 | Implementation_deps bazel.LabelListAttribute |
| 39 | Dynamic_deps bazel.LabelListAttribute |
| 40 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 41 | Whole_archive_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 42 | |
| 43 | System_dynamic_deps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 44 | } |
| 45 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 46 | func groupSrcsByExtension(ctx android.TopDownMutatorContext, srcs bazel.LabelListAttribute) (cppSrcs, cSrcs, asSrcs bazel.LabelListAttribute) { |
| 47 | // Branch srcs into three language-specific groups. |
| 48 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 49 | // know the language of these sources until the genrule is executed. |
| 50 | // TODO(b/190006308): Handle language detection of sources in a Bazel rule. |
| 51 | isCSrcOrFilegroup := func(s string) bool { |
| 52 | return strings.HasSuffix(s, ".c") || strings.HasSuffix(s, "_c_srcs") |
| 53 | } |
| 54 | |
| 55 | isAsmSrcOrFilegroup := func(s string) bool { |
| 56 | return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") || strings.HasSuffix(s, "_as_srcs") |
| 57 | } |
| 58 | |
| 59 | // Check that a module is a filegroup type named <label>. |
| 60 | isFilegroupNamed := func(m android.Module, fullLabel string) bool { |
| 61 | if ctx.OtherModuleType(m) != "filegroup" { |
| 62 | return false |
| 63 | } |
| 64 | labelParts := strings.Split(fullLabel, ":") |
| 65 | if len(labelParts) > 2 { |
| 66 | // There should not be more than one colon in a label. |
| 67 | panic(fmt.Errorf("%s is not a valid Bazel label for a filegroup", fullLabel)) |
| 68 | } else { |
| 69 | return m.Name() == labelParts[len(labelParts)-1] |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Convert the filegroup dependencies into the extension-specific filegroups |
| 74 | // filtered in the filegroup.bzl macro. |
| 75 | cppFilegroup := func(label string) string { |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 76 | m, exists := ctx.ModuleFromName(label) |
| 77 | if exists { |
| 78 | aModule, _ := m.(android.Module) |
| 79 | if isFilegroupNamed(aModule, label) { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 80 | label = label + "_cpp_srcs" |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 81 | } |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 82 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 83 | return label |
| 84 | } |
| 85 | cFilegroup := func(label string) string { |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 86 | m, exists := ctx.ModuleFromName(label) |
| 87 | if exists { |
| 88 | aModule, _ := m.(android.Module) |
| 89 | if isFilegroupNamed(aModule, label) { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 90 | label = label + "_c_srcs" |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 91 | } |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 92 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 93 | return label |
| 94 | } |
| 95 | asFilegroup := func(label string) string { |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 96 | m, exists := ctx.ModuleFromName(label) |
| 97 | if exists { |
| 98 | aModule, _ := m.(android.Module) |
| 99 | if isFilegroupNamed(aModule, label) { |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 100 | label = label + "_as_srcs" |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 101 | } |
Chris Parsons | 5a34ffb | 2021-07-21 14:34:58 -0400 | [diff] [blame] | 102 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 103 | return label |
| 104 | } |
| 105 | |
| 106 | cSrcs = bazel.MapLabelListAttribute(srcs, cFilegroup) |
| 107 | cSrcs = bazel.FilterLabelListAttribute(cSrcs, isCSrcOrFilegroup) |
| 108 | |
| 109 | asSrcs = bazel.MapLabelListAttribute(srcs, asFilegroup) |
| 110 | asSrcs = bazel.FilterLabelListAttribute(asSrcs, isAsmSrcOrFilegroup) |
| 111 | |
| 112 | cppSrcs = bazel.MapLabelListAttribute(srcs, cppFilegroup) |
| 113 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, cSrcs) |
| 114 | cppSrcs = bazel.SubtractBazelLabelListAttribute(cppSrcs, asSrcs) |
| 115 | return |
| 116 | } |
| 117 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 118 | // bp2BuildParseLibProps returns the attributes for a variant of a cc_library. |
| 119 | func bp2BuildParseLibProps(ctx android.TopDownMutatorContext, module *Module, isStatic bool) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 120 | lib, ok := module.compiler.(*libraryDecorator) |
| 121 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 122 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 123 | } |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 124 | return bp2buildParseStaticOrSharedProps(ctx, module, lib, isStatic) |
| 125 | } |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 126 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 127 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
| 128 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
| 129 | return bp2BuildParseLibProps(ctx, module, false) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 133 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 134 | return bp2BuildParseLibProps(ctx, module, true) |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 135 | } |
| 136 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 137 | type depsPartition struct { |
| 138 | export bazel.LabelList |
| 139 | implementation bazel.LabelList |
| 140 | } |
| 141 | |
| 142 | type bazelLabelForDepsFn func(android.TopDownMutatorContext, []string) bazel.LabelList |
| 143 | |
| 144 | func partitionExportedAndImplementationsDeps(ctx android.TopDownMutatorContext, allDeps, exportedDeps []string, fn bazelLabelForDepsFn) depsPartition { |
| 145 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 146 | |
| 147 | return depsPartition{ |
| 148 | export: fn(ctx, export), |
| 149 | implementation: fn(ctx, implementation), |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | type bazelLabelForDepsExcludesFn func(android.TopDownMutatorContext, []string, []string) bazel.LabelList |
| 154 | |
| 155 | func partitionExportedAndImplementationsDepsExcludes(ctx android.TopDownMutatorContext, allDeps, excludes, exportedDeps []string, fn bazelLabelForDepsExcludesFn) depsPartition { |
| 156 | implementation, export := android.FilterList(allDeps, exportedDeps) |
| 157 | |
| 158 | return depsPartition{ |
| 159 | export: fn(ctx, export, excludes), |
| 160 | implementation: fn(ctx, implementation, excludes), |
| 161 | } |
| 162 | } |
| 163 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 164 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 165 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 166 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 167 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 168 | attrs.Copts.SetSelectValue(axis, config, props.Cflags) |
| 169 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 170 | attrs.System_dynamic_deps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, props.System_shared_libs)) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 171 | |
| 172 | staticDeps := partitionExportedAndImplementationsDeps(ctx, props.Static_libs, props.Export_static_lib_headers, bazelLabelForStaticDeps) |
| 173 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 174 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 175 | |
| 176 | sharedDeps := partitionExportedAndImplementationsDeps(ctx, props.Shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDeps) |
| 177 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 178 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 179 | |
| 180 | attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs)) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 181 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 182 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 183 | // nil -> use default values |
| 184 | // empty list -> no values specified |
| 185 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 186 | |
| 187 | if isStatic { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 188 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StaticProperties{}) { |
| 189 | for config, props := range configToProps { |
| 190 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 191 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | } else { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 196 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &SharedProperties{}) { |
| 197 | for config, props := range configToProps { |
| 198 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 199 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | } |
| 203 | } |
| 204 | |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 205 | cppSrcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 206 | attrs.Srcs = cppSrcs |
| 207 | attrs.Srcs_c = cSrcs |
| 208 | attrs.Srcs_as = asSrcs |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 209 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 210 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 211 | } |
| 212 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 213 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 214 | type prebuiltAttributes struct { |
| 215 | Src bazel.LabelAttribute |
| 216 | } |
| 217 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 218 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 219 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.TopDownMutatorContext, module *Module) prebuiltAttributes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 220 | var srcLabelAttribute bazel.LabelAttribute |
| 221 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 222 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &prebuiltLinkerProperties{}) { |
| 223 | for config, props := range configToProps { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 224 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 225 | if len(prebuiltLinkerProperties.Srcs) > 1 { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 226 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most once source file for %s %s\n", axis, config) |
| 227 | continue |
| 228 | } else if len(prebuiltLinkerProperties.Srcs) == 0 { |
| 229 | continue |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 230 | } |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 231 | src := android.BazelLabelForModuleSrcSingle(ctx, prebuiltLinkerProperties.Srcs[0]) |
| 232 | srcLabelAttribute.SetSelectValue(axis, config, src) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 237 | return prebuiltAttributes{ |
| 238 | Src: srcLabelAttribute, |
| 239 | } |
| 240 | } |
| 241 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 242 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 243 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 244 | // Options for all languages |
| 245 | copts bazel.StringListAttribute |
| 246 | // Assembly options and sources |
| 247 | asFlags bazel.StringListAttribute |
| 248 | asSrcs bazel.LabelListAttribute |
| 249 | // C options and sources |
| 250 | conlyFlags bazel.StringListAttribute |
| 251 | cSrcs bazel.LabelListAttribute |
| 252 | // C++ options and sources |
| 253 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 254 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 255 | |
| 256 | rtti bazel.BoolAttribute |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 257 | |
| 258 | localIncludes bazel.StringListAttribute |
| 259 | absoluteIncludes bazel.StringListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 262 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 263 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 264 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 265 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 266 | var asFlags bazel.StringListAttribute |
| 267 | var conlyFlags bazel.StringListAttribute |
| 268 | var cppFlags bazel.StringListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 269 | var rtti bazel.BoolAttribute |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 270 | var localIncludes bazel.StringListAttribute |
| 271 | var absoluteIncludes bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 272 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 273 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 274 | var result []string |
| 275 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 276 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 277 | // Bazel's copts, split them up to be compatible with the |
| 278 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 279 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 280 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 281 | return result |
| 282 | } |
| 283 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 284 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 285 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 286 | // Add srcs-like dependencies such as generated files. |
| 287 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 288 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 289 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 290 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 291 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 292 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, baseCompilerProps.Srcs, baseCompilerProps.Exclude_srcs) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 293 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 296 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 297 | for axis, configToProps := range archVariantCompilerProps { |
| 298 | for config, props := range configToProps { |
| 299 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
| 300 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 301 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 302 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 303 | srcsList := parseSrcs(baseCompilerProps) |
| 304 | srcs.SetSelectValue(axis, config, srcsList) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 305 | } |
| 306 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 307 | archVariantCopts := parseCommandLineFlags(baseCompilerProps.Cflags) |
| 308 | archVariantAsflags := parseCommandLineFlags(baseCompilerProps.Asflags) |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 309 | |
| 310 | localIncludeDirs := baseCompilerProps.Local_include_dirs |
| 311 | if axis == bazel.NoConfigAxis && includeBuildDirectory(baseCompilerProps.Include_build_directory) { |
| 312 | localIncludeDirs = append(localIncludeDirs, ".") |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 313 | } |
| 314 | |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 315 | absoluteIncludes.SetSelectValue(axis, config, baseCompilerProps.Include_dirs) |
| 316 | localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 317 | |
Chris Parsons | 69fa9f9 | 2021-07-13 11:47:44 -0400 | [diff] [blame] | 318 | copts.SetSelectValue(axis, config, archVariantCopts) |
| 319 | asFlags.SetSelectValue(axis, config, archVariantAsflags) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 320 | conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 321 | cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 322 | rtti.SetSelectValue(axis, config, baseCompilerProps.Rtti) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 323 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Liz Kammer | 74deed4 | 2021-06-02 13:02:03 -0400 | [diff] [blame] | 327 | srcs.ResolveExcludes() |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 328 | absoluteIncludes.DeduplicateAxesFromBase() |
| 329 | localIncludes.DeduplicateAxesFromBase() |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 330 | |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 331 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
| 332 | "Cflags": &copts, |
| 333 | "Asflags": &asFlags, |
| 334 | "CppFlags": &cppFlags, |
| 335 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 336 | productVariableProps := android.ProductVariableProperties(ctx) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 337 | for propName, attr := range productVarPropNameToAttribute { |
| 338 | if props, exists := productVariableProps[propName]; exists { |
| 339 | for _, prop := range props { |
| 340 | flags, ok := prop.Property.([]string) |
| 341 | if !ok { |
| 342 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 343 | } |
| 344 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 345 | attr.SetSelectValue(bazel.ProductVariableConfigurationAxis(prop.FullConfig), prop.FullConfig, newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 346 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 350 | srcs, cSrcs, asSrcs := groupSrcsByExtension(ctx, srcs) |
| 351 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 352 | return compilerAttributes{ |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 353 | copts: copts, |
| 354 | srcs: srcs, |
| 355 | asFlags: asFlags, |
| 356 | asSrcs: asSrcs, |
| 357 | cSrcs: cSrcs, |
| 358 | conlyFlags: conlyFlags, |
| 359 | cppFlags: cppFlags, |
| 360 | rtti: rtti, |
| 361 | localIncludes: localIncludes, |
| 362 | absoluteIncludes: absoluteIncludes, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | // Convenience struct to hold all attributes parsed from linker properties. |
| 367 | type linkerAttributes struct { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 368 | deps bazel.LabelListAttribute |
| 369 | implementationDeps bazel.LabelListAttribute |
| 370 | dynamicDeps bazel.LabelListAttribute |
| 371 | implementationDynamicDeps bazel.LabelListAttribute |
| 372 | wholeArchiveDeps bazel.LabelListAttribute |
| 373 | systemDynamicDeps bazel.LabelListAttribute |
| 374 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 375 | useLibcrt bazel.BoolAttribute |
| 376 | linkopts bazel.StringListAttribute |
| 377 | versionScript bazel.LabelAttribute |
| 378 | stripKeepSymbols bazel.BoolAttribute |
| 379 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 380 | stripKeepSymbolsList bazel.StringListAttribute |
| 381 | stripAll bazel.BoolAttribute |
| 382 | stripNone bazel.BoolAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 383 | } |
| 384 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 385 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 386 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 387 | flags := linkerProperties.Ldflags |
| 388 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 389 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 390 | } |
| 391 | return flags |
| 392 | } |
| 393 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 394 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 395 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 396 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 397 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 398 | var headerDeps bazel.LabelListAttribute |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 399 | var implementationHeaderDeps bazel.LabelListAttribute |
| 400 | var deps bazel.LabelListAttribute |
| 401 | var implementationDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 402 | var dynamicDeps bazel.LabelListAttribute |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 403 | var implementationDynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 404 | var wholeArchiveDeps bazel.LabelListAttribute |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 405 | systemSharedDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true} |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 406 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 407 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 408 | var versionScript bazel.LabelAttribute |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 409 | var useLibcrt bazel.BoolAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 410 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 411 | var stripKeepSymbols bazel.BoolAttribute |
| 412 | var stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 413 | var stripKeepSymbolsList bazel.StringListAttribute |
| 414 | var stripAll bazel.BoolAttribute |
| 415 | var stripNone bazel.BoolAttribute |
| 416 | |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 417 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &StripProperties{}) { |
| 418 | for config, props := range configToProps { |
| 419 | if stripProperties, ok := props.(*StripProperties); ok { |
| 420 | stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 421 | stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 422 | stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 423 | stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 424 | stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 429 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) { |
| 430 | for config, props := range configToProps { |
| 431 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 432 | |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 433 | // Excludes to parallel Soong: |
| 434 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 435 | staticLibs := android.FirstUniqueStrings(baseLinkerProps.Static_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 436 | staticDeps := partitionExportedAndImplementationsDepsExcludes(ctx, staticLibs, baseLinkerProps.Exclude_static_libs, baseLinkerProps.Export_static_lib_headers, bazelLabelForStaticDepsExcludes) |
| 437 | deps.SetSelectValue(axis, config, staticDeps.export) |
| 438 | implementationDeps.SetSelectValue(axis, config, staticDeps.implementation) |
| 439 | |
| 440 | wholeStaticLibs := android.FirstUniqueStrings(baseLinkerProps.Whole_static_libs) |
| 441 | wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, baseLinkerProps.Exclude_static_libs)) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 442 | |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 443 | systemSharedLibs := baseLinkerProps.System_shared_libs |
| 444 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 445 | // nil -> use default values |
| 446 | // empty list -> no values specified |
| 447 | if len(systemSharedLibs) > 0 { |
| 448 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 449 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 450 | systemSharedDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 451 | |
| 452 | sharedLibs := android.FirstUniqueStrings(baseLinkerProps.Shared_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 453 | sharedDeps := partitionExportedAndImplementationsDepsExcludes(ctx, sharedLibs, baseLinkerProps.Exclude_shared_libs, baseLinkerProps.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes) |
| 454 | dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 455 | implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 456 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 457 | headerLibs := android.FirstUniqueStrings(baseLinkerProps.Header_libs) |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 458 | hDeps := partitionExportedAndImplementationsDeps(ctx, headerLibs, baseLinkerProps.Export_header_lib_headers, bazelLabelForHeaderDeps) |
| 459 | |
| 460 | headerDeps.SetSelectValue(axis, config, hDeps.export) |
| 461 | implementationHeaderDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 462 | |
| 463 | linkopts.SetSelectValue(axis, config, getBp2BuildLinkerFlags(baseLinkerProps)) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 464 | if baseLinkerProps.Version_script != nil { |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 465 | versionScript.SetSelectValue(axis, config, android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
Rupert Shuttleworth | 22cd2eb | 2021-05-27 02:15:54 -0400 | [diff] [blame] | 466 | } |
Liz Kammer | d366c90 | 2021-06-03 13:43:01 -0400 | [diff] [blame] | 467 | useLibcrt.SetSelectValue(axis, config, baseLinkerProps.libCrt()) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 468 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 469 | } |
| 470 | } |
| 471 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 472 | type productVarDep struct { |
| 473 | // the name of the corresponding excludes field, if one exists |
| 474 | excludesField string |
| 475 | // reference to the bazel attribute that should be set for the given product variable config |
| 476 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 477 | |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 478 | depResolutionFunc func(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | productVarToDepFields := map[string]productVarDep{ |
| 482 | // product variables do not support exclude_shared_libs |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 483 | "Shared_libs": productVarDep{attribute: &implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 484 | "Static_libs": productVarDep{"Exclude_static_libs", &implementationDeps, bazelLabelForStaticDepsExcludes}, |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 485 | "Whole_static_libs": productVarDep{"Exclude_static_libs", &wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | productVariableProps := android.ProductVariableProperties(ctx) |
| 489 | for name, dep := range productVarToDepFields { |
| 490 | props, exists := productVariableProps[name] |
| 491 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 492 | // if neither an include or excludes property exists, then skip it |
| 493 | if !exists && !excludesExists { |
| 494 | continue |
| 495 | } |
| 496 | // collect all the configurations that an include or exclude property exists for. |
| 497 | // we want to iterate all configurations rather than either the include or exclude because for a |
| 498 | // particular configuration we may have only and include or only an exclude to handle |
| 499 | configs := make(map[string]bool, len(props)+len(excludeProps)) |
| 500 | for config := range props { |
| 501 | configs[config] = true |
| 502 | } |
| 503 | for config := range excludeProps { |
| 504 | configs[config] = true |
| 505 | } |
| 506 | |
| 507 | for config := range configs { |
| 508 | prop, includesExists := props[config] |
| 509 | excludesProp, excludesExists := excludeProps[config] |
| 510 | var includes, excludes []string |
| 511 | var ok bool |
| 512 | // if there was no includes/excludes property, casting fails and that's expected |
| 513 | if includes, ok = prop.Property.([]string); includesExists && !ok { |
| 514 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 515 | } |
| 516 | if excludes, ok = excludesProp.Property.([]string); excludesExists && !ok { |
| 517 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 518 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 519 | |
| 520 | dep.attribute.SetSelectValue(bazel.ProductVariableConfigurationAxis(config), config, dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes)) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 524 | headerDeps.Append(deps) |
| 525 | implementationHeaderDeps.Append(implementationDeps) |
| 526 | |
| 527 | headerDeps.ResolveExcludes() |
| 528 | implementationHeaderDeps.ResolveExcludes() |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 529 | dynamicDeps.ResolveExcludes() |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 530 | implementationDynamicDeps.ResolveExcludes() |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 531 | wholeArchiveDeps.ResolveExcludes() |
| 532 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 533 | return linkerAttributes{ |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame^] | 534 | deps: headerDeps, |
| 535 | implementationDeps: implementationHeaderDeps, |
| 536 | dynamicDeps: dynamicDeps, |
| 537 | implementationDynamicDeps: implementationDynamicDeps, |
| 538 | wholeArchiveDeps: wholeArchiveDeps, |
| 539 | systemDynamicDeps: systemSharedDeps, |
| 540 | |
| 541 | linkopts: linkopts, |
| 542 | useLibcrt: useLibcrt, |
| 543 | versionScript: versionScript, |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 544 | |
| 545 | // Strip properties |
| 546 | stripKeepSymbols: stripKeepSymbols, |
| 547 | stripKeepSymbolsAndDebugFrame: stripKeepSymbolsAndDebugFrame, |
| 548 | stripKeepSymbolsList: stripKeepSymbolsList, |
| 549 | stripAll: stripAll, |
| 550 | stripNone: stripNone, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 551 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 554 | // Relativize a list of root-relative paths with respect to the module's |
| 555 | // directory. |
| 556 | // |
| 557 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 558 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 559 | // module dir relative. This function makes a list of paths entirely module dir |
| 560 | // relative. |
| 561 | // |
| 562 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 563 | // module. |
| 564 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 565 | var relativePaths []string |
| 566 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 567 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 568 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 569 | if err != nil { |
| 570 | panic(err) |
| 571 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 572 | relativePaths = append(relativePaths, relativePath) |
| 573 | } |
| 574 | return relativePaths |
| 575 | } |
| 576 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 577 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 578 | // attributes. |
| 579 | type BazelIncludes struct { |
| 580 | Includes bazel.StringListAttribute |
| 581 | SystemIncludes bazel.StringListAttribute |
| 582 | } |
| 583 | |
| 584 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) BazelIncludes { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 585 | libraryDecorator := module.linker.(*libraryDecorator) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 586 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 587 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 588 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 589 | // Bp2buildParseExportedIncludesForPrebuiltLibrary returns a BazelIncludes with Bazel-ified values |
| 590 | // to export includes from the underlying module's properties. |
| 591 | func Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx android.TopDownMutatorContext, module *Module) BazelIncludes { |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 592 | prebuiltLibraryLinker := module.linker.(*prebuiltLibraryLinker) |
| 593 | libraryDecorator := prebuiltLibraryLinker.libraryDecorator |
| 594 | return bp2BuildParseExportedIncludesHelper(ctx, module, libraryDecorator) |
| 595 | } |
| 596 | |
| 597 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
| 598 | // exported included directories of a module. |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 599 | func bp2BuildParseExportedIncludesHelper(ctx android.TopDownMutatorContext, module *Module, libraryDecorator *libraryDecorator) BazelIncludes { |
| 600 | exported := BazelIncludes{} |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 601 | for axis, configToProps := range module.GetArchVariantProperties(ctx, &FlagExporterProperties{}) { |
| 602 | for config, props := range configToProps { |
| 603 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 604 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 605 | exported.Includes.SetSelectValue(axis, config, flagExporterProperties.Export_include_dirs) |
| 606 | } |
| 607 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 608 | exported.SystemIncludes.SetSelectValue(axis, config, flagExporterProperties.Export_system_include_dirs) |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame] | 609 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 610 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 611 | } |
| 612 | } |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 613 | exported.Includes.DeduplicateAxesFromBase() |
| 614 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 615 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 616 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 617 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 618 | |
| 619 | func bazelLabelForStaticModule(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 620 | label := android.BazelModuleLabel(ctx, m) |
| 621 | if aModule, ok := m.(android.Module); ok { |
| 622 | if ctx.OtherModuleType(aModule) == "cc_library" && !android.GenerateCcLibraryStaticOnly(m.Name()) { |
| 623 | label += "_bp2build_cc_library_static" |
| 624 | } |
| 625 | } |
| 626 | return label |
| 627 | } |
| 628 | |
| 629 | func bazelLabelForSharedModule(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 630 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 631 | // library. |
| 632 | return android.BazelModuleLabel(ctx, m) |
| 633 | } |
| 634 | |
| 635 | func bazelLabelForStaticWholeModuleDeps(ctx android.TopDownMutatorContext, m blueprint.Module) string { |
| 636 | label := bazelLabelForStaticModule(ctx, m) |
| 637 | if aModule, ok := m.(android.Module); ok { |
| 638 | if android.IsModulePrebuilt(aModule) { |
| 639 | label += "_alwayslink" |
| 640 | } |
| 641 | } |
| 642 | return label |
| 643 | } |
| 644 | |
| 645 | func bazelLabelForWholeDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 646 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 647 | } |
| 648 | |
| 649 | func bazelLabelForWholeDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 650 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 651 | } |
| 652 | |
| 653 | func bazelLabelForStaticDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 654 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 655 | } |
| 656 | |
| 657 | func bazelLabelForStaticDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 658 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 659 | } |
| 660 | |
| 661 | func bazelLabelForSharedDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 662 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 663 | } |
| 664 | |
| 665 | func bazelLabelForHeaderDeps(ctx android.TopDownMutatorContext, modules []string) bazel.LabelList { |
| 666 | // This is not elegant, but bp2build's shared library targets only propagate |
| 667 | // their header information as part of the normal C++ provider. |
| 668 | return bazelLabelForSharedDeps(ctx, modules) |
| 669 | } |
| 670 | |
| 671 | func bazelLabelForSharedDepsExcludes(ctx android.TopDownMutatorContext, modules, excludes []string) bazel.LabelList { |
| 672 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 673 | } |