blob: d16bf32f973abc315cc9647f706e0a4559fa0863 [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"
Liz Kammer12dc96e2023-08-11 14:16:05 -040027
Inseob Kimc0907f12019-02-08 21:00:45 +090028 "github.com/google/blueprint"
29 "github.com/google/blueprint/proptools"
Inseob Kim42882742019-07-30 17:55:33 +090030
31 "android/soong/android"
32 "android/soong/cc"
33 "android/soong/java"
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
56var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
57
58var (
59 syspropJava = pctx.AndroidStaticRule("syspropJava",
60 blueprint.RuleParams{
61 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
62 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
63 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
64 CommandDeps: []string{
65 "$syspropJavaCmd",
66 "$soongZipCmd",
67 },
68 }, "scope")
69)
70
71func init() {
72 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
73 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Inseob Kim988f53c2019-09-16 15:59:01 +090074}
75
Inseob Kim07def122020-11-23 14:43:02 +090076// syspropJavaGenRule module generates srcjar containing generated java APIs.
77// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090078func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
79 var checkApiFileTimeStamp android.WritablePath
80
81 ctx.VisitDirectDeps(func(dep android.Module) {
82 if m, ok := dep.(*syspropLibrary); ok {
83 checkApiFileTimeStamp = m.checkApiFileTimeStamp
84 }
85 })
86
87 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
88 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
89
90 ctx.Build(pctx, android.BuildParams{
91 Rule: syspropJava,
92 Description: "sysprop_java " + syspropFile.Rel(),
93 Output: srcJarFile,
94 Input: syspropFile,
95 Implicit: checkApiFileTimeStamp,
96 Args: map[string]string{
97 "scope": g.properties.Scope,
98 },
99 })
100
101 g.genSrcjars = append(g.genSrcjars, srcJarFile)
102 }
103}
104
Colin Cross75ce9ec2021-02-26 16:20:32 -0800105func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
106 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
107 // the check API rule of the sysprop library.
108 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
109}
110
Inseob Kim988f53c2019-09-16 15:59:01 +0900111func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
112 switch tag {
113 case "":
114 return g.genSrcjars, nil
115 default:
116 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
117 }
118}
119
120func syspropJavaGenFactory() android.Module {
121 g := &syspropJavaGenRule{}
122 g.AddProperties(&g.properties)
123 android.InitAndroidModule(g)
124 return g
125}
126
Inseob Kimc0907f12019-02-08 21:00:45 +0900127type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900128 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100129 android.ApexModuleBase
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000130 android.BazelModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900131
Inseob Kim42882742019-07-30 17:55:33 +0900132 properties syspropLibraryProperties
133
134 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900135 latestApiFile android.OptionalPath
136 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900137 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900138}
139
140type syspropLibraryProperties struct {
141 // Determine who owns this sysprop library. Possible values are
142 // "Platform", "Vendor", or "Odm"
143 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900144
145 // list of package names that will be documented and publicized as API
146 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900147
Inseob Kim42882742019-07-30 17:55:33 +0900148 // If set to true, allow this module to be dexed and installed on devices.
149 Installable *bool
150
Inseob Kim9da1f812021-06-14 12:03:59 +0900151 // Make this module available when building for ramdisk
152 Ramdisk_available *bool
153
Inseob Kim42882742019-07-30 17:55:33 +0900154 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900155 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900156
157 // Make this module available when building for vendor
158 Vendor_available *bool
159
Justin Yun63e9ec72020-10-29 16:49:43 +0900160 // Make this module available when building for product
161 Product_available *bool
162
Inseob Kim42882742019-07-30 17:55:33 +0900163 // list of .sysprop files which defines the properties.
164 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900165
Inseob Kim89db15d2020-02-03 18:06:46 +0900166 // If set to true, build a variant of the module for the host. Defaults to false.
167 Host_supported *bool
168
Jooyung Han379660c2020-04-21 15:24:00 +0900169 Cpp struct {
170 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
171 // Forwarded to cc_library.min_sdk_version
172 Min_sdk_version *string
173 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900174
175 Java struct {
176 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
177 // Forwarded to java_library.min_sdk_version
178 Min_sdk_version *string
179 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900180}
181
182var (
Inseob Kim42882742019-07-30 17:55:33 +0900183 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900184 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900185
186 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
187 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900188)
189
Inseob Kim07def122020-11-23 14:43:02 +0900190// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900191func syspropLibraries(config android.Config) *[]string {
192 return config.Once(syspropLibrariesKey, func() interface{} {
193 return &[]string{}
194 }).(*[]string)
195}
196
197func SyspropLibraries(config android.Config) []string {
198 return append([]string{}, *syspropLibraries(config)...)
199}
200
Inseob Kimc0907f12019-02-08 21:00:45 +0900201func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000202 registerSyspropBuildComponents(android.InitRegistrationContext)
203}
204
205func registerSyspropBuildComponents(ctx android.RegistrationContext) {
206 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900207}
208
Inseob Kim42882742019-07-30 17:55:33 +0900209func (m *syspropLibrary) Name() string {
210 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900211}
212
Inseob Kimac1e9862019-12-09 18:15:47 +0900213func (m *syspropLibrary) Owner() string {
214 return m.properties.Property_owner
215}
216
Inseob Kim07def122020-11-23 14:43:02 +0900217func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900218 return "lib" + m.BaseModuleName()
219}
220
Colin Cross75ce9ec2021-02-26 16:20:32 -0800221func (m *syspropLibrary) javaPublicStubName() string {
222 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900223}
224
Inseob Kim988f53c2019-09-16 15:59:01 +0900225func (m *syspropLibrary) javaGenModuleName() string {
226 return m.BaseModuleName() + "_java_gen"
227}
228
Inseob Kimac1e9862019-12-09 18:15:47 +0900229func (m *syspropLibrary) javaGenPublicStubName() string {
230 return m.BaseModuleName() + "_java_gen_public"
231}
232
Inseob Kim42882742019-07-30 17:55:33 +0900233func (m *syspropLibrary) BaseModuleName() string {
234 return m.ModuleBase.Name()
235}
236
Inseob Kimc9770d62021-01-15 18:04:20 +0900237func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900238 return m.currentApiFile
239}
240
Inseob Kim07def122020-11-23 14:43:02 +0900241// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
242// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900243func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900244 baseModuleName := m.BaseModuleName()
245
246 for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
247 if syspropFile.Ext() != ".sysprop" {
248 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
249 }
250 }
251
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).
268 Inputs(android.PathsForModuleSrc(ctx, m.properties.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 {
Liz Kammer12dc96e2023-08-11 14:16:05 -0400414 Label *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000415 }
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
Liz Kammer12dc96e2023-08-11 14:16:05 -0400432 Bazel_module struct {
433 Bp2build_available *bool
434 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900435}
436
Inseob Kimc0907f12019-02-08 21:00:45 +0900437func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900438 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900439 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
440 }
441
Inseob Kimac1e9862019-12-09 18:15:47 +0900442 // ctx's Platform or Specific functions represent where this sysprop_library installed.
443 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
444 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900445 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900446 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900447 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900448
Inseob Kim07def122020-11-23 14:43:02 +0900449 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
450 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900451 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900452 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900453 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900454 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900455 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900456 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900457 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900458
Inseob Kimac1e9862019-12-09 18:15:47 +0900459 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900460 case "Platform":
461 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900462 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900463 case "Vendor":
464 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900465 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900466 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
467 "System can't access sysprop_library owned by Vendor")
468 }
469 case "Odm":
470 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900471 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900472 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
473 "Odm-defined properties should be accessed only in Vendor or Odm")
474 }
475 default:
476 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900477 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900478 }
479
Liz Kammer12dc96e2023-08-11 14:16:05 -0400480 var label *string
481 if b, ok := ctx.Module().(android.Bazelable); ok && b.ShouldConvertWithBp2build(ctx) {
482 // TODO: b/295566168 - this will need to change once build files are checked in to account for
483 // checked in modules in mixed builds
484 label = proptools.StringPtr(
485 fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.CcImplementationModuleName()))
486 }
487
Inseob Kim07def122020-11-23 14:43:02 +0900488 // Generate a C++ implementation library.
489 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900490 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900491 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900492 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900493 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
494 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
495 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
496 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900497 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
498 ccProps.Target.Android.Shared_libs = []string{"liblog"}
499 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900500 ccProps.Recovery_available = m.properties.Recovery_available
501 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900502 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900503 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900504 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100505 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900506 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Liz Kammer12dc96e2023-08-11 14:16:05 -0400507 ccProps.Bazel_module.Label = label
Colin Cross84dfc3d2019-09-25 11:33:01 -0700508 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900509
Inseob Kim988f53c2019-09-16 15:59:01 +0900510 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900511
Inseob Kimac1e9862019-12-09 18:15:47 +0900512 // We need to only use public version, if the partition where sysprop_library will be installed
513 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900514 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900515 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900516 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900517 } else if isOwnerPlatform && installedInVendorOrOdm {
518 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900519 scope = "public"
520 }
521
Inseob Kim07def122020-11-23 14:43:02 +0900522 // Generate a Java implementation library.
523 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
524 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900525 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800526 Srcs: m.properties.Srcs,
527 Scope: scope,
528 Name: proptools.StringPtr(m.javaGenModuleName()),
529 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900530 })
531
532 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
533 // and allow any modules (even from different partition) to link against the sysprop_library.
534 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800535 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900536 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800537 publicStub = m.javaPublicStubName()
538 }
539
540 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
541 Name: proptools.StringPtr(m.BaseModuleName()),
542 Srcs: []string{":" + m.javaGenModuleName()},
543 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
544 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
545 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
546 Installable: m.properties.Installable,
547 Sdk_version: proptools.StringPtr("core_current"),
548 Libs: []string{javaSyspropStub},
549 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900550 Apex_available: m.ApexProperties.Apex_available,
551 Min_sdk_version: m.properties.Java.Min_sdk_version,
Liz Kammer12dc96e2023-08-11 14:16:05 -0400552 Bazel_module: struct {
553 Bp2build_available *bool
554 }{
555 Bp2build_available: proptools.BoolPtr(false),
556 },
Colin Cross75ce9ec2021-02-26 16:20:32 -0800557 })
558
559 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900560 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800561 Srcs: m.properties.Srcs,
562 Scope: "public",
563 Name: proptools.StringPtr(m.javaGenPublicStubName()),
564 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900565 })
566
567 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800568 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900569 Srcs: []string{":" + m.javaGenPublicStubName()},
570 Installable: proptools.BoolPtr(false),
571 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900572 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900573 Stem: proptools.StringPtr(m.BaseModuleName()),
Liz Kammer12dc96e2023-08-11 14:16:05 -0400574 Bazel_module: struct {
575 Bp2build_available *bool
576 }{
577 Bp2build_available: proptools.BoolPtr(false),
578 },
Inseob Kimac1e9862019-12-09 18:15:47 +0900579 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900580 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900581
Inseob Kim07def122020-11-23 14:43:02 +0900582 // syspropLibraries will be used by property_contexts to check types.
583 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900584 if m.ExportedToMake() {
585 syspropLibrariesLock.Lock()
586 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900587
Inseob Kim69cf09e2020-05-04 19:28:25 +0900588 libraries := syspropLibraries(ctx.Config())
589 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
590 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900591}
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000592
593// TODO(b/240463568): Additional properties will be added for API validation
Chris Parsons637458d2023-09-19 20:09:00 +0000594func (m *syspropLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000595 labels := cc.SyspropLibraryLabels{
596 SyspropLibraryLabel: m.BaseModuleName(),
597 SharedLibraryLabel: m.CcImplementationModuleName(),
598 StaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()),
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000599 }
Trevor Radcliffecee4e052022-09-06 19:31:25 +0000600 cc.Bp2buildSysprop(ctx,
601 labels,
602 bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)),
603 m.properties.Cpp.Min_sdk_version)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000604}