Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 1 | // Copyright 2021 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package cc |
| 15 | |
| 16 | import ( |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 17 | "path/filepath" |
Jingwen Chen | 3950cd6 | 2021-05-12 04:33:00 +0000 | [diff] [blame] | 18 | "strings" |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
| 21 | "android/soong/bazel" |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // bp2build functions and helpers for converting cc_* modules to Bazel. |
| 25 | |
| 26 | func init() { |
| 27 | android.DepsBp2BuildMutators(RegisterDepsBp2Build) |
| 28 | } |
| 29 | |
| 30 | func RegisterDepsBp2Build(ctx android.RegisterMutatorsContext) { |
| 31 | ctx.BottomUp("cc_bp2build_deps", depsBp2BuildMutator) |
| 32 | } |
| 33 | |
| 34 | // A naive deps mutator to add deps on all modules across all combinations of |
| 35 | // target props for cc modules. This is needed to make module -> bazel label |
| 36 | // resolution work in the bp2build mutator later. This is probably |
| 37 | // the wrong way to do it, but it works. |
| 38 | // |
| 39 | // TODO(jingwen): can we create a custom os mutator in depsBp2BuildMutator to do this? |
| 40 | func depsBp2BuildMutator(ctx android.BottomUpMutatorContext) { |
| 41 | module, ok := ctx.Module().(*Module) |
| 42 | if !ok { |
| 43 | // Not a cc module |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | if !module.ConvertWithBp2build(ctx) { |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | var allDeps []string |
| 52 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 53 | for _, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
| 54 | // os base compiler props |
| 55 | if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 56 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 57 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 58 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 59 | // os + arch base compiler props |
| 60 | for _, archProps := range osProps.ArchProperties { |
| 61 | if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok { |
| 62 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 63 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 64 | } |
| 65 | } |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 66 | } |
| 67 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 68 | for _, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 69 | // arch specific compiler props |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 70 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 71 | allDeps = append(allDeps, baseCompilerProps.Generated_headers...) |
| 72 | allDeps = append(allDeps, baseCompilerProps.Generated_sources...) |
| 73 | } |
| 74 | } |
| 75 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 76 | for _, osProps := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
| 77 | // os specific linker props |
| 78 | if baseLinkerProps, ok := osProps.Properties.(*BaseLinkerProperties); ok { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 79 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 80 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 81 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 82 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 83 | } |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 84 | // os + arch base compiler props |
| 85 | for _, archProps := range osProps.ArchProperties { |
| 86 | if baseLinkerProps, ok := archProps.(*BaseLinkerProperties); ok { |
| 87 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 88 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 89 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 90 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
| 91 | } |
| 92 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 95 | for _, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 96 | // arch specific linker props |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 97 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 98 | allDeps = append(allDeps, baseLinkerProps.Header_libs...) |
| 99 | allDeps = append(allDeps, baseLinkerProps.Export_header_lib_headers...) |
| 100 | allDeps = append(allDeps, baseLinkerProps.Static_libs...) |
| 101 | allDeps = append(allDeps, baseLinkerProps.Whole_static_libs...) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 105 | // Deps in the static: { .. } and shared: { .. } props of a cc_library. |
| 106 | if lib, ok := module.compiler.(*libraryDecorator); ok { |
| 107 | allDeps = append(allDeps, lib.SharedProperties.Shared.Static_libs...) |
| 108 | allDeps = append(allDeps, lib.SharedProperties.Shared.Whole_static_libs...) |
| 109 | allDeps = append(allDeps, lib.SharedProperties.Shared.Shared_libs...) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 110 | |
| 111 | allDeps = append(allDeps, lib.StaticProperties.Static.Static_libs...) |
| 112 | allDeps = append(allDeps, lib.StaticProperties.Static.Whole_static_libs...) |
| 113 | allDeps = append(allDeps, lib.StaticProperties.Static.Shared_libs...) |
Jingwen Chen | 45dec10 | 2021-05-19 10:30:29 +0000 | [diff] [blame] | 114 | |
| 115 | // TODO(b/186024507, b/186489250): Temporarily exclude adding |
| 116 | // system_shared_libs deps until libc and libm builds. |
| 117 | // allDeps = append(allDeps, lib.SharedProperties.Shared.System_shared_libs...) |
| 118 | // allDeps = append(allDeps, lib.StaticProperties.Static.System_shared_libs...) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 121 | ctx.AddDependency(module, nil, android.SortedUniqueStrings(allDeps)...) |
| 122 | } |
| 123 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 124 | // staticOrSharedAttributes are the Bazel-ified versions of StaticOrSharedProperties -- |
| 125 | // properities which apply to either the shared or static version of a cc_library module. |
| 126 | type staticOrSharedAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 127 | copts bazel.StringListAttribute |
| 128 | srcs bazel.LabelListAttribute |
| 129 | staticDeps bazel.LabelListAttribute |
| 130 | dynamicDeps bazel.LabelListAttribute |
| 131 | wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // bp2buildParseSharedProps returns the attributes for the shared variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 135 | func bp2BuildParseSharedProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 136 | lib, ok := module.compiler.(*libraryDecorator) |
| 137 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 138 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 139 | } |
| 140 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 141 | return bp2buildParseStaticOrSharedProps(ctx, lib.SharedProperties.Shared) |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | // bp2buildParseStaticProps returns the attributes for the static variant of a cc_library. |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 145 | func bp2BuildParseStaticProps(ctx android.TopDownMutatorContext, module *Module) staticOrSharedAttributes { |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 146 | lib, ok := module.compiler.(*libraryDecorator) |
| 147 | if !ok { |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 148 | return staticOrSharedAttributes{} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 151 | return bp2buildParseStaticOrSharedProps(ctx, lib.StaticProperties.Static) |
| 152 | } |
| 153 | |
| 154 | func bp2buildParseStaticOrSharedProps(ctx android.TopDownMutatorContext, props StaticOrSharedProperties) staticOrSharedAttributes { |
| 155 | copts := bazel.StringListAttribute{Value: props.Cflags} |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 156 | |
| 157 | srcs := bazel.LabelListAttribute{ |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 158 | Value: android.BazelLabelForModuleSrc(ctx, props.Srcs)} |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 159 | |
| 160 | staticDeps := bazel.LabelListAttribute{ |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 161 | Value: android.BazelLabelForModuleDeps(ctx, props.Static_libs)} |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 162 | |
| 163 | dynamicDeps := bazel.LabelListAttribute{ |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 164 | Value: android.BazelLabelForModuleDeps(ctx, props.Shared_libs)} |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 165 | |
| 166 | wholeArchiveDeps := bazel.LabelListAttribute{ |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 167 | Value: android.BazelLabelForModuleDeps(ctx, props.Whole_static_libs)} |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 168 | |
Liz Kammer | 2222c6b | 2021-05-24 15:41:47 -0400 | [diff] [blame] | 169 | return staticOrSharedAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 170 | copts: copts, |
| 171 | srcs: srcs, |
| 172 | staticDeps: staticDeps, |
| 173 | dynamicDeps: dynamicDeps, |
| 174 | wholeArchiveDeps: wholeArchiveDeps, |
Jingwen Chen | 53681ef | 2021-04-29 08:15:13 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 178 | // Convenience struct to hold all attributes parsed from compiler properties. |
| 179 | type compilerAttributes struct { |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 180 | // Options for all languages |
| 181 | copts bazel.StringListAttribute |
| 182 | // Assembly options and sources |
| 183 | asFlags bazel.StringListAttribute |
| 184 | asSrcs bazel.LabelListAttribute |
| 185 | // C options and sources |
| 186 | conlyFlags bazel.StringListAttribute |
| 187 | cSrcs bazel.LabelListAttribute |
| 188 | // C++ options and sources |
| 189 | cppFlags bazel.StringListAttribute |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 190 | srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 193 | // bp2BuildParseCompilerProps returns copts, srcs and hdrs and other attributes. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 194 | func bp2BuildParseCompilerProps(ctx android.TopDownMutatorContext, module *Module) compilerAttributes { |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 195 | var srcs bazel.LabelListAttribute |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 196 | var copts bazel.StringListAttribute |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 197 | var asFlags bazel.StringListAttribute |
| 198 | var conlyFlags bazel.StringListAttribute |
| 199 | var cppFlags bazel.StringListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 200 | |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 201 | // Creates the -I flags for a directory, while making the directory relative |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 202 | // to the exec root for Bazel to work. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 203 | includeFlags := func(dir string) []string { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 204 | // filepath.Join canonicalizes the path, i.e. it takes care of . or .. elements. |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 205 | moduleDirRootedPath := filepath.Join(ctx.ModuleDir(), dir) |
| 206 | return []string{ |
| 207 | "-I" + moduleDirRootedPath, |
| 208 | // Include the bindir-rooted path (using make variable substitution). This most |
| 209 | // closely matches Bazel's native include path handling, which allows for dependency |
| 210 | // on generated headers in these directories. |
| 211 | // TODO(b/188084383): Handle local include directories in Bazel. |
| 212 | "-I$(BINDIR)/" + moduleDirRootedPath, |
| 213 | } |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 216 | // Parse the list of module-relative include directories (-I). |
| 217 | parseLocalIncludeDirs := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 218 | // include_dirs are root-relative, not module-relative. |
| 219 | includeDirs := bp2BuildMakePathsRelativeToModule(ctx, baseCompilerProps.Include_dirs) |
| 220 | return append(includeDirs, baseCompilerProps.Local_include_dirs...) |
| 221 | } |
| 222 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 223 | parseCommandLineFlags := func(soongFlags []string) []string { |
| 224 | var result []string |
| 225 | for _, flag := range soongFlags { |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 226 | // Soong's cflags can contain spaces, like `-include header.h`. For |
| 227 | // Bazel's copts, split them up to be compatible with the |
| 228 | // no_copts_tokenization feature. |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 229 | result = append(result, strings.Split(flag, " ")...) |
Colin Cross | 52aa4e1 | 2021-05-25 15:20:39 +0000 | [diff] [blame] | 230 | } |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 231 | return result |
| 232 | } |
| 233 | |
| 234 | // Parse the list of copts. |
| 235 | parseCopts := func(baseCompilerProps *BaseCompilerProperties) []string { |
| 236 | var copts []string |
| 237 | copts = append(copts, parseCommandLineFlags(baseCompilerProps.Cflags)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 238 | for _, dir := range parseLocalIncludeDirs(baseCompilerProps) { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 239 | copts = append(copts, includeFlags(dir)...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 240 | } |
| 241 | return copts |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 244 | // baseSrcs contain the list of src files that are used for every configuration. |
| 245 | var baseSrcs []string |
| 246 | // baseExcludeSrcs contain the list of src files that are excluded for every configuration. |
| 247 | var baseExcludeSrcs []string |
| 248 | // baseSrcsLabelList is a clone of the base srcs LabelList, used for computing the |
| 249 | // arch or os specific srcs later. |
| 250 | var baseSrcsLabelList bazel.LabelList |
| 251 | |
| 252 | // Parse srcs from an arch or OS's props value, taking the base srcs and |
| 253 | // exclude srcs into account. |
| 254 | parseSrcs := func(baseCompilerProps *BaseCompilerProperties) bazel.LabelList { |
| 255 | // Combine the base srcs and arch-specific srcs |
| 256 | allSrcs := append(baseSrcs, baseCompilerProps.Srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 257 | // Add srcs-like dependencies such as generated files. |
| 258 | // First create a LabelList containing these dependencies, then merge the values with srcs. |
| 259 | generatedHdrsAndSrcs := baseCompilerProps.Generated_headers |
| 260 | generatedHdrsAndSrcs = append(generatedHdrsAndSrcs, baseCompilerProps.Generated_sources...) |
| 261 | |
| 262 | generatedHdrsAndSrcsLabelList := android.BazelLabelForModuleDeps(ctx, generatedHdrsAndSrcs) |
| 263 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 264 | // Combine the base exclude_srcs and configuration-specific exclude_srcs |
| 265 | allExcludeSrcs := append(baseExcludeSrcs, baseCompilerProps.Exclude_srcs...) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 266 | allSrcsLabelList := android.BazelLabelForModuleSrcExcludes(ctx, allSrcs, allExcludeSrcs) |
| 267 | return bazel.AppendBazelLabelLists(allSrcsLabelList, generatedHdrsAndSrcsLabelList) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 270 | for _, props := range module.compiler.compilerProps() { |
| 271 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 272 | srcs.Value = parseSrcs(baseCompilerProps) |
| 273 | copts.Value = parseCopts(baseCompilerProps) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 274 | asFlags.Value = parseCommandLineFlags(baseCompilerProps.Asflags) |
| 275 | conlyFlags.Value = parseCommandLineFlags(baseCompilerProps.Conlyflags) |
| 276 | cppFlags.Value = parseCommandLineFlags(baseCompilerProps.Cppflags) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 277 | |
| 278 | // Used for arch-specific srcs later. |
| 279 | baseSrcs = baseCompilerProps.Srcs |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 280 | baseSrcsLabelList = parseSrcs(baseCompilerProps) |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 281 | baseExcludeSrcs = baseCompilerProps.Exclude_srcs |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 282 | break |
| 283 | } |
| 284 | } |
| 285 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 286 | // Handle include_build_directory prop. If the property is true, then the |
| 287 | // target has access to all headers recursively in the package, and has |
| 288 | // "-I<module-dir>" in its copts. |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 289 | if c, ok := module.compiler.(*baseCompiler); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 290 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 291 | } else if c, ok := module.compiler.(*libraryDecorator); ok && c.includeBuildDirectory() { |
Chris Parsons | 484e50a | 2021-05-13 15:13:04 -0400 | [diff] [blame] | 292 | copts.Value = append(copts.Value, includeFlags(".")...) |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 295 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 296 | if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 297 | // If there's arch specific srcs or exclude_srcs, generate a select entry for it. |
| 298 | // TODO(b/186153868): do this for OS specific srcs and exclude_srcs too. |
| 299 | if len(baseCompilerProps.Srcs) > 0 || len(baseCompilerProps.Exclude_srcs) > 0 { |
| 300 | srcsList := parseSrcs(baseCompilerProps) |
| 301 | srcs.SetValueForArch(arch.Name, srcsList) |
| 302 | // The base srcs value should not contain any arch-specific excludes. |
| 303 | srcs.Value = bazel.SubtractBazelLabelList(srcs.Value, bazel.LabelList{Includes: srcsList.Excludes}) |
| 304 | } |
| 305 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 306 | copts.SetValueForArch(arch.Name, parseCopts(baseCompilerProps)) |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 307 | asFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 308 | conlyFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 309 | cppFlags.SetValueForArch(arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 313 | // After going through all archs, delete the duplicate files in the arch |
| 314 | // values that are already in the base srcs.Value. |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 315 | for arch, props := range module.GetArchProperties(ctx, &BaseCompilerProperties{}) { |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 316 | if _, ok := props.(*BaseCompilerProperties); ok { |
| 317 | srcs.SetValueForArch(arch.Name, bazel.SubtractBazelLabelList(srcs.GetValueForArch(arch.Name), srcs.Value)) |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | // Now that the srcs.Value list is finalized, compare it with the original |
| 322 | // list, and put the difference into the default condition for the arch |
| 323 | // select. |
| 324 | defaultsSrcs := bazel.SubtractBazelLabelList(baseSrcsLabelList, srcs.Value) |
| 325 | // TODO(b/186153868): handle the case with multiple variant types, e.g. when arch and os are both used. |
| 326 | srcs.SetValueForArch(bazel.CONDITIONS_DEFAULT, defaultsSrcs) |
| 327 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 328 | // Handle target specific properties. |
| 329 | for os, osProps := range module.GetTargetProperties(ctx, &BaseCompilerProperties{}) { |
| 330 | if baseCompilerProps, ok := osProps.Properties.(*BaseCompilerProperties); ok { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 331 | srcsList := parseSrcs(baseCompilerProps) |
Jingwen Chen | e32e9e0 | 2021-04-23 09:17:24 +0000 | [diff] [blame] | 332 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 333 | srcs.SetOsValueForTarget(os.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
| 334 | copts.SetOsValueForTarget(os.Name, parseCopts(baseCompilerProps)) |
| 335 | asFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 336 | conlyFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 337 | cppFlags.SetOsValueForTarget(os.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 338 | } |
| 339 | for arch, archProps := range osProps.ArchProperties { |
| 340 | if baseCompilerProps, ok := archProps.(*BaseCompilerProperties); ok { |
| 341 | srcsList := parseSrcs(baseCompilerProps) |
| 342 | // TODO(b/186153868): add support for os-specific srcs and exclude_srcs |
| 343 | srcs.SetOsArchValueForTarget(os.Name, arch.Name, bazel.SubtractBazelLabelList(srcsList, baseSrcsLabelList)) |
| 344 | copts.SetOsArchValueForTarget(os.Name, arch.Name, parseCopts(baseCompilerProps)) |
| 345 | asFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Asflags)) |
| 346 | conlyFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Conlyflags)) |
| 347 | cppFlags.SetOsArchValueForTarget(os.Name, arch.Name, parseCommandLineFlags(baseCompilerProps.Cppflags)) |
| 348 | } |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 352 | productVariableProps := android.ProductVariableProperties(ctx) |
| 353 | if props, exists := productVariableProps["Cflags"]; exists { |
| 354 | for _, prop := range props { |
| 355 | flags, ok := prop.Property.([]string) |
| 356 | if !ok { |
| 357 | ctx.ModuleErrorf("Could not convert product variable cflag property") |
| 358 | } |
| 359 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
| 360 | copts.ProductValues = append(copts.ProductValues, bazel.ProductVariableValues{ |
| 361 | ProductVariable: prop.ProductConfigVariable, |
| 362 | Values: newFlags, |
| 363 | }) |
| 364 | } |
| 365 | } |
| 366 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 367 | // Branch srcs into three language-specific groups. |
| 368 | // C++ is the "catch-all" group, and comprises generated sources because we don't |
| 369 | // know the language of these sources until the genrule is executed. |
| 370 | // TODO(b/): Handle language detection of sources in a Bazel rule. |
| 371 | isCSrc := func(s string) bool { |
| 372 | return strings.HasSuffix(s, ".c") |
| 373 | } |
| 374 | isAsmSrc := func(s string) bool { |
| 375 | return strings.HasSuffix(s, ".S") || strings.HasSuffix(s, ".s") |
| 376 | } |
| 377 | cSrcs := bazel.FilterLabelListAttribute(srcs, isCSrc) |
| 378 | asSrcs := bazel.FilterLabelListAttribute(srcs, isAsmSrc) |
| 379 | srcs = bazel.SubtractBazelLabelListAttribute(srcs, cSrcs) |
| 380 | srcs = bazel.SubtractBazelLabelListAttribute(srcs, asSrcs) |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 381 | return compilerAttributes{ |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame] | 382 | copts: copts, |
| 383 | srcs: srcs, |
| 384 | asFlags: asFlags, |
| 385 | asSrcs: asSrcs, |
| 386 | cSrcs: cSrcs, |
| 387 | conlyFlags: conlyFlags, |
| 388 | cppFlags: cppFlags, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | // Convenience struct to hold all attributes parsed from linker properties. |
| 393 | type linkerAttributes struct { |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 394 | deps bazel.LabelListAttribute |
| 395 | dynamicDeps bazel.LabelListAttribute |
| 396 | wholeArchiveDeps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 397 | exportedDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 398 | linkopts bazel.StringListAttribute |
| 399 | versionScript bazel.LabelAttribute |
Jingwen Chen | c1c2650 | 2021-04-05 10:35:13 +0000 | [diff] [blame] | 400 | } |
| 401 | |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 402 | // FIXME(b/187655838): Use the existing linkerFlags() function instead of duplicating logic here |
| 403 | func getBp2BuildLinkerFlags(linkerProperties *BaseLinkerProperties) []string { |
| 404 | flags := linkerProperties.Ldflags |
| 405 | if !BoolDefault(linkerProperties.Pack_relocations, true) { |
| 406 | flags = append(flags, "-Wl,--pack-dyn-relocs=none") |
| 407 | } |
| 408 | return flags |
| 409 | } |
| 410 | |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 411 | // bp2BuildParseLinkerProps parses the linker properties of a module, including |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 412 | // configurable attribute values. |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 413 | func bp2BuildParseLinkerProps(ctx android.TopDownMutatorContext, module *Module) linkerAttributes { |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 414 | var deps bazel.LabelListAttribute |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 415 | var exportedDeps bazel.LabelListAttribute |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 416 | var dynamicDeps bazel.LabelListAttribute |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 417 | var wholeArchiveDeps bazel.LabelListAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 418 | var linkopts bazel.StringListAttribute |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 419 | var versionScript bazel.LabelAttribute |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 420 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 421 | getLibs := func(baseLinkerProps *BaseLinkerProperties) []string { |
| 422 | libs := baseLinkerProps.Header_libs |
| 423 | libs = append(libs, baseLinkerProps.Static_libs...) |
| 424 | libs = android.SortedUniqueStrings(libs) |
| 425 | return libs |
| 426 | } |
| 427 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 428 | for _, linkerProps := range module.linker.linkerProps() { |
| 429 | if baseLinkerProps, ok := linkerProps.(*BaseLinkerProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 430 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 431 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 432 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 433 | deps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, libs)) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 434 | exportedDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 435 | linkopts.Value = getBp2BuildLinkerFlags(baseLinkerProps) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 436 | wholeArchiveDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 437 | |
| 438 | if baseLinkerProps.Version_script != nil { |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 439 | versionScript.Value = android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script) |
Lukacs T. Berki | 1353e59 | 2021-04-30 15:35:09 +0200 | [diff] [blame] | 440 | } |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 441 | |
| 442 | sharedLibs := baseLinkerProps.Shared_libs |
| 443 | dynamicDeps = bazel.MakeLabelListAttribute(android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 444 | |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 445 | break |
| 446 | } |
| 447 | } |
| 448 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 449 | for arch, props := range module.GetArchProperties(ctx, &BaseLinkerProperties{}) { |
| 450 | if baseLinkerProps, ok := props.(*BaseLinkerProperties); ok { |
| 451 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 452 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 453 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 454 | deps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 455 | exportedDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | 143be94 | 2021-05-09 23:55:51 -0400 | [diff] [blame] | 456 | linkopts.SetValueForArch(arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 457 | wholeArchiveDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 458 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 459 | if baseLinkerProps.Version_script != nil { |
| 460 | versionScript.SetValueForArch(arch.Name, |
| 461 | android.BazelLabelForModuleSrcSingle(ctx, *baseLinkerProps.Version_script)) |
| 462 | } |
Rupert Shuttleworth | 3b413d3 | 2021-05-10 18:41:51 -0400 | [diff] [blame] | 463 | |
| 464 | sharedLibs := baseLinkerProps.Shared_libs |
| 465 | dynamicDeps.SetValueForArch(arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
Jingwen Chen | 6393098 | 2021-03-24 10:04:33 -0400 | [diff] [blame] | 466 | } |
| 467 | } |
| 468 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 469 | for os, targetProperties := range module.GetTargetProperties(ctx, &BaseLinkerProperties{}) { |
| 470 | if baseLinkerProps, ok := targetProperties.Properties.(*BaseLinkerProperties); ok { |
| 471 | libs := getLibs(baseLinkerProps) |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 472 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 473 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 474 | wholeArchiveDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
| 475 | deps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 476 | exportedDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 477 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 478 | linkopts.SetOsValueForTarget(os.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
Rupert Shuttleworth | c50fa8d | 2021-05-06 02:40:33 -0400 | [diff] [blame] | 479 | |
| 480 | sharedLibs := baseLinkerProps.Shared_libs |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 481 | dynamicDeps.SetOsValueForTarget(os.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 482 | } |
| 483 | for arch, archProperties := range targetProperties.ArchProperties { |
| 484 | if baseLinkerProps, ok := archProperties.(*BaseLinkerProperties); ok { |
| 485 | libs := getLibs(baseLinkerProps) |
| 486 | exportedLibs := baseLinkerProps.Export_header_lib_headers |
| 487 | wholeArchiveLibs := baseLinkerProps.Whole_static_libs |
| 488 | wholeArchiveDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, wholeArchiveLibs)) |
| 489 | deps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, libs)) |
| 490 | exportedDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, exportedLibs)) |
| 491 | |
| 492 | linkopts.SetOsArchValueForTarget(os.Name, arch.Name, getBp2BuildLinkerFlags(baseLinkerProps)) |
| 493 | |
| 494 | sharedLibs := baseLinkerProps.Shared_libs |
| 495 | dynamicDeps.SetOsArchValueForTarget(os.Name, arch.Name, android.BazelLabelForModuleDeps(ctx, sharedLibs)) |
| 496 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 500 | return linkerAttributes{ |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 501 | deps: deps, |
Chris Parsons | d635877 | 2021-05-18 18:35:24 -0400 | [diff] [blame] | 502 | exportedDeps: exportedDeps, |
Chris Parsons | 0864831 | 2021-05-06 16:23:19 -0400 | [diff] [blame] | 503 | dynamicDeps: dynamicDeps, |
| 504 | wholeArchiveDeps: wholeArchiveDeps, |
| 505 | linkopts: linkopts, |
| 506 | versionScript: versionScript, |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 507 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 508 | } |
| 509 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 510 | // Relativize a list of root-relative paths with respect to the module's |
| 511 | // directory. |
| 512 | // |
| 513 | // include_dirs Soong prop are root-relative (b/183742505), but |
| 514 | // local_include_dirs, export_include_dirs and export_system_include_dirs are |
| 515 | // module dir relative. This function makes a list of paths entirely module dir |
| 516 | // relative. |
| 517 | // |
| 518 | // For the `include` attribute, Bazel wants the paths to be relative to the |
| 519 | // module. |
| 520 | func bp2BuildMakePathsRelativeToModule(ctx android.BazelConversionPathContext, paths []string) []string { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 521 | var relativePaths []string |
| 522 | for _, path := range paths { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 523 | // Semantics of filepath.Rel: join(ModuleDir, rel(ModuleDir, path)) == path |
| 524 | relativePath, err := filepath.Rel(ctx.ModuleDir(), path) |
| 525 | if err != nil { |
| 526 | panic(err) |
| 527 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 528 | relativePaths = append(relativePaths, relativePath) |
| 529 | } |
| 530 | return relativePaths |
| 531 | } |
| 532 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 533 | // bp2BuildParseExportedIncludes creates a string list attribute contains the |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 534 | // exported included directories of a module. |
| 535 | func bp2BuildParseExportedIncludes(ctx android.TopDownMutatorContext, module *Module) bazel.StringListAttribute { |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 536 | libraryDecorator := module.linker.(*libraryDecorator) |
| 537 | |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 538 | // Export_system_include_dirs and export_include_dirs are already module dir |
| 539 | // relative, so they don't need to be relativized like include_dirs, which |
| 540 | // are root-relative. |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 541 | includeDirs := libraryDecorator.flagExporter.Properties.Export_system_include_dirs |
| 542 | includeDirs = append(includeDirs, libraryDecorator.flagExporter.Properties.Export_include_dirs...) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 543 | includeDirsAttribute := bazel.MakeStringListAttribute(includeDirs) |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 544 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 545 | getVariantIncludeDirs := func(includeDirs []string, flagExporterProperties *FlagExporterProperties) []string { |
| 546 | variantIncludeDirs := flagExporterProperties.Export_system_include_dirs |
| 547 | variantIncludeDirs = append(variantIncludeDirs, flagExporterProperties.Export_include_dirs...) |
| 548 | |
| 549 | // To avoid duplicate includes when base includes + arch includes are combined |
| 550 | // TODO: This doesn't take conflicts between arch and os includes into account |
| 551 | variantIncludeDirs = bazel.SubtractStrings(variantIncludeDirs, includeDirs) |
| 552 | return variantIncludeDirs |
| 553 | } |
| 554 | |
Lukacs T. Berki | 598dd00 | 2021-05-05 09:00:01 +0200 | [diff] [blame] | 555 | for arch, props := range module.GetArchProperties(ctx, &FlagExporterProperties{}) { |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 556 | if flagExporterProperties, ok := props.(*FlagExporterProperties); ok { |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 557 | archIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 558 | if len(archIncludeDirs) > 0 { |
| 559 | includeDirsAttribute.SetValueForArch(arch.Name, archIncludeDirs) |
| 560 | } |
Rupert Shuttleworth | b815168 | 2021-04-06 20:06:21 +0000 | [diff] [blame] | 561 | } |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 562 | } |
| 563 | |
Rupert Shuttleworth | c194ffb | 2021-05-19 06:49:02 -0400 | [diff] [blame^] | 564 | for os, targetProperties := range module.GetTargetProperties(ctx, &FlagExporterProperties{}) { |
| 565 | if flagExporterProperties, ok := targetProperties.Properties.(*FlagExporterProperties); ok { |
| 566 | targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 567 | if len(targetIncludeDirs) > 0 { |
| 568 | includeDirsAttribute.SetOsValueForTarget(os.Name, targetIncludeDirs) |
| 569 | } |
| 570 | } |
| 571 | for arch, archProperties := range targetProperties.ArchProperties { |
| 572 | if flagExporterProperties, ok := archProperties.(*FlagExporterProperties); ok { |
| 573 | targetIncludeDirs := getVariantIncludeDirs(includeDirs, flagExporterProperties) |
| 574 | if len(targetIncludeDirs) > 0 { |
| 575 | includeDirsAttribute.SetOsArchValueForTarget(os.Name, arch.Name, targetIncludeDirs) |
| 576 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 577 | } |
Rupert Shuttleworth | 375451e | 2021-04-26 07:49:08 -0400 | [diff] [blame] | 578 | } |
| 579 | } |
| 580 | |
Jingwen Chen | 882bcc1 | 2021-04-27 05:54:20 +0000 | [diff] [blame] | 581 | return includeDirsAttribute |
Jingwen Chen | 91220d7 | 2021-03-24 02:18:33 -0400 | [diff] [blame] | 582 | } |