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