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