blob: a1abc728aa7542edfe341c75894e5d667d95316b [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"
Martin Stjernholm10566a02020-03-24 01:19:52 +000021
Paul Duffin25ce04b2020-01-16 11:47:25 +000022 "github.com/google/blueprint"
Martin Stjernholm7130fab2020-05-28 22:58:01 +010023 "github.com/google/blueprint/proptools"
Paul Duffin25ce04b2020-01-16 11:47:25 +000024)
25
26func init() {
27 android.RegisterSdkMemberType(ccBinarySdkMemberType)
28}
29
30var ccBinarySdkMemberType = &binarySdkMemberType{
31 SdkMemberTypeBase: android.SdkMemberTypeBase{
Martin Stjernholmcaa47d72020-07-11 04:52:24 +010032 PropertyName: "native_binaries",
33 HostOsDependent: true,
Paul Duffin25ce04b2020-01-16 11:47:25 +000034 },
35}
36
37type binarySdkMemberType struct {
38 android.SdkMemberTypeBase
39}
40
41func (mt *binarySdkMemberType) AddDependencies(mctx android.BottomUpMutatorContext, dependencyTag blueprint.DependencyTag, names []string) {
42 targets := mctx.MultiTargets()
43 for _, lib := range names {
44 for _, target := range targets {
45 name, version := StubsLibNameAndVersion(lib)
46 if version == "" {
47 version = LatestStubsVersionFor(mctx.Config(), name)
48 }
Colin Cross42507332020-08-21 16:15:23 -070049 variations := target.Variations()
50 if mctx.Device() {
51 variations = append(variations,
52 blueprint.Variation{Mutator: "image", Variation: android.CoreVariation},
53 blueprint.Variation{Mutator: "version", Variation: version})
54 }
55 mctx.AddFarVariationDependencies(variations, dependencyTag, name)
Paul Duffin25ce04b2020-01-16 11:47:25 +000056 }
57 }
58}
59
60func (mt *binarySdkMemberType) IsInstance(module android.Module) bool {
61 // Check the module to see if it can be used with this module type.
62 if m, ok := module.(*Module); ok {
63 for _, allowableMemberType := range m.sdkMemberTypes {
64 if allowableMemberType == mt {
65 return true
66 }
67 }
68 }
69
70 return false
71}
72
Paul Duffin3a4eb502020-03-19 16:11:18 +000073func (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
Martin Stjernholm7130fab2020-05-28 22:58:01 +010074 pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary")
75
76 ccModule := member.Variants()[0].(*Module)
77
78 if stl := ccModule.stl.Properties.Stl; stl != nil {
79 pbm.AddProperty("stl", proptools.String(stl))
80 }
81
82 return pbm
Paul Duffin88f2fbe2020-02-27 16:00:53 +000083}
Paul Duffin13ad94f2020-02-19 16:19:27 +000084
Paul Duffin88f2fbe2020-02-27 16:00:53 +000085func (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
86 return &nativeBinaryInfoProperties{}
Paul Duffin25ce04b2020-01-16 11:47:25 +000087}
88
89const (
90 nativeBinaryDir = "bin"
91)
92
93// path to the native binary. Relative to <sdk_root>/<api_dir>
94func nativeBinaryPathFor(lib nativeBinaryInfoProperties) string {
Paul Duffina04c1072020-03-02 10:16:35 +000095 return filepath.Join(lib.OsPrefix(), lib.archType,
Paul Duffin25ce04b2020-01-16 11:47:25 +000096 nativeBinaryDir, lib.outputFile.Base())
97}
98
99// nativeBinaryInfoProperties represents properties of a native binary
100//
101// The exported (capitalized) fields will be examined and may be changed during common value extraction.
102// The unexported fields will be left untouched.
103type nativeBinaryInfoProperties struct {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000104 android.SdkMemberPropertiesBase
Paul Duffin25ce04b2020-01-16 11:47:25 +0000105
106 // archType is not exported as if set (to a non default value) it is always arch specific.
107 // This is "" for common properties.
108 archType string
109
110 // outputFile is not exported as it is always arch specific.
111 outputFile android.Path
Paul Duffin13f02712020-03-06 12:30:43 +0000112
113 // The set of shared libraries
114 //
115 // This field is exported as its contents may not be arch specific.
116 SharedLibs []string
117
118 // The set of system shared libraries
119 //
120 // This field is exported as its contents may not be arch specific.
121 SystemSharedLibs []string
Martin Stjernholm7130fab2020-05-28 22:58:01 +0100122
123 // Arch specific flags.
124 StaticExecutable bool
125 Nocrt bool
Paul Duffin25ce04b2020-01-16 11:47:25 +0000126}
127
Paul Duffin3a4eb502020-03-19 16:11:18 +0000128func (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000129 ccModule := variant.(*Module)
130
131 p.archType = ccModule.Target().Arch.ArchType.String()
Paul Duffin712993c2020-05-05 14:11:57 +0100132 p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
Paul Duffin13f02712020-03-06 12:30:43 +0000133
Martin Stjernholm7130fab2020-05-28 22:58:01 +0100134 binaryLinker := ccModule.linker.(*binaryDecorator)
135 p.StaticExecutable = binaryLinker.static()
136 p.Nocrt = Bool(binaryLinker.baseLinker.Properties.Nocrt)
137
Paul Duffin13f02712020-03-06 12:30:43 +0000138 if ccModule.linker != nil {
139 specifiedDeps := specifiedDeps{}
140 specifiedDeps = ccModule.linker.linkerSpecifiedDeps(specifiedDeps)
141
142 p.SharedLibs = specifiedDeps.sharedLibs
143 p.SystemSharedLibs = specifiedDeps.systemSharedLibs
144 }
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000145}
146
Paul Duffin3a4eb502020-03-19 16:11:18 +0000147func (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
Paul Duffin3a4eb502020-03-19 16:11:18 +0000148 builder := ctx.SnapshotBuilder()
Paul Duffin88f2fbe2020-02-27 16:00:53 +0000149 if p.outputFile != nil {
150 propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})
151
152 builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p))
153 }
Paul Duffin13f02712020-03-06 12:30:43 +0000154
155 if len(p.SharedLibs) > 0 {
156 propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
157 }
158
Martin Stjernholm10566a02020-03-24 01:19:52 +0000159 // SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
160 // so check for non-nil instead of nonzero length.
161 if p.SystemSharedLibs != nil {
Paul Duffin13f02712020-03-06 12:30:43 +0000162 propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
163 }
Martin Stjernholm7130fab2020-05-28 22:58:01 +0100164
165 if p.StaticExecutable {
166 propertySet.AddProperty("static_executable", p.StaticExecutable)
167 }
168 if p.Nocrt {
169 propertySet.AddProperty("nocrt", p.Nocrt)
170 }
Paul Duffin25ce04b2020-01-16 11:47:25 +0000171}