blob: abc3e832a4de8518390159909b76da0dbfd3f32c [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 Kammer57355682021-03-16 13:34:48 -040056 // TODO(b/181794963): restore mixed builds once cc_object incompatibility resolved
57 return false
Chris Parsons8d6e4332021-02-22 16:13:50 -050058}
59
Pete Bentley74c9bba2019-08-16 20:25:06 +010060type ObjectLinkerProperties struct {
61 // list of modules that should only provide headers for this module.
62 Header_libs []string `android:"arch_variant,variant_prepend"`
63
64 // names of other cc_object modules to link into this module using partial linking
65 Objs []string `android:"arch_variant"`
66
67 // if set, add an extra objcopy --prefix-symbols= step
68 Prefix_symbols *string
69
70 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
71 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070072
73 // Indicates that this module is a CRT object. CRT objects will be split
74 // into a variant per-API level between min_sdk_version and current.
75 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010076}
77
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000078func newObject() *Module {
79 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
80 module.sanitize = &sanitize{}
81 module.stl = &stl{}
82 return module
83}
84
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070085// cc_object runs the compiler without running the linker. It is rarely
86// necessary, but sometimes used to generate .s files from .c files to use as
87// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070088func ObjectFactory() android.Module {
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000089 module := newObject()
Colin Crossb916a382016-07-29 17:28:03 -070090 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -070091 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -070092 }
93 module.compiler = NewBaseCompiler()
Chris Parsons8d6e4332021-02-22 16:13:50 -050094 module.bazelHandler = &objectBazelHandler{module: module}
Peter Collingbourne486e42c2018-10-25 10:53:44 -070095
96 // Clang's address-significance tables are incompatible with ld -r.
97 module.compiler.appendCflags([]string{"-fno-addrsig"})
98
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000099 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500100
Colin Cross4d9c2d12016-07-29 12:48:20 -0700101 return module.Init()
102}
103
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500104// For bp2build conversion.
105type bazelObjectAttributes struct {
Jingwen Chen07027912021-03-15 06:02:43 -0400106 Srcs bazel.LabelListAttribute
107 Deps bazel.LabelListAttribute
Jingwen Chen5d864492021-02-24 07:20:12 -0500108 Copts bazel.StringListAttribute
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500109 Local_include_dirs []string
110}
111
112type bazelObject struct {
113 android.BazelTargetModuleBase
114 bazelObjectAttributes
115}
116
117func (m *bazelObject) Name() string {
118 return m.BaseModuleName()
119}
120
121func (m *bazelObject) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
122
123func BazelObjectFactory() android.Module {
124 module := &bazelObject{}
125 module.AddProperties(&module.bazelObjectAttributes)
126 android.InitBazelTargetModule(module)
127 return module
128}
129
130// ObjectBp2Build is the bp2build converter from cc_object modules to the
131// Bazel equivalent target, plus any necessary include deps for the cc_object.
132func ObjectBp2Build(ctx android.TopDownMutatorContext) {
133 m, ok := ctx.Module().(*Module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500134 if !ok || !m.ConvertWithBp2build(ctx) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500135 return
136 }
137
138 // a Module can be something other than a cc_object.
139 if ctx.ModuleType() != "cc_object" {
140 return
141 }
142
143 if m.compiler == nil {
144 // a cc_object must have access to the compiler decorator for its props.
145 ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
146 }
147
Jingwen Chen5d864492021-02-24 07:20:12 -0500148 // Set arch-specific configurable attributes
149 var copts bazel.StringListAttribute
Jingwen Chen07027912021-03-15 06:02:43 -0400150 var srcs bazel.LabelListAttribute
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500151 var localIncludeDirs []string
152 for _, props := range m.compiler.compilerProps() {
153 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
Jingwen Chen5d864492021-02-24 07:20:12 -0500154 copts.Value = baseCompilerProps.Cflags
Jingwen Chen07027912021-03-15 06:02:43 -0400155 srcs = bazel.MakeLabelListAttribute(
156 android.BazelLabelForModuleSrcExcludes(
157 ctx,
158 baseCompilerProps.Srcs,
159 baseCompilerProps.Exclude_srcs))
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500160 localIncludeDirs = baseCompilerProps.Local_include_dirs
161 break
162 }
163 }
164
Liz Kammera4aa4302021-03-18 16:56:36 -0400165 if c, ok := m.compiler.(*baseCompiler); ok && c.includeBuildDirectory() {
166 localIncludeDirs = append(localIncludeDirs, ".")
167 }
168
Jingwen Chen07027912021-03-15 06:02:43 -0400169 var deps bazel.LabelListAttribute
Jingwen Chendb120242021-02-23 00:46:47 -0500170 for _, props := range m.linker.linkerProps() {
171 if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400172 deps = bazel.MakeLabelListAttribute(
173 android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
Jingwen Chendb120242021-02-23 00:46:47 -0500174 }
175 }
176
Jingwen Chen5d864492021-02-24 07:20:12 -0500177 for arch, p := range m.GetArchProperties(&BaseCompilerProperties{}) {
178 if cProps, ok := p.(*BaseCompilerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400179 srcs.SetValueForArch(arch.Name, android.BazelLabelForModuleSrcExcludes(ctx, cProps.Srcs, cProps.Exclude_srcs))
Jingwen Chen5d864492021-02-24 07:20:12 -0500180 copts.SetValueForArch(arch.Name, cProps.Cflags)
181 }
182 }
Jingwen Chen5d864492021-02-24 07:20:12 -0500183
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500184 attrs := &bazelObjectAttributes{
Jingwen Chen07027912021-03-15 06:02:43 -0400185 Srcs: srcs,
Jingwen Chendb120242021-02-23 00:46:47 -0500186 Deps: deps,
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500187 Copts: copts,
188 Local_include_dirs: localIncludeDirs,
189 }
190
Liz Kammerfc46bc12021-02-19 11:06:17 -0500191 props := bazel.BazelTargetModuleProperties{
192 Rule_class: "cc_object",
193 Bzl_load_location: "//build/bazel/rules:cc_object.bzl",
194 }
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500195
Liz Kammerfc46bc12021-02-19 11:06:17 -0500196 ctx.CreateBazelTargetModule(BazelObjectFactory, m.Name(), props, attrs)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500197}
198
Colin Cross4d9c2d12016-07-29 12:48:20 -0700199func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700200 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201}
202
Colin Cross42742b82016-08-01 13:20:05 -0700203func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700204 return []interface{}{&object.Properties}
205}
206
Colin Cross42742b82016-08-01 13:20:05 -0700207func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700208
Colin Cross37047f12016-12-13 17:06:13 -0800209func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100210 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700211 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
212 return deps
213}
214
Pete Bentley74c9bba2019-08-16 20:25:06 +0100215func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800216 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217
Pete Bentley74c9bba2019-08-16 20:25:06 +0100218 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800219 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100220 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
221 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700222 return flags
223}
224
225func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700226 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700227
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700228 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700229
230 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700231 builderFlags := flagsToBuilderFlags(flags)
232
Pete Bentleyab65ba92019-10-18 12:39:56 +0100233 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700234 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700235
Nan Zhang0007d812017-11-07 10:57:05 -0800236 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700237 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500238 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700239 builderFlags, output)
240 outputFile = output
241 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700242 } else {
243 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700244 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700245
Nan Zhang0007d812017-11-07 10:57:05 -0800246 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700247 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500248 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700249 builderFlags, output)
250 output = input
251 }
252
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500253 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700254 }
255
256 ctx.CheckbuildFile(outputFile)
257 return outputFile
258}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900259
260func (object *objectLinker) unstrippedOutputFilePath() android.Path {
261 return nil
262}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700263
264func (object *objectLinker) nativeCoverage() bool {
265 return true
266}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900267
268func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
269 return android.OptionalPath{}
270}
Inseob Kim1042d292020-06-01 23:23:05 +0900271
272func (object *objectLinker) object() bool {
273 return true
274}
Dan Albert92fe7402020-07-15 13:33:30 -0700275
276func (object *objectLinker) isCrt() bool {
277 return Bool(object.Properties.Crt)
278}