blob: cd3eb1e78abc920f63d18922d1c708c6e0b24657 [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
Chris Parsons637458d2023-09-19 20:09:00 +000048func (fp *fdoProfile) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Vinh Trance40b922023-06-05 12:57:55 -040049 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{
Vinh Tran0bc8c952023-09-26 11:17:31 -040075 Bzl_load_location: "//build/bazel/rules/fdo:fdo_profile.bzl",
76 Rule_class: "fdo_profile",
Vinh Trance40b922023-06-05 12:57:55 -040077 },
78 android.CommonAttributes{
79 Name: fp.Name(),
80 },
81 &bazelFdoProfileAttributes{
82 Profile: profileAttr,
83 },
84 noRestriction,
85 )
86}
87
Vinh Tran0ccc2322023-03-09 22:07:19 -050088// FdoProfileInfo is provided by FdoProfileProvider
89type FdoProfileInfo struct {
90 Path android.Path
91}
92
93// FdoProfileProvider is used to provide path to an fdo profile
94var FdoProfileProvider = blueprint.NewMutatorProvider(FdoProfileInfo{}, "fdo_profile")
95
96// FdoProfileMutatorInterface is the interface implemented by fdo_profile module type
97// module types that can depend on an fdo_profile module
98type FdoProfileMutatorInterface interface {
99 // FdoProfileMutator eithers set or get FdoProfileProvider
Vinh Tran611f0362023-03-09 22:07:19 -0500100 fdoProfileMutator(ctx android.BottomUpMutatorContext)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500101}
102
103var _ FdoProfileMutatorInterface = (*fdoProfile)(nil)
104
105// GenerateAndroidBuildActions of fdo_profile does not have any build actions
106func (fp *fdoProfile) GenerateAndroidBuildActions(ctx android.ModuleContext) {}
107
108// FdoProfileMutator sets FdoProfileProvider to fdo_profile module
109// or sets afdo.Properties.FdoProfilePath to path in FdoProfileProvider of the depended fdo_profile
Vinh Tran611f0362023-03-09 22:07:19 -0500110func (fp *fdoProfile) fdoProfileMutator(ctx android.BottomUpMutatorContext) {
Vinh Tran0ccc2322023-03-09 22:07:19 -0500111 if fp.properties.Profile != nil {
112 path := android.PathForModuleSrc(ctx, *fp.properties.Profile)
113 ctx.SetProvider(FdoProfileProvider, FdoProfileInfo{
114 Path: path,
115 })
116 }
117}
118
Vinh Tran611f0362023-03-09 22:07:19 -0500119// fdoProfileMutator calls the generic fdoProfileMutator function of fdoProfileMutator
Vinh Tran0ccc2322023-03-09 22:07:19 -0500120// which is implemented by cc and cc.FdoProfile
121func fdoProfileMutator(ctx android.BottomUpMutatorContext) {
122 if f, ok := ctx.Module().(FdoProfileMutatorInterface); ok {
Vinh Tran611f0362023-03-09 22:07:19 -0500123 f.fdoProfileMutator(ctx)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500124 }
125}
126
Vinh Trance40b922023-06-05 12:57:55 -0400127func FdoProfileFactory() android.Module {
Vinh Tran0ccc2322023-03-09 22:07:19 -0500128 m := &fdoProfile{}
129 m.AddProperties(&m.properties)
130 android.InitAndroidMultiTargetsArchModule(m, android.DeviceSupported, android.MultilibBoth)
Vinh Trance40b922023-06-05 12:57:55 -0400131 android.InitBazelModule(m)
Vinh Tran0ccc2322023-03-09 22:07:19 -0500132 return m
133}