blob: b9b68be2d89b74c4808e06acb0e64cdd20f72232 [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
52
53 genSrcjars android.Paths
54}
55
Andrew Walbrana5deb732024-02-15 13:39:46 +000056type syspropRustGenRule struct {
Andrew Walbranacd75d22024-03-12 17:27:29 +000057 *rust.BaseSourceProvider
Andrew Walbrana5deb732024-02-15 13:39:46 +000058
Andrew Walbranacd75d22024-03-12 17:27:29 +000059 properties rustLibraryProperties
Andrew Walbrana5deb732024-02-15 13:39:46 +000060}
61
Inseob Kim988f53c2019-09-16 15:59:01 +090062var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
Andrew Walbranacd75d22024-03-12 17:27:29 +000063var _ rust.SourceProvider = (*syspropRustGenRule)(nil)
Inseob Kim988f53c2019-09-16 15:59:01 +090064
65var (
66 syspropJava = pctx.AndroidStaticRule("syspropJava",
67 blueprint.RuleParams{
68 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
69 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
70 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
71 CommandDeps: []string{
72 "$syspropJavaCmd",
73 "$soongZipCmd",
74 },
75 }, "scope")
Andrew Walbrana5deb732024-02-15 13:39:46 +000076 syspropRust = pctx.AndroidStaticRule("syspropRust",
77 blueprint.RuleParams{
78 Command: `rm -rf $out_dir && mkdir -p $out_dir && ` +
79 `$syspropRustCmd --scope $scope --rust-output-dir $out_dir $in`,
80 CommandDeps: []string{
81 "$syspropRustCmd",
82 },
83 }, "scope", "out_dir")
Inseob Kim988f53c2019-09-16 15:59:01 +090084)
85
86func init() {
87 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
88 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Andrew Walbrana5deb732024-02-15 13:39:46 +000089 pctx.HostBinToolVariable("syspropRustCmd", "sysprop_rust")
Inseob Kim988f53c2019-09-16 15:59:01 +090090}
91
Inseob Kim07def122020-11-23 14:43:02 +090092// syspropJavaGenRule module generates srcjar containing generated java APIs.
93// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090094func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
95 var checkApiFileTimeStamp android.WritablePath
96
97 ctx.VisitDirectDeps(func(dep android.Module) {
98 if m, ok := dep.(*syspropLibrary); ok {
99 checkApiFileTimeStamp = m.checkApiFileTimeStamp
100 }
101 })
102
103 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
104 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
105
106 ctx.Build(pctx, android.BuildParams{
107 Rule: syspropJava,
108 Description: "sysprop_java " + syspropFile.Rel(),
109 Output: srcJarFile,
110 Input: syspropFile,
111 Implicit: checkApiFileTimeStamp,
112 Args: map[string]string{
113 "scope": g.properties.Scope,
114 },
115 })
116
117 g.genSrcjars = append(g.genSrcjars, srcJarFile)
118 }
119}
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 (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
128 switch tag {
129 case "":
130 return g.genSrcjars, nil
131 default:
132 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
133 }
134}
135
136func syspropJavaGenFactory() android.Module {
137 g := &syspropJavaGenRule{}
138 g.AddProperties(&g.properties)
139 android.InitAndroidModule(g)
140 return g
141}
142
Andrew Walbrana5deb732024-02-15 13:39:46 +0000143// syspropRustGenRule module generates rust source files containing generated rust APIs.
144// It also depends on check api rule, so api check has to pass to use sysprop_library.
Andrew Walbranacd75d22024-03-12 17:27:29 +0000145func (g *syspropRustGenRule) GenerateSource(ctx rust.ModuleContext, deps rust.PathDeps) android.Path {
Andrew Walbrana5deb732024-02-15 13:39:46 +0000146 var checkApiFileTimeStamp android.WritablePath
147
148 ctx.VisitDirectDeps(func(dep android.Module) {
149 if m, ok := dep.(*syspropLibrary); ok {
150 checkApiFileTimeStamp = m.checkApiFileTimeStamp
151 }
152 })
153
Andrew Walbranacd75d22024-03-12 17:27:29 +0000154 outputDir := android.PathForModuleOut(ctx, "src")
155 libFile := outputDir.Join(ctx, "lib.rs")
156 g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, libFile)
157 libFileLines := []string{"//! Autogenerated system property accessors."}
158
159 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Sysprop_srcs) {
160 moduleName := syspropPathToRustModule(syspropFile)
161 moduleDir := outputDir.Join(ctx, moduleName)
162 modulePath := moduleDir.Join(ctx, "mod.rs")
Andrew Walbrana5deb732024-02-15 13:39:46 +0000163
164 ctx.Build(pctx, android.BuildParams{
165 Rule: syspropRust,
166 Description: "sysprop_rust " + syspropFile.Rel(),
Andrew Walbranacd75d22024-03-12 17:27:29 +0000167 Output: modulePath,
Andrew Walbrana5deb732024-02-15 13:39:46 +0000168 Input: syspropFile,
169 Implicit: checkApiFileTimeStamp,
170 Args: map[string]string{
171 "scope": g.properties.Scope,
Andrew Walbranacd75d22024-03-12 17:27:29 +0000172 "out_dir": moduleDir.String(),
Andrew Walbrana5deb732024-02-15 13:39:46 +0000173 },
174 })
175
Andrew Walbranacd75d22024-03-12 17:27:29 +0000176 g.BaseSourceProvider.OutputFiles = append(g.BaseSourceProvider.OutputFiles, modulePath)
177 libFileLines = append(libFileLines, fmt.Sprintf("pub mod %s;", moduleName))
Andrew Walbrana5deb732024-02-15 13:39:46 +0000178 }
Andrew Walbranacd75d22024-03-12 17:27:29 +0000179
180 libFileSource := strings.Join(libFileLines, "\n")
181 android.WriteFileRule(ctx, libFile, libFileSource)
182
183 return libFile
184}
185
186func (g *syspropRustGenRule) SourceProviderProps() []interface{} {
187 return append(g.BaseSourceProvider.SourceProviderProps(), &g.Properties)
188}
189
190// syspropPathToRustModule takes a path to a .sysprop file and returns the name to use for the
191// corresponding Rust module.
192func syspropPathToRustModule(syspropFilename android.Path) string {
193 filenameBase := strings.TrimSuffix(syspropFilename.Base(), ".sysprop")
194 return strings.ToLower(filenameBase)
Andrew Walbrana5deb732024-02-15 13:39:46 +0000195}
196
197func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
198 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
199 // the check API rule of the sysprop library.
200 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
201}
202
Andrew Walbrana5deb732024-02-15 13:39:46 +0000203func syspropRustGenFactory() android.Module {
Andrew Walbranacd75d22024-03-12 17:27:29 +0000204 g := &syspropRustGenRule{
205 BaseSourceProvider: rust.NewSourceProvider(),
206 }
207 sourceProvider := rust.NewSourceProviderModule(android.DeviceSupported, g, false, false)
208 sourceProvider.AddProperties(&g.properties)
209 return sourceProvider.Init()
Andrew Walbrana5deb732024-02-15 13:39:46 +0000210}
211
Inseob Kimc0907f12019-02-08 21:00:45 +0900212type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900213 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100214 android.ApexModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900215
Inseob Kim42882742019-07-30 17:55:33 +0900216 properties syspropLibraryProperties
217
218 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900219 latestApiFile android.OptionalPath
220 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900221 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900222}
223
224type syspropLibraryProperties struct {
225 // Determine who owns this sysprop library. Possible values are
226 // "Platform", "Vendor", or "Odm"
227 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900228
229 // list of package names that will be documented and publicized as API
230 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900231
Inseob Kim42882742019-07-30 17:55:33 +0900232 // If set to true, allow this module to be dexed and installed on devices.
233 Installable *bool
234
Inseob Kim9da1f812021-06-14 12:03:59 +0900235 // Make this module available when building for ramdisk
236 Ramdisk_available *bool
237
Inseob Kim42882742019-07-30 17:55:33 +0900238 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900239 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900240
241 // Make this module available when building for vendor
242 Vendor_available *bool
243
Justin Yun63e9ec72020-10-29 16:49:43 +0900244 // Make this module available when building for product
245 Product_available *bool
246
Inseob Kim42882742019-07-30 17:55:33 +0900247 // list of .sysprop files which defines the properties.
248 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900249
Inseob Kim89db15d2020-02-03 18:06:46 +0900250 // If set to true, build a variant of the module for the host. Defaults to false.
251 Host_supported *bool
252
Jooyung Han379660c2020-04-21 15:24:00 +0900253 Cpp struct {
254 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
255 // Forwarded to cc_library.min_sdk_version
256 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000257
258 // C compiler flags used to build library
259 Cflags []string
260
261 // Linker flags used to build binary
262 Ldflags []string
Jooyung Han379660c2020-04-21 15:24:00 +0900263 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900264
265 Java struct {
266 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
267 // Forwarded to java_library.min_sdk_version
268 Min_sdk_version *string
269 }
Andrew Walbrana5deb732024-02-15 13:39:46 +0000270
271 Rust struct {
272 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
273 // Forwarded to rust_library.min_sdk_version
274 Min_sdk_version *string
275 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900276}
277
278var (
Inseob Kim42882742019-07-30 17:55:33 +0900279 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900280 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900281
282 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
283 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900284)
285
Inseob Kim07def122020-11-23 14:43:02 +0900286// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900287func syspropLibraries(config android.Config) *[]string {
288 return config.Once(syspropLibrariesKey, func() interface{} {
289 return &[]string{}
290 }).(*[]string)
291}
292
293func SyspropLibraries(config android.Config) []string {
294 return append([]string{}, *syspropLibraries(config)...)
295}
296
Inseob Kimc0907f12019-02-08 21:00:45 +0900297func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000298 registerSyspropBuildComponents(android.InitRegistrationContext)
299}
300
301func registerSyspropBuildComponents(ctx android.RegistrationContext) {
302 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900303}
304
Inseob Kim42882742019-07-30 17:55:33 +0900305func (m *syspropLibrary) Name() string {
306 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900307}
308
Inseob Kimac1e9862019-12-09 18:15:47 +0900309func (m *syspropLibrary) Owner() string {
310 return m.properties.Property_owner
311}
312
Inseob Kim07def122020-11-23 14:43:02 +0900313func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900314 return "lib" + m.BaseModuleName()
315}
316
Colin Cross75ce9ec2021-02-26 16:20:32 -0800317func (m *syspropLibrary) javaPublicStubName() string {
318 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900319}
320
Inseob Kim988f53c2019-09-16 15:59:01 +0900321func (m *syspropLibrary) javaGenModuleName() string {
322 return m.BaseModuleName() + "_java_gen"
323}
324
Inseob Kimac1e9862019-12-09 18:15:47 +0900325func (m *syspropLibrary) javaGenPublicStubName() string {
326 return m.BaseModuleName() + "_java_gen_public"
327}
328
Andrew Walbrana5deb732024-02-15 13:39:46 +0000329func (m *syspropLibrary) rustGenStubName() string {
330 return "lib" + m.rustCrateName() + "_rust"
331}
332
333func (m *syspropLibrary) rustCrateName() string {
334 moduleName := strings.ToLower(m.BaseModuleName())
335 moduleName = strings.ReplaceAll(moduleName, "-", "_")
336 moduleName = strings.ReplaceAll(moduleName, ".", "_")
337 return moduleName
338}
339
Inseob Kim42882742019-07-30 17:55:33 +0900340func (m *syspropLibrary) BaseModuleName() string {
341 return m.ModuleBase.Name()
342}
343
Inseob Kimc9770d62021-01-15 18:04:20 +0900344func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900345 return m.currentApiFile
346}
347
Inseob Kim07def122020-11-23 14:43:02 +0900348// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
349// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900350func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900351 baseModuleName := m.BaseModuleName()
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000352 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
353 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900354 if syspropFile.Ext() != ".sysprop" {
355 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
356 }
357 }
Colin Cross40213022023-12-13 15:19:49 -0800358 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Inseob Kim988f53c2019-09-16 15:59:01 +0900359
360 if ctx.Failed() {
361 return
362 }
363
Inseob Kimc9770d62021-01-15 18:04:20 +0900364 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
365 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
366 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
367 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
368 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900369
370 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800371 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900372 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
373 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800374 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900375 Output(m.dumpedApiFile).
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000376 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800377 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900378
379 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800380 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900381
Inseob Kimc9770d62021-01-15 18:04:20 +0900382 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
383 // properties. But we have to feed current api file and latest api file to the rule builder.
384 // Currently we can't get android.Path representing the null device, so we add any existing API
385 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
386 // method.
387 var apiFileList android.Paths
388 currentApiArgument := os.DevNull
389 if m.currentApiFile.Valid() {
390 apiFileList = append(apiFileList, m.currentApiFile.Path())
391 currentApiArgument = m.currentApiFile.String()
392 }
393
394 latestApiArgument := os.DevNull
395 if m.latestApiFile.Valid() {
396 apiFileList = append(apiFileList, m.latestApiFile.Path())
397 latestApiArgument = m.latestApiFile.String()
398 }
399
Inseob Kim07def122020-11-23 14:43:02 +0900400 // 1. compares current.txt to api-dump.txt
401 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900402 msg := fmt.Sprintf(`\n******************************\n`+
403 `API of sysprop_library %s doesn't match with current.txt\n`+
404 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900405 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900406 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900407 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900408
409 rule.Command().
410 Text("( cmp").Flag("-s").
411 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900412 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900413 Text("|| ( echo").Flag("-e").
414 Flag(`"` + msg + `"`).
415 Text("; exit 38) )")
416
Inseob Kim07def122020-11-23 14:43:02 +0900417 // 2. compares current.txt to latest.txt (frozen API)
418 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900419 msg = fmt.Sprintf(`\n******************************\n`+
420 `API of sysprop_library %s doesn't match with latest version\n`+
421 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900422 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900423
424 rule.Command().
425 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800426 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900427 Text(latestApiArgument).
428 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900429 Text(" || ( echo").Flag("-e").
430 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900431 Text("; exit 38) )").
432 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900433
434 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
435
436 rule.Command().
437 Text("touch").
438 Output(m.checkApiFileTimeStamp)
439
Colin Crossf1a035e2020-11-16 17:32:30 -0800440 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900441}
442
443func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
444 return android.AndroidMkData{
445 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
446 // sysprop_library module itself is defined as a FAKE module to perform API check.
447 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800448 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
449 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Inseob Kim42882742019-07-30 17:55:33 +0900450 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
451 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
LaMont Jonesb5099382024-01-10 23:42:36 +0000452 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
453 for _, extra := range data.Extra {
454 extra(w, nil)
455 }
Inseob Kim42882742019-07-30 17:55:33 +0900456 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
457 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
458 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900459 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
460
461 // dump API rule
462 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900463
464 // check API rule
465 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900466 }}
467}
468
Jiyong Park45bf82e2020-12-15 22:29:02 +0900469var _ android.ApexModule = (*syspropLibrary)(nil)
470
471// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700472func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
473 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900474 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
475}
476
Inseob Kim42882742019-07-30 17:55:33 +0900477// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
478// Both Java and C++ modules can link against sysprop_library, and API stability check
479// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000480// is performed. Note that the generated C++ module has its name prefixed with
481// `lib`, and it is this module that should be depended on from other C++
482// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
483// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900484func syspropLibraryFactory() android.Module {
485 m := &syspropLibrary{}
486
487 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900488 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900489 )
Inseob Kim42882742019-07-30 17:55:33 +0900490 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100491 android.InitApexModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900492 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900493 return m
494}
495
Inseob Kimac1e9862019-12-09 18:15:47 +0900496type ccLibraryProperties struct {
497 Name *string
498 Srcs []string
499 Soc_specific *bool
500 Device_specific *bool
501 Product_specific *bool
502 Sysprop struct {
503 Platform *bool
504 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900505 Target struct {
506 Android struct {
507 Header_libs []string
508 Shared_libs []string
509 }
510 Host struct {
511 Static_libs []string
512 }
513 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900514 Required []string
515 Recovery *bool
516 Recovery_available *bool
517 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900518 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900519 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900520 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100521 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900522 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000523 Cflags []string
524 Ldflags []string
Inseob Kimac1e9862019-12-09 18:15:47 +0900525}
526
527type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800528 Name *string
529 Srcs []string
530 Soc_specific *bool
531 Device_specific *bool
532 Product_specific *bool
533 Required []string
534 Sdk_version *string
535 Installable *bool
536 Libs []string
537 Stem *string
538 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900539 Apex_available []string
540 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900541}
542
Andrew Walbrana5deb732024-02-15 13:39:46 +0000543type rustLibraryProperties struct {
544 Name *string
Andrew Walbranacd75d22024-03-12 17:27:29 +0000545 Sysprop_srcs []string `android:"path"`
546 Scope string
547 Check_api *string
Andrew Walbrana5deb732024-02-15 13:39:46 +0000548 Srcs []string
549 Installable *bool
550 Crate_name string
551 Rustlibs []string
552 Vendor_available *bool
553 Product_available *bool
554 Apex_available []string
555 Min_sdk_version *string
556}
557
Inseob Kimc0907f12019-02-08 21:00:45 +0900558func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900559 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900560 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
561 }
562
Inseob Kimac1e9862019-12-09 18:15:47 +0900563 // ctx's Platform or Specific functions represent where this sysprop_library installed.
564 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
565 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900566 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900567 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900568 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900569
Inseob Kim07def122020-11-23 14:43:02 +0900570 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
571 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900572 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900573 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900574 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900575 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900576 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900577 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900578 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900579
Inseob Kimac1e9862019-12-09 18:15:47 +0900580 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900581 case "Platform":
582 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900583 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900584 case "Vendor":
585 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900586 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900587 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
588 "System can't access sysprop_library owned by Vendor")
589 }
590 case "Odm":
591 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900592 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900593 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
594 "Odm-defined properties should be accessed only in Vendor or Odm")
595 }
596 default:
597 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900598 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900599 }
600
Inseob Kim07def122020-11-23 14:43:02 +0900601 // Generate a C++ implementation library.
602 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900603 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900604 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900605 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900606 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
607 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
608 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
609 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900610 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
611 ccProps.Target.Android.Shared_libs = []string{"liblog"}
612 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900613 ccProps.Recovery_available = m.properties.Recovery_available
614 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900615 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900616 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900617 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100618 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900619 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000620 ccProps.Cflags = m.properties.Cpp.Cflags
621 ccProps.Ldflags = m.properties.Cpp.Ldflags
Colin Cross84dfc3d2019-09-25 11:33:01 -0700622 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900623
Inseob Kim988f53c2019-09-16 15:59:01 +0900624 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900625
Inseob Kimac1e9862019-12-09 18:15:47 +0900626 // We need to only use public version, if the partition where sysprop_library will be installed
627 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900628 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900629 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900630 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900631 } else if isOwnerPlatform && installedInVendorOrOdm {
632 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900633 scope = "public"
634 }
635
Inseob Kim07def122020-11-23 14:43:02 +0900636 // Generate a Java implementation library.
637 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
638 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900639 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800640 Srcs: m.properties.Srcs,
641 Scope: scope,
642 Name: proptools.StringPtr(m.javaGenModuleName()),
643 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900644 })
645
646 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
647 // and allow any modules (even from different partition) to link against the sysprop_library.
648 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800649 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900650 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800651 publicStub = m.javaPublicStubName()
652 }
653
654 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
655 Name: proptools.StringPtr(m.BaseModuleName()),
656 Srcs: []string{":" + m.javaGenModuleName()},
657 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
658 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
659 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
660 Installable: m.properties.Installable,
661 Sdk_version: proptools.StringPtr("core_current"),
662 Libs: []string{javaSyspropStub},
663 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900664 Apex_available: m.ApexProperties.Apex_available,
665 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800666 })
667
668 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900669 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800670 Srcs: m.properties.Srcs,
671 Scope: "public",
672 Name: proptools.StringPtr(m.javaGenPublicStubName()),
673 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900674 })
675
676 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800677 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900678 Srcs: []string{":" + m.javaGenPublicStubName()},
679 Installable: proptools.BoolPtr(false),
680 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900681 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900682 Stem: proptools.StringPtr(m.BaseModuleName()),
683 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900684 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900685
Andrew Walbrana5deb732024-02-15 13:39:46 +0000686 // Generate a Rust implementation library.
Andrew Walbrana5deb732024-02-15 13:39:46 +0000687 rustProps := rustLibraryProperties{
Andrew Walbranacd75d22024-03-12 17:27:29 +0000688 Name: proptools.StringPtr(m.rustGenStubName()),
689 Sysprop_srcs: m.properties.Srcs,
690 Scope: scope,
691 Check_api: proptools.StringPtr(ctx.ModuleName()),
692 Installable: proptools.BoolPtr(false),
693 Crate_name: m.rustCrateName(),
Andrew Walbrana5deb732024-02-15 13:39:46 +0000694 Rustlibs: []string{
Andrew Walbranacd75d22024-03-12 17:27:29 +0000695 "liblog_rust",
Andrew Walbrana5deb732024-02-15 13:39:46 +0000696 "librustutils",
697 },
698 Vendor_available: m.properties.Vendor_available,
699 Product_available: m.properties.Product_available,
700 Apex_available: m.ApexProperties.Apex_available,
701 Min_sdk_version: proptools.StringPtr("29"),
702 }
Andrew Walbranacd75d22024-03-12 17:27:29 +0000703 ctx.CreateModule(syspropRustGenFactory, &rustProps)
Andrew Walbrana5deb732024-02-15 13:39:46 +0000704
Inseob Kim07def122020-11-23 14:43:02 +0900705 // syspropLibraries will be used by property_contexts to check types.
706 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900707 if m.ExportedToMake() {
708 syspropLibrariesLock.Lock()
709 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900710
Inseob Kim69cf09e2020-05-04 19:28:25 +0900711 libraries := syspropLibraries(ctx.Config())
712 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
713 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900714}