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) |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 35 | ctx.RegisterModuleType("cc_prebuilt_object", PrebuiltObjectFactory) |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 36 | ctx.RegisterModuleType("cc_prebuilt_binary", PrebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | type prebuiltLinkerInterface interface { |
| 40 | Name(string) string |
| 41 | prebuilt() *android.Prebuilt |
| 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 | 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 |
| 355 | } |
| 356 | |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 357 | // TODO(b/228623543): The below is not entirely true until the bug is fixed. For now, both targets are always generated |
| 358 | // 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] | 359 | // - Only a cc_prebuilt_library_static if the shared.enabled property is set to false across all variants. |
| 360 | // - Only a cc_prebuilt_library_shared if the static.enabled property is set to false across all variants |
| 361 | // - 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] | 362 | // all variants |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 363 | // |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 364 | // In all cases, cc_prebuilt_library_static target names will be appended with "_bp2build_cc_library_static". |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 365 | func prebuiltLibraryBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 366 | prebuiltLibraryStaticBp2Build(ctx, module, true) |
| 367 | prebuiltLibrarySharedBp2Build(ctx, module) |
| 368 | } |
| 369 | |
| 370 | func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module, fullBuild bool) { |
| 371 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module, true) |
Liz Kammer | 5454944 | 2022-05-11 13:55:06 -0400 | [diff] [blame] | 372 | exportedIncludes := bp2BuildParseExportedIncludes(ctx, module, nil) |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 373 | |
| 374 | attrs := &bazelPrebuiltLibraryStaticAttributes{ |
| 375 | Static_library: prebuiltAttrs.Src, |
| 376 | Export_includes: exportedIncludes.Includes, |
| 377 | Export_system_includes: exportedIncludes.SystemIncludes, |
| 378 | } |
| 379 | |
| 380 | 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] | 381 | Rule_class: "cc_prebuilt_library_static", |
| 382 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_library_static.bzl", |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
Trevor Radcliffe | 58ea451 | 2022-04-07 20:36:39 +0000 | [diff] [blame] | 386 | if fullBuild { |
| 387 | name += "_bp2build_cc_library_static" |
| 388 | } |
Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 389 | |
| 390 | tags := android.ApexAvailableTags(module) |
| 391 | ctx.CreateBazelTargetModuleWithRestrictions(props, android.CommonAttributes{Name: name, Tags: tags}, 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{ |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c5184ec | 2022-10-17 14:48:57 +0000 | [diff] [blame] | 406 | Rule_class: "cc_prebuilt_library_shared", |
| 407 | 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] | 408 | } |
| 409 | |
| 410 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 411 | tags := android.ApexAvailableTags(module) |
| 412 | 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] | 413 | } |
| 414 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 415 | type prebuiltObjectProperties struct { |
| 416 | Srcs []string `android:"path,arch_variant"` |
| 417 | } |
| 418 | |
| 419 | type prebuiltObjectLinker struct { |
| 420 | android.Prebuilt |
| 421 | objectLinker |
| 422 | |
| 423 | properties prebuiltObjectProperties |
| 424 | } |
| 425 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 426 | type prebuiltLibraryBazelHandler struct { |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 427 | module *Module |
| 428 | library *libraryDecorator |
| 429 | } |
| 430 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 431 | var _ BazelHandler = (*prebuiltLibraryBazelHandler)(nil) |
Chris Parsons | 6ce2cf9 | 2022-05-20 10:54:17 -0400 | [diff] [blame] | 432 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 433 | func (h *prebuiltLibraryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
| 434 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { |
| 435 | return |
| 436 | } |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 437 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 438 | bazelCtx.QueueBazelRequest(label, cquery.GetCcInfo, android.GetConfigKey(ctx)) |
| 439 | } |
| 440 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 441 | func (h *prebuiltLibraryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 442 | if h.module.linker.(*prebuiltLibraryLinker).properties.MixedBuildsDisabled { |
| 443 | return |
| 444 | } |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 445 | bazelCtx := ctx.Config().BazelContext |
| 446 | ccInfo, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 447 | if err != nil { |
Chris Parsons | f874e46 | 2022-05-10 13:50:12 -0400 | [diff] [blame] | 448 | ctx.ModuleErrorf(err.Error()) |
| 449 | return |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 450 | } |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 451 | |
| 452 | if h.module.static() { |
| 453 | if ok := h.processStaticBazelQueryResponse(ctx, label, ccInfo); !ok { |
| 454 | return |
| 455 | } |
| 456 | } else if h.module.Shared() { |
| 457 | if ok := h.processSharedBazelQueryResponse(ctx, label, ccInfo); !ok { |
| 458 | return |
| 459 | } |
| 460 | } else { |
| 461 | return |
| 462 | } |
| 463 | |
| 464 | h.module.maybeUnhideFromMake() |
Sam Delmerico | 5fb794a | 2023-01-27 16:01:37 -0500 | [diff] [blame] | 465 | |
| 466 | h.module.setAndroidMkVariablesFromCquery(ccInfo.CcAndroidMkInfo) |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | 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] | 470 | staticLibs := ccInfo.CcStaticLibraryFiles |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 471 | if len(staticLibs) > 1 { |
| 472 | 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] | 473 | return false |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags |
| 477 | |
| 478 | // TODO(eakammer):Add stub-related flags if this library is a stub library. |
| 479 | // h.library.exportVersioningMacroIfNeeded(ctx) |
| 480 | |
| 481 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 482 | // validation will fail. For now, set this to an empty list. |
| 483 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 484 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 485 | |
| 486 | if len(staticLibs) == 0 { |
| 487 | h.module.outputFile = android.OptionalPath{} |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 488 | return true |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 489 | } |
| 490 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 491 | var outputPath android.Path = android.PathForBazelOut(ctx, staticLibs[0]) |
| 492 | if len(ccInfo.TidyFiles) > 0 { |
| 493 | h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) |
| 494 | outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles) |
| 495 | } |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 496 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 497 | h.module.outputFile = android.OptionalPathForPath(outputPath) |
| 498 | |
| 499 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(outputPath).Build() |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 500 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 501 | StaticLibrary: outputPath, |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 502 | TransitiveStaticLibrariesForOrdering: depSet, |
| 503 | }) |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 504 | |
| 505 | return true |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 506 | } |
| 507 | |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 508 | 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] | 509 | sharedLibs := ccInfo.CcSharedLibraryFiles |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 510 | 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] | 511 | 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] | 512 | return false |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | // TODO(b/184543518): cc_prebuilt_library_shared may have properties for re-exporting flags |
| 516 | |
| 517 | // TODO(eakammer):Add stub-related flags if this library is a stub library. |
| 518 | // h.library.exportVersioningMacroIfNeeded(ctx) |
| 519 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 520 | if len(sharedLibs) == 0 { |
| 521 | h.module.outputFile = android.OptionalPath{} |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 522 | return true |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 523 | } |
| 524 | |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 525 | var outputPath android.Path = android.PathForBazelOut(ctx, sharedLibs[0]) |
| 526 | if len(ccInfo.TidyFiles) > 0 { |
| 527 | h.module.tidyFiles = android.PathsForBazelOut(ctx, ccInfo.TidyFiles) |
| 528 | outputPath = android.AttachValidationActions(ctx, outputPath, h.module.tidyFiles) |
| 529 | } |
| 530 | |
| 531 | 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] | 532 | |
| 533 | // FIXME(b/214600441): We don't yet strip prebuilt shared libraries |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 534 | 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] | 535 | |
| 536 | var toc android.Path |
| 537 | if len(ccInfo.TocFile) > 0 { |
| 538 | toc = android.PathForBazelOut(ctx, ccInfo.TocFile) |
| 539 | } else { |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 540 | 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] | 541 | } |
| 542 | |
| 543 | info := SharedLibraryInfo{ |
Sam Delmerico | 4ed95e2 | 2023-02-03 18:12:15 -0500 | [diff] [blame^] | 544 | SharedLibrary: outputPath, |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 545 | TableOfContents: android.OptionalPathForPath(toc), |
| 546 | Target: ctx.Target(), |
| 547 | } |
| 548 | ctx.SetProvider(SharedLibraryInfoProvider, info) |
| 549 | |
| 550 | h.library.setFlagExporterInfoFromCcInfo(ctx, ccInfo) |
| 551 | h.module.maybeUnhideFromMake() |
Trevor Radcliffe | 5d6fa4d | 2022-05-17 21:59:36 +0000 | [diff] [blame] | 552 | return true |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | c3b97c3 | 2021-10-05 13:43:23 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 555 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 556 | return &p.Prebuilt |
| 557 | } |
| 558 | |
| 559 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 560 | |
| 561 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 562 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 563 | if len(p.properties.Srcs) > 0 { |
Colin Cross | ee02aed | 2022-04-15 15:16:02 -0700 | [diff] [blame] | 564 | // Copy objects to a name matching the final installed name |
| 565 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 566 | outputFile := android.PathForModuleOut(ctx, ctx.ModuleName()+".o") |
| 567 | ctx.Build(pctx, android.BuildParams{ |
| 568 | Rule: android.CpExecutable, |
| 569 | Description: "prebuilt", |
| 570 | Output: outputFile, |
| 571 | Input: in, |
| 572 | }) |
| 573 | return outputFile |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 574 | } |
| 575 | return nil |
| 576 | } |
| 577 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 578 | func (p *prebuiltObjectLinker) object() bool { |
| 579 | return true |
| 580 | } |
| 581 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 582 | func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module { |
| 583 | module := newObject(hod) |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 584 | module.bazelHandler = &prebuiltObjectBazelHandler{module: module} |
| 585 | module.bazelable = true |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 586 | prebuilt := &prebuiltObjectLinker{ |
| 587 | objectLinker: objectLinker{ |
| 588 | baseLinker: NewBaseLinker(nil), |
| 589 | }, |
| 590 | } |
| 591 | module.linker = prebuilt |
| 592 | module.AddProperties(&prebuilt.properties) |
| 593 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 594 | return module |
| 595 | } |
| 596 | |
Colin Cross | c5075e9 | 2022-12-05 16:46:39 -0800 | [diff] [blame] | 597 | type prebuiltObjectBazelHandler struct { |
| 598 | module *Module |
| 599 | } |
| 600 | |
| 601 | var _ BazelHandler = (*prebuiltObjectBazelHandler)(nil) |
| 602 | |
| 603 | func (h *prebuiltObjectBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
| 604 | bazelCtx := ctx.Config().BazelContext |
| 605 | bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx)) |
| 606 | } |
| 607 | |
| 608 | func (h *prebuiltObjectBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 609 | bazelCtx := ctx.Config().BazelContext |
| 610 | outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx)) |
| 611 | if err != nil { |
| 612 | ctx.ModuleErrorf(err.Error()) |
| 613 | return |
| 614 | } |
| 615 | if len(outputs) != 1 { |
| 616 | ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs) |
| 617 | return |
| 618 | } |
| 619 | out := android.PathForBazelOut(ctx, outputs[0]) |
| 620 | h.module.outputFile = android.OptionalPathForPath(out) |
| 621 | h.module.maybeUnhideFromMake() |
| 622 | } |
| 623 | |
| 624 | type bazelPrebuiltObjectAttributes struct { |
| 625 | Src bazel.LabelAttribute |
| 626 | } |
| 627 | |
| 628 | func prebuiltObjectBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 629 | prebuiltAttrs := bp2BuildParsePrebuiltObjectProps(ctx, module) |
| 630 | |
| 631 | attrs := &bazelPrebuiltObjectAttributes{ |
| 632 | Src: prebuiltAttrs.Src, |
| 633 | } |
| 634 | |
| 635 | props := bazel.BazelTargetModuleProperties{ |
| 636 | Rule_class: "cc_prebuilt_object", |
| 637 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_object.bzl", |
| 638 | } |
| 639 | |
| 640 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 641 | tags := android.ApexAvailableTags(module) |
| 642 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name, Tags: tags}, attrs) |
| 643 | } |
| 644 | |
| 645 | func PrebuiltObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 646 | module := NewPrebuiltObject(android.HostAndDeviceSupported) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 647 | return module.Init() |
| 648 | } |
| 649 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 650 | type prebuiltBinaryLinker struct { |
| 651 | *binaryDecorator |
| 652 | prebuiltLinker |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 653 | |
| 654 | toolPath android.OptionalPath |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 658 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 659 | func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath { |
| 660 | return p.toolPath |
| 661 | } |
| 662 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 663 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 664 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 665 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 666 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 667 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 668 | in := p.Prebuilt.SingleSourcePath(ctx) |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 669 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 670 | p.unstrippedOutputFile = in |
| 671 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 672 | if ctx.Host() { |
| 673 | // Host binaries are symlinked to their prebuilt source locations. That |
| 674 | // way they are executed directly from there so the linker resolves their |
| 675 | // shared library dependencies relative to that location (using |
| 676 | // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt |
| 677 | // repository can supply the expected versions of the shared libraries |
| 678 | // without interference from what is in the out tree. |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 679 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 680 | // These shared lib paths may point to copies of the libs in |
| 681 | // .intermediates, which isn't where the binary will load them from, but |
| 682 | // it's fine for dependency tracking. If a library dependency is updated, |
| 683 | // the symlink will get a new timestamp, along with any installed symlinks |
| 684 | // handled in make. |
| 685 | sharedLibPaths := deps.EarlySharedLibs |
| 686 | sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...) |
| 687 | sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...) |
| 688 | |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 689 | var fromPath = in.String() |
| 690 | if !filepath.IsAbs(fromPath) { |
| 691 | fromPath = "$$PWD/" + fromPath |
| 692 | } |
| 693 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 694 | ctx.Build(pctx, android.BuildParams{ |
| 695 | Rule: android.Symlink, |
| 696 | Output: outputFile, |
| 697 | Input: in, |
| 698 | Implicits: sharedLibPaths, |
| 699 | Args: map[string]string{ |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 700 | "fromPath": fromPath, |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 701 | }, |
| 702 | }) |
| 703 | |
| 704 | p.toolPath = android.OptionalPathForPath(outputFile) |
| 705 | } else { |
| 706 | if p.stripper.NeedsStrip(ctx) { |
| 707 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
| 708 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags)) |
| 709 | in = stripped |
| 710 | } |
| 711 | |
| 712 | // Copy binaries to a name matching the final installed name |
| 713 | ctx.Build(pctx, android.BuildParams{ |
| 714 | Rule: android.CpExecutable, |
| 715 | Description: "prebuilt", |
| 716 | Output: outputFile, |
| 717 | Input: in, |
| 718 | }) |
| 719 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 720 | |
| 721 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | return nil |
| 725 | } |
| 726 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 727 | func (p *prebuiltBinaryLinker) binary() bool { |
| 728 | return true |
| 729 | } |
| 730 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 731 | // 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] | 732 | // device's directory, for both the host and device |
| 733 | func PrebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 734 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 735 | return module.Init() |
| 736 | } |
| 737 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 738 | type prebuiltBinaryBazelHandler struct { |
| 739 | module *Module |
| 740 | decorator *binaryDecorator |
| 741 | } |
| 742 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 743 | 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] | 744 | module, binary := newBinary(hod, true) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 745 | 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] | 746 | module.bazelHandler = &prebuiltBinaryBazelHandler{module, binary} |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 747 | |
| 748 | prebuilt := &prebuiltBinaryLinker{ |
| 749 | binaryDecorator: binary, |
| 750 | } |
| 751 | module.linker = prebuilt |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 752 | module.installer = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 753 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 754 | module.AddProperties(&prebuilt.properties) |
| 755 | |
| 756 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 757 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 758 | } |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 759 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 256e3b4 | 2022-10-04 18:24:58 +0000 | [diff] [blame] | 760 | var _ BazelHandler = (*prebuiltBinaryBazelHandler)(nil) |
| 761 | |
| 762 | func (h *prebuiltBinaryBazelHandler) QueueBazelCall(ctx android.BaseModuleContext, label string) { |
| 763 | bazelCtx := ctx.Config().BazelContext |
| 764 | bazelCtx.QueueBazelRequest(label, cquery.GetOutputFiles, android.GetConfigKey(ctx)) |
| 765 | } |
| 766 | |
| 767 | func (h *prebuiltBinaryBazelHandler) ProcessBazelQueryResponse(ctx android.ModuleContext, label string) { |
| 768 | bazelCtx := ctx.Config().BazelContext |
| 769 | outputs, err := bazelCtx.GetOutputFiles(label, android.GetConfigKey(ctx)) |
| 770 | if err != nil { |
| 771 | ctx.ModuleErrorf(err.Error()) |
| 772 | return |
| 773 | } |
| 774 | if len(outputs) != 1 { |
| 775 | ctx.ModuleErrorf("Expected a single output for `%s`, but got:\n%v", label, outputs) |
| 776 | return |
| 777 | } |
| 778 | out := android.PathForBazelOut(ctx, outputs[0]) |
| 779 | h.module.outputFile = android.OptionalPathForPath(out) |
| 780 | h.module.maybeUnhideFromMake() |
| 781 | } |
| 782 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | b12ff59 | 2022-09-01 15:04:04 +0000 | [diff] [blame] | 783 | type bazelPrebuiltBinaryAttributes struct { |
| 784 | Src bazel.LabelAttribute |
| 785 | Strip stripAttributes |
| 786 | } |
| 787 | |
| 788 | func prebuiltBinaryBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 789 | prebuiltAttrs := bp2BuildParsePrebuiltBinaryProps(ctx, module) |
| 790 | |
| 791 | var la linkerAttributes |
| 792 | la.convertStripProps(ctx, module) |
| 793 | attrs := &bazelPrebuiltBinaryAttributes{ |
| 794 | Src: prebuiltAttrs.Src, |
| 795 | Strip: stripAttrsFromLinkerAttrs(&la), |
| 796 | } |
| 797 | |
| 798 | props := bazel.BazelTargetModuleProperties{ |
| 799 | Rule_class: "cc_prebuilt_binary", |
| 800 | Bzl_load_location: "//build/bazel/rules/cc:cc_prebuilt_binary.bzl", |
| 801 | } |
| 802 | |
| 803 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
Jingwen Chen | c4c34e1 | 2022-11-29 12:07:45 +0000 | [diff] [blame] | 804 | tags := android.ApexAvailableTags(module) |
| 805 | 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] | 806 | } |
| 807 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 808 | type Sanitized struct { |
| 809 | None struct { |
| 810 | Srcs []string `android:"path,arch_variant"` |
| 811 | } `android:"arch_variant"` |
| 812 | Address struct { |
| 813 | Srcs []string `android:"path,arch_variant"` |
| 814 | } `android:"arch_variant"` |
| 815 | Hwaddress struct { |
| 816 | Srcs []string `android:"path,arch_variant"` |
| 817 | } `android:"arch_variant"` |
| 818 | } |
| 819 | |
| 820 | func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string { |
| 821 | if sanitize == nil { |
| 822 | return nil |
| 823 | } |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 824 | if sanitize.isSanitizerEnabled(Asan) && sanitized.Address.Srcs != nil { |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 825 | return sanitized.Address.Srcs |
| 826 | } |
Liz Kammer | 2c1d6aa | 2022-10-03 15:07:37 -0400 | [diff] [blame] | 827 | if sanitize.isSanitizerEnabled(Hwasan) && sanitized.Hwaddress.Srcs != nil { |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 828 | return sanitized.Hwaddress.Srcs |
| 829 | } |
| 830 | return sanitized.None.Srcs |
| 831 | } |