blob: 65a11e005864144bd2d564b741c50dc5ca255bc7 [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"
Chris Parsonsf874e462022-05-10 13:50:12 -040022 "android/soong/bazel/cquery"
Colin Cross4d9c2d12016-07-29 12:48:20 -070023)
24
25//
26// Objects (for crt*.o)
27//
28
29func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070030 android.RegisterModuleType("cc_object", ObjectFactory)
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000031 android.RegisterSdkMemberType(ccObjectSdkMemberType)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050032
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000033}
34
35var ccObjectSdkMemberType = &librarySdkMemberType{
36 SdkMemberTypeBase: android.SdkMemberTypeBase{
37 PropertyName: "native_objects",
38 SupportsSdk: true,
39 },
40 prebuiltModuleType: "cc_prebuilt_object",
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 {
Chris Parsons8d6e4332021-02-22 16:13:50 -050049 module *Module
50}
51
Chris Parsons6ce2cf92022-05-20 10:54:17 -040052var _ BazelHandler = (*objectBazelHandler)(nil)
53
Chris Parsonsf874e462022-05-10 13:50:12 -040054func (handler *objectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) {
Liz Kammer8206d4f2021-03-03 16:40:52 -050055 bazelCtx := ctx.Config().BazelContext
Chris Parsonsf874e462022-05-10 13:50:12 -040056 bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx))
57}
Liz Kammer8206d4f2021-03-03 16:40:52 -050058
Chris Parsonsf874e462022-05-10 13:50:12 -040059func (handler *objectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) {
60 bazelCtx := ctx.Config().BazelContext
61 objPaths, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx))
62 if err != nil {
63 ctx.ModuleErrorf(err.Error())
64 return
Liz Kammer8206d4f2021-03-03 16:40:52 -050065 }
Chris Parsonsf874e462022-05-10 13:50:12 -040066
67 if len(objPaths) != 1 {
68 ctx.ModuleErrorf("expected exactly one object file for '%s', but got %s", label, objPaths)
69 return
70 }
71
72 handler.module.outputFile = android.OptionalPathForPath(android.PathForBazelOut(ctx, objPaths[0]))
Chris Parsons8d6e4332021-02-22 16:13:50 -050073}
74
Pete Bentley74c9bba2019-08-16 20:25:06 +010075type ObjectLinkerProperties struct {
Colin Cross137d7da2021-06-21 16:41:29 -070076 // list of static library modules that should only provide headers for this module.
77 Static_libs []string `android:"arch_variant,variant_prepend"`
78
79 // list of shared library modules should only provide headers for this module.
80 Shared_libs []string `android:"arch_variant"`
81
Pete Bentley74c9bba2019-08-16 20:25:06 +010082 // list of modules that should only provide headers for this module.
83 Header_libs []string `android:"arch_variant,variant_prepend"`
84
Colin Cross137d7da2021-06-21 16:41:29 -070085 // list of default libraries that will provide headers for this module. If unset, generally
86 // defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
Colin Cross6b8f4252021-07-22 11:39:44 -070087 System_shared_libs []string `android:"arch_variant"`
Colin Cross137d7da2021-06-21 16:41:29 -070088
Pete Bentley74c9bba2019-08-16 20:25:06 +010089 // names of other cc_object modules to link into this module using partial linking
90 Objs []string `android:"arch_variant"`
91
92 // if set, add an extra objcopy --prefix-symbols= step
93 Prefix_symbols *string
94
95 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
96 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070097
98 // Indicates that this module is a CRT object. CRT objects will be split
99 // into a variant per-API level between min_sdk_version and current.
100 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +0100101}
102
Colin Cross7cabd422021-06-25 14:21:04 -0700103func newObject(hod android.HostOrDeviceSupported) *Module {
104 module := newBaseModule(hod, android.MultilibBoth)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +0000105 module.sanitize = &sanitize{}
106 module.stl = &stl{}
107 return module
108}
109
Patrice Arrudabaff0ce2019-03-26 10:39:49 -0700110// cc_object runs the compiler without running the linker. It is rarely
111// necessary, but sometimes used to generate .s files from .c files to use as
112// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -0700113func ObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700114 module := newObject(android.HostAndDeviceSupported)
Colin Crossb916a382016-07-29 17:28:03 -0700115 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -0700116 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700117 }
118 module.compiler = NewBaseCompiler()
Chris Parsons8d6e4332021-02-22 16:13:50 -0500119 module.bazelHandler = &objectBazelHandler{module: module}
Peter Collingbourne486e42c2018-10-25 10:53:44 -0700120
121 // Clang's address-significance tables are incompatible with ld -r.
122 module.compiler.appendCflags([]string{"-fno-addrsig"})
123
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000124 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500125
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400126 module.bazelable = true
Colin Cross4d9c2d12016-07-29 12:48:20 -0700127 return module.Init()
128}
129
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500130// For bp2build conversion.
131type bazelObjectAttributes struct {
Chris Parsonsa37e1952021-09-28 16:47:36 -0400132 Srcs bazel.LabelListAttribute
133 Srcs_as bazel.LabelListAttribute
134 Hdrs bazel.LabelListAttribute
135 Deps bazel.LabelListAttribute
136 System_dynamic_deps bazel.LabelListAttribute
137 Copts bazel.StringListAttribute
138 Asflags bazel.StringListAttribute
139 Local_includes bazel.StringListAttribute
140 Absolute_includes bazel.StringListAttribute
141 Stl *string
142 Linker_script bazel.LabelAttribute
Yu Liufc603162022-03-01 15:44:08 -0800143 sdkAttributes
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500144}
145
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400146// objectBp2Build is the bp2build converter from cc_object modules to the
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500147// Bazel equivalent target, plus any necessary include deps for the cc_object.
Liz Kammerbe46fcc2021-11-01 15:32:43 -0400148func objectBp2Build(ctx android.TopDownMutatorContext, m *Module) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500149 if m.compiler == nil {
150 // a cc_object must have access to the compiler decorator for its props.
151 ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
152 }
153
Jingwen Chen5d864492021-02-24 07:20:12 -0500154 // Set arch-specific configurable attributes
Liz Kammere6583482021-10-19 13:56:10 -0400155 baseAttributes := bp2BuildParseBaseProps(ctx, m)
156 compilerAttrs := baseAttributes.compilerAttributes
Jingwen Chen07027912021-03-15 06:02:43 -0400157 var deps bazel.LabelListAttribute
Chris Parsonsa37e1952021-09-28 16:47:36 -0400158 systemDynamicDeps := bazel.LabelListAttribute{ForceSpecifyEmptyList: true}
159
160 var linkerScript bazel.LabelAttribute
161
162 for axis, configToProps := range m.GetArchVariantProperties(ctx, &ObjectLinkerProperties{}) {
163 for config, props := range configToProps {
164 if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
165 if objectLinkerProps.Linker_script != nil {
Chris Parsons58852a02021-12-09 18:10:18 -0500166 label := android.BazelLabelForModuleSrcSingle(ctx, *objectLinkerProps.Linker_script)
167 linkerScript.SetSelectValue(axis, config, label)
Chris Parsonsa37e1952021-09-28 16:47:36 -0400168 }
169 deps.SetSelectValue(axis, config, android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
170 systemSharedLibs := objectLinkerProps.System_shared_libs
171 if len(systemSharedLibs) > 0 {
172 systemSharedLibs = android.FirstUniqueStrings(systemSharedLibs)
173 }
174 systemDynamicDeps.SetSelectValue(axis, config, bazelLabelForSharedDeps(ctx, systemSharedLibs))
175 }
Jingwen Chendb120242021-02-23 00:46:47 -0500176 }
177 }
Chris Parsonsa37e1952021-09-28 16:47:36 -0400178 deps.ResolveExcludes()
Jingwen Chendb120242021-02-23 00:46:47 -0500179
Chris Parsons990c4f42021-05-25 12:10:58 -0400180 // Don't split cc_object srcs across languages. Doing so would add complexity,
181 // and this isn't typically done for cc_object.
182 srcs := compilerAttrs.srcs
183 srcs.Append(compilerAttrs.cSrcs)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400184
185 asFlags := compilerAttrs.asFlags
186 if compilerAttrs.asSrcs.IsEmpty() {
187 // Skip asflags for BUILD file simplicity if there are no assembly sources.
188 asFlags = bazel.MakeStringListAttribute(nil)
189 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400190
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500191 attrs := &bazelObjectAttributes{
Chris Parsonsa37e1952021-09-28 16:47:36 -0400192 Srcs: srcs,
193 Srcs_as: compilerAttrs.asSrcs,
194 Deps: deps,
195 System_dynamic_deps: systemDynamicDeps,
196 Copts: compilerAttrs.copts,
197 Asflags: asFlags,
198 Local_includes: compilerAttrs.localIncludes,
199 Absolute_includes: compilerAttrs.absoluteIncludes,
200 Stl: compilerAttrs.stl,
201 Linker_script: linkerScript,
Yu Liufc603162022-03-01 15:44:08 -0800202 sdkAttributes: bp2BuildParseSdkAttributes(m),
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500203 }
204
Liz Kammerfc46bc12021-02-19 11:06:17 -0500205 props := bazel.BazelTargetModuleProperties{
206 Rule_class: "cc_object",
Liz Kammer2b376bc2022-01-12 12:00:49 -0500207 Bzl_load_location: "//build/bazel/rules/cc:cc_object.bzl",
Liz Kammerfc46bc12021-02-19 11:06:17 -0500208 }
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500209
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux447f6c92021-08-31 20:30:36 +0000210 ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: m.Name()}, attrs)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500211}
212
Colin Cross4d9c2d12016-07-29 12:48:20 -0700213func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700214 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700215}
216
Colin Cross42742b82016-08-01 13:20:05 -0700217func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700218 return []interface{}{&object.Properties}
219}
220
Colin Cross42742b82016-08-01 13:20:05 -0700221func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222
Colin Cross37047f12016-12-13 17:06:13 -0800223func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100224 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700225 deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs...)
226 deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700228
Colin Cross6b8f4252021-07-22 11:39:44 -0700229 deps.SystemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700230 if deps.SystemSharedLibs == nil {
Colin Cross6b8f4252021-07-22 11:39:44 -0700231 // Provide a default set of shared libraries if system_shared_libs is unspecified.
Colin Cross137d7da2021-06-21 16:41:29 -0700232 // Note: If an empty list [] is specified, it implies that the module declines the
233 // default shared libraries.
234 deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
235 }
236 deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 return deps
238}
239
Pete Bentley74c9bba2019-08-16 20:25:06 +0100240func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross33bac242021-07-14 17:03:16 -0700241 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242
Pete Bentley74c9bba2019-08-16 20:25:06 +0100243 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800244 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100245 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
246 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700247 return flags
248}
249
250func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700251 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700252
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700253 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254
255 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700256 builderFlags := flagsToBuilderFlags(flags)
257
Pete Bentleyab65ba92019-10-18 12:39:56 +0100258 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700259 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700260
Nan Zhang0007d812017-11-07 10:57:05 -0800261 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700262 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500263 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700264 builderFlags, output)
265 outputFile = output
266 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700267 } else {
268 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700269 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700270
Nan Zhang0007d812017-11-07 10:57:05 -0800271 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700272 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500273 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700274 builderFlags, output)
275 output = input
276 }
277
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500278 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700279 }
280
281 ctx.CheckbuildFile(outputFile)
282 return outputFile
283}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900284
Colin Cross137d7da2021-06-21 16:41:29 -0700285func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
286 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...)
287
Colin Cross6b8f4252021-07-22 11:39:44 -0700288 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
Colin Cross137d7da2021-06-21 16:41:29 -0700289 // either input list doesn't come out as nil.
Colin Cross6b8f4252021-07-22 11:39:44 -0700290 if specifiedDeps.systemSharedLibs == nil {
291 specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700292 } else {
Colin Cross6b8f4252021-07-22 11:39:44 -0700293 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700294 }
295
296 return specifiedDeps
297}
298
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900299func (object *objectLinker) unstrippedOutputFilePath() android.Path {
300 return nil
301}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700302
303func (object *objectLinker) nativeCoverage() bool {
304 return true
305}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900306
307func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
308 return android.OptionalPath{}
309}
Inseob Kim1042d292020-06-01 23:23:05 +0900310
311func (object *objectLinker) object() bool {
312 return true
313}
Dan Albert92fe7402020-07-15 13:33:30 -0700314
315func (object *objectLinker) isCrt() bool {
316 return Bool(object.Properties.Crt)
317}