xref: /aosp_15_r20/system/sepolicy/build/soong/sepolicy_vers.go (revision e4a36f4174b17bbab9dc043f4a65dc8d87377290)
1*e4a36f41SAndroid Build Coastguard Worker// Copyright 2021 The Android Open Source Project
2*e4a36f41SAndroid Build Coastguard Worker//
3*e4a36f41SAndroid Build Coastguard Worker// Licensed under the Apache License, Version 2.0 (the "License");
4*e4a36f41SAndroid Build Coastguard Worker// you may not use this file except in compliance with the License.
5*e4a36f41SAndroid Build Coastguard Worker// You may obtain a copy of the License at
6*e4a36f41SAndroid Build Coastguard Worker//
7*e4a36f41SAndroid Build Coastguard Worker//     http://www.apache.org/licenses/LICENSE-2.0
8*e4a36f41SAndroid Build Coastguard Worker//
9*e4a36f41SAndroid Build Coastguard Worker// Unless required by applicable law or agreed to in writing, software
10*e4a36f41SAndroid Build Coastguard Worker// distributed under the License is distributed on an "AS IS" BASIS,
11*e4a36f41SAndroid Build Coastguard Worker// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e4a36f41SAndroid Build Coastguard Worker// See the License for the specific language governing permissions and
13*e4a36f41SAndroid Build Coastguard Worker// limitations under the License.
14*e4a36f41SAndroid Build Coastguard Worker
15*e4a36f41SAndroid Build Coastguard Workerpackage selinux
16*e4a36f41SAndroid Build Coastguard Worker
17*e4a36f41SAndroid Build Coastguard Workerimport (
18*e4a36f41SAndroid Build Coastguard Worker	"github.com/google/blueprint/proptools"
19*e4a36f41SAndroid Build Coastguard Worker
20*e4a36f41SAndroid Build Coastguard Worker	"android/soong/android"
21*e4a36f41SAndroid Build Coastguard Worker)
22*e4a36f41SAndroid Build Coastguard Worker
23*e4a36f41SAndroid Build Coastguard Workerfunc init() {
24*e4a36f41SAndroid Build Coastguard Worker	android.RegisterModuleType("sepolicy_vers", sepolicyVersFactory)
25*e4a36f41SAndroid Build Coastguard Worker}
26*e4a36f41SAndroid Build Coastguard Worker
27*e4a36f41SAndroid Build Coastguard Worker// sepolicy_vers prints sepolicy version string to {partition}/etc/selinux.
28*e4a36f41SAndroid Build Coastguard Workerfunc sepolicyVersFactory() android.Module {
29*e4a36f41SAndroid Build Coastguard Worker	v := &sepolicyVers{}
30*e4a36f41SAndroid Build Coastguard Worker	v.AddProperties(&v.properties)
31*e4a36f41SAndroid Build Coastguard Worker	android.InitAndroidArchModule(v, android.DeviceSupported, android.MultilibCommon)
32*e4a36f41SAndroid Build Coastguard Worker	return v
33*e4a36f41SAndroid Build Coastguard Worker}
34*e4a36f41SAndroid Build Coastguard Worker
35*e4a36f41SAndroid Build Coastguard Workertype sepolicyVers struct {
36*e4a36f41SAndroid Build Coastguard Worker	android.ModuleBase
37*e4a36f41SAndroid Build Coastguard Worker	properties    sepolicyVersProperties
38*e4a36f41SAndroid Build Coastguard Worker	installSource android.Path
39*e4a36f41SAndroid Build Coastguard Worker	installPath   android.InstallPath
40*e4a36f41SAndroid Build Coastguard Worker}
41*e4a36f41SAndroid Build Coastguard Worker
42*e4a36f41SAndroid Build Coastguard Workertype sepolicyVersProperties struct {
43*e4a36f41SAndroid Build Coastguard Worker	// Version to output. Can be "platform" for PLATFORM_SEPOLICY_VERSION, "vendor" for
44*e4a36f41SAndroid Build Coastguard Worker	// BOARD_SEPOLICY_VERS
45*e4a36f41SAndroid Build Coastguard Worker	Version *string
46*e4a36f41SAndroid Build Coastguard Worker
47*e4a36f41SAndroid Build Coastguard Worker	// Output file name. Defaults to module name if unspecified.
48*e4a36f41SAndroid Build Coastguard Worker	Stem *string
49*e4a36f41SAndroid Build Coastguard Worker
50*e4a36f41SAndroid Build Coastguard Worker	// Whether this module is directly installable to one of the partitions. Default is true
51*e4a36f41SAndroid Build Coastguard Worker	Installable *bool
52*e4a36f41SAndroid Build Coastguard Worker}
53*e4a36f41SAndroid Build Coastguard Worker
54*e4a36f41SAndroid Build Coastguard Workerfunc (v *sepolicyVers) installable() bool {
55*e4a36f41SAndroid Build Coastguard Worker	return proptools.BoolDefault(v.properties.Installable, true)
56*e4a36f41SAndroid Build Coastguard Worker}
57*e4a36f41SAndroid Build Coastguard Worker
58*e4a36f41SAndroid Build Coastguard Workerfunc (v *sepolicyVers) stem() string {
59*e4a36f41SAndroid Build Coastguard Worker	return proptools.StringDefault(v.properties.Stem, v.Name())
60*e4a36f41SAndroid Build Coastguard Worker}
61*e4a36f41SAndroid Build Coastguard Worker
62*e4a36f41SAndroid Build Coastguard Workerfunc (v *sepolicyVers) DepsMutator(ctx android.BottomUpMutatorContext) {
63*e4a36f41SAndroid Build Coastguard Worker	// do nothing
64*e4a36f41SAndroid Build Coastguard Worker}
65*e4a36f41SAndroid Build Coastguard Worker
66*e4a36f41SAndroid Build Coastguard Workerfunc (v *sepolicyVers) GenerateAndroidBuildActions(ctx android.ModuleContext) {
67*e4a36f41SAndroid Build Coastguard Worker	var ver string
68*e4a36f41SAndroid Build Coastguard Worker	switch proptools.String(v.properties.Version) {
69*e4a36f41SAndroid Build Coastguard Worker	case "platform":
70*e4a36f41SAndroid Build Coastguard Worker		ver = ctx.DeviceConfig().PlatformSepolicyVersion()
71*e4a36f41SAndroid Build Coastguard Worker	case "vendor":
72*e4a36f41SAndroid Build Coastguard Worker		ver = ctx.DeviceConfig().BoardSepolicyVers()
73*e4a36f41SAndroid Build Coastguard Worker	default:
74*e4a36f41SAndroid Build Coastguard Worker		ctx.PropertyErrorf("version", `should be either "platform" or "vendor"`)
75*e4a36f41SAndroid Build Coastguard Worker	}
76*e4a36f41SAndroid Build Coastguard Worker
77*e4a36f41SAndroid Build Coastguard Worker	out := android.PathForModuleGen(ctx, v.stem())
78*e4a36f41SAndroid Build Coastguard Worker
79*e4a36f41SAndroid Build Coastguard Worker	rule := android.NewRuleBuilder(pctx, ctx)
80*e4a36f41SAndroid Build Coastguard Worker	rule.Command().Text("echo").Text(ver).Text(">").Output(out)
81*e4a36f41SAndroid Build Coastguard Worker	rule.Build("sepolicy_vers", v.Name())
82*e4a36f41SAndroid Build Coastguard Worker
83*e4a36f41SAndroid Build Coastguard Worker	if !v.installable() {
84*e4a36f41SAndroid Build Coastguard Worker		v.SkipInstall()
85*e4a36f41SAndroid Build Coastguard Worker	}
86*e4a36f41SAndroid Build Coastguard Worker
87*e4a36f41SAndroid Build Coastguard Worker	v.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
88*e4a36f41SAndroid Build Coastguard Worker	v.installSource = out
89*e4a36f41SAndroid Build Coastguard Worker	ctx.InstallFile(v.installPath, v.stem(), v.installSource)
90*e4a36f41SAndroid Build Coastguard Worker
91*e4a36f41SAndroid Build Coastguard Worker	ctx.SetOutputFiles(android.Paths{v.installSource}, "")
92*e4a36f41SAndroid Build Coastguard Worker}
93*e4a36f41SAndroid Build Coastguard Worker
94*e4a36f41SAndroid Build Coastguard Workerfunc (v *sepolicyVers) AndroidMkEntries() []android.AndroidMkEntries {
95*e4a36f41SAndroid Build Coastguard Worker	return []android.AndroidMkEntries{android.AndroidMkEntries{
96*e4a36f41SAndroid Build Coastguard Worker		Class:      "ETC",
97*e4a36f41SAndroid Build Coastguard Worker		OutputFile: android.OptionalPathForPath(v.installSource),
98*e4a36f41SAndroid Build Coastguard Worker		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
99*e4a36f41SAndroid Build Coastguard Worker			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
100*e4a36f41SAndroid Build Coastguard Worker				entries.SetPath("LOCAL_MODULE_PATH", v.installPath)
101*e4a36f41SAndroid Build Coastguard Worker				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", v.stem())
102*e4a36f41SAndroid Build Coastguard Worker			},
103*e4a36f41SAndroid Build Coastguard Worker		},
104*e4a36f41SAndroid Build Coastguard Worker	}}
105*e4a36f41SAndroid Build Coastguard Worker}
106