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