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" |
| 22 | ) |
| 23 | |
| 24 | // |
| 25 | // Objects (for crt*.o) |
| 26 | // |
| 27 | |
| 28 | func init() { |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 29 | android.RegisterModuleType("cc_object", ObjectFactory) |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 30 | android.RegisterSdkMemberType(ccObjectSdkMemberType) |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 31 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | var ccObjectSdkMemberType = &librarySdkMemberType{ |
| 35 | SdkMemberTypeBase: android.SdkMemberTypeBase{ |
| 36 | PropertyName: "native_objects", |
| 37 | SupportsSdk: true, |
| 38 | }, |
| 39 | prebuiltModuleType: "cc_prebuilt_object", |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | type objectLinker struct { |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 43 | *baseLinker |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 44 | Properties ObjectLinkerProperties |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 45 | |
| 46 | // Location of the object in the sysroot. Empty if the object is not |
| 47 | // included in the NDK. |
| 48 | ndkSysrootPath android.Path |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 51 | type ObjectLinkerProperties struct { |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 52 | // list of static library modules that should only provide headers for this module. |
| 53 | Static_libs []string `android:"arch_variant,variant_prepend"` |
| 54 | |
| 55 | // list of shared library modules should only provide headers for this module. |
Zi Wang | 9f609db | 2023-01-04 11:06:54 -0800 | [diff] [blame] | 56 | Shared_libs []string `android:"arch_variant,variant_prepend"` |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 57 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 58 | // list of modules that should only provide headers for this module. |
| 59 | Header_libs []string `android:"arch_variant,variant_prepend"` |
| 60 | |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 61 | // list of default libraries that will provide headers for this module. If unset, generally |
| 62 | // 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] | 63 | System_shared_libs []string `android:"arch_variant"` |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 64 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 65 | // names of other cc_object modules to link into this module using partial linking |
| 66 | Objs []string `android:"arch_variant"` |
| 67 | |
| 68 | // if set, add an extra objcopy --prefix-symbols= step |
| 69 | Prefix_symbols *string |
| 70 | |
| 71 | // if set, the path to a linker script to pass to ld -r when combining multiple object files. |
| 72 | Linker_script *string `android:"path,arch_variant"` |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 73 | |
| 74 | // Indicates that this module is a CRT object. CRT objects will be split |
| 75 | // into a variant per-API level between min_sdk_version and current. |
| 76 | Crt *bool |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 77 | |
| 78 | // Indicates that this module should not be included in the NDK sysroot. |
| 79 | // Only applies to CRT objects. Defaults to false. |
| 80 | Exclude_from_ndk_sysroot *bool |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 81 | } |
| 82 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 83 | func newObject(hod android.HostOrDeviceSupported) *Module { |
| 84 | module := newBaseModule(hod, android.MultilibBoth) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 85 | module.sanitize = &sanitize{} |
| 86 | module.stl = &stl{} |
| 87 | return module |
| 88 | } |
| 89 | |
Patrice Arruda | baff0ce | 2019-03-26 10:39:49 -0700 | [diff] [blame] | 90 | // cc_object runs the compiler without running the linker. It is rarely |
| 91 | // necessary, but sometimes used to generate .s files from .c files to use as |
| 92 | // input to a cc_genrule module. |
Colin Cross | e40b4ea | 2018-10-02 22:25:58 -0700 | [diff] [blame] | 93 | func ObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 94 | module := newObject(android.HostAndDeviceSupported) |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 95 | module.linker = &objectLinker{ |
Peter Collingbourne | 1c648b8 | 2019-09-26 12:24:45 -0700 | [diff] [blame] | 96 | baseLinker: NewBaseLinker(module.sanitize), |
Colin Cross | b916a38 | 2016-07-29 17:28:03 -0700 | [diff] [blame] | 97 | } |
| 98 | module.compiler = NewBaseCompiler() |
Peter Collingbourne | 486e42c | 2018-10-25 10:53:44 -0700 | [diff] [blame] | 99 | |
| 100 | // Clang's address-significance tables are incompatible with ld -r. |
| 101 | module.compiler.appendCflags([]string{"-fno-addrsig"}) |
| 102 | |
Martin Stjernholm | cd07bce | 2020-03-10 22:37:59 +0000 | [diff] [blame] | 103 | module.sdkMemberTypes = []android.SdkMemberType{ccObjectSdkMemberType} |
Jingwen Chen | 8c1b97e | 2021-02-18 03:21:34 -0500 | [diff] [blame] | 104 | |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 105 | return module.Init() |
| 106 | } |
| 107 | |
| 108 | func (object *objectLinker) appendLdflags(flags []string) { |
Jeff Gaston | af3cc2d | 2017-09-27 17:01:44 -0700 | [diff] [blame] | 109 | panic(fmt.Errorf("appendLdflags on objectLinker not supported")) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 112 | func (object *objectLinker) linkerProps() []interface{} { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 113 | return []interface{}{&object.Properties} |
| 114 | } |
| 115 | |
Colin Cross | 42742b8 | 2016-08-01 13:20:05 -0700 | [diff] [blame] | 116 | func (*objectLinker) linkerInit(ctx BaseModuleContext) {} |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 117 | |
Colin Cross | 37047f1 | 2016-12-13 17:06:13 -0800 | [diff] [blame] | 118 | func (object *objectLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Paul Duffin | a37832a | 2019-07-18 12:31:26 +0100 | [diff] [blame] | 119 | deps.HeaderLibs = append(deps.HeaderLibs, object.Properties.Header_libs...) |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 120 | deps.SharedLibs = append(deps.SharedLibs, object.Properties.Shared_libs...) |
| 121 | deps.StaticLibs = append(deps.StaticLibs, object.Properties.Static_libs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 122 | deps.ObjFiles = append(deps.ObjFiles, object.Properties.Objs...) |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 123 | |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 124 | deps.SystemSharedLibs = object.Properties.System_shared_libs |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 125 | if deps.SystemSharedLibs == nil { |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 126 | // 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] | 127 | // Note: If an empty list [] is specified, it implies that the module declines the |
| 128 | // default shared libraries. |
| 129 | deps.SystemSharedLibs = append(deps.SystemSharedLibs, ctx.toolchain().DefaultSharedLibraries()...) |
| 130 | } |
| 131 | deps.LateSharedLibs = append(deps.LateSharedLibs, deps.SystemSharedLibs...) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 132 | return deps |
| 133 | } |
| 134 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 135 | func (object *objectLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 33bac24 | 2021-07-14 17:03:16 -0700 | [diff] [blame] | 136 | flags.Global.LdFlags = append(flags.Global.LdFlags, ctx.toolchain().ToolchainLdflags()) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 137 | |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 138 | if lds := android.OptionalPathForModuleSrc(ctx, object.Properties.Linker_script); lds.Valid() { |
Colin Cross | 4af21ed | 2019-11-04 09:37:55 -0800 | [diff] [blame] | 139 | flags.Local.LdFlags = append(flags.Local.LdFlags, "-Wl,-T,"+lds.String()) |
Pete Bentley | 74c9bba | 2019-08-16 20:25:06 +0100 | [diff] [blame] | 140 | flags.LdFlagsDeps = append(flags.LdFlagsDeps, lds.Path()) |
| 141 | } |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 142 | return flags |
| 143 | } |
| 144 | |
| 145 | func (object *objectLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 146 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 147 | |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 148 | objs = objs.Append(deps.Objs) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 149 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 150 | var output android.WritablePath |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 151 | builderFlags := flagsToBuilderFlags(flags) |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 152 | outputName := ctx.ModuleName() |
| 153 | if !strings.HasSuffix(outputName, objectExtension) { |
| 154 | outputName += objectExtension |
| 155 | } |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 156 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 157 | // isForPlatform is terribly named and actually means isNotApex. |
| 158 | if Bool(object.Properties.Crt) && |
| 159 | !Bool(object.Properties.Exclude_from_ndk_sysroot) && ctx.useSdk() && |
| 160 | ctx.isSdkVariant() && ctx.isForPlatform() { |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 161 | |
Dan Albert | 5b0d4f3 | 2023-04-04 23:22:11 +0000 | [diff] [blame] | 162 | output = getVersionedLibraryInstallPath(ctx, |
| 163 | nativeApiLevelOrPanic(ctx, ctx.sdkVersion())).Join(ctx, outputName) |
| 164 | object.ndkSysrootPath = output |
| 165 | } else { |
| 166 | output = android.PathForModuleOut(ctx, outputName) |
| 167 | } |
| 168 | |
| 169 | outputFile := output |
| 170 | |
| 171 | if len(objs.objFiles) == 1 && String(object.Properties.Linker_script) == "" { |
Nan Zhang | 0007d81 | 2017-11-07 10:57:05 -0800 | [diff] [blame] | 172 | if String(object.Properties.Prefix_symbols) != "" { |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 173 | transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), objs.objFiles[0], |
| 174 | builderFlags, output) |
| 175 | } else { |
| 176 | ctx.Build(pctx, android.BuildParams{ |
| 177 | Rule: android.Cp, |
| 178 | Input: objs.objFiles[0], |
| 179 | Output: output, |
| 180 | }) |
| 181 | } |
| 182 | } else { |
Colin Cross | dea1d03 | 2022-12-06 14:50:08 -0800 | [diff] [blame] | 183 | outputAddrSig := android.PathForModuleOut(ctx, "addrsig", outputName) |
| 184 | |
Colin Cross | fb44cd2 | 2022-09-09 15:11:16 -0700 | [diff] [blame] | 185 | if String(object.Properties.Prefix_symbols) != "" { |
| 186 | input := android.PathForModuleOut(ctx, "unprefixed", outputName) |
Chris Parsons | bf4f55f | 2020-11-23 17:02:44 -0500 | [diff] [blame] | 187 | transformBinaryPrefixSymbols(ctx, String(object.Properties.Prefix_symbols), input, |
Dan Willemsen | efb1dd9 | 2017-09-18 22:47:20 -0700 | [diff] [blame] | 188 | builderFlags, output) |
| 189 | output = input |
| 190 | } |
| 191 | |
Colin Cross | dea1d03 | 2022-12-06 14:50:08 -0800 | [diff] [blame] | 192 | transformObjsToObj(ctx, objs.objFiles, builderFlags, outputAddrSig, flags.LdFlagsDeps) |
| 193 | |
| 194 | // ld -r reorders symbols and invalidates the .llvm_addrsig section, which then causes warnings |
| 195 | // if the resulting object is used with ld --icf=safe. Strip the .llvm_addrsig section to |
| 196 | // prevent the warnings. |
| 197 | transformObjectNoAddrSig(ctx, outputAddrSig, output) |
Colin Cross | 4d9c2d1 | 2016-07-29 12:48:20 -0700 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | ctx.CheckbuildFile(outputFile) |
| 201 | return outputFile |
| 202 | } |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 203 | |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 204 | func (object *objectLinker) linkerSpecifiedDeps(specifiedDeps specifiedDeps) specifiedDeps { |
| 205 | specifiedDeps.sharedLibs = append(specifiedDeps.sharedLibs, object.Properties.Shared_libs...) |
| 206 | |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 207 | // Must distinguish nil and [] in system_shared_libs - ensure that [] in |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 208 | // either input list doesn't come out as nil. |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 209 | if specifiedDeps.systemSharedLibs == nil { |
| 210 | specifiedDeps.systemSharedLibs = object.Properties.System_shared_libs |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 211 | } else { |
Colin Cross | 6b8f425 | 2021-07-22 11:39:44 -0700 | [diff] [blame] | 212 | specifiedDeps.systemSharedLibs = append(specifiedDeps.systemSharedLibs, object.Properties.System_shared_libs...) |
Colin Cross | 137d7da | 2021-06-21 16:41:29 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | return specifiedDeps |
| 216 | } |
| 217 | |
Jiyong Park | af6d895 | 2019-01-31 12:21:23 +0900 | [diff] [blame] | 218 | func (object *objectLinker) unstrippedOutputFilePath() android.Path { |
| 219 | return nil |
| 220 | } |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 221 | |
Wei Li | 5f5d271 | 2023-12-11 15:40:29 -0800 | [diff] [blame] | 222 | func (object *objectLinker) strippedAllOutputFilePath() android.Path { |
| 223 | panic("Not implemented.") |
| 224 | } |
| 225 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 226 | func (object *objectLinker) nativeCoverage() bool { |
| 227 | return true |
| 228 | } |
Jiyong Park | ee9a98d | 2019-08-09 14:44:36 +0900 | [diff] [blame] | 229 | |
| 230 | func (object *objectLinker) coverageOutputFilePath() android.OptionalPath { |
| 231 | return android.OptionalPath{} |
| 232 | } |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 233 | |
| 234 | func (object *objectLinker) object() bool { |
| 235 | return true |
| 236 | } |
Dan Albert | 92fe740 | 2020-07-15 13:33:30 -0700 | [diff] [blame] | 237 | |
| 238 | func (object *objectLinker) isCrt() bool { |
| 239 | return Bool(object.Properties.Crt) |
| 240 | } |
Colin Cross | 4a9e6ec | 2023-12-18 15:29:41 -0800 | [diff] [blame] | 241 | |
| 242 | func (object *objectLinker) moduleInfoJSON(ctx ModuleContext, moduleInfoJSON *android.ModuleInfoJSON) { |
| 243 | object.baseLinker.moduleInfoJSON(ctx, moduleInfoJSON) |
| 244 | moduleInfoJSON.Class = []string{"STATIC_LIBRARIES"} |
| 245 | } |