blob: 82abba4cbce971b0873860b1f0a1eba67d4cf602 [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 {
57 android.ModuleBase
58
59 properties syspropGenProperties
60
61 genSrcs android.Paths
62}
63
Inseob Kim988f53c2019-09-16 15:59:01 +090064var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
Andrew Walbrana5deb732024-02-15 13:39:46 +000065var _ android.OutputFileProducer = (*syspropRustGenRule)(nil)
Inseob Kim988f53c2019-09-16 15:59:01 +090066
67var (
68 syspropJava = pctx.AndroidStaticRule("syspropJava",
69 blueprint.RuleParams{
70 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
71 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
72 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
73 CommandDeps: []string{
74 "$syspropJavaCmd",
75 "$soongZipCmd",
76 },
77 }, "scope")
Andrew Walbrana5deb732024-02-15 13:39:46 +000078 syspropRust = pctx.AndroidStaticRule("syspropRust",
79 blueprint.RuleParams{
80 Command: `rm -rf $out_dir && mkdir -p $out_dir && ` +
81 `$syspropRustCmd --scope $scope --rust-output-dir $out_dir $in`,
82 CommandDeps: []string{
83 "$syspropRustCmd",
84 },
85 }, "scope", "out_dir")
Inseob Kim988f53c2019-09-16 15:59:01 +090086)
87
88func init() {
89 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
90 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Andrew Walbrana5deb732024-02-15 13:39:46 +000091 pctx.HostBinToolVariable("syspropRustCmd", "sysprop_rust")
Inseob Kim988f53c2019-09-16 15:59:01 +090092}
93
Inseob Kim07def122020-11-23 14:43:02 +090094// syspropJavaGenRule module generates srcjar containing generated java APIs.
95// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090096func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
97 var checkApiFileTimeStamp android.WritablePath
98
99 ctx.VisitDirectDeps(func(dep android.Module) {
100 if m, ok := dep.(*syspropLibrary); ok {
101 checkApiFileTimeStamp = m.checkApiFileTimeStamp
102 }
103 })
104
105 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
106 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
107
108 ctx.Build(pctx, android.BuildParams{
109 Rule: syspropJava,
110 Description: "sysprop_java " + syspropFile.Rel(),
111 Output: srcJarFile,
112 Input: syspropFile,
113 Implicit: checkApiFileTimeStamp,
114 Args: map[string]string{
115 "scope": g.properties.Scope,
116 },
117 })
118
119 g.genSrcjars = append(g.genSrcjars, srcJarFile)
120 }
121}
122
Colin Cross75ce9ec2021-02-26 16:20:32 -0800123func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
124 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
125 // the check API rule of the sysprop library.
126 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
127}
128
Inseob Kim988f53c2019-09-16 15:59:01 +0900129func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
130 switch tag {
131 case "":
132 return g.genSrcjars, nil
133 default:
134 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
135 }
136}
137
138func syspropJavaGenFactory() android.Module {
139 g := &syspropJavaGenRule{}
140 g.AddProperties(&g.properties)
141 android.InitAndroidModule(g)
142 return g
143}
144
Andrew Walbrana5deb732024-02-15 13:39:46 +0000145// syspropRustGenRule module generates rust source files containing generated rust APIs.
146// It also depends on check api rule, so api check has to pass to use sysprop_library.
147func (g *syspropRustGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
148 var checkApiFileTimeStamp android.WritablePath
149
150 ctx.VisitDirectDeps(func(dep android.Module) {
151 if m, ok := dep.(*syspropLibrary); ok {
152 checkApiFileTimeStamp = m.checkApiFileTimeStamp
153 }
154 })
155
156 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
Aleksei Vetrovf4775ca2024-03-07 19:24:47 +0000157 syspropDir := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcrust")
158 outputDir := syspropDir.Join(ctx, "src")
159 libPath := syspropDir.Join(ctx, "src", "lib.rs")
160 parsersPath := syspropDir.Join(ctx, "src", "gen_parsers_and_formatters.rs")
Andrew Walbrana5deb732024-02-15 13:39:46 +0000161
162 ctx.Build(pctx, android.BuildParams{
163 Rule: syspropRust,
164 Description: "sysprop_rust " + syspropFile.Rel(),
165 Outputs: android.WritablePaths{libPath, parsersPath},
166 Input: syspropFile,
167 Implicit: checkApiFileTimeStamp,
168 Args: map[string]string{
169 "scope": g.properties.Scope,
170 "out_dir": outputDir.String(),
171 },
172 })
173
174 g.genSrcs = append(g.genSrcs, libPath, parsersPath)
175 }
176}
177
178func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
179 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
180 // the check API rule of the sysprop library.
181 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
182}
183
184func (g *syspropRustGenRule) OutputFiles(_ string) (android.Paths, error) {
185 return g.genSrcs, nil
186}
187
188func syspropRustGenFactory() android.Module {
189 g := &syspropRustGenRule{}
190 g.AddProperties(&g.properties)
191 android.InitAndroidModule(g)
192 return g
193}
194
Inseob Kimc0907f12019-02-08 21:00:45 +0900195type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900196 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100197 android.ApexModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900198
Inseob Kim42882742019-07-30 17:55:33 +0900199 properties syspropLibraryProperties
200
201 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900202 latestApiFile android.OptionalPath
203 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900204 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900205}
206
207type syspropLibraryProperties struct {
208 // Determine who owns this sysprop library. Possible values are
209 // "Platform", "Vendor", or "Odm"
210 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900211
212 // list of package names that will be documented and publicized as API
213 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900214
Inseob Kim42882742019-07-30 17:55:33 +0900215 // If set to true, allow this module to be dexed and installed on devices.
216 Installable *bool
217
Inseob Kim9da1f812021-06-14 12:03:59 +0900218 // Make this module available when building for ramdisk
219 Ramdisk_available *bool
220
Inseob Kim42882742019-07-30 17:55:33 +0900221 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900222 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900223
224 // Make this module available when building for vendor
225 Vendor_available *bool
226
Justin Yun63e9ec72020-10-29 16:49:43 +0900227 // Make this module available when building for product
228 Product_available *bool
229
Inseob Kim42882742019-07-30 17:55:33 +0900230 // list of .sysprop files which defines the properties.
231 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900232
Inseob Kim89db15d2020-02-03 18:06:46 +0900233 // If set to true, build a variant of the module for the host. Defaults to false.
234 Host_supported *bool
235
Jooyung Han379660c2020-04-21 15:24:00 +0900236 Cpp struct {
237 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
238 // Forwarded to cc_library.min_sdk_version
239 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000240
241 // C compiler flags used to build library
242 Cflags []string
243
244 // Linker flags used to build binary
245 Ldflags []string
Jooyung Han379660c2020-04-21 15:24:00 +0900246 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900247
248 Java struct {
249 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
250 // Forwarded to java_library.min_sdk_version
251 Min_sdk_version *string
252 }
Andrew Walbrana5deb732024-02-15 13:39:46 +0000253
254 Rust struct {
255 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
256 // Forwarded to rust_library.min_sdk_version
257 Min_sdk_version *string
258 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900259}
260
261var (
Inseob Kim42882742019-07-30 17:55:33 +0900262 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900263 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900264
265 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
266 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900267)
268
Inseob Kim07def122020-11-23 14:43:02 +0900269// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900270func syspropLibraries(config android.Config) *[]string {
271 return config.Once(syspropLibrariesKey, func() interface{} {
272 return &[]string{}
273 }).(*[]string)
274}
275
276func SyspropLibraries(config android.Config) []string {
277 return append([]string{}, *syspropLibraries(config)...)
278}
279
Inseob Kimc0907f12019-02-08 21:00:45 +0900280func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000281 registerSyspropBuildComponents(android.InitRegistrationContext)
282}
283
284func registerSyspropBuildComponents(ctx android.RegistrationContext) {
285 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900286}
287
Inseob Kim42882742019-07-30 17:55:33 +0900288func (m *syspropLibrary) Name() string {
289 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900290}
291
Inseob Kimac1e9862019-12-09 18:15:47 +0900292func (m *syspropLibrary) Owner() string {
293 return m.properties.Property_owner
294}
295
Inseob Kim07def122020-11-23 14:43:02 +0900296func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900297 return "lib" + m.BaseModuleName()
298}
299
Colin Cross75ce9ec2021-02-26 16:20:32 -0800300func (m *syspropLibrary) javaPublicStubName() string {
301 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900302}
303
Inseob Kim988f53c2019-09-16 15:59:01 +0900304func (m *syspropLibrary) javaGenModuleName() string {
305 return m.BaseModuleName() + "_java_gen"
306}
307
Inseob Kimac1e9862019-12-09 18:15:47 +0900308func (m *syspropLibrary) javaGenPublicStubName() string {
309 return m.BaseModuleName() + "_java_gen_public"
310}
311
Andrew Walbrana5deb732024-02-15 13:39:46 +0000312func (m *syspropLibrary) rustGenModuleName() string {
313 return m.rustCrateName() + "_rust_gen"
314}
315
316func (m *syspropLibrary) rustGenStubName() string {
317 return "lib" + m.rustCrateName() + "_rust"
318}
319
320func (m *syspropLibrary) rustCrateName() string {
321 moduleName := strings.ToLower(m.BaseModuleName())
322 moduleName = strings.ReplaceAll(moduleName, "-", "_")
323 moduleName = strings.ReplaceAll(moduleName, ".", "_")
324 return moduleName
325}
326
Inseob Kim42882742019-07-30 17:55:33 +0900327func (m *syspropLibrary) BaseModuleName() string {
328 return m.ModuleBase.Name()
329}
330
Inseob Kimc9770d62021-01-15 18:04:20 +0900331func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900332 return m.currentApiFile
333}
334
Inseob Kim07def122020-11-23 14:43:02 +0900335// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
336// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900337func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900338 baseModuleName := m.BaseModuleName()
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000339 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
340 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900341 if syspropFile.Ext() != ".sysprop" {
342 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
343 }
344 }
Colin Cross40213022023-12-13 15:19:49 -0800345 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Inseob Kim988f53c2019-09-16 15:59:01 +0900346
347 if ctx.Failed() {
348 return
349 }
350
Inseob Kimc9770d62021-01-15 18:04:20 +0900351 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
352 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
353 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
354 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
355 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900356
357 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800358 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900359 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
360 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800361 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900362 Output(m.dumpedApiFile).
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000363 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800364 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900365
366 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800367 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900368
Inseob Kimc9770d62021-01-15 18:04:20 +0900369 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
370 // properties. But we have to feed current api file and latest api file to the rule builder.
371 // Currently we can't get android.Path representing the null device, so we add any existing API
372 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
373 // method.
374 var apiFileList android.Paths
375 currentApiArgument := os.DevNull
376 if m.currentApiFile.Valid() {
377 apiFileList = append(apiFileList, m.currentApiFile.Path())
378 currentApiArgument = m.currentApiFile.String()
379 }
380
381 latestApiArgument := os.DevNull
382 if m.latestApiFile.Valid() {
383 apiFileList = append(apiFileList, m.latestApiFile.Path())
384 latestApiArgument = m.latestApiFile.String()
385 }
386
Inseob Kim07def122020-11-23 14:43:02 +0900387 // 1. compares current.txt to api-dump.txt
388 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900389 msg := fmt.Sprintf(`\n******************************\n`+
390 `API of sysprop_library %s doesn't match with current.txt\n`+
391 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900392 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900393 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900394 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900395
396 rule.Command().
397 Text("( cmp").Flag("-s").
398 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900399 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900400 Text("|| ( echo").Flag("-e").
401 Flag(`"` + msg + `"`).
402 Text("; exit 38) )")
403
Inseob Kim07def122020-11-23 14:43:02 +0900404 // 2. compares current.txt to latest.txt (frozen API)
405 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900406 msg = fmt.Sprintf(`\n******************************\n`+
407 `API of sysprop_library %s doesn't match with latest version\n`+
408 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900409 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900410
411 rule.Command().
412 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800413 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900414 Text(latestApiArgument).
415 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900416 Text(" || ( echo").Flag("-e").
417 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900418 Text("; exit 38) )").
419 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900420
421 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
422
423 rule.Command().
424 Text("touch").
425 Output(m.checkApiFileTimeStamp)
426
Colin Crossf1a035e2020-11-16 17:32:30 -0800427 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900428}
429
430func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
431 return android.AndroidMkData{
432 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
433 // sysprop_library module itself is defined as a FAKE module to perform API check.
434 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800435 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
436 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Inseob Kim42882742019-07-30 17:55:33 +0900437 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
438 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
LaMont Jonesb5099382024-01-10 23:42:36 +0000439 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
440 for _, extra := range data.Extra {
441 extra(w, nil)
442 }
Inseob Kim42882742019-07-30 17:55:33 +0900443 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
444 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
445 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900446 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
447
448 // dump API rule
449 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900450
451 // check API rule
452 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900453 }}
454}
455
Jiyong Park45bf82e2020-12-15 22:29:02 +0900456var _ android.ApexModule = (*syspropLibrary)(nil)
457
458// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700459func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
460 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900461 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
462}
463
Inseob Kim42882742019-07-30 17:55:33 +0900464// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
465// Both Java and C++ modules can link against sysprop_library, and API stability check
466// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000467// is performed. Note that the generated C++ module has its name prefixed with
468// `lib`, and it is this module that should be depended on from other C++
469// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
470// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900471func syspropLibraryFactory() android.Module {
472 m := &syspropLibrary{}
473
474 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900475 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900476 )
Inseob Kim42882742019-07-30 17:55:33 +0900477 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100478 android.InitApexModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900479 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900480 return m
481}
482
Inseob Kimac1e9862019-12-09 18:15:47 +0900483type ccLibraryProperties struct {
484 Name *string
485 Srcs []string
486 Soc_specific *bool
487 Device_specific *bool
488 Product_specific *bool
489 Sysprop struct {
490 Platform *bool
491 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900492 Target struct {
493 Android struct {
494 Header_libs []string
495 Shared_libs []string
496 }
497 Host struct {
498 Static_libs []string
499 }
500 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900501 Required []string
502 Recovery *bool
503 Recovery_available *bool
504 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900505 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900506 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900507 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100508 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900509 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000510 Cflags []string
511 Ldflags []string
Inseob Kimac1e9862019-12-09 18:15:47 +0900512}
513
514type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800515 Name *string
516 Srcs []string
517 Soc_specific *bool
518 Device_specific *bool
519 Product_specific *bool
520 Required []string
521 Sdk_version *string
522 Installable *bool
523 Libs []string
524 Stem *string
525 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900526 Apex_available []string
527 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900528}
529
Andrew Walbrana5deb732024-02-15 13:39:46 +0000530type rustLibraryProperties struct {
531 Name *string
532 Srcs []string
533 Installable *bool
534 Crate_name string
535 Rustlibs []string
536 Vendor_available *bool
537 Product_available *bool
538 Apex_available []string
539 Min_sdk_version *string
540}
541
Inseob Kimc0907f12019-02-08 21:00:45 +0900542func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900543 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900544 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
545 }
546
Inseob Kimac1e9862019-12-09 18:15:47 +0900547 // ctx's Platform or Specific functions represent where this sysprop_library installed.
548 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
549 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900550 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900551 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900552 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900553
Inseob Kim07def122020-11-23 14:43:02 +0900554 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
555 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900556 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900557 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900558 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900559 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900560 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900561 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900562 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900563
Inseob Kimac1e9862019-12-09 18:15:47 +0900564 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900565 case "Platform":
566 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900567 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900568 case "Vendor":
569 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900570 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900571 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
572 "System can't access sysprop_library owned by Vendor")
573 }
574 case "Odm":
575 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900576 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900577 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
578 "Odm-defined properties should be accessed only in Vendor or Odm")
579 }
580 default:
581 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900582 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900583 }
584
Inseob Kim07def122020-11-23 14:43:02 +0900585 // Generate a C++ implementation library.
586 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900587 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900588 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900589 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900590 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
591 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
592 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
593 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900594 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
595 ccProps.Target.Android.Shared_libs = []string{"liblog"}
596 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900597 ccProps.Recovery_available = m.properties.Recovery_available
598 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900599 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900600 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900601 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100602 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900603 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000604 ccProps.Cflags = m.properties.Cpp.Cflags
605 ccProps.Ldflags = m.properties.Cpp.Ldflags
Colin Cross84dfc3d2019-09-25 11:33:01 -0700606 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900607
Inseob Kim988f53c2019-09-16 15:59:01 +0900608 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900609
Inseob Kimac1e9862019-12-09 18:15:47 +0900610 // We need to only use public version, if the partition where sysprop_library will be installed
611 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900612 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900613 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900614 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900615 } else if isOwnerPlatform && installedInVendorOrOdm {
616 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900617 scope = "public"
618 }
619
Inseob Kim07def122020-11-23 14:43:02 +0900620 // Generate a Java implementation library.
621 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
622 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900623 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800624 Srcs: m.properties.Srcs,
625 Scope: scope,
626 Name: proptools.StringPtr(m.javaGenModuleName()),
627 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900628 })
629
630 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
631 // and allow any modules (even from different partition) to link against the sysprop_library.
632 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800633 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900634 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800635 publicStub = m.javaPublicStubName()
636 }
637
638 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
639 Name: proptools.StringPtr(m.BaseModuleName()),
640 Srcs: []string{":" + m.javaGenModuleName()},
641 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
642 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
643 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
644 Installable: m.properties.Installable,
645 Sdk_version: proptools.StringPtr("core_current"),
646 Libs: []string{javaSyspropStub},
647 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900648 Apex_available: m.ApexProperties.Apex_available,
649 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800650 })
651
652 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900653 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800654 Srcs: m.properties.Srcs,
655 Scope: "public",
656 Name: proptools.StringPtr(m.javaGenPublicStubName()),
657 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900658 })
659
660 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800661 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900662 Srcs: []string{":" + m.javaGenPublicStubName()},
663 Installable: proptools.BoolPtr(false),
664 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900665 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900666 Stem: proptools.StringPtr(m.BaseModuleName()),
667 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900668 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900669
Andrew Walbrana5deb732024-02-15 13:39:46 +0000670 // Generate a Rust implementation library.
671 ctx.CreateModule(syspropRustGenFactory, &syspropGenProperties{
672 Srcs: m.properties.Srcs,
673 Scope: scope,
674 Name: proptools.StringPtr(m.rustGenModuleName()),
675 Check_api: proptools.StringPtr(ctx.ModuleName()),
676 })
677 rustProps := rustLibraryProperties{
678 Name: proptools.StringPtr(m.rustGenStubName()),
679 Srcs: []string{":" + m.rustGenModuleName()},
680 Installable: proptools.BoolPtr(false),
681 Crate_name: m.rustCrateName(),
682 Rustlibs: []string{
683 "librustutils",
684 },
685 Vendor_available: m.properties.Vendor_available,
686 Product_available: m.properties.Product_available,
687 Apex_available: m.ApexProperties.Apex_available,
688 Min_sdk_version: proptools.StringPtr("29"),
689 }
690 ctx.CreateModule(rust.RustLibraryFactory, &rustProps)
691
Inseob Kim07def122020-11-23 14:43:02 +0900692 // syspropLibraries will be used by property_contexts to check types.
693 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900694 if m.ExportedToMake() {
695 syspropLibrariesLock.Lock()
696 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900697
Inseob Kim69cf09e2020-05-04 19:28:25 +0900698 libraries := syspropLibraries(ctx.Config())
699 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
700 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900701}