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