blob: a15c65cff8a92897a9927dad7367cfbedc8695e0 [file] [log] [blame]
Inseob Kim619e4a72021-03-15 14:53:55 +09001// 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"
Inseob Kim36d9d392023-09-04 17:40:03 +090019 "path"
Inseob Kim619e4a72021-03-15 14:53:55 +090020 "path/filepath"
Inseob Kim619e4a72021-03-15 14:53:55 +090021 "strings"
22
23 "android/soong/android"
24)
25
26func init() {
27 android.RegisterModuleType("se_build_files", buildFilesFactory)
28}
29
30// se_build_files gathers policy files from sepolicy dirs, and acts like a filegroup. A tag with
31// partition(plat, system_ext, product) and scope(public, private) is used to select directories.
Inseob Kim0a707fa2021-12-09 23:35:11 +090032// Supported tags are: "plat_public", "plat_private", "system_ext_public", "system_ext_private",
33// "product_public", "product_private", and "reqd_mask".
Inseob Kim619e4a72021-03-15 14:53:55 +090034func buildFilesFactory() android.Module {
35 module := &buildFiles{}
36 module.AddProperties(&module.properties)
37 android.InitAndroidModule(module)
38 return module
39}
40
41type buildFilesProperties struct {
42 // list of source file suffixes used to collect selinux policy files.
43 // Source files will be looked up in the following local directories:
44 // system/sepolicy/{public, private, vendor, reqd_mask}
45 // and directories specified by following config variables:
46 // BOARD_SEPOLICY_DIRS, BOARD_ODM_SEPOLICY_DIRS
Yi-Yo Chiang40073d42021-04-12 19:44:53 +080047 // SYSTEM_EXT_PUBLIC_SEPOLICY_DIR, SYSTEM_EXT_PRIVATE_SEPOLICY_DIR
Inseob Kim619e4a72021-03-15 14:53:55 +090048 Srcs []string
49}
50
51type buildFiles struct {
52 android.ModuleBase
53 properties buildFilesProperties
54
55 srcs map[string]android.Paths
56}
57
58func (b *buildFiles) findSrcsInDirs(ctx android.ModuleContext, dirs ...string) android.Paths {
59 result := android.Paths{}
60 for _, file := range b.properties.Srcs {
61 for _, dir := range dirs {
62 path := filepath.Join(dir, file)
63 files, err := ctx.GlobWithDeps(path, nil)
64 if err != nil {
65 ctx.ModuleErrorf("glob: %s", err.Error())
66 }
67 for _, f := range files {
68 result = append(result, android.PathForSource(ctx, f))
69 }
70 }
71 }
72 return result
73}
74
75func (b *buildFiles) DepsMutator(ctx android.BottomUpMutatorContext) {
76 // do nothing
77}
78
79func (b *buildFiles) OutputFiles(tag string) (android.Paths, error) {
80 if paths, ok := b.srcs[tag]; ok {
81 return paths, nil
82 }
83
Cole Faust22f253c2023-03-01 10:59:59 -080084 return nil, fmt.Errorf("unknown tag %q. Supported tags are: %q", tag, strings.Join(android.SortedKeys(b.srcs), " "))
Inseob Kim619e4a72021-03-15 14:53:55 +090085}
86
87var _ android.OutputFileProducer = (*buildFiles)(nil)
88
Inseob Kim619e4a72021-03-15 14:53:55 +090089type sepolicyDir struct {
Inseob Kim0a707fa2021-12-09 23:35:11 +090090 tag string
91 paths []string
Inseob Kim619e4a72021-03-15 14:53:55 +090092}
93
94func (b *buildFiles) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim619e4a72021-03-15 14:53:55 +090095 b.srcs = make(map[string]android.Paths)
Inseob Kim79fdbeb2022-08-12 22:27:35 +090096 b.srcs[".reqd_mask"] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "reqd_mask"))
97 b.srcs[".plat_public"] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "public"))
98 b.srcs[".plat_private"] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "private"))
99 b.srcs[".plat_vendor"] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "vendor"))
Inseob Kim0a707fa2021-12-09 23:35:11 +0900100 b.srcs[".system_ext_public"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPublicSepolicyDirs()...)
101 b.srcs[".system_ext_private"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPrivateSepolicyDirs()...)
102 b.srcs[".product_public"] = b.findSrcsInDirs(ctx, ctx.Config().ProductPublicSepolicyDirs()...)
103 b.srcs[".product_private"] = b.findSrcsInDirs(ctx, ctx.Config().ProductPrivateSepolicyDirs()...)
Inseob Kim3ac62fe2021-12-16 19:00:03 +0900104 b.srcs[".vendor"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().VendorSepolicyDirs()...)
105 b.srcs[".odm"] = b.findSrcsInDirs(ctx, ctx.DeviceConfig().OdmSepolicyDirs()...)
106
Inseob Kim36d9d392023-09-04 17:40:03 +0900107 prebuilt_directories, err := ctx.GlobWithDeps("system/sepolicy/prebuilts/api/*", nil)
108 if err != nil {
109 ctx.ModuleErrorf("error while globbing: %w", err)
110 return
111 }
112
Inseob Kim73f43ff2022-02-14 23:01:04 +0900113 // directories used for compat tests and Treble tests
Inseob Kim36d9d392023-09-04 17:40:03 +0900114 for _, dir := range prebuilt_directories {
115 ver := path.Base(dir)
Inseob Kim79fdbeb2022-08-12 22:27:35 +0900116 b.srcs[".plat_public_"+ver] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "prebuilts", "api", ver, "public"))
117 b.srcs[".plat_private_"+ver] = b.findSrcsInDirs(ctx, filepath.Join("system", "sepolicy", "prebuilts", "api", ver, "private"))
Inseob Kim73f43ff2022-02-14 23:01:04 +0900118 b.srcs[".system_ext_public_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.DeviceConfig().SystemExtSepolicyPrebuiltApiDir(), "prebuilts", "api", ver, "public"))
119 b.srcs[".system_ext_private_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.DeviceConfig().SystemExtSepolicyPrebuiltApiDir(), "prebuilts", "api", ver, "private"))
120 b.srcs[".product_public_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.DeviceConfig().ProductSepolicyPrebuiltApiDir(), "prebuilts", "api", ver, "public"))
121 b.srcs[".product_private_"+ver] = b.findSrcsInDirs(ctx, filepath.Join(ctx.DeviceConfig().ProductSepolicyPrebuiltApiDir(), "prebuilts", "api", ver, "private"))
Inseob Kim16d3be32022-01-07 09:15:27 +0900122 }
Inseob Kim619e4a72021-03-15 14:53:55 +0900123}