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