blob: 778d131ba9887fe236271f1233f57065dce8094b [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"
21)
22
23//
24// Objects (for crt*.o)
25//
26
27func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070028 android.RegisterModuleType("cc_object", ObjectFactory)
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000029 android.RegisterSdkMemberType(ccObjectSdkMemberType)
30}
31
32var ccObjectSdkMemberType = &librarySdkMemberType{
33 SdkMemberTypeBase: android.SdkMemberTypeBase{
34 PropertyName: "native_objects",
35 SupportsSdk: true,
36 },
37 prebuiltModuleType: "cc_prebuilt_object",
38 linkTypes: nil,
Colin Cross4d9c2d12016-07-29 12:48:20 -070039}
40
41type objectLinker struct {
Colin Crossb916a382016-07-29 17:28:03 -070042 *baseLinker
Colin Cross4d9c2d12016-07-29 12:48:20 -070043 Properties ObjectLinkerProperties
44}
45
Pete Bentley74c9bba2019-08-16 20:25:06 +010046type ObjectLinkerProperties struct {
47 // list of modules that should only provide headers for this module.
48 Header_libs []string `android:"arch_variant,variant_prepend"`
49
50 // names of other cc_object modules to link into this module using partial linking
51 Objs []string `android:"arch_variant"`
52
53 // if set, add an extra objcopy --prefix-symbols= step
54 Prefix_symbols *string
55
56 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
57 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070058
59 // Indicates that this module is a CRT object. CRT objects will be split
60 // into a variant per-API level between min_sdk_version and current.
61 Crt *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010062}
63
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000064func newObject() *Module {
65 module := newBaseModule(android.HostAndDeviceSupported, android.MultilibBoth)
66 module.sanitize = &sanitize{}
67 module.stl = &stl{}
68 return module
69}
70
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070071// cc_object runs the compiler without running the linker. It is rarely
72// necessary, but sometimes used to generate .s files from .c files to use as
73// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070074func ObjectFactory() android.Module {
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000075 module := newObject()
Colin Crossb916a382016-07-29 17:28:03 -070076 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -070077 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -070078 }
79 module.compiler = NewBaseCompiler()
Peter Collingbourne486e42c2018-10-25 10:53:44 -070080
81 // Clang's address-significance tables are incompatible with ld -r.
82 module.compiler.appendCflags([]string{"-fno-addrsig"})
83
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000084 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Colin Cross4d9c2d12016-07-29 12:48:20 -070085 return module.Init()
86}
87
88func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -070089 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -070090}
91
Colin Cross42742b82016-08-01 13:20:05 -070092func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -070093 return []interface{}{&object.Properties}
94}
95
Colin Cross42742b82016-08-01 13:20:05 -070096func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -070097
Colin Cross37047f12016-12-13 17:06:13 -080098func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Colin Cross87dd9632017-11-03 13:31:05 -070099 if ctx.useVndk() && ctx.toolchain().Bionic() {
Dan Willemsen0c162932017-09-18 16:33:37 -0700100 // Needed for VNDK builds where bionic headers aren't automatically added.
101 deps.LateSharedLibs = append(deps.LateSharedLibs, "libc")
102 }
103
Paul Duffina37832a2019-07-18 12:31:26 +0100104 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700105 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
106 return deps
107}
108
Pete Bentley74c9bba2019-08-16 20:25:06 +0100109func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross4af21ed2019-11-04 09:37:55 -0800110 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainClangLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700111
Pete Bentley74c9bba2019-08-16 20:25:06 +0100112 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800113 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100114 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
115 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700116 return flags
117}
118
119func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700120 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700121
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700122 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700123
124 var outputFile android.Path
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700125 builderFlags := flagsToBuilderFlags(flags)
126
Pete Bentleyab65ba92019-10-18 12:39:56 +0100127 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700128 outputFile = objs.objFiles[0]
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700129
Nan Zhang0007d812017-11-07 10:57:05 -0800130 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700131 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -0800132 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), outputFile,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700133 builderFlags, output)
134 outputFile = output
135 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700136 } else {
137 output := android.PathForModuleOut(ctx, ctx.ModuleName()+objectExtension)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700138 outputFile = output
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700139
Nan Zhang0007d812017-11-07 10:57:05 -0800140 if String(object.Properties.Prefix_symbols) != "" {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700141 input := android.PathForModuleOut(ctx, "unprefixed", ctx.ModuleName()+objectExtension)
Nan Zhang0007d812017-11-07 10:57:05 -0800142 TransformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700143 builderFlags, output)
144 output = input
145 }
146
Dan Willemsen724ab5d2019-09-19 10:50:18 -0700147 TransformObjsToObj(ctx, objs.objFiles, builderFlags, output, flags.LdFlagsDeps)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700148 }
149
150 ctx.CheckbuildFile(outputFile)
151 return outputFile
152}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900153
154func (object *objectLinker) unstrippedOutputFilePath() android.Path {
155 return nil
156}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700157
158func (object *objectLinker) nativeCoverage() bool {
159 return true
160}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900161
162func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
163 return android.OptionalPath{}
164}
Inseob Kim1042d292020-06-01 23:23:05 +0900165
166func (object *objectLinker) object() bool {
167 return true
168}
Dan Albert92fe7402020-07-15 13:33:30 -0700169
170func (object *objectLinker) isCrt() bool {
171 return Bool(object.Properties.Crt)
172}