blob: 13cf68f7d0acec1772eada656893226820a50b82 [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"
Trevor Radcliffe9b81d792023-09-29 15:22:52 +000027 "android/soong/sysprop/bp2build"
28 "android/soong/ui/metrics/bp2build_metrics_proto"
Liz Kammer12dc96e2023-08-11 14:16:05 -040029
Inseob Kimc0907f12019-02-08 21:00:45 +090030 "github.com/google/blueprint"
31 "github.com/google/blueprint/proptools"
Inseob Kim42882742019-07-30 17:55:33 +090032
33 "android/soong/android"
34 "android/soong/cc"
35 "android/soong/java"
Inseob Kimc0907f12019-02-08 21:00:45 +090036)
37
38type dependencyTag struct {
39 blueprint.BaseDependencyTag
40 name string
41}
42
Inseob Kim988f53c2019-09-16 15:59:01 +090043type syspropGenProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -080044 Srcs []string `android:"path"`
45 Scope string
46 Name *string
47 Check_api *string
Inseob Kim988f53c2019-09-16 15:59:01 +090048}
49
50type syspropJavaGenRule struct {
51 android.ModuleBase
52
53 properties syspropGenProperties
54
55 genSrcjars android.Paths
56}
57
58var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
59
60var (
61 syspropJava = pctx.AndroidStaticRule("syspropJava",
62 blueprint.RuleParams{
63 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
64 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
65 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
66 CommandDeps: []string{
67 "$syspropJavaCmd",
68 "$soongZipCmd",
69 },
70 }, "scope")
71)
72
73func init() {
74 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
75 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
Inseob Kim988f53c2019-09-16 15:59:01 +090076}
77
Inseob Kim07def122020-11-23 14:43:02 +090078// syspropJavaGenRule module generates srcjar containing generated java APIs.
79// It also depends on check api rule, so api check has to pass to use sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +090080func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
81 var checkApiFileTimeStamp android.WritablePath
82
83 ctx.VisitDirectDeps(func(dep android.Module) {
84 if m, ok := dep.(*syspropLibrary); ok {
85 checkApiFileTimeStamp = m.checkApiFileTimeStamp
86 }
87 })
88
89 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
90 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
91
92 ctx.Build(pctx, android.BuildParams{
93 Rule: syspropJava,
94 Description: "sysprop_java " + syspropFile.Rel(),
95 Output: srcJarFile,
96 Input: syspropFile,
97 Implicit: checkApiFileTimeStamp,
98 Args: map[string]string{
99 "scope": g.properties.Scope,
100 },
101 })
102
103 g.genSrcjars = append(g.genSrcjars, srcJarFile)
104 }
105}
106
Colin Cross75ce9ec2021-02-26 16:20:32 -0800107func (g *syspropJavaGenRule) DepsMutator(ctx android.BottomUpMutatorContext) {
108 // Add a dependency from the stubs to sysprop library so that the generator rule can depend on
109 // the check API rule of the sysprop library.
110 ctx.AddFarVariationDependencies(nil, nil, proptools.String(g.properties.Check_api))
111}
112
Inseob Kim988f53c2019-09-16 15:59:01 +0900113func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
114 switch tag {
115 case "":
116 return g.genSrcjars, nil
117 default:
118 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
119 }
120}
121
122func syspropJavaGenFactory() android.Module {
123 g := &syspropJavaGenRule{}
124 g.AddProperties(&g.properties)
125 android.InitAndroidModule(g)
126 return g
127}
128
Inseob Kimc0907f12019-02-08 21:00:45 +0900129type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900130 android.ModuleBase
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100131 android.ApexModuleBase
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000132 android.BazelModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900133
Inseob Kim42882742019-07-30 17:55:33 +0900134 properties syspropLibraryProperties
135
136 checkApiFileTimeStamp android.WritablePath
Inseob Kimc9770d62021-01-15 18:04:20 +0900137 latestApiFile android.OptionalPath
138 currentApiFile android.OptionalPath
Inseob Kim42882742019-07-30 17:55:33 +0900139 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900140}
141
142type syspropLibraryProperties struct {
143 // Determine who owns this sysprop library. Possible values are
144 // "Platform", "Vendor", or "Odm"
145 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900146
147 // list of package names that will be documented and publicized as API
148 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900149
Inseob Kim42882742019-07-30 17:55:33 +0900150 // If set to true, allow this module to be dexed and installed on devices.
151 Installable *bool
152
Inseob Kim9da1f812021-06-14 12:03:59 +0900153 // Make this module available when building for ramdisk
154 Ramdisk_available *bool
155
Inseob Kim42882742019-07-30 17:55:33 +0900156 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900157 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900158
159 // Make this module available when building for vendor
160 Vendor_available *bool
161
Justin Yun63e9ec72020-10-29 16:49:43 +0900162 // Make this module available when building for product
163 Product_available *bool
164
Inseob Kim42882742019-07-30 17:55:33 +0900165 // list of .sysprop files which defines the properties.
166 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900167
Inseob Kim89db15d2020-02-03 18:06:46 +0900168 // If set to true, build a variant of the module for the host. Defaults to false.
169 Host_supported *bool
170
Jooyung Han379660c2020-04-21 15:24:00 +0900171 Cpp struct {
172 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
173 // Forwarded to cc_library.min_sdk_version
174 Min_sdk_version *string
175 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900176
177 Java struct {
178 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
179 // Forwarded to java_library.min_sdk_version
180 Min_sdk_version *string
181 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900182}
183
184var (
Inseob Kim42882742019-07-30 17:55:33 +0900185 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900186 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900187
188 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
189 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900190)
191
Inseob Kim07def122020-11-23 14:43:02 +0900192// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900193func syspropLibraries(config android.Config) *[]string {
194 return config.Once(syspropLibrariesKey, func() interface{} {
195 return &[]string{}
196 }).(*[]string)
197}
198
199func SyspropLibraries(config android.Config) []string {
200 return append([]string{}, *syspropLibraries(config)...)
201}
202
Inseob Kimc0907f12019-02-08 21:00:45 +0900203func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000204 registerSyspropBuildComponents(android.InitRegistrationContext)
205}
206
207func registerSyspropBuildComponents(ctx android.RegistrationContext) {
208 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900209}
210
Inseob Kim42882742019-07-30 17:55:33 +0900211func (m *syspropLibrary) Name() string {
212 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900213}
214
Inseob Kimac1e9862019-12-09 18:15:47 +0900215func (m *syspropLibrary) Owner() string {
216 return m.properties.Property_owner
217}
218
Inseob Kim07def122020-11-23 14:43:02 +0900219func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900220 return "lib" + m.BaseModuleName()
221}
222
Colin Cross75ce9ec2021-02-26 16:20:32 -0800223func (m *syspropLibrary) javaPublicStubName() string {
224 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900225}
226
Inseob Kim988f53c2019-09-16 15:59:01 +0900227func (m *syspropLibrary) javaGenModuleName() string {
228 return m.BaseModuleName() + "_java_gen"
229}
230
Inseob Kimac1e9862019-12-09 18:15:47 +0900231func (m *syspropLibrary) javaGenPublicStubName() string {
232 return m.BaseModuleName() + "_java_gen_public"
233}
234
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000235func (m *syspropLibrary) bp2buildJavaImplementationModuleName() string {
236 return m.BaseModuleName() + "_java_library"
237}
238
Inseob Kim42882742019-07-30 17:55:33 +0900239func (m *syspropLibrary) BaseModuleName() string {
240 return m.ModuleBase.Name()
241}
242
Inseob Kimc9770d62021-01-15 18:04:20 +0900243func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900244 return m.currentApiFile
245}
246
Inseob Kim07def122020-11-23 14:43:02 +0900247// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
248// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900249func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900250 baseModuleName := m.BaseModuleName()
251
252 for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
253 if syspropFile.Ext() != ".sysprop" {
254 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
255 }
256 }
257
258 if ctx.Failed() {
259 return
260 }
261
Inseob Kimc9770d62021-01-15 18:04:20 +0900262 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
263 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
264 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
265 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
266 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900267
268 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800269 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900270 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
271 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800272 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900273 Output(m.dumpedApiFile).
274 Inputs(android.PathsForModuleSrc(ctx, m.properties.Srcs))
Colin Crossf1a035e2020-11-16 17:32:30 -0800275 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900276
277 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800278 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900279
Inseob Kimc9770d62021-01-15 18:04:20 +0900280 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
281 // properties. But we have to feed current api file and latest api file to the rule builder.
282 // Currently we can't get android.Path representing the null device, so we add any existing API
283 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
284 // method.
285 var apiFileList android.Paths
286 currentApiArgument := os.DevNull
287 if m.currentApiFile.Valid() {
288 apiFileList = append(apiFileList, m.currentApiFile.Path())
289 currentApiArgument = m.currentApiFile.String()
290 }
291
292 latestApiArgument := os.DevNull
293 if m.latestApiFile.Valid() {
294 apiFileList = append(apiFileList, m.latestApiFile.Path())
295 latestApiArgument = m.latestApiFile.String()
296 }
297
Inseob Kim07def122020-11-23 14:43:02 +0900298 // 1. compares current.txt to api-dump.txt
299 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900300 msg := fmt.Sprintf(`\n******************************\n`+
301 `API of sysprop_library %s doesn't match with current.txt\n`+
302 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900303 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900304 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900305 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900306
307 rule.Command().
308 Text("( cmp").Flag("-s").
309 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900310 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900311 Text("|| ( echo").Flag("-e").
312 Flag(`"` + msg + `"`).
313 Text("; exit 38) )")
314
Inseob Kim07def122020-11-23 14:43:02 +0900315 // 2. compares current.txt to latest.txt (frozen API)
316 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900317 msg = fmt.Sprintf(`\n******************************\n`+
318 `API of sysprop_library %s doesn't match with latest version\n`+
319 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900320 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900321
322 rule.Command().
323 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800324 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900325 Text(latestApiArgument).
326 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900327 Text(" || ( echo").Flag("-e").
328 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900329 Text("; exit 38) )").
330 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900331
332 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
333
334 rule.Command().
335 Text("touch").
336 Output(m.checkApiFileTimeStamp)
337
Colin Crossf1a035e2020-11-16 17:32:30 -0800338 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900339}
340
341func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
342 return android.AndroidMkData{
343 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
344 // sysprop_library module itself is defined as a FAKE module to perform API check.
345 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800346 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
347 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Bob Badourb4999222021-01-07 03:34:31 +0000348 data.Entries.WriteLicenseVariables(w)
Inseob Kim42882742019-07-30 17:55:33 +0900349 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
350 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
351 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
352 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
353 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900354 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
355
356 // dump API rule
357 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900358
359 // check API rule
360 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900361 }}
362}
363
Jiyong Park45bf82e2020-12-15 22:29:02 +0900364var _ android.ApexModule = (*syspropLibrary)(nil)
365
366// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700367func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
368 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900369 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
370}
371
Inseob Kim42882742019-07-30 17:55:33 +0900372// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
373// Both Java and C++ modules can link against sysprop_library, and API stability check
374// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000375// is performed. Note that the generated C++ module has its name prefixed with
376// `lib`, and it is this module that should be depended on from other C++
377// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
378// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900379func syspropLibraryFactory() android.Module {
380 m := &syspropLibrary{}
381
382 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900383 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900384 )
Inseob Kim42882742019-07-30 17:55:33 +0900385 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100386 android.InitApexModule(m)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000387 android.InitBazelModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900388 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900389 return m
390}
391
Inseob Kimac1e9862019-12-09 18:15:47 +0900392type ccLibraryProperties struct {
393 Name *string
394 Srcs []string
395 Soc_specific *bool
396 Device_specific *bool
397 Product_specific *bool
398 Sysprop struct {
399 Platform *bool
400 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900401 Target struct {
402 Android struct {
403 Header_libs []string
404 Shared_libs []string
405 }
406 Host struct {
407 Static_libs []string
408 }
409 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900410 Required []string
411 Recovery *bool
412 Recovery_available *bool
413 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900414 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900415 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900416 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100417 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900418 Min_sdk_version *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000419 Bazel_module struct {
Liz Kammer12dc96e2023-08-11 14:16:05 -0400420 Label *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000421 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900422}
423
424type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800425 Name *string
426 Srcs []string
427 Soc_specific *bool
428 Device_specific *bool
429 Product_specific *bool
430 Required []string
431 Sdk_version *string
432 Installable *bool
433 Libs []string
434 Stem *string
435 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900436 Apex_available []string
437 Min_sdk_version *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400438 Bazel_module struct {
439 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000440 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400441 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900442}
443
Inseob Kimc0907f12019-02-08 21:00:45 +0900444func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900445 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900446 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
447 }
448
Inseob Kimac1e9862019-12-09 18:15:47 +0900449 // ctx's Platform or Specific functions represent where this sysprop_library installed.
450 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
451 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900452 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900453 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900454 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900455
Inseob Kim07def122020-11-23 14:43:02 +0900456 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
457 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900458 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900459 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900460 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900461 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900462 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900463 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900464 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900465
Inseob Kimac1e9862019-12-09 18:15:47 +0900466 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900467 case "Platform":
468 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900469 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900470 case "Vendor":
471 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900472 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900473 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
474 "System can't access sysprop_library owned by Vendor")
475 }
476 case "Odm":
477 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900478 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900479 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
480 "Odm-defined properties should be accessed only in Vendor or Odm")
481 }
482 default:
483 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900484 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900485 }
486
Liz Kammer12dc96e2023-08-11 14:16:05 -0400487 var label *string
488 if b, ok := ctx.Module().(android.Bazelable); ok && b.ShouldConvertWithBp2build(ctx) {
489 // TODO: b/295566168 - this will need to change once build files are checked in to account for
490 // checked in modules in mixed builds
491 label = proptools.StringPtr(
492 fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.CcImplementationModuleName()))
493 }
494
Inseob Kim07def122020-11-23 14:43:02 +0900495 // Generate a C++ implementation library.
496 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900497 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900498 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900499 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900500 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
501 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
502 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
503 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900504 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
505 ccProps.Target.Android.Shared_libs = []string{"liblog"}
506 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900507 ccProps.Recovery_available = m.properties.Recovery_available
508 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900509 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900510 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900511 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100512 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900513 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Liz Kammer12dc96e2023-08-11 14:16:05 -0400514 ccProps.Bazel_module.Label = label
Colin Cross84dfc3d2019-09-25 11:33:01 -0700515 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900516
Inseob Kim988f53c2019-09-16 15:59:01 +0900517 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900518
Inseob Kimac1e9862019-12-09 18:15:47 +0900519 // We need to only use public version, if the partition where sysprop_library will be installed
520 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900521 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900522 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900523 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900524 } else if isOwnerPlatform && installedInVendorOrOdm {
525 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900526 scope = "public"
527 }
528
Inseob Kim07def122020-11-23 14:43:02 +0900529 // Generate a Java implementation library.
530 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
531 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900532 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800533 Srcs: m.properties.Srcs,
534 Scope: scope,
535 Name: proptools.StringPtr(m.javaGenModuleName()),
536 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900537 })
538
539 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
540 // and allow any modules (even from different partition) to link against the sysprop_library.
541 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800542 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900543 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800544 publicStub = m.javaPublicStubName()
545 }
546
547 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
548 Name: proptools.StringPtr(m.BaseModuleName()),
549 Srcs: []string{":" + m.javaGenModuleName()},
550 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
551 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
552 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
553 Installable: m.properties.Installable,
554 Sdk_version: proptools.StringPtr("core_current"),
555 Libs: []string{javaSyspropStub},
556 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900557 Apex_available: m.ApexProperties.Apex_available,
558 Min_sdk_version: m.properties.Java.Min_sdk_version,
Liz Kammer12dc96e2023-08-11 14:16:05 -0400559 Bazel_module: struct {
560 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000561 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400562 }{
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000563 Label: proptools.StringPtr(
564 fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.bp2buildJavaImplementationModuleName())),
Liz Kammer12dc96e2023-08-11 14:16:05 -0400565 },
Colin Cross75ce9ec2021-02-26 16:20:32 -0800566 })
567
568 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900569 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800570 Srcs: m.properties.Srcs,
571 Scope: "public",
572 Name: proptools.StringPtr(m.javaGenPublicStubName()),
573 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900574 })
575
576 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800577 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900578 Srcs: []string{":" + m.javaGenPublicStubName()},
579 Installable: proptools.BoolPtr(false),
580 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900581 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900582 Stem: proptools.StringPtr(m.BaseModuleName()),
Liz Kammer12dc96e2023-08-11 14:16:05 -0400583 Bazel_module: struct {
584 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000585 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400586 }{
587 Bp2build_available: proptools.BoolPtr(false),
588 },
Inseob Kimac1e9862019-12-09 18:15:47 +0900589 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900590 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900591
Inseob Kim07def122020-11-23 14:43:02 +0900592 // syspropLibraries will be used by property_contexts to check types.
593 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900594 if m.ExportedToMake() {
595 syspropLibrariesLock.Lock()
596 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900597
Inseob Kim69cf09e2020-05-04 19:28:25 +0900598 libraries := syspropLibraries(ctx.Config())
599 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
600 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900601}
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000602
603// TODO(b/240463568): Additional properties will be added for API validation
Chris Parsons637458d2023-09-19 20:09:00 +0000604func (m *syspropLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000605 if m.Owner() != "Platform" {
606 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "Only sysprop libraries owned by platform are supported at this time")
607 return
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000608 }
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000609 labels := bp2build.SyspropLibraryLabels{
610 SyspropLibraryLabel: m.BaseModuleName(),
611 CcSharedLibraryLabel: m.CcImplementationModuleName(),
612 CcStaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()),
613 JavaLibraryLabel: m.bp2buildJavaImplementationModuleName(),
614 }
615 bp2build.Bp2buildBaseSyspropLibrary(ctx, labels.SyspropLibraryLabel, bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)))
616 bp2build.Bp2buildSyspropCc(ctx, labels, m.properties.Cpp.Min_sdk_version)
617 bp2build.Bp2buildSyspropJava(ctx, labels, m.properties.Java.Min_sdk_version)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000618}