blob: fc9b89e75803bed87955d149bffbf3a1d6e317a5 [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 Duffin88f2fbe2020-02-27 16:00:53 +000066func (mt *binarySdkMemberType) AddPrebuiltModule(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, member android.SdkMember) android.BpModule {
Paul Duffin25ce04b2020-01-16 11:47:25 +000067 pbm := builder.AddPrebuiltModule(member, "cc_prebuilt_binary")
Paul Duffin88f2fbe2020-02-27 16:00:53 +000068 return pbm
69}
Paul Duffin13ad94f2020-02-19 16:19:27 +000070
Paul Duffin88f2fbe2020-02-27 16:00:53 +000071func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
72 return &nativeBinaryInfoProperties{}
Paul Duffin25ce04b2020-01-16 11:47:25 +000073}
74
75const (
76 nativeBinaryDir = "bin"
77)
78
79// path to the native binary. Relative to <sdk_root>/<api_dir>
80func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +000081 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin25ce04b2020-01-16 11:47:25 +000082 nativeBinaryDir, lib.outputFile.Base())
83}
84
85// nativeBinaryInfoProperties represents properties of a native binary
86//
87// The exported (capitalized) fields will be examined and may be changed during common value extraction.
88// The unexported fields will be left untouched.
89type nativeBinaryInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +000090 android.SdkMemberPropertiesBase
Paul Duffin25ce04b2020-01-16 11:47:25 +000091
92 // archType is not exported as if set (to a non default value) it is always arch specific.
93 // This is "" for common properties.
94 archType string
95
96 // outputFile is not exported as it is always arch specific.
97 outputFile android.Path
98}
99
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000100func (p *nativeBinaryInfoProperties) PopulateFromVariant(variant android.SdkAware) {
101 ccModule := variant.(*Module)
102
103 p.archType = ccModule.Target().Arch.ArchType.String()
104 p.outputFile = ccModule.OutputFile().Path()
105}
106
107func (p *nativeBinaryInfoProperties) AddToPropertySet(sdkModuleContext android.ModuleContext, builder android.SnapshotBuilder, propertySet android.BpPropertySet) {
108 if p.Compile_multilib != "" {
109 propertySet.AddProperty("compile_multilib", p.Compile_multilib)
110 }
111
112 if p.outputFile != nil {
113 propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})
114
115 builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p))
116 }
Paul Duffin25ce04b2020-01-16 11:47:25 +0000117}