1// Copyright 2018 Google Inc. All rights reserved.
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	"android/soong/android"
19	"path/filepath"
20)
21
22func init() {
23	android.RegisterModuleType("se_filegroup", FileGroupFactory)
24}
25
26func FileGroupFactory() android.Module {
27	module := &fileGroup{}
28	module.AddProperties(&module.properties)
29	android.InitAndroidModule(module)
30	return module
31}
32
33type fileGroupProperties struct {
34	// list of source file suffixes used to collect selinux policy files.
35	// Source files will be looked up in the following local directories:
36	// system/sepolicy/{public, private, vendor, reqd_mask}
37	// and directories specified by following config variables:
38	// BOARD_SEPOLICY_DIRS, BOARD_ODM_SEPOLICY_DIRS
39	// SYSTEM_EXT_PUBLIC_SEPOLICY_DIR, SYSTEM_EXT_PRIVATE_SEPOLICY_DIR
40	Srcs []string
41}
42
43type fileGroup struct {
44	android.ModuleBase
45	properties fileGroupProperties
46
47	systemPublicSrcs   android.Paths
48	systemPrivateSrcs  android.Paths
49	systemVendorSrcs   android.Paths
50	systemReqdMaskSrcs android.Paths
51
52	systemExtPublicSrcs  android.Paths
53	systemExtPrivateSrcs android.Paths
54
55	productPublicSrcs  android.Paths
56	productPrivateSrcs android.Paths
57
58	vendorSrcs         android.Paths
59	vendorReqdMaskSrcs android.Paths
60	odmSrcs            android.Paths
61}
62
63// Source files from system/sepolicy/public
64func (fg *fileGroup) SystemPublicSrcs() android.Paths {
65	return fg.systemPublicSrcs
66}
67
68// Source files from system/sepolicy/private
69func (fg *fileGroup) SystemPrivateSrcs() android.Paths {
70	return fg.systemPrivateSrcs
71}
72
73// Source files from system/sepolicy/vendor
74func (fg *fileGroup) SystemVendorSrcs() android.Paths {
75	return fg.systemVendorSrcs
76}
77
78// Source files from system/sepolicy/reqd_mask
79func (fg *fileGroup) SystemReqdMaskSrcs() android.Paths {
80	return fg.systemReqdMaskSrcs
81}
82
83// Source files from SYSTEM_EXT_PUBLIC_SEPOLICY_DIR
84func (fg *fileGroup) SystemExtPublicSrcs() android.Paths {
85	return fg.systemExtPublicSrcs
86}
87
88// Source files from SYSTEM_EXT_PRIVATE_SEPOLICY_DIR
89func (fg *fileGroup) SystemExtPrivateSrcs() android.Paths {
90	return fg.systemExtPrivateSrcs
91}
92
93// Source files from PRODUCT_PUBLIC_SEPOLICY_DIRS
94func (fg *fileGroup) ProductPublicSrcs() android.Paths {
95	return fg.productPublicSrcs
96}
97
98// Source files from PRODUCT_PRIVATE_SEPOLICY_DIRS
99func (fg *fileGroup) ProductPrivateSrcs() android.Paths {
100	return fg.productPrivateSrcs
101}
102
103// Source files from BOARD_VENDOR_SEPOLICY_DIRS
104func (fg *fileGroup) VendorSrcs() android.Paths {
105	return fg.vendorSrcs
106}
107
108func (fg *fileGroup) VendorReqdMaskSrcs() android.Paths {
109	return fg.vendorReqdMaskSrcs
110}
111
112// Source files from BOARD_ODM_SEPOLICY_DIRS
113func (fg *fileGroup) OdmSrcs() android.Paths {
114	return fg.odmSrcs
115}
116
117func (fg *fileGroup) findSrcsInDirs(ctx android.ModuleContext, dirs []string) android.Paths {
118	result := android.Paths{}
119	for _, f := range fg.properties.Srcs {
120		for _, d := range dirs {
121			path := filepath.Join(d, f)
122			files, _ := ctx.GlobWithDeps(path, nil)
123			for _, f := range files {
124				result = append(result, android.PathForSource(ctx, f))
125			}
126		}
127	}
128	return result
129}
130
131func (fg *fileGroup) findSrcsInDir(ctx android.ModuleContext, dir string) android.Paths {
132	return fg.findSrcsInDirs(ctx, []string{dir})
133}
134
135func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) {}
136
137func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
138	fg.systemPublicSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "public"))
139	fg.systemPrivateSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "private"))
140	fg.systemVendorSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "vendor"))
141	fg.systemReqdMaskSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "reqd_mask"))
142
143	fg.systemExtPublicSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPublicSepolicyDirs())
144	fg.systemExtPrivateSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().SystemExtPrivateSepolicyDirs())
145
146	fg.productPublicSrcs = fg.findSrcsInDirs(ctx, ctx.Config().ProductPublicSepolicyDirs())
147	fg.productPrivateSrcs = fg.findSrcsInDirs(ctx, ctx.Config().ProductPrivateSepolicyDirs())
148
149	fg.vendorReqdMaskSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().BoardReqdMaskPolicy())
150	fg.vendorSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().VendorSepolicyDirs())
151	fg.odmSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().OdmSepolicyDirs())
152}
153