blob: 976c711dde7c8c20ee9c18320126929ff6a4873b [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
34}
35
Colin Crossde89fb82017-03-17 13:28:06 -070036func (p *prebuiltLinker) prebuilt() *android.Prebuilt {
Colin Crossce75d2c2016-10-06 16:12:58 -070037 return &p.Prebuilt
38}
39
Colin Crossde89fb82017-03-17 13:28:06 -070040type prebuiltLibraryLinker struct {
41 *libraryDecorator
42 prebuiltLinker
43}
44
45var _ prebuiltLinkerInterface = (*prebuiltLibraryLinker)(nil)
46
Colin Crossce75d2c2016-10-06 16:12:58 -070047func (p *prebuiltLibraryLinker) linkerProps() []interface{} {
48 props := p.libraryDecorator.linkerProps()
49 return append(props, &p.Prebuilt.Properties)
50}
51
52func (p *prebuiltLibraryLinker) link(ctx ModuleContext,
Dan Willemsen5cb580f2016-09-26 17:33:01 -070053 flags Flags, deps PathDeps, objs Objects) android.Path {
Colin Crossce75d2c2016-10-06 16:12:58 -070054 // TODO(ccross): verify shared library dependencies
55 if len(p.Prebuilt.Properties.Srcs) > 0 {
56 p.libraryDecorator.exportIncludes(ctx, "-I")
57 p.libraryDecorator.reexportFlags(deps.ReexportedFlags)
58 p.libraryDecorator.reexportDeps(deps.ReexportedFlagsDeps)
59 // TODO(ccross): .toc optimization, stripping, packing
60 return p.Prebuilt.Path(ctx)
61 }
62
63 return nil
64}
65
Colin Cross36242852017-06-23 15:06:31 -070066func prebuiltSharedLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -070067 module, _ := NewPrebuiltSharedLibrary(android.HostAndDeviceSupported)
68 return module.Init()
69}
70
71func NewPrebuiltSharedLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
72 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -070073 library.BuildOnlyShared()
Colin Crossce75d2c2016-10-06 16:12:58 -070074 module.compiler = nil
75
76 prebuilt := &prebuiltLibraryLinker{
77 libraryDecorator: library,
78 }
79 module.linker = prebuilt
Colin Crossde89fb82017-03-17 13:28:06 -070080
Leo Li74f7b972017-05-17 11:30:45 -070081 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -070082}
83
Colin Cross36242852017-06-23 15:06:31 -070084func prebuiltStaticLibraryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -070085 module, _ := NewPrebuiltStaticLibrary(android.HostAndDeviceSupported)
86 return module.Init()
87}
88
89func NewPrebuiltStaticLibrary(hod android.HostOrDeviceSupported) (*Module, *libraryDecorator) {
90 module, library := NewLibrary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -070091 library.BuildOnlyStatic()
92 module.compiler = nil
93
94 prebuilt := &prebuiltLibraryLinker{
95 libraryDecorator: library,
96 }
97 module.linker = prebuilt
98
Leo Li74f7b972017-05-17 11:30:45 -070099 return module, library
Colin Crossde89fb82017-03-17 13:28:06 -0700100}
101
102type prebuiltBinaryLinker struct {
103 *binaryDecorator
104 prebuiltLinker
105}
106
107var _ prebuiltLinkerInterface = (*prebuiltBinaryLinker)(nil)
108
109func (p *prebuiltBinaryLinker) linkerProps() []interface{} {
110 props := p.binaryDecorator.linkerProps()
111 return append(props, &p.Prebuilt.Properties)
112}
113
114func (p *prebuiltBinaryLinker) link(ctx ModuleContext,
115 flags Flags, deps PathDeps, objs Objects) android.Path {
116 // TODO(ccross): verify shared library dependencies
117 if len(p.Prebuilt.Properties.Srcs) > 0 {
118 // TODO(ccross): .toc optimization, stripping, packing
Colin Cross94921e72017-08-08 16:20:15 -0700119
120 // Copy binaries to a name matching the final installed name
121 fileName := p.getStem(ctx) + flags.Toolchain.ExecutableSuffix()
122 outputFile := android.PathForModuleOut(ctx, fileName)
123
124 ctx.ModuleBuild(pctx, android.ModuleBuildParams{
125 Rule: android.Cp,
126 Description: "prebuilt",
127 Output: outputFile,
128 Input: p.Prebuilt.Path(ctx),
129 })
130
131 return outputFile
Colin Crossde89fb82017-03-17 13:28:06 -0700132 }
133
134 return nil
135}
136
Colin Cross36242852017-06-23 15:06:31 -0700137func prebuiltBinaryFactory() android.Module {
Leo Li74f7b972017-05-17 11:30:45 -0700138 module, _ := NewPrebuiltBinary(android.HostAndDeviceSupported)
139 return module.Init()
140}
141
142func NewPrebuiltBinary(hod android.HostOrDeviceSupported) (*Module, *binaryDecorator) {
143 module, binary := NewBinary(hod)
Colin Crossde89fb82017-03-17 13:28:06 -0700144 module.compiler = nil
145
146 prebuilt := &prebuiltBinaryLinker{
147 binaryDecorator: binary,
148 }
149 module.linker = prebuilt
Colin Crossce75d2c2016-10-06 16:12:58 -0700150
Leo Li74f7b972017-05-17 11:30:45 -0700151 return module, binary
Colin Crossce75d2c2016-10-06 16:12:58 -0700152}