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