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