blob: 698aaf29a0afbaa0ec0179399e7e374ab9f9b255 [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")
Andrew Walbrana5deb732024-02-15 13:39:46 +0000160
161 ctx.Build(pctx, android.BuildParams{
162 Rule: syspropRust,
163 Description: "sysprop_rust " + syspropFile.Rel(),
Andrew Walbrand3f500d2024-03-08 17:35:53 +0000164 Outputs: android.WritablePaths{libPath},
Andrew Walbrana5deb732024-02-15 13:39:46 +0000165 Input: syspropFile,
166 Implicit: checkApiFileTimeStamp,
167 Args: map[string]string{
168 "scope": g.properties.Scope,
169 "out_dir": outputDir.String(),
170 },
171 })
172
Andrew Walbrand3f500d2024-03-08 17:35:53 +0000173 g.genSrcs = append(g.genSrcs, libPath)
Andrew Walbrana5deb732024-02-15 13:39:46 +0000174 }
175}
176
177func (g *syspropRustGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
178 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
179 // the check API rule of the sysprop library.
180 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
181}
182
183func (g *syspropRustGenRule) OutputFiles(_ string) (android.Paths, error) {
184 return g.genSrcs, nil
185}
186
187func syspropRustGenFactory() android.Module {
188 g := &syspropRustGenRule{}
189 g.AddProperties(&g.properties)
190 android.InitAndroidModule(g)
191 return g
192}
193
Inseob Kimc0907f12019-02-08 21:00:45 +0900194type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900195 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100196 android.ApexModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900197
Inseob Kim42882742019-07-30 17:55:33 +0900198 properties syspropLibraryProperties
199
200 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900201 latestApiFile android.OptionalPath
202 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900203 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900204}
205
206type syspropLibraryProperties struct {
207 // Determine who owns this sysprop library. Possible values are
208 // "Platform", "Vendor", or "Odm"
209 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900210
211 // list of package names that will be documented and publicized as API
212 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900213
Inseob Kim42882742019-07-30 17:55:33 +0900214 // If set to true, allow this module to be dexed and installed on devices.
215 Installable *bool
216
Inseob Kim9da1f812021-06-14 12:03:59 +0900217 // Make this module available when building for ramdisk
218 Ramdisk_available *bool
219
Inseob Kim42882742019-07-30 17:55:33 +0900220 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900221 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900222
223 // Make this module available when building for vendor
224 Vendor_available *bool
225
Justin Yun63e9ec72020-10-29 16:49:43 +0900226 // Make this module available when building for product
227 Product_available *bool
228
Inseob Kim42882742019-07-30 17:55:33 +0900229 // list of .sysprop files which defines the properties.
230 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900231
Inseob Kim89db15d2020-02-03 18:06:46 +0900232 // If set to true, build a variant of the module for the host. Defaults to false.
233 Host_supported *bool
234
Jooyung Han379660c2020-04-21 15:24:00 +0900235 Cpp struct {
236 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
237 // Forwarded to cc_library.min_sdk_version
238 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000239
240 // C compiler flags used to build library
241 Cflags []string
242
243 // Linker flags used to build binary
244 Ldflags []string
Jooyung Han379660c2020-04-21 15:24:00 +0900245 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900246
247 Java struct {
248 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
249 // Forwarded to java_library.min_sdk_version
250 Min_sdk_version *string
251 }
Andrew Walbrana5deb732024-02-15 13:39:46 +0000252
253 Rust struct {
254 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
255 // Forwarded to rust_library.min_sdk_version
256 Min_sdk_version *string
257 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900258}
259
260var (
Inseob Kim42882742019-07-30 17:55:33 +0900261 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900262 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900263
264 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
265 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900266)
267
Inseob Kim07def122020-11-23 14:43:02 +0900268// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900269func syspropLibraries(config android.Config) *[]string {
270 return config.Once(syspropLibrariesKey, func() interface{} {
271 return &[]string{}
272 }).(*[]string)
273}
274
275func SyspropLibraries(config android.Config) []string {
276 return append([]string{}, *syspropLibraries(config)...)
277}
278
Inseob Kimc0907f12019-02-08 21:00:45 +0900279func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000280 registerSyspropBuildComponents(android.InitRegistrationContext)
281}
282
283func registerSyspropBuildComponents(ctx android.RegistrationContext) {
284 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900285}
286
Inseob Kim42882742019-07-30 17:55:33 +0900287func (m *syspropLibrary) Name() string {
288 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900289}
290
Inseob Kimac1e9862019-12-09 18:15:47 +0900291func (m *syspropLibrary) Owner() string {
292 return m.properties.Property_owner
293}
294
Inseob Kim07def122020-11-23 14:43:02 +0900295func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900296 return "lib" + m.BaseModuleName()
297}
298
Colin Cross75ce9ec2021-02-26 16:20:32 -0800299func (m *syspropLibrary) javaPublicStubName() string {
300 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900301}
302
Inseob Kim988f53c2019-09-16 15:59:01 +0900303func (m *syspropLibrary) javaGenModuleName() string {
304 return m.BaseModuleName() + "_java_gen"
305}
306
Inseob Kimac1e9862019-12-09 18:15:47 +0900307func (m *syspropLibrary) javaGenPublicStubName() string {
308 return m.BaseModuleName() + "_java_gen_public"
309}
310
Andrew Walbrana5deb732024-02-15 13:39:46 +0000311func (m *syspropLibrary) rustGenModuleName() string {
312 return m.rustCrateName() + "_rust_gen"
313}
314
315func (m *syspropLibrary) rustGenStubName() string {
316 return "lib" + m.rustCrateName() + "_rust"
317}
318
319func (m *syspropLibrary) rustCrateName() string {
320 moduleName := strings.ToLower(m.BaseModuleName())
321 moduleName = strings.ReplaceAll(moduleName, "-", "_")
322 moduleName = strings.ReplaceAll(moduleName, ".", "_")
323 return moduleName
324}
325
Inseob Kim42882742019-07-30 17:55:33 +0900326func (m *syspropLibrary) BaseModuleName() string {
327 return m.ModuleBase.Name()
328}
329
Inseob Kimc9770d62021-01-15 18:04:20 +0900330func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900331 return m.currentApiFile
332}
333
Inseob Kim07def122020-11-23 14:43:02 +0900334// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
335// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900336func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900337 baseModuleName := m.BaseModuleName()
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000338 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
339 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900340 if syspropFile.Ext() != ".sysprop" {
341 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
342 }
343 }
Colin Cross40213022023-12-13 15:19:49 -0800344 android.SetProvider(ctx, blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Inseob Kim988f53c2019-09-16 15:59:01 +0900345
346 if ctx.Failed() {
347 return
348 }
349
Inseob Kimc9770d62021-01-15 18:04:20 +0900350 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
351 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
352 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
353 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
354 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900355
356 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800357 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900358 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
359 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800360 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900361 Output(m.dumpedApiFile).
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000362 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800363 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900364
365 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800366 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900367
Inseob Kimc9770d62021-01-15 18:04:20 +0900368 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
369 // properties. But we have to feed current api file and latest api file to the rule builder.
370 // Currently we can't get android.Path representing the null device, so we add any existing API
371 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
372 // method.
373 var apiFileList android.Paths
374 currentApiArgument := os.DevNull
375 if m.currentApiFile.Valid() {
376 apiFileList = append(apiFileList, m.currentApiFile.Path())
377 currentApiArgument = m.currentApiFile.String()
378 }
379
380 latestApiArgument := os.DevNull
381 if m.latestApiFile.Valid() {
382 apiFileList = append(apiFileList, m.latestApiFile.Path())
383 latestApiArgument = m.latestApiFile.String()
384 }
385
Inseob Kim07def122020-11-23 14:43:02 +0900386 // 1. compares current.txt to api-dump.txt
387 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900388 msg := fmt.Sprintf(`\n******************************\n`+
389 `API of sysprop_library %s doesn't match with current.txt\n`+
390 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900391 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900392 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900393 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900394
395 rule.Command().
396 Text("( cmp").Flag("-s").
397 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900398 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900399 Text("|| ( echo").Flag("-e").
400 Flag(`"` + msg + `"`).
401 Text("; exit 38) )")
402
Inseob Kim07def122020-11-23 14:43:02 +0900403 // 2. compares current.txt to latest.txt (frozen API)
404 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900405 msg = fmt.Sprintf(`\n******************************\n`+
406 `API of sysprop_library %s doesn't match with latest version\n`+
407 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900408 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900409
410 rule.Command().
411 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800412 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900413 Text(latestApiArgument).
414 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900415 Text(" || ( echo").Flag("-e").
416 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900417 Text("; exit 38) )").
418 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900419
420 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
421
422 rule.Command().
423 Text("touch").
424 Output(m.checkApiFileTimeStamp)
425
Colin Crossf1a035e2020-11-16 17:32:30 -0800426 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900427}
428
429func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
430 return android.AndroidMkData{
431 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
432 // sysprop_library module itself is defined as a FAKE module to perform API check.
433 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800434 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
435 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Inseob Kim42882742019-07-30 17:55:33 +0900436 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
437 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
LaMont Jonesb5099382024-01-10 23:42:36 +0000438 // AconfigUpdateAndroidMkData may have added elements to Extra. Process them here.
439 for _, extra := range data.Extra {
440 extra(w, nil)
441 }
Inseob Kim42882742019-07-30 17:55:33 +0900442 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
443 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
444 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900445 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
446
447 // dump API rule
448 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900449
450 // check API rule
451 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900452 }}
453}
454
Jiyong Park45bf82e2020-12-15 22:29:02 +0900455var _ android.ApexModule = (*syspropLibrary)(nil)
456
457// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700458func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
459 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900460 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
461}
462
Inseob Kim42882742019-07-30 17:55:33 +0900463// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
464// Both Java and C++ modules can link against sysprop_library, and API stability check
465// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000466// is performed. Note that the generated C++ module has its name prefixed with
467// `lib`, and it is this module that should be depended on from other C++
468// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
469// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900470func syspropLibraryFactory() android.Module {
471 m := &syspropLibrary{}
472
473 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900474 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900475 )
Inseob Kim42882742019-07-30 17:55:33 +0900476 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100477 android.InitApexModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900478 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900479 return m
480}
481
Inseob Kimac1e9862019-12-09 18:15:47 +0900482type ccLibraryProperties struct {
483 Name *string
484 Srcs []string
485 Soc_specific *bool
486 Device_specific *bool
487 Product_specific *bool
488 Sysprop struct {
489 Platform *bool
490 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900491 Target struct {
492 Android struct {
493 Header_libs []string
494 Shared_libs []string
495 }
496 Host struct {
497 Static_libs []string
498 }
499 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900500 Required []string
501 Recovery *bool
502 Recovery_available *bool
503 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900504 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900505 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900506 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100507 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900508 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000509 Cflags []string
510 Ldflags []string
Inseob Kimac1e9862019-12-09 18:15:47 +0900511}
512
513type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800514 Name *string
515 Srcs []string
516 Soc_specific *bool
517 Device_specific *bool
518 Product_specific *bool
519 Required []string
520 Sdk_version *string
521 Installable *bool
522 Libs []string
523 Stem *string
524 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900525 Apex_available []string
526 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900527}
528
Andrew Walbrana5deb732024-02-15 13:39:46 +0000529type rustLibraryProperties struct {
530 Name *string
531 Srcs []string
532 Installable *bool
533 Crate_name string
534 Rustlibs []string
535 Vendor_available *bool
536 Product_available *bool
537 Apex_available []string
538 Min_sdk_version *string
539}
540
Inseob Kimc0907f12019-02-08 21:00:45 +0900541func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900542 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900543 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
544 }
545
Inseob Kimac1e9862019-12-09 18:15:47 +0900546 // ctx's Platform or Specific functions represent where this sysprop_library installed.
547 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
548 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900549 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900550 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900551 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900552
Inseob Kim07def122020-11-23 14:43:02 +0900553 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
554 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900555 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900556 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900557 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900558 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900559 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900560 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900561 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900562
Inseob Kimac1e9862019-12-09 18:15:47 +0900563 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900564 case "Platform":
565 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900566 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900567 case "Vendor":
568 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900569 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900570 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
571 "System can't access sysprop_library owned by Vendor")
572 }
573 case "Odm":
574 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900575 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900576 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
577 "Odm-defined properties should be accessed only in Vendor or Odm")
578 }
579 default:
580 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900581 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900582 }
583
Inseob Kim07def122020-11-23 14:43:02 +0900584 // Generate a C++ implementation library.
585 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900586 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900587 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900588 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900589 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
590 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
591 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
592 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900593 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
594 ccProps.Target.Android.Shared_libs = []string{"liblog"}
595 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900596 ccProps.Recovery_available = m.properties.Recovery_available
597 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900598 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900599 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900600 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100601 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900602 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000603 ccProps.Cflags = m.properties.Cpp.Cflags
604 ccProps.Ldflags = m.properties.Cpp.Ldflags
Colin Cross84dfc3d2019-09-25 11:33:01 -0700605 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900606
Inseob Kim988f53c2019-09-16 15:59:01 +0900607 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900608
Inseob Kimac1e9862019-12-09 18:15:47 +0900609 // We need to only use public version, if the partition where sysprop_library will be installed
610 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900611 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900612 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900613 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900614 } else if isOwnerPlatform && installedInVendorOrOdm {
615 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900616 scope = "public"
617 }
618
Inseob Kim07def122020-11-23 14:43:02 +0900619 // Generate a Java implementation library.
620 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
621 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900622 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800623 Srcs: m.properties.Srcs,
624 Scope: scope,
625 Name: proptools.StringPtr(m.javaGenModuleName()),
626 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900627 })
628
629 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
630 // and allow any modules (even from different partition) to link against the sysprop_library.
631 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800632 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900633 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800634 publicStub = m.javaPublicStubName()
635 }
636
637 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
638 Name: proptools.StringPtr(m.BaseModuleName()),
639 Srcs: []string{":" + m.javaGenModuleName()},
640 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
641 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
642 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
643 Installable: m.properties.Installable,
644 Sdk_version: proptools.StringPtr("core_current"),
645 Libs: []string{javaSyspropStub},
646 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900647 Apex_available: m.ApexProperties.Apex_available,
648 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800649 })
650
651 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900652 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800653 Srcs: m.properties.Srcs,
654 Scope: "public",
655 Name: proptools.StringPtr(m.javaGenPublicStubName()),
656 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900657 })
658
659 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800660 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900661 Srcs: []string{":" + m.javaGenPublicStubName()},
662 Installable: proptools.BoolPtr(false),
663 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900664 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900665 Stem: proptools.StringPtr(m.BaseModuleName()),
666 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900667 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900668
Andrew Walbrana5deb732024-02-15 13:39:46 +0000669 // Generate a Rust implementation library.
670 ctx.CreateModule(syspropRustGenFactory, &syspropGenProperties{
671 Srcs: m.properties.Srcs,
672 Scope: scope,
673 Name: proptools.StringPtr(m.rustGenModuleName()),
674 Check_api: proptools.StringPtr(ctx.ModuleName()),
675 })
676 rustProps := rustLibraryProperties{
677 Name: proptools.StringPtr(m.rustGenStubName()),
678 Srcs: []string{":" + m.rustGenModuleName()},
679 Installable: proptools.BoolPtr(false),
680 Crate_name: m.rustCrateName(),
681 Rustlibs: []string{
682 "librustutils",
683 },
684 Vendor_available: m.properties.Vendor_available,
685 Product_available: m.properties.Product_available,
686 Apex_available: m.ApexProperties.Apex_available,
687 Min_sdk_version: proptools.StringPtr("29"),
688 }
689 ctx.CreateModule(rust.RustLibraryFactory, &rustProps)
690
Inseob Kim07def122020-11-23 14:43:02 +0900691 // syspropLibraries will be used by property_contexts to check types.
692 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900693 if m.ExportedToMake() {
694 syspropLibrariesLock.Lock()
695 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900696
Inseob Kim69cf09e2020-05-04 19:28:25 +0900697 libraries := syspropLibraries(ctx.Config())
698 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
699 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900700}