Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 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 | |
| 15 | package selinux |
| 16 | |
| 17 | import ( |
| 18 | "github.com/google/blueprint/proptools" |
| 19 | |
| 20 | "android/soong/android" |
| 21 | ) |
| 22 | |
| 23 | func init() { |
| 24 | android.RegisterModuleType("se_compat_cil", compatCilFactory) |
| 25 | } |
| 26 | |
| 27 | // se_compat_cil collects and installs backwards compatibility cil files. |
| 28 | func compatCilFactory() android.Module { |
| 29 | c := &compatCil{} |
| 30 | c.AddProperties(&c.properties) |
| 31 | android.InitAndroidArchModule(c, android.DeviceSupported, android.MultilibCommon) |
| 32 | return c |
| 33 | } |
| 34 | |
| 35 | type compatCil struct { |
| 36 | android.ModuleBase |
| 37 | properties compatCilProperties |
| 38 | installSource android.Path |
| 39 | installPath android.InstallPath |
| 40 | } |
| 41 | |
| 42 | type compatCilProperties struct { |
| 43 | // List of source files. Can reference se_filegroup type modules with the ":module" syntax. |
Paul Duffin | 532bde1 | 2021-07-09 22:53:03 +0100 | [diff] [blame^] | 44 | Srcs []string `android:"path"` |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 45 | |
| 46 | // Output file name. Defaults to module name if unspecified. |
| 47 | Stem *string |
| 48 | } |
| 49 | |
| 50 | func (c *compatCil) stem() string { |
| 51 | return proptools.StringDefault(c.properties.Stem, c.Name()) |
| 52 | } |
| 53 | |
| 54 | func (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 Duffin | 532bde1 | 2021-07-09 22:53:03 +0100 | [diff] [blame^] | 58 | module := android.GetModuleFromPathDep(ctx, m, "") |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 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 | |
Yi-Yo Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 79 | func (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 Chiang | 41c34d6 | 2021-04-13 02:44:41 +0800 | [diff] [blame] | 86 | 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 | |
| 98 | func (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 | } |