| 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 |  | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 54 | func (p *prebuiltLibraryLinker) link(ctx ModuleContext, | 
| Dan Willemsen | 5cb580f | 2016-09-26 17:33:01 -0700 | [diff] [blame] | 55 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 56 | // TODO(ccross): verify shared library dependencies | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 57 | if len(p.properties.Srcs) > 0 { | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 58 | p.libraryDecorator.exportIncludes(ctx, "-I") | 
|  | 59 | p.libraryDecorator.reexportFlags(deps.ReexportedFlags) | 
|  | 60 | p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps) | 
|  | 61 | // TODO(ccross): .toc optimization, stripping, packing | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 62 | return p.Prebuilt.SingleSourcePath(ctx) | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 63 | } | 
|  | 64 |  | 
|  | 65 | return nil | 
|  | 66 | } | 
|  | 67 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 68 | func prebuiltSharedLibraryFactory() android.Module { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 69 | module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported) | 
|  | 70 | return module.Init() | 
|  | 71 | } | 
|  | 72 |  | 
|  | 73 | func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { | 
|  | 74 | module, library := NewLibrary(hod) | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 75 | library.BuildOnlyShared() | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 76 | module.compiler = nil | 
|  | 77 |  | 
|  | 78 | prebuilt := &prebuiltLibraryLinker{ | 
|  | 79 | libraryDecorator: library, | 
|  | 80 | } | 
|  | 81 | module.linker = prebuilt | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 82 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 83 | module.AddProperties(&prebuilt.properties) | 
|  | 84 |  | 
|  | 85 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 86 | return module, library | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 87 | } | 
|  | 88 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 89 | func prebuiltStaticLibraryFactory() android.Module { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 90 | module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported) | 
|  | 91 | return module.Init() | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) { | 
|  | 95 | module, library := NewLibrary(hod) | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 96 | library.BuildOnlyStatic() | 
|  | 97 | module.compiler = nil | 
|  | 98 |  | 
|  | 99 | prebuilt := &prebuiltLibraryLinker{ | 
|  | 100 | libraryDecorator: library, | 
|  | 101 | } | 
|  | 102 | module.linker = prebuilt | 
|  | 103 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 104 | module.AddProperties(&prebuilt.properties) | 
|  | 105 |  | 
|  | 106 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 107 | return module, library | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 108 | } | 
|  | 109 |  | 
|  | 110 | type prebuiltBinaryLinker struct { | 
|  | 111 | *binaryDecorator | 
|  | 112 | prebuiltLinker | 
|  | 113 | } | 
|  | 114 |  | 
|  | 115 | var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil) | 
|  | 116 |  | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 117 | func (p *prebuiltBinaryLinker) link(ctx ModuleContext, | 
|  | 118 | flags Flags, deps PathDeps, objs Objects) android.Path { | 
|  | 119 | // TODO(ccross): verify shared library dependencies | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 120 | if len(p.properties.Srcs) > 0 { | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 121 | // TODO(ccross): .toc optimization, stripping, packing | 
| Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 122 |  | 
|  | 123 | // Copy binaries to a name matching the final installed name | 
|  | 124 | fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix() | 
|  | 125 | outputFile := android.PathForModuleOut(ctx, fileName) | 
|  | 126 |  | 
| Colin Cross | ae88703 | 2017-10-23 17:16:14 -0700 | [diff] [blame] | 127 | ctx.Build(pctx, android.BuildParams{ | 
| Colin Cross | 5c51792 | 2017-08-31 12:29:17 -0700 | [diff] [blame] | 128 | Rule:        android.CpExecutable, | 
| Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 129 | Description: "prebuilt", | 
|  | 130 | Output:      outputFile, | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 131 | Input:       p.Prebuilt.SingleSourcePath(ctx), | 
| Colin Cross | 94921e7 | 2017-08-08 16:20:15 -0700 | [diff] [blame] | 132 | }) | 
|  | 133 |  | 
|  | 134 | return outputFile | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 135 | } | 
|  | 136 |  | 
|  | 137 | return nil | 
|  | 138 | } | 
|  | 139 |  | 
| Colin Cross | 3624285 | 2017-06-23 15:06:31 -0700 | [diff] [blame] | 140 | func prebuiltBinaryFactory() android.Module { | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 141 | module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported) | 
|  | 142 | return module.Init() | 
|  | 143 | } | 
|  | 144 |  | 
|  | 145 | func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) { | 
|  | 146 | module, binary := NewBinary(hod) | 
| Colin Cross | de89fb8 | 2017-03-17 13:28:06 -0700 | [diff] [blame] | 147 | module.compiler = nil | 
|  | 148 |  | 
|  | 149 | prebuilt := &prebuiltBinaryLinker{ | 
|  | 150 | binaryDecorator: binary, | 
|  | 151 | } | 
|  | 152 | module.linker = prebuilt | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 153 |  | 
| Colin Cross | 74d73e2 | 2017-08-02 11:05:49 -0700 | [diff] [blame] | 154 | module.AddProperties(&prebuilt.properties) | 
|  | 155 |  | 
|  | 156 | android.InitPrebuiltModule(module, &prebuilt.properties.Srcs) | 
| Leo Li | 74f7b97 | 2017-05-17 11:30:45 -0700 | [diff] [blame] | 157 | return module, binary | 
| Colin Cross | ce75d2c | 2016-10-06 16:12:58 -0700 | [diff] [blame] | 158 | } |