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