xref: /aosp_15_r20/build/soong/cc/binary_sdk_member.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2020 Google Inc. All rights reserved.
2*333d2b36SAndroid Build Coastguard Worker//
3*333d2b36SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*333d2b36SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*333d2b36SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*333d2b36SAndroid Build Coastguard Worker//
7*333d2b36SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*333d2b36SAndroid Build Coastguard Worker//
9*333d2b36SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*333d2b36SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*333d2b36SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*333d2b36SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*333d2b36SAndroid Build Coastguard Worker// limitations under the License.
14*333d2b36SAndroid Build Coastguard Worker
15*333d2b36SAndroid Build Coastguard Workerpackage cc
16*333d2b36SAndroid Build Coastguard Worker
17*333d2b36SAndroid Build Coastguard Workerimport (
18*333d2b36SAndroid Build Coastguard Worker	"path/filepath"
19*333d2b36SAndroid Build Coastguard Worker
20*333d2b36SAndroid Build Coastguard Worker	"android/soong/android"
21*333d2b36SAndroid Build Coastguard Worker
22*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint"
23*333d2b36SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
24*333d2b36SAndroid Build Coastguard Worker)
25*333d2b36SAndroid Build Coastguard Worker
26*333d2b36SAndroid Build Coastguard Workerfunc init() {
27*333d2b36SAndroid Build Coastguard Worker	android.RegisterSdkMemberType(ccBinarySdkMemberType)
28*333d2b36SAndroid Build Coastguard Worker}
29*333d2b36SAndroid Build Coastguard Worker
30*333d2b36SAndroid Build Coastguard Workervar ccBinarySdkMemberType = &binarySdkMemberType{
31*333d2b36SAndroid Build Coastguard Worker	SdkMemberTypeBase: android.SdkMemberTypeBase{
32*333d2b36SAndroid Build Coastguard Worker		PropertyName:    "native_binaries",
33*333d2b36SAndroid Build Coastguard Worker		HostOsDependent: true,
34*333d2b36SAndroid Build Coastguard Worker	},
35*333d2b36SAndroid Build Coastguard Worker}
36*333d2b36SAndroid Build Coastguard Worker
37*333d2b36SAndroid Build Coastguard Workertype binarySdkMemberType struct {
38*333d2b36SAndroid Build Coastguard Worker	android.SdkMemberTypeBase
39*333d2b36SAndroid Build Coastguard Worker}
40*333d2b36SAndroid Build Coastguard Worker
41*333d2b36SAndroid Build Coastguard Workerfunc (mt *binarySdkMemberType) AddDependencies(ctx android.SdkDependencyContext, dependencyTag blueprint.DependencyTag, names []string) {
42*333d2b36SAndroid Build Coastguard Worker	targets := ctx.MultiTargets()
43*333d2b36SAndroid Build Coastguard Worker	for _, bin := range names {
44*333d2b36SAndroid Build Coastguard Worker		for _, target := range targets {
45*333d2b36SAndroid Build Coastguard Worker			variations := target.Variations()
46*333d2b36SAndroid Build Coastguard Worker			if ctx.Device() {
47*333d2b36SAndroid Build Coastguard Worker				variations = append(variations,
48*333d2b36SAndroid Build Coastguard Worker					blueprint.Variation{Mutator: "image", Variation: android.CoreVariation})
49*333d2b36SAndroid Build Coastguard Worker			}
50*333d2b36SAndroid Build Coastguard Worker			ctx.AddFarVariationDependencies(variations, dependencyTag, bin)
51*333d2b36SAndroid Build Coastguard Worker		}
52*333d2b36SAndroid Build Coastguard Worker	}
53*333d2b36SAndroid Build Coastguard Worker}
54*333d2b36SAndroid Build Coastguard Worker
55*333d2b36SAndroid Build Coastguard Workerfunc (mt *binarySdkMemberType) IsInstance(module android.Module) bool {
56*333d2b36SAndroid Build Coastguard Worker	// Check the module to see if it can be used with this module type.
57*333d2b36SAndroid Build Coastguard Worker	if m, ok := module.(*Module); ok {
58*333d2b36SAndroid Build Coastguard Worker		for _, allowableMemberType := range m.sdkMemberTypes {
59*333d2b36SAndroid Build Coastguard Worker			if allowableMemberType == mt {
60*333d2b36SAndroid Build Coastguard Worker				return true
61*333d2b36SAndroid Build Coastguard Worker			}
62*333d2b36SAndroid Build Coastguard Worker		}
63*333d2b36SAndroid Build Coastguard Worker	}
64*333d2b36SAndroid Build Coastguard Worker
65*333d2b36SAndroid Build Coastguard Worker	return false
66*333d2b36SAndroid Build Coastguard Worker}
67*333d2b36SAndroid Build Coastguard Worker
68*333d2b36SAndroid Build Coastguard Workerfunc (mt *binarySdkMemberType) AddPrebuiltModule(ctx android.SdkMemberContext, member android.SdkMember) android.BpModule {
69*333d2b36SAndroid Build Coastguard Worker	pbm := ctx.SnapshotBuilder().AddPrebuiltModule(member, "cc_prebuilt_binary")
70*333d2b36SAndroid Build Coastguard Worker
71*333d2b36SAndroid Build Coastguard Worker	ccModule := member.Variants()[0].(*Module)
72*333d2b36SAndroid Build Coastguard Worker
73*333d2b36SAndroid Build Coastguard Worker	if stl := ccModule.stl.Properties.Stl; stl != nil {
74*333d2b36SAndroid Build Coastguard Worker		pbm.AddProperty("stl", proptools.String(stl))
75*333d2b36SAndroid Build Coastguard Worker	}
76*333d2b36SAndroid Build Coastguard Worker
77*333d2b36SAndroid Build Coastguard Worker	return pbm
78*333d2b36SAndroid Build Coastguard Worker}
79*333d2b36SAndroid Build Coastguard Worker
80*333d2b36SAndroid Build Coastguard Workerfunc (mt *binarySdkMemberType) CreateVariantPropertiesStruct() android.SdkMemberProperties {
81*333d2b36SAndroid Build Coastguard Worker	return &nativeBinaryInfoProperties{}
82*333d2b36SAndroid Build Coastguard Worker}
83*333d2b36SAndroid Build Coastguard Worker
84*333d2b36SAndroid Build Coastguard Workerconst (
85*333d2b36SAndroid Build Coastguard Worker	nativeBinaryDir = "bin"
86*333d2b36SAndroid Build Coastguard Worker)
87*333d2b36SAndroid Build Coastguard Worker
88*333d2b36SAndroid Build Coastguard Worker// path to the native binary. Relative to <sdk_root>/<api_dir>
89*333d2b36SAndroid Build Coastguard Workerfunc nativeBinaryPathFor(lib nativeBinaryInfoProperties) string {
90*333d2b36SAndroid Build Coastguard Worker	return filepath.Join(lib.OsPrefix(), lib.archType,
91*333d2b36SAndroid Build Coastguard Worker		nativeBinaryDir, lib.outputFile.Base())
92*333d2b36SAndroid Build Coastguard Worker}
93*333d2b36SAndroid Build Coastguard Worker
94*333d2b36SAndroid Build Coastguard Worker// nativeBinaryInfoProperties represents properties of a native binary
95*333d2b36SAndroid Build Coastguard Worker//
96*333d2b36SAndroid Build Coastguard Worker// The exported (capitalized) fields will be examined and may be changed during common value extraction.
97*333d2b36SAndroid Build Coastguard Worker// The unexported fields will be left untouched.
98*333d2b36SAndroid Build Coastguard Workertype nativeBinaryInfoProperties struct {
99*333d2b36SAndroid Build Coastguard Worker	android.SdkMemberPropertiesBase
100*333d2b36SAndroid Build Coastguard Worker
101*333d2b36SAndroid Build Coastguard Worker	// archType is not exported as if set (to a non default value) it is always arch specific.
102*333d2b36SAndroid Build Coastguard Worker	// This is "" for common properties.
103*333d2b36SAndroid Build Coastguard Worker	archType string
104*333d2b36SAndroid Build Coastguard Worker
105*333d2b36SAndroid Build Coastguard Worker	// outputFile is not exported as it is always arch specific.
106*333d2b36SAndroid Build Coastguard Worker	outputFile android.Path
107*333d2b36SAndroid Build Coastguard Worker
108*333d2b36SAndroid Build Coastguard Worker	// The set of shared libraries
109*333d2b36SAndroid Build Coastguard Worker	//
110*333d2b36SAndroid Build Coastguard Worker	// This field is exported as its contents may not be arch specific.
111*333d2b36SAndroid Build Coastguard Worker	SharedLibs []string
112*333d2b36SAndroid Build Coastguard Worker
113*333d2b36SAndroid Build Coastguard Worker	// The set of system shared libraries
114*333d2b36SAndroid Build Coastguard Worker	//
115*333d2b36SAndroid Build Coastguard Worker	// This field is exported as its contents may not be arch specific.
116*333d2b36SAndroid Build Coastguard Worker	SystemSharedLibs []string
117*333d2b36SAndroid Build Coastguard Worker
118*333d2b36SAndroid Build Coastguard Worker	// Arch specific flags.
119*333d2b36SAndroid Build Coastguard Worker	StaticExecutable bool
120*333d2b36SAndroid Build Coastguard Worker	Nocrt            bool
121*333d2b36SAndroid Build Coastguard Worker}
122*333d2b36SAndroid Build Coastguard Worker
123*333d2b36SAndroid Build Coastguard Workerfunc (p *nativeBinaryInfoProperties) PopulateFromVariant(ctx android.SdkMemberContext, variant android.Module) {
124*333d2b36SAndroid Build Coastguard Worker	ccModule := variant.(*Module)
125*333d2b36SAndroid Build Coastguard Worker
126*333d2b36SAndroid Build Coastguard Worker	p.archType = ccModule.Target().Arch.ArchType.String()
127*333d2b36SAndroid Build Coastguard Worker	p.outputFile = getRequiredMemberOutputFile(ctx, ccModule)
128*333d2b36SAndroid Build Coastguard Worker
129*333d2b36SAndroid Build Coastguard Worker	binaryLinker := ccModule.linker.(*binaryDecorator)
130*333d2b36SAndroid Build Coastguard Worker	p.StaticExecutable = binaryLinker.static()
131*333d2b36SAndroid Build Coastguard Worker	p.Nocrt = Bool(binaryLinker.baseLinker.Properties.Nocrt)
132*333d2b36SAndroid Build Coastguard Worker
133*333d2b36SAndroid Build Coastguard Worker	if ccModule.linker != nil {
134*333d2b36SAndroid Build Coastguard Worker		specifiedDeps := specifiedDeps{}
135*333d2b36SAndroid Build Coastguard Worker		specifiedDeps = ccModule.linker.linkerSpecifiedDeps(ctx.SdkModuleContext(), ccModule, specifiedDeps)
136*333d2b36SAndroid Build Coastguard Worker
137*333d2b36SAndroid Build Coastguard Worker		p.SharedLibs = specifiedDeps.sharedLibs
138*333d2b36SAndroid Build Coastguard Worker		p.SystemSharedLibs = specifiedDeps.systemSharedLibs
139*333d2b36SAndroid Build Coastguard Worker	}
140*333d2b36SAndroid Build Coastguard Worker}
141*333d2b36SAndroid Build Coastguard Worker
142*333d2b36SAndroid Build Coastguard Workerfunc (p *nativeBinaryInfoProperties) AddToPropertySet(ctx android.SdkMemberContext, propertySet android.BpPropertySet) {
143*333d2b36SAndroid Build Coastguard Worker	builder := ctx.SnapshotBuilder()
144*333d2b36SAndroid Build Coastguard Worker	if p.outputFile != nil {
145*333d2b36SAndroid Build Coastguard Worker		propertySet.AddProperty("srcs", []string{nativeBinaryPathFor(*p)})
146*333d2b36SAndroid Build Coastguard Worker
147*333d2b36SAndroid Build Coastguard Worker		builder.CopyToSnapshot(p.outputFile, nativeBinaryPathFor(*p))
148*333d2b36SAndroid Build Coastguard Worker	}
149*333d2b36SAndroid Build Coastguard Worker
150*333d2b36SAndroid Build Coastguard Worker	if len(p.SharedLibs) > 0 {
151*333d2b36SAndroid Build Coastguard Worker		propertySet.AddPropertyWithTag("shared_libs", p.SharedLibs, builder.SdkMemberReferencePropertyTag(false))
152*333d2b36SAndroid Build Coastguard Worker	}
153*333d2b36SAndroid Build Coastguard Worker
154*333d2b36SAndroid Build Coastguard Worker	// SystemSharedLibs needs to be propagated if it's a list, even if it's empty,
155*333d2b36SAndroid Build Coastguard Worker	// so check for non-nil instead of nonzero length.
156*333d2b36SAndroid Build Coastguard Worker	if p.SystemSharedLibs != nil {
157*333d2b36SAndroid Build Coastguard Worker		propertySet.AddPropertyWithTag("system_shared_libs", p.SystemSharedLibs, builder.SdkMemberReferencePropertyTag(false))
158*333d2b36SAndroid Build Coastguard Worker	}
159*333d2b36SAndroid Build Coastguard Worker
160*333d2b36SAndroid Build Coastguard Worker	if p.StaticExecutable {
161*333d2b36SAndroid Build Coastguard Worker		propertySet.AddProperty("static_executable", p.StaticExecutable)
162*333d2b36SAndroid Build Coastguard Worker	}
163*333d2b36SAndroid Build Coastguard Worker	if p.Nocrt {
164*333d2b36SAndroid Build Coastguard Worker		propertySet.AddProperty("nocrt", p.Nocrt)
165*333d2b36SAndroid Build Coastguard Worker	}
166*333d2b36SAndroid Build Coastguard Worker}
167