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