Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 1 | // Copyright (C) 2019 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 | "io" |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 20 | "os" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 21 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 22 | "github.com/google/blueprint" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 23 | "github.com/google/blueprint/proptools" |
| 24 | |
| 25 | "android/soong/android" |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 26 | "android/soong/sysprop" |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 27 | ) |
| 28 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 29 | type selinuxContextsProperties struct { |
| 30 | // Filenames under sepolicy directories, which will be used to generate contexts file. |
| 31 | Srcs []string `android:"path"` |
| 32 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 33 | // Output file name. Defaults to module name |
| 34 | Stem *string |
| 35 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 36 | Product_variables struct { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 37 | Address_sanitize struct { |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 38 | Srcs []string `android:"path"` |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 42 | // Whether the comments in generated contexts file will be removed or not. |
| 43 | Remove_comment *bool |
| 44 | |
| 45 | // Whether the result context file is sorted with fc_sort or not. |
| 46 | Fc_sort *bool |
| 47 | |
| 48 | // Make this module available when building for recovery |
| 49 | Recovery_available *bool |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 50 | } |
| 51 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 52 | type seappProperties struct { |
| 53 | // Files containing neverallow rules. |
| 54 | Neverallow_files []string `android:"path"` |
| 55 | |
| 56 | // Precompiled sepolicy binary file which will be fed to checkseapp. |
| 57 | Sepolicy *string `android:"path"` |
| 58 | } |
| 59 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 60 | type selinuxContextsModule struct { |
| 61 | android.ModuleBase |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 62 | android.DefaultableModuleBase |
| 63 | flaggableModuleBase |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 64 | |
Jooyung Han | 804e234 | 2023-06-20 15:38:50 +0900 | [diff] [blame] | 65 | properties selinuxContextsProperties |
| 66 | seappProperties seappProperties |
| 67 | build func(ctx android.ModuleContext, inputs android.Paths) android.Path |
| 68 | deps func(ctx android.BottomUpMutatorContext) |
| 69 | outputPath android.Path |
| 70 | installPath android.InstallPath |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 71 | } |
| 72 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 73 | var _ flaggableModule = (*selinuxContextsModule)(nil) |
| 74 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 75 | var ( |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 76 | reuseContextsDepTag = dependencyTag{name: "reuseContexts"} |
| 77 | syspropLibraryDepTag = dependencyTag{name: "sysprop_library"} |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 78 | ) |
| 79 | |
| 80 | func init() { |
| 81 | pctx.HostBinToolVariable("fc_sort", "fc_sort") |
| 82 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 83 | android.RegisterModuleType("contexts_defaults", contextsDefaultsFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 84 | android.RegisterModuleType("file_contexts", fileFactory) |
| 85 | android.RegisterModuleType("hwservice_contexts", hwServiceFactory) |
| 86 | android.RegisterModuleType("property_contexts", propertyFactory) |
| 87 | android.RegisterModuleType("service_contexts", serviceFactory) |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 88 | android.RegisterModuleType("keystore2_key_contexts", keystoreKeyFactory) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 89 | android.RegisterModuleType("seapp_contexts", seappFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 90 | android.RegisterModuleType("vndservice_contexts", vndServiceFactory) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 91 | |
| 92 | android.RegisterModuleType("file_contexts_test", fileContextsTestFactory) |
| 93 | android.RegisterModuleType("property_contexts_test", propertyContextsTestFactory) |
| 94 | android.RegisterModuleType("hwservice_contexts_test", hwserviceContextsTestFactory) |
| 95 | android.RegisterModuleType("service_contexts_test", serviceContextsTestFactory) |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 96 | android.RegisterModuleType("vndservice_contexts_test", vndServiceContextsTestFactory) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 97 | } |
| 98 | |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 99 | func (m *selinuxContextsModule) InstallInRoot() bool { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 100 | return m.InRecovery() |
| 101 | } |
| 102 | |
| 103 | func (m *selinuxContextsModule) InstallInRecovery() bool { |
| 104 | // ModuleBase.InRecovery() checks the image variant |
| 105 | return m.InRecovery() |
| 106 | } |
| 107 | |
| 108 | func (m *selinuxContextsModule) onlyInRecovery() bool { |
| 109 | // ModuleBase.InstallInRecovery() checks commonProperties.Recovery property |
| 110 | return m.ModuleBase.InstallInRecovery() |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 113 | func (m *selinuxContextsModule) DepsMutator(ctx android.BottomUpMutatorContext) { |
| 114 | if m.deps != nil { |
| 115 | m.deps(ctx) |
| 116 | } |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 117 | |
| 118 | if m.InRecovery() && !m.onlyInRecovery() { |
| 119 | ctx.AddFarVariationDependencies([]blueprint.Variation{ |
| 120 | {Mutator: "image", Variation: android.CoreVariation}, |
| 121 | }, reuseContextsDepTag, ctx.ModuleName()) |
| 122 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | func (m *selinuxContextsModule) propertyContextsDeps(ctx android.BottomUpMutatorContext) { |
| 126 | for _, lib := range sysprop.SyspropLibraries(ctx.Config()) { |
| 127 | ctx.AddFarVariationDependencies([]blueprint.Variation{}, syspropLibraryDepTag, lib) |
| 128 | } |
| 129 | } |
| 130 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 131 | func (m *selinuxContextsModule) stem() string { |
| 132 | return proptools.StringDefault(m.properties.Stem, m.Name()) |
| 133 | } |
| 134 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 135 | func (m *selinuxContextsModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 136 | if m.InRecovery() { |
Colin Cross | 040f151 | 2019-10-02 10:36:09 -0700 | [diff] [blame] | 137 | // Installing context files at the root of the recovery partition |
| 138 | m.installPath = android.PathForModuleInstall(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 139 | } else { |
| 140 | m.installPath = android.PathForModuleInstall(ctx, "etc", "selinux") |
| 141 | } |
| 142 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 143 | if m.InRecovery() && !m.onlyInRecovery() { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 144 | dep := ctx.GetDirectDepWithTag(m.Name(), reuseContextsDepTag) |
| 145 | |
| 146 | if reuseDeps, ok := dep.(*selinuxContextsModule); ok { |
| 147 | m.outputPath = reuseDeps.outputPath |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 148 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 149 | return |
| 150 | } |
| 151 | } |
| 152 | |
Inseob Kim | 6d3d5a6 | 2021-12-21 20:55:32 +0900 | [diff] [blame] | 153 | m.outputPath = m.build(ctx, android.PathsForModuleSrc(ctx, m.properties.Srcs)) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 154 | ctx.InstallFile(m.installPath, m.stem(), m.outputPath) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | func newModule() *selinuxContextsModule { |
| 158 | m := &selinuxContextsModule{} |
| 159 | m.AddProperties( |
| 160 | &m.properties, |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 161 | &m.seappProperties, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 162 | ) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 163 | initFlaggableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 164 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 165 | android.InitDefaultableModule(m) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 166 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 167 | m.selinuxContextsHook(ctx) |
| 168 | }) |
| 169 | return m |
| 170 | } |
| 171 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 172 | type contextsDefaults struct { |
| 173 | android.ModuleBase |
| 174 | android.DefaultsModuleBase |
| 175 | } |
| 176 | |
| 177 | // contexts_defaults provides a set of properties that can be inherited by other contexts modules. |
| 178 | // (file_contexts, property_contexts, seapp_contexts, etc.) A module can use the properties from a |
| 179 | // contexts_defaults using `defaults: ["<:default_module_name>"]`. Properties of both modules are |
| 180 | // erged (when possible) by prepending the default module's values to the depending module's values. |
| 181 | func contextsDefaultsFactory() android.Module { |
| 182 | m := &contextsDefaults{} |
| 183 | m.AddProperties( |
| 184 | &selinuxContextsProperties{}, |
| 185 | &seappProperties{}, |
| 186 | &flagsProperties{}, |
| 187 | ) |
| 188 | android.InitDefaultsModule(m) |
| 189 | return m |
| 190 | } |
| 191 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 192 | func (m *selinuxContextsModule) selinuxContextsHook(ctx android.LoadHookContext) { |
| 193 | // TODO: clean this up to use build/soong/android/variable.go after b/79249983 |
| 194 | var srcs []string |
| 195 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 196 | for _, sanitize := range ctx.Config().SanitizeDevice() { |
| 197 | if sanitize == "address" { |
| 198 | srcs = append(srcs, m.properties.Product_variables.Address_sanitize.Srcs...) |
| 199 | break |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | m.properties.Srcs = append(m.properties.Srcs, srcs...) |
| 204 | } |
| 205 | |
| 206 | func (m *selinuxContextsModule) AndroidMk() android.AndroidMkData { |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 207 | nameSuffix := "" |
| 208 | if m.InRecovery() && !m.onlyInRecovery() { |
| 209 | nameSuffix = ".recovery" |
| 210 | } |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 211 | return android.AndroidMkData{ |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 212 | Class: "ETC", |
| 213 | OutputFile: android.OptionalPathForPath(m.outputPath), |
| 214 | SubName: nameSuffix, |
| 215 | Extra: []android.AndroidMkExtraFunc{ |
| 216 | func(w io.Writer, outputFile android.Path) { |
Colin Cross | 6c7f937 | 2022-01-11 19:35:43 -0800 | [diff] [blame] | 217 | fmt.Fprintln(w, "LOCAL_MODULE_PATH :=", m.installPath.String()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 218 | fmt.Fprintln(w, "LOCAL_INSTALLED_MODULE_STEM :=", m.stem()) |
Colin Cross | f82aed0 | 2021-11-04 17:25:55 -0700 | [diff] [blame] | 219 | }, |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 220 | }, |
| 221 | } |
| 222 | } |
| 223 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 224 | func (m *selinuxContextsModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 225 | if proptools.Bool(m.properties.Recovery_available) && m.ModuleBase.InstallInRecovery() { |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 226 | ctx.PropertyErrorf("recovery_available", |
| 227 | "doesn't make sense at the same time as `recovery: true`") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 228 | } |
| 229 | } |
| 230 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 231 | func (m *selinuxContextsModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 232 | return !m.ModuleBase.InstallInRecovery() |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | func (m *selinuxContextsModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 236 | return false |
| 237 | } |
| 238 | |
| 239 | func (m *selinuxContextsModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 240 | return false |
| 241 | } |
| 242 | |
Inseob Kim | 6cc75f4 | 2021-04-29 13:53:20 +0000 | [diff] [blame] | 243 | func (m *selinuxContextsModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 244 | return false |
| 245 | } |
| 246 | |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 247 | func (m *selinuxContextsModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 248 | return m.ModuleBase.InstallInRecovery() || proptools.Bool(m.properties.Recovery_available) |
Inseob Kim | fa6fe47 | 2021-01-12 13:40:27 +0900 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | func (m *selinuxContextsModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 252 | return nil |
| 253 | } |
| 254 | |
| 255 | func (m *selinuxContextsModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 256 | } |
| 257 | |
| 258 | var _ android.ImageInterface = (*selinuxContextsModule)(nil) |
| 259 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 260 | func (m *selinuxContextsModule) buildGeneralContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 261 | builtContext := pathForModuleOut(ctx, ctx.ModuleName()+"_m4out") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 262 | |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 263 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 264 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 265 | newlineFile := pathForModuleOut(ctx, "newline") |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 266 | |
| 267 | rule.Command().Text("echo").FlagWithOutput("> ", newlineFile) |
| 268 | rule.Temporary(newlineFile) |
| 269 | |
| 270 | var inputsWithNewline android.Paths |
| 271 | for _, input := range inputs { |
| 272 | inputsWithNewline = append(inputsWithNewline, input, newlineFile) |
| 273 | } |
| 274 | |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 275 | flags := m.getBuildFlags(ctx) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 276 | rule.Command(). |
Dan Willemsen | 3c3e59b | 2019-06-19 10:52:50 -0700 | [diff] [blame] | 277 | Tool(ctx.Config().PrebuiltBuildTool(ctx, "m4")). |
| 278 | Text("--fatal-warnings -s"). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 279 | FlagForEachArg("-D", ctx.DeviceConfig().SepolicyM4Defs()). |
Inseob Kim | 6cd0ddd | 2023-10-25 23:48:16 +0900 | [diff] [blame^] | 280 | Flags(flagsToM4Macros(flags)). |
Inseob Kim | 35e9d41 | 2023-01-04 15:27:32 +0900 | [diff] [blame] | 281 | Inputs(inputsWithNewline). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 282 | FlagWithOutput("> ", builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 283 | |
| 284 | if proptools.Bool(m.properties.Remove_comment) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 285 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 286 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 287 | remove_comment_output := pathForModuleOut(ctx, ctx.ModuleName()+"_remove_comment") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 288 | |
| 289 | rule.Command(). |
| 290 | Text("sed -e 's/#.*$//' -e '/^$/d'"). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 291 | Input(builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 292 | FlagWithOutput("> ", remove_comment_output) |
| 293 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 294 | builtContext = remove_comment_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | if proptools.Bool(m.properties.Fc_sort) { |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 298 | rule.Temporary(builtContext) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 299 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 300 | sorted_output := pathForModuleOut(ctx, ctx.ModuleName()+"_sorted") |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 301 | |
| 302 | rule.Command(). |
| 303 | Tool(ctx.Config().HostToolPath(ctx, "fc_sort")). |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 304 | FlagWithInput("-i ", builtContext). |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 305 | FlagWithOutput("-o ", sorted_output) |
| 306 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 307 | builtContext = sorted_output |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 308 | } |
| 309 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 310 | ret := pathForModuleOut(ctx, m.stem()) |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 311 | rule.Temporary(builtContext) |
| 312 | rule.Command().Text("cp").Input(builtContext).Output(ret) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 313 | |
| 314 | rule.DeleteTemporaryFiles() |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 315 | rule.Build("selinux_contexts", "building contexts: "+m.Name()) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 316 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 317 | return ret |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 318 | } |
| 319 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 320 | func (m *selinuxContextsModule) buildFileContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | dfa4a48 | 2023-11-01 17:58:18 +0900 | [diff] [blame] | 321 | if m.properties.Remove_comment == nil { |
| 322 | m.properties.Remove_comment = proptools.BoolPtr(true) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 323 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 324 | return m.buildGeneralContexts(ctx, inputs) |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | func fileFactory() android.Module { |
| 328 | m := newModule() |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 329 | m.build = m.buildFileContexts |
| 330 | return m |
| 331 | } |
| 332 | |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 333 | func (m *selinuxContextsModule) buildServiceContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 334 | if m.properties.Remove_comment == nil { |
| 335 | m.properties.Remove_comment = proptools.BoolPtr(true) |
| 336 | } |
| 337 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 338 | return m.buildGeneralContexts(ctx, inputs) |
| 339 | } |
| 340 | |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 341 | func (m *selinuxContextsModule) checkVendorPropertyNamespace(ctx android.ModuleContext, inputs android.Paths) android.Paths { |
| 342 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 343 | ApiLevelR := android.ApiLevelOrPanic(ctx, "R") |
| 344 | |
| 345 | rule := android.NewRuleBuilder(pctx, ctx) |
| 346 | |
| 347 | // This list is from vts_treble_sys_prop_test. |
| 348 | allowedPropertyPrefixes := []string{ |
| 349 | "ctl.odm.", |
| 350 | "ctl.vendor.", |
| 351 | "ctl.start$odm.", |
| 352 | "ctl.start$vendor.", |
| 353 | "ctl.stop$odm.", |
| 354 | "ctl.stop$vendor.", |
| 355 | "init.svc.odm.", |
| 356 | "init.svc.vendor.", |
| 357 | "ro.boot.", |
| 358 | "ro.hardware.", |
| 359 | "ro.odm.", |
| 360 | "ro.vendor.", |
| 361 | "odm.", |
| 362 | "persist.odm.", |
| 363 | "persist.vendor.", |
| 364 | "vendor.", |
| 365 | } |
| 366 | |
| 367 | // persist.camera is also allowed for devices launching with R or eariler |
| 368 | if shippingApiLevel.LessThanOrEqualTo(ApiLevelR) { |
| 369 | allowedPropertyPrefixes = append(allowedPropertyPrefixes, "persist.camera.") |
| 370 | } |
| 371 | |
| 372 | var allowedContextPrefixes []string |
| 373 | |
| 374 | if shippingApiLevel.GreaterThanOrEqualTo(ApiLevelR) { |
| 375 | // This list is from vts_treble_sys_prop_test. |
| 376 | allowedContextPrefixes = []string{ |
| 377 | "vendor_", |
| 378 | "odm_", |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | var ret android.Paths |
| 383 | for _, input := range inputs { |
| 384 | cmd := rule.Command(). |
| 385 | BuiltTool("check_prop_prefix"). |
| 386 | FlagWithInput("--property-contexts ", input). |
| 387 | FlagForEachArg("--allowed-property-prefix ", proptools.ShellEscapeList(allowedPropertyPrefixes)). // contains shell special character '$' |
| 388 | FlagForEachArg("--allowed-context-prefix ", allowedContextPrefixes) |
| 389 | |
| 390 | if !ctx.DeviceConfig().BuildBrokenVendorPropertyNamespace() { |
| 391 | cmd.Flag("--strict") |
| 392 | } |
| 393 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 394 | out := pathForModuleOut(ctx, "namespace_checked").Join(ctx, input.String()) |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 395 | rule.Command().Text("cp -f").Input(input).Output(out) |
| 396 | ret = append(ret, out) |
| 397 | } |
| 398 | rule.Build("check_namespace", "checking namespace of "+ctx.ModuleName()) |
| 399 | return ret |
| 400 | } |
| 401 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 402 | func (m *selinuxContextsModule) buildPropertyContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 2bcc045 | 2020-12-21 13:16:44 +0900 | [diff] [blame] | 403 | // vendor/odm properties are enforced for devices launching with Android Q or later. So, if |
| 404 | // vendor/odm, make sure that only vendor/odm properties exist. |
| 405 | shippingApiLevel := ctx.DeviceConfig().ShippingApiLevel() |
| 406 | ApiLevelQ := android.ApiLevelOrPanic(ctx, "Q") |
| 407 | if (ctx.SocSpecific() || ctx.DeviceSpecific()) && shippingApiLevel.GreaterThanOrEqualTo(ApiLevelQ) { |
| 408 | inputs = m.checkVendorPropertyNamespace(ctx, inputs) |
| 409 | } |
| 410 | |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 411 | builtCtxFile := m.buildGeneralContexts(ctx, inputs) |
| 412 | |
| 413 | var apiFiles android.Paths |
| 414 | ctx.VisitDirectDepsWithTag(syspropLibraryDepTag, func(c android.Module) { |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 415 | i, ok := c.(interface{ CurrentSyspropApiFile() android.OptionalPath }) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 416 | if !ok { |
| 417 | panic(fmt.Errorf("unknown dependency %q for %q", ctx.OtherModuleName(c), ctx.ModuleName())) |
| 418 | } |
Inseob Kim | 3a3539a | 2021-01-15 18:10:29 +0900 | [diff] [blame] | 419 | if api := i.CurrentSyspropApiFile(); api.Valid() { |
| 420 | apiFiles = append(apiFiles, api.Path()) |
| 421 | } |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 422 | }) |
| 423 | |
| 424 | // check compatibility with sysprop_library |
| 425 | if len(apiFiles) > 0 { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 426 | out := pathForModuleOut(ctx, ctx.ModuleName()+"_api_checked") |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 427 | rule := android.NewRuleBuilder(pctx, ctx) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 428 | |
| 429 | msg := `\n******************************\n` + |
| 430 | `API of sysprop_library doesn't match with property_contexts\n` + |
| 431 | `Please fix the breakage and rebuild.\n` + |
| 432 | `******************************\n` |
| 433 | |
| 434 | rule.Command(). |
| 435 | Text("( "). |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 436 | BuiltTool("sysprop_type_checker"). |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 437 | FlagForEachInput("--api ", apiFiles). |
| 438 | FlagWithInput("--context ", builtCtxFile). |
| 439 | Text(" || ( echo").Flag("-e"). |
| 440 | Flag(`"` + msg + `"`). |
| 441 | Text("; exit 38) )") |
| 442 | |
| 443 | rule.Command().Text("cp -f").Input(builtCtxFile).Output(out) |
Colin Cross | 242c8bc | 2020-11-16 17:58:17 -0800 | [diff] [blame] | 444 | rule.Build("property_contexts_check_api", "checking API: "+m.Name()) |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 445 | builtCtxFile = out |
| 446 | } |
| 447 | |
| 448 | return builtCtxFile |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 449 | } |
| 450 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 451 | func (m *selinuxContextsModule) shouldCheckCoredomain(ctx android.ModuleContext) bool { |
| 452 | if !ctx.SocSpecific() && !ctx.DeviceSpecific() { |
| 453 | return false |
| 454 | } |
| 455 | |
| 456 | return ctx.DeviceConfig().CheckVendorSeappViolations() |
| 457 | } |
| 458 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 459 | func (m *selinuxContextsModule) buildSeappContexts(ctx android.ModuleContext, inputs android.Paths) android.Path { |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 460 | neverallowFile := pathForModuleOut(ctx, "neverallow") |
| 461 | ret := pathForModuleOut(ctx, m.stem()) |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 462 | |
| 463 | rule := android.NewRuleBuilder(pctx, ctx) |
| 464 | rule.Command().Text("(grep"). |
| 465 | Flag("-ihe"). |
| 466 | Text("'^neverallow'"). |
| 467 | Inputs(android.PathsForModuleSrc(ctx, m.seappProperties.Neverallow_files)). |
| 468 | Text(os.DevNull). // to make grep happy even when Neverallow_files is empty |
| 469 | Text(">"). |
| 470 | Output(neverallowFile). |
| 471 | Text("|| true)") // to make ninja happy even when result is empty |
| 472 | |
| 473 | rule.Temporary(neverallowFile) |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 474 | checkCmd := rule.Command().BuiltTool("checkseapp"). |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 475 | FlagWithInput("-p ", android.PathForModuleSrc(ctx, proptools.String(m.seappProperties.Sepolicy))). |
| 476 | FlagWithOutput("-o ", ret). |
| 477 | Inputs(inputs). |
| 478 | Input(neverallowFile) |
| 479 | |
Inseob Kim | 06518b1 | 2023-08-25 21:20:08 +0900 | [diff] [blame] | 480 | if m.shouldCheckCoredomain(ctx) { |
| 481 | checkCmd.Flag("-c") // check coredomain for vendor contexts |
Inseob Kim | d7d3609 | 2023-06-26 20:48:48 +0900 | [diff] [blame] | 482 | } |
| 483 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 484 | rule.Build("seapp_contexts", "Building seapp_contexts: "+m.Name()) |
| 485 | return ret |
| 486 | } |
| 487 | |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 488 | func hwServiceFactory() android.Module { |
| 489 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 490 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 491 | return m |
| 492 | } |
| 493 | |
| 494 | func propertyFactory() android.Module { |
| 495 | m := newModule() |
Inseob Kim | cd61649 | 2020-03-24 23:06:40 +0900 | [diff] [blame] | 496 | m.build = m.buildPropertyContexts |
| 497 | m.deps = m.propertyContextsDeps |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 498 | return m |
| 499 | } |
| 500 | |
| 501 | func serviceFactory() android.Module { |
| 502 | m := newModule() |
Thiébaud Weksteen | 74482f5 | 2023-04-26 13:46:59 +1000 | [diff] [blame] | 503 | m.build = m.buildServiceContexts |
Inseob Kim | b554e59 | 2019-04-15 20:10:46 +0900 | [diff] [blame] | 504 | return m |
| 505 | } |
Janis Danisevskis | c40681f | 2020-07-25 13:02:29 -0700 | [diff] [blame] | 506 | |
| 507 | func keystoreKeyFactory() android.Module { |
| 508 | m := newModule() |
| 509 | m.build = m.buildGeneralContexts |
| 510 | return m |
| 511 | } |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 512 | |
Inseob Kim | 2dac267 | 2021-12-29 17:54:57 +0900 | [diff] [blame] | 513 | func seappFactory() android.Module { |
| 514 | m := newModule() |
| 515 | m.build = m.buildSeappContexts |
| 516 | return m |
| 517 | } |
| 518 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 519 | func vndServiceFactory() android.Module { |
| 520 | m := newModule() |
| 521 | m.build = m.buildGeneralContexts |
| 522 | android.AddLoadHook(m, func(ctx android.LoadHookContext) { |
| 523 | if !ctx.SocSpecific() { |
| 524 | ctx.ModuleErrorf(m.Name(), "must set vendor: true") |
| 525 | return |
| 526 | } |
| 527 | }) |
| 528 | return m |
| 529 | } |
| 530 | |
Yuntao Xu | 42e732c | 2021-11-18 22:33:02 +0000 | [diff] [blame] | 531 | var _ android.OutputFileProducer = (*selinuxContextsModule)(nil) |
| 532 | |
| 533 | // Implements android.OutputFileProducer |
| 534 | func (m *selinuxContextsModule) OutputFiles(tag string) (android.Paths, error) { |
| 535 | if tag == "" { |
| 536 | return []android.Path{m.outputPath}, nil |
| 537 | } |
| 538 | return nil, fmt.Errorf("unsupported module reference tag %q", tag) |
| 539 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 540 | |
| 541 | type contextsTestProperties struct { |
| 542 | // Contexts files to be tested. |
| 543 | Srcs []string `android:"path"` |
| 544 | |
| 545 | // Precompiled sepolicy binary to be tesed together. |
| 546 | Sepolicy *string `android:"path"` |
| 547 | } |
| 548 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 549 | type fileContextsTestProperties struct { |
| 550 | // Test data. File passed to `checkfc -t` to validate how contexts are resolved. |
| 551 | Test_data *string `android:"path"` |
| 552 | } |
| 553 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 554 | type contextsTestModule struct { |
| 555 | android.ModuleBase |
| 556 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 557 | // The type of context. |
| 558 | context contextType |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 559 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 560 | properties contextsTestProperties |
| 561 | fileProperties fileContextsTestProperties |
| 562 | testTimestamp android.OutputPath |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 565 | type contextType int |
| 566 | |
| 567 | const ( |
| 568 | FileContext contextType = iota |
| 569 | PropertyContext |
| 570 | ServiceContext |
| 571 | HwServiceContext |
| 572 | VndServiceContext |
| 573 | ) |
| 574 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 575 | // checkfc parses a context file and checks for syntax errors. |
| 576 | // If -s is specified, the service backend is used to verify binder services. |
| 577 | // If -l is specified, the service backend is used to verify hwbinder services. |
| 578 | // Otherwise, context_file is assumed to be a file_contexts file |
| 579 | // If -e is specified, then the context_file is allowed to be empty. |
| 580 | |
| 581 | // file_contexts_test tests given file_contexts files with checkfc. |
| 582 | func fileContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 583 | m := &contextsTestModule{context: FileContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 584 | m.AddProperties(&m.properties) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 585 | m.AddProperties(&m.fileProperties) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 586 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 587 | return m |
| 588 | } |
| 589 | |
| 590 | // property_contexts_test tests given property_contexts files with property_info_checker. |
| 591 | func propertyContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 592 | m := &contextsTestModule{context: PropertyContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 593 | m.AddProperties(&m.properties) |
| 594 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 595 | return m |
| 596 | } |
| 597 | |
| 598 | // hwservice_contexts_test tests given hwservice_contexts files with checkfc. |
| 599 | func hwserviceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 600 | m := &contextsTestModule{context: HwServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 601 | m.AddProperties(&m.properties) |
| 602 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 603 | return m |
| 604 | } |
| 605 | |
| 606 | // service_contexts_test tests given service_contexts files with checkfc. |
| 607 | func serviceContextsTestFactory() android.Module { |
| 608 | // checkfc -s: service_contexts test |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 609 | m := &contextsTestModule{context: ServiceContext} |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 610 | m.AddProperties(&m.properties) |
| 611 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 612 | return m |
| 613 | } |
| 614 | |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 615 | // vndservice_contexts_test tests given vndservice_contexts files with checkfc. |
| 616 | func vndServiceContextsTestFactory() android.Module { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 617 | m := &contextsTestModule{context: VndServiceContext} |
Inseob Kim | c7596c4 | 2022-02-25 11:45:41 +0900 | [diff] [blame] | 618 | m.AddProperties(&m.properties) |
| 619 | android.InitAndroidArchModule(m, android.DeviceSupported, android.MultilibCommon) |
| 620 | return m |
| 621 | } |
| 622 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 623 | func (m *contextsTestModule) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 624 | tool := "checkfc" |
| 625 | if m.context == PropertyContext { |
| 626 | tool = "property_info_checker" |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | if len(m.properties.Srcs) == 0 { |
| 630 | ctx.PropertyErrorf("srcs", "can't be empty") |
| 631 | return |
| 632 | } |
| 633 | |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 634 | validateWithPolicy := true |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 635 | if proptools.String(m.properties.Sepolicy) == "" { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 636 | if m.context == FileContext { |
| 637 | if proptools.String(m.fileProperties.Test_data) == "" { |
| 638 | ctx.PropertyErrorf("test_data", "Either test_data or sepolicy should be provided") |
| 639 | return |
| 640 | } |
| 641 | validateWithPolicy = false |
| 642 | } else { |
| 643 | ctx.PropertyErrorf("sepolicy", "can't be empty") |
| 644 | return |
| 645 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 648 | flags := []string(nil) |
| 649 | switch m.context { |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 650 | case FileContext: |
| 651 | if !validateWithPolicy { |
| 652 | flags = []string{"-t"} |
| 653 | } |
Thiébaud Weksteen | a69e14f | 2023-10-20 13:31:19 +1100 | [diff] [blame] | 654 | case ServiceContext: |
| 655 | flags = []string{"-s" /* binder services */} |
| 656 | case HwServiceContext: |
| 657 | flags = []string{"-e" /* allow empty */, "-l" /* hwbinder services */} |
| 658 | case VndServiceContext: |
| 659 | flags = []string{"-e" /* allow empty */, "-v" /* vnd service */} |
| 660 | } |
| 661 | |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 662 | srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs) |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 663 | rule := android.NewRuleBuilder(pctx, ctx) |
Thiébaud Weksteen | b6e7430 | 2023-10-20 15:36:15 +1100 | [diff] [blame] | 664 | |
| 665 | if validateWithPolicy { |
| 666 | sepolicy := android.PathForModuleSrc(ctx, proptools.String(m.properties.Sepolicy)) |
| 667 | rule.Command().BuiltTool(tool). |
| 668 | Flags(flags). |
| 669 | Input(sepolicy). |
| 670 | Inputs(srcs) |
| 671 | } else { |
| 672 | test_data := android.PathForModuleSrc(ctx, proptools.String(m.fileProperties.Test_data)) |
| 673 | rule.Command().BuiltTool(tool). |
| 674 | Flags(flags). |
| 675 | Inputs(srcs). |
| 676 | Input(test_data) |
| 677 | } |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 678 | |
Inseob Kim | 6c6f53b | 2023-04-26 11:03:35 +0900 | [diff] [blame] | 679 | m.testTimestamp = pathForModuleOut(ctx, "timestamp") |
Inseob Kim | b5e2353 | 2022-02-16 02:26:11 +0000 | [diff] [blame] | 680 | rule.Command().Text("touch").Output(m.testTimestamp) |
| 681 | rule.Build("contexts_test", "running contexts test: "+ctx.ModuleName()) |
| 682 | } |
| 683 | |
| 684 | func (m *contextsTestModule) AndroidMkEntries() []android.AndroidMkEntries { |
| 685 | return []android.AndroidMkEntries{android.AndroidMkEntries{ |
| 686 | Class: "FAKE", |
| 687 | // OutputFile is needed, even though BUILD_PHONY_PACKAGE doesn't use it. |
| 688 | // Without OutputFile this module won't be exported to Makefile. |
| 689 | OutputFile: android.OptionalPathForPath(m.testTimestamp), |
| 690 | Include: "$(BUILD_PHONY_PACKAGE)", |
| 691 | ExtraEntries: []android.AndroidMkExtraEntriesFunc{ |
| 692 | func(ctx android.AndroidMkExtraEntriesContext, entries *android.AndroidMkEntries) { |
| 693 | entries.SetString("LOCAL_ADDITIONAL_DEPENDENCIES", m.testTimestamp.String()) |
| 694 | }, |
| 695 | }, |
| 696 | }} |
| 697 | } |
| 698 | |
| 699 | // contextsTestModule implements ImageInterface to be able to include recovery_available contexts |
| 700 | // modules as its sources. |
| 701 | func (m *contextsTestModule) ImageMutatorBegin(ctx android.BaseModuleContext) { |
| 702 | } |
| 703 | |
| 704 | func (m *contextsTestModule) CoreVariantNeeded(ctx android.BaseModuleContext) bool { |
| 705 | return true |
| 706 | } |
| 707 | |
| 708 | func (m *contextsTestModule) RamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 709 | return false |
| 710 | } |
| 711 | |
| 712 | func (m *contextsTestModule) VendorRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 713 | return false |
| 714 | } |
| 715 | |
| 716 | func (m *contextsTestModule) DebugRamdiskVariantNeeded(ctx android.BaseModuleContext) bool { |
| 717 | return false |
| 718 | } |
| 719 | |
| 720 | func (m *contextsTestModule) RecoveryVariantNeeded(ctx android.BaseModuleContext) bool { |
| 721 | return false |
| 722 | } |
| 723 | |
| 724 | func (m *contextsTestModule) ExtraImageVariations(ctx android.BaseModuleContext) []string { |
| 725 | return nil |
| 726 | } |
| 727 | |
| 728 | func (m *contextsTestModule) SetImageVariation(ctx android.BaseModuleContext, variation string, module android.Module) { |
| 729 | } |
| 730 | |
| 731 | var _ android.ImageInterface = (*contextsTestModule)(nil) |