blob: d61af7e44cdb44f561945e8c9b964b4162a1cf91 [file] [log] [blame]
Vinh Tran0ccc2322023-03-09 22:07:19 -05001// Copyright 2023 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"
Vinh Trance40b922023-06-05 12:57:55 -040019 "android/soong/bazel"
Vinh Tran0ccc2322023-03-09 22:07:19 -050020
21 "github.com/google/blueprint"
Vinh Trance40b922023-06-05 12:57:55 -040022 "github.com/google/blueprint/proptools"
Vinh Tran0ccc2322023-03-09 22:07:19 -050023)
24
25func init() {
26 RegisterFdoProfileBuildComponents(android.InitRegistrationContext)
27}
28
29func RegisterFdoProfileBuildComponents(ctx android.RegistrationContext) {
Vinh Trance40b922023-06-05 12:57:55 -040030 ctx.RegisterModuleType("fdo_profile", FdoProfileFactory)
Vinh Tran0ccc2322023-03-09 22:07:19 -050031}
32
33type fdoProfile struct {
34 android.ModuleBase
Vinh Trance40b922023-06-05 12:57:55 -040035 android.BazelModuleBase
Vinh Tran0ccc2322023-03-09 22:07:19 -050036
37 properties fdoProfileProperties
38}
39
40type fdoProfileProperties struct {
41 Profile *string `android:"arch_variant"`
42}
43
Vinh Trance40b922023-06-05 12:57:55 -040044type bazelFdoProfileAttributes struct {
45 Profile bazel.StringAttribute
46}
47
48func (fp *fdoProfile) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
49 var profileAttr bazel.StringAttribute
50
51 archVariantProps := fp.GetArchVariantProperties(ctx, &fdoProfileProperties{})
52 for axis, configToProps := range archVariantProps {
53 for config, _props := range configToProps {
54 if archProps, ok := _props.(*fdoProfileProperties); ok {
55 if axis.String() == "arch" || axis.String() == "no_config" {
56 if archProps.Profile != nil {
57 profileAttr.SetSelectValue(axis, config, archProps.Profile)
58 }
59 }
60 }
61 }
62 }
63
64 // Ideally, cc_library_shared's fdo_profile attr can be a select statement so that we
65 // don't lift the restriction here. However, in cc_library_shared macro, fdo_profile
66 // is used as a string, we need to temporarily lift the host restriction until we can
67 // pass use fdo_profile attr with select statement
68 // https://cs.android.com/android/platform/superproject/+/master:build/bazel/rules/cc/cc_library_shared.bzl;l=127;drc=cc01bdfd39857eddbab04ef69ab6db22dcb1858a
69 // TODO(b/276287371): Drop the restriction override after fdo_profile path is handled properly
70 var noRestriction bazel.BoolAttribute
71 noRestriction.SetSelectValue(bazel.NoConfigAxis, "", proptools.BoolPtr(true))
72
73 ctx.CreateBazelTargetModuleWithRestrictions(
74 bazel.BazelTargetModuleProperties{
75 Rule_class: "fdo_profile",
76 },
77 android.CommonAttributes{
78 Name: fp.Name(),
79 },
80 &bazelFdoProfileAttributes{
81 Profile: profileAttr,
82 },
83 noRestriction,
84 )
85}
86
Vinh Tran0ccc2322023-03-09 22:07:19 -050087// FdoProfileInfo is provided by FdoProfileProvider
88type FdoProfileInfo struct {
89 Path android.Path
90}
91
92// FdoProfileProvider is used to provide path to an fdo profile
93var FdoProfileProvider = blueprint.NewMutatorProvider(FdoProfileInfo{}, "fdo_profile")
94
95// FdoProfileMutatorInterface is the interface implemented by fdo_profile module type
96// module types that can depend on an fdo_profile module
97type FdoProfileMutatorInterface interface {
98 // FdoProfileMutator eithers set or get FdoProfileProvider
Vinh Tran611f0362023-03-09 22:07:19 -050099 fdoProfileMutator(ctx android.BottomUpMutatorContext)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500100}
101
102var _ FdoProfileMutatorInterface = (*fdoProfile)(nil)
103
104// GenerateAndroidBuildActions of fdo_profile does not have any build actions
105func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
106
107// FdoProfileMutator sets FdoProfileProvider to fdo_profile module
108// or sets afdo.Properties.FdoProfilePath to path in FdoProfileProvider of the depended fdo_profile
Vinh Tran611f0362023-03-09 22:07:19 -0500109func (fp *fdoProfile) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
Vinh Tran0ccc2322023-03-09 22:07:19 -0500110 if fp.properties.Profile != nil {
111 path := android.PathForModuleSrc(ctx, *fp.properties.Profile)
112 ctx.SetProvider(FdoProfileProvider, FdoProfileInfo{
113 Path: path,
114 })
115 }
116}
117
Vinh Tran611f0362023-03-09 22:07:19 -0500118// fdoProfileMutator calls the generic fdoProfileMutator function of fdoProfileMutator
Vinh Tran0ccc2322023-03-09 22:07:19 -0500119// which is implemented by cc and cc.FdoProfile
120func fdoProfileMutator(ctx android.BottomUpMutatorContext) {
121 if f, ok := ctx.Module().(FdoProfileMutatorInterface); ok {
Vinh Tran611f0362023-03-09 22:07:19 -0500122 f.fdoProfileMutator(ctx)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500123 }
124}
125
Vinh Trance40b922023-06-05 12:57:55 -0400126func FdoProfileFactory() android.Module {
Vinh Tran0ccc2322023-03-09 22:07:19 -0500127 m := &fdoProfile{}
128 m.AddProperties(&m.properties)
129 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibBoth)
Vinh Trance40b922023-06-05 12:57:55 -0400130 android.InitBazelModule(m)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500131 return m
132}