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() { |
Colin Cross | dfee1bc | 2017-03-17 14:03:18 -0700 | [diff] [blame] | 22 | android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 23 | android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory) |
| 24 | android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory) |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | type prebuiltLinkerInterface interface { |
| 28 | Name(string) string |
| 29 | prebuilt() *android.Prebuilt |
| 30 | } |
| 31 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 32 | type prebuiltLinker struct { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 33 | android.Prebuilt |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 34 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 35 | properties struct { |
Colin Cross | 27b922f | 2019-03-04 22:35:41 -0800 | [diff] [blame] | 36 | Srcs []string `android:"path,arch_variant"` |
Logan Chien | 4fcea3d | 2018-11-20 11:59:08 +0800 | [diff] [blame] | 37 | |
| 38 | // Check the prebuilt ELF files (e.g. DT_SONAME, DT_NEEDED, resolution of undefined |
| 39 | // symbols, etc), default true. |
| 40 | Check_elf_files *bool |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 41 | } |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 42 | } |
| 43 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 44 | func (p *prebuiltLinker) prebuilt() *android.Prebuilt { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 45 | return &p.Prebuilt |
| 46 | } |
| 47 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 48 | func (p *prebuiltLinker) PrebuiltSrcs() []string { |
| 49 | return p.properties.Srcs |
| 50 | } |
| 51 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 52 | type prebuiltLibraryLinker struct { |
| 53 | *libraryDecorator |
| 54 | prebuiltLinker |
| 55 | } |
| 56 | |
| 57 | var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil) |
| 58 | |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 59 | func (p *prebuiltLibraryLinker) linkerInit(ctx BaseModuleContext) {} |
| 60 | |
| 61 | func (p *prebuiltLibraryLinker) linkerDeps(ctx DepsContext, deps Deps) Deps { |
Logan Chien | c7f797e | 2019-01-14 15:35:08 +0800 | [diff] [blame] | 62 | return p.libraryDecorator.linkerDeps(ctx, deps) |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | func (p *prebuiltLibraryLinker) linkerFlags(ctx ModuleContext, flags Flags) Flags { |
Colin Cross | 1ab10a7 | 2018-09-04 11:02:37 -0700 | [diff] [blame] | 66 | return flags |
Yi Kong | 0098166 | 2018-08-13 16:02:49 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | func (p *prebuiltLibraryLinker) linkerProps() []interface{} { |
| 70 | return p.libraryDecorator.linkerProps() |
| 71 | } |
| 72 | |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 73 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, |
Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 74 | flags Flags, deps PathDeps, objs Objects) android.Path { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 75 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 76 | if len(p.properties.Srcs) > 0 { |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 77 | p.libraryDecorator.exportIncludes(ctx, "-I") |
| 78 | p.libraryDecorator.reexportFlags(deps.ReexportedFlags) |
| 79 | p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 80 | |
| 81 | builderFlags := flagsToBuilderFlags(flags) |
| 82 | |
| 83 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 84 | |
| 85 | if p.shared() { |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 86 | p.unstrippedOutputFile = in |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 87 | libName := ctx.baseModuleName() + flags.Toolchain.ShlibSuffix() |
| 88 | if p.needsStrip(ctx) { |
| 89 | stripped := android.PathForModuleOut(ctx, "stripped", libName) |
| 90 | p.strip(ctx, in, stripped, builderFlags) |
| 91 | in = stripped |
| 92 | } |
| 93 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 94 | // Optimize out relinking against shared libraries whose interface hasn't changed by |
| 95 | // depending on a table of contents file instead of the library itself. |
| 96 | tocFile := android.PathForModuleOut(ctx, libName+".toc") |
| 97 | p.tocFile = android.OptionalPathForPath(tocFile) |
| 98 | TransformSharedObjectToToc(ctx, in, tocFile, builderFlags) |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | return in |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | return nil |
| 105 | } |
| 106 | |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 107 | func (p *prebuiltLibraryLinker) shared() bool { |
| 108 | return p.libraryDecorator.shared() |
| 109 | } |
| 110 | |
Pirama Arumuga Nainar | 65c95ff | 2019-03-25 10:21:31 -0700 | [diff] [blame] | 111 | func (p *prebuiltLibraryLinker) nativeCoverage() bool { |
| 112 | return false |
| 113 | } |
| 114 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 115 | func prebuiltSharedLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 116 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) |
| 117 | return module.Init() |
| 118 | } |
| 119 | |
| 120 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
| 121 | module, library := NewLibrary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 122 | library.BuildOnlyShared() |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 123 | module.compiler = nil |
| 124 | |
| 125 | prebuilt := &prebuiltLibraryLinker{ |
| 126 | libraryDecorator: library, |
| 127 | } |
| 128 | module.linker = prebuilt |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 129 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 130 | module.AddProperties(&prebuilt.properties) |
| 131 | |
| 132 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Jiyong Park | 379de2f | 2018-12-19 02:47:14 +0900 | [diff] [blame] | 133 | |
| 134 | // Prebuilt libraries can be included in APEXes |
| 135 | android.InitApexModule(module) |
| 136 | |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 137 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 140 | func prebuiltStaticLibraryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 141 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) |
| 142 | return module.Init() |
| 143 | } |
| 144 | |
| 145 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { |
| 146 | module, library := NewLibrary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 147 | library.BuildOnlyStatic() |
| 148 | module.compiler = nil |
| 149 | |
| 150 | prebuilt := &prebuiltLibraryLinker{ |
| 151 | libraryDecorator: library, |
| 152 | } |
| 153 | module.linker = prebuilt |
| 154 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 155 | module.AddProperties(&prebuilt.properties) |
| 156 | |
| 157 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 158 | return module, library |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | type prebuiltBinaryLinker struct { |
| 162 | *binaryDecorator |
| 163 | prebuiltLinker |
| 164 | } |
| 165 | |
| 166 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) |
| 167 | |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 168 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, |
| 169 | flags Flags, deps PathDeps, objs Objects) android.Path { |
| 170 | // TODO(ccross): verify shared library dependencies |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 171 | if len(p.properties.Srcs) > 0 { |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 172 | builderFlags := flagsToBuilderFlags(flags) |
| 173 | |
| 174 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() |
| 175 | in := p.Prebuilt.SingleSourcePath(ctx) |
| 176 | |
Colin Cross | b60190a | 2018-09-04 16:28:17 -0700 | [diff] [blame] | 177 | p.unstrippedOutputFile = in |
| 178 | |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 179 | if p.needsStrip(ctx) { |
| 180 | stripped := android.PathForModuleOut(ctx, "stripped", fileName) |
| 181 | p.strip(ctx, in, stripped, builderFlags) |
| 182 | in = stripped |
| 183 | } |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 184 | |
| 185 | // Copy binaries to a name matching the final installed name |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 186 | outputFile := android.PathForModuleOut(ctx, fileName) |
Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 187 | ctx.Build(pctx, android.BuildParams{ |
Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 188 | Rule: android.CpExecutable, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 189 | Description: "prebuilt", |
| 190 | Output: outputFile, |
Colin Cross | 88f6fef | 2018-09-05 14:20:03 -0700 | [diff] [blame] | 191 | Input: in, |
Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 192 | }) |
| 193 | |
| 194 | return outputFile |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return nil |
| 198 | } |
| 199 | |
Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 200 | func prebuiltBinaryFactory() android.Module { |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 201 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) |
| 202 | return module.Init() |
| 203 | } |
| 204 | |
| 205 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { |
| 206 | module, binary := NewBinary(hod) |
Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 207 | module.compiler = nil |
| 208 | |
| 209 | prebuilt := &prebuiltBinaryLinker{ |
| 210 | binaryDecorator: binary, |
| 211 | } |
| 212 | module.linker = prebuilt |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 213 | |
Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 214 | module.AddProperties(&prebuilt.properties) |
| 215 | |
| 216 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) |
Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 217 | return module, binary |
Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 218 | } |