blob: 25fbc41d3b5d1850de3583c76951bd6b5de89729 [file] [log] [blame]
Inseob Kimc0907f12019-02-08 21:00:45 +09001// 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
Inseob Kim07def122020-11-23 14:43:02 +090015// sysprop package defines a module named sysprop_library that can implement sysprop as API
16// See https://source.android.com/devices/architecture/sysprops-apis for details
Inseob Kimc0907f12019-02-08 21:00:45 +090017package sysprop
18
19import (
Inseob Kim42882742019-07-30 17:55:33 +090020 "fmt"
21 "io"
Inseob Kimc9770d62021-01-15 18:04:20 +090022 "os"
Inseob Kim42882742019-07-30 17:55:33 +090023 "path"
Andrew Walbrana5deb732024-02-15 13:39:46 +000024 "strings"
Inseob Kim628d7ef2020-03-21 03:38:32 +090025 "sync"
Colin Crossf8b860a2019-04-16 14:43:28 -070026
Inseob Kimc0907f12019-02-08 21:00:45 +090027 "github.com/google/blueprint"
28 "github.com/google/blueprint/proptools"
Inseob Kim42882742019-07-30 17:55:33 +090029
30 "android/soong/android"
31 "android/soong/cc"
32 "android/soong/java"
Andrew Walbrana5deb732024-02-15 13:39:46 +000033 "android/soong/rust"
Inseob Kimc0907f12019-02-08 21:00:45 +090034)
35
36type dependencyTag struct {
37 blueprint.BaseDependencyTag
38 name string
39}
40
Inseob Kim988f53c2019-09-16 15:59:01 +090041type syspropGenProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -080042 Srcs []string `android:"path"`
43 Scope string
44 Name *string
45 Check_api *string
Inseob Kim988f53c2019-09-16 15:59:01 +090046}
47
48type syspropJavaGenRule struct {
49 android.ModuleBase
50
51 properties syspropGenProperties
Inseob Kim988f53c2019-09-16 15:59:01 +090052}
53
Andrew Walbrana5deb732024-02-15 13:39:46 +000054type syspropRustGenRule struct {
Andrew Walbranacd75d22024-03-12 17:27:29 +000055 *rust.BaseSourceProvider
Andrew Walbrana5deb732024-02-15 13:39:46 +000056
Andrew Walbranacd75d22024-03-12 17:27:29 +000057 properties rustLibraryProperties
Andrew Walbrana5deb732024-02-15 13:39:46 +000058}
59
Andrew Walbranacd75d22024-03-12 17:27:29 +000060var _ rust.SourceProvider = (*syspropRustGenRule)(nil)
Inseob Kim988f53c2019-09-16 15:59:01 +090061
62var (
63 syspropJava = pctx.AndroidStaticRule("syspropJava",
64 blueprint.RuleParams{
65 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
66 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
67 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
68 CommandDeps: []string{
69 "$syspropJavaCmd",
70 "$soongZipCmd",
71 },
72 }, "scope")
Andrew Walbrana5deb732024-02-15 13:39:46 +000073 syspropRust = pctx.AndroidStaticRule("syspropRust",
74 blueprint.RuleParams{
75 Command: `rm -rf $out_dir && mkdir -p $out_dir && ` +
76 `$syspropRustCmd --scope $scope --rust-output-dir $out_dir $in`,
77 CommandDeps: []string{
78 "$syspropRustCmd",
79 },
80 }, "scope", "out_dir")
Inseob Kim988f53c2019-09-16 15:59:01 +090081)
82
83func init() {
84 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
85 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Andrew Walbrana5deb732024-02-15 13:39:46 +000086 pctx.HostBinToolVariable("syspropRustCmd", "sysprop_rust")
Inseob Kim988f53c2019-09-16 15:59:01 +090087}
88
Inseob Kim07def122020-11-23 14:43:02 +090089// syspropJavaGenRule module generates srcjar containing generated java APIs.
90// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090091func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
92 var checkApiFileTimeStamp android.WritablePath
93
94 ctx.VisitDirectDeps(func(dep android.Module) {
95 if m, ok := dep.(*syspropLibrary); ok {
96 checkApiFileTimeStamp = m.checkApiFileTimeStamp
97 }
98 })
99
mrziwangba2a4602024-06-11 14:39:15 -0700100 var genSrcjars android.Paths
Inseob Kim988f53c2019-09-16 15:59:01 +0900101 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
102 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
103
104 ctx.Build(pctx, android.BuildParams{
105 Rule: syspropJava,
106 Description: "sysprop_java " + syspropFile.Rel(),
107 Output: srcJarFile,
108 Input: syspropFile,
109 Implicit: checkApiFileTimeStamp,
110 Args: map[string]string{
111 "scope": g.properties.Scope,
112 },
113 })
114
mrziwangba2a4602024-06-11 14:39:15 -0700115 genSrcjars = append(genSrcjars, srcJarFile)
Inseob Kim988f53c2019-09-16 15:59:01 +0900116 }
mrziwangba2a4602024-06-11 14:39:15 -0700117
118 ctx.SetOutputFiles(genSrcjars, "")
Inseob Kim988f53c2019-09-16 15:59:01 +0900119}
120
Colin Cross75ce9ec2021-02-26 16:20:32 -0800121func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
122 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
123 // the check API rule of the sysprop library.
124 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
125}
126
Inseob Kim988f53c2019-09-16 15:59:01 +0900127func syspropJavaGenFactory() android.Module {
128 g := &syspropJavaGenRule{}
129 g.AddProperties(&g.properties)
130 android.InitAndroidModule(g)
131 return g
132}
133
Andrew Walbrana5deb732024-02-15 13:39:46 +0000134// syspropRustGenRule module generates rust source files containing generated rust APIs.
135// It also depends on check api rule, so api check has to pass to use sysprop_library.
Andrew Walbranacd75d22024-03-12 17:27:29 +0000136func (g *syspropRustGenRule) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
Andrew Walbrana5deb732024-02-15 13:39:46 +0000137 var checkApiFileTimeStamp android.WritablePath
138
139 ctx.VisitDirectDeps(func(dep android.Module) {
140 if m, ok := dep.(*syspropLibrary); ok {
141 checkApiFileTimeStamp = m.checkApiFileTimeStamp
142 }
143 })
144
Andrew Walbranacd75d22024-03-12 17:27:29 +0000145 outputDir := android.PathForModuleOut(ctx, "src")
146 libFile := outputDir.Join(ctx, "lib.rs")
147 g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, libFile)
148 libFileLines := []string{"//! Autogenerated system property accessors."}
149
150 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Sysprop_srcs) {
151 moduleName := syspropPathToRustModule(syspropFile)
152 moduleDir := outputDir.Join(ctx, moduleName)
153 modulePath := moduleDir.Join(ctx, "mod.rs")
Andrew Walbrana5deb732024-02-15 13:39:46 +0000154
155 ctx.Build(pctx, android.BuildParams{
156 Rule: syspropRust,
157 Description: "sysprop_rust " + syspropFile.Rel(),
Andrew Walbranacd75d22024-03-12 17:27:29 +0000158 Output: modulePath,
Andrew Walbrana5deb732024-02-15 13:39:46 +0000159 Input: syspropFile,
160 Implicit: checkApiFileTimeStamp,
161 Args: map[string]string{
162 "scope": g.properties.Scope,
Andrew Walbranacd75d22024-03-12 17:27:29 +0000163 "out_dir": moduleDir.String(),
Andrew Walbrana5deb732024-02-15 13:39:46 +0000164 },
165 })
166
Andrew Walbranacd75d22024-03-12 17:27:29 +0000167 g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, modulePath)
168 libFileLines = append(libFileLines, fmt.Sprintf("pub mod %s;", moduleName))
Andrew Walbrana5deb732024-02-15 13:39:46 +0000169 }
Andrew Walbranacd75d22024-03-12 17:27:29 +0000170
171 libFileSource := strings.Join(libFileLines, "\n")
172 android.WriteFileRule(ctx, libFile, libFileSource)
173
174 return libFile
175}
176
177func (g *syspropRustGenRule) SourceProviderProps() []interface{} {
178 return append(g.BaseSourceProvider.SourceProviderProps(), &g.Properties)
179}
180
181// syspropPathToRustModule takes a path to a .sysprop file and returns the name to use for the
182// corresponding Rust module.
183func syspropPathToRustModule(syspropFilename android.Path) string {
184 filenameBase := strings.TrimSuffix(syspropFilename.Base(), ".sysprop")
185 return strings.ToLower(filenameBase)
Andrew Walbrana5deb732024-02-15 13:39:46 +0000186}
187
188func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
189 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
190 // the check API rule of the sysprop library.
191 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
192}
193
Andrew Walbrana5deb732024-02-15 13:39:46 +0000194func syspropRustGenFactory() android.Module {
Andrew Walbranacd75d22024-03-12 17:27:29 +0000195 g := &syspropRustGenRule{
196 BaseSourceProvider: rust.NewSourceProvider(),
197 }
198 sourceProvider := rust.NewSourceProviderModule(android.DeviceSupported, g, false, false)
199 sourceProvider.AddProperties(&g.properties)
200 return sourceProvider.Init()
Andrew Walbrana5deb732024-02-15 13:39:46 +0000201}
202
Inseob Kimc0907f12019-02-08 21:00:45 +0900203type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900204 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100205 android.ApexModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900206
Inseob Kim42882742019-07-30 17:55:33 +0900207 properties syspropLibraryProperties
208
209 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900210 latestApiFile android.OptionalPath
211 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900212 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900213}
214
215type syspropLibraryProperties struct {
216 // Determine who owns this sysprop library. Possible values are
217 // "Platform", "Vendor", or "Odm"
218 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900219
220 // list of package names that will be documented and publicized as API
221 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900222
Inseob Kim42882742019-07-30 17:55:33 +0900223 // If set to true, allow this module to be dexed and installed on devices.
224 Installable *bool
225
Inseob Kim9da1f812021-06-14 12:03:59 +0900226 // Make this module available when building for ramdisk
227 Ramdisk_available *bool
228
Inseob Kim42882742019-07-30 17:55:33 +0900229 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900230 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900231
232 // Make this module available when building for vendor
233 Vendor_available *bool
234
Justin Yun63e9ec72020-10-29 16:49:43 +0900235 // Make this module available when building for product
236 Product_available *bool
237
Inseob Kim42882742019-07-30 17:55:33 +0900238 // list of .sysprop files which defines the properties.
239 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900240
Inseob Kim89db15d2020-02-03 18:06:46 +0900241 // If set to true, build a variant of the module for the host. Defaults to false.
242 Host_supported *bool
243
Jooyung Han379660c2020-04-21 15:24:00 +0900244 Cpp struct {
245 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
246 // Forwarded to cc_library.min_sdk_version
247 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000248
249 // C compiler flags used to build library
250 Cflags []string
251
252 // Linker flags used to build binary
253 Ldflags []string
Jooyung Han379660c2020-04-21 15:24:00 +0900254 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900255
256 Java struct {
257 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
258 // Forwarded to java_library.min_sdk_version
259 Min_sdk_version *string
260 }
Andrew Walbrana5deb732024-02-15 13:39:46 +0000261
262 Rust struct {
263 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
264 // Forwarded to rust_library.min_sdk_version
265 Min_sdk_version *string
266 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900267}
268
269var (
Inseob Kim42882742019-07-30 17:55:33 +0900270 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900271 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900272
273 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
274 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900275)
276
Inseob Kim07def122020-11-23 14:43:02 +0900277// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900278func syspropLibraries(config android.Config) *[]string {
279 return config.Once(syspropLibrariesKey, func() interface{} {
280 return &[]string{}
281 }).(*[]string)
282}
283
284func SyspropLibraries(config android.Config) []string {
285 return append([]string{}, *syspropLibraries(config)...)
286}
287
Inseob Kimc0907f12019-02-08 21:00:45 +0900288func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000289 registerSyspropBuildComponents(android.InitRegistrationContext)
290}
291
292func registerSyspropBuildComponents(ctx android.RegistrationContext) {
293 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900294}
295
Inseob Kim42882742019-07-30 17:55:33 +0900296func (m *syspropLibrary) Name() string {
297 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900298}
299
Inseob Kimac1e9862019-12-09 18:15:47 +0900300func (m *syspropLibrary) Owner() string {
301 return m.properties.Property_owner
302}
303
Inseob Kim07def122020-11-23 14:43:02 +0900304func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900305 return "lib" + m.BaseModuleName()
306}
307
Colin Cross75ce9ec2021-02-26 16:20:32 -0800308func (m *syspropLibrary) javaPublicStubName() string {
309 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900310}
311
Inseob Kim988f53c2019-09-16 15:59:01 +0900312func (m *syspropLibrary) javaGenModuleName() string {
313 return m.BaseModuleName() + "_java_gen"
314}
315
Inseob Kimac1e9862019-12-09 18:15:47 +0900316func (m *syspropLibrary) javaGenPublicStubName() string {
317 return m.BaseModuleName() + "_java_gen_public"
318}
319
Andrew Walbrana5deb732024-02-15 13:39:46 +0000320func (m *syspropLibrary) rustGenStubName() string {
321 return "lib" + m.rustCrateName() + "_rust"
322}
323
324func (m *syspropLibrary) rustCrateName() string {
325 moduleName := strings.ToLower(m.BaseModuleName())
326 moduleName = strings.ReplaceAll(moduleName, "-", "_")
327 moduleName = strings.ReplaceAll(moduleName, ".", "_")
328 return moduleName
329}
330
Inseob Kim42882742019-07-30 17:55:33 +0900331func (m *syspropLibrary) BaseModuleName() string {
332 return m.ModuleBase.Name()
333}
334
Inseob Kimc9770d62021-01-15 18:04:20 +0900335func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900336 return m.currentApiFile
337}
338
Inseob Kim07def122020-11-23 14:43:02 +0900339// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
340// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900341func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900342 baseModuleName := m.BaseModuleName()
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000343 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
344 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900345 if syspropFile.Ext() != ".sysprop" {
346 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
347 }
348 }
349
350 if ctx.Failed() {
351 return
352 }
353
Inseob Kimc9770d62021-01-15 18:04:20 +0900354 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
355 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
356 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
357 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
358 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900359
360 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800361 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900362 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
363 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800364 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900365 Output(m.dumpedApiFile).
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000366 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800367 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900368
369 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800370 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900371
Inseob Kimc9770d62021-01-15 18:04:20 +0900372 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
373 // properties. But we have to feed current api file and latest api file to the rule builder.
374 // Currently we can't get android.Path representing the null device, so we add any existing API
375 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
376 // method.
377 var apiFileList android.Paths
378 currentApiArgument := os.DevNull
379 if m.currentApiFile.Valid() {
380 apiFileList = append(apiFileList, m.currentApiFile.Path())
381 currentApiArgument = m.currentApiFile.String()
382 }
383
384 latestApiArgument := os.DevNull
385 if m.latestApiFile.Valid() {
386 apiFileList = append(apiFileList, m.latestApiFile.Path())
387 latestApiArgument = m.latestApiFile.String()
388 }
389
Inseob Kim07def122020-11-23 14:43:02 +0900390 // 1. compares current.txt to api-dump.txt
391 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900392 msg := fmt.Sprintf(`\n******************************\n`+
393 `API of sysprop_library %s doesn't match with current.txt\n`+
394 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900395 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900396 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900397 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900398
399 rule.Command().
400 Text("( cmp").Flag("-s").
401 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900402 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900403 Text("|| ( echo").Flag("-e").
404 Flag(`"` + msg + `"`).
405 Text("; exit 38) )")
406
Inseob Kim07def122020-11-23 14:43:02 +0900407 // 2. compares current.txt to latest.txt (frozen API)
408 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900409 msg = fmt.Sprintf(`\n******************************\n`+
410 `API of sysprop_library %s doesn't match with latest version\n`+
411 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900412 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900413
414 rule.Command().
415 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800416 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900417 Text(latestApiArgument).
418 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900419 Text(" || ( echo").Flag("-e").
420 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900421 Text("; exit 38) )").
422 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900423
424 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
425
426 rule.Command().
427 Text("touch").
428 Output(m.checkApiFileTimeStamp)
429
Colin Crossf1a035e2020-11-16 17:32:30 -0800430 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900431}
432
433func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
434 return android.AndroidMkData{
435 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
436 // sysprop_library module itself is defined as a FAKE module to perform API check.
437 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800438 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
439 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Inseob Kim42882742019-07-30 17:55:33 +0900440 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
441 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
LaMont Jonesb5099382024-01-10 23:42:36 +0000442 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
443 for _, extra := range data.Extra {
444 extra(w, nil)
445 }
Inseob Kim42882742019-07-30 17:55:33 +0900446 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
447 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
448 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900449 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
450
451 // dump API rule
452 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900453
454 // check API rule
455 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900456 }}
457}
458
Jiyong Park45bf82e2020-12-15 22:29:02 +0900459var _ android.ApexModule = (*syspropLibrary)(nil)
460
461// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700462func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
463 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900464 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
465}
466
Inseob Kim42882742019-07-30 17:55:33 +0900467// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
468// Both Java and C++ modules can link against sysprop_library, and API stability check
469// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000470// is performed. Note that the generated C++ module has its name prefixed with
471// `lib`, and it is this module that should be depended on from other C++
472// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
473// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900474func syspropLibraryFactory() android.Module {
475 m := &syspropLibrary{}
476
477 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900478 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900479 )
Inseob Kim42882742019-07-30 17:55:33 +0900480 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100481 android.InitApexModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900482 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900483 return m
484}
485
Inseob Kimac1e9862019-12-09 18:15:47 +0900486type ccLibraryProperties struct {
487 Name *string
488 Srcs []string
489 Soc_specific *bool
490 Device_specific *bool
491 Product_specific *bool
492 Sysprop struct {
493 Platform *bool
494 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900495 Target struct {
496 Android struct {
497 Header_libs []string
498 Shared_libs []string
499 }
500 Host struct {
501 Static_libs []string
502 }
503 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900504 Required []string
505 Recovery *bool
506 Recovery_available *bool
507 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900508 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900509 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900510 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100511 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900512 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000513 Cflags []string
514 Ldflags []string
Inseob Kimac1e9862019-12-09 18:15:47 +0900515}
516
517type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800518 Name *string
519 Srcs []string
520 Soc_specific *bool
521 Device_specific *bool
522 Product_specific *bool
523 Required []string
524 Sdk_version *string
525 Installable *bool
526 Libs []string
527 Stem *string
528 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900529 Apex_available []string
530 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900531}
532
Andrew Walbrana5deb732024-02-15 13:39:46 +0000533type rustLibraryProperties struct {
534 Name *string
Andrew Walbranacd75d22024-03-12 17:27:29 +0000535 Sysprop_srcs []string `android:"path"`
536 Scope string
537 Check_api *string
Andrew Walbrana5deb732024-02-15 13:39:46 +0000538 Srcs []string
539 Installable *bool
540 Crate_name string
541 Rustlibs []string
542 Vendor_available *bool
543 Product_available *bool
544 Apex_available []string
545 Min_sdk_version *string
546}
547
Inseob Kimc0907f12019-02-08 21:00:45 +0900548func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900549 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900550 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
551 }
552
Inseob Kimac1e9862019-12-09 18:15:47 +0900553 // ctx's Platform or Specific functions represent where this sysprop_library installed.
554 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
555 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900556 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900557 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900558 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900559
Inseob Kim07def122020-11-23 14:43:02 +0900560 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
561 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900562 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900563 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900564 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900565 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900566 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900567 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900568 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900569
Inseob Kimac1e9862019-12-09 18:15:47 +0900570 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900571 case "Platform":
572 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900573 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900574 case "Vendor":
575 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900576 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900577 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
578 "System can't access sysprop_library owned by Vendor")
579 }
580 case "Odm":
581 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900582 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900583 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
584 "Odm-defined properties should be accessed only in Vendor or Odm")
585 }
586 default:
587 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900588 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900589 }
590
Inseob Kim07def122020-11-23 14:43:02 +0900591 // Generate a C++ implementation library.
592 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900593 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900594 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900595 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900596 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
597 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
598 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
599 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900600 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
601 ccProps.Target.Android.Shared_libs = []string{"liblog"}
602 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900603 ccProps.Recovery_available = m.properties.Recovery_available
604 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900605 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900606 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900607 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100608 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900609 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000610 ccProps.Cflags = m.properties.Cpp.Cflags
611 ccProps.Ldflags = m.properties.Cpp.Ldflags
Colin Cross84dfc3d2019-09-25 11:33:01 -0700612 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900613
Inseob Kim988f53c2019-09-16 15:59:01 +0900614 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900615
Inseob Kimac1e9862019-12-09 18:15:47 +0900616 // We need to only use public version, if the partition where sysprop_library will be installed
617 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900618 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900619 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900620 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900621 } else if isOwnerPlatform && installedInVendorOrOdm {
622 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900623 scope = "public"
624 }
625
Inseob Kim07def122020-11-23 14:43:02 +0900626 // Generate a Java implementation library.
627 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
628 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900629 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800630 Srcs: m.properties.Srcs,
631 Scope: scope,
632 Name: proptools.StringPtr(m.javaGenModuleName()),
633 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900634 })
635
636 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
637 // and allow any modules (even from different partition) to link against the sysprop_library.
638 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800639 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900640 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800641 publicStub = m.javaPublicStubName()
642 }
643
644 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
645 Name: proptools.StringPtr(m.BaseModuleName()),
646 Srcs: []string{":" + m.javaGenModuleName()},
647 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
648 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
649 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
650 Installable: m.properties.Installable,
651 Sdk_version: proptools.StringPtr("core_current"),
652 Libs: []string{javaSyspropStub},
653 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900654 Apex_available: m.ApexProperties.Apex_available,
655 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800656 })
657
658 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900659 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800660 Srcs: m.properties.Srcs,
661 Scope: "public",
662 Name: proptools.StringPtr(m.javaGenPublicStubName()),
663 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900664 })
665
666 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800667 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900668 Srcs: []string{":" + m.javaGenPublicStubName()},
669 Installable: proptools.BoolPtr(false),
670 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900671 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900672 Stem: proptools.StringPtr(m.BaseModuleName()),
673 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900674 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900675
Andrew Walbrana5deb732024-02-15 13:39:46 +0000676 // Generate a Rust implementation library.
Andrew Walbrana5deb732024-02-15 13:39:46 +0000677 rustProps := rustLibraryProperties{
Andrew Walbranacd75d22024-03-12 17:27:29 +0000678 Name: proptools.StringPtr(m.rustGenStubName()),
679 Sysprop_srcs: m.properties.Srcs,
680 Scope: scope,
681 Check_api: proptools.StringPtr(ctx.ModuleName()),
Ivan Lozanoa6e92bd2024-12-12 16:52:17 +0000682 Installable: m.properties.Installable,
Andrew Walbranacd75d22024-03-12 17:27:29 +0000683 Crate_name: m.rustCrateName(),
Andrew Walbrana5deb732024-02-15 13:39:46 +0000684 Rustlibs: []string{
Andrew Walbranacd75d22024-03-12 17:27:29 +0000685 "liblog_rust",
Andrew Walbrana5deb732024-02-15 13:39:46 +0000686 "librustutils",
687 },
688 Vendor_available: m.properties.Vendor_available,
689 Product_available: m.properties.Product_available,
690 Apex_available: m.ApexProperties.Apex_available,
691 Min_sdk_version: proptools.StringPtr("29"),
692 }
Andrew Walbranacd75d22024-03-12 17:27:29 +0000693 ctx.CreateModule(syspropRustGenFactory, &rustProps)
Andrew Walbrana5deb732024-02-15 13:39:46 +0000694
Inseob Kim07def122020-11-23 14:43:02 +0900695 // syspropLibraries will be used by property_contexts to check types.
696 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900697 if m.ExportedToMake() {
698 syspropLibrariesLock.Lock()
699 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900700
Inseob Kim69cf09e2020-05-04 19:28:25 +0900701 libraries := syspropLibraries(ctx.Config())
702 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
703 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900704}