blob: 99b257aaa9ade1972799e5d31b8464a6b38efded [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 {
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000050 android.BazelHandler
Chris Parsons8d6e4332021-02-22 16:13:50 -050051
52 module *Module
53}
54
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux0d990452021-08-11 16:46:13 +000055func (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 {
Colin Cross137d7da2021-06-21 16:41:29 -070070 // list of static library modules that should only provide headers for this module.
71 Static_libs []string `android:"arch_variant,variant_prepend"`
72
73 // list of shared library modules should only provide headers for this module.
74 Shared_libs []string `android:"arch_variant"`
75
Pete Bentley74c9bba2019-08-16 20:25:06 +010076 // list of modules that should only provide headers for this module.
77 Header_libs []string `android:"arch_variant,variant_prepend"`
78
Colin Cross137d7da2021-06-21 16:41:29 -070079 // list of default libraries that will provide headers for this module. If unset, generally
80 // defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
Colin Cross6b8f4252021-07-22 11:39:44 -070081 System_shared_libs []string `android:"arch_variant"`
Colin Cross137d7da2021-06-21 16:41:29 -070082
Pete Bentley74c9bba2019-08-16 20:25:06 +010083 // names of other cc_object modules to link into this module using partial linking
84 Objs []string `android:"arch_variant"`
85
86 // if set, add an extra objcopy --prefix-symbols= step
87 Prefix_symbols *string
88
89 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
90 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070091
92 // Indicates that this module is a CRT object. CRT objects will be split
93 // into a variant per-API level between min_sdk_version and current.
94 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010095}
96
Colin Cross7cabd422021-06-25 14:21:04 -070097func newObject(hod android.HostOrDeviceSupported) *Module {
98 module := newBaseModule(hod, android.MultilibBoth)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000099 module.sanitize = &sanitize{}
100 module.stl = &stl{}
101 return module
102}
103
Patrice Arrudabaff0ce2019-03-26 10:39:49 -0700104// cc_object runs the compiler without running the linker. It is rarely
105// necessary, but sometimes used to generate .s files from .c files to use as
106// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -0700107func ObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -0700108 module := newObject(android.HostAndDeviceSupported)
Colin Crossb916a382016-07-29 17:28:03 -0700109 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -0700110 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -0700111 }
112 module.compiler = NewBaseCompiler()
Chris Parsons8d6e4332021-02-22 16:13:50 -0500113 module.bazelHandler = &objectBazelHandler{module: module}
Peter Collingbourne486e42c2018-10-25 10:53:44 -0700114
115 // Clang's address-significance tables are incompatible with ld -r.
116 module.compiler.appendCflags([]string{"-fno-addrsig"})
117
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000118 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500119
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 {
Liz Kammer35687bc2021-09-10 10:07:07 -0400125 Srcs bazel.LabelListAttribute
126 Srcs_as bazel.LabelListAttribute
127 Hdrs bazel.LabelListAttribute
128 Deps bazel.LabelListAttribute
129 Copts bazel.StringListAttribute
130 Asflags bazel.StringListAttribute
131 Local_includes bazel.StringListAttribute
132 Absolute_includes bazel.StringListAttribute
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500133}
134
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500135// ObjectBp2Build is the bp2build converter from cc_object modules to the
136// Bazel equivalent target, plus any necessary include deps for the cc_object.
137func ObjectBp2Build(ctx android.TopDownMutatorContext) {
138 m, ok := ctx.Module().(*Module)
Jingwen Chen12b4c272021-03-10 02:05:59 -0500139 if !ok || !m.ConvertWithBp2build(ctx) {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500140 return
141 }
142
143 // a Module can be something other than a cc_object.
144 if ctx.ModuleType() != "cc_object" {
145 return
146 }
147
148 if m.compiler == nil {
149 // a cc_object must have access to the compiler decorator for its props.
150 ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
151 }
152
Jingwen Chen5d864492021-02-24 07:20:12 -0500153 // Set arch-specific configurable attributes
Jingwen Chen107c0de2021-04-09 10:43:12 +0000154 compilerAttrs := bp2BuildParseCompilerProps(ctx, m)
Jingwen Chen07027912021-03-15 06:02:43 -0400155 var deps bazel.LabelListAttribute
Jingwen Chendb120242021-02-23 00:46:47 -0500156 for _, props := range m.linker.linkerProps() {
157 if objectLinkerProps, ok := props.(*ObjectLinkerProperties); ok {
Jingwen Chen07027912021-03-15 06:02:43 -0400158 deps = bazel.MakeLabelListAttribute(
159 android.BazelLabelForModuleDeps(ctx, objectLinkerProps.Objs))
Jingwen Chendb120242021-02-23 00:46:47 -0500160 }
161 }
162
Chris Parsons990c4f42021-05-25 12:10:58 -0400163 // Don't split cc_object srcs across languages. Doing so would add complexity,
164 // and this isn't typically done for cc_object.
165 srcs := compilerAttrs.srcs
166 srcs.Append(compilerAttrs.cSrcs)
Chris Parsons69fa9f92021-07-13 11:47:44 -0400167
168 asFlags := compilerAttrs.asFlags
169 if compilerAttrs.asSrcs.IsEmpty() {
170 // Skip asflags for BUILD file simplicity if there are no assembly sources.
171 asFlags = bazel.MakeStringListAttribute(nil)
172 }
Chris Parsons990c4f42021-05-25 12:10:58 -0400173
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500174 attrs := &bazelObjectAttributes{
Liz Kammer35687bc2021-09-10 10:07:07 -0400175 Srcs: srcs,
176 Srcs_as: compilerAttrs.asSrcs,
177 Deps: deps,
178 Copts: compilerAttrs.copts,
179 Asflags: asFlags,
180 Local_includes: compilerAttrs.localIncludes,
181 Absolute_includes: compilerAttrs.absoluteIncludes,
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500182 }
183
Liz Kammerfc46bc12021-02-19 11:06:17 -0500184 props := bazel.BazelTargetModuleProperties{
185 Rule_class: "cc_object",
186 Bzl_load_location: "//build/bazel/rules:cc_object.bzl",
187 }
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500188
Liz Kammer2ada09a2021-08-11 00:17:36 -0400189 ctx.CreateBazelTargetModule(m.Name(), props, attrs)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500190}
191
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700193 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700194}
195
Colin Cross42742b82016-08-01 13:20:05 -0700196func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700197 return []interface{}{&object.Properties}
198}
199
Colin Cross42742b82016-08-01 13:20:05 -0700200func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700201
Colin Cross37047f12016-12-13 17:06:13 -0800202func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100203 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700204 deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs...)
205 deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700206 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700207
Colin Cross6b8f4252021-07-22 11:39:44 -0700208 deps.SystemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700209 if deps.SystemSharedLibs == nil {
Colin Cross6b8f4252021-07-22 11:39:44 -0700210 // Provide a default set of shared libraries if system_shared_libs is unspecified.
Colin Cross137d7da2021-06-21 16:41:29 -0700211 // Note: If an empty list [] is specified, it implies that the module declines the
212 // default shared libraries.
213 deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
214 }
215 deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700216 return deps
217}
218
Pete Bentley74c9bba2019-08-16 20:25:06 +0100219func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross33bac242021-07-14 17:03:16 -0700220 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700221
Pete Bentley74c9bba2019-08-16 20:25:06 +0100222 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800223 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100224 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
225 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700226 return flags
227}
228
229func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700230 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700231
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700232 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700233
234 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700235 builderFlags := flagsToBuilderFlags(flags)
236
Pete Bentleyab65ba92019-10-18 12:39:56 +0100237 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700238 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700239
Nan Zhang0007d812017-11-07 10:57:05 -0800240 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700241 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500242 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700243 builderFlags, output)
244 outputFile = output
245 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700246 } else {
247 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700248 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700249
Nan Zhang0007d812017-11-07 10:57:05 -0800250 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700251 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500252 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700253 builderFlags, output)
254 output = input
255 }
256
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500257 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700258 }
259
260 ctx.CheckbuildFile(outputFile)
261 return outputFile
262}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900263
Colin Cross137d7da2021-06-21 16:41:29 -0700264func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps {
265 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...)
266
Colin Cross6b8f4252021-07-22 11:39:44 -0700267 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
Colin Cross137d7da2021-06-21 16:41:29 -0700268 // either input list doesn't come out as nil.
Colin Cross6b8f4252021-07-22 11:39:44 -0700269 if specifiedDeps.systemSharedLibs == nil {
270 specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700271 } else {
Colin Cross6b8f4252021-07-22 11:39:44 -0700272 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700273 }
274
275 return specifiedDeps
276}
277
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900278func (object *objectLinker) unstrippedOutputFilePath() android.Path {
279 return nil
280}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700281
282func (object *objectLinker) nativeCoverage() bool {
283 return true
284}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900285
286func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
287 return android.OptionalPath{}
288}
Inseob Kim1042d292020-06-01 23:23:05 +0900289
290func (object *objectLinker) object() bool {
291 return true
292}
Dan Albert92fe7402020-07-15 13:33:30 -0700293
294func (object *objectLinker) isCrt() bool {
295 return Bool(object.Properties.Crt)
296}