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