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