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