Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [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 | "fmt" |
| 19 | "path/filepath" |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 20 | "strings" |
| 21 | |
| 22 | "android/soong/android" |
| 23 | ) |
| 24 | |
| 25 | func init() { |
| 26 | android.RegisterModuleType("se_build_files", buildFilesFactory) |
| 27 | } |
| 28 | |
| 29 | // se_build_files gathers policy files from sepolicy dirs, and acts like a filegroup. A tag with |
| 30 | // partition(plat, system_ext, product) and scope(public, private) is used to select directories. |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 31 | // Supported tags are: "plat_public", "plat_private", "system_ext_public", "system_ext_private", |
| 32 | // "product_public", "product_private", and "reqd_mask". |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 33 | func buildFilesFactory() android.Module { |
| 34 | module := &buildFiles{} |
| 35 | module.AddProperties(&module.properties) |
| 36 | android.InitAndroidModule(module) |
| 37 | return module |
| 38 | } |
| 39 | |
| 40 | type buildFilesProperties struct { |
| 41 | // list of source file suffixes used to collect selinux policy files. |
| 42 | // Source files will be looked up in the following local directories: |
| 43 | // system/sepolicy/{public, private, vendor, reqd_mask} |
| 44 | // and directories specified by following config variables: |
| 45 | // BOARD_SEPOLICY_DIRS, BOARD_ODM_SEPOLICY_DIRS |
Yi-Yo Chiang | 40073d4 | 2021-04-12 19:44:53 +0800 | [diff] [blame] | 46 | // SYSTEM_EXT_PUBLIC_SEPOLICY_DIR, SYSTEM_EXT_PRIVATE_SEPOLICY_DIR |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 47 | Srcs []string |
| 48 | } |
| 49 | |
| 50 | type buildFiles struct { |
| 51 | android.ModuleBase |
| 52 | properties buildFilesProperties |
| 53 | |
| 54 | srcs map[string]android.Paths |
| 55 | } |
| 56 | |
| 57 | func (b *buildFiles) findSrcsInDirs(ctx android.ModuleContext, dirs ...string) android.Paths { |
| 58 | result := android.Paths{} |
| 59 | for _, file := range b.properties.Srcs { |
| 60 | for _, dir := range dirs { |
| 61 | path := filepath.Join(dir, file) |
| 62 | files, err := ctx.GlobWithDeps(path, nil) |
| 63 | if err != nil { |
| 64 | ctx.ModuleErrorf("glob: %s", err.Error()) |
| 65 | } |
| 66 | for _, f := range files { |
| 67 | result = append(result, android.PathForSource(ctx, f)) |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | return result |
| 72 | } |
| 73 | |
| 74 | func (b *buildFiles) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 75 | // do nothing |
| 76 | } |
| 77 | |
| 78 | func (b *buildFiles) OutputFiles(tag string) (android.Paths, error) { |
| 79 | if paths, ok := b.srcs[tag]; ok { |
| 80 | return paths, nil |
| 81 | } |
| 82 | |
| 83 | return nil, fmt.Errorf("unknown tag %q. Supported tags are: %q", tag, strings.Join(android.SortedStringKeys(b.srcs), " ")) |
| 84 | } |
| 85 | |
| 86 | var _ android.OutputFileProducer = (*buildFiles)(nil) |
| 87 | |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 88 | type sepolicyDir struct { |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 89 | tag string |
| 90 | paths []string |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | func (b *buildFiles) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 94 | b.srcs = make(map[string]android.Paths) |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 95 | b.srcs[".reqd_mask"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "reqd_mask")) |
| 96 | b.srcs[".plat_public"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "public")) |
| 97 | b.srcs[".plat_private"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "private")) |
Inseob Kim | 3ac62fe | 2021-12-16 19:00:03 +0900 | [diff] [blame] | 98 | b.srcs[".plat_vendor"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "vendor")) |
Inseob Kim | 0a707fa | 2021-12-09 23:35:11 +0900 | [diff] [blame] | 99 | b.srcs[".system_ext_public"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPublicSepolicyDirs()...) |
| 100 | b.srcs[".system_ext_private"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPrivateSepolicyDirs()...) |
| 101 | b.srcs[".product_public"] = b.findSrcsInDirs(ctx, ctx.Config().ProductPublicSepolicyDirs()...) |
| 102 | b.srcs[".product_private"] = b.findSrcsInDirs(ctx, ctx.Config().ProductPrivateSepolicyDirs()...) |
Inseob Kim | 3ac62fe | 2021-12-16 19:00:03 +0900 | [diff] [blame] | 103 | b.srcs[".vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().VendorSepolicyDirs()...) |
| 104 | b.srcs[".odm"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().OdmSepolicyDirs()...) |
| 105 | |
| 106 | if ctx.DeviceConfig().PlatformSepolicyVersion() == ctx.DeviceConfig().BoardSepolicyVers() { |
| 107 | // vendor uses the same source with plat policy |
| 108 | b.srcs[".reqd_mask_for_vendor"] = b.srcs[".reqd_mask"] |
| 109 | b.srcs[".plat_vendor_for_vendor"] = b.srcs[".plat_vendor"] |
| 110 | b.srcs[".plat_public_for_vendor"] = b.srcs[".plat_public"] |
| 111 | b.srcs[".plat_private_for_vendor"] = b.srcs[".plat_private"] |
| 112 | b.srcs[".system_ext_public_for_vendor"] = b.srcs[".system_ext_public"] |
| 113 | b.srcs[".system_ext_private_for_vendor"] = b.srcs[".system_ext_private"] |
| 114 | b.srcs[".product_public_for_vendor"] = b.srcs[".product_public"] |
| 115 | b.srcs[".product_private_for_vendor"] = b.srcs[".product_private"] |
| 116 | } else { |
| 117 | // use vendor-supplied plat prebuilts |
| 118 | b.srcs[".reqd_mask_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardReqdMaskPolicy()...) |
| 119 | b.srcs[".plat_vendor_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardPlatVendorPolicy()...) |
| 120 | b.srcs[".plat_public_for_vendor"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "prebuilts", "api", ctx.DeviceConfig().BoardSepolicyVers(), "public")) |
| 121 | b.srcs[".plat_private_for_vendor"] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "prebuilts", "api", ctx.DeviceConfig().BoardSepolicyVers(), "private")) |
| 122 | b.srcs[".system_ext_public_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardSystemExtPublicPrebuiltDirs()...) |
| 123 | b.srcs[".system_ext_private_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardSystemExtPrivatePrebuiltDirs()...) |
| 124 | b.srcs[".product_public_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardProductPublicPrebuiltDirs()...) |
| 125 | b.srcs[".product_private_for_vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardProductPrivatePrebuiltDirs()...) |
| 126 | } |
Inseob Kim | 16d3be3 | 2022-01-07 09:15:27 +0900 | [diff] [blame^] | 127 | |
| 128 | for _, ver := range ctx.DeviceConfig().PlatformSepolicyCompatVersions() { |
| 129 | b.srcs[".plat_public_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "prebuilts", "api", ver, "public")) |
| 130 | b.srcs[".plat_private_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.ModuleDir(), "prebuilts", "api", ver, "private")) |
| 131 | } |
Inseob Kim | 619e4a7 | 2021-03-15 14:53:55 +0900 | [diff] [blame] | 132 | } |