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	// BOARD_PLAT_PUBLIC_SEPOLICY_DIR, BOARD_PLAT_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	odmSrcs    android.Paths
60}
61
62// Source files from system/sepolicy/public
63func (fg *fileGroup) SystemPublicSrcs() android.Paths {
64	return fg.systemPublicSrcs
65}
66
67// Source files from system/sepolicy/private
68func (fg *fileGroup) SystemPrivateSrcs() android.Paths {
69	return fg.systemPrivateSrcs
70}
71
72// Source files from system/sepolicy/vendor
73func (fg *fileGroup) SystemVendorSrcs() android.Paths {
74	return fg.systemVendorSrcs
75}
76
77// Source files from system/sepolicy/reqd_mask
78func (fg *fileGroup) SystemReqdMaskSrcs() android.Paths {
79	return fg.systemReqdMaskSrcs
80}
81
82// Source files from BOARD_PLAT_PUBLIC_SEPOLICY_DIR
83func (fg *fileGroup) SystemExtPublicSrcs() android.Paths {
84	return fg.systemExtPublicSrcs
85}
86
87// Source files from BOARD_PLAT_PRIVATE_SEPOLICY_DIR
88func (fg *fileGroup) SystemExtPrivateSrcs() android.Paths {
89	return fg.systemExtPrivateSrcs
90}
91
92// Source files from PRODUCT_PUBLIC_SEPOLICY_DIRS
93func (fg *fileGroup) ProductPublicSrcs() android.Paths {
94	return fg.productPublicSrcs
95}
96
97// Source files from PRODUCT_PRIVATE_SEPOLICY_DIRS
98func (fg *fileGroup) ProductPrivateSrcs() android.Paths {
99	return fg.productPrivateSrcs
100}
101
102// Source files from BOARD_VENDOR_SEPOLICY_DIRS
103func (fg *fileGroup) VendorSrcs() android.Paths {
104	return fg.vendorSrcs
105}
106
107// Source files from BOARD_ODM_SEPOLICY_DIRS
108func (fg *fileGroup) OdmSrcs() android.Paths {
109	return fg.odmSrcs
110}
111
112func (fg *fileGroup) findSrcsInDirs(ctx android.ModuleContext, dirs []string) android.Paths {
113	result := android.Paths{}
114	for _, f := range fg.properties.Srcs {
115		for _, d := range dirs {
116			path := filepath.Join(d, f)
117			files, _ := ctx.GlobWithDeps(path, nil)
118			for _, f := range files {
119				result = append(result, android.PathForSource(ctx, f))
120			}
121		}
122	}
123	return result
124}
125
126func (fg *fileGroup) findSrcsInDir(ctx android.ModuleContext, dir string) android.Paths {
127	return fg.findSrcsInDirs(ctx, []string{dir})
128}
129
130func (fg *fileGroup) DepsMutator(ctx android.BottomUpMutatorContext) {}
131
132func (fg *fileGroup) GenerateAndroidBuildActions(ctx android.ModuleContext) {
133	fg.systemPublicSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "public"))
134	fg.systemPrivateSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "private"))
135	fg.systemVendorSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "vendor"))
136	fg.systemReqdMaskSrcs = fg.findSrcsInDir(ctx, filepath.Join(ctx.ModuleDir(), "reqd_mask"))
137
138	fg.systemExtPublicSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().PlatPublicSepolicyDirs())
139	fg.systemExtPrivateSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().PlatPrivateSepolicyDirs())
140
141	fg.productPublicSrcs = fg.findSrcsInDirs(ctx, ctx.Config().ProductPublicSepolicyDirs())
142	fg.productPrivateSrcs = fg.findSrcsInDirs(ctx, ctx.Config().ProductPrivateSepolicyDirs())
143
144	fg.vendorSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().VendorSepolicyDirs())
145	fg.odmSrcs = fg.findSrcsInDirs(ctx, ctx.DeviceConfig().OdmSepolicyDirs())
146}
147