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 ( |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 30 | cSrcPartition = "c" |
| 31 | asSrcPartition = "as" |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 32 | lSrcPartition = "l" |
| 33 | llSrcPartition = "ll" |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 34 | cppSrcPartition = "cpp" |
| 35 | protoSrcPartition = "proto" |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 36 | ) |
| 37 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 38 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 39 | // 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] | 40 | type staticOrSharedAttributes struct { |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 41 | Srcs bazel.LabelListAttribute |
| 42 | Srcs_c bazel.LabelListAttribute |
| 43 | Srcs_as bazel.LabelListAttribute |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 44 | Hdrs bazel.LabelListAttribute |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 45 | Copts bazel.StringListAttribute |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 46 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 47 | Deps bazel.LabelListAttribute |
| 48 | Implementation_deps bazel.LabelListAttribute |
| 49 | Dynamic_deps bazel.LabelListAttribute |
| 50 | Implementation_dynamic_deps bazel.LabelListAttribute |
| 51 | Whole_archive_deps bazel.LabelListAttribute |
| 52 | Implementation_whole_archive_deps bazel.LabelListAttribute |
Chris Parsons | 51f8c39 | 2021-08-03 21:01:05 -0400 | [diff] [blame] | 53 | |
| 54 | System_dynamic_deps bazel.LabelListAttribute |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 55 | |
| 56 | Enabled bazel.BoolAttribute |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 57 | |
| 58 | sdkAttributes |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 59 | } |
| 60 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 61 | // groupSrcsByExtension partitions `srcs` into groups based on file extension. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 62 | func groupSrcsByExtension(ctx android.BazelConversionPathContext, srcs bazel.LabelListAttribute) bazel.PartitionToLabelListAttribute { |
Liz Kammer | 57e2e7a | 2021-09-20 12:55:02 -0400 | [diff] [blame] | 63 | // Convert filegroup dependencies into extension-specific filegroups filtered in the filegroup.bzl |
| 64 | // macro. |
| 65 | addSuffixForFilegroup := func(suffix string) bazel.LabelMapper { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 66 | return func(ctx bazel.OtherModuleContext, label bazel.Label) (string, bool) { |
| 67 | m, exists := ctx.ModuleFromName(label.OriginalModuleName) |
| 68 | labelStr := label.Label |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 69 | if !exists || !android.IsFilegroup(ctx, m) { |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 70 | return labelStr, false |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 71 | } |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 72 | return labelStr + 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. |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 77 | labels := bazel.LabelPartitions{ |
| 78 | protoSrcPartition: android.ProtoSrcLabelPartition, |
Liz Kammer | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 79 | cSrcPartition: bazel.LabelPartition{Extensions: []string{".c"}, LabelMapper: addSuffixForFilegroup("_c_srcs")}, |
| 80 | asSrcPartition: bazel.LabelPartition{Extensions: []string{".s", ".S"}, LabelMapper: addSuffixForFilegroup("_as_srcs")}, |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 81 | // TODO(http://b/231968910): If there is ever a filegroup target that |
| 82 | // contains .l or .ll files we will need to find a way to add a |
| 83 | // LabelMapper for these that identifies these filegroups and |
| 84 | // converts them appropriately |
| 85 | lSrcPartition: bazel.LabelPartition{Extensions: []string{".l"}}, |
| 86 | llSrcPartition: bazel.LabelPartition{Extensions: []string{".ll"}}, |
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 | aabfb5d | 2021-12-08 15:25:06 -0500 | [diff] [blame] | 89 | cppSrcPartition: bazel.LabelPartition{Extensions: []string{".cpp", ".cc", ".cxx", ".mm"}, LabelMapper: addSuffixForFilegroup("_cpp_srcs"), Keep_remainder: true}, |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 90 | } |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 91 | |
Sam Delmerico | c768102 | 2022-02-04 21:01:20 +0000 | [diff] [blame] | 92 | return bazel.PartitionLabelListAttribute(ctx, &srcs, labels) |
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 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 152 | // Parses properties common to static and shared libraries. Also used for prebuilt libraries. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 153 | func bp2buildParseStaticOrSharedProps(ctx android.BazelConversionPathContext, module *Module, lib *libraryDecorator, isStatic bool) staticOrSharedAttributes { |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 154 | attrs := staticOrSharedAttributes{} |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 155 | |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 156 | setAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 157 | attrs.Copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag)) |
Jingwen Chen | c4dc9b4 | 2021-06-11 12:51:48 +0000 | [diff] [blame] | 158 | attrs.Srcs.SetSelectValue(axis, config, android.BazelLabelForModuleSrc(ctx, props.Srcs)) |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 159 | 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] | 160 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 161 | 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] | 162 | attrs.Deps.SetSelectValue(axis, config, staticDeps.export) |
| 163 | attrs.Implementation_deps.SetSelectValue(axis, config, staticDeps.implementation) |
| 164 | |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 165 | 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] | 166 | attrs.Dynamic_deps.SetSelectValue(axis, config, sharedDeps.export) |
| 167 | attrs.Implementation_dynamic_deps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 168 | |
| 169 | attrs.Whole_archive_deps.SetSelectValue(axis, config, bazelLabelForWholeDeps(ctx, props.Whole_static_libs)) |
Chris Parsons | 58852a0 | 2021-12-09 18:10:18 -0500 | [diff] [blame] | 170 | attrs.Enabled.SetSelectValue(axis, config, props.Enabled) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 171 | } |
Liz Kammer | 135bf55 | 2021-08-11 10:46:06 -0400 | [diff] [blame] | 172 | // system_dynamic_deps distinguishes between nil/empty list behavior: |
| 173 | // nil -> use default values |
| 174 | // empty list -> no values specified |
| 175 | attrs.System_dynamic_deps.ForceSpecifyEmptyList = true |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 176 | |
| 177 | if isStatic { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 178 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 179 | if staticOrSharedProps, ok := props.(*StaticProperties); ok { |
| 180 | setAttrs(axis, config, staticOrSharedProps.Static) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 181 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 182 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 183 | } else { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 184 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 185 | if staticOrSharedProps, ok := props.(*SharedProperties); ok { |
| 186 | setAttrs(axis, config, staticOrSharedProps.Shared) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 187 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 188 | }) |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Liz Kammer | ae3994e | 2021-10-19 09:45:48 -0400 | [diff] [blame] | 191 | partitionedSrcs := groupSrcsByExtension(ctx, attrs.Srcs) |
| 192 | attrs.Srcs = partitionedSrcs[cppSrcPartition] |
| 193 | attrs.Srcs_c = partitionedSrcs[cSrcPartition] |
| 194 | attrs.Srcs_as = partitionedSrcs[asSrcPartition] |
Jingwen Chen | 14a8bda | 2021-06-02 11:10:02 +0000 | [diff] [blame] | 195 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 196 | if !partitionedSrcs[protoSrcPartition].IsEmpty() { |
| 197 | // TODO(b/208815215): determine whether this is used and add support if necessary |
| 198 | ctx.ModuleErrorf("Migrating static/shared only proto srcs is not currently supported") |
| 199 | } |
| 200 | |
Jingwen Chen | bcf5304 | 2021-05-26 04:42:42 +0000 | [diff] [blame] | 201 | return attrs |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 204 | // Convenience struct to hold all attributes parsed from prebuilt properties. |
| 205 | type prebuiltAttributes struct { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 206 | Src bazel.LabelAttribute |
| 207 | Enabled bazel.BoolAttribute |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 208 | } |
| 209 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | ac5097f | 2021-09-01 21:22:09 +0000 | [diff] [blame] | 210 | // NOTE: Used outside of Soong repo project, in the clangprebuilts.go bootstrap_go_package |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 211 | func Bp2BuildParsePrebuiltLibraryProps(ctx android.BazelConversionPathContext, module *Module, isStatic bool) prebuiltAttributes { |
| 212 | manySourceFileError := func(axis bazel.ConfigurationAxis, config string) { |
| 213 | ctx.ModuleErrorf("Bp2BuildParsePrebuiltLibraryProps: Expected at most one source file for %s %s\n", axis, config) |
| 214 | } |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 215 | var srcLabelAttribute bazel.LabelAttribute |
| 216 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 217 | parseSrcs := func(ctx android.BazelConversionPathContext, axis bazel.ConfigurationAxis, config string, srcs []string) { |
| 218 | if len(srcs) > 1 { |
| 219 | manySourceFileError(axis, config) |
| 220 | return |
| 221 | } else if len(srcs) == 0 { |
| 222 | return |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 223 | } |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 224 | if srcLabelAttribute.SelectValue(axis, config) != nil { |
| 225 | manySourceFileError(axis, config) |
| 226 | return |
| 227 | } |
| 228 | |
| 229 | src := android.BazelLabelForModuleSrcSingle(ctx, srcs[0]) |
| 230 | srcLabelAttribute.SetSelectValue(axis, config, src) |
| 231 | } |
| 232 | |
| 233 | bp2BuildPropParseHelper(ctx, module, &prebuiltLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 234 | if prebuiltLinkerProperties, ok := props.(*prebuiltLinkerProperties); ok { |
| 235 | parseSrcs(ctx, axis, config, prebuiltLinkerProperties.Srcs) |
| 236 | } |
| 237 | }) |
| 238 | |
| 239 | var enabledLabelAttribute bazel.BoolAttribute |
| 240 | parseAttrs := func(axis bazel.ConfigurationAxis, config string, props StaticOrSharedProperties) { |
| 241 | if props.Enabled != nil { |
| 242 | enabledLabelAttribute.SetSelectValue(axis, config, props.Enabled) |
| 243 | } |
| 244 | parseSrcs(ctx, axis, config, props.Srcs) |
| 245 | } |
| 246 | |
| 247 | if isStatic { |
| 248 | bp2BuildPropParseHelper(ctx, module, &StaticProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 249 | if staticProperties, ok := props.(*StaticProperties); ok { |
| 250 | parseAttrs(axis, config, staticProperties.Static) |
| 251 | } |
| 252 | }) |
| 253 | } else { |
| 254 | bp2BuildPropParseHelper(ctx, module, &SharedProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 255 | if sharedProperties, ok := props.(*SharedProperties); ok { |
| 256 | parseAttrs(axis, config, sharedProperties.Shared) |
| 257 | } |
| 258 | }) |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 259 | } |
| 260 | |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 261 | return prebuiltAttributes{ |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 262 | Src: srcLabelAttribute, |
| 263 | Enabled: enabledLabelAttribute, |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func bp2BuildPropParseHelper(ctx android.ArchVariantContext, module *Module, propsType interface{}, parseFunc func(axis bazel.ConfigurationAxis, config string, props interface{})) { |
| 268 | for axis, configToProps := range module.GetArchVariantProperties(ctx, propsType) { |
| 269 | for config, props := range configToProps { |
| 270 | parseFunc(axis, config, props) |
| 271 | } |
Rupert Shuttleworth | ffd4582 | 2021-05-14 03:02:34 -0400 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 275 | type baseAttributes struct { |
| 276 | compilerAttributes |
| 277 | linkerAttributes |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 278 | |
| 279 | protoDependency *bazel.LabelAttribute |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 280 | } |
| 281 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 282 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 283 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 284 | // Options for all languages |
| 285 | copts bazel.StringListAttribute |
| 286 | // Assembly options and sources |
| 287 | asFlags bazel.StringListAttribute |
| 288 | asSrcs bazel.LabelListAttribute |
| 289 | // C options and sources |
| 290 | conlyFlags bazel.StringListAttribute |
| 291 | cSrcs bazel.LabelListAttribute |
| 292 | // C++ options and sources |
| 293 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 294 | srcs bazel.LabelListAttribute |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 295 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 296 | // Lex sources and options |
| 297 | lSrcs bazel.LabelListAttribute |
| 298 | llSrcs bazel.LabelListAttribute |
| 299 | lexopts bazel.StringListAttribute |
| 300 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 301 | hdrs bazel.LabelListAttribute |
| 302 | |
Chris Parsons | 2c78839 | 2021-08-10 11:58:07 -0400 | [diff] [blame] | 303 | rtti bazel.BoolAttribute |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 304 | |
| 305 | // Not affected by arch variants |
| 306 | stl *string |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 307 | cStd *string |
Jingwen Chen | 5b11ab1 | 2021-10-11 17:44:33 +0000 | [diff] [blame] | 308 | cppStd *string |
Liz Kammer | 35687bc | 2021-09-10 10:07:07 -0400 | [diff] [blame] | 309 | |
| 310 | localIncludes bazel.StringListAttribute |
| 311 | absoluteIncludes bazel.StringListAttribute |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 312 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 313 | includes BazelIncludes |
| 314 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 315 | protoSrcs bazel.LabelListAttribute |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 316 | |
| 317 | stubsSymbolFile *string |
| 318 | stubsVersions bazel.StringListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 321 | type filterOutFn func(string) bool |
| 322 | |
| 323 | func filterOutStdFlag(flag string) bool { |
| 324 | return strings.HasPrefix(flag, "-std=") |
| 325 | } |
| 326 | |
| 327 | func parseCommandLineFlags(soongFlags []string, filterOut filterOutFn) []string { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 328 | var result []string |
| 329 | for _, flag := range soongFlags { |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 330 | if filterOut != nil && filterOut(flag) { |
| 331 | continue |
| 332 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 333 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 334 | // Bazel's copts, split them up to be compatible with the |
| 335 | // no_copts_tokenization feature. |
| 336 | result = append(result, strings.Split(flag, " ")...) |
| 337 | } |
| 338 | return result |
| 339 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 340 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 341 | 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] | 342 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 343 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 344 | if srcsList, ok := parseSrcs(ctx, props); ok { |
| 345 | ca.srcs.SetSelectValue(axis, config, srcsList) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 346 | } |
| 347 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 348 | localIncludeDirs := props.Local_include_dirs |
| 349 | if axis == bazel.NoConfigAxis { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 350 | 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] | 351 | if includeBuildDirectory(props.Include_build_directory) { |
| 352 | localIncludeDirs = append(localIncludeDirs, ".") |
Liz Kammer | 222bdcf | 2021-10-11 14:15:51 -0400 | [diff] [blame] | 353 | } |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 356 | ca.absoluteIncludes.SetSelectValue(axis, config, props.Include_dirs) |
| 357 | ca.localIncludes.SetSelectValue(axis, config, localIncludeDirs) |
| 358 | |
Liz Kammer | cac7f69 | 2021-12-16 14:19:32 -0500 | [diff] [blame] | 359 | // In Soong, cflags occur on the command line before -std=<val> flag, resulting in the value being |
| 360 | // overridden. In Bazel we always allow overriding, via flags; however, this can cause |
| 361 | // incompatibilities, so we remove "-std=" flags from Cflag properties while leaving it in other |
| 362 | // cases. |
| 363 | ca.copts.SetSelectValue(axis, config, parseCommandLineFlags(props.Cflags, filterOutStdFlag)) |
| 364 | ca.asFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Asflags, nil)) |
| 365 | ca.conlyFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Conlyflags, nil)) |
| 366 | ca.cppFlags.SetSelectValue(axis, config, parseCommandLineFlags(props.Cppflags, nil)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 367 | ca.rtti.SetSelectValue(axis, config, props.Rtti) |
| 368 | } |
| 369 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 370 | func (ca *compilerAttributes) convertStlProps(ctx android.ArchVariantContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 371 | bp2BuildPropParseHelper(ctx, module, &StlProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 372 | if stlProps, ok := props.(*StlProperties); ok { |
| 373 | if stlProps.Stl == nil { |
| 374 | return |
| 375 | } |
| 376 | if ca.stl == nil { |
| 377 | ca.stl = stlProps.Stl |
| 378 | } else if ca.stl != stlProps.Stl { |
| 379 | ctx.ModuleErrorf("Unsupported conversion: module with different stl for different variants: %s and %s", *ca.stl, stlProps.Stl) |
Liz Kammer | 9abd62d | 2021-05-21 08:37:59 -0400 | [diff] [blame] | 380 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 381 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 382 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 383 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 384 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 385 | func (ca *compilerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 386 | productVarPropNameToAttribute := map[string]*bazel.StringListAttribute{ |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 387 | "Cflags": &ca.copts, |
| 388 | "Asflags": &ca.asFlags, |
| 389 | "CppFlags": &ca.cppFlags, |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 390 | } |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 391 | for propName, attr := range productVarPropNameToAttribute { |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 392 | if productConfigProps, exists := productVariableProps[propName]; exists { |
| 393 | for productConfigProp, prop := range productConfigProps { |
| 394 | flags, ok := prop.([]string) |
Liz Kammer | ba7a9c5 | 2021-05-26 08:45:30 -0400 | [diff] [blame] | 395 | if !ok { |
| 396 | ctx.ModuleErrorf("Could not convert product variable %s property", proptools.PropertyNameForField(propName)) |
| 397 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 398 | newFlags, _ := bazel.TryVariableSubstitutions(flags, productConfigProp.Name) |
| 399 | attr.SetSelectValue(productConfigProp.ConfigurationAxis(), productConfigProp.SelectKey(), newFlags) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 400 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 401 | } |
| 402 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 403 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 404 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 405 | func (ca *compilerAttributes) finalize(ctx android.BazelConversionPathContext, implementationHdrs bazel.LabelListAttribute) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 406 | ca.srcs.ResolveExcludes() |
| 407 | partitionedSrcs := groupSrcsByExtension(ctx, ca.srcs) |
| 408 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 409 | ca.protoSrcs = partitionedSrcs[protoSrcPartition] |
| 410 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 411 | for p, lla := range partitionedSrcs { |
| 412 | // if there are no sources, there is no need for headers |
| 413 | if lla.IsEmpty() { |
| 414 | continue |
| 415 | } |
| 416 | lla.Append(implementationHdrs) |
| 417 | partitionedSrcs[p] = lla |
| 418 | } |
| 419 | |
| 420 | ca.srcs = partitionedSrcs[cppSrcPartition] |
| 421 | ca.cSrcs = partitionedSrcs[cSrcPartition] |
| 422 | ca.asSrcs = partitionedSrcs[asSrcPartition] |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 423 | ca.lSrcs = partitionedSrcs[lSrcPartition] |
| 424 | ca.llSrcs = partitionedSrcs[llSrcPartition] |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 425 | |
| 426 | ca.absoluteIncludes.DeduplicateAxesFromBase() |
| 427 | ca.localIncludes.DeduplicateAxesFromBase() |
| 428 | } |
| 429 | |
| 430 | // Parse srcs from an arch or OS's props value. |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 431 | func parseSrcs(ctx android.BazelConversionPathContext, props *BaseCompilerProperties) (bazel.LabelList, bool) { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 432 | anySrcs := false |
| 433 | // Add srcs-like dependencies such as generated files. |
| 434 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 435 | generatedSrcsLabelList := android.BazelLabelForModuleDepsExcludes(ctx, props.Generated_sources, props.Exclude_generated_sources) |
| 436 | if len(props.Generated_sources) > 0 || len(props.Exclude_generated_sources) > 0 { |
| 437 | anySrcs = true |
| 438 | } |
| 439 | |
| 440 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, props.Srcs, props.Exclude_srcs) |
| 441 | if len(props.Srcs) > 0 || len(props.Exclude_srcs) > 0 { |
| 442 | anySrcs = true |
| 443 | } |
| 444 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedSrcsLabelList), anySrcs |
| 445 | } |
| 446 | |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 447 | func bp2buildResolveCppStdValue(c_std *string, cpp_std *string, gnu_extensions *bool) (*string, *string) { |
| 448 | var cStdVal, cppStdVal string |
| 449 | // If c{,pp}std properties are not specified, don't generate them in the BUILD file. |
| 450 | // Defaults are handled by the toolchain definition. |
| 451 | // 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] | 452 | if cpp_std != nil { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 453 | cppStdVal = parseCppStd(cpp_std) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 454 | } else if gnu_extensions != nil && !*gnu_extensions { |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 455 | cppStdVal = "c++17" |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 456 | } |
Chris Parsons | 79bd2b7 | 2021-11-29 17:52:41 -0500 | [diff] [blame] | 457 | if c_std != nil { |
| 458 | cStdVal = parseCStd(c_std) |
| 459 | } else if gnu_extensions != nil && !*gnu_extensions { |
| 460 | cStdVal = "c99" |
| 461 | } |
| 462 | |
| 463 | cStdVal, cppStdVal = maybeReplaceGnuToC(gnu_extensions, cStdVal, cppStdVal) |
Liz Kammer | 46fb7ab | 2021-12-01 10:09:34 -0500 | [diff] [blame] | 464 | var c_std_prop, cpp_std_prop *string |
| 465 | if cStdVal != "" { |
| 466 | c_std_prop = &cStdVal |
| 467 | } |
| 468 | if cppStdVal != "" { |
| 469 | cpp_std_prop = &cppStdVal |
| 470 | } |
| 471 | |
| 472 | return c_std_prop, cpp_std_prop |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 473 | } |
| 474 | |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 475 | // packageFromLabel extracts package from a fully-qualified or relative Label and whether the label |
| 476 | // is fully-qualified. |
| 477 | // e.g. fully-qualified "//a/b:foo" -> "a/b", true, relative: ":bar" -> ".", false |
| 478 | func packageFromLabel(label string) (string, bool) { |
| 479 | split := strings.Split(label, ":") |
| 480 | if len(split) != 2 { |
| 481 | return "", false |
| 482 | } |
| 483 | if split[0] == "" { |
| 484 | return ".", false |
| 485 | } |
| 486 | // remove leading "//" |
| 487 | return split[0][2:], true |
| 488 | } |
| 489 | |
| 490 | // includesFromLabelList extracts relative/absolute includes from a bazel.LabelList> |
| 491 | func includesFromLabelList(labelList bazel.LabelList) (relative, absolute []string) { |
| 492 | for _, hdr := range labelList.Includes { |
| 493 | if pkg, hasPkg := packageFromLabel(hdr.Label); hasPkg { |
| 494 | absolute = append(absolute, pkg) |
| 495 | } else if pkg != "" { |
| 496 | relative = append(relative, pkg) |
| 497 | } |
| 498 | } |
| 499 | return relative, absolute |
| 500 | } |
| 501 | |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 502 | // bp2BuildParseBaseProps returns all compiler, linker, library attributes of a cc module.. |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 503 | func bp2BuildParseBaseProps(ctx android.Bp2buildMutatorContext, module *Module) baseAttributes { |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 504 | archVariantCompilerProps := module.GetArchVariantProperties(ctx, &BaseCompilerProperties{}) |
| 505 | archVariantLinkerProps := module.GetArchVariantProperties(ctx, &BaseLinkerProperties{}) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 506 | archVariantLibraryProperties := module.GetArchVariantProperties(ctx, &LibraryProperties{}) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 507 | |
| 508 | var implementationHdrs bazel.LabelListAttribute |
| 509 | |
| 510 | axisToConfigs := map[bazel.ConfigurationAxis]map[string]bool{} |
| 511 | allAxesAndConfigs := func(cp android.ConfigurationAxisToArchVariantProperties) { |
| 512 | for axis, configMap := range cp { |
| 513 | if _, ok := axisToConfigs[axis]; !ok { |
| 514 | axisToConfigs[axis] = map[string]bool{} |
| 515 | } |
| 516 | for config, _ := range configMap { |
| 517 | axisToConfigs[axis][config] = true |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 518 | } |
| 519 | } |
| 520 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 521 | allAxesAndConfigs(archVariantCompilerProps) |
| 522 | allAxesAndConfigs(archVariantLinkerProps) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 523 | allAxesAndConfigs(archVariantLibraryProperties) |
Chris Parsons | a967f25 | 2021-09-23 16:34:35 -0400 | [diff] [blame] | 524 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 525 | compilerAttrs := compilerAttributes{} |
| 526 | linkerAttrs := linkerAttributes{} |
| 527 | |
| 528 | for axis, configs := range axisToConfigs { |
| 529 | for config, _ := range configs { |
| 530 | var allHdrs []string |
| 531 | if baseCompilerProps, ok := archVariantCompilerProps[axis][config].(*BaseCompilerProperties); ok { |
| 532 | allHdrs = baseCompilerProps.Generated_headers |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 533 | if baseCompilerProps.Lex != nil { |
| 534 | compilerAttrs.lexopts.SetSelectValue(axis, config, baseCompilerProps.Lex.Flags) |
| 535 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 536 | (&compilerAttrs).bp2buildForAxisAndConfig(ctx, axis, config, baseCompilerProps) |
| 537 | } |
| 538 | |
| 539 | var exportHdrs []string |
| 540 | |
| 541 | if baseLinkerProps, ok := archVariantLinkerProps[axis][config].(*BaseLinkerProperties); ok { |
| 542 | exportHdrs = baseLinkerProps.Export_generated_headers |
| 543 | |
| 544 | (&linkerAttrs).bp2buildForAxisAndConfig(ctx, module.Binary(), axis, config, baseLinkerProps) |
| 545 | } |
| 546 | headers := maybePartitionExportedAndImplementationsDeps(ctx, !module.Binary(), allHdrs, exportHdrs, android.BazelLabelForModuleDeps) |
| 547 | implementationHdrs.SetSelectValue(axis, config, headers.implementation) |
| 548 | compilerAttrs.hdrs.SetSelectValue(axis, config, headers.export) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 549 | |
| 550 | exportIncludes, exportAbsoluteIncludes := includesFromLabelList(headers.export) |
| 551 | compilerAttrs.includes.Includes.SetSelectValue(axis, config, exportIncludes) |
| 552 | compilerAttrs.includes.AbsoluteIncludes.SetSelectValue(axis, config, exportAbsoluteIncludes) |
| 553 | |
| 554 | includes, absoluteIncludes := includesFromLabelList(headers.implementation) |
| 555 | currAbsoluteIncludes := compilerAttrs.absoluteIncludes.SelectValue(axis, config) |
| 556 | currAbsoluteIncludes = android.FirstUniqueStrings(append(currAbsoluteIncludes, absoluteIncludes...)) |
| 557 | compilerAttrs.absoluteIncludes.SetSelectValue(axis, config, currAbsoluteIncludes) |
| 558 | currIncludes := compilerAttrs.localIncludes.SelectValue(axis, config) |
| 559 | currIncludes = android.FirstUniqueStrings(append(currIncludes, includes...)) |
| 560 | compilerAttrs.localIncludes.SetSelectValue(axis, config, currIncludes) |
Jingwen Chen | 0ee88a6 | 2022-01-07 14:55:29 +0000 | [diff] [blame] | 561 | |
| 562 | if libraryProps, ok := archVariantLibraryProperties[axis][config].(*LibraryProperties); ok { |
| 563 | if axis == bazel.NoConfigAxis { |
| 564 | compilerAttrs.stubsSymbolFile = libraryProps.Stubs.Symbol_file |
| 565 | compilerAttrs.stubsVersions.SetSelectValue(axis, config, libraryProps.Stubs.Versions) |
| 566 | } |
| 567 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | compilerAttrs.convertStlProps(ctx, module) |
| 572 | (&linkerAttrs).convertStripProps(ctx, module) |
| 573 | |
| 574 | productVariableProps := android.ProductVariableProperties(ctx) |
| 575 | |
| 576 | (&compilerAttrs).convertProductVariables(ctx, productVariableProps) |
| 577 | (&linkerAttrs).convertProductVariables(ctx, productVariableProps) |
| 578 | |
| 579 | (&compilerAttrs).finalize(ctx, implementationHdrs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 580 | (&linkerAttrs).finalize(ctx) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 581 | |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 582 | protoDep := bp2buildProto(ctx, module, compilerAttrs.protoSrcs) |
| 583 | |
| 584 | // bp2buildProto will only set wholeStaticLib or implementationWholeStaticLib, but we don't know |
| 585 | // which. This will add the newly generated proto library to the appropriate attribute and nothing |
| 586 | // to the other |
| 587 | (&linkerAttrs).wholeArchiveDeps.Add(protoDep.wholeStaticLib) |
| 588 | (&linkerAttrs).implementationWholeArchiveDeps.Add(protoDep.implementationWholeStaticLib) |
| 589 | |
Trevor Radcliffe | ef9c900 | 2022-05-13 20:55:35 +0000 | [diff] [blame^] | 590 | convertedLSrcs := bp2BuildLex(ctx, module.Name(), compilerAttrs) |
| 591 | (&compilerAttrs).srcs.Add(&convertedLSrcs.srcName) |
| 592 | (&compilerAttrs).cSrcs.Add(&convertedLSrcs.cSrcName) |
| 593 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 594 | return baseAttributes{ |
| 595 | compilerAttrs, |
| 596 | linkerAttrs, |
Liz Kammer | 12615db | 2021-09-28 09:19:17 -0400 | [diff] [blame] | 597 | protoDep.protoDep, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 601 | func bp2BuildParseSdkAttributes(module *Module) sdkAttributes { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 602 | return sdkAttributes{ |
| 603 | Sdk_version: module.Properties.Sdk_version, |
Yu Liu | fc60316 | 2022-03-01 15:44:08 -0800 | [diff] [blame] | 604 | Min_sdk_version: module.Properties.Min_sdk_version, |
| 605 | } |
| 606 | } |
| 607 | |
| 608 | type sdkAttributes struct { |
| 609 | Sdk_version *string |
| 610 | Min_sdk_version *string |
| 611 | } |
| 612 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 613 | // Convenience struct to hold all attributes parsed from linker properties. |
| 614 | type linkerAttributes struct { |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 615 | deps bazel.LabelListAttribute |
| 616 | implementationDeps bazel.LabelListAttribute |
| 617 | dynamicDeps bazel.LabelListAttribute |
| 618 | implementationDynamicDeps bazel.LabelListAttribute |
| 619 | wholeArchiveDeps bazel.LabelListAttribute |
| 620 | implementationWholeArchiveDeps bazel.LabelListAttribute |
| 621 | systemDynamicDeps bazel.LabelListAttribute |
| 622 | usedSystemDynamicDepAsDynamicDep map[string]bool |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 623 | |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 624 | linkCrt bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 625 | useLibcrt bazel.BoolAttribute |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 626 | useVersionLib bazel.BoolAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 627 | linkopts bazel.StringListAttribute |
Liz Kammer | d287118 | 2021-10-04 13:54:37 -0400 | [diff] [blame] | 628 | additionalLinkerInputs bazel.LabelListAttribute |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 629 | stripKeepSymbols bazel.BoolAttribute |
| 630 | stripKeepSymbolsAndDebugFrame bazel.BoolAttribute |
| 631 | stripKeepSymbolsList bazel.StringListAttribute |
| 632 | stripAll bazel.BoolAttribute |
| 633 | stripNone bazel.BoolAttribute |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 634 | features bazel.StringListAttribute |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 635 | } |
| 636 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 637 | var ( |
| 638 | soongSystemSharedLibs = []string{"libc", "libm", "libdl"} |
| 639 | ) |
| 640 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 641 | 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] | 642 | // Use a single variable to capture usage of nocrt in arch variants, so there's only 1 error message for this module |
| 643 | var axisFeatures []string |
Liz Kammer | 7a210ac | 2021-09-22 15:52:58 -0400 | [diff] [blame] | 644 | |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 645 | wholeStaticLibs := android.FirstUniqueStrings(props.Whole_static_libs) |
| 646 | la.wholeArchiveDeps.SetSelectValue(axis, config, bazelLabelForWholeDepsExcludes(ctx, wholeStaticLibs, props.Exclude_static_libs)) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 647 | // Excludes to parallel Soong: |
| 648 | // https://cs.android.com/android/platform/superproject/+/master:build/soong/cc/linker.go;l=247-249;drc=088b53577dde6e40085ffd737a1ae96ad82fc4b0 |
Liz Kammer | cc2c1ef | 2022-03-21 09:03:29 -0400 | [diff] [blame] | 649 | staticLibs := android.FirstUniqueStrings(android.RemoveListFromList(props.Static_libs, wholeStaticLibs)) |
| 650 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 651 | 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] | 652 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 653 | headerLibs := android.FirstUniqueStrings(props.Header_libs) |
| 654 | hDeps := maybePartitionExportedAndImplementationsDeps(ctx, !isBinary, headerLibs, props.Export_header_lib_headers, bazelLabelForHeaderDeps) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 655 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 656 | (&hDeps.export).Append(staticDeps.export) |
| 657 | la.deps.SetSelectValue(axis, config, hDeps.export) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 658 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 659 | (&hDeps.implementation).Append(staticDeps.implementation) |
| 660 | la.implementationDeps.SetSelectValue(axis, config, hDeps.implementation) |
Liz Kammer | 0eae52e | 2021-10-06 10:32:26 -0400 | [diff] [blame] | 661 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 662 | systemSharedLibs := props.System_shared_libs |
| 663 | // systemSharedLibs distinguishes between nil/empty list behavior: |
| 664 | // nil -> use default values |
| 665 | // empty list -> no values specified |
| 666 | if len(systemSharedLibs) > 0 { |
| 667 | systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs) |
| 668 | } |
| 669 | la.systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs)) |
| 670 | |
| 671 | sharedLibs := android.FirstUniqueStrings(props.Shared_libs) |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 672 | excludeSharedLibs := props.Exclude_shared_libs |
| 673 | usedSystem := android.FilterListPred(sharedLibs, func(s string) bool { |
| 674 | return android.InList(s, soongSystemSharedLibs) && !android.InList(s, excludeSharedLibs) |
| 675 | }) |
| 676 | for _, el := range usedSystem { |
| 677 | if la.usedSystemDynamicDepAsDynamicDep == nil { |
| 678 | la.usedSystemDynamicDepAsDynamicDep = map[string]bool{} |
| 679 | } |
| 680 | la.usedSystemDynamicDepAsDynamicDep[el] = true |
| 681 | } |
| 682 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 683 | sharedDeps := maybePartitionExportedAndImplementationsDepsExcludes(ctx, !isBinary, sharedLibs, props.Exclude_shared_libs, props.Export_shared_lib_headers, bazelLabelForSharedDepsExcludes) |
| 684 | la.dynamicDeps.SetSelectValue(axis, config, sharedDeps.export) |
| 685 | la.implementationDynamicDeps.SetSelectValue(axis, config, sharedDeps.implementation) |
| 686 | |
| 687 | if !BoolDefault(props.Pack_relocations, packRelocationsDefault) { |
| 688 | axisFeatures = append(axisFeatures, "disable_pack_relocations") |
| 689 | } |
| 690 | |
| 691 | if Bool(props.Allow_undefined_symbols) { |
| 692 | axisFeatures = append(axisFeatures, "-no_undefined_symbols") |
| 693 | } |
| 694 | |
| 695 | var linkerFlags []string |
| 696 | if len(props.Ldflags) > 0 { |
Liz Kammer | f38a837 | 2022-02-04 15:39:00 -0500 | [diff] [blame] | 697 | linkerFlags = append(linkerFlags, proptools.NinjaEscapeList(props.Ldflags)...) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 698 | // binaries remove static flag if -shared is in the linker flags |
| 699 | if isBinary && android.InList("-shared", linkerFlags) { |
| 700 | axisFeatures = append(axisFeatures, "-static_flag") |
| 701 | } |
| 702 | } |
| 703 | if props.Version_script != nil { |
| 704 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Version_script) |
| 705 | la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 706 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--version-script,$(location %s)", label.Label)) |
| 707 | } |
Alix | 773adaa | 2022-04-27 17:49:34 +0000 | [diff] [blame] | 708 | |
| 709 | if props.Dynamic_list != nil { |
| 710 | label := android.BazelLabelForModuleSrcSingle(ctx, *props.Dynamic_list) |
| 711 | la.additionalLinkerInputs.SetSelectValue(axis, config, bazel.LabelList{Includes: []bazel.Label{label}}) |
| 712 | linkerFlags = append(linkerFlags, fmt.Sprintf("-Wl,--dynamic-list,$(location %s)", label.Label)) |
| 713 | } |
| 714 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 715 | la.linkopts.SetSelectValue(axis, config, linkerFlags) |
| 716 | la.useLibcrt.SetSelectValue(axis, config, props.libCrt()) |
| 717 | |
Rupert Shuttleworth | 484aa25 | 2021-12-10 07:22:53 -0500 | [diff] [blame] | 718 | if axis == bazel.NoConfigAxis { |
| 719 | la.useVersionLib.SetSelectValue(axis, config, props.Use_version_lib) |
| 720 | } |
| 721 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 722 | // it's very unlikely for nocrt to be arch variant, so bp2build doesn't support it. |
| 723 | if props.crt() != nil { |
| 724 | if axis == bazel.NoConfigAxis { |
| 725 | la.linkCrt.SetSelectValue(axis, config, props.crt()) |
| 726 | } else if axis == bazel.ArchConfigurationAxis { |
| 727 | ctx.ModuleErrorf("nocrt is not supported for arch variants") |
| 728 | } |
| 729 | } |
| 730 | |
| 731 | if axisFeatures != nil { |
| 732 | la.features.SetSelectValue(axis, config, axisFeatures) |
| 733 | } |
| 734 | } |
| 735 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 736 | func (la *linkerAttributes) convertStripProps(ctx android.BazelConversionPathContext, module *Module) { |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 737 | bp2BuildPropParseHelper(ctx, module, &StripProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 738 | if stripProperties, ok := props.(*StripProperties); ok { |
| 739 | la.stripKeepSymbols.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols) |
| 740 | la.stripKeepSymbolsList.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_list) |
| 741 | la.stripKeepSymbolsAndDebugFrame.SetSelectValue(axis, config, stripProperties.Strip.Keep_symbols_and_debug_frame) |
| 742 | la.stripAll.SetSelectValue(axis, config, stripProperties.Strip.All) |
| 743 | la.stripNone.SetSelectValue(axis, config, stripProperties.Strip.None) |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 744 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 745 | }) |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 746 | } |
Jingwen Chen | 3d383bb | 2021-06-09 07:18:37 +0000 | [diff] [blame] | 747 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 748 | func (la *linkerAttributes) convertProductVariables(ctx android.BazelConversionPathContext, productVariableProps android.ProductConfigProperties) { |
Jingwen Chen | 6ada589 | 2021-09-17 11:38:09 +0000 | [diff] [blame] | 749 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 750 | type productVarDep struct { |
| 751 | // the name of the corresponding excludes field, if one exists |
| 752 | excludesField string |
| 753 | // reference to the bazel attribute that should be set for the given product variable config |
| 754 | attribute *bazel.LabelListAttribute |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 755 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 756 | depResolutionFunc func(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | productVarToDepFields := map[string]productVarDep{ |
| 760 | // product variables do not support exclude_shared_libs |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 761 | "Shared_libs": {attribute: &la.implementationDynamicDeps, depResolutionFunc: bazelLabelForSharedDepsExcludes}, |
| 762 | "Static_libs": {"Exclude_static_libs", &la.implementationDeps, bazelLabelForStaticDepsExcludes}, |
| 763 | "Whole_static_libs": {"Exclude_static_libs", &la.wholeArchiveDeps, bazelLabelForWholeDepsExcludes}, |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 764 | } |
| 765 | |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 766 | for name, dep := range productVarToDepFields { |
| 767 | props, exists := productVariableProps[name] |
| 768 | excludeProps, excludesExists := productVariableProps[dep.excludesField] |
| 769 | // if neither an include or excludes property exists, then skip it |
| 770 | if !exists && !excludesExists { |
| 771 | continue |
| 772 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 773 | // Collect all the configurations that an include or exclude property exists for. |
| 774 | // We want to iterate all configurations rather than either the include or exclude because, for a |
| 775 | // particular configuration, we may have either only an include or an exclude to handle. |
| 776 | productConfigProps := make(map[android.ProductConfigProperty]bool, len(props)+len(excludeProps)) |
| 777 | for p := range props { |
| 778 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 779 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 780 | for p := range excludeProps { |
| 781 | productConfigProps[p] = true |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 782 | } |
| 783 | |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 784 | for productConfigProp := range productConfigProps { |
| 785 | prop, includesExists := props[productConfigProp] |
| 786 | excludesProp, excludesExists := excludeProps[productConfigProp] |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 787 | var includes, excludes []string |
| 788 | var ok bool |
| 789 | // 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] | 790 | if includes, ok = prop.([]string); includesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 791 | ctx.ModuleErrorf("Could not convert product variable %s property", name) |
| 792 | } |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 793 | if excludes, ok = excludesProp.([]string); excludesExists && !ok { |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 794 | ctx.ModuleErrorf("Could not convert product variable %s property", dep.excludesField) |
| 795 | } |
Liz Kammer | 2d7bbe3 | 2021-06-10 18:20:06 -0400 | [diff] [blame] | 796 | |
Jingwen Chen | 58ff680 | 2021-11-17 12:14:41 +0000 | [diff] [blame] | 797 | dep.attribute.EmitEmptyList = productConfigProp.AlwaysEmit() |
Jingwen Chen | 25825ca | 2021-11-15 12:28:43 +0000 | [diff] [blame] | 798 | dep.attribute.SetSelectValue( |
| 799 | productConfigProp.ConfigurationAxis(), |
| 800 | productConfigProp.SelectKey(), |
| 801 | dep.depResolutionFunc(ctx, android.FirstUniqueStrings(includes), excludes), |
| 802 | ) |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 803 | } |
| 804 | } |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 805 | } |
Liz Kammer | 47535c5 | 2021-06-02 16:02:22 -0400 | [diff] [blame] | 806 | |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 807 | func (la *linkerAttributes) finalize(ctx android.BazelConversionPathContext) { |
| 808 | // if system dynamic deps have the default value, any use of a system dynamic library used will |
| 809 | // result in duplicate library errors for bionic OSes. Here, we explicitly exclude those libraries |
| 810 | // from bionic OSes. |
| 811 | if la.systemDynamicDeps.IsNil() && len(la.usedSystemDynamicDepAsDynamicDep) > 0 { |
| 812 | toRemove := bazelLabelForSharedDeps(ctx, android.SortedStringKeys(la.usedSystemDynamicDepAsDynamicDep)) |
| 813 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 814 | la.dynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
| 815 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "android", toRemove) |
| 816 | la.implementationDynamicDeps.Exclude(bazel.OsConfigurationAxis, "linux_bionic", toRemove) |
| 817 | } |
| 818 | |
Liz Kammer | e658348 | 2021-10-19 13:56:10 -0400 | [diff] [blame] | 819 | la.deps.ResolveExcludes() |
| 820 | la.implementationDeps.ResolveExcludes() |
| 821 | la.dynamicDeps.ResolveExcludes() |
| 822 | la.implementationDynamicDeps.ResolveExcludes() |
| 823 | la.wholeArchiveDeps.ResolveExcludes() |
| 824 | la.systemDynamicDeps.ForceSpecifyEmptyList = true |
Liz Kammer | 5430953 | 2021-12-14 12:21:22 -0500 | [diff] [blame] | 825 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 826 | } |
| 827 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 828 | // Relativize a list of root-relative paths with respect to the module's |
| 829 | // directory. |
| 830 | // |
| 831 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 832 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 833 | // module dir relative. This function makes a list of paths entirely module dir |
| 834 | // relative. |
| 835 | // |
| 836 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 837 | // module. |
| 838 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 839 | var relativePaths []string |
| 840 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 841 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 842 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 843 | if err != nil { |
| 844 | panic(err) |
| 845 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 846 | relativePaths = append(relativePaths, relativePath) |
| 847 | } |
| 848 | return relativePaths |
| 849 | } |
| 850 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 851 | // BazelIncludes contains information about -I and -isystem paths from a module converted to Bazel |
| 852 | // attributes. |
| 853 | type BazelIncludes struct { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 854 | AbsoluteIncludes bazel.StringListAttribute |
| 855 | Includes bazel.StringListAttribute |
| 856 | SystemIncludes bazel.StringListAttribute |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 857 | } |
| 858 | |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 859 | func bp2BuildParseExportedIncludes(ctx android.BazelConversionPathContext, module *Module, includes *BazelIncludes) BazelIncludes { |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 860 | var exported BazelIncludes |
| 861 | if includes != nil { |
| 862 | exported = *includes |
| 863 | } else { |
| 864 | exported = BazelIncludes{} |
| 865 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 866 | bp2BuildPropParseHelper(ctx, module, &FlagExporterProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 867 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
| 868 | if len(flagExporterProperties.Export_include_dirs) > 0 { |
| 869 | exported.Includes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.Includes.SelectValue(axis, config), flagExporterProperties.Export_include_dirs...))) |
| 870 | } |
| 871 | if len(flagExporterProperties.Export_system_include_dirs) > 0 { |
| 872 | exported.SystemIncludes.SetSelectValue(axis, config, android.FirstUniqueStrings(append(exported.SystemIncludes.SelectValue(axis, config), flagExporterProperties.Export_system_include_dirs...))) |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 873 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 874 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 875 | }) |
Liz Kammer | 1263d9b | 2021-12-10 14:28:20 -0500 | [diff] [blame] | 876 | exported.AbsoluteIncludes.DeduplicateAxesFromBase() |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 877 | exported.Includes.DeduplicateAxesFromBase() |
| 878 | exported.SystemIncludes.DeduplicateAxesFromBase() |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 879 | |
Liz Kammer | 5fad501 | 2021-09-09 14:08:21 -0400 | [diff] [blame] | 880 | return exported |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 881 | } |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 882 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 883 | func bazelLabelForStaticModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 884 | label := android.BazelModuleLabel(ctx, m) |
Liz Kammer | 35ca77e | 2021-12-22 15:31:40 -0500 | [diff] [blame] | 885 | if ccModule, ok := m.(*Module); ok && ccModule.typ() == fullLibrary && !android.GenerateCcLibraryStaticOnly(m.Name()) { |
| 886 | label += "_bp2build_cc_library_static" |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 887 | } |
| 888 | return label |
| 889 | } |
| 890 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 891 | func bazelLabelForSharedModule(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 892 | // cc_library, at it's root name, propagates the shared library, which depends on the static |
| 893 | // library. |
| 894 | return android.BazelModuleLabel(ctx, m) |
| 895 | } |
| 896 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 897 | func bazelLabelForStaticWholeModuleDeps(ctx android.BazelConversionPathContext, m blueprint.Module) string { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 898 | label := bazelLabelForStaticModule(ctx, m) |
| 899 | if aModule, ok := m.(android.Module); ok { |
| 900 | if android.IsModulePrebuilt(aModule) { |
| 901 | label += "_alwayslink" |
| 902 | } |
| 903 | } |
| 904 | return label |
| 905 | } |
| 906 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 907 | func bazelLabelForWholeDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 908 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticWholeModuleDeps) |
| 909 | } |
| 910 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 911 | func bazelLabelForWholeDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 912 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticWholeModuleDeps) |
| 913 | } |
| 914 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 915 | func bazelLabelForStaticDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 916 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForStaticModule) |
| 917 | } |
| 918 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 919 | func bazelLabelForStaticDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 920 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForStaticModule) |
| 921 | } |
| 922 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 923 | func bazelLabelForSharedDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 924 | return android.BazelLabelForModuleDepsWithFn(ctx, modules, bazelLabelForSharedModule) |
| 925 | } |
| 926 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 927 | func bazelLabelForHeaderDeps(ctx android.BazelConversionPathContext, modules []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 928 | // This is not elegant, but bp2build's shared library targets only propagate |
| 929 | // their header information as part of the normal C++ provider. |
| 930 | return bazelLabelForSharedDeps(ctx, modules) |
| 931 | } |
| 932 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 933 | func bazelLabelForSharedDepsExcludes(ctx android.BazelConversionPathContext, modules, excludes []string) bazel.LabelList { |
Chris Parsons | 953b356 | 2021-09-20 15:14:39 -0400 | [diff] [blame] | 934 | return android.BazelLabelForModuleDepsExcludesWithFn(ctx, modules, excludes, bazelLabelForSharedModule) |
| 935 | } |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 936 | |
| 937 | type binaryLinkerAttrs struct { |
| 938 | Linkshared *bool |
| 939 | } |
| 940 | |
Jingwen Chen | 55bc820 | 2021-11-02 06:40:51 +0000 | [diff] [blame] | 941 | func bp2buildBinaryLinkerProps(ctx android.BazelConversionPathContext, m *Module) binaryLinkerAttrs { |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 942 | attrs := binaryLinkerAttrs{} |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 943 | bp2BuildPropParseHelper(ctx, m, &BinaryLinkerProperties{}, func(axis bazel.ConfigurationAxis, config string, props interface{}) { |
| 944 | linkerProps := props.(*BinaryLinkerProperties) |
| 945 | staticExecutable := linkerProps.Static_executable |
| 946 | if axis == bazel.NoConfigAxis { |
| 947 | if linkBinaryShared := !proptools.Bool(staticExecutable); !linkBinaryShared { |
| 948 | attrs.Linkshared = &linkBinaryShared |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 949 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 950 | } else if staticExecutable != nil { |
| 951 | // TODO(b/202876379): Static_executable is arch-variant; however, linkshared is a |
| 952 | // nonconfigurable attribute. Only 4 AOSP modules use this feature, defer handling |
| 953 | ctx.ModuleErrorf("bp2build cannot migrate a module with arch/target-specific static_executable values") |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 954 | } |
Trevor Radcliffe | 542954f | 2022-04-21 20:04:42 +0000 | [diff] [blame] | 955 | }) |
Liz Kammer | 2b8004b | 2021-10-04 13:55:44 -0400 | [diff] [blame] | 956 | |
| 957 | return attrs |
| 958 | } |