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