blob: afbec403db021b0dab6e5eddb74aa6e403f82c4a [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"
Colin Crossfb44cd22022-09-09 15:11:16 -070019 "strings"
Colin Cross4d9c2d12016-07-29 12:48:20 -070020
Colin Cross4d9c2d12016-07-29 12:48:20 -070021 "android/soong/android"
Cole Faust9d6c7dc2024-08-19 14:39:19 -070022
23 "github.com/google/blueprint/proptools"
Colin Cross4d9c2d12016-07-29 12:48:20 -070024)
25
26//
27// Objects (for crt*.o)
28//
29
30func init() {
Colin Crosse40b4ea2018-10-02 22:25:58 -070031 android.RegisterModuleType("cc_object", ObjectFactory)
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000032 android.RegisterSdkMemberType(ccObjectSdkMemberType)
Jingwen Chen8c1b97e2021-02-18 03:21:34 -050033
Martin Stjernholmcd07bce2020-03-10 22:37:59 +000034}
35
36var ccObjectSdkMemberType = &librarySdkMemberType{
37 SdkMemberTypeBase: android.SdkMemberTypeBase{
38 PropertyName: "native_objects",
39 SupportsSdk: true,
40 },
41 prebuiltModuleType: "cc_prebuilt_object",
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
Dan Albert5b0d4f32023-04-04 23:22:11 +000047
48 // Location of the object in the sysroot. Empty if the object is not
49 // included in the NDK.
50 ndkSysrootPath android.Path
Colin Cross4d9c2d12016-07-29 12:48:20 -070051}
52
Pete Bentley74c9bba2019-08-16 20:25:06 +010053type ObjectLinkerProperties struct {
Colin Cross137d7da2021-06-21 16:41:29 -070054 // list of static library modules that should only provide headers for this module.
Cole Faust9d6c7dc2024-08-19 14:39:19 -070055 Static_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
Colin Cross137d7da2021-06-21 16:41:29 -070056
57 // list of shared library modules should only provide headers for this module.
Cole Faust9d6c7dc2024-08-19 14:39:19 -070058 Shared_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
Colin Cross137d7da2021-06-21 16:41:29 -070059
Pete Bentley74c9bba2019-08-16 20:25:06 +010060 // list of modules that should only provide headers for this module.
Cole Faust9d6c7dc2024-08-19 14:39:19 -070061 Header_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"`
Pete Bentley74c9bba2019-08-16 20:25:06 +010062
Colin Cross137d7da2021-06-21 16:41:29 -070063 // list of default libraries that will provide headers for this module. If unset, generally
64 // defaults to libc, libm, and libdl. Set to [] to prevent using headers from the defaults.
Colin Cross6b8f4252021-07-22 11:39:44 -070065 System_shared_libs []string `android:"arch_variant"`
Colin Cross137d7da2021-06-21 16:41:29 -070066
Pete Bentley74c9bba2019-08-16 20:25:06 +010067 // names of other cc_object modules to link into this module using partial linking
68 Objs []string `android:"arch_variant"`
69
70 // if set, add an extra objcopy --prefix-symbols= step
71 Prefix_symbols *string
72
73 // if set, the path to a linker script to pass to ld -r when combining multiple object files.
74 Linker_script *string `android:"path,arch_variant"`
Dan Albert92fe7402020-07-15 13:33:30 -070075
76 // Indicates that this module is a CRT object. CRT objects will be split
77 // into a variant per-API level between min_sdk_version and current.
78 Crt *bool
Dan Albert5b0d4f32023-04-04 23:22:11 +000079
80 // Indicates that this module should not be included in the NDK sysroot.
81 // Only applies to CRT objects. Defaults to false.
82 Exclude_from_ndk_sysroot *bool
Pete Bentley74c9bba2019-08-16 20:25:06 +010083}
84
Colin Cross7cabd422021-06-25 14:21:04 -070085func newObject(hod android.HostOrDeviceSupported) *Module {
86 module := newBaseModule(hod, android.MultilibBoth)
Martin Stjernholm0b92ac82020-03-11 21:45:49 +000087 module.sanitize = &sanitize{}
88 module.stl = &stl{}
89 return module
90}
91
Patrice Arrudabaff0ce2019-03-26 10:39:49 -070092// cc_object runs the compiler without running the linker. It is rarely
93// necessary, but sometimes used to generate .s files from .c files to use as
94// input to a cc_genrule module.
Colin Crosse40b4ea2018-10-02 22:25:58 -070095func ObjectFactory() android.Module {
Colin Cross7cabd422021-06-25 14:21:04 -070096 module := newObject(android.HostAndDeviceSupported)
Colin Crossb916a382016-07-29 17:28:03 -070097 module.linker = &objectLinker{
Peter Collingbourne1c648b82019-09-26 12:24:45 -070098 baseLinker: NewBaseLinker(module.sanitize),
Colin Crossb916a382016-07-29 17:28:03 -070099 }
100 module.compiler = NewBaseCompiler()
Peter Collingbourne486e42c2018-10-25 10:53:44 -0700101
102 // Clang's address-significance tables are incompatible with ld -r.
103 module.compiler.appendCflags([]string{"-fno-addrsig"})
104
Martin Stjernholmcd07bce2020-03-10 22:37:59 +0000105 module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType}
Jingwen Chen8c1b97e2021-02-18 03:21:34 -0500106
Colin Cross4d9c2d12016-07-29 12:48:20 -0700107 return module.Init()
108}
109
110func (object *objectLinker) appendLdflags(flags []string) {
Jeff Gastonaf3cc2d2017-09-27 17:01:44 -0700111 panic(fmt.Errorf("appendLdflags on objectLinker not supported"))
Colin Cross4d9c2d12016-07-29 12:48:20 -0700112}
113
Colin Cross42742b82016-08-01 13:20:05 -0700114func (object *objectLinker) linkerProps() []interface{} {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700115 return []interface{}{&object.Properties}
116}
117
Colin Cross42742b82016-08-01 13:20:05 -0700118func (*objectLinker) linkerInit(ctx BaseModuleContext) {}
Colin Cross4d9c2d12016-07-29 12:48:20 -0700119
Colin Cross37047f12016-12-13 17:06:13 -0800120func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps {
Cole Faust9d6c7dc2024-08-19 14:39:19 -0700121 deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs.GetOrDefault(ctx, nil)...)
122 deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs.GetOrDefault(ctx, nil)...)
123 deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs.GetOrDefault(ctx, nil)...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700124 deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700125
Colin Cross6b8f4252021-07-22 11:39:44 -0700126 deps.SystemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700127 if deps.SystemSharedLibs == nil {
Colin Cross6b8f4252021-07-22 11:39:44 -0700128 // Provide a default set of shared libraries if system_shared_libs is unspecified.
Colin Cross137d7da2021-06-21 16:41:29 -0700129 // Note: If an empty list [] is specified, it implies that the module declines the
130 // default shared libraries.
131 deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...)
132 }
133 deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700134 return deps
135}
136
Pete Bentley74c9bba2019-08-16 20:25:06 +0100137func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags {
Colin Cross33bac242021-07-14 17:03:16 -0700138 flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags())
Colin Cross4d9c2d12016-07-29 12:48:20 -0700139
Pete Bentley74c9bba2019-08-16 20:25:06 +0100140 if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() {
Colin Cross4af21ed2019-11-04 09:37:55 -0800141 flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String())
Pete Bentley74c9bba2019-08-16 20:25:06 +0100142 flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path())
143 }
Colin Cross4d9c2d12016-07-29 12:48:20 -0700144 return flags
145}
146
147func (object *objectLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700148 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Cross4d9c2d12016-07-29 12:48:20 -0700149
Dan Willemsen5cb580f2016-09-26 17:33:01 -0700150 objs = objs.Append(deps.Objs)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700151
Dan Albert5b0d4f32023-04-04 23:22:11 +0000152 var output android.WritablePath
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700153 builderFlags := flagsToBuilderFlags(flags)
Colin Crossfb44cd22022-09-09 15:11:16 -0700154 outputName := ctx.ModuleName()
155 if !strings.HasSuffix(outputName, objectExtension) {
156 outputName += objectExtension
157 }
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700158
Dan Albert5b0d4f32023-04-04 23:22:11 +0000159 // isForPlatform is terribly named and actually means isNotApex.
160 if Bool(object.Properties.Crt) &&
161 !Bool(object.Properties.Exclude_from_ndk_sysroot) && ctx.useSdk() &&
162 ctx.isSdkVariant() && ctx.isForPlatform() {
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700163
Dan Albert5b0d4f32023-04-04 23:22:11 +0000164 output = getVersionedLibraryInstallPath(ctx,
165 nativeApiLevelOrPanic(ctx, ctx.sdkVersion())).Join(ctx, outputName)
166 object.ndkSysrootPath = output
167 } else {
168 output = android.PathForModuleOut(ctx, outputName)
169 }
170
171 outputFile := output
172
173 if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" {
Nan Zhang0007d812017-11-07 10:57:05 -0800174 if String(object.Properties.Prefix_symbols) != "" {
Colin Crossfb44cd22022-09-09 15:11:16 -0700175 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), objs.objFiles[0],
176 builderFlags, output)
177 } else {
178 ctx.Build(pctx, android.BuildParams{
179 Rule: android.Cp,
180 Input: objs.objFiles[0],
181 Output: output,
182 })
183 }
184 } else {
Colin Crossdea1d032022-12-06 14:50:08 -0800185 outputAddrSig := android.PathForModuleOut(ctx, "addrsig", outputName)
186
Colin Crossfb44cd22022-09-09 15:11:16 -0700187 if String(object.Properties.Prefix_symbols) != "" {
188 input := android.PathForModuleOut(ctx, "unprefixed", outputName)
Chris Parsonsbf4f55f2020-11-23 17:02:44 -0500189 transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input,
Dan Willemsenefb1dd92017-09-18 22:47:20 -0700190 builderFlags, output)
191 output = input
192 }
193
Colin Crossdea1d032022-12-06 14:50:08 -0800194 transformObjsToObj(ctx, objs.objFiles, builderFlags, outputAddrSig, flags.LdFlagsDeps)
195
196 // ld -r reorders symbols and invalidates the .llvm_addrsig section, which then causes warnings
197 // if the resulting object is used with ld --icf=safe. Strip the .llvm_addrsig section to
198 // prevent the warnings.
199 transformObjectNoAddrSig(ctx, outputAddrSig, output)
Colin Cross4d9c2d12016-07-29 12:48:20 -0700200 }
201
202 ctx.CheckbuildFile(outputFile)
203 return outputFile
204}
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900205
Cole Faust9d6c7dc2024-08-19 14:39:19 -0700206func (object *objectLinker) linkerSpecifiedDeps(ctx android.ConfigAndErrorContext, module *Module, specifiedDeps specifiedDeps) specifiedDeps {
207 eval := module.ConfigurableEvaluator(ctx)
208 specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs.GetOrDefault(eval, nil)...)
Colin Cross137d7da2021-06-21 16:41:29 -0700209
Colin Cross6b8f4252021-07-22 11:39:44 -0700210 // Must distinguish nil and [] in system_shared_libs - ensure that [] in
Colin Cross137d7da2021-06-21 16:41:29 -0700211 // either input list doesn't come out as nil.
Colin Cross6b8f4252021-07-22 11:39:44 -0700212 if specifiedDeps.systemSharedLibs == nil {
213 specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs
Colin Cross137d7da2021-06-21 16:41:29 -0700214 } else {
Colin Cross6b8f4252021-07-22 11:39:44 -0700215 specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...)
Colin Cross137d7da2021-06-21 16:41:29 -0700216 }
217
218 return specifiedDeps
219}
220
Jiyong Parkaf6d8952019-01-31 12:21:23 +0900221func (object *objectLinker) unstrippedOutputFilePath() android.Path {
222 return nil
223}
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700224
Wei Li5f5d2712023-12-11 15:40:29 -0800225func (object *objectLinker) strippedAllOutputFilePath() android.Path {
226 panic("Not implemented.")
227}
228
Pirama Arumuga Nainar65c95ff2019-03-25 10:21:31 -0700229func (object *objectLinker) nativeCoverage() bool {
230 return true
231}
Jiyong Parkee9a98d2019-08-09 14:44:36 +0900232
233func (object *objectLinker) coverageOutputFilePath() android.OptionalPath {
234 return android.OptionalPath{}
235}
Inseob Kim1042d292020-06-01 23:23:05 +0900236
237func (object *objectLinker) object() bool {
238 return true
239}
Dan Albert92fe7402020-07-15 13:33:30 -0700240
241func (object *objectLinker) isCrt() bool {
242 return Bool(object.Properties.Crt)
243}
Colin Cross4a9e6ec2023-12-18 15:29:41 -0800244
245func (object *objectLinker) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) {
246 object.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON)
247 moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"}
248}