blob: fdd0b113cfb24e7d054cf71764f724b087765ee2 [file] [log] [blame]
Colin Cross4d9c2d12016-07-29 12:48:20 -07001// 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
15package cc
16
17import (
18 "fmt"
19
Colin Cross4d9c2d12016-07-29 12:48:20 -070020 "android/soong/android"
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050021 "android/soong/bazel"
Colin Cross4d9c2d12016-07-29 12:48:20 -070022)
23
24//
25// Objects (for crt*.o)
26//
27
28func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070029 android.RegisterModuleType("cc_object", ObjectFactory)
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000030 android.RegisterSdkMemberType(ccObjectSdkMemberType)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050031
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000032}
33
34var ccObjectSdkMemberType = &librarySdkMemberType{
35 SdkMemberTypeBase: android.SdkMemberTypeBase{
36 PropertyName: "native_objects",
37 SupportsSdk: true,
38 },
39 prebuiltModuleType: "cc_prebuilt_object",
40 linkTypes: nil,
Colin Cross4d9c2d12016-07-29 12:48:20 -070041}
42
43type objectLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070044 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070045 Properties ObjectLinkerProperties
46}
47
Chris Parsons8d6e4332021-02-22 16:13:50 -050048type objectBazelHandler struct {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000049 android.BazelHandler
Chris Parsons8d6e4332021-02-22 16:13:50 -050050
51 module *Module
52}
53
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000054func (handler *objectBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer8206d4f2021-03-03 16:40:52 -050055 bazelCtx := ctx.Config().BazelContext
Chris Parsons787fb362021-10-14 18:43:51 -040056 objPaths, ok := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
Liz Kammer8206d4f2021-03-03 16:40:52 -050057 if ok {
58 if len(objPaths) != 1 {
59 ctx.ModuleErrorf("expected exactly one object file for '%s', but got %s", label, objPaths)
60 return false
61 }
62
63 handler.module.outputFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, objPaths[0]))
64 }
65 return ok
Chris Parsons8d6e4332021-02-22 16:13:50 -050066}
67
Pete Bentley74c9bba2019-08-16 20:25:06 +010068type ObjectLinkerProperties struct {
Colin Cross137d7da2021-06-21 16:41:29 -070069 // list of static library modules that should only provide headers for this module.
70 Static_libs []string `android:"arch_variant,variant_prepend"`
71
72 // list of shared library modules should only provide headers for this module.
73 Shared_libs []string `android:"arch_variant"`
74
Pete Bentley74c9bba2019-08-16 20:25:06 +010075 // list of modules that should only provide headers for this module.
76 Header_libs []string `android:"arch_variant,variant_prepend"`
77
Colin Cross137d7da2021-06-21 16:41:29 -070078 // list of default libraries that will provide headers for this module. If unset, generally
79 // defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
Colin Cross6b8f4252021-07-22 11:39:44 -070080 System_shared_libs []string `android:"arch_variant"`
Colin Cross137d7da2021-06-21 16:41:29 -070081
Pete Bentley74c9bba2019-08-16 20:25:06 +010082 // names of other cc_object modules to link into this module using partial linking
83 Objs []string `android:"arch_variant"`
84
85 // if set, add an extra objcopy --prefix-symbols= step
86 Prefix_symbols *string
87
88 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
89 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070090
91 // Indicates that this module is a CRT object. CRT objects will be split
92 // into a variant per-API level between min_sdk_version and current.
93 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010094}
95
Colin Cross7cabd422021-06-25 14:21:04 -070096func newObject(hod android.HostOrDeviceSupported) *Module {
97 module := newBaseModule(hod, android.MultilibBoth)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000098 module.sanitize = &sanitize{}
99 module.stl = &stl{}
100 return module
101}
102
Patrice Arrudabaff0ce2019-03-26 10:39:49 -0700103// cc_object runs the compiler without running the linker. It is rarely
104// necessary, but sometimes used to generate .s files from .c files to use as
105// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -0700106func ObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700107 module := newObject(android.HostAndDeviceSupported)
Colin Crossb916a382016-07-29 17:28:03 -0700108 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -0700109 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700110 }
111 module.compiler = NewBaseCompiler()
Chris Parsons8d6e4332021-02-22 16:13:50 -0500112 module.bazelHandler = &objectBazelHandler{module: module}
Peter Collingbourne486e42c2018-10-25 10:53:44 -0700113
114 // Clang's address-significance tables are incompatible with ld -r.
115 module.compiler.appendCflags([]string{"-fno-addrsig"})
116
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000117 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500118
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400119 module.bazelable = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700120 return module.Init()
121}
122
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500123// For bp2build conversion.
124type bazelObjectAttributes struct {
Chris Parsonsa37e1952021-09-28 16:47:36 -0400125 Srcs bazel.LabelListAttribute
126 Srcs_as bazel.LabelListAttribute
127 Hdrs bazel.LabelListAttribute
128 Deps bazel.LabelListAttribute
129 System_dynamic_deps bazel.LabelListAttribute
130 Copts bazel.StringListAttribute
131 Asflags bazel.StringListAttribute
132 Local_includes bazel.StringListAttribute
133 Absolute_includes bazel.StringListAttribute
134 Stl *string
135 Linker_script bazel.LabelAttribute
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500136}
137
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400138// objectBp2Build is the bp2build converter from cc_object modules to the
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500139// Bazel equivalent target, plus any necessary include deps for the cc_object.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400140func objectBp2Build(ctx android.TopDownMutatorContext, m *Module) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500141 if m.compiler == nil {
142 // a cc_object must have access to the compiler decorator for its props.
143 ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
144 }
145
Jingwen Chen5d864492021-02-24 07:20:12 -0500146 // Set arch-specific configurable attributes
Liz Kammere6583482021-10-19 13:56:10 -0400147 baseAttributes := bp2BuildParseBaseProps(ctx, m)
148 compilerAttrs := baseAttributes.compilerAttributes
Jingwen Chen07027912021-03-15 06:02:43 -0400149 var deps bazel.LabelListAttribute
Chris Parsonsa37e1952021-09-28 16:47:36 -0400150 systemDynamicDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
151
152 var linkerScript bazel.LabelAttribute
153
154 for axis, configToProps := range m.GetArchVariantProperties(ctx, &ObjectLinkerProperties{}) {
155 for config, props := range configToProps {
156 if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
157 if objectLinkerProps.Linker_script != nil {
Chris Parsons58852a02021-12-09 18:10:18 -0500158 label := android.BazelLabelForModuleSrcSingle(ctx, *objectLinkerProps.Linker_script)
159 linkerScript.SetSelectValue(axis, config, label)
Chris Parsonsa37e1952021-09-28 16:47:36 -0400160 }
161 deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
162 systemSharedLibs := objectLinkerProps.System_shared_libs
163 if len(systemSharedLibs) > 0 {
164 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
165 }
166 systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
167 }
Jingwen Chendb120242021-02-23 00:46:47 -0500168 }
169 }
Chris Parsonsa37e1952021-09-28 16:47:36 -0400170 deps.ResolveExcludes()
Jingwen Chendb120242021-02-23 00:46:47 -0500171
Chris Parsons990c4f42021-05-25 12:10:58 -0400172 // Don't split cc_object srcs across languages. Doing so would add complexity,
173 // and this isn't typically done for cc_object.
174 srcs := compilerAttrs.srcs
175 srcs.Append(compilerAttrs.cSrcs)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400176
177 asFlags := compilerAttrs.asFlags
178 if compilerAttrs.asSrcs.IsEmpty() {
179 // Skip asflags for BUILD file simplicity if there are no assembly sources.
180 asFlags = bazel.MakeStringListAttribute(nil)
181 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400182
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500183 attrs := &bazelObjectAttributes{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400184 Srcs: srcs,
185 Srcs_as: compilerAttrs.asSrcs,
186 Deps: deps,
187 System_dynamic_deps: systemDynamicDeps,
188 Copts: compilerAttrs.copts,
189 Asflags: asFlags,
190 Local_includes: compilerAttrs.localIncludes,
191 Absolute_includes: compilerAttrs.absoluteIncludes,
192 Stl: compilerAttrs.stl,
193 Linker_script: linkerScript,
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500194 }
195
Liz Kammerfc46bc12021-02-19 11:06:17 -0500196 props := bazel.BazelTargetModuleProperties{
197 Rule_class: "cc_object",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500198 Bzl_load_location: "//build/bazel/rules/cc:cc_object.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500199 }
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500200
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000201 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500202}
203
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700205 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206}
207
Colin Cross42742b82016-08-01 13:20:05 -0700208func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700209 return []interface{}{&object.Properties}
210}
211
Colin Cross42742b82016-08-01 13:20:05 -0700212func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213
Colin Cross37047f12016-12-13 17:06:13 -0800214func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100215 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700216 deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs...)
217 deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700219
Colin Cross6b8f4252021-07-22 11:39:44 -0700220 deps.SystemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700221 if deps.SystemSharedLibs == nil {
Colin Cross6b8f4252021-07-22 11:39:44 -0700222 // Provide a default set of shared libraries if system_shared_libs is unspecified.
Colin Cross137d7da2021-06-21 16:41:29 -0700223 // Note: If an empty list [] is specified, it implies that the module declines the
224 // default shared libraries.
225 deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
226 }
227 deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700228 return deps
229}
230
Pete Bentley74c9bba2019-08-16 20:25:06 +0100231func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross33bac242021-07-14 17:03:16 -0700232 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233
Pete Bentley74c9bba2019-08-16 20:25:06 +0100234 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800235 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100236 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
237 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700238 return flags
239}
240
241func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700242 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700244 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700245
246 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700247 builderFlags := flagsToBuilderFlags(flags)
248
Pete Bentleyab65ba92019-10-18 12:39:56 +0100249 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700250 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700251
Nan Zhang0007d812017-11-07 10:57:05 -0800252 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700253 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500254 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700255 builderFlags, output)
256 outputFile = output
257 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 } else {
259 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700260 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700261
Nan Zhang0007d812017-11-07 10:57:05 -0800262 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700263 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500264 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700265 builderFlags, output)
266 output = input
267 }
268
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500269 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 }
271
272 ctx.CheckbuildFile(outputFile)
273 return outputFile
274}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900275
Colin Cross137d7da2021-06-21 16:41:29 -0700276func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
277 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...)
278
Colin Cross6b8f4252021-07-22 11:39:44 -0700279 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
Colin Cross137d7da2021-06-21 16:41:29 -0700280 // either input list doesn't come out as nil.
Colin Cross6b8f4252021-07-22 11:39:44 -0700281 if specifiedDeps.systemSharedLibs == nil {
282 specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700283 } else {
Colin Cross6b8f4252021-07-22 11:39:44 -0700284 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700285 }
286
287 return specifiedDeps
288}
289
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900290func (object *objectLinker) unstrippedOutputFilePath() android.Path {
291 return nil
292}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700293
294func (object *objectLinker) nativeCoverage() bool {
295 return true
296}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900297
298func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
299 return android.OptionalPath{}
300}
Inseob Kim1042d292020-06-01 23:23:05 +0900301
302func (object *objectLinker) object() bool {
303 return true
304}
Dan Albert92fe7402020-07-15 13:33:30 -0700305
306func (object *objectLinker) isCrt() bool {
307 return Bool(object.Properties.Crt)
308}