| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. | 
|  | 2 | // | 
|  | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 4 | // you may not use this file except in compliance with the License. | 
|  | 5 | // You may obtain a copy of the License at | 
|  | 6 | // | 
|  | 7 | //     http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 8 | // | 
|  | 9 | // Unless required by applicable law or agreed to in writing, software | 
|  | 10 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 12 | // See the License for the specific language governing permissions and | 
|  | 13 | // limitations under the License. | 
|  | 14 |  | 
|  | 15 | package cc | 
|  | 16 |  | 
|  | 17 | import ( | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 18 | "path/filepath" | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 19 |  | 
|  | 20 | "android/soong/android" | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 21 | "android/soong/bazel" | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 22 | "android/soong/bazel/cquery" | 
| 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 | 
|  | 41 | } | 
|  | 42 |  | 
| Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 43 | type prebuiltLinkerProperties struct { | 
| Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 44 | // a prebuilt library or binary. Can reference a genrule module that generates an executable file. | 
|  | 45 | Srcs []string `android:"path,arch_variant"` | 
|  | 46 |  | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 47 | Sanitized Sanitized `android:"arch_variant"` | 
|  | 48 |  | 
| Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 49 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined | 
|  | 50 | // symbols, etc), default true. | 
|  | 51 | Check_elf_files *bool | 
| Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 52 |  | 
| Pierre-Clément Tosi | 6f630ae | 2022-08-10 09:25:54 +0100 | [diff] [blame] | 53 | // if set, add an extra objcopy --prefix-symbols= step | 
|  | 54 | Prefix_symbols *string | 
|  | 55 |  | 
| Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 56 | // Optionally provide an import library if this is a Windows PE DLL prebuilt. | 
|  | 57 | // This is needed only if this library is linked by other modules in build time. | 
|  | 58 | // Only makes sense for the Windows target. | 
|  | 59 | Windows_import_lib *string `android:"path,arch_variant"` | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 60 |  | 
|  | 61 | // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either | 
|  | 62 | // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with | 
|  | 63 | // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this | 
|  | 64 | // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see | 
|  | 65 | // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled. | 
|  | 66 | MixedBuildsDisabled bool `blueprint:"mutated"` | 
| 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 | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 79 | func (p *prebuiltLinker) PrebuiltSrcs() []string { | 
|  | 80 | return p.properties.Srcs | 
|  | 81 | } | 
|  | 82 |  | 
| Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 83 | type prebuiltLibraryInterface interface { | 
|  | 84 | libraryInterface | 
|  | 85 | prebuiltLinkerInterface | 
|  | 86 | disablePrebuilt() | 
|  | 87 | } | 
|  | 88 |  | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 89 | type prebuiltLibraryLinker struct { | 
|  | 90 | *libraryDecorator | 
|  | 91 | prebuiltLinker | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) | 
| Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 95 | var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 96 |  | 
| Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 97 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} | 
|  | 98 |  | 
|  | 99 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { | 
| Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 100 | return p.libraryDecorator.linkerDeps(ctx, deps) | 
| Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
|  | 103 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { | 
| Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 104 | return flags | 
| Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 105 | } | 
|  | 106 |  | 
|  | 107 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { | 
|  | 108 | return p.libraryDecorator.linkerProps() | 
|  | 109 | } | 
|  | 110 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 111 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 112 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
| Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 113 |  | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 114 | p.libraryDecorator.flagExporter.exportIncludes(ctx) | 
|  | 115 | p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...) | 
|  | 116 | p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...) | 
|  | 117 | p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...) | 
|  | 118 | p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...) | 
|  | 119 | p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) | 
|  | 120 |  | 
|  | 121 | p.libraryDecorator.flagExporter.setProvider(ctx) | 
| Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 122 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 123 | // TODO(ccross): verify shared library dependencies | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 124 | srcs := p.prebuiltSrcs(ctx) | 
| 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 | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 143 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(in).Build() | 
| Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 144 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ | 
|  | 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{ | 
|  | 181 | Rule:        android.Cp, | 
|  | 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{ | 
|  | 192 | Rule:        android.Cp, | 
|  | 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 | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 202 | ctx.SetProvider(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, | 
|  | 207 | }) | 
|  | 208 |  | 
| Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 209 | // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub | 
| Jooyung Han | e5f063e | 2023-03-23 14:31:19 +0900 | [diff] [blame] | 210 | // library as their source and must not be installed, but other prebuilts like | 
|  | 211 | // libclang_rt.* libraries set `stubs` property because they are LLNDK libraries, | 
|  | 212 | // but use an implementation library as their source and need to be installed. | 
|  | 213 | // This discrepancy should be resolved without the prefix hack below. | 
|  | 214 | isModuleSdkPrebuilts := android.HasAnyPrefix(ctx.ModuleDir(), []string{ | 
|  | 215 | "prebuilts/runtime/mainline/", "prebuilts/module_sdk/"}) | 
|  | 216 | if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && isModuleSdkPrebuilts { | 
| Jingwen Chen | 8ac7d7d | 2023-03-20 11:05:16 +0000 | [diff] [blame] | 217 | ctx.Module().MakeUninstallable() | 
| Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 220 | return outputFile | 
|  | 221 | } | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 222 | } | 
|  | 223 |  | 
| Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 224 | if p.header() { | 
|  | 225 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) | 
|  | 226 |  | 
| Martin Stjernholm | d51cb5c | 2021-11-30 18:49:03 +0000 | [diff] [blame] | 227 | // Need to return an output path so that the AndroidMk logic doesn't skip | 
|  | 228 | // the prebuilt header. For compatibility, in case Android.mk files use a | 
|  | 229 | // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as | 
|  | 230 | // placeholder, just like non-prebuilt header modules do in linkStatic(). | 
|  | 231 | ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension) | 
|  | 232 | transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil) | 
|  | 233 | return ph | 
| Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 234 | } | 
|  | 235 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 236 | return nil | 
|  | 237 | } | 
|  | 238 |  | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 239 | func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string { | 
|  | 240 | sanitize := ctx.Module().(*Module).sanitize | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 241 | srcs := p.properties.Srcs | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 242 | srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...) | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 243 | if p.static() { | 
|  | 244 | srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...) | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 245 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...) | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 246 | } | 
|  | 247 | if p.shared() { | 
|  | 248 | srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...) | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 249 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...) | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 250 | } | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 251 | return srcs | 
|  | 252 | } | 
|  | 253 |  | 
| Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 254 | func (p *prebuiltLibraryLinker) shared() bool { | 
|  | 255 | return p.libraryDecorator.shared() | 
|  | 256 | } | 
|  | 257 |  | 
| Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 258 | func (p *prebuiltLibraryLinker) nativeCoverage() bool { | 
|  | 259 | return false | 
|  | 260 | } | 
|  | 261 |  | 
| Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 262 | func (p *prebuiltLibraryLinker) disablePrebuilt() { | 
|  | 263 | p.properties.Srcs = nil | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 264 | p.properties.MixedBuildsDisabled = true | 
| Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 265 | } | 
|  | 266 |  | 
| Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 267 | // Implements versionedInterface | 
|  | 268 | func (p *prebuiltLibraryLinker) implementationModuleName(name string) string { | 
| Paul Duffin | 9804da0 | 2021-10-26 10:42:42 +0100 | [diff] [blame] | 269 | return android.RemoveOptionalPrebuiltPrefix(name) | 
| Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 270 | } | 
|  | 271 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 272 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 273 | module, library := NewLibrary(hod) | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 274 | module.compiler = nil | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 275 | module.bazelable = true | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 276 | module.bazelHandler = &prebuiltLibraryBazelHandler{module: module, library: library} | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 277 |  | 
|  | 278 | prebuilt := &prebuiltLibraryLinker{ | 
|  | 279 | libraryDecorator: library, | 
|  | 280 | } | 
|  | 281 | module.linker = prebuilt | 
| Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 282 | module.library = prebuilt | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 283 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 284 | module.AddProperties(&prebuilt.properties) | 
|  | 285 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 286 | if srcsProperty == "" { | 
|  | 287 | android.InitPrebuiltModuleWithoutSrcs(module) | 
|  | 288 | } else { | 
|  | 289 | srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string { | 
|  | 290 | return prebuilt.prebuiltSrcs(ctx) | 
|  | 291 | } | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 292 |  | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 293 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty) | 
|  | 294 | } | 
| Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 295 |  | 
| Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 296 | return module, library | 
|  | 297 | } | 
|  | 298 |  | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 299 | // cc_prebuilt_library installs a precompiled shared library that are | 
|  | 300 | // listed in the srcs property in the device's directory. | 
|  | 301 | func PrebuiltLibraryFactory() android.Module { | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 302 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") | 
| Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 303 |  | 
|  | 304 | // Prebuilt shared libraries can be included in APEXes | 
|  | 305 | android.InitApexModule(module) | 
|  | 306 |  | 
|  | 307 | return module.Init() | 
|  | 308 | } | 
|  | 309 |  | 
| Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 310 | // cc_prebuilt_library_shared installs a precompiled shared library that are | 
|  | 311 | // listed in the srcs property in the device's directory. | 
|  | 312 | func PrebuiltSharedLibraryFactory() android.Module { | 
|  | 313 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) | 
|  | 314 | return module.Init() | 
|  | 315 | } | 
|  | 316 |  | 
| Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 317 | // cc_prebuilt_test_library_shared installs a precompiled shared library | 
|  | 318 | // to be used as a data dependency of a test-related module (such as cc_test, or | 
|  | 319 | // cc_test_library). | 
|  | 320 | func PrebuiltSharedTestLibraryFactory() android.Module { | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 321 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") | 
| Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 322 | library.BuildOnlyShared() | 
|  | 323 | library.baseInstaller = NewTestInstaller() | 
|  | 324 | return module.Init() | 
|  | 325 | } | 
|  | 326 |  | 
| Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 327 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 328 | module, library := NewPrebuiltLibrary(hod, "srcs") | 
| Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 329 | library.BuildOnlyShared() | 
|  | 330 |  | 
|  | 331 | // Prebuilt shared libraries can be included in APEXes | 
|  | 332 | android.InitApexModule(module) | 
| Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 333 |  | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 334 | return module, library | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 335 | } | 
|  | 336 |  | 
| Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 337 | // cc_prebuilt_library_static installs a precompiled static library that are | 
|  | 338 | // listed in the srcs property in the device's directory. | 
| Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 339 | func PrebuiltStaticLibraryFactory() android.Module { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 340 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) | 
|  | 341 | return module.Init() | 
|  | 342 | } | 
|  | 343 |  | 
|  | 344 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { | 
| Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 345 | module, library := NewPrebuiltLibrary(hod, "srcs") | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 346 | library.BuildOnlyStatic() | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 347 |  | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 348 | return module, library | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 349 | } | 
|  | 350 |  | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 351 | type bazelPrebuiltLibraryStaticAttributes struct { | 
|  | 352 | Static_library         bazel.LabelAttribute | 
|  | 353 | Export_includes        bazel.StringListAttribute | 
|  | 354 | Export_system_includes bazel.StringListAttribute | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame] | 355 | Alwayslink             bazel.BoolAttribute | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 358 | // TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated | 
|  | 359 | // Implements bp2build for cc_prebuilt_library modules. This will generate: | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 360 | //   - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants. | 
|  | 361 | //   - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants | 
|  | 362 | //   - Both a cc_prebuilt_library_static and cc_prebuilt_library_shared if the aforementioned properties are not false across | 
| Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 363 | //     all variants | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 364 | // | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 365 | // In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static". | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 366 | func prebuiltLibraryBp2Build(ctx android.Bp2buildMutatorContext, module *Module) { | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 367 | prebuiltLibraryStaticBp2Build(ctx, module, true) | 
|  | 368 | prebuiltLibrarySharedBp2Build(ctx, module) | 
|  | 369 | } | 
|  | 370 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 371 | func prebuiltLibraryStaticBp2Build(ctx android.Bp2buildMutatorContext, module *Module, fullBuild bool) { | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 372 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true) | 
| Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 373 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil) | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 374 |  | 
|  | 375 | attrs := &bazelPrebuiltLibraryStaticAttributes{ | 
|  | 376 | Static_library:         prebuiltAttrs.Src, | 
|  | 377 | Export_includes:        exportedIncludes.Includes, | 
|  | 378 | Export_system_includes: exportedIncludes.SystemIncludes, | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c641cc4 | 2023-03-16 17:03:43 +0000 | [diff] [blame] | 379 | // TODO: ¿Alwayslink? | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 380 | } | 
|  | 381 |  | 
|  | 382 | props := bazel.BazelTargetModuleProperties{ | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 383 | Rule_class:        "cc_prebuilt_library_static", | 
|  | 384 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl", | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 385 | } | 
|  | 386 |  | 
|  | 387 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 388 | if fullBuild { | 
|  | 389 | name += "_bp2build_cc_library_static" | 
|  | 390 | } | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 391 |  | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 392 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module) | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 393 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled) | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c353abd | 2023-03-10 20:57:38 +0000 | [diff] [blame] | 394 |  | 
|  | 395 | _true := true | 
|  | 396 | alwayslinkAttrs := *attrs | 
|  | 397 | alwayslinkAttrs.Alwayslink.SetValue(&_true) | 
|  | 398 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name + "_alwayslink", Tags: tags}, &alwayslinkAttrs, prebuiltAttrs.Enabled) | 
| Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 399 | } | 
|  | 400 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 401 | type bazelPrebuiltLibrarySharedAttributes struct { | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c641cc4 | 2023-03-16 17:03:43 +0000 | [diff] [blame] | 402 | Shared_library         bazel.LabelAttribute | 
|  | 403 | Export_includes        bazel.StringListAttribute | 
|  | 404 | Export_system_includes bazel.StringListAttribute | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 405 | } | 
|  | 406 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 407 | func prebuiltLibrarySharedBp2Build(ctx android.Bp2buildMutatorContext, module *Module) { | 
| Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 408 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false) | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c641cc4 | 2023-03-16 17:03:43 +0000 | [diff] [blame] | 409 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 410 |  | 
|  | 411 | attrs := &bazelPrebuiltLibrarySharedAttributes{ | 
| Alex Márquez Pérez Muñíz Díaz Puras Thaureaux | c641cc4 | 2023-03-16 17:03:43 +0000 | [diff] [blame] | 412 | Shared_library:         prebuiltAttrs.Src, | 
|  | 413 | Export_includes:        exportedIncludes.Includes, | 
|  | 414 | Export_system_includes: exportedIncludes.SystemIncludes, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 415 | } | 
|  | 416 |  | 
|  | 417 | props := bazel.BazelTargetModuleProperties{ | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 418 | Rule_class:        "cc_prebuilt_library_shared", | 
|  | 419 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_shared.bzl", | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 420 | } | 
|  | 421 |  | 
|  | 422 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 423 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module) | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 424 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, attrs, prebuiltAttrs.Enabled) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 425 | } | 
|  | 426 |  | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 427 | type prebuiltObjectProperties struct { | 
|  | 428 | Srcs []string `android:"path,arch_variant"` | 
|  | 429 | } | 
|  | 430 |  | 
|  | 431 | type prebuiltObjectLinker struct { | 
|  | 432 | android.Prebuilt | 
|  | 433 | objectLinker | 
|  | 434 |  | 
|  | 435 | properties prebuiltObjectProperties | 
|  | 436 | } | 
|  | 437 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 438 | type prebuiltLibraryBazelHandler struct { | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 439 | module  *Module | 
|  | 440 | library *libraryDecorator | 
|  | 441 | } | 
|  | 442 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 443 | var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil) | 
| Chris Parsons | 6ce2cf9 | 2022-05-20 10:54:17 -0400 | [diff] [blame] | 444 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 445 | func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { | 
|  | 446 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { | 
|  | 447 | return | 
|  | 448 | } | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 449 | bazelCtx := ctx.Config().BazelContext | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 450 | bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx)) | 
|  | 451 | } | 
|  | 452 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 453 | func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { | 
|  | 454 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { | 
|  | 455 | return | 
|  | 456 | } | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 457 | bazelCtx := ctx.Config().BazelContext | 
|  | 458 | ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) | 
| Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 459 | if err != nil { | 
| Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 460 | ctx.ModuleErrorf(err.Error()) | 
|  | 461 | return | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 462 | } | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 463 |  | 
|  | 464 | if h.module.static() { | 
|  | 465 | if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok { | 
|  | 466 | return | 
|  | 467 | } | 
|  | 468 | } else if h.module.Shared() { | 
|  | 469 | if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok { | 
|  | 470 | return | 
|  | 471 | } | 
|  | 472 | } else { | 
|  | 473 | return | 
|  | 474 | } | 
|  | 475 |  | 
|  | 476 | h.module.maybeUnhideFromMake() | 
| Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 477 |  | 
|  | 478 | h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo) | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 479 | } | 
|  | 480 |  | 
|  | 481 | func (h *prebuiltLibraryBazelHandler) processStaticBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool { | 
| Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 482 | staticLibs := ccInfo.CcStaticLibraryFiles | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 483 | if len(staticLibs) > 1 { | 
|  | 484 | ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs) | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 485 | return false | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 486 | } | 
|  | 487 |  | 
|  | 488 | // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags | 
|  | 489 |  | 
|  | 490 | // TODO(eakammer):Add stub-related flags if this library is a stub library. | 
|  | 491 | // h.library.exportVersioningMacroIfNeeded(ctx) | 
|  | 492 |  | 
|  | 493 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise | 
|  | 494 | // validation will fail. For now, set this to an empty list. | 
|  | 495 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. | 
|  | 496 | h.library.collectedSnapshotHeaders = android.Paths{} | 
|  | 497 |  | 
|  | 498 | if len(staticLibs) == 0 { | 
|  | 499 | h.module.outputFile = android.OptionalPath{} | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 500 | return true | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 501 | } | 
|  | 502 |  | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 503 | var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0]) | 
|  | 504 | if len(ccInfo.TidyFiles) > 0 { | 
|  | 505 | h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) | 
|  | 506 | outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles) | 
|  | 507 | } | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 508 |  | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 509 | h.module.outputFile = android.OptionalPathForPath(outputPath) | 
|  | 510 |  | 
| Colin Cross | c85750b | 2022-04-21 12:50:51 -0700 | [diff] [blame] | 511 | depSet := android.NewDepSetBuilder[android.Path](android.TOPOLOGICAL).Direct(outputPath).Build() | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 512 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 513 | StaticLibrary:                        outputPath, | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 514 | TransitiveStaticLibrariesForOrdering: depSet, | 
|  | 515 | }) | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 516 |  | 
|  | 517 | return true | 
| Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 518 | } | 
|  | 519 |  | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 520 | func (h *prebuiltLibraryBazelHandler) processSharedBazelQueryResponse(ctx android.ModuleContext, label string, ccInfo cquery.CcInfo) bool { | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 521 | sharedLibs := ccInfo.CcSharedLibraryFiles | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 522 | if len(sharedLibs) > 1 { | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 523 | ctx.ModuleErrorf("expected 1 shared library from bazel target %s, got %q", label, sharedLibs) | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 524 | return false | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 525 | } | 
|  | 526 |  | 
|  | 527 | // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags | 
|  | 528 |  | 
|  | 529 | // TODO(eakammer):Add stub-related flags if this library is a stub library. | 
|  | 530 | // h.library.exportVersioningMacroIfNeeded(ctx) | 
|  | 531 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 532 | if len(sharedLibs) == 0 { | 
|  | 533 | h.module.outputFile = android.OptionalPath{} | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 534 | return true | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 535 | } | 
|  | 536 |  | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 537 | var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0]) | 
|  | 538 | if len(ccInfo.TidyFiles) > 0 { | 
|  | 539 | h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) | 
|  | 540 | outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles) | 
|  | 541 | } | 
|  | 542 |  | 
|  | 543 | h.module.outputFile = android.OptionalPathForPath(outputPath) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 544 |  | 
|  | 545 | // FIXME(b/214600441): We don't yet strip prebuilt shared libraries | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 546 | h.library.unstrippedOutputFile = outputPath | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 547 |  | 
|  | 548 | var toc android.Path | 
|  | 549 | if len(ccInfo.TocFile) > 0 { | 
|  | 550 | toc = android.PathForBazelOut(ctx, ccInfo.TocFile) | 
|  | 551 | } else { | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 552 | toc = outputPath // Just reuse `out` so ninja still gets an input but won't matter | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 553 | } | 
|  | 554 |  | 
|  | 555 | info := SharedLibraryInfo{ | 
| Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame] | 556 | SharedLibrary:   outputPath, | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 557 | TableOfContents: android.OptionalPathForPath(toc), | 
|  | 558 | Target:          ctx.Target(), | 
|  | 559 | } | 
|  | 560 | ctx.SetProvider(SharedLibraryInfoProvider, info) | 
|  | 561 |  | 
|  | 562 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) | 
|  | 563 | h.module.maybeUnhideFromMake() | 
| Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 564 | return true | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 565 | } | 
|  | 566 |  | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 567 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { | 
|  | 568 | return &p.Prebuilt | 
|  | 569 | } | 
|  | 570 |  | 
|  | 571 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) | 
|  | 572 |  | 
|  | 573 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, | 
|  | 574 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
|  | 575 | if len(p.properties.Srcs) > 0 { | 
| Colin Cross | ee02aed | 2022-04-15 15:16:02 -0700 | [diff] [blame] | 576 | // Copy objects to a name matching the final installed name | 
|  | 577 | in := p.Prebuilt.SingleSourcePath(ctx) | 
|  | 578 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o") | 
|  | 579 | ctx.Build(pctx, android.BuildParams{ | 
|  | 580 | Rule:        android.CpExecutable, | 
|  | 581 | Description: "prebuilt", | 
|  | 582 | Output:      outputFile, | 
|  | 583 | Input:       in, | 
|  | 584 | }) | 
|  | 585 | return outputFile | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 586 | } | 
|  | 587 | return nil | 
|  | 588 | } | 
|  | 589 |  | 
| Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 590 | func (p *prebuiltObjectLinker) object() bool { | 
|  | 591 | return true | 
|  | 592 | } | 
|  | 593 |  | 
| Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 594 | func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module { | 
|  | 595 | module := newObject(hod) | 
| Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 596 | module.bazelHandler = &prebuiltObjectBazelHandler{module: module} | 
|  | 597 | module.bazelable = true | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 598 | prebuilt := &prebuiltObjectLinker{ | 
|  | 599 | objectLinker: objectLinker{ | 
|  | 600 | baseLinker: NewBaseLinker(nil), | 
|  | 601 | }, | 
|  | 602 | } | 
|  | 603 | module.linker = prebuilt | 
|  | 604 | module.AddProperties(&prebuilt.properties) | 
|  | 605 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 606 | return module | 
|  | 607 | } | 
|  | 608 |  | 
| Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 609 | type prebuiltObjectBazelHandler struct { | 
|  | 610 | module *Module | 
|  | 611 | } | 
|  | 612 |  | 
|  | 613 | var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil) | 
|  | 614 |  | 
|  | 615 | func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { | 
|  | 616 | bazelCtx := ctx.Config().BazelContext | 
|  | 617 | bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx)) | 
|  | 618 | } | 
|  | 619 |  | 
|  | 620 | func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { | 
|  | 621 | bazelCtx := ctx.Config().BazelContext | 
|  | 622 | outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx)) | 
|  | 623 | if err != nil { | 
|  | 624 | ctx.ModuleErrorf(err.Error()) | 
|  | 625 | return | 
|  | 626 | } | 
|  | 627 | if len(outputs) != 1 { | 
|  | 628 | ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs) | 
|  | 629 | return | 
|  | 630 | } | 
|  | 631 | out := android.PathForBazelOut(ctx, outputs[0]) | 
|  | 632 | h.module.outputFile = android.OptionalPathForPath(out) | 
|  | 633 | h.module.maybeUnhideFromMake() | 
|  | 634 | } | 
|  | 635 |  | 
|  | 636 | type bazelPrebuiltObjectAttributes struct { | 
|  | 637 | Src bazel.LabelAttribute | 
|  | 638 | } | 
|  | 639 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 640 | func prebuiltObjectBp2Build(ctx android.Bp2buildMutatorContext, module *Module) { | 
| Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 641 | prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module) | 
|  | 642 |  | 
|  | 643 | attrs := &bazelPrebuiltObjectAttributes{ | 
|  | 644 | Src: prebuiltAttrs.Src, | 
|  | 645 | } | 
|  | 646 |  | 
|  | 647 | props := bazel.BazelTargetModuleProperties{ | 
|  | 648 | Rule_class:        "cc_prebuilt_object", | 
|  | 649 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl", | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 653 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module) | 
| Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 654 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs) | 
|  | 655 | } | 
|  | 656 |  | 
|  | 657 | func PrebuiltObjectFactory() android.Module { | 
| Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 658 | module := NewPrebuiltObject(android.HostAndDeviceSupported) | 
| Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 659 | return module.Init() | 
|  | 660 | } | 
|  | 661 |  | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 662 | type prebuiltBinaryLinker struct { | 
|  | 663 | *binaryDecorator | 
|  | 664 | prebuiltLinker | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 665 |  | 
|  | 666 | toolPath android.OptionalPath | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 667 | } | 
|  | 668 |  | 
|  | 669 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) | 
|  | 670 |  | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 671 | func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath { | 
|  | 672 | return p.toolPath | 
|  | 673 | } | 
|  | 674 |  | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 675 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, | 
|  | 676 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
|  | 677 | // TODO(ccross): verify shared library dependencies | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 678 | if len(p.properties.Srcs) > 0 { | 
| Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 679 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() | 
|  | 680 | in := p.Prebuilt.SingleSourcePath(ctx) | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 681 | outputFile := android.PathForModuleOut(ctx, fileName) | 
| Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 682 | p.unstrippedOutputFile = in | 
|  | 683 |  | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 684 | if ctx.Host() { | 
|  | 685 | // Host binaries are symlinked to their prebuilt source locations. That | 
|  | 686 | // way they are executed directly from there so the linker resolves their | 
|  | 687 | // shared library dependencies relative to that location (using | 
|  | 688 | // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt | 
|  | 689 | // repository can supply the expected versions of the shared libraries | 
|  | 690 | // without interference from what is in the out tree. | 
| Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 691 |  | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 692 | // These shared lib paths may point to copies of the libs in | 
|  | 693 | // .intermediates, which isn't where the binary will load them from, but | 
|  | 694 | // it's fine for dependency tracking. If a library dependency is updated, | 
|  | 695 | // the symlink will get a new timestamp, along with any installed symlinks | 
|  | 696 | // handled in make. | 
|  | 697 | sharedLibPaths := deps.EarlySharedLibs | 
|  | 698 | sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...) | 
|  | 699 | sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...) | 
|  | 700 |  | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 701 | var fromPath = in.String() | 
|  | 702 | if !filepath.IsAbs(fromPath) { | 
|  | 703 | fromPath = "$$PWD/" + fromPath | 
|  | 704 | } | 
|  | 705 |  | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 706 | ctx.Build(pctx, android.BuildParams{ | 
|  | 707 | Rule:      android.Symlink, | 
|  | 708 | Output:    outputFile, | 
|  | 709 | Input:     in, | 
|  | 710 | Implicits: sharedLibPaths, | 
|  | 711 | Args: map[string]string{ | 
| Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 712 | "fromPath": fromPath, | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 713 | }, | 
|  | 714 | }) | 
|  | 715 |  | 
|  | 716 | p.toolPath = android.OptionalPathForPath(outputFile) | 
|  | 717 | } else { | 
|  | 718 | if p.stripper.NeedsStrip(ctx) { | 
|  | 719 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) | 
|  | 720 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags)) | 
|  | 721 | in = stripped | 
|  | 722 | } | 
|  | 723 |  | 
|  | 724 | // Copy binaries to a name matching the final installed name | 
|  | 725 | ctx.Build(pctx, android.BuildParams{ | 
|  | 726 | Rule:        android.CpExecutable, | 
|  | 727 | Description: "prebuilt", | 
|  | 728 | Output:      outputFile, | 
|  | 729 | Input:       in, | 
|  | 730 | }) | 
|  | 731 | } | 
| Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 732 |  | 
|  | 733 | return outputFile | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 734 | } | 
|  | 735 |  | 
|  | 736 | return nil | 
|  | 737 | } | 
|  | 738 |  | 
| Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 739 | func (p *prebuiltBinaryLinker) binary() bool { | 
|  | 740 | return true | 
|  | 741 | } | 
|  | 742 |  | 
| Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 743 | // 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] | 744 | // device's directory, for both the host and device | 
|  | 745 | func PrebuiltBinaryFactory() android.Module { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 746 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) | 
|  | 747 | return module.Init() | 
|  | 748 | } | 
|  | 749 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 750 | type prebuiltBinaryBazelHandler struct { | 
|  | 751 | module    *Module | 
|  | 752 | decorator *binaryDecorator | 
|  | 753 | } | 
|  | 754 |  | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 755 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 756 | module, binary := newBinary(hod, true) | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 757 | module.compiler = nil | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 758 | module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary} | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 759 |  | 
|  | 760 | prebuilt := &prebuiltBinaryLinker{ | 
|  | 761 | binaryDecorator: binary, | 
|  | 762 | } | 
|  | 763 | module.linker = prebuilt | 
| Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 764 | module.installer = prebuilt | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 765 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 766 | module.AddProperties(&prebuilt.properties) | 
|  | 767 |  | 
|  | 768 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 769 | return module, binary | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 770 | } | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 771 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 772 | var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil) | 
|  | 773 |  | 
|  | 774 | func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { | 
|  | 775 | bazelCtx := ctx.Config().BazelContext | 
| Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 776 | bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 777 | } | 
|  | 778 |  | 
|  | 779 | func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { | 
|  | 780 | bazelCtx := ctx.Config().BazelContext | 
| Yu Liu | e431240 | 2023-01-18 09:15:31 -0800 | [diff] [blame] | 781 | outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKeyApexVariant(ctx, GetApexConfigKey(ctx))) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 782 | if err != nil { | 
|  | 783 | ctx.ModuleErrorf(err.Error()) | 
|  | 784 | return | 
|  | 785 | } | 
|  | 786 | if len(outputs) != 1 { | 
|  | 787 | ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs) | 
|  | 788 | return | 
|  | 789 | } | 
|  | 790 | out := android.PathForBazelOut(ctx, outputs[0]) | 
|  | 791 | h.module.outputFile = android.OptionalPathForPath(out) | 
|  | 792 | h.module.maybeUnhideFromMake() | 
|  | 793 | } | 
|  | 794 |  | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 795 | type bazelPrebuiltBinaryAttributes struct { | 
|  | 796 | Src   bazel.LabelAttribute | 
|  | 797 | Strip stripAttributes | 
|  | 798 | } | 
|  | 799 |  | 
| Chris Parsons | 637458d | 2023-09-19 20:09:00 +0000 | [diff] [blame] | 800 | func prebuiltBinaryBp2Build(ctx android.Bp2buildMutatorContext, module *Module) { | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 801 | prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module) | 
|  | 802 |  | 
|  | 803 | var la linkerAttributes | 
|  | 804 | la.convertStripProps(ctx, module) | 
|  | 805 | attrs := &bazelPrebuiltBinaryAttributes{ | 
|  | 806 | Src:   prebuiltAttrs.Src, | 
|  | 807 | Strip: stripAttrsFromLinkerAttrs(&la), | 
|  | 808 | } | 
|  | 809 |  | 
|  | 810 | props := bazel.BazelTargetModuleProperties{ | 
|  | 811 | Rule_class:        "cc_prebuilt_binary", | 
|  | 812 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl", | 
|  | 813 | } | 
|  | 814 |  | 
|  | 815 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) | 
| Spandan Das | 39b6cc5 | 2023-04-12 19:05:49 +0000 | [diff] [blame] | 816 | tags := android.ApexAvailableTagsWithoutTestApexes(ctx, module) | 
| Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 817 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs) | 
| Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 818 | } | 
|  | 819 |  | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 820 | type Sanitized struct { | 
|  | 821 | None struct { | 
|  | 822 | Srcs []string `android:"path,arch_variant"` | 
|  | 823 | } `android:"arch_variant"` | 
|  | 824 | Address struct { | 
|  | 825 | Srcs []string `android:"path,arch_variant"` | 
|  | 826 | } `android:"arch_variant"` | 
|  | 827 | Hwaddress struct { | 
|  | 828 | Srcs []string `android:"path,arch_variant"` | 
|  | 829 | } `android:"arch_variant"` | 
|  | 830 | } | 
|  | 831 |  | 
|  | 832 | func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string { | 
|  | 833 | if sanitize == nil { | 
|  | 834 | return nil | 
|  | 835 | } | 
| Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 836 | if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil { | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 837 | return sanitized.Address.Srcs | 
|  | 838 | } | 
| Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 839 | if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil { | 
| Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 840 | return sanitized.Hwaddress.Srcs | 
|  | 841 | } | 
|  | 842 | return sanitized.None.Srcs | 
|  | 843 | } |