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