blob: 61fabae2b3fd563e53b177f8e449d5cce46bc406 [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"
Inseob Kim628d7ef2020-03-21 03:38:32 +090024 "sync"
Colin Crossf8b860a2019-04-16 14:43:28 -070025
Trevor Radcliffead3d1232022-09-01 16:25:10 +000026 "android/soong/bazel"
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"
Inseob Kimc0907f12019-02-08 21:00:45 +090033)
34
35type dependencyTag struct {
36 blueprint.BaseDependencyTag
37 name string
38}
39
Inseob Kim988f53c2019-09-16 15:59:01 +090040type syspropGenProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -080041 Srcs []string `android:"path"`
42 Scope string
43 Name *string
44 Check_api *string
Inseob Kim988f53c2019-09-16 15:59:01 +090045}
46
47type syspropJavaGenRule struct {
48 android.ModuleBase
49
50 properties syspropGenProperties
51
52 genSrcjars android.Paths
53}
54
55var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
56
57var (
58 syspropJava = pctx.AndroidStaticRule("syspropJava",
59 blueprint.RuleParams{
60 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
61 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
62 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
63 CommandDeps: []string{
64 "$syspropJavaCmd",
65 "$soongZipCmd",
66 },
67 }, "scope")
68)
69
70func init() {
71 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
72 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Inseob Kim988f53c2019-09-16 15:59:01 +090073}
74
Inseob Kim07def122020-11-23 14:43:02 +090075// syspropJavaGenRule module generates srcjar containing generated java APIs.
76// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090077func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
78 var checkApiFileTimeStamp android.WritablePath
79
80 ctx.VisitDirectDeps(func(dep android.Module) {
81 if m, ok := dep.(*syspropLibrary); ok {
82 checkApiFileTimeStamp = m.checkApiFileTimeStamp
83 }
84 })
85
86 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
87 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
88
89 ctx.Build(pctx, android.BuildParams{
90 Rule: syspropJava,
91 Description: "sysprop_java " + syspropFile.Rel(),
92 Output: srcJarFile,
93 Input: syspropFile,
94 Implicit: checkApiFileTimeStamp,
95 Args: map[string]string{
96 "scope": g.properties.Scope,
97 },
98 })
99
100 g.genSrcjars = append(g.genSrcjars, srcJarFile)
101 }
102}
103
Colin Cross75ce9ec2021-02-26 16:20:32 -0800104func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
105 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
106 // the check API rule of the sysprop library.
107 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
108}
109
Inseob Kim988f53c2019-09-16 15:59:01 +0900110func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
111 switch tag {
112 case "":
113 return g.genSrcjars, nil
114 default:
115 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
116 }
117}
118
119func syspropJavaGenFactory() android.Module {
120 g := &syspropJavaGenRule{}
121 g.AddProperties(&g.properties)
122 android.InitAndroidModule(g)
123 return g
124}
125
Inseob Kimc0907f12019-02-08 21:00:45 +0900126type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900127 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100128 android.ApexModuleBase
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000129 android.BazelModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900130
Inseob Kim42882742019-07-30 17:55:33 +0900131 properties syspropLibraryProperties
132
133 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900134 latestApiFile android.OptionalPath
135 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900136 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900137}
138
139type syspropLibraryProperties struct {
140 // Determine who owns this sysprop library. Possible values are
141 // "Platform", "Vendor", or "Odm"
142 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900143
144 // list of package names that will be documented and publicized as API
145 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900146
Inseob Kim42882742019-07-30 17:55:33 +0900147 // If set to true, allow this module to be dexed and installed on devices.
148 Installable *bool
149
Inseob Kim9da1f812021-06-14 12:03:59 +0900150 // Make this module available when building for ramdisk
151 Ramdisk_available *bool
152
Inseob Kim42882742019-07-30 17:55:33 +0900153 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900154 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900155
156 // Make this module available when building for vendor
157 Vendor_available *bool
158
Justin Yun63e9ec72020-10-29 16:49:43 +0900159 // Make this module available when building for product
160 Product_available *bool
161
Inseob Kim42882742019-07-30 17:55:33 +0900162 // list of .sysprop files which defines the properties.
163 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900164
Inseob Kim89db15d2020-02-03 18:06:46 +0900165 // If set to true, build a variant of the module for the host. Defaults to false.
166 Host_supported *bool
167
Jooyung Han379660c2020-04-21 15:24:00 +0900168 Cpp struct {
169 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
170 // Forwarded to cc_library.min_sdk_version
171 Min_sdk_version *string
172 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900173
174 Java struct {
175 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
176 // Forwarded to java_library.min_sdk_version
177 Min_sdk_version *string
178 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900179}
180
181var (
Inseob Kim42882742019-07-30 17:55:33 +0900182 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900183 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900184
185 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
186 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900187)
188
Inseob Kim07def122020-11-23 14:43:02 +0900189// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900190func syspropLibraries(config android.Config) *[]string {
191 return config.Once(syspropLibrariesKey, func() interface{} {
192 return &[]string{}
193 }).(*[]string)
194}
195
196func SyspropLibraries(config android.Config) []string {
197 return append([]string{}, *syspropLibraries(config)...)
198}
199
Inseob Kimc0907f12019-02-08 21:00:45 +0900200func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000201 registerSyspropBuildComponents(android.InitRegistrationContext)
202}
203
204func registerSyspropBuildComponents(ctx android.RegistrationContext) {
205 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900206}
207
Inseob Kim42882742019-07-30 17:55:33 +0900208func (m *syspropLibrary) Name() string {
209 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900210}
211
Inseob Kimac1e9862019-12-09 18:15:47 +0900212func (m *syspropLibrary) Owner() string {
213 return m.properties.Property_owner
214}
215
Inseob Kim07def122020-11-23 14:43:02 +0900216func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900217 return "lib" + m.BaseModuleName()
218}
219
Colin Cross75ce9ec2021-02-26 16:20:32 -0800220func (m *syspropLibrary) javaPublicStubName() string {
221 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900222}
223
Inseob Kim988f53c2019-09-16 15:59:01 +0900224func (m *syspropLibrary) javaGenModuleName() string {
225 return m.BaseModuleName() + "_java_gen"
226}
227
Inseob Kimac1e9862019-12-09 18:15:47 +0900228func (m *syspropLibrary) javaGenPublicStubName() string {
229 return m.BaseModuleName() + "_java_gen_public"
230}
231
Inseob Kim42882742019-07-30 17:55:33 +0900232func (m *syspropLibrary) BaseModuleName() string {
233 return m.ModuleBase.Name()
234}
235
Inseob Kimc9770d62021-01-15 18:04:20 +0900236func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900237 return m.currentApiFile
238}
239
Inseob Kim07def122020-11-23 14:43:02 +0900240// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
241// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900242func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900243 baseModuleName := m.BaseModuleName()
Aditya Choudhary95e2a3c2023-11-29 16:42:42 +0000244 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
245 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900246 if syspropFile.Ext() != ".sysprop" {
247 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
248 }
249 }
Aditya Choudhary95e2a3c2023-11-29 16:42:42 +0000250 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Inseob Kim988f53c2019-09-16 15:59:01 +0900251
252 if ctx.Failed() {
253 return
254 }
255
Inseob Kimc9770d62021-01-15 18:04:20 +0900256 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
257 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
258 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
259 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
260 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900261
262 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800263 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900264 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
265 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800266 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900267 Output(m.dumpedApiFile).
Aditya Choudhary95e2a3c2023-11-29 16:42:42 +0000268 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800269 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900270
271 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800272 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900273
Inseob Kimc9770d62021-01-15 18:04:20 +0900274 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
275 // properties. But we have to feed current api file and latest api file to the rule builder.
276 // Currently we can't get android.Path representing the null device, so we add any existing API
277 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
278 // method.
279 var apiFileList android.Paths
280 currentApiArgument := os.DevNull
281 if m.currentApiFile.Valid() {
282 apiFileList = append(apiFileList, m.currentApiFile.Path())
283 currentApiArgument = m.currentApiFile.String()
284 }
285
286 latestApiArgument := os.DevNull
287 if m.latestApiFile.Valid() {
288 apiFileList = append(apiFileList, m.latestApiFile.Path())
289 latestApiArgument = m.latestApiFile.String()
290 }
291
Inseob Kim07def122020-11-23 14:43:02 +0900292 // 1. compares current.txt to api-dump.txt
293 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900294 msg := fmt.Sprintf(`\n******************************\n`+
295 `API of sysprop_library %s doesn't match with current.txt\n`+
296 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900297 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900298 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900299 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900300
301 rule.Command().
302 Text("( cmp").Flag("-s").
303 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900304 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900305 Text("|| ( echo").Flag("-e").
306 Flag(`"` + msg + `"`).
307 Text("; exit 38) )")
308
Inseob Kim07def122020-11-23 14:43:02 +0900309 // 2. compares current.txt to latest.txt (frozen API)
310 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900311 msg = fmt.Sprintf(`\n******************************\n`+
312 `API of sysprop_library %s doesn't match with latest version\n`+
313 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900314 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900315
316 rule.Command().
317 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800318 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900319 Text(latestApiArgument).
320 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900321 Text(" || ( echo").Flag("-e").
322 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900323 Text("; exit 38) )").
324 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900325
326 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
327
328 rule.Command().
329 Text("touch").
330 Output(m.checkApiFileTimeStamp)
331
Colin Crossf1a035e2020-11-16 17:32:30 -0800332 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900333}
334
335func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
336 return android.AndroidMkData{
337 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
338 // sysprop_library module itself is defined as a FAKE module to perform API check.
339 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800340 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
341 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Bob Badourb4999222021-01-07 03:34:31 +0000342 data.Entries.WriteLicenseVariables(w)
Inseob Kim42882742019-07-30 17:55:33 +0900343 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
344 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
345 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
346 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
347 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900348 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
349
350 // dump API rule
351 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900352
353 // check API rule
354 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900355 }}
356}
357
Jiyong Park45bf82e2020-12-15 22:29:02 +0900358var _ android.ApexModule = (*syspropLibrary)(nil)
359
360// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700361func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
362 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900363 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
364}
365
Inseob Kim42882742019-07-30 17:55:33 +0900366// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
367// Both Java and C++ modules can link against sysprop_library, and API stability check
368// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000369// is performed. Note that the generated C++ module has its name prefixed with
370// `lib`, and it is this module that should be depended on from other C++
371// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
372// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900373func syspropLibraryFactory() android.Module {
374 m := &syspropLibrary{}
375
376 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900377 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900378 )
Inseob Kim42882742019-07-30 17:55:33 +0900379 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100380 android.InitApexModule(m)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000381 android.InitBazelModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900382 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900383 return m
384}
385
Inseob Kimac1e9862019-12-09 18:15:47 +0900386type ccLibraryProperties struct {
387 Name *string
388 Srcs []string
389 Soc_specific *bool
390 Device_specific *bool
391 Product_specific *bool
392 Sysprop struct {
393 Platform *bool
394 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900395 Target struct {
396 Android struct {
397 Header_libs []string
398 Shared_libs []string
399 }
400 Host struct {
401 Static_libs []string
402 }
403 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900404 Required []string
405 Recovery *bool
406 Recovery_available *bool
407 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900408 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900409 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900410 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100411 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900412 Min_sdk_version *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000413 Bazel_module struct {
414 Bp2build_available *bool
415 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900416}
417
418type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800419 Name *string
420 Srcs []string
421 Soc_specific *bool
422 Device_specific *bool
423 Product_specific *bool
424 Required []string
425 Sdk_version *string
426 Installable *bool
427 Libs []string
428 Stem *string
429 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900430 Apex_available []string
431 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900432}
433
Inseob Kimc0907f12019-02-08 21:00:45 +0900434func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900435 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900436 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
437 }
438
Inseob Kimac1e9862019-12-09 18:15:47 +0900439 // ctx's Platform or Specific functions represent where this sysprop_library installed.
440 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
441 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900442 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900443 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900444 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900445
Inseob Kim07def122020-11-23 14:43:02 +0900446 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
447 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900448 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900449 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900450 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900451 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900452 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900453 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900454 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900455
Inseob Kimac1e9862019-12-09 18:15:47 +0900456 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900457 case "Platform":
458 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900459 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900460 case "Vendor":
461 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900462 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900463 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
464 "System can't access sysprop_library owned by Vendor")
465 }
466 case "Odm":
467 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900468 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900469 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
470 "Odm-defined properties should be accessed only in Vendor or Odm")
471 }
472 default:
473 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900474 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900475 }
476
Inseob Kim07def122020-11-23 14:43:02 +0900477 // Generate a C++ implementation library.
478 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900479 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900480 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900481 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900482 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
483 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
484 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
485 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900486 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
487 ccProps.Target.Android.Shared_libs = []string{"liblog"}
488 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900489 ccProps.Recovery_available = m.properties.Recovery_available
490 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900491 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900492 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900493 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100494 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900495 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000496 // A Bazel macro handles this, so this module does not need to be handled
497 // in bp2build
498 // TODO(b/237810289) perhaps do something different here so that we aren't
499 // also disabling these modules in mixed builds
500 ccProps.Bazel_module.Bp2build_available = proptools.BoolPtr(false)
Colin Cross84dfc3d2019-09-25 11:33:01 -0700501 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900502
Inseob Kim988f53c2019-09-16 15:59:01 +0900503 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900504
Inseob Kimac1e9862019-12-09 18:15:47 +0900505 // We need to only use public version, if the partition where sysprop_library will be installed
506 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900507 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900508 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900509 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900510 } else if isOwnerPlatform && installedInVendorOrOdm {
511 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900512 scope = "public"
513 }
514
Inseob Kim07def122020-11-23 14:43:02 +0900515 // Generate a Java implementation library.
516 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
517 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900518 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800519 Srcs: m.properties.Srcs,
520 Scope: scope,
521 Name: proptools.StringPtr(m.javaGenModuleName()),
522 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900523 })
524
525 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
526 // and allow any modules (even from different partition) to link against the sysprop_library.
527 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800528 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900529 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800530 publicStub = m.javaPublicStubName()
531 }
532
533 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
534 Name: proptools.StringPtr(m.BaseModuleName()),
535 Srcs: []string{":" + m.javaGenModuleName()},
536 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
537 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
538 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
539 Installable: m.properties.Installable,
540 Sdk_version: proptools.StringPtr("core_current"),
541 Libs: []string{javaSyspropStub},
542 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900543 Apex_available: m.ApexProperties.Apex_available,
544 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800545 })
546
547 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900548 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800549 Srcs: m.properties.Srcs,
550 Scope: "public",
551 Name: proptools.StringPtr(m.javaGenPublicStubName()),
552 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900553 })
554
555 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800556 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900557 Srcs: []string{":" + m.javaGenPublicStubName()},
558 Installable: proptools.BoolPtr(false),
559 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900560 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900561 Stem: proptools.StringPtr(m.BaseModuleName()),
562 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900563 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900564
Inseob Kim07def122020-11-23 14:43:02 +0900565 // syspropLibraries will be used by property_contexts to check types.
566 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900567 if m.ExportedToMake() {
568 syspropLibrariesLock.Lock()
569 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900570
Inseob Kim69cf09e2020-05-04 19:28:25 +0900571 libraries := syspropLibraries(ctx.Config())
572 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
573 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900574}
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000575
576// TODO(b/240463568): Additional properties will be added for API validation
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000577func (m *syspropLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000578 labels := cc.SyspropLibraryLabels{
579 SyspropLibraryLabel: m.BaseModuleName(),
580 SharedLibraryLabel: m.CcImplementationModuleName(),
581 StaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()),
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000582 }
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000583 cc.Bp2buildSysprop(ctx,
584 labels,
585 bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)),
586 m.properties.Cpp.Min_sdk_version)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000587}