blob: 8f0a3105b7e1d16250573c74922f9176412dc3b6 [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
Pete Bentley74c9bba2019-08-16 20:25:06 +010049type ObjectLinkerProperties struct {
50 // list of modules that should only provide headers for this module.
51 Header_libs []string `android:"arch_variant,variant_prepend"`
52
53 // names of other cc_object modules to link into this module using partial linking
54 Objs []string `android:"arch_variant"`
55
56 // if set, add an extra objcopy --prefix-symbols= step
57 Prefix_symbols *string
58
59 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
60 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070061
62 // Indicates that this module is a CRT object. CRT objects will be split
63 // into a variant per-API level between min_sdk_version and current.
64 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010065}
66
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000067func newObject() *Module {
68 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
69 module.sanitize = &sanitize{}
70 module.stl = &stl{}
71 return module
72}
73
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070074// cc_object runs the compiler without running the linker. It is rarely
75// necessary, but sometimes used to generate .s files from .c files to use as
76// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070077func ObjectFactory() android.Module {
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000078 module := newObject()
Colin Crossb916a382016-07-29 17:28:03 -070079 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -070080 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -070081 }
82 module.compiler = NewBaseCompiler()
Peter Collingbourne486e42c2018-10-25 10:53:44 -070083
84 // Clang's address-significance tables are incompatible with ld -r.
85 module.compiler.appendCflags([]string{"-fno-addrsig"})
86
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000087 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050088
Colin Cross4d9c2d12016-07-29 12:48:20 -070089 return module.Init()
90}
91
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050092// For bp2build conversion.
93type bazelObjectAttributes struct {
94 Srcs bazel.LabelList
95 Copts []string
96 Local_include_dirs []string
97}
98
99type bazelObject struct {
100 android.BazelTargetModuleBase
101 bazelObjectAttributes
102}
103
104func (m *bazelObject) Name() string {
105 return m.BaseModuleName()
106}
107
108func (m *bazelObject) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
109
110func BazelObjectFactory() android.Module {
111 module := &bazelObject{}
112 module.AddProperties(&module.bazelObjectAttributes)
113 android.InitBazelTargetModule(module)
114 return module
115}
116
117// ObjectBp2Build is the bp2build converter from cc_object modules to the
118// Bazel equivalent target, plus any necessary include deps for the cc_object.
119func ObjectBp2Build(ctx android.TopDownMutatorContext) {
120 m, ok := ctx.Module().(*Module)
Liz Kammerea6666f2021-02-17 10:17:28 -0500121 if !ok || !m.ConvertWithBp2build() {
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500122 return
123 }
124
125 // a Module can be something other than a cc_object.
126 if ctx.ModuleType() != "cc_object" {
127 return
128 }
129
130 if m.compiler == nil {
131 // a cc_object must have access to the compiler decorator for its props.
132 ctx.ModuleErrorf("compiler must not be nil for a cc_object module")
133 }
134
135 var copts []string
136 var srcs []string
137 var localIncludeDirs []string
138 for _, props := range m.compiler.compilerProps() {
139 if baseCompilerProps, ok := props.(*BaseCompilerProperties); ok {
140 copts = baseCompilerProps.Cflags
141 srcs = baseCompilerProps.Srcs
142 localIncludeDirs = baseCompilerProps.Local_include_dirs
143 break
144 }
145 }
146
147 attrs := &bazelObjectAttributes{
148 Srcs: android.BazelLabelForModuleSrc(ctx, srcs),
149 Copts: copts,
150 Local_include_dirs: localIncludeDirs,
151 }
152
153 props := bazel.NewBazelTargetModuleProperties(
154 m.Name(),
155 "cc_object",
156 "//build/bazel/rules:cc_object.bzl",
157 )
158
159 ctx.CreateBazelTargetModule(BazelObjectFactory, props, attrs)
160}
161
Colin Cross4d9c2d12016-07-29 12:48:20 -0700162func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700163 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700164}
165
Colin Cross42742b82016-08-01 13:20:05 -0700166func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700167 return []interface{}{&object.Properties}
168}
169
Colin Cross42742b82016-08-01 13:20:05 -0700170func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700171
Colin Cross37047f12016-12-13 17:06:13 -0800172func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Paul Duffina37832a2019-07-18 12:31:26 +0100173 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700174 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
175 return deps
176}
177
Pete Bentley74c9bba2019-08-16 20:25:06 +0100178func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800179 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700180
Pete Bentley74c9bba2019-08-16 20:25:06 +0100181 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800182 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100183 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
184 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700185 return flags
186}
187
188func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700189 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700190
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700191 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700192
193 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700194 builderFlags := flagsToBuilderFlags(flags)
195
Pete Bentleyab65ba92019-10-18 12:39:56 +0100196 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700197 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700198
Nan Zhang0007d812017-11-07 10:57:05 -0800199 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700200 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500201 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700202 builderFlags, output)
203 outputFile = output
204 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700205 } else {
206 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700207 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700208
Nan Zhang0007d812017-11-07 10:57:05 -0800209 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700210 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500211 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700212 builderFlags, output)
213 output = input
214 }
215
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500216 transformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700217 }
218
219 ctx.CheckbuildFile(outputFile)
220 return outputFile
221}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900222
223func (object *objectLinker) unstrippedOutputFilePath() android.Path {
224 return nil
225}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700226
227func (object *objectLinker) nativeCoverage() bool {
228 return true
229}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900230
231func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
232 return android.OptionalPath{}
233}
Inseob Kim1042d292020-06-01 23:23:05 +0900234
235func (object *objectLinker) object() bool {
236 return true
237}
Dan Albert92fe7402020-07-15 13:33:30 -0700238
239func (object *objectLinker) isCrt() bool {
240 return Bool(object.Properties.Crt)
241}