Colin Cross | ce75d2c | 2016-10-06 16:12:58 -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 ( |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 18 | "path/filepath" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 21 | ) |
| 22 | |
| 23 | func init() { |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 24 | RegisterPrebuiltBuildComponents(android.InitRegistrationContext) |
| 25 | } |
| 26 | |
| 27 | func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 28 | ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 29 | ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory) |
| 30 | ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory) |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 31 | ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory) |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 32 | ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 33 | ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | type prebuiltLinkerInterface interface { |
| 37 | Name(string) string |
| 38 | prebuilt() *android.Prebuilt |
| 39 | } |
| 40 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 41 | type prebuiltLinkerProperties struct { |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 42 | // a prebuilt library or binary. Can reference a genrule module that generates an executable file. |
| 43 | Srcs []string `android:"path,arch_variant"` |
| 44 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 45 | Sanitized Sanitized `android:"arch_variant"` |
| 46 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 47 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined |
| 48 | // symbols, etc), default true. |
| 49 | Check_elf_files *bool |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 50 | |
Pierre-Clément Tosi | 6f630ae | 2022-08-10 09:25:54 +0100 | [diff] [blame] | 51 | // if set, add an extra objcopy --prefix-symbols= step |
| 52 | Prefix_symbols *string |
| 53 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 54 | // Optionally provide an import library if this is a Windows PE DLL prebuilt. |
| 55 | // This is needed only if this library is linked by other modules in build time. |
| 56 | // Only makes sense for the Windows target. |
| 57 | Windows_import_lib *string `android:"path,arch_variant"` |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 60 | type prebuiltLinker struct { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 61 | android.Prebuilt |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 62 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 63 | properties prebuiltLinkerProperties |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 66 | func (p *prebuiltLinker) prebuilt() *android.Prebuilt { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 67 | return &p.Prebuilt |
| 68 | } |
| 69 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 70 | func (p *prebuiltLinker) PrebuiltSrcs() []string { |
| 71 | return p.properties.Srcs |
| 72 | } |
| 73 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 74 | type prebuiltLibraryInterface interface { |
| 75 | libraryInterface |
| 76 | prebuiltLinkerInterface |
| 77 | disablePrebuilt() |
| 78 | } |
| 79 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 80 | type prebuiltLibraryLinker struct { |
| 81 | *libraryDecorator |
| 82 | prebuiltLinker |
| 83 | } |
| 84 | |
| 85 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 86 | var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 87 | |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 88 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} |
| 89 | |
| 90 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 91 | return p.libraryDecorator.linkerDeps(ctx, deps) |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 95 | return flags |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { |
| 99 | return p.libraryDecorator.linkerProps() |
| 100 | } |
| 101 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 102 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 103 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 104 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 105 | p.libraryDecorator.flagExporter.exportIncludes(ctx) |
| 106 | p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...) |
| 107 | p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...) |
| 108 | p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...) |
| 109 | p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...) |
| 110 | p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) |
| 111 | |
| 112 | p.libraryDecorator.flagExporter.setProvider(ctx) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 113 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 114 | // TODO(ccross): verify shared library dependencies |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 115 | srcs := p.prebuiltSrcs(ctx) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 116 | if len(srcs) > 0 { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 117 | if len(srcs) > 1 { |
| 118 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 119 | return nil |
| 120 | } |
| 121 | |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 122 | p.libraryDecorator.exportVersioningMacroIfNeeded(ctx) |
| 123 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 124 | in := android.PathForModuleSrc(ctx, srcs[0]) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 125 | |
Pierre-Clément Tosi | 6f630ae | 2022-08-10 09:25:54 +0100 | [diff] [blame] | 126 | if String(p.prebuiltLinker.properties.Prefix_symbols) != "" { |
| 127 | prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0]) |
| 128 | transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols), |
| 129 | in, flagsToBuilderFlags(flags), prefixed) |
| 130 | in = prefixed |
| 131 | } |
| 132 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 133 | if p.static() { |
Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 134 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build() |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 135 | android.SetProvider(ctx, StaticLibraryInfoProvider, StaticLibraryInfo{ |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 136 | StaticLibrary: in, |
| 137 | |
| 138 | TransitiveStaticLibrariesForOrdering: depSet, |
| 139 | }) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 140 | return in |
| 141 | } |
| 142 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 143 | if p.shared() { |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 144 | p.unstrippedOutputFile = in |
Colin Cross | 0fd6a41 | 2019-08-16 14:22:10 -0700 | [diff] [blame] | 145 | libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 146 | outputFile := android.PathForModuleOut(ctx, libName) |
| 147 | var implicits android.Paths |
| 148 | |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 149 | if p.stripper.NeedsStrip(ctx) { |
| 150 | stripFlags := flagsToStripFlags(flags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 151 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 152 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 153 | in = stripped |
| 154 | } |
| 155 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 156 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 157 | // depending on a table of contents file instead of the library itself. |
| 158 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 159 | p.tocFile = android.OptionalPathForPath(tocFile) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 160 | TransformSharedObjectToToc(ctx, outputFile, tocFile) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 161 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 162 | if ctx.Windows() && p.properties.Windows_import_lib != nil { |
| 163 | // Consumers of this library actually links to the import library in build |
| 164 | // time and dynamically links to the DLL in run time. i.e. |
| 165 | // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll |
| 166 | importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib)) |
| 167 | importLibName := p.libraryDecorator.getLibName(ctx) + ".lib" |
| 168 | importLibOutputFile := android.PathForModuleOut(ctx, importLibName) |
| 169 | implicits = append(implicits, importLibOutputFile) |
| 170 | |
| 171 | ctx.Build(pctx, android.BuildParams{ |
| 172 | Rule: android.Cp, |
| 173 | Description: "prebuilt import library", |
| 174 | Input: importLibSrc, |
| 175 | Output: importLibOutputFile, |
| 176 | Args: map[string]string{ |
| 177 | "cpFlags": "-L", |
| 178 | }, |
| 179 | }) |
| 180 | } |
| 181 | |
| 182 | ctx.Build(pctx, android.BuildParams{ |
| 183 | Rule: android.Cp, |
| 184 | Description: "prebuilt shared library", |
| 185 | Implicits: implicits, |
| 186 | Input: in, |
| 187 | Output: outputFile, |
| 188 | Args: map[string]string{ |
| 189 | "cpFlags": "-L", |
| 190 | }, |
| 191 | }) |
| 192 | |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 193 | android.SetProvider(ctx, SharedLibraryInfoProvider, SharedLibraryInfo{ |
Liz Kammer | ef6dfea | 2021-06-08 15:37:09 -0400 | [diff] [blame] | 194 | SharedLibrary: outputFile, |
| 195 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 196 | |
| 197 | TableOfContents: p.tocFile, |
| 198 | }) |
| 199 | |
Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 200 | // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub |
Jooyung Han | e5f063e | 2023-03-23 14:31:19 +0900 | [diff] [blame] | 201 | // library as their source and must not be installed, but other prebuilts like |
| 202 | // libclang_rt.* libraries set `stubs` property because they are LLNDK libraries, |
| 203 | // but use an implementation library as their source and need to be installed. |
| 204 | // This discrepancy should be resolved without the prefix hack below. |
| 205 | isModuleSdkPrebuilts := android.HasAnyPrefix(ctx.ModuleDir(), []string{ |
| 206 | "prebuilts/runtime/mainline/", "prebuilts/module_sdk/"}) |
| 207 | if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && isModuleSdkPrebuilts { |
Jingwen Chen | 8ac7d7d | 2023-03-20 11:05:16 +0000 | [diff] [blame] | 208 | ctx.Module().MakeUninstallable() |
Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 211 | return outputFile |
| 212 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 215 | if p.header() { |
Colin Cross | 4021302 | 2023-12-13 15:19:49 -0800 | [diff] [blame] | 216 | android.SetProvider(ctx, HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 217 | |
Martin Stjernholm | d51cb5c | 2021-11-30 18:49:03 +0000 | [diff] [blame] | 218 | // Need to return an output path so that the AndroidMk logic doesn't skip |
| 219 | // the prebuilt header. For compatibility, in case Android.mk files use a |
| 220 | // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as |
| 221 | // placeholder, just like non-prebuilt header modules do in linkStatic(). |
| 222 | ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension) |
| 223 | transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil) |
| 224 | return ph |
Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 225 | } |
| 226 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 227 | return nil |
| 228 | } |
| 229 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 230 | func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string { |
| 231 | sanitize := ctx.Module().(*Module).sanitize |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 232 | srcs := p.properties.Srcs |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 233 | srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 234 | if p.static() { |
| 235 | srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 236 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 237 | } |
| 238 | if p.shared() { |
| 239 | srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 240 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 241 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 242 | return srcs |
| 243 | } |
| 244 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 245 | func (p *prebuiltLibraryLinker) shared() bool { |
| 246 | return p.libraryDecorator.shared() |
| 247 | } |
| 248 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 249 | func (p *prebuiltLibraryLinker) nativeCoverage() bool { |
| 250 | return false |
| 251 | } |
| 252 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 253 | func (p *prebuiltLibraryLinker) disablePrebuilt() { |
| 254 | p.properties.Srcs = nil |
| 255 | } |
| 256 | |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 257 | // Implements versionedInterface |
| 258 | func (p *prebuiltLibraryLinker) implementationModuleName(name string) string { |
Paul Duffin | 9804da0 | 2021-10-26 10:42:42 +0100 | [diff] [blame] | 259 | return android.RemoveOptionalPrebuiltPrefix(name) |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 260 | } |
| 261 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 262 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 263 | module, library := NewLibrary(hod) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 264 | module.compiler = nil |
| 265 | |
| 266 | prebuilt := &prebuiltLibraryLinker{ |
| 267 | libraryDecorator: library, |
| 268 | } |
| 269 | module.linker = prebuilt |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 270 | module.library = prebuilt |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 271 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 272 | module.AddProperties(&prebuilt.properties) |
| 273 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 274 | if srcsProperty == "" { |
| 275 | android.InitPrebuiltModuleWithoutSrcs(module) |
| 276 | } else { |
| 277 | srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string { |
| 278 | return prebuilt.prebuiltSrcs(ctx) |
| 279 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 280 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 281 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty) |
| 282 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 283 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 284 | return module, library |
| 285 | } |
| 286 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 287 | // cc_prebuilt_library installs a precompiled shared library that are |
| 288 | // listed in the srcs property in the device's directory. |
| 289 | func PrebuiltLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 290 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 291 | |
| 292 | // Prebuilt shared libraries can be included in APEXes |
| 293 | android.InitApexModule(module) |
| 294 | |
| 295 | return module.Init() |
| 296 | } |
| 297 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 298 | // cc_prebuilt_library_shared installs a precompiled shared library that are |
| 299 | // listed in the srcs property in the device's directory. |
| 300 | func PrebuiltSharedLibraryFactory() android.Module { |
| 301 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 302 | return module.Init() |
| 303 | } |
| 304 | |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 305 | // cc_prebuilt_test_library_shared installs a precompiled shared library |
| 306 | // to be used as a data dependency of a test-related module (such as cc_test, or |
| 307 | // cc_test_library). |
| 308 | func PrebuiltSharedTestLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 309 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 310 | library.BuildOnlyShared() |
| 311 | library.baseInstaller = NewTestInstaller() |
| 312 | return module.Init() |
| 313 | } |
| 314 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 315 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 316 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 317 | library.BuildOnlyShared() |
| 318 | |
| 319 | // Prebuilt shared libraries can be included in APEXes |
| 320 | android.InitApexModule(module) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 321 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 322 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 325 | // cc_prebuilt_library_static installs a precompiled static library that are |
| 326 | // listed in the srcs property in the device's directory. |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 327 | func PrebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 328 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 329 | return module.Init() |
| 330 | } |
| 331 | |
| 332 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 333 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 334 | library.BuildOnlyStatic() |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 335 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 336 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 339 | type prebuiltObjectProperties struct { |
| 340 | Srcs []string `android:"path,arch_variant"` |
| 341 | } |
| 342 | |
| 343 | type prebuiltObjectLinker struct { |
| 344 | android.Prebuilt |
| 345 | objectLinker |
| 346 | |
| 347 | properties prebuiltObjectProperties |
| 348 | } |
| 349 | |
| 350 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 351 | return &p.Prebuilt |
| 352 | } |
| 353 | |
| 354 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 355 | |
| 356 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 357 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 358 | if len(p.properties.Srcs) > 0 { |
Colin Cross | ee02aed | 2022-04-15 15:16:02 -0700 | [diff] [blame] | 359 | // Copy objects to a name matching the final installed name |
| 360 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 361 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o") |
| 362 | ctx.Build(pctx, android.BuildParams{ |
| 363 | Rule: android.CpExecutable, |
| 364 | Description: "prebuilt", |
| 365 | Output: outputFile, |
| 366 | Input: in, |
| 367 | }) |
| 368 | return outputFile |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 369 | } |
| 370 | return nil |
| 371 | } |
| 372 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 373 | func (p *prebuiltObjectLinker) object() bool { |
| 374 | return true |
| 375 | } |
| 376 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 377 | func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module { |
| 378 | module := newObject(hod) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 379 | prebuilt := &prebuiltObjectLinker{ |
| 380 | objectLinker: objectLinker{ |
| 381 | baseLinker: NewBaseLinker(nil), |
| 382 | }, |
| 383 | } |
| 384 | module.linker = prebuilt |
| 385 | module.AddProperties(&prebuilt.properties) |
| 386 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 387 | return module |
| 388 | } |
| 389 | |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 390 | func PrebuiltObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 391 | module := NewPrebuiltObject(android.HostAndDeviceSupported) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 392 | return module.Init() |
| 393 | } |
| 394 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 395 | type prebuiltBinaryLinker struct { |
| 396 | *binaryDecorator |
| 397 | prebuiltLinker |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 398 | |
| 399 | toolPath android.OptionalPath |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 403 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 404 | func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath { |
| 405 | return p.toolPath |
| 406 | } |
| 407 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 408 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 409 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 410 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 411 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 412 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 413 | in := p.Prebuilt.SingleSourcePath(ctx) |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 414 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 415 | p.unstrippedOutputFile = in |
| 416 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 417 | if ctx.Host() { |
| 418 | // Host binaries are symlinked to their prebuilt source locations. That |
| 419 | // way they are executed directly from there so the linker resolves their |
| 420 | // shared library dependencies relative to that location (using |
| 421 | // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt |
| 422 | // repository can supply the expected versions of the shared libraries |
| 423 | // without interference from what is in the out tree. |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 424 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 425 | // These shared lib paths may point to copies of the libs in |
| 426 | // .intermediates, which isn't where the binary will load them from, but |
| 427 | // it's fine for dependency tracking. If a library dependency is updated, |
| 428 | // the symlink will get a new timestamp, along with any installed symlinks |
| 429 | // handled in make. |
| 430 | sharedLibPaths := deps.EarlySharedLibs |
| 431 | sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...) |
| 432 | sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...) |
| 433 | |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 434 | var fromPath = in.String() |
| 435 | if !filepath.IsAbs(fromPath) { |
| 436 | fromPath = "$$PWD/" + fromPath |
| 437 | } |
| 438 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 439 | ctx.Build(pctx, android.BuildParams{ |
| 440 | Rule: android.Symlink, |
| 441 | Output: outputFile, |
| 442 | Input: in, |
| 443 | Implicits: sharedLibPaths, |
| 444 | Args: map[string]string{ |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 445 | "fromPath": fromPath, |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 446 | }, |
| 447 | }) |
| 448 | |
| 449 | p.toolPath = android.OptionalPathForPath(outputFile) |
| 450 | } else { |
| 451 | if p.stripper.NeedsStrip(ctx) { |
| 452 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
| 453 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags)) |
| 454 | in = stripped |
| 455 | } |
| 456 | |
| 457 | // Copy binaries to a name matching the final installed name |
| 458 | ctx.Build(pctx, android.BuildParams{ |
| 459 | Rule: android.CpExecutable, |
| 460 | Description: "prebuilt", |
| 461 | Output: outputFile, |
| 462 | Input: in, |
| 463 | }) |
| 464 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 465 | |
| 466 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | return nil |
| 470 | } |
| 471 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 472 | func (p *prebuiltBinaryLinker) binary() bool { |
| 473 | return true |
| 474 | } |
| 475 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 476 | // cc_prebuilt_binary installs a precompiled executable in srcs property in the |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 477 | // device's directory, for both the host and device |
| 478 | func PrebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 479 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 480 | return module.Init() |
| 481 | } |
| 482 | |
| 483 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
Colin Cross | 8ff1058 | 2023-12-07 13:10:56 -0800 | [diff] [blame] | 484 | module, binary := newBinary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 485 | module.compiler = nil |
| 486 | |
| 487 | prebuilt := &prebuiltBinaryLinker{ |
| 488 | binaryDecorator: binary, |
| 489 | } |
| 490 | module.linker = prebuilt |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 491 | module.installer = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 492 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 493 | module.AddProperties(&prebuilt.properties) |
| 494 | |
| 495 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 496 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 497 | } |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 498 | |
| 499 | type Sanitized struct { |
| 500 | None struct { |
| 501 | Srcs []string `android:"path,arch_variant"` |
| 502 | } `android:"arch_variant"` |
| 503 | Address struct { |
| 504 | Srcs []string `android:"path,arch_variant"` |
| 505 | } `android:"arch_variant"` |
| 506 | Hwaddress struct { |
| 507 | Srcs []string `android:"path,arch_variant"` |
| 508 | } `android:"arch_variant"` |
| 509 | } |
| 510 | |
| 511 | func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string { |
| 512 | if sanitize == nil { |
| 513 | return nil |
| 514 | } |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 515 | if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil { |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 516 | return sanitized.Address.Srcs |
| 517 | } |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 518 | if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil { |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 519 | return sanitized.Hwaddress.Srcs |
| 520 | } |
| 521 | return sanitized.None.Srcs |
| 522 | } |