blob: 9b3235c5d111cbbce28d23c2cce2e3a8b6e5a960 [file] [log] [blame]
Paul Duffin25ce04b2020-01-16 11:47:25 +00001// Copyright 2020 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 "path/filepath"
19
20 "android/soong/android"
21 "github.com/google/blueprint"
22)
23
24func init() {
25 android.RegisterSdkMemberType(ccBinarySdkMemberType)
26}
27
28var ccBinarySdkMemberType = &binarySdkMemberType{
29 SdkMemberTypeBase: android.SdkMemberTypeBase{
30 PropertyName: "native_binaries",
31 },
32}
33
34type binarySdkMemberType struct {
35 android.SdkMemberTypeBase
36}
37
38func (mt *binarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
39 targets := mctx.MultiTargets()
40 for _, lib := range names {
41 for _, target := range targets {
42 name, version := StubsLibNameAndVersion(lib)
43 if version == "" {
44 version = LatestStubsVersionFor(mctx.Config(), name)
45 }
46 mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
47 {Mutator: "version", Variation: version},
48 }...), dependencyTag, name)
49 }
50 }
51}
52
53func (mt *binarySdkMemberType) IsInstance(module android.Module) bool {
54 // Check the module to see if it can be used with this module type.
55 if m, ok := module.(*Module); ok {
56 for _, allowableMemberType := range m.sdkMemberTypes {
57 if allowableMemberType == mt {
58 return true
59 }
60 }
61 }
62
63 return false
64}
65
Paul Duffin3a4eb502020-03-19 16:11:18 +000066func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
67 return ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary")
Paul Duffin88f2fbe2020-02-27 16:00:53 +000068}
Paul Duffin13ad94f2020-02-19 16:19:27 +000069
Paul Duffin88f2fbe2020-02-27 16:00:53 +000070func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
71 return &nativeBinaryInfoProperties{}
Paul Duffin25ce04b2020-01-16 11:47:25 +000072}
73
74const (
75 nativeBinaryDir = "bin"
76)
77
78// path to the native binary. Relative to <sdk_root>/<api_dir>
79func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +000080 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin25ce04b2020-01-16 11:47:25 +000081 nativeBinaryDir, lib.outputFile.Base())
82}
83
84// nativeBinaryInfoProperties represents properties of a native binary
85//
86// The exported (capitalized) fields will be examined and may be changed during common value extraction.
87// The unexported fields will be left untouched.
88type nativeBinaryInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +000089 android.SdkMemberPropertiesBase
Paul Duffin25ce04b2020-01-16 11:47:25 +000090
91 // archType is not exported as if set (to a non default value) it is always arch specific.
92 // This is "" for common properties.
93 archType string
94
95 // outputFile is not exported as it is always arch specific.
96 outputFile android.Path
Paul Duffin13f02712020-03-06 12:30:43 +000097
98 // The set of shared libraries
99 //
100 // This field is exported as its contents may not be arch specific.
101 SharedLibs []string
102
103 // The set of system shared libraries
104 //
105 // This field is exported as its contents may not be arch specific.
106 SystemSharedLibs []string
Paul Duffin25ce04b2020-01-16 11:47:25 +0000107}
108
Paul Duffin3a4eb502020-03-19 16:11:18 +0000109func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000110 ccModule := variant.(*Module)
111
112 p.archType = ccModule.Target().Arch.ArchType.String()
113 p.outputFile = ccModule.OutputFile().Path()
Paul Duffin13f02712020-03-06 12:30:43 +0000114
115 if ccModule.linker != nil {
116 specifiedDeps := specifiedDeps{}
117 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
118
119 p.SharedLibs = specifiedDeps.sharedLibs
120 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
121 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000122}
123
Paul Duffin3a4eb502020-03-19 16:11:18 +0000124func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000125 if p.Compile_multilib != "" {
126 propertySet.AddProperty("compile_multilib", p.Compile_multilib)
127 }
128
Paul Duffin3a4eb502020-03-19 16:11:18 +0000129 builder := ctx.SnapshotBuilder()
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000130 if p.outputFile != nil {
131 propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})
132
133 builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p))
134 }
Paul Duffin13f02712020-03-06 12:30:43 +0000135
136 if len(p.SharedLibs) > 0 {
137 propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
138 }
139
140 if len(p.SystemSharedLibs) > 0 {
141 propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
142 }
Paul Duffin25ce04b2020-01-16 11:47:25 +0000143}