• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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	"fmt"
19
20	"github.com/google/blueprint/proptools"
21
22	"android/soong/android"
23)
24
25func init() {
26	android.RegisterModuleType("sepolicy_vers", sepolicyVersFactory)
27}
28
29// sepolicy_vers prints sepolicy version string to {partition}/etc/selinux.
30func sepolicyVersFactory() android.Module {
31	v := &sepolicyVers{}
32	v.AddProperties(&v.properties)
33	android.InitAndroidArchModule(v, android.DeviceSupported, android.MultilibCommon)
34	return v
35}
36
37type sepolicyVers struct {
38	android.ModuleBase
39	properties    sepolicyVersProperties
40	installSource android.Path
41	installPath   android.InstallPath
42}
43
44type sepolicyVersProperties struct {
45	// Version to output. Can be "platform" for PLATFORM_SEPOLICY_VERSION, "vendor" for
46	// BOARD_SEPOLICY_VERS
47	Version *string
48
49	// Output file name. Defaults to module name if unspecified.
50	Stem *string
51
52	// Whether this module is directly installable to one of the partitions. Default is true
53	Installable *bool
54}
55
56func (v *sepolicyVers) installable() bool {
57	return proptools.BoolDefault(v.properties.Installable, true)
58}
59
60func (v *sepolicyVers) stem() string {
61	return proptools.StringDefault(v.properties.Stem, v.Name())
62}
63
64func (v *sepolicyVers) DepsMutator(ctx android.BottomUpMutatorContext) {
65	// do nothing
66}
67
68func (v *sepolicyVers) GenerateAndroidBuildActions(ctx android.ModuleContext) {
69	var ver string
70	switch proptools.String(v.properties.Version) {
71	case "platform":
72		ver = ctx.DeviceConfig().PlatformSepolicyVersion()
73	case "vendor":
74		ver = ctx.DeviceConfig().BoardSepolicyVers()
75	default:
76		ctx.PropertyErrorf("version", `should be either "platform" or "vendor"`)
77	}
78
79	out := android.PathForModuleGen(ctx, v.stem())
80
81	rule := android.NewRuleBuilder(pctx, ctx)
82	rule.Command().Text("echo").Text(ver).Text(">").Output(out)
83	rule.Build("sepolicy_vers", v.Name())
84
85	v.installPath = android.PathForModuleInstall(ctx, "etc", "selinux")
86	v.installSource = out
87	ctx.InstallFile(v.installPath, v.stem(), v.installSource)
88
89	if !v.installable() {
90		v.SkipInstall()
91	}
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.ToMakePath())
101				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", v.stem())
102			},
103		},
104	}}
105}
106
107func (v *sepolicyVers) OutputFiles(tag string) (android.Paths, error) {
108	if tag == "" {
109		return android.Paths{v.installSource}, nil
110	}
111	return nil, fmt.Errorf("Unknown tag %q", tag)
112}
113
114var _ android.OutputFileProducer = (*sepolicyVers)(nil)
115