Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 1 | // Copyright 2016 Google Inc. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package cc |
| 16 | |
| 17 | import ( |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 18 | "path/filepath" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 19 | |
| 20 | "android/soong/android" |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 21 | "android/soong/bazel" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 22 | ) |
| 23 | |
| 24 | func init() { |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 25 | RegisterPrebuiltBuildComponents(android.InitRegistrationContext) |
| 26 | } |
| 27 | |
| 28 | func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 29 | ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 30 | ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory) |
| 31 | ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory) |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 32 | ctx.RegisterModuleType("cc_prebuilt_test_library_shared", PrebuiltSharedTestLibraryFactory) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 33 | ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 34 | ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | type prebuiltLinkerInterface interface { |
| 38 | Name(string) string |
| 39 | prebuilt() *android.Prebuilt |
| 40 | } |
| 41 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 42 | type prebuiltLinkerProperties struct { |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 43 | // a prebuilt library or binary. Can reference a genrule module that generates an executable file. |
| 44 | Srcs []string `android:"path,arch_variant"` |
| 45 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 46 | Sanitized Sanitized `android:"arch_variant"` |
| 47 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 48 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined |
| 49 | // symbols, etc), default true. |
| 50 | Check_elf_files *bool |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 51 | |
| 52 | // Optionally provide an import library if this is a Windows PE DLL prebuilt. |
| 53 | // This is needed only if this library is linked by other modules in build time. |
| 54 | // Only makes sense for the Windows target. |
| 55 | Windows_import_lib *string `android:"path,arch_variant"` |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 58 | type prebuiltLinker struct { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 59 | android.Prebuilt |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 60 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 61 | properties prebuiltLinkerProperties |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 64 | func (p *prebuiltLinker) prebuilt() *android.Prebuilt { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 65 | return &p.Prebuilt |
| 66 | } |
| 67 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 68 | func (p *prebuiltLinker) PrebuiltSrcs() []string { |
| 69 | return p.properties.Srcs |
| 70 | } |
| 71 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 72 | type prebuiltLibraryInterface interface { |
| 73 | libraryInterface |
| 74 | prebuiltLinkerInterface |
| 75 | disablePrebuilt() |
| 76 | } |
| 77 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 78 | type prebuiltLibraryLinker struct { |
| 79 | *libraryDecorator |
| 80 | prebuiltLinker |
| 81 | } |
| 82 | |
| 83 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 84 | var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 85 | |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 86 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} |
| 87 | |
| 88 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 89 | return p.libraryDecorator.linkerDeps(ctx, deps) |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 93 | return flags |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { |
| 97 | return p.libraryDecorator.linkerProps() |
| 98 | } |
| 99 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 100 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 101 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 102 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 103 | p.libraryDecorator.flagExporter.exportIncludes(ctx) |
| 104 | p.libraryDecorator.flagExporter.reexportDirs(deps.ReexportedDirs...) |
| 105 | p.libraryDecorator.flagExporter.reexportSystemDirs(deps.ReexportedSystemDirs...) |
| 106 | p.libraryDecorator.flagExporter.reexportFlags(deps.ReexportedFlags...) |
| 107 | p.libraryDecorator.flagExporter.reexportDeps(deps.ReexportedDeps...) |
| 108 | p.libraryDecorator.flagExporter.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) |
| 109 | |
| 110 | p.libraryDecorator.flagExporter.setProvider(ctx) |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 111 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 112 | // TODO(ccross): verify shared library dependencies |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 113 | srcs := p.prebuiltSrcs(ctx) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 114 | if len(srcs) > 0 { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 115 | if len(srcs) > 1 { |
| 116 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 117 | return nil |
| 118 | } |
| 119 | |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 120 | p.libraryDecorator.exportVersioningMacroIfNeeded(ctx) |
| 121 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 122 | in := android.PathForModuleSrc(ctx, srcs[0]) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 123 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 124 | if p.static() { |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 125 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(in).Build() |
| 126 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 127 | StaticLibrary: in, |
| 128 | |
| 129 | TransitiveStaticLibrariesForOrdering: depSet, |
| 130 | }) |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 131 | return in |
| 132 | } |
| 133 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 134 | if p.shared() { |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 135 | p.unstrippedOutputFile = in |
Colin Cross | 0fd6a41 | 2019-08-16 14:22:10 -0700 | [diff] [blame] | 136 | libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 137 | outputFile := android.PathForModuleOut(ctx, libName) |
| 138 | var implicits android.Paths |
| 139 | |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 140 | if p.stripper.NeedsStrip(ctx) { |
| 141 | stripFlags := flagsToStripFlags(flags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 142 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
Thiébaud Weksteen | d458745 | 2020-08-19 14:53:01 +0200 | [diff] [blame] | 143 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, stripFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 144 | in = stripped |
| 145 | } |
| 146 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 147 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 148 | // depending on a table of contents file instead of the library itself. |
| 149 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 150 | p.tocFile = android.OptionalPathForPath(tocFile) |
Ivan Lozano | 7b0781d | 2021-11-03 15:30:18 -0400 | [diff] [blame] | 151 | TransformSharedObjectToToc(ctx, outputFile, tocFile) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 152 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 153 | if ctx.Windows() && p.properties.Windows_import_lib != nil { |
| 154 | // Consumers of this library actually links to the import library in build |
| 155 | // time and dynamically links to the DLL in run time. i.e. |
| 156 | // a.exe <-- static link --> foo.lib <-- dynamic link --> foo.dll |
| 157 | importLibSrc := android.PathForModuleSrc(ctx, String(p.properties.Windows_import_lib)) |
| 158 | importLibName := p.libraryDecorator.getLibName(ctx) + ".lib" |
| 159 | importLibOutputFile := android.PathForModuleOut(ctx, importLibName) |
| 160 | implicits = append(implicits, importLibOutputFile) |
| 161 | |
| 162 | ctx.Build(pctx, android.BuildParams{ |
| 163 | Rule: android.Cp, |
| 164 | Description: "prebuilt import library", |
| 165 | Input: importLibSrc, |
| 166 | Output: importLibOutputFile, |
| 167 | Args: map[string]string{ |
| 168 | "cpFlags": "-L", |
| 169 | }, |
| 170 | }) |
| 171 | } |
| 172 | |
| 173 | ctx.Build(pctx, android.BuildParams{ |
| 174 | Rule: android.Cp, |
| 175 | Description: "prebuilt shared library", |
| 176 | Implicits: implicits, |
| 177 | Input: in, |
| 178 | Output: outputFile, |
| 179 | Args: map[string]string{ |
| 180 | "cpFlags": "-L", |
| 181 | }, |
| 182 | }) |
| 183 | |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 184 | ctx.SetProvider(SharedLibraryInfoProvider, SharedLibraryInfo{ |
Liz Kammer | ef6dfea | 2021-06-08 15:37:09 -0400 | [diff] [blame] | 185 | SharedLibrary: outputFile, |
| 186 | Target: ctx.Target(), |
Colin Cross | 0de8a1e | 2020-09-18 14:15:30 -0700 | [diff] [blame] | 187 | |
| 188 | TableOfContents: p.tocFile, |
| 189 | }) |
| 190 | |
Yo Chiang | a3ad9b2 | 2020-03-18 14:19:07 +0800 | [diff] [blame] | 191 | return outputFile |
| 192 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 195 | if p.header() { |
| 196 | ctx.SetProvider(HeaderLibraryInfoProvider, HeaderLibraryInfo{}) |
| 197 | |
Martin Stjernholm | d51cb5c | 2021-11-30 18:49:03 +0000 | [diff] [blame] | 198 | // Need to return an output path so that the AndroidMk logic doesn't skip |
| 199 | // the prebuilt header. For compatibility, in case Android.mk files use a |
| 200 | // header lib in LOCAL_STATIC_LIBRARIES, create an empty ar file as |
| 201 | // placeholder, just like non-prebuilt header modules do in linkStatic(). |
| 202 | ph := android.PathForModuleOut(ctx, ctx.ModuleName()+staticLibraryExtension) |
| 203 | transformObjToStaticLib(ctx, nil, nil, builderFlags{}, ph, nil, nil) |
| 204 | return ph |
Colin Cross | 649d817 | 2020-12-10 12:30:21 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 207 | return nil |
| 208 | } |
| 209 | |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 210 | func (p *prebuiltLibraryLinker) prebuiltSrcs(ctx android.BaseModuleContext) []string { |
| 211 | sanitize := ctx.Module().(*Module).sanitize |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 212 | srcs := p.properties.Srcs |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 213 | srcs = append(srcs, srcsForSanitizer(sanitize, p.properties.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 214 | if p.static() { |
| 215 | srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 216 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.StaticProperties.Static.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 217 | } |
| 218 | if p.shared() { |
| 219 | srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...) |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 220 | srcs = append(srcs, srcsForSanitizer(sanitize, p.libraryDecorator.SharedProperties.Shared.Sanitized)...) |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 221 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 222 | return srcs |
| 223 | } |
| 224 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 225 | func (p *prebuiltLibraryLinker) shared() bool { |
| 226 | return p.libraryDecorator.shared() |
| 227 | } |
| 228 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 229 | func (p *prebuiltLibraryLinker) nativeCoverage() bool { |
| 230 | return false |
| 231 | } |
| 232 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 233 | func (p *prebuiltLibraryLinker) disablePrebuilt() { |
| 234 | p.properties.Srcs = nil |
| 235 | } |
| 236 | |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 237 | // Implements versionedInterface |
| 238 | func (p *prebuiltLibraryLinker) implementationModuleName(name string) string { |
Paul Duffin | 9804da0 | 2021-10-26 10:42:42 +0100 | [diff] [blame] | 239 | return android.RemoveOptionalPrebuiltPrefix(name) |
Jiyong Park | 892a98f | 2020-12-14 09:20:00 +0900 | [diff] [blame] | 240 | } |
| 241 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 242 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported, srcsProperty string) (*Module, *libraryDecorator) { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 243 | module, library := NewLibrary(hod) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 244 | module.compiler = nil |
| 245 | |
| 246 | prebuilt := &prebuiltLibraryLinker{ |
| 247 | libraryDecorator: library, |
| 248 | } |
| 249 | module.linker = prebuilt |
Colin Cross | 31076b3 | 2020-10-23 17:22:06 -0700 | [diff] [blame] | 250 | module.library = prebuilt |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 251 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 252 | module.AddProperties(&prebuilt.properties) |
| 253 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 254 | if srcsProperty == "" { |
| 255 | android.InitPrebuiltModuleWithoutSrcs(module) |
| 256 | } else { |
| 257 | srcsSupplier := func(ctx android.BaseModuleContext, _ android.Module) []string { |
| 258 | return prebuilt.prebuiltSrcs(ctx) |
| 259 | } |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 260 | |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 261 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, srcsProperty) |
| 262 | } |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 263 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 264 | // Prebuilt libraries can be used in SDKs. |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 265 | android.InitSdkAwareModule(module) |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 266 | return module, library |
| 267 | } |
| 268 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 269 | // cc_prebuilt_library installs a precompiled shared library that are |
| 270 | // listed in the srcs property in the device's directory. |
| 271 | func PrebuiltLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 272 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 273 | |
| 274 | // Prebuilt shared libraries can be included in APEXes |
| 275 | android.InitApexModule(module) |
| 276 | |
| 277 | return module.Init() |
| 278 | } |
| 279 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 280 | // cc_prebuilt_library_shared installs a precompiled shared library that are |
| 281 | // listed in the srcs property in the device's directory. |
| 282 | func PrebuiltSharedLibraryFactory() android.Module { |
| 283 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 284 | return module.Init() |
| 285 | } |
| 286 | |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 287 | // cc_prebuilt_test_library_shared installs a precompiled shared library |
| 288 | // to be used as a data dependency of a test-related module (such as cc_test, or |
| 289 | // cc_test_library). |
| 290 | func PrebuiltSharedTestLibraryFactory() android.Module { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 291 | module, library := NewPrebuiltLibrary(android.HostAndDeviceSupported, "srcs") |
Chris Parsons | 1f6d90f | 2020-06-17 16:10:42 -0400 | [diff] [blame] | 292 | library.BuildOnlyShared() |
| 293 | library.baseInstaller = NewTestInstaller() |
| 294 | return module.Init() |
| 295 | } |
| 296 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 297 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 298 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 299 | library.BuildOnlyShared() |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 300 | module.bazelable = true |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 301 | |
| 302 | // Prebuilt shared libraries can be included in APEXes |
| 303 | android.InitApexModule(module) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 304 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 305 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 308 | // cc_prebuilt_library_static installs a precompiled static library that are |
| 309 | // listed in the srcs property in the device's directory. |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 310 | func PrebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 311 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 312 | return module.Init() |
| 313 | } |
| 314 | |
| 315 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Martin Stjernholm | e65c3ae | 2021-11-23 01:24:06 +0000 | [diff] [blame] | 316 | module, library := NewPrebuiltLibrary(hod, "srcs") |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 317 | library.BuildOnlyStatic() |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 318 | module.bazelable = true |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 319 | module.bazelHandler = &prebuiltStaticLibraryBazelHandler{module: module, library: library} |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 320 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 323 | type bazelPrebuiltLibraryStaticAttributes struct { |
| 324 | Static_library bazel.LabelAttribute |
| 325 | Export_includes bazel.StringListAttribute |
| 326 | Export_system_includes bazel.StringListAttribute |
| 327 | } |
| 328 | |
| 329 | func prebuiltLibraryStaticBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
| 330 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module) |
| 331 | exportedIncludes := Bp2BuildParseExportedIncludesForPrebuiltLibrary(ctx, module) |
| 332 | |
| 333 | attrs := &bazelPrebuiltLibraryStaticAttributes{ |
| 334 | Static_library: prebuiltAttrs.Src, |
| 335 | Export_includes: exportedIncludes.Includes, |
| 336 | Export_system_includes: exportedIncludes.SystemIncludes, |
| 337 | } |
| 338 | |
| 339 | props := bazel.BazelTargetModuleProperties{ |
| 340 | Rule_class: "prebuilt_library_static", |
| 341 | Bzl_load_location: "//build/bazel/rules:prebuilt_library_static.bzl", |
| 342 | } |
| 343 | |
| 344 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 345 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) |
| 346 | } |
| 347 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 348 | type bazelPrebuiltLibrarySharedAttributes struct { |
| 349 | Shared_library bazel.LabelAttribute |
| 350 | } |
| 351 | |
Liz Kammer | be46fcc | 2021-11-01 15:32:43 -0400 | [diff] [blame^] | 352 | func prebuiltLibrarySharedBp2Build(ctx android.TopDownMutatorContext, module *Module) { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 7fa0696 | 2021-10-25 10:28:33 -0400 | [diff] [blame] | 353 | prebuiltAttrs := Bp2BuildParsePrebuiltLibraryProps(ctx, module) |
| 354 | |
| 355 | attrs := &bazelPrebuiltLibrarySharedAttributes{ |
| 356 | Shared_library: prebuiltAttrs.Src, |
| 357 | } |
| 358 | |
| 359 | props := bazel.BazelTargetModuleProperties{ |
| 360 | Rule_class: "prebuilt_library_shared", |
| 361 | Bzl_load_location: "//build/bazel/rules:prebuilt_library_shared.bzl", |
| 362 | } |
| 363 | |
| 364 | name := android.RemoveOptionalPrebuiltPrefix(module.Name()) |
| 365 | ctx.CreateBazelTargetModule(props, android.CommonAttributes{Name: name}, attrs) |
| 366 | } |
| 367 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 368 | type prebuiltObjectProperties struct { |
| 369 | Srcs []string `android:"path,arch_variant"` |
| 370 | } |
| 371 | |
| 372 | type prebuiltObjectLinker struct { |
| 373 | android.Prebuilt |
| 374 | objectLinker |
| 375 | |
| 376 | properties prebuiltObjectProperties |
| 377 | } |
| 378 | |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 379 | type prebuiltStaticLibraryBazelHandler struct { |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 380 | android.BazelHandler |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 381 | |
| 382 | module *Module |
| 383 | library *libraryDecorator |
| 384 | } |
| 385 | |
Alex Márquez Pérez Muñíz Díaz Púras Thaureaux | 0d99045 | 2021-08-11 16:46:13 +0000 | [diff] [blame] | 386 | func (h *prebuiltStaticLibraryBazelHandler) GenerateBazelBuildActions(ctx android.ModuleContext, label string) bool { |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 387 | bazelCtx := ctx.Config().BazelContext |
Chris Parsons | 787fb36 | 2021-10-14 18:43:51 -0400 | [diff] [blame] | 388 | ccInfo, ok, err := bazelCtx.GetCcInfo(label, android.GetConfigKey(ctx)) |
Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 389 | if err != nil { |
| 390 | ctx.ModuleErrorf("Error getting Bazel CcInfo: %s", err) |
| 391 | } |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 392 | if !ok { |
| 393 | return false |
| 394 | } |
Liz Kammer | fe23bf3 | 2021-04-09 16:17:05 -0400 | [diff] [blame] | 395 | staticLibs := ccInfo.CcStaticLibraryFiles |
Liz Kammer | 3f9e155 | 2021-04-02 18:47:09 -0400 | [diff] [blame] | 396 | if len(staticLibs) > 1 { |
| 397 | ctx.ModuleErrorf("expected 1 static library from bazel target %q, got %s", label, staticLibs) |
| 398 | return false |
| 399 | } |
| 400 | |
| 401 | // TODO(b/184543518): cc_prebuilt_library_static may have properties for re-exporting flags |
| 402 | |
| 403 | // TODO(eakammer):Add stub-related flags if this library is a stub library. |
| 404 | // h.library.exportVersioningMacroIfNeeded(ctx) |
| 405 | |
| 406 | // Dependencies on this library will expect collectedSnapshotHeaders to be set, otherwise |
| 407 | // validation will fail. For now, set this to an empty list. |
| 408 | // TODO(cparsons): More closely mirror the collectHeadersForSnapshot implementation. |
| 409 | h.library.collectedSnapshotHeaders = android.Paths{} |
| 410 | |
| 411 | if len(staticLibs) == 0 { |
| 412 | h.module.outputFile = android.OptionalPath{} |
| 413 | return true |
| 414 | } |
| 415 | |
| 416 | out := android.PathForBazelOut(ctx, staticLibs[0]) |
| 417 | h.module.outputFile = android.OptionalPathForPath(out) |
| 418 | |
| 419 | depSet := android.NewDepSetBuilder(android.TOPOLOGICAL).Direct(out).Build() |
| 420 | ctx.SetProvider(StaticLibraryInfoProvider, StaticLibraryInfo{ |
| 421 | StaticLibrary: out, |
| 422 | |
| 423 | TransitiveStaticLibrariesForOrdering: depSet, |
| 424 | }) |
| 425 | |
| 426 | return true |
| 427 | } |
| 428 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 429 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 430 | return &p.Prebuilt |
| 431 | } |
| 432 | |
| 433 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 434 | |
| 435 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 436 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 437 | if len(p.properties.Srcs) > 0 { |
| 438 | return p.Prebuilt.SingleSourcePath(ctx) |
| 439 | } |
| 440 | return nil |
| 441 | } |
| 442 | |
Inseob Kim | 1042d29 | 2020-06-01 23:23:05 +0900 | [diff] [blame] | 443 | func (p *prebuiltObjectLinker) object() bool { |
| 444 | return true |
| 445 | } |
| 446 | |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 447 | func NewPrebuiltObject(hod android.HostOrDeviceSupported) *Module { |
| 448 | module := newObject(hod) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 449 | prebuilt := &prebuiltObjectLinker{ |
| 450 | objectLinker: objectLinker{ |
| 451 | baseLinker: NewBaseLinker(nil), |
| 452 | }, |
| 453 | } |
| 454 | module.linker = prebuilt |
| 455 | module.AddProperties(&prebuilt.properties) |
| 456 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
| 457 | android.InitSdkAwareModule(module) |
| 458 | return module |
| 459 | } |
| 460 | |
| 461 | func prebuiltObjectFactory() android.Module { |
Colin Cross | 7cabd42 | 2021-06-25 14:21:04 -0700 | [diff] [blame] | 462 | module := NewPrebuiltObject(android.HostAndDeviceSupported) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 463 | return module.Init() |
| 464 | } |
| 465 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 466 | type prebuiltBinaryLinker struct { |
| 467 | *binaryDecorator |
| 468 | prebuiltLinker |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 469 | |
| 470 | toolPath android.OptionalPath |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 474 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 475 | func (p *prebuiltBinaryLinker) hostToolPath() android.OptionalPath { |
| 476 | return p.toolPath |
| 477 | } |
| 478 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 479 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 480 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 481 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 482 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 483 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 484 | in := p.Prebuilt.SingleSourcePath(ctx) |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 485 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 486 | p.unstrippedOutputFile = in |
| 487 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 488 | if ctx.Host() { |
| 489 | // Host binaries are symlinked to their prebuilt source locations. That |
| 490 | // way they are executed directly from there so the linker resolves their |
| 491 | // shared library dependencies relative to that location (using |
| 492 | // $ORIGIN/../lib(64):$ORIGIN/lib(64) as RUNPATH). This way the prebuilt |
| 493 | // repository can supply the expected versions of the shared libraries |
| 494 | // without interference from what is in the out tree. |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 495 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 496 | // These shared lib paths may point to copies of the libs in |
| 497 | // .intermediates, which isn't where the binary will load them from, but |
| 498 | // it's fine for dependency tracking. If a library dependency is updated, |
| 499 | // the symlink will get a new timestamp, along with any installed symlinks |
| 500 | // handled in make. |
| 501 | sharedLibPaths := deps.EarlySharedLibs |
| 502 | sharedLibPaths = append(sharedLibPaths, deps.SharedLibs...) |
| 503 | sharedLibPaths = append(sharedLibPaths, deps.LateSharedLibs...) |
| 504 | |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 505 | var fromPath = in.String() |
| 506 | if !filepath.IsAbs(fromPath) { |
| 507 | fromPath = "$$PWD/" + fromPath |
| 508 | } |
| 509 | |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 510 | ctx.Build(pctx, android.BuildParams{ |
| 511 | Rule: android.Symlink, |
| 512 | Output: outputFile, |
| 513 | Input: in, |
| 514 | Implicits: sharedLibPaths, |
| 515 | Args: map[string]string{ |
Martin Stjernholm | 14ee832 | 2020-09-21 21:45:49 +0100 | [diff] [blame] | 516 | "fromPath": fromPath, |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 517 | }, |
| 518 | }) |
| 519 | |
| 520 | p.toolPath = android.OptionalPathForPath(outputFile) |
| 521 | } else { |
| 522 | if p.stripper.NeedsStrip(ctx) { |
| 523 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
| 524 | p.stripper.StripExecutableOrSharedLib(ctx, in, stripped, flagsToStripFlags(flags)) |
| 525 | in = stripped |
| 526 | } |
| 527 | |
| 528 | // Copy binaries to a name matching the final installed name |
| 529 | ctx.Build(pctx, android.BuildParams{ |
| 530 | Rule: android.CpExecutable, |
| 531 | Description: "prebuilt", |
| 532 | Output: outputFile, |
| 533 | Input: in, |
| 534 | }) |
| 535 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 536 | |
| 537 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | return nil |
| 541 | } |
| 542 | |
Inseob Kim | 7f283f4 | 2020-06-01 21:53:49 +0900 | [diff] [blame] | 543 | func (p *prebuiltBinaryLinker) binary() bool { |
| 544 | return true |
| 545 | } |
| 546 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 547 | // cc_prebuilt_binary installs a precompiled executable in srcs property in the |
| 548 | // device's directory. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 549 | func prebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 550 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 551 | return module.Init() |
| 552 | } |
| 553 | |
| 554 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 555 | module, binary := NewBinary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 556 | module.compiler = nil |
| 557 | |
| 558 | prebuilt := &prebuiltBinaryLinker{ |
| 559 | binaryDecorator: binary, |
| 560 | } |
| 561 | module.linker = prebuilt |
Martin Stjernholm | 837ee1a | 2020-08-20 02:54:52 +0100 | [diff] [blame] | 562 | module.installer = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 563 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 564 | module.AddProperties(&prebuilt.properties) |
| 565 | |
| 566 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 567 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 568 | } |
Evgenii Stepanov | 2080bfe | 2020-07-24 15:35:40 -0700 | [diff] [blame] | 569 | |
| 570 | type Sanitized struct { |
| 571 | None struct { |
| 572 | Srcs []string `android:"path,arch_variant"` |
| 573 | } `android:"arch_variant"` |
| 574 | Address struct { |
| 575 | Srcs []string `android:"path,arch_variant"` |
| 576 | } `android:"arch_variant"` |
| 577 | Hwaddress struct { |
| 578 | Srcs []string `android:"path,arch_variant"` |
| 579 | } `android:"arch_variant"` |
| 580 | } |
| 581 | |
| 582 | func srcsForSanitizer(sanitize *sanitize, sanitized Sanitized) []string { |
| 583 | if sanitize == nil { |
| 584 | return nil |
| 585 | } |
| 586 | if Bool(sanitize.Properties.Sanitize.Address) && sanitized.Address.Srcs != nil { |
| 587 | return sanitized.Address.Srcs |
| 588 | } |
| 589 | if Bool(sanitize.Properties.Sanitize.Hwaddress) && sanitized.Hwaddress.Srcs != nil { |
| 590 | return sanitized.Hwaddress.Srcs |
| 591 | } |
| 592 | return sanitized.None.Srcs |
| 593 | } |