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