blob: 3f277aa3d8596586cc27b78ef0767c4ee479007f [file] [log] [blame]
Colin Crossce75d2c2016-10-06 16:12:58 -07001// 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
15package cc
16
17import (
18 "android/soong/android"
Colin Crossce75d2c2016-10-06 16:12:58 -070019)
20
21func init() {
Colin Crossdfee1bc2017-03-17 14:03:18 -070022 android.RegisterModuleType("cc_prebuilt_library_shared", prebuiltSharedLibraryFactory)
Colin Crossde89fb82017-03-17 13:28:06 -070023 android.RegisterModuleType("cc_prebuilt_library_static", prebuiltStaticLibraryFactory)
24 android.RegisterModuleType("cc_prebuilt_binary", prebuiltBinaryFactory)
Colin Crossce75d2c2016-10-06 16:12:58 -070025}
26
27type prebuiltLinkerInterface interface {
28 Name(string) string
29 prebuilt() *android.Prebuilt
30}
31
Colin Crossde89fb82017-03-17 13:28:06 -070032type prebuiltLinker struct {
Colin Crossce75d2c2016-10-06 16:12:58 -070033 android.Prebuilt
Colin Cross74d73e22017-08-02 11:05:49 -070034 properties struct {
35 Srcs []string `android:"arch_variant"`
36 }
Colin Crossce75d2c2016-10-06 16:12:58 -070037}
38
Colin Crossde89fb82017-03-17 13:28:06 -070039func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070040 return &p.Prebuilt
41}
42
Colin Cross74d73e22017-08-02 11:05:49 -070043func (p *prebuiltLinker) PrebuiltSrcs() []string {
44 return p.properties.Srcs
45}
46
Colin Crossde89fb82017-03-17 13:28:06 -070047type prebuiltLibraryLinker struct {
48 *libraryDecorator
49 prebuiltLinker
50}
51
52var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
53
Colin Crossce75d2c2016-10-06 16:12:58 -070054func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070055 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070056 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -070057 if len(p.properties.Srcs) > 0 {
Colin Crossce75d2c2016-10-06 16:12:58 -070058 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 Cross74d73e22017-08-02 11:05:49 -070062 return p.Prebuilt.SingleSourcePath(ctx)
Colin Crossce75d2c2016-10-06 16:12:58 -070063 }
64
65 return nil
66}
67
Colin Cross36242852017-06-23 15:06:31 -070068func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -070069 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
70 return module.Init()
71}
72
73func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
74 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -070075 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -070076 module.compiler = nil
77
78 prebuilt := &prebuiltLibraryLinker{
79 libraryDecorator: library,
80 }
81 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -070082
Colin Cross74d73e22017-08-02 11:05:49 -070083 module.AddProperties(&prebuilt.properties)
84
85 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -070086 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -070087}
88
Colin Cross36242852017-06-23 15:06:31 -070089func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -070090 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
91 return module.Init()
92}
93
94func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
95 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -070096 library.BuildOnlyStatic()
97 module.compiler = nil
98
99 prebuilt := &prebuiltLibraryLinker{
100 libraryDecorator: library,
101 }
102 module.linker = prebuilt
103
Colin Cross74d73e22017-08-02 11:05:49 -0700104 module.AddProperties(&prebuilt.properties)
105
106 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700107 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700108}
109
110type prebuiltBinaryLinker struct {
111 *binaryDecorator
112 prebuiltLinker
113}
114
115var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
116
Colin Crossde89fb82017-03-17 13:28:06 -0700117func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
118 flags Flags, deps PathDeps, objs Objects) android.Path {
119 // TODO(ccross): verify shared library dependencies
Colin Cross74d73e22017-08-02 11:05:49 -0700120 if len(p.properties.Srcs) > 0 {
Colin Crossde89fb82017-03-17 13:28:06 -0700121 // TODO(ccross): .toc optimization, stripping, packing
Colin Cross94921e72017-08-08 16:20:15 -0700122
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 Crossae887032017-10-23 17:16:14 -0700127 ctx.Build(pctx, android.BuildParams{
Colin Cross5c517922017-08-31 12:29:17 -0700128 Rule: android.CpExecutable,
Colin Cross94921e72017-08-08 16:20:15 -0700129 Description: "prebuilt",
130 Output: outputFile,
Colin Cross74d73e22017-08-02 11:05:49 -0700131 Input: p.Prebuilt.SingleSourcePath(ctx),
Colin Cross94921e72017-08-08 16:20:15 -0700132 })
133
134 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700135 }
136
137 return nil
138}
139
Colin Cross36242852017-06-23 15:06:31 -0700140func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700141 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
142 return module.Init()
143}
144
145func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
146 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700147 module.compiler = nil
148
149 prebuilt := &prebuiltBinaryLinker{
150 binaryDecorator: binary,
151 }
152 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700153
Colin Cross74d73e22017-08-02 11:05:49 -0700154 module.AddProperties(&prebuilt.properties)
155
156 android.InitPrebuiltModule(module, &prebuilt.properties.Srcs)
Leo Li74f7b972017-05-17 11:30:45 -0700157 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700158}