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