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