blob: 4f8797dea38d6edd3948a513f20eb7cabcd39a02 [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
32 android.RegisterBp2BuildMutator("cc_object", ObjectBp2Build)
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",
41 linkTypes: nil,
Colin Cross4d9c2d12016-07-29 12:48:20 -070042}
43
44type objectLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070045 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070046 Properties ObjectLinkerProperties
47}
48
Chris Parsons8d6e4332021-02-22 16:13:50 -050049type objectBazelHandler struct {
50 bazelHandler
51
52 module *Module
53}
54
55func (handler *objectBazelHandler) generateBazelBuildActions(ctx android.ModuleContext, label string) bool {
Liz Kammer8206d4f2021-03-03 16:40:52 -050056 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 Parsons8d6e4332021-02-22 16:13:50 -050067}
68
Pete Bentley74c9bba2019-08-16 20:25:06 +010069type 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 Albert92fe7402020-07-15 13:33:30 -070081
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 Bentley74c9bba2019-08-16 20:25:06 +010085}
86
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000087func newObject() *Module {
88 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
89 module.sanitize = &sanitize{}
90 module.stl = &stl{}
91 return module
92}
93
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070094// 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 Crosse40b4ea2018-10-02 22:25:58 -070097func ObjectFactory() android.Module {
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000098 module := newObject()
Colin Crossb916a382016-07-29 17:28:03 -070099 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -0700100 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700101 }
102 module.compiler = NewBaseCompiler()
Chris Parsons8d6e4332021-02-22 16:13:50 -0500103 module.bazelHandler = &objectBazelHandler{module: module}
Peter Collingbourne486e42c2018-10-25 10:53:44 -0700104
105 // Clang's address-significance tables are incompatible with ld -r.
106 module.compiler.appendCflags([]string{"-fno-addrsig"})
107
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000108 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500109
Colin Cross4d9c2d12016-07-29 12:48:20 -0700110 return module.Init()
111}
112
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500113// For bp2build conversion.
114type bazelObjectAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400115 Srcs bazel.LabelListAttribute
116 Deps bazel.LabelListAttribute
Jingwen Chen5d864492021-02-24 07:20:12 -0500117 Copts bazel.StringListAttribute
Liz Kammera060c452021-03-24 10:14:47 -0400118 Asflags []string
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500119 Local_include_dirs []string
120}
121
122type bazelObject struct {
123 android.BazelTargetModuleBase
124 bazelObjectAttributes
125}
126
127func (m *bazelObject) Name() string {
128 return m.BaseModuleName()
129}
130
131func (m *bazelObject) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
132
133func 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.
142func ObjectBp2Build(ctx android.TopDownMutatorContext) {
143 m, ok := ctx.Module().(*Module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500144 if !ok || !m.ConvertWithBp2build(ctx) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500145 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 Chen5d864492021-02-24 07:20:12 -0500158 // Set arch-specific configurable attributes
Jingwen Chen07027912021-03-15 06:02:43 -0400159 var srcs bazel.LabelListAttribute
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500160 var localIncludeDirs []string
Liz Kammera060c452021-03-24 10:14:47 -0400161 var asFlags []string
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500162 for _, props := range m.compiler.compilerProps() {
163 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400164 srcs = bazel.MakeLabelListAttribute(
165 android.BazelLabelForModuleSrcExcludes(
166 ctx,
167 baseCompilerProps.Srcs,
168 baseCompilerProps.Exclude_srcs))
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500169 localIncludeDirs = baseCompilerProps.Local_include_dirs
170 break
171 }
172 }
173
Liz Kammera4aa4302021-03-18 16:56:36 -0400174 if c, ok := m.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
175 localIncludeDirs = append(localIncludeDirs, ".")
176 }
177
Jingwen Chen07027912021-03-15 06:02:43 -0400178 var deps bazel.LabelListAttribute
Jingwen Chendb120242021-02-23 00:46:47 -0500179 for _, props := range m.linker.linkerProps() {
180 if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400181 deps = bazel.MakeLabelListAttribute(
182 android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
Jingwen Chendb120242021-02-23 00:46:47 -0500183 }
184 }
185
Liz Kammera060c452021-03-24 10:14:47 -0400186 productVariableProps := android.ProductVariableProperties(ctx)
187 if props, exists := productVariableProps["Asflags"]; exists {
188 // TODO(b/183595873): consider deduplicating handling of product variable properties
189 for _, prop := range props {
190 flags, ok := prop.Property.([]string)
191 if !ok {
192 ctx.ModuleErrorf("Could not convert product variable asflag property")
193 return
194 }
195 // TODO(b/183595873) handle other product variable usages -- as selects?
196 if newFlags, subbed := bazel.TryVariableSubstitutions(flags, prop.ProductConfigVariable); subbed {
197 asFlags = append(asFlags, newFlags...)
198 }
199 }
200 }
201 // TODO(b/183595872) warn/error if we're not handling product variables
202
Jingwen Chen5d864492021-02-24 07:20:12 -0500203 for arch, p := range m.GetArchProperties(&BaseCompilerProperties{}) {
204 if cProps, ok := p.(*BaseCompilerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400205 srcs.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcExcludes(ctx, cProps.Srcs, cProps.Exclude_srcs))
Jingwen Chen5d864492021-02-24 07:20:12 -0500206 }
207 }
Jingwen Chen5d864492021-02-24 07:20:12 -0500208
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500209 attrs := &bazelObjectAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -0400210 Srcs: srcs,
Jingwen Chendb120242021-02-23 00:46:47 -0500211 Deps: deps,
Jingwen Chenc1c26502021-04-05 10:35:13 +0000212 Copts: bp2BuildParseCflags(ctx, m),
Liz Kammera060c452021-03-24 10:14:47 -0400213 Asflags: asFlags,
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500214 Local_include_dirs: localIncludeDirs,
215 }
216
Liz Kammerfc46bc12021-02-19 11:06:17 -0500217 props := bazel.BazelTargetModuleProperties{
218 Rule_class: "cc_object",
219 Bzl_load_location: "//build/bazel/rules:cc_object.bzl",
220 }
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500221
Liz Kammerfc46bc12021-02-19 11:06:17 -0500222 ctx.CreateBazelTargetModule(BazelObjectFactory, m.Name(), props, attrs)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500223}
224
Colin Cross4d9c2d12016-07-29 12:48:20 -0700225func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700226 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227}
228
Colin Cross42742b82016-08-01 13:20:05 -0700229func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700230 return []interface{}{&object.Properties}
231}
232
Colin Cross42742b82016-08-01 13:20:05 -0700233func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700234
Colin Cross37047f12016-12-13 17:06:13 -0800235func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100236 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700237 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
238 return deps
239}
240
Pete Bentley74c9bba2019-08-16 20:25:06 +0100241func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800242 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700243
Pete Bentley74c9bba2019-08-16 20:25:06 +0100244 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800245 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100246 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
247 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 return flags
249}
250
251func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700252 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700253
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700254 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700255
256 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700257 builderFlags := flagsToBuilderFlags(flags)
258
Pete Bentleyab65ba92019-10-18 12:39:56 +0100259 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700260 outputFile = objs.objFiles[0]
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 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500264 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700265 builderFlags, output)
266 outputFile = output
267 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700268 } else {
269 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700270 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700271
Nan Zhang0007d812017-11-07 10:57:05 -0800272 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700273 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500274 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700275 builderFlags, output)
276 output = input
277 }
278
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500279 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700280 }
281
282 ctx.CheckbuildFile(outputFile)
283 return outputFile
284}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900285
286func (object *objectLinker) unstrippedOutputFilePath() android.Path {
287 return nil
288}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700289
290func (object *objectLinker) nativeCoverage() bool {
291 return true
292}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900293
294func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
295 return android.OptionalPath{}
296}
Inseob Kim1042d292020-06-01 23:23:05 +0900297
298func (object *objectLinker) object() bool {
299 return true
300}
Dan Albert92fe7402020-07-15 13:33:30 -0700301
302func (object *objectLinker) isCrt() bool {
303 return Bool(object.Properties.Crt)
304}