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("se_compat_cil", compatCilFactory)
25}
26
27// se_compat_cil collects and installs backwards compatibility cil files.
28func compatCilFactory() android.Module {
29	c := &compatCil{}
30	c.AddProperties(&c.properties)
31	android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon)
32	return c
33}
34
35type compatCil struct {
36	android.ModuleBase
37	properties    compatCilProperties
38	installSource android.Path
39	installPath   android.InstallPath
40}
41
42type compatCilProperties struct {
43	// List of source files. Can reference se_filegroup type modules with the ":module" syntax.
44	Srcs []string
45
46	// Output file name. Defaults to module name if unspecified.
47	Stem *string
48}
49
50func (c *compatCil) stem() string {
51	return proptools.StringDefault(c.properties.Stem, c.Name())
52}
53
54func (c *compatCil) expandSeSources(ctx android.ModuleContext) android.Paths {
55	srcPaths := make(android.Paths, 0, len(c.properties.Srcs))
56	for _, src := range c.properties.Srcs {
57		if m := android.SrcIsModule(src); m != "" {
58			module := ctx.GetDirectDepWithTag(m, android.SourceDepTag)
59			if module == nil {
60				// Error would have been handled by ExtractSourcesDeps
61				continue
62			}
63			if fg, ok := module.(*fileGroup); ok {
64				if c.SystemExtSpecific() {
65					srcPaths = append(srcPaths, fg.SystemExtPrivateSrcs()...)
66				} else {
67					srcPaths = append(srcPaths, fg.SystemPrivateSrcs()...)
68				}
69			} else {
70				ctx.PropertyErrorf("srcs", "%q is not an se_filegroup", m)
71			}
72		} else {
73			srcPaths = append(srcPaths, android.PathForModuleSrc(ctx, src))
74		}
75	}
76	return srcPaths
77}
78
79func (c *compatCil) DepsMutator(ctx android.BottomUpMutatorContext) {
80	android.ExtractSourcesDeps(ctx, c.properties.Srcs)
81}
82
83func (c *compatCil) GenerateAndroidBuildActions(ctx android.ModuleContext) {
84	if c.ProductSpecific() || c.SocSpecific() || c.DeviceSpecific() {
85		ctx.ModuleErrorf("Compat cil files only support system and system_ext partitions")
86	}
87
88	srcPaths := c.expandSeSources(ctx)
89	out := android.PathForModuleGen(ctx, c.Name())
90	ctx.Build(pctx, android.BuildParams{
91		Rule:        android.Cat,
92		Inputs:      srcPaths,
93		Output:      out,
94		Description: "Combining compat cil for " + c.Name(),
95	})
96
97	c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping")
98	c.installSource = out
99	ctx.InstallFile(c.installPath, c.stem(), c.installSource)
100}
101
102func (c *compatCil) AndroidMkEntries() []android.AndroidMkEntries {
103	return []android.AndroidMkEntries{android.AndroidMkEntries{
104		Class:      "ETC",
105		OutputFile: android.OptionalPathForPath(c.installSource),
106		ExtraEntries: []android.AndroidMkExtraEntriesFunc{
107			func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
108				entries.SetPath("LOCAL_MODULE_PATH", c.installPath.ToMakePath())
109				entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
110			},
111		},
112	}}
113}
114