Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 1 | // 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 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 19 | "strings" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 20 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 21 | "android/soong/android" |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 22 | |
| 23 | "github.com/google/blueprint/proptools" |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | // |
| 27 | // Objects (for crt*.o) |
| 28 | // |
| 29 | |
| 30 | func init() { |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 31 | android.RegisterModuleType("cc_object", ObjectFactory) |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 32 | android.RegisterSdkMemberType(ccObjectSdkMemberType) |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 33 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | var ccObjectSdkMemberType = &librarySdkMemberType{ |
| 37 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 38 | PropertyName: "native_objects", |
| 39 | SupportsSdk: true, |
| 40 | }, |
| 41 | prebuiltModuleType: "cc_prebuilt_object", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | type objectLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 45 | *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 46 | Properties ObjectLinkerProperties |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 47 | |
| 48 | // Location of the object in the sysroot. Empty if the object is not |
| 49 | // included in the NDK. |
| 50 | ndkSysrootPath android.Path |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 53 | type ObjectLinkerProperties struct { |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 54 | // list of static library modules that should only provide headers for this module. |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 55 | Static_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"` |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 56 | |
| 57 | // list of shared library modules should only provide headers for this module. |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 58 | Shared_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"` |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 59 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 60 | // list of modules that should only provide headers for this module. |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 61 | Header_libs proptools.Configurable[[]string] `android:"arch_variant,variant_prepend"` |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 62 | |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 63 | // 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 Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 65 | System_shared_libs []string `android:"arch_variant"` |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 66 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 67 | // 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 Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 75 | |
| 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 Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 79 | |
| 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 Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 83 | } |
| 84 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 85 | func newObject(hod android.HostOrDeviceSupported) *Module { |
| 86 | module := newBaseModule(hod, android.MultilibBoth) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 87 | module.sanitize = &sanitize{} |
| 88 | module.stl = &stl{} |
| 89 | return module |
| 90 | } |
| 91 | |
Patrice Arruda | baff0ce | 2019-03-26 10:39:49 -0700 | [diff] [blame] | 92 | // 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 Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 95 | func ObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 96 | module := newObject(android.HostAndDeviceSupported) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 97 | module.linker = &objectLinker{ |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 98 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 99 | } |
| 100 | module.compiler = NewBaseCompiler() |
Peter Collingbourne | 486e42c | 2018-10-25 10:53:44 -0700 | [diff] [blame] | 101 | |
| 102 | // Clang's address-significance tables are incompatible with ld -r. |
| 103 | module.compiler.appendCflags([]string{"-fno-addrsig"}) |
| 104 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 105 | module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType} |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 106 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 107 | return module.Init() |
| 108 | } |
| 109 | |
| 110 | func (object *objectLinker) appendLdflags(flags []string) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 111 | panic(fmt.Errorf("appendLdflags on objectLinker not supported")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 114 | func (object *objectLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 115 | return []interface{}{&object.Properties} |
| 116 | } |
| 117 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 118 | func (*objectLinker) linkerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 119 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 120 | func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 121 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 124 | deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...) |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 125 | |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 126 | deps.SystemSharedLibs = object.Properties.System_shared_libs |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 127 | if deps.SystemSharedLibs == nil { |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 128 | // Provide a default set of shared libraries if system_shared_libs is unspecified. |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 129 | // 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 134 | return deps |
| 135 | } |
| 136 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 137 | func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 138 | flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 139 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 140 | if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 141 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String()) |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 142 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path()) |
| 143 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 144 | return flags |
| 145 | } |
| 146 | |
| 147 | func (object *objectLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 148 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 149 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 150 | objs = objs.Append(deps.Objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 151 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 152 | var output android.WritablePath |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 153 | builderFlags := flagsToBuilderFlags(flags) |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 154 | outputName := ctx.ModuleName() |
| 155 | if !strings.HasSuffix(outputName, objectExtension) { |
| 156 | outputName += objectExtension |
| 157 | } |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 158 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 159 | // 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 Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 163 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 164 | 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 Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 174 | if String(object.Properties.Prefix_symbols) != "" { |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 175 | 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 Cross | dea1d03 | 2022-12-06 14:50:08 -0800 | [diff] [blame] | 185 | outputAddrSig := android.PathForModuleOut(ctx, "addrsig", outputName) |
| 186 | |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 187 | if String(object.Properties.Prefix_symbols) != "" { |
| 188 | input := android.PathForModuleOut(ctx, "unprefixed", outputName) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 189 | transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 190 | builderFlags, output) |
| 191 | output = input |
| 192 | } |
| 193 | |
Colin Cross | dea1d03 | 2022-12-06 14:50:08 -0800 | [diff] [blame] | 194 | 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 Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | ctx.CheckbuildFile(outputFile) |
| 203 | return outputFile |
| 204 | } |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 205 | |
Cole Faust | 9d6c7dc | 2024-08-19 14:39:19 -0700 | [diff] [blame] | 206 | func (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 Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 209 | |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 210 | // Must distinguish nil and [] in system_shared_libs - ensure that [] in |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 211 | // either input list doesn't come out as nil. |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 212 | if specifiedDeps.systemSharedLibs == nil { |
| 213 | specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 214 | } else { |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 215 | specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...) |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | return specifiedDeps |
| 219 | } |
| 220 | |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 221 | func (object *objectLinker) unstrippedOutputFilePath() android.Path { |
| 222 | return nil |
| 223 | } |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 224 | |
Wei Li | 5f5d271 | 2023-12-11 15:40:29 -0800 | [diff] [blame] | 225 | func (object *objectLinker) strippedAllOutputFilePath() android.Path { |
| 226 | panic("Not implemented.") |
| 227 | } |
| 228 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 229 | func (object *objectLinker) nativeCoverage() bool { |
| 230 | return true |
| 231 | } |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 232 | |
| 233 | func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { |
| 234 | return android.OptionalPath{} |
| 235 | } |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 236 | |
| 237 | func (object *objectLinker) object() bool { |
| 238 | return true |
| 239 | } |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 240 | |
| 241 | func (object *objectLinker) isCrt() bool { |
| 242 | return Bool(object.Properties.Crt) |
| 243 | } |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 244 | |
| 245 | func (object *objectLinker) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 246 | object.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON) |
| 247 | moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"} |
| 248 | } |