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 ( |
| 18 | "android/soong/android" |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 19 | ) |
| 20 | |
| 21 | func init() { |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 22 | RegisterPrebuiltBuildComponents(android.InitRegistrationContext) |
| 23 | } |
| 24 | |
| 25 | func RegisterPrebuiltBuildComponents(ctx android.RegistrationContext) { |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 26 | ctx.RegisterModuleType("cc_prebuilt_library", PrebuiltLibraryFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 27 | ctx.RegisterModuleType("cc_prebuilt_library_shared", PrebuiltSharedLibraryFactory) |
| 28 | ctx.RegisterModuleType("cc_prebuilt_library_static", PrebuiltStaticLibraryFactory) |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 29 | ctx.RegisterModuleType("cc_prebuilt_object", prebuiltObjectFactory) |
Paul Duffin | 59986b2 | 2019-12-19 14:38:36 +0000 | [diff] [blame] | 30 | ctx.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | type prebuiltLinkerInterface interface { |
| 34 | Name(string) string |
| 35 | prebuilt() *android.Prebuilt |
| 36 | } |
| 37 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 38 | type prebuiltLinkerProperties struct { |
| 39 | |
| 40 | // a prebuilt library or binary. Can reference a genrule module that generates an executable file. |
| 41 | Srcs []string `android:"path,arch_variant"` |
| 42 | |
| 43 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined |
| 44 | // symbols, etc), default true. |
| 45 | Check_elf_files *bool |
| 46 | } |
| 47 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 48 | type prebuiltLinker struct { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 49 | android.Prebuilt |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 50 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 51 | properties prebuiltLinkerProperties |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 54 | func (p *prebuiltLinker) prebuilt() *android.Prebuilt { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 55 | return &p.Prebuilt |
| 56 | } |
| 57 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 58 | func (p *prebuiltLinker) PrebuiltSrcs() []string { |
| 59 | return p.properties.Srcs |
| 60 | } |
| 61 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 62 | type prebuiltLibraryInterface interface { |
| 63 | libraryInterface |
| 64 | prebuiltLinkerInterface |
| 65 | disablePrebuilt() |
| 66 | } |
| 67 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 68 | type prebuiltLibraryLinker struct { |
| 69 | *libraryDecorator |
| 70 | prebuiltLinker |
| 71 | } |
| 72 | |
| 73 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 74 | var _ prebuiltLibraryInterface = (*prebuiltLibraryLinker)(nil) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 75 | |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 76 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} |
| 77 | |
| 78 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 79 | return p.libraryDecorator.linkerDeps(ctx, deps) |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 83 | return flags |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { |
| 87 | return p.libraryDecorator.linkerProps() |
| 88 | } |
| 89 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 90 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 91 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Paul Duffin | f5ea9e1 | 2020-02-21 10:57:00 +0000 | [diff] [blame] | 92 | |
| 93 | p.libraryDecorator.exportIncludes(ctx) |
| 94 | p.libraryDecorator.reexportDirs(deps.ReexportedDirs...) |
| 95 | p.libraryDecorator.reexportSystemDirs(deps.ReexportedSystemDirs...) |
| 96 | p.libraryDecorator.reexportFlags(deps.ReexportedFlags...) |
| 97 | p.libraryDecorator.reexportDeps(deps.ReexportedDeps...) |
| 98 | p.libraryDecorator.addExportedGeneratedHeaders(deps.ReexportedGeneratedHeaders...) |
| 99 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 100 | // TODO(ccross): verify shared library dependencies |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 101 | srcs := p.prebuiltSrcs() |
| 102 | if len(srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 103 | builderFlags := flagsToBuilderFlags(flags) |
| 104 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 105 | if len(srcs) > 1 { |
| 106 | ctx.PropertyErrorf("srcs", "multiple prebuilt source files") |
| 107 | return nil |
| 108 | } |
| 109 | |
| 110 | in := android.PathForModuleSrc(ctx, srcs[0]) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 111 | |
| 112 | if p.shared() { |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 113 | p.unstrippedOutputFile = in |
Colin Cross | 0fd6a41 | 2019-08-16 14:22:10 -0700 | [diff] [blame] | 114 | libName := p.libraryDecorator.getLibName(ctx) + flags.Toolchain.ShlibSuffix() |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 115 | if p.needsStrip(ctx) { |
| 116 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 117 | p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 118 | in = stripped |
| 119 | } |
| 120 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 121 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 122 | // depending on a table of contents file instead of the library itself. |
| 123 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 124 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 125 | TransformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | return in |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | return nil |
| 132 | } |
| 133 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 134 | func (p *prebuiltLibraryLinker) prebuiltSrcs() []string { |
| 135 | srcs := p.properties.Srcs |
| 136 | if p.static() { |
| 137 | srcs = append(srcs, p.libraryDecorator.StaticProperties.Static.Srcs...) |
| 138 | } |
| 139 | if p.shared() { |
| 140 | srcs = append(srcs, p.libraryDecorator.SharedProperties.Shared.Srcs...) |
| 141 | } |
| 142 | |
| 143 | return srcs |
| 144 | } |
| 145 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 146 | func (p *prebuiltLibraryLinker) shared() bool { |
| 147 | return p.libraryDecorator.shared() |
| 148 | } |
| 149 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 150 | func (p *prebuiltLibraryLinker) nativeCoverage() bool { |
| 151 | return false |
| 152 | } |
| 153 | |
Colin Cross | 33b2fb7 | 2019-05-14 14:07:01 -0700 | [diff] [blame] | 154 | func (p *prebuiltLibraryLinker) disablePrebuilt() { |
| 155 | p.properties.Srcs = nil |
| 156 | } |
| 157 | |
Martin Stjernholm | bf37d16 | 2020-03-31 16:05:34 +0100 | [diff] [blame^] | 158 | func (p *prebuiltLibraryLinker) skipInstall(mod *Module) { |
| 159 | mod.ModuleBase.SkipInstall() |
| 160 | } |
| 161 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 162 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 163 | module, library := NewLibrary(hod) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 164 | module.compiler = nil |
| 165 | |
| 166 | prebuilt := &prebuiltLibraryLinker{ |
| 167 | libraryDecorator: library, |
| 168 | } |
| 169 | module.linker = prebuilt |
Martin Stjernholm | bf37d16 | 2020-03-31 16:05:34 +0100 | [diff] [blame^] | 170 | module.installer = prebuilt |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 171 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 172 | module.AddProperties(&prebuilt.properties) |
| 173 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 174 | srcsSupplier := func() []string { |
| 175 | return prebuilt.prebuiltSrcs() |
| 176 | } |
| 177 | |
| 178 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 179 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 180 | // Prebuilt libraries can be used in SDKs. |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 181 | android.InitSdkAwareModule(module) |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 182 | return module, library |
| 183 | } |
| 184 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 185 | // cc_prebuilt_library installs a precompiled shared library that are |
| 186 | // listed in the srcs property in the device's directory. |
| 187 | func PrebuiltLibraryFactory() android.Module { |
| 188 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 189 | |
| 190 | // Prebuilt shared libraries can be included in APEXes |
| 191 | android.InitApexModule(module) |
| 192 | |
| 193 | return module.Init() |
| 194 | } |
| 195 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 196 | // cc_prebuilt_library_shared installs a precompiled shared library that are |
| 197 | // listed in the srcs property in the device's directory. |
| 198 | func PrebuiltSharedLibraryFactory() android.Module { |
| 199 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 200 | return module.Init() |
| 201 | } |
| 202 | |
| 203 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
| 204 | module, library := NewPrebuiltLibrary(hod) |
| 205 | library.BuildOnlyShared() |
| 206 | |
| 207 | // Prebuilt shared libraries can be included in APEXes |
| 208 | android.InitApexModule(module) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 209 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 210 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 213 | // cc_prebuilt_library_static installs a precompiled static library that are |
| 214 | // listed in the srcs property in the device's directory. |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 215 | func PrebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 216 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 217 | return module.Init() |
| 218 | } |
| 219 | |
| 220 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 221 | module, library := NewPrebuiltLibrary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 222 | library.BuildOnlyStatic() |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 223 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 226 | type prebuiltObjectProperties struct { |
| 227 | Srcs []string `android:"path,arch_variant"` |
| 228 | } |
| 229 | |
| 230 | type prebuiltObjectLinker struct { |
| 231 | android.Prebuilt |
| 232 | objectLinker |
| 233 | |
| 234 | properties prebuiltObjectProperties |
| 235 | } |
| 236 | |
| 237 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 238 | return &p.Prebuilt |
| 239 | } |
| 240 | |
| 241 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 242 | |
| 243 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 244 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 245 | if len(p.properties.Srcs) > 0 { |
| 246 | return p.Prebuilt.SingleSourcePath(ctx) |
| 247 | } |
| 248 | return nil |
| 249 | } |
| 250 | |
| 251 | func newPrebuiltObject() *Module { |
| 252 | module := newObject() |
| 253 | prebuilt := &prebuiltObjectLinker{ |
| 254 | objectLinker: objectLinker{ |
| 255 | baseLinker: NewBaseLinker(nil), |
| 256 | }, |
| 257 | } |
| 258 | module.linker = prebuilt |
| 259 | module.AddProperties(&prebuilt.properties) |
| 260 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
| 261 | android.InitSdkAwareModule(module) |
| 262 | return module |
| 263 | } |
| 264 | |
| 265 | func prebuiltObjectFactory() android.Module { |
| 266 | module := newPrebuiltObject() |
| 267 | return module.Init() |
| 268 | } |
| 269 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 270 | type prebuiltBinaryLinker struct { |
| 271 | *binaryDecorator |
| 272 | prebuiltLinker |
| 273 | } |
| 274 | |
| 275 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 276 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 277 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 278 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 279 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 280 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 281 | builderFlags := flagsToBuilderFlags(flags) |
| 282 | |
| 283 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 284 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 285 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 286 | p.unstrippedOutputFile = in |
| 287 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 288 | if p.needsStrip(ctx) { |
| 289 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 290 | p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 291 | in = stripped |
| 292 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 293 | |
| 294 | // Copy binaries to a name matching the final installed name |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 295 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 296 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 297 | Rule: android.CpExecutable, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 298 | Description: "prebuilt", |
| 299 | Output: outputFile, |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 300 | Input: in, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 301 | }) |
| 302 | |
| 303 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | return nil |
| 307 | } |
| 308 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 309 | // cc_prebuilt_binary installs a precompiled executable in srcs property in the |
| 310 | // device's directory. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 311 | func prebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 312 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 313 | return module.Init() |
| 314 | } |
| 315 | |
| 316 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 317 | module, binary := NewBinary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 318 | module.compiler = nil |
| 319 | |
| 320 | prebuilt := &prebuiltBinaryLinker{ |
| 321 | binaryDecorator: binary, |
| 322 | } |
| 323 | module.linker = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 324 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 325 | module.AddProperties(&prebuilt.properties) |
| 326 | |
| 327 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 328 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 329 | } |