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" |
Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 19 | "strings" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 20 | |
| 21 | "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] | 22 | "android/soong/bazel" |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 23 | "android/soong/bazel/cquery" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | func init() { |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 27 | RegisterPrebuiltBuildComponents(android.InitRegistrationContext) |
| 28 | } |
| 29 | |
| 30 | func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 31 | ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 32 | ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory) |
| 33 | ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory) |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 34 | ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 35 | ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 36 | ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type prebuiltLinkerInterface interface { |
| 40 | Name(string) string |
| 41 | prebuilt() *android.Prebuilt |
| 42 | } |
| 43 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 44 | type prebuiltLinkerProperties struct { |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 45 | // a prebuilt library or binary. Can reference a genrule module that generates an executable file. |
| 46 | Srcs []string `android:"path,arch_variant"` |
| 47 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 48 | Sanitized Sanitized `android:"arch_variant"` |
| 49 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 50 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined |
| 51 | // symbols, etc), default true. |
| 52 | Check_elf_files *bool |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 53 | |
Pierre-Clément Tosi | 6f630ae | 2022-08-10 09:25:54 +0100 | [diff] [blame] | 54 | // if set, add an extra objcopy --prefix-symbols= step |
| 55 | Prefix_symbols *string |
| 56 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 57 | // Optionally provide an import library if this is a Windows PE DLL prebuilt. |
| 58 | // This is needed only if this library is linked by other modules in build time. |
| 59 | // Only makes sense for the Windows target. |
| 60 | Windows_import_lib *string `android:"path,arch_variant"` |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 61 | |
| 62 | // MixedBuildsDisabled is true if and only if building this prebuilt is explicitly disabled in mixed builds for either |
| 63 | // its static or shared version on the current build variant. This is to prevent Bazel targets for build variants with |
| 64 | // which either the static or shared version is incompatible from participating in mixed buiods. Please note that this |
| 65 | // is an override and does not fully determine whether Bazel or Soong will be used. For the full determination, see |
| 66 | // cc.ProcessBazelQueryResponse, cc.QueueBazelCall, and cc.MixedBuildsDisabled. |
| 67 | MixedBuildsDisabled bool `blueprint:"mutated"` |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 70 | type prebuiltLinker struct { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 71 | android.Prebuilt |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 72 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 73 | properties prebuiltLinkerProperties |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 76 | func (p *prebuiltLinker) prebuilt() *android.Prebuilt { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 77 | return &p.Prebuilt |
| 78 | } |
| 79 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 80 | func (p *prebuiltLinker) PrebuiltSrcs() []string { |
| 81 | return p.properties.Srcs |
| 82 | } |
| 83 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 84 | type prebuiltLibraryInterface interface { |
| 85 | libraryInterface |
| 86 | prebuiltLinkerInterface |
| 87 | disablePrebuilt() |
| 88 | } |
| 89 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 90 | type prebuiltLibraryLinker struct { |
| 91 | *libraryDecorator |
| 92 | prebuiltLinker |
| 93 | } |
| 94 | |
| 95 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 96 | var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 97 | |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 98 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} |
| 99 | |
| 100 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 101 | return p.libraryDecorator.linkerDeps(ctx, deps) |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 105 | return flags |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { |
| 109 | return p.libraryDecorator.linkerProps() |
| 110 | } |
| 111 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 112 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 113 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 114 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 115 | p.libraryDecorator.flagExporter.exportIncludes(ctx) |
| 116 | p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...) |
| 117 | p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...) |
| 118 | p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...) |
| 119 | p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...) |
| 120 | p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) |
| 121 | |
| 122 | p.libraryDecorator.flagExporter.setProvider(ctx) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 123 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 124 | // TODO(ccross): verify shared library dependencies |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 125 | srcs := p.prebuiltSrcs(ctx) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 126 | if len(srcs) > 0 { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 127 | if len(srcs) > 1 { |
| 128 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 129 | return nil |
| 130 | } |
| 131 | |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 132 | p.libraryDecorator.exportVersioningMacroIfNeeded(ctx) |
| 133 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 134 | in := android.PathForModuleSrc(ctx, srcs[0]) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 135 | |
Pierre-Clément Tosi | 6f630ae | 2022-08-10 09:25:54 +0100 | [diff] [blame] | 136 | if String(p.prebuiltLinker.properties.Prefix_symbols) != "" { |
| 137 | prefixed := android.PathForModuleOut(ctx, "prefixed", srcs[0]) |
| 138 | transformBinaryPrefixSymbols(ctx, String(p.prebuiltLinker.properties.Prefix_symbols), |
| 139 | in, flagsToBuilderFlags(flags), prefixed) |
| 140 | in = prefixed |
| 141 | } |
| 142 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 143 | if p.static() { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 144 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build() |
| 145 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 146 | StaticLibrary: in, |
| 147 | |
| 148 | TransitiveStaticLibrariesForOrdering: depSet, |
| 149 | }) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 150 | return in |
| 151 | } |
| 152 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 153 | if p.shared() { |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 154 | p.unstrippedOutputFile = in |
Colin Cross | 0fd6a41 | 2019-08-16 14:22:10 -0700 | [diff] [blame] | 155 | libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 156 | outputFile := android.PathForModuleOut(ctx, libName) |
| 157 | var implicits android.Paths |
| 158 | |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 159 | if p.stripper.NeedsStrip(ctx) { |
| 160 | stripFlags := flagsToStripFlags(flags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 161 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 162 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 163 | in = stripped |
| 164 | } |
| 165 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 166 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 167 | // depending on a table of contents file instead of the library itself. |
| 168 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 169 | p.tocFile = android.OptionalPathForPath(tocFile) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 170 | TransformSharedObjectToToc(ctx, outputFile, tocFile) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 171 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 172 | if ctx.Windows() && p.properties.Windows_import_lib != nil { |
| 173 | // Consumers of this library actually links to the import library in build |
| 174 | // time and dynamically links to the DLL in run time. i.e. |
| 175 | // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll |
| 176 | importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib)) |
| 177 | importLibName := p.libraryDecorator.getLibName(ctx) + ".lib" |
| 178 | importLibOutputFile := android.PathForModuleOut(ctx, importLibName) |
| 179 | implicits = append(implicits, importLibOutputFile) |
| 180 | |
| 181 | ctx.Build(pctx, android.BuildParams{ |
| 182 | Rule: android.Cp, |
| 183 | Description: "prebuilt import library", |
| 184 | Input: importLibSrc, |
| 185 | Output: importLibOutputFile, |
| 186 | Args: map[string]string{ |
| 187 | "cpFlags": "-L", |
| 188 | }, |
| 189 | }) |
| 190 | } |
| 191 | |
| 192 | ctx.Build(pctx, android.BuildParams{ |
| 193 | Rule: android.Cp, |
| 194 | Description: "prebuilt shared library", |
| 195 | Implicits: implicits, |
| 196 | Input: in, |
| 197 | Output: outputFile, |
| 198 | Args: map[string]string{ |
| 199 | "cpFlags": "-L", |
| 200 | }, |
| 201 | }) |
| 202 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 203 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
Liz Kammer | ef6dfea | 2021-06-08 15:37:09 -0400 | [diff] [blame] | 204 | SharedLibrary: outputFile, |
| 205 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 206 | |
| 207 | TableOfContents: p.tocFile, |
| 208 | }) |
| 209 | |
Martin Stjernholm | 5bdf2d5 | 2022-02-06 22:07:45 +0000 | [diff] [blame] | 210 | // TODO(b/220898484): Mainline module sdk prebuilts of stub libraries use a stub |
| 211 | // library as their source and must not be installed, but libclang_rt.* libraries |
| 212 | // have stubs because they are LLNDK libraries, but use an implementation library |
| 213 | // as their source and need to be installed. This discrepancy should be resolved |
| 214 | // without the prefix hack below. |
| 215 | if p.hasStubsVariants() && !p.buildStubs() && !ctx.Host() && |
| 216 | !strings.HasPrefix(ctx.baseModuleName(), "libclang_rt.") { |
| 217 | ctx.Module().MakeUninstallable() |
| 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 | // Prebuilt libraries can be used in SDKs. |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 297 | android.InitSdkAwareModule(module) |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 298 | return module, library |
| 299 | } |
| 300 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 301 | // cc_prebuilt_library installs a precompiled shared library that are |
| 302 | // listed in the srcs property in the device's directory. |
| 303 | func PrebuiltLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 304 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 305 | |
| 306 | // Prebuilt shared libraries can be included in APEXes |
| 307 | android.InitApexModule(module) |
| 308 | |
| 309 | return module.Init() |
| 310 | } |
| 311 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 312 | // cc_prebuilt_library_shared installs a precompiled shared library that are |
| 313 | // listed in the srcs property in the device's directory. |
| 314 | func PrebuiltSharedLibraryFactory() android.Module { |
| 315 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 316 | return module.Init() |
| 317 | } |
| 318 | |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 319 | // cc_prebuilt_test_library_shared installs a precompiled shared library |
| 320 | // to be used as a data dependency of a test-related module (such as cc_test, or |
| 321 | // cc_test_library). |
| 322 | func PrebuiltSharedTestLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 323 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 324 | library.BuildOnlyShared() |
| 325 | library.baseInstaller = NewTestInstaller() |
| 326 | return module.Init() |
| 327 | } |
| 328 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 329 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 330 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 331 | library.BuildOnlyShared() |
| 332 | |
| 333 | // Prebuilt shared libraries can be included in APEXes |
| 334 | android.InitApexModule(module) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 335 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 336 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 339 | // cc_prebuilt_library_static installs a precompiled static library that are |
| 340 | // listed in the srcs property in the device's directory. |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 341 | func PrebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 342 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 343 | return module.Init() |
| 344 | } |
| 345 | |
| 346 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 347 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 348 | library.BuildOnlyStatic() |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 349 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 350 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 353 | type bazelPrebuiltLibraryStaticAttributes struct { |
| 354 | Static_library bazel.LabelAttribute |
| 355 | Export_includes bazel.StringListAttribute |
| 356 | Export_system_includes bazel.StringListAttribute |
| 357 | } |
| 358 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 359 | // TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated |
| 360 | // Implements bp2build for cc_prebuilt_library modules. This will generate: |
Colin Cross | d079e0b | 2022-08-16 10:27:33 -0700 | [diff] [blame] | 361 | // - Only a prebuilt_library_static if the shared.enabled property is set to false across all variants. |
| 362 | // - Only a prebuilt_library_shared if the static.enabled property is set to false across all variants |
| 363 | // - Both a prebuilt_library_static and prebuilt_library_shared if the aforementioned properties are not false across |
| 364 | // all variants |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 365 | // |
| 366 | // In all cases, prebuilt_library_static target names will be appended with "_bp2build_cc_library_static". |
| 367 | func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 368 | prebuiltLibraryStaticBp2Build(ctx, module, true) |
| 369 | prebuiltLibrarySharedBp2Build(ctx, module) |
| 370 | } |
| 371 | |
| 372 | func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) { |
| 373 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true) |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 374 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 375 | |
| 376 | attrs := &bazelPrebuiltLibraryStaticAttributes{ |
| 377 | Static_library: prebuiltAttrs.Src, |
| 378 | Export_includes: exportedIncludes.Includes, |
| 379 | Export_system_includes: exportedIncludes.SystemIncludes, |
| 380 | } |
| 381 | |
| 382 | props := bazel.BazelTargetModuleProperties{ |
| 383 | Rule_class: "prebuilt_library_static", |
Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 384 | Bzl_load_location: "//build/bazel/rules/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 | } |
| 391 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, attrs, prebuiltAttrs.Enabled) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 392 | } |
| 393 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 394 | type bazelPrebuiltLibrarySharedAttributes struct { |
| 395 | Shared_library bazel.LabelAttribute |
| 396 | } |
| 397 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 398 | func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 399 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, false) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 400 | |
| 401 | attrs := &bazelPrebuiltLibrarySharedAttributes{ |
| 402 | Shared_library: prebuiltAttrs.Src, |
| 403 | } |
| 404 | |
| 405 | props := bazel.BazelTargetModuleProperties{ |
| 406 | Rule_class: "prebuilt_library_shared", |
Liz Kammer | 2b376bc | 2022-01-12 12:00:49 -0500 | [diff] [blame] | 407 | Bzl_load_location: "//build/bazel/rules/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] | 408 | } |
| 409 | |
| 410 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 411 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name}, 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] | 412 | } |
| 413 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 414 | type prebuiltObjectProperties struct { |
| 415 | Srcs []string `android:"path,arch_variant"` |
| 416 | } |
| 417 | |
| 418 | type prebuiltObjectLinker struct { |
| 419 | android.Prebuilt |
| 420 | objectLinker |
| 421 | |
| 422 | properties prebuiltObjectProperties |
| 423 | } |
| 424 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 425 | type prebuiltLibraryBazelHandler struct { |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 426 | module *Module |
| 427 | library *libraryDecorator |
| 428 | } |
| 429 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 430 | var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil) |
Chris Parsons | 6ce2cf9 | 2022-05-20 10:54:17 -0400 | [diff] [blame] | 431 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 432 | func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
| 433 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { |
| 434 | return |
| 435 | } |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 436 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 437 | bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx)) |
| 438 | } |
| 439 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 440 | func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 441 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { |
| 442 | return |
| 443 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 444 | bazelCtx := ctx.Config().BazelContext |
| 445 | ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 446 | if err != nil { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 447 | ctx.ModuleErrorf(err.Error()) |
| 448 | return |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 449 | } |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 450 | |
| 451 | if h.module.static() { |
| 452 | if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok { |
| 453 | return |
| 454 | } |
| 455 | } else if h.module.Shared() { |
| 456 | if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok { |
| 457 | return |
| 458 | } |
| 459 | } else { |
| 460 | return |
| 461 | } |
| 462 | |
| 463 | h.module.maybeUnhideFromMake() |
| 464 | } |
| 465 | |
| 466 | 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] | 467 | staticLibs := ccInfo.CcStaticLibraryFiles |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 468 | if len(staticLibs) > 1 { |
| 469 | 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] | 470 | return false |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags |
| 474 | |
| 475 | // TODO(eakammer):Add stub-related flags if this library is a stub library. |
| 476 | // h.library.exportVersioningMacroIfNeeded(ctx) |
| 477 | |
| 478 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 479 | // validation will fail. For now, set this to an empty list. |
| 480 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 481 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 482 | |
| 483 | if len(staticLibs) == 0 { |
| 484 | h.module.outputFile = android.OptionalPath{} |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 485 | return true |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | out := android.PathForBazelOut(ctx, staticLibs[0]) |
| 489 | h.module.outputFile = android.OptionalPathForPath(out) |
| 490 | |
| 491 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build() |
| 492 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 493 | StaticLibrary: out, |
| 494 | |
| 495 | TransitiveStaticLibrariesForOrdering: depSet, |
| 496 | }) |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 497 | |
| 498 | return true |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 499 | } |
| 500 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 501 | 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] | 502 | sharedLibs := ccInfo.CcSharedLibraryFiles |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 503 | 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] | 504 | 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] | 505 | return false |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags |
| 509 | |
| 510 | // TODO(eakammer):Add stub-related flags if this library is a stub library. |
| 511 | // h.library.exportVersioningMacroIfNeeded(ctx) |
| 512 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 513 | if len(sharedLibs) == 0 { |
| 514 | h.module.outputFile = android.OptionalPath{} |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 515 | return true |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | out := android.PathForBazelOut(ctx, sharedLibs[0]) |
| 519 | h.module.outputFile = android.OptionalPathForPath(out) |
| 520 | |
| 521 | // FIXME(b/214600441): We don't yet strip prebuilt shared libraries |
| 522 | h.library.unstrippedOutputFile = out |
| 523 | |
| 524 | var toc android.Path |
| 525 | if len(ccInfo.TocFile) > 0 { |
| 526 | toc = android.PathForBazelOut(ctx, ccInfo.TocFile) |
| 527 | } else { |
| 528 | toc = out // Just reuse `out` so ninja still gets an input but won't matter |
| 529 | } |
| 530 | |
| 531 | info := SharedLibraryInfo{ |
| 532 | SharedLibrary: out, |
| 533 | TableOfContents: android.OptionalPathForPath(toc), |
| 534 | Target: ctx.Target(), |
| 535 | } |
| 536 | ctx.SetProvider(SharedLibraryInfoProvider, info) |
| 537 | |
| 538 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
| 539 | h.module.maybeUnhideFromMake() |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 540 | return true |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 543 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 544 | return &p.Prebuilt |
| 545 | } |
| 546 | |
| 547 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 548 | |
| 549 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 550 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 551 | if len(p.properties.Srcs) > 0 { |
Colin Cross | ee02aed | 2022-04-15 15:16:02 -0700 | [diff] [blame] | 552 | // Copy objects to a name matching the final installed name |
| 553 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 554 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o") |
| 555 | ctx.Build(pctx, android.BuildParams{ |
| 556 | Rule: android.CpExecutable, |
| 557 | Description: "prebuilt", |
| 558 | Output: outputFile, |
| 559 | Input: in, |
| 560 | }) |
| 561 | return outputFile |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 562 | } |
| 563 | return nil |
| 564 | } |
| 565 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 566 | func (p *prebuiltObjectLinker) object() bool { |
| 567 | return true |
| 568 | } |
| 569 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 570 | func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module { |
| 571 | module := newObject(hod) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 572 | prebuilt := &prebuiltObjectLinker{ |
| 573 | objectLinker: objectLinker{ |
| 574 | baseLinker: NewBaseLinker(nil), |
| 575 | }, |
| 576 | } |
| 577 | module.linker = prebuilt |
| 578 | module.AddProperties(&prebuilt.properties) |
| 579 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
| 580 | android.InitSdkAwareModule(module) |
| 581 | return module |
| 582 | } |
| 583 | |
| 584 | func prebuiltObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 585 | module := NewPrebuiltObject(android.HostAndDeviceSupported) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 586 | return module.Init() |
| 587 | } |
| 588 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 589 | type prebuiltBinaryLinker struct { |
| 590 | *binaryDecorator |
| 591 | prebuiltLinker |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 592 | |
| 593 | toolPath android.OptionalPath |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 597 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 598 | func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath { |
| 599 | return p.toolPath |
| 600 | } |
| 601 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 602 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 603 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 604 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 605 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 606 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 607 | in := p.Prebuilt.SingleSourcePath(ctx) |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 608 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 609 | p.unstrippedOutputFile = in |
| 610 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 611 | if ctx.Host() { |
| 612 | // Host binaries are symlinked to their prebuilt source locations. That |
| 613 | // way they are executed directly from there so the linker resolves their |
| 614 | // shared library dependencies relative to that location (using |
| 615 | // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt |
| 616 | // repository can supply the expected versions of the shared libraries |
| 617 | // without interference from what is in the out tree. |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 618 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 619 | // These shared lib paths may point to copies of the libs in |
| 620 | // .intermediates, which isn't where the binary will load them from, but |
| 621 | // it's fine for dependency tracking. If a library dependency is updated, |
| 622 | // the symlink will get a new timestamp, along with any installed symlinks |
| 623 | // handled in make. |
| 624 | sharedLibPaths := deps.EarlySharedLibs |
| 625 | sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...) |
| 626 | sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...) |
| 627 | |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 628 | var fromPath = in.String() |
| 629 | if !filepath.IsAbs(fromPath) { |
| 630 | fromPath = "$$PWD/" + fromPath |
| 631 | } |
| 632 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 633 | ctx.Build(pctx, android.BuildParams{ |
| 634 | Rule: android.Symlink, |
| 635 | Output: outputFile, |
| 636 | Input: in, |
| 637 | Implicits: sharedLibPaths, |
| 638 | Args: map[string]string{ |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 639 | "fromPath": fromPath, |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 640 | }, |
| 641 | }) |
| 642 | |
| 643 | p.toolPath = android.OptionalPathForPath(outputFile) |
| 644 | } else { |
| 645 | if p.stripper.NeedsStrip(ctx) { |
| 646 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
| 647 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags)) |
| 648 | in = stripped |
| 649 | } |
| 650 | |
| 651 | // Copy binaries to a name matching the final installed name |
| 652 | ctx.Build(pctx, android.BuildParams{ |
| 653 | Rule: android.CpExecutable, |
| 654 | Description: "prebuilt", |
| 655 | Output: outputFile, |
| 656 | Input: in, |
| 657 | }) |
| 658 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 659 | |
| 660 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | return nil |
| 664 | } |
| 665 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 666 | func (p *prebuiltBinaryLinker) binary() bool { |
| 667 | return true |
| 668 | } |
| 669 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 670 | // 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] | 671 | // device's directory, for both the host and device |
| 672 | func PrebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 673 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 674 | return module.Init() |
| 675 | } |
| 676 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame^] | 677 | type prebuiltBinaryBazelHandler struct { |
| 678 | module *Module |
| 679 | decorator *binaryDecorator |
| 680 | } |
| 681 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 682 | 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^] | 683 | module, binary := newBinary(hod, true) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 684 | 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^] | 685 | module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary} |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 686 | |
| 687 | prebuilt := &prebuiltBinaryLinker{ |
| 688 | binaryDecorator: binary, |
| 689 | } |
| 690 | module.linker = prebuilt |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 691 | module.installer = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 692 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 693 | module.AddProperties(&prebuilt.properties) |
| 694 | |
| 695 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 696 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 697 | } |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 698 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame^] | 699 | var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil) |
| 700 | |
| 701 | func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
| 702 | bazelCtx := ctx.Config().BazelContext |
| 703 | bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx)) |
| 704 | } |
| 705 | |
| 706 | func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 707 | bazelCtx := ctx.Config().BazelContext |
| 708 | outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx)) |
| 709 | if err != nil { |
| 710 | ctx.ModuleErrorf(err.Error()) |
| 711 | return |
| 712 | } |
| 713 | if len(outputs) != 1 { |
| 714 | ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs) |
| 715 | return |
| 716 | } |
| 717 | out := android.PathForBazelOut(ctx, outputs[0]) |
| 718 | h.module.outputFile = android.OptionalPathForPath(out) |
| 719 | h.module.maybeUnhideFromMake() |
| 720 | } |
| 721 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 722 | type bazelPrebuiltBinaryAttributes struct { |
| 723 | Src bazel.LabelAttribute |
| 724 | Strip stripAttributes |
| 725 | } |
| 726 | |
| 727 | func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 728 | prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module) |
| 729 | |
| 730 | var la linkerAttributes |
| 731 | la.convertStripProps(ctx, module) |
| 732 | attrs := &bazelPrebuiltBinaryAttributes{ |
| 733 | Src: prebuiltAttrs.Src, |
| 734 | Strip: stripAttrsFromLinkerAttrs(&la), |
| 735 | } |
| 736 | |
| 737 | props := bazel.BazelTargetModuleProperties{ |
| 738 | Rule_class: "cc_prebuilt_binary", |
| 739 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl", |
| 740 | } |
| 741 | |
| 742 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 743 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) |
| 744 | } |
| 745 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 746 | type Sanitized struct { |
| 747 | None struct { |
| 748 | Srcs []string `android:"path,arch_variant"` |
| 749 | } `android:"arch_variant"` |
| 750 | Address struct { |
| 751 | Srcs []string `android:"path,arch_variant"` |
| 752 | } `android:"arch_variant"` |
| 753 | Hwaddress struct { |
| 754 | Srcs []string `android:"path,arch_variant"` |
| 755 | } `android:"arch_variant"` |
| 756 | } |
| 757 | |
| 758 | func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string { |
| 759 | if sanitize == nil { |
| 760 | return nil |
| 761 | } |
| 762 | if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil { |
| 763 | return sanitized.Address.Srcs |
| 764 | } |
| 765 | if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil { |
| 766 | return sanitized.Hwaddress.Srcs |
| 767 | } |
| 768 | return sanitized.None.Srcs |
| 769 | } |