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 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 158 | func NewPrebuiltLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 159 | module, library := NewLibrary(hod) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 160 | module.compiler = nil |
| 161 | |
| 162 | prebuilt := &prebuiltLibraryLinker{ |
| 163 | libraryDecorator: library, |
| 164 | } |
| 165 | module.linker = prebuilt |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 166 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 167 | module.AddProperties(&prebuilt.properties) |
| 168 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 169 | srcsSupplier := func() []string { |
| 170 | return prebuilt.prebuiltSrcs() |
| 171 | } |
| 172 | |
| 173 | android.InitPrebuiltModuleWithSrcSupplier(module, srcsSupplier, "srcs") |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 174 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 175 | // Prebuilt libraries can be used in SDKs. |
Jiyong Park | d1063c1 | 2019-07-17 20:08:41 +0900 | [diff] [blame] | 176 | android.InitSdkAwareModule(module) |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 177 | return module, library |
| 178 | } |
| 179 | |
Paul Duffin | bce90da | 2020-03-12 20:17:14 +0000 | [diff] [blame] | 180 | // cc_prebuilt_library installs a precompiled shared library that are |
| 181 | // listed in the srcs property in the device's directory. |
| 182 | func PrebuiltLibraryFactory() android.Module { |
| 183 | module, _ := NewPrebuiltLibrary(android.HostAndDeviceSupported) |
| 184 | |
| 185 | // Prebuilt shared libraries can be included in APEXes |
| 186 | android.InitApexModule(module) |
| 187 | |
| 188 | return module.Init() |
| 189 | } |
| 190 | |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 191 | // cc_prebuilt_library_shared installs a precompiled shared library that are |
| 192 | // listed in the srcs property in the device's directory. |
| 193 | func PrebuiltSharedLibraryFactory() android.Module { |
| 194 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 195 | return module.Init() |
| 196 | } |
| 197 | |
| 198 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
| 199 | module, library := NewPrebuiltLibrary(hod) |
| 200 | library.BuildOnlyShared() |
| 201 | |
| 202 | // Prebuilt shared libraries can be included in APEXes |
| 203 | android.InitApexModule(module) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 204 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 205 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 208 | // cc_prebuilt_library_static installs a precompiled static library that are |
| 209 | // listed in the srcs property in the device's directory. |
Jooyung Han | 344d543 | 2019-08-23 11:17:39 +0900 | [diff] [blame] | 210 | func PrebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 211 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 212 | return module.Init() |
| 213 | } |
| 214 | |
| 215 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
Paul Duffin | ac6e608 | 2019-12-11 15:22:32 +0000 | [diff] [blame] | 216 | module, library := NewPrebuiltLibrary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 217 | library.BuildOnlyStatic() |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 218 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Martin Stjernholm | 0b92ac8 | 2020-03-11 21:45:49 +0000 | [diff] [blame] | 221 | type prebuiltObjectProperties struct { |
| 222 | Srcs []string `android:"path,arch_variant"` |
| 223 | } |
| 224 | |
| 225 | type prebuiltObjectLinker struct { |
| 226 | android.Prebuilt |
| 227 | objectLinker |
| 228 | |
| 229 | properties prebuiltObjectProperties |
| 230 | } |
| 231 | |
| 232 | func (p *prebuiltObjectLinker) prebuilt() *android.Prebuilt { |
| 233 | return &p.Prebuilt |
| 234 | } |
| 235 | |
| 236 | var _ prebuiltLinkerInterface = (*prebuiltObjectLinker)(nil) |
| 237 | |
| 238 | func (p *prebuiltObjectLinker) link(ctx ModuleContext, |
| 239 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 240 | if len(p.properties.Srcs) > 0 { |
| 241 | return p.Prebuilt.SingleSourcePath(ctx) |
| 242 | } |
| 243 | return nil |
| 244 | } |
| 245 | |
| 246 | func newPrebuiltObject() *Module { |
| 247 | module := newObject() |
| 248 | prebuilt := &prebuiltObjectLinker{ |
| 249 | objectLinker: objectLinker{ |
| 250 | baseLinker: NewBaseLinker(nil), |
| 251 | }, |
| 252 | } |
| 253 | module.linker = prebuilt |
| 254 | module.AddProperties(&prebuilt.properties) |
| 255 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
| 256 | android.InitSdkAwareModule(module) |
| 257 | return module |
| 258 | } |
| 259 | |
| 260 | func prebuiltObjectFactory() android.Module { |
| 261 | module := newPrebuiltObject() |
| 262 | return module.Init() |
| 263 | } |
| 264 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 265 | type prebuiltBinaryLinker struct { |
| 266 | *binaryDecorator |
| 267 | prebuiltLinker |
| 268 | } |
| 269 | |
| 270 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 271 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 272 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 273 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 274 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 275 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 276 | builderFlags := flagsToBuilderFlags(flags) |
| 277 | |
| 278 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 279 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 280 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 281 | p.unstrippedOutputFile = in |
| 282 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 283 | if p.needsStrip(ctx) { |
| 284 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
Ryan Prichard | f979d73 | 2019-05-30 20:53:29 -0700 | [diff] [blame] | 285 | p.stripExecutableOrSharedLib(ctx, in, stripped, builderFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 286 | in = stripped |
| 287 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 288 | |
| 289 | // Copy binaries to a name matching the final installed name |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 290 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 291 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 292 | Rule: android.CpExecutable, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 293 | Description: "prebuilt", |
| 294 | Output: outputFile, |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 295 | Input: in, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 296 | }) |
| 297 | |
| 298 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | return nil |
| 302 | } |
| 303 | |
Patrice Arruda | 3554a98 | 2019-03-27 19:09:10 -0700 | [diff] [blame] | 304 | // cc_prebuilt_binary installs a precompiled executable in srcs property in the |
| 305 | // device's directory. |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 306 | func prebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 307 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 308 | return module.Init() |
| 309 | } |
| 310 | |
| 311 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 312 | module, binary := NewBinary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 313 | module.compiler = nil |
| 314 | |
| 315 | prebuilt := &prebuiltBinaryLinker{ |
| 316 | binaryDecorator: binary, |
| 317 | } |
| 318 | module.linker = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 319 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 320 | module.AddProperties(&prebuilt.properties) |
| 321 | |
| 322 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 323 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 324 | } |