Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // Copyright 2016 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | "android/soong/android" |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 21 | "android/soong/bazel" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | // |
| 25 | // Objects (for crt*.o) |
| 26 | // |
| 27 | |
| 28 | func init() { |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 29 | android.RegisterModuleType("cc_object", ObjectFactory) |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 30 | android.RegisterSdkMemberType(ccObjectSdkMemberType) |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 31 | |
| 32 | android.RegisterBp2BuildMutator("cc_object", ObjectBp2Build) |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | var ccObjectSdkMemberType = &librarySdkMemberType{ |
| 36 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 37 | PropertyName: "native_objects", |
| 38 | SupportsSdk: true, |
| 39 | }, |
| 40 | prebuiltModuleType: "cc_prebuilt_object", |
| 41 | linkTypes: nil, |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type objectLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 45 | *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 46 | Properties ObjectLinkerProperties |
| 47 | } |
| 48 | |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 49 | type objectBazelHandler struct { |
| 50 | bazelHandler |
| 51 | |
| 52 | module *Module |
| 53 | } |
| 54 | |
| 55 | func (handler *objectBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
Liz Kammer | 8206d4f | 2021-03-03 16:40:52 -0500 | [diff] [blame] | 56 | bazelCtx := ctx.Config().BazelContext |
| 57 | objPaths, ok := bazelCtx.GetOutputFiles(label, ctx.Arch().ArchType) |
| 58 | if ok { |
| 59 | if len(objPaths) != 1 { |
| 60 | ctx.ModuleErrorf("expected exactly one object file for '%s', but got %s", label, objPaths) |
| 61 | return false |
| 62 | } |
| 63 | |
| 64 | handler.module.outputFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, objPaths[0])) |
| 65 | } |
| 66 | return ok |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 67 | } |
| 68 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 69 | type ObjectLinkerProperties struct { |
| 70 | // list of modules that should only provide headers for this module. |
| 71 | Header_libs []string `android:"arch_variant,variant_prepend"` |
| 72 | |
| 73 | // names of other cc_object modules to link into this module using partial linking |
| 74 | Objs []string `android:"arch_variant"` |
| 75 | |
| 76 | // if set, add an extra objcopy --prefix-symbols= step |
| 77 | Prefix_symbols *string |
| 78 | |
| 79 | // if set, the path to a linker script to pass to ld -r when combining multiple object files. |
| 80 | Linker_script *string `android:"path,arch_variant"` |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 81 | |
| 82 | // Indicates that this module is a CRT object. CRT objects will be split |
| 83 | // into a variant per-API level between min_sdk_version and current. |
| 84 | Crt *bool |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 85 | } |
| 86 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 87 | func newObject() *Module { |
| 88 | module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth) |
| 89 | module.sanitize = &sanitize{} |
| 90 | module.stl = &stl{} |
| 91 | return module |
| 92 | } |
| 93 | |
Patrice Arruda | baff0ce | 2019-03-26 10:39:49 -0700 | [diff] [blame] | 94 | // cc_object runs the compiler without running the linker. It is rarely |
| 95 | // necessary, but sometimes used to generate .s files from .c files to use as |
| 96 | // input to a cc_genrule module. |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 97 | func ObjectFactory() android.Module { |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 98 | module := newObject() |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 99 | module.linker = &objectLinker{ |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 100 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 101 | } |
| 102 | module.compiler = NewBaseCompiler() |
Chris Parsons | 8d6e433 | 2021-02-22 16:13:50 -0500 | [diff] [blame] | 103 | module.bazelHandler = &objectBazelHandler{module: module} |
Peter Collingbourne | 486e42c | 2018-10-25 10:53:44 -0700 | [diff] [blame] | 104 | |
| 105 | // Clang's address-significance tables are incompatible with ld -r. |
| 106 | module.compiler.appendCflags([]string{"-fno-addrsig"}) |
| 107 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 108 | module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType} |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 109 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 110 | return module.Init() |
| 111 | } |
| 112 | |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 113 | // For bp2build conversion. |
| 114 | type bazelObjectAttributes struct { |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 115 | Srcs bazel.LabelListAttribute |
| 116 | Hdrs bazel.LabelListAttribute |
| 117 | Deps bazel.LabelListAttribute |
| 118 | Copts bazel.StringListAttribute |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 119 | Asflags bazel.StringListAttribute |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | type bazelObject struct { |
| 123 | android.BazelTargetModuleBase |
| 124 | bazelObjectAttributes |
| 125 | } |
| 126 | |
| 127 | func (m *bazelObject) Name() string { |
| 128 | return m.BaseModuleName() |
| 129 | } |
| 130 | |
| 131 | func (m *bazelObject) GenerateAndroidBuildActions(ctx android.ModuleContext) {} |
| 132 | |
| 133 | func BazelObjectFactory() android.Module { |
| 134 | module := &bazelObject{} |
| 135 | module.AddProperties(&module.bazelObjectAttributes) |
| 136 | android.InitBazelTargetModule(module) |
| 137 | return module |
| 138 | } |
| 139 | |
| 140 | // ObjectBp2Build is the bp2build converter from cc_object modules to the |
| 141 | // Bazel equivalent target, plus any necessary include deps for the cc_object. |
| 142 | func ObjectBp2Build(ctx android.TopDownMutatorContext) { |
| 143 | m, ok := ctx.Module().(*Module) |
Jingwen Chen | 12b4c27 | 2021-03-10 02:05:59 -0500 | [diff] [blame] | 144 | if !ok || !m.ConvertWithBp2build(ctx) { |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 145 | return |
| 146 | } |
| 147 | |
| 148 | // a Module can be something other than a cc_object. |
| 149 | if ctx.ModuleType() != "cc_object" { |
| 150 | return |
| 151 | } |
| 152 | |
| 153 | if m.compiler == nil { |
| 154 | // a cc_object must have access to the compiler decorator for its props. |
| 155 | ctx.ModuleErrorf("compiler must not be nil for a cc_object module") |
| 156 | } |
| 157 | |
Jingwen Chen | 5d86449 | 2021-02-24 07:20:12 -0500 | [diff] [blame] | 158 | // Set arch-specific configurable attributes |
Jingwen Chen | 107c0de | 2021-04-09 10:43:12 +0000 | [diff] [blame] | 159 | compilerAttrs := bp2BuildParseCompilerProps(ctx, m) |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 160 | var asFlags bazel.StringListAttribute |
Liz Kammer | a4aa430 | 2021-03-18 16:56:36 -0400 | [diff] [blame] | 161 | |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 162 | var deps bazel.LabelListAttribute |
Jingwen Chen | db12024 | 2021-02-23 00:46:47 -0500 | [diff] [blame] | 163 | for _, props := range m.linker.linkerProps() { |
| 164 | if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok { |
Jingwen Chen | 0702791 | 2021-03-15 06:02:43 -0400 | [diff] [blame] | 165 | deps = bazel.MakeLabelListAttribute( |
| 166 | android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs)) |
Jingwen Chen | db12024 | 2021-02-23 00:46:47 -0500 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 170 | productVariableProps := android.ProductVariableProperties(ctx) |
| 171 | if props, exists := productVariableProps["Asflags"]; exists { |
| 172 | // TODO(b/183595873): consider deduplicating handling of product variable properties |
| 173 | for _, prop := range props { |
| 174 | flags, ok := prop.Property.([]string) |
| 175 | if !ok { |
| 176 | ctx.ModuleErrorf("Could not convert product variable asflag property") |
| 177 | return |
| 178 | } |
Liz Kammer | 6fd7b3f | 2021-05-06 13:54:29 -0400 | [diff] [blame] | 179 | newFlags, _ := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable) |
| 180 | asFlags.ProductValues = append(asFlags.ProductValues, bazel.ProductVariableValues{ |
| 181 | ProductVariable: prop.ProductConfigVariable, |
| 182 | Values: newFlags, |
| 183 | }) |
Liz Kammer | a060c45 | 2021-03-24 10:14:47 -0400 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | // TODO(b/183595872) warn/error if we're not handling product variables |
| 187 | |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame^] | 188 | // Don't split cc_object srcs across languages. Doing so would add complexity, |
| 189 | // and this isn't typically done for cc_object. |
| 190 | srcs := compilerAttrs.srcs |
| 191 | srcs.Append(compilerAttrs.cSrcs) |
| 192 | srcs.Append(compilerAttrs.asSrcs) |
| 193 | |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 194 | attrs := &bazelObjectAttributes{ |
Chris Parsons | 990c4f4 | 2021-05-25 12:10:58 -0400 | [diff] [blame^] | 195 | Srcs: srcs, |
Jingwen Chen | ed9c17d | 2021-04-13 07:14:55 +0000 | [diff] [blame] | 196 | Deps: deps, |
| 197 | Copts: compilerAttrs.copts, |
| 198 | Asflags: asFlags, |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 201 | props := bazel.BazelTargetModuleProperties{ |
| 202 | Rule_class: "cc_object", |
| 203 | Bzl_load_location: "//build/bazel/rules:cc_object.bzl", |
| 204 | } |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 205 | |
Liz Kammer | fc46bc1 | 2021-02-19 11:06:17 -0500 | [diff] [blame] | 206 | ctx.CreateBazelTargetModule(BazelObjectFactory, m.Name(), props, attrs) |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 207 | } |
| 208 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 209 | func (object *objectLinker) appendLdflags(flags []string) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 210 | panic(fmt.Errorf("appendLdflags on objectLinker not supported")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 213 | func (object *objectLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 214 | return []interface{}{&object.Properties} |
| 215 | } |
| 216 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 217 | func (*objectLinker) linkerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 218 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 219 | func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Paul Duffin | a37832a | 2019-07-18 12:31:26 +0100 | [diff] [blame] | 220 | deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 221 | deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...) |
| 222 | return deps |
| 223 | } |
| 224 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 225 | func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 226 | flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 227 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 228 | if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 229 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String()) |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 230 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path()) |
| 231 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 232 | return flags |
| 233 | } |
| 234 | |
| 235 | func (object *objectLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 236 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 237 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 238 | objs = objs.Append(deps.Objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 239 | |
| 240 | var outputFile android.Path |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 241 | builderFlags := flagsToBuilderFlags(flags) |
| 242 | |
Pete Bentley | ab65ba9 | 2019-10-18 12:39:56 +0100 | [diff] [blame] | 243 | if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 244 | outputFile = objs.objFiles[0] |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 245 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 246 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 247 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 248 | transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 249 | builderFlags, output) |
| 250 | outputFile = output |
| 251 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 252 | } else { |
| 253 | output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 254 | outputFile = output |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 255 | |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 256 | if String(object.Properties.Prefix_symbols) != "" { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 257 | input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 258 | transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 259 | builderFlags, output) |
| 260 | output = input |
| 261 | } |
| 262 | |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 263 | transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | ctx.CheckbuildFile(outputFile) |
| 267 | return outputFile |
| 268 | } |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 269 | |
| 270 | func (object *objectLinker) unstrippedOutputFilePath() android.Path { |
| 271 | return nil |
| 272 | } |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 273 | |
| 274 | func (object *objectLinker) nativeCoverage() bool { |
| 275 | return true |
| 276 | } |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 277 | |
| 278 | func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { |
| 279 | return android.OptionalPath{} |
| 280 | } |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 281 | |
| 282 | func (object *objectLinker) object() bool { |
| 283 | return true |
| 284 | } |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 285 | |
| 286 | func (object *objectLinker) isCrt() bool { |
| 287 | return Bool(object.Properties.Crt) |
| 288 | } |