blob: 46b0f7100aca68aebbba54ec83c1a8a5e6ef5f14 [file] [log] [blame]
Yi-Yo Chiang41c34d62021-04-13 02:44:41 +08001// 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.
Paul Duffin532bde12021-07-09 22:53:03 +010044 Srcs []string `android:"path"`
Yi-Yo Chiang41c34d62021-04-13 02:44:41 +080045
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 != "" {
Paul Duffin532bde12021-07-09 22:53:03 +010058 module := android.GetModuleFromPathDep(ctx, m, "")
Yi-Yo Chiang41c34d62021-04-13 02:44:41 +080059 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
Yi-Yo Chiang41c34d62021-04-13 02:44:41 +080079func (c *compatCil) GenerateAndroidBuildActions(ctx android.ModuleContext) {
80 if c.ProductSpecific() || c.SocSpecific() || c.DeviceSpecific() {
81 ctx.ModuleErrorf("Compat cil files only support system and system_ext partitions")
82 }
83
84 srcPaths := c.expandSeSources(ctx)
85 out := android.PathForModuleGen(ctx, c.Name())
Yi-Yo Chiang41c34d62021-04-13 02:44:41 +080086 ctx.Build(pctx, android.BuildParams{
87 Rule: android.Cat,
88 Inputs: srcPaths,
89 Output: out,
90 Description: "Combining compat cil for " + c.Name(),
91 })
92
93 c.installPath = android.PathForModuleInstall(ctx, "etc", "selinux", "mapping")
94 c.installSource = out
95 ctx.InstallFile(c.installPath, c.stem(), c.installSource)
96}
97
98func (c *compatCil) AndroidMkEntries() []android.AndroidMkEntries {
99 return []android.AndroidMkEntries{android.AndroidMkEntries{
100 Class: "ETC",
101 OutputFile: android.OptionalPathForPath(c.installSource),
102 ExtraEntries: []android.AndroidMkExtraEntriesFunc{
103 func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) {
104 entries.SetPath("LOCAL_MODULE_PATH", c.installPath.ToMakePath())
105 entries.SetString("LOCAL_INSTALLED_MODULE_STEM", c.stem())
106 },
107 },
108 }}
109}