blob: 1f0d28d568d36e15115e86ab0414264827bfe5da [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()
244
245 for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
246 if syspropFile.Ext() != ".sysprop" {
247 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
248 }
249 }
250
251 if ctx.Failed() {
252 return
253 }
254
Inseob Kimc9770d62021-01-15 18:04:20 +0900255 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
256 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
257 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
258 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
259 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900260
261 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800262 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900263 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
264 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800265 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900266 Output(m.dumpedApiFile).
267 Inputs(android.PathsForModuleSrc(ctx, m.properties.Srcs))
Colin Crossf1a035e2020-11-16 17:32:30 -0800268 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900269
270 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800271 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900272
Inseob Kimc9770d62021-01-15 18:04:20 +0900273 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
274 // properties. But we have to feed current api file and latest api file to the rule builder.
275 // Currently we can't get android.Path representing the null device, so we add any existing API
276 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
277 // method.
278 var apiFileList android.Paths
279 currentApiArgument := os.DevNull
280 if m.currentApiFile.Valid() {
281 apiFileList = append(apiFileList, m.currentApiFile.Path())
282 currentApiArgument = m.currentApiFile.String()
283 }
284
285 latestApiArgument := os.DevNull
286 if m.latestApiFile.Valid() {
287 apiFileList = append(apiFileList, m.latestApiFile.Path())
288 latestApiArgument = m.latestApiFile.String()
289 }
290
Inseob Kim07def122020-11-23 14:43:02 +0900291 // 1. compares current.txt to api-dump.txt
292 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900293 msg := fmt.Sprintf(`\n******************************\n`+
294 `API of sysprop_library %s doesn't match with current.txt\n`+
295 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900296 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900297 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900298 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900299
300 rule.Command().
301 Text("( cmp").Flag("-s").
302 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900303 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900304 Text("|| ( echo").Flag("-e").
305 Flag(`"` + msg + `"`).
306 Text("; exit 38) )")
307
Inseob Kim07def122020-11-23 14:43:02 +0900308 // 2. compares current.txt to latest.txt (frozen API)
309 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900310 msg = fmt.Sprintf(`\n******************************\n`+
311 `API of sysprop_library %s doesn't match with latest version\n`+
312 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900313 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900314
315 rule.Command().
316 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800317 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900318 Text(latestApiArgument).
319 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900320 Text(" || ( echo").Flag("-e").
321 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900322 Text("; exit 38) )").
323 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900324
325 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
326
327 rule.Command().
328 Text("touch").
329 Output(m.checkApiFileTimeStamp)
330
Colin Crossf1a035e2020-11-16 17:32:30 -0800331 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900332}
333
334func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
335 return android.AndroidMkData{
336 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
337 // sysprop_library module itself is defined as a FAKE module to perform API check.
338 // Actual implementation libraries are created on LoadHookMutator
339 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
340 fmt.Fprintf(w, "LOCAL_MODULE := %s\n", m.Name())
Bob Badourb4999222021-01-07 03:34:31 +0000341 data.Entries.WriteLicenseVariables(w)
Inseob Kim42882742019-07-30 17:55:33 +0900342 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
343 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
344 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
345 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
346 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900347 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
348
349 // dump API rule
350 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900351
352 // check API rule
353 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900354 }}
355}
356
Jiyong Park45bf82e2020-12-15 22:29:02 +0900357var _ android.ApexModule = (*syspropLibrary)(nil)
358
359// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700360func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
361 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900362 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
363}
364
Inseob Kim42882742019-07-30 17:55:33 +0900365// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
366// Both Java and C++ modules can link against sysprop_library, and API stability check
367// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000368// is performed. Note that the generated C++ module has its name prefixed with
369// `lib`, and it is this module that should be depended on from other C++
370// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
371// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900372func syspropLibraryFactory() android.Module {
373 m := &syspropLibrary{}
374
375 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900376 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900377 )
Inseob Kim42882742019-07-30 17:55:33 +0900378 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100379 android.InitApexModule(m)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000380 android.InitBazelModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900381 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900382 return m
383}
384
Inseob Kimac1e9862019-12-09 18:15:47 +0900385type ccLibraryProperties struct {
386 Name *string
387 Srcs []string
388 Soc_specific *bool
389 Device_specific *bool
390 Product_specific *bool
391 Sysprop struct {
392 Platform *bool
393 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900394 Target struct {
395 Android struct {
396 Header_libs []string
397 Shared_libs []string
398 }
399 Host struct {
400 Static_libs []string
401 }
402 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900403 Required []string
404 Recovery *bool
405 Recovery_available *bool
406 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900407 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900408 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900409 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100410 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900411 Min_sdk_version *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000412 Bazel_module struct {
413 Bp2build_available *bool
414 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900415}
416
417type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800418 Name *string
419 Srcs []string
420 Soc_specific *bool
421 Device_specific *bool
422 Product_specific *bool
423 Required []string
424 Sdk_version *string
425 Installable *bool
426 Libs []string
427 Stem *string
428 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900429 Apex_available []string
430 Min_sdk_version *string
Inseob Kimac1e9862019-12-09 18:15:47 +0900431}
432
Inseob Kimc0907f12019-02-08 21:00:45 +0900433func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900434 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900435 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
436 }
437
Inseob Kimac1e9862019-12-09 18:15:47 +0900438 // ctx's Platform or Specific functions represent where this sysprop_library installed.
439 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
440 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900441 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900442 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900443 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900444
Inseob Kim07def122020-11-23 14:43:02 +0900445 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
446 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900447 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900448 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900449 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900450 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900451 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900452 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900453 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900454
Inseob Kimac1e9862019-12-09 18:15:47 +0900455 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900456 case "Platform":
457 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900458 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900459 case "Vendor":
460 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900461 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900462 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
463 "System can't access sysprop_library owned by Vendor")
464 }
465 case "Odm":
466 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900467 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900468 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
469 "Odm-defined properties should be accessed only in Vendor or Odm")
470 }
471 default:
472 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900473 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900474 }
475
Inseob Kim07def122020-11-23 14:43:02 +0900476 // Generate a C++ implementation library.
477 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900478 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900479 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900480 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900481 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
482 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
483 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
484 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900485 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
486 ccProps.Target.Android.Shared_libs = []string{"liblog"}
487 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900488 ccProps.Recovery_available = m.properties.Recovery_available
489 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900490 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900491 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900492 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100493 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900494 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000495 // A Bazel macro handles this, so this module does not need to be handled
496 // in bp2build
497 // TODO(b/237810289) perhaps do something different here so that we aren't
498 // also disabling these modules in mixed builds
499 ccProps.Bazel_module.Bp2build_available = proptools.BoolPtr(false)
Colin Cross84dfc3d2019-09-25 11:33:01 -0700500 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900501
Inseob Kim988f53c2019-09-16 15:59:01 +0900502 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900503
Inseob Kimac1e9862019-12-09 18:15:47 +0900504 // We need to only use public version, if the partition where sysprop_library will be installed
505 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900506 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900507 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900508 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900509 } else if isOwnerPlatform && installedInVendorOrOdm {
510 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900511 scope = "public"
512 }
513
Inseob Kim07def122020-11-23 14:43:02 +0900514 // Generate a Java implementation library.
515 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
516 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900517 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800518 Srcs: m.properties.Srcs,
519 Scope: scope,
520 Name: proptools.StringPtr(m.javaGenModuleName()),
521 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900522 })
523
524 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
525 // and allow any modules (even from different partition) to link against the sysprop_library.
526 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800527 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900528 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800529 publicStub = m.javaPublicStubName()
530 }
531
532 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
533 Name: proptools.StringPtr(m.BaseModuleName()),
534 Srcs: []string{":" + m.javaGenModuleName()},
535 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
536 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
537 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
538 Installable: m.properties.Installable,
539 Sdk_version: proptools.StringPtr("core_current"),
540 Libs: []string{javaSyspropStub},
541 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900542 Apex_available: m.ApexProperties.Apex_available,
543 Min_sdk_version: m.properties.Java.Min_sdk_version,
Colin Cross75ce9ec2021-02-26 16:20:32 -0800544 })
545
546 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900547 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800548 Srcs: m.properties.Srcs,
549 Scope: "public",
550 Name: proptools.StringPtr(m.javaGenPublicStubName()),
551 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900552 })
553
554 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800555 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900556 Srcs: []string{":" + m.javaGenPublicStubName()},
557 Installable: proptools.BoolPtr(false),
558 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900559 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900560 Stem: proptools.StringPtr(m.BaseModuleName()),
561 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900562 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900563
Inseob Kim07def122020-11-23 14:43:02 +0900564 // syspropLibraries will be used by property_contexts to check types.
565 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900566 if m.ExportedToMake() {
567 syspropLibrariesLock.Lock()
568 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900569
Inseob Kim69cf09e2020-05-04 19:28:25 +0900570 libraries := syspropLibraries(ctx.Config())
571 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
572 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900573}
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000574
575// TODO(b/240463568): Additional properties will be added for API validation
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000576func (m *syspropLibrary) ConvertWithBp2build(ctx android.TopDownMutatorContext) {
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000577 labels := cc.SyspropLibraryLabels{
578 SyspropLibraryLabel: m.BaseModuleName(),
579 SharedLibraryLabel: m.CcImplementationModuleName(),
580 StaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()),
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000581 }
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000582 cc.Bp2buildSysprop(ctx,
583 labels,
584 bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)),
585 m.properties.Cpp.Min_sdk_version)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000586}