xref: /aosp_15_r20/system/sepolicy/build/soong/sepolicy_vers.go (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1// Copyright 2021 The Android Open Source Project
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 selinux
16
17import (
18	"github.com/google/blueprint/proptools"
19
20	"android/soong/android"
21)
22
23func init() {
24	android.RegisterModuleType("sepolicy_vers", sepolicyVersFactory)
25}
26
27// sepolicy_vers prints sepolicy version string to {partition}/etc/selinux.
28func sepolicyVersFactory() android.Module {
29	v := &sepolicyVers{}
30	v.AddProperties(&v.properties)
31	android.InitAndroidArchModule(v, android.DeviceSupported, android.MultilibCommon)
32	return v
33}
34
35type sepolicyVers struct {
36	android.ModuleBase
37	properties    sepolicyVersProperties
38	installSource android.Path
39	installPath   android.InstallPath
40}
41
42type sepolicyVersProperties struct {
43	// Version to output. Can be "platform" for PLATFORM_SEPOLICY_VERSION, "vendor" for
44	// BOARD_SEPOLICY_VERS
45	Version *string
46
47	// Output file name. Defaults to module name if unspecified.
48	Stem *string
49
50	// Whether this module is directly installable to one of the partitions. Default is true
51	Installable *bool
52}
53
54func (v *sepolicyVers) installable() bool {
55	return proptools.BoolDefault(v.properties.Installable, true)
56}
57
58func (v *sepolicyVers) stem() string {
59	return proptools.StringDefault(v.properties.Stem, v.Name())
60}
61
62func (v *sepolicyVers) DepsMutator(ctx android.BottomUpMutatorContext) {
63	// do nothing
64}
65
66func (v *sepolicyVers) GenerateAndroidBuildActions(ctx android.ModuleContext) {
67	var ver string
68	switch proptools.String(v.properties.Version) {
69	case "platform":
70		ver = ctx.DeviceConfig().PlatformSepolicyVersion()
71	case "vendor":
72		ver = ctx.DeviceConfig().BoardSepolicyVers()
73	default:
74		ctx.PropertyErrorf("version", `should be either "platform" or "vendor"`)
75	}
76
77	out := android.PathForModuleGen(ctx, v.stem())
78
79	rule := android.NewRuleBuilder(pctx, ctx)
80	rule.Command().Text("echo").Text(ver).Text(">").Output(out)
81	rule.Build("sepolicy_vers", v.Name())
82
83	if !v.installable() {
84		v.SkipInstall()
85	}
86
87	v.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
88	v.installSource = out
89	ctx.InstallFile(v.installPath, v.stem(), v.installSource)
90
91	ctx.SetOutputFiles(android.Paths{v.installSource}, "")
92}
93
94func (v *sepolicyVers) AndroidMkEntries() []android.AndroidMkEntries {
95	return []android.AndroidMkEntries{android.AndroidMkEntries{
96		Class:      "ETC",
97		OutputFile: android.OptionalPathForPath(v.installSource),
98		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
99			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
100				entries.SetPath("LOCAL_MODULE_PATH", v.installPath)
101				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", v.stem())
102			},
103		},
104	}}
105}
106