xref: /aosp_15_r20/build/soong/aconfig/build_flags/release_configs.go (revision 333d2b3687b3a337dbcca9d65000bca186795e39)
1*333d2b36SAndroid Build Coastguard Worker// Copyright 2023 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 build_flags
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)
24*333d2b36SAndroid Build Coastguard Worker
25*333d2b36SAndroid Build Coastguard Workertype ReleaseConfigContributionsProviderData struct {
26*333d2b36SAndroid Build Coastguard Worker	ContributionDir android.SourcePath
27*333d2b36SAndroid Build Coastguard Worker}
28*333d2b36SAndroid Build Coastguard Worker
29*333d2b36SAndroid Build Coastguard Workervar ReleaseConfigContributionsProviderKey = blueprint.NewProvider[ReleaseConfigContributionsProviderData]()
30*333d2b36SAndroid Build Coastguard Worker
31*333d2b36SAndroid Build Coastguard Worker// Soong uses `release_config_contributions` modules to produce the
32*333d2b36SAndroid Build Coastguard Worker// `build_flags/all_release_config_contributions.*` artifacts, listing *all* of
33*333d2b36SAndroid Build Coastguard Worker// the directories in the source tree that contribute to each release config,
34*333d2b36SAndroid Build Coastguard Worker// whether or not they are actually used for the lunch product.
35*333d2b36SAndroid Build Coastguard Worker//
36*333d2b36SAndroid Build Coastguard Worker// This artifact helps flagging automation determine in which directory a flag
37*333d2b36SAndroid Build Coastguard Worker// should be placed by default.
38*333d2b36SAndroid Build Coastguard Workertype ReleaseConfigContributionsModule struct {
39*333d2b36SAndroid Build Coastguard Worker	android.ModuleBase
40*333d2b36SAndroid Build Coastguard Worker	android.DefaultableModuleBase
41*333d2b36SAndroid Build Coastguard Worker
42*333d2b36SAndroid Build Coastguard Worker	// Properties for "release_config_contributions"
43*333d2b36SAndroid Build Coastguard Worker	properties struct {
44*333d2b36SAndroid Build Coastguard Worker		// The `release_configs/*.textproto` files provided by this
45*333d2b36SAndroid Build Coastguard Worker		// directory, relative to this Android.bp file
46*333d2b36SAndroid Build Coastguard Worker		Srcs []string `android:"path"`
47*333d2b36SAndroid Build Coastguard Worker	}
48*333d2b36SAndroid Build Coastguard Worker}
49*333d2b36SAndroid Build Coastguard Worker
50*333d2b36SAndroid Build Coastguard Workerfunc ReleaseConfigContributionsFactory() android.Module {
51*333d2b36SAndroid Build Coastguard Worker	module := &ReleaseConfigContributionsModule{}
52*333d2b36SAndroid Build Coastguard Worker
53*333d2b36SAndroid Build Coastguard Worker	android.InitAndroidModule(module)
54*333d2b36SAndroid Build Coastguard Worker	android.InitDefaultableModule(module)
55*333d2b36SAndroid Build Coastguard Worker	module.AddProperties(&module.properties)
56*333d2b36SAndroid Build Coastguard Worker
57*333d2b36SAndroid Build Coastguard Worker	return module
58*333d2b36SAndroid Build Coastguard Worker}
59*333d2b36SAndroid Build Coastguard Worker
60*333d2b36SAndroid Build Coastguard Workerfunc (module *ReleaseConfigContributionsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
61*333d2b36SAndroid Build Coastguard Worker	srcs := android.PathsForModuleSrc(ctx, module.properties.Srcs)
62*333d2b36SAndroid Build Coastguard Worker	if len(srcs) == 0 {
63*333d2b36SAndroid Build Coastguard Worker		return
64*333d2b36SAndroid Build Coastguard Worker	}
65*333d2b36SAndroid Build Coastguard Worker	contributionDir := filepath.Dir(filepath.Dir(srcs[0].String()))
66*333d2b36SAndroid Build Coastguard Worker	for _, file := range srcs {
67*333d2b36SAndroid Build Coastguard Worker		if filepath.Dir(filepath.Dir(file.String())) != contributionDir {
68*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Cannot include %s with %s contributions", file, contributionDir)
69*333d2b36SAndroid Build Coastguard Worker		}
70*333d2b36SAndroid Build Coastguard Worker		if filepath.Base(filepath.Dir(file.String())) != "release_configs" || file.Ext() != ".textproto" {
71*333d2b36SAndroid Build Coastguard Worker			ctx.ModuleErrorf("Invalid contribution file %s", file)
72*333d2b36SAndroid Build Coastguard Worker		}
73*333d2b36SAndroid Build Coastguard Worker	}
74*333d2b36SAndroid Build Coastguard Worker	android.SetProvider(ctx, ReleaseConfigContributionsProviderKey, ReleaseConfigContributionsProviderData{
75*333d2b36SAndroid Build Coastguard Worker		ContributionDir: android.PathForSource(ctx, contributionDir),
76*333d2b36SAndroid Build Coastguard Worker	})
77*333d2b36SAndroid Build Coastguard Worker
78*333d2b36SAndroid Build Coastguard Worker}
79