blob: 013624fc4daaaab302252380736ab20a67a249bc [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
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000175
176 // C compiler flags used to build library
177 Cflags []string
178
179 // Linker flags used to build binary
180 Ldflags []string
Jooyung Han379660c2020-04-21 15:24:00 +0900181 }
Jiyong Park5e914b22021-03-08 10:09:52 +0900182
183 Java struct {
184 // Minimum sdk version that the artifact should support when it runs as part of mainline modules(APEX).
185 // Forwarded to java_library.min_sdk_version
186 Min_sdk_version *string
187 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900188}
189
190var (
Inseob Kim42882742019-07-30 17:55:33 +0900191 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900192 syspropCcTag = dependencyTag{name: "syspropCc"}
Inseob Kim628d7ef2020-03-21 03:38:32 +0900193
194 syspropLibrariesKey = android.NewOnceKey("syspropLibraries")
195 syspropLibrariesLock sync.Mutex
Inseob Kimc0907f12019-02-08 21:00:45 +0900196)
197
Inseob Kim07def122020-11-23 14:43:02 +0900198// List of sysprop_library used by property_contexts to perform type check.
Inseob Kim628d7ef2020-03-21 03:38:32 +0900199func syspropLibraries(config android.Config) *[]string {
200 return config.Once(syspropLibrariesKey, func() interface{} {
201 return &[]string{}
202 }).(*[]string)
203}
204
205func SyspropLibraries(config android.Config) []string {
206 return append([]string{}, *syspropLibraries(config)...)
207}
208
Inseob Kimc0907f12019-02-08 21:00:45 +0900209func init() {
Paul Duffin6e3ce722021-03-18 00:20:11 +0000210 registerSyspropBuildComponents(android.InitRegistrationContext)
211}
212
213func registerSyspropBuildComponents(ctx android.RegistrationContext) {
214 ctx.RegisterModuleType("sysprop_library", syspropLibraryFactory)
Inseob Kimc0907f12019-02-08 21:00:45 +0900215}
216
Inseob Kim42882742019-07-30 17:55:33 +0900217func (m *syspropLibrary) Name() string {
218 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900219}
220
Inseob Kimac1e9862019-12-09 18:15:47 +0900221func (m *syspropLibrary) Owner() string {
222 return m.properties.Property_owner
223}
224
Inseob Kim07def122020-11-23 14:43:02 +0900225func (m *syspropLibrary) CcImplementationModuleName() string {
Inseob Kim42882742019-07-30 17:55:33 +0900226 return "lib" + m.BaseModuleName()
227}
228
Colin Cross75ce9ec2021-02-26 16:20:32 -0800229func (m *syspropLibrary) javaPublicStubName() string {
230 return m.BaseModuleName() + "_public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900231}
232
Inseob Kim988f53c2019-09-16 15:59:01 +0900233func (m *syspropLibrary) javaGenModuleName() string {
234 return m.BaseModuleName() + "_java_gen"
235}
236
Inseob Kimac1e9862019-12-09 18:15:47 +0900237func (m *syspropLibrary) javaGenPublicStubName() string {
238 return m.BaseModuleName() + "_java_gen_public"
239}
240
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000241func (m *syspropLibrary) bp2buildJavaImplementationModuleName() string {
242 return m.BaseModuleName() + "_java_library"
243}
244
Inseob Kim42882742019-07-30 17:55:33 +0900245func (m *syspropLibrary) BaseModuleName() string {
246 return m.ModuleBase.Name()
247}
248
Inseob Kimc9770d62021-01-15 18:04:20 +0900249func (m *syspropLibrary) CurrentSyspropApiFile() android.OptionalPath {
Inseob Kim628d7ef2020-03-21 03:38:32 +0900250 return m.currentApiFile
251}
252
Inseob Kim07def122020-11-23 14:43:02 +0900253// GenerateAndroidBuildActions of sysprop_library handles API dump and API check.
254// generated java_library will depend on these API files.
Inseob Kim42882742019-07-30 17:55:33 +0900255func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900256 baseModuleName := m.BaseModuleName()
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000257 srcs := android.PathsForModuleSrc(ctx, m.properties.Srcs)
258 for _, syspropFile := range srcs {
Inseob Kim988f53c2019-09-16 15:59:01 +0900259 if syspropFile.Ext() != ".sysprop" {
260 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
261 }
262 }
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000263 ctx.SetProvider(blueprint.SrcsFileProviderKey, blueprint.SrcsFileProviderData{SrcPaths: srcs.Strings()})
Inseob Kim988f53c2019-09-16 15:59:01 +0900264
265 if ctx.Failed() {
266 return
267 }
268
Inseob Kimc9770d62021-01-15 18:04:20 +0900269 apiDirectoryPath := path.Join(ctx.ModuleDir(), "api")
270 currentApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-current.txt")
271 latestApiFilePath := path.Join(apiDirectoryPath, baseModuleName+"-latest.txt")
272 m.currentApiFile = android.ExistentPathForSource(ctx, currentApiFilePath)
273 m.latestApiFile = android.ExistentPathForSource(ctx, latestApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900274
275 // dump API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800276 rule := android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900277 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
278 rule.Command().
Colin Crossf1a035e2020-11-16 17:32:30 -0800279 BuiltTool("sysprop_api_dump").
Inseob Kim42882742019-07-30 17:55:33 +0900280 Output(m.dumpedApiFile).
Aditya Choudhary26df39f2023-11-29 16:42:42 +0000281 Inputs(srcs)
Colin Crossf1a035e2020-11-16 17:32:30 -0800282 rule.Build(baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900283
284 // check API rule
Colin Crossf1a035e2020-11-16 17:32:30 -0800285 rule = android.NewRuleBuilder(pctx, ctx)
Inseob Kim42882742019-07-30 17:55:33 +0900286
Inseob Kimc9770d62021-01-15 18:04:20 +0900287 // We allow that the API txt files don't exist, when the sysprop_library only contains internal
288 // properties. But we have to feed current api file and latest api file to the rule builder.
289 // Currently we can't get android.Path representing the null device, so we add any existing API
290 // txt files to implicits, and then directly feed string paths, rather than calling Input(Path)
291 // method.
292 var apiFileList android.Paths
293 currentApiArgument := os.DevNull
294 if m.currentApiFile.Valid() {
295 apiFileList = append(apiFileList, m.currentApiFile.Path())
296 currentApiArgument = m.currentApiFile.String()
297 }
298
299 latestApiArgument := os.DevNull
300 if m.latestApiFile.Valid() {
301 apiFileList = append(apiFileList, m.latestApiFile.Path())
302 latestApiArgument = m.latestApiFile.String()
303 }
304
Inseob Kim07def122020-11-23 14:43:02 +0900305 // 1. compares current.txt to api-dump.txt
306 // current.txt should be identical to api-dump.txt.
Inseob Kim42882742019-07-30 17:55:33 +0900307 msg := fmt.Sprintf(`\n******************************\n`+
308 `API of sysprop_library %s doesn't match with current.txt\n`+
309 `Please update current.txt by:\n`+
Inseob Kimc9770d62021-01-15 18:04:20 +0900310 `m %s-dump-api && mkdir -p %q && rm -rf %q && cp -f %q %q\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900311 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kimc9770d62021-01-15 18:04:20 +0900312 apiDirectoryPath, currentApiFilePath, m.dumpedApiFile.String(), currentApiFilePath)
Inseob Kim42882742019-07-30 17:55:33 +0900313
314 rule.Command().
315 Text("( cmp").Flag("-s").
316 Input(m.dumpedApiFile).
Inseob Kimc9770d62021-01-15 18:04:20 +0900317 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900318 Text("|| ( echo").Flag("-e").
319 Flag(`"` + msg + `"`).
320 Text("; exit 38) )")
321
Inseob Kim07def122020-11-23 14:43:02 +0900322 // 2. compares current.txt to latest.txt (frozen API)
323 // current.txt should be compatible with latest.txt
Inseob Kim42882742019-07-30 17:55:33 +0900324 msg = fmt.Sprintf(`\n******************************\n`+
325 `API of sysprop_library %s doesn't match with latest version\n`+
326 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900327 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900328
329 rule.Command().
330 Text("( ").
Colin Crossf1a035e2020-11-16 17:32:30 -0800331 BuiltTool("sysprop_api_checker").
Inseob Kimc9770d62021-01-15 18:04:20 +0900332 Text(latestApiArgument).
333 Text(currentApiArgument).
Inseob Kim42882742019-07-30 17:55:33 +0900334 Text(" || ( echo").Flag("-e").
335 Flag(`"` + msg + `"`).
Inseob Kimc9770d62021-01-15 18:04:20 +0900336 Text("; exit 38) )").
337 Implicits(apiFileList)
Inseob Kim42882742019-07-30 17:55:33 +0900338
339 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
340
341 rule.Command().
342 Text("touch").
343 Output(m.checkApiFileTimeStamp)
344
Colin Crossf1a035e2020-11-16 17:32:30 -0800345 rule.Build(baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900346}
347
348func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
349 return android.AndroidMkData{
350 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
351 // sysprop_library module itself is defined as a FAKE module to perform API check.
352 // Actual implementation libraries are created on LoadHookMutator
Sasha Smundak5c4729d2022-12-01 10:49:23 -0800353 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)", " # sysprop.syspropLibrary")
354 fmt.Fprintln(w, "LOCAL_MODULE :=", m.Name())
Inseob Kim42882742019-07-30 17:55:33 +0900355 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
356 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
357 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
358 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
359 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900360 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
361
362 // dump API rule
363 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900364
365 // check API rule
366 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900367 }}
368}
369
Jiyong Park45bf82e2020-12-15 22:29:02 +0900370var _ android.ApexModule = (*syspropLibrary)(nil)
371
372// Implements android.ApexModule
Dan Albertc8060532020-07-22 22:32:17 -0700373func (m *syspropLibrary) ShouldSupportSdkVersion(ctx android.BaseModuleContext,
374 sdkVersion android.ApiLevel) error {
Jooyung Han749dc692020-04-15 11:03:39 +0900375 return fmt.Errorf("sysprop_library is not supposed to be part of apex modules")
376}
377
Inseob Kim42882742019-07-30 17:55:33 +0900378// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
379// Both Java and C++ modules can link against sysprop_library, and API stability check
380// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
Trevor Radcliffed82e8f62022-06-08 16:16:31 +0000381// is performed. Note that the generated C++ module has its name prefixed with
382// `lib`, and it is this module that should be depended on from other C++
383// modules; i.e., if the sysprop_library module is named `foo`, C++ modules
384// should depend on `libfoo`.
Inseob Kimc0907f12019-02-08 21:00:45 +0900385func syspropLibraryFactory() android.Module {
386 m := &syspropLibrary{}
387
388 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900389 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900390 )
Inseob Kim42882742019-07-30 17:55:33 +0900391 android.InitAndroidModule(m)
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100392 android.InitApexModule(m)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000393 android.InitBazelModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900394 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900395 return m
396}
397
Inseob Kimac1e9862019-12-09 18:15:47 +0900398type ccLibraryProperties struct {
399 Name *string
400 Srcs []string
401 Soc_specific *bool
402 Device_specific *bool
403 Product_specific *bool
404 Sysprop struct {
405 Platform *bool
406 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900407 Target struct {
408 Android struct {
409 Header_libs []string
410 Shared_libs []string
411 }
412 Host struct {
413 Static_libs []string
414 }
415 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900416 Required []string
417 Recovery *bool
418 Recovery_available *bool
419 Vendor_available *bool
Justin Yun63e9ec72020-10-29 16:49:43 +0900420 Product_available *bool
Inseob Kim9da1f812021-06-14 12:03:59 +0900421 Ramdisk_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900422 Host_supported *bool
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100423 Apex_available []string
Jooyung Han379660c2020-04-21 15:24:00 +0900424 Min_sdk_version *string
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000425 Cflags []string
426 Ldflags []string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000427 Bazel_module struct {
Liz Kammer12dc96e2023-08-11 14:16:05 -0400428 Label *string
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000429 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900430}
431
432type javaLibraryProperties struct {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800433 Name *string
434 Srcs []string
435 Soc_specific *bool
436 Device_specific *bool
437 Product_specific *bool
438 Required []string
439 Sdk_version *string
440 Installable *bool
441 Libs []string
442 Stem *string
443 SyspropPublicStub string
Jiyong Park5e914b22021-03-08 10:09:52 +0900444 Apex_available []string
445 Min_sdk_version *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400446 Bazel_module struct {
447 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000448 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400449 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900450}
451
Inseob Kimc0907f12019-02-08 21:00:45 +0900452func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900453 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900454 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
455 }
456
Inseob Kimac1e9862019-12-09 18:15:47 +0900457 // ctx's Platform or Specific functions represent where this sysprop_library installed.
458 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
459 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
Inseob Kimfe612182020-10-20 16:29:55 +0900460 installedInProduct := ctx.ProductSpecific()
Inseob Kimac1e9862019-12-09 18:15:47 +0900461 isOwnerPlatform := false
Inseob Kim07def122020-11-23 14:43:02 +0900462 var javaSyspropStub string
Inseob Kimfe612182020-10-20 16:29:55 +0900463
Inseob Kim07def122020-11-23 14:43:02 +0900464 // javaSyspropStub contains stub libraries used by generated APIs, instead of framework stub.
465 // This is to make sysprop_library link against core_current.
Inseob Kimfe612182020-10-20 16:29:55 +0900466 if installedInVendorOrOdm {
Inseob Kim07def122020-11-23 14:43:02 +0900467 javaSyspropStub = "sysprop-library-stub-vendor"
Inseob Kimfe612182020-10-20 16:29:55 +0900468 } else if installedInProduct {
Inseob Kim07def122020-11-23 14:43:02 +0900469 javaSyspropStub = "sysprop-library-stub-product"
Inseob Kimfe612182020-10-20 16:29:55 +0900470 } else {
Inseob Kim07def122020-11-23 14:43:02 +0900471 javaSyspropStub = "sysprop-library-stub-platform"
Inseob Kimfe612182020-10-20 16:29:55 +0900472 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900473
Inseob Kimac1e9862019-12-09 18:15:47 +0900474 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900475 case "Platform":
476 // Every partition can access platform-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900477 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900478 case "Vendor":
479 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900480 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900481 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
482 "System can't access sysprop_library owned by Vendor")
483 }
484 case "Odm":
485 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900486 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900487 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
488 "Odm-defined properties should be accessed only in Vendor or Odm")
489 }
490 default:
491 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900492 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900493 }
494
Liz Kammer12dc96e2023-08-11 14:16:05 -0400495 var label *string
496 if b, ok := ctx.Module().(android.Bazelable); ok && b.ShouldConvertWithBp2build(ctx) {
497 // TODO: b/295566168 - this will need to change once build files are checked in to account for
498 // checked in modules in mixed builds
499 label = proptools.StringPtr(
500 fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.CcImplementationModuleName()))
501 }
502
Inseob Kim07def122020-11-23 14:43:02 +0900503 // Generate a C++ implementation library.
504 // cc_library can receive *.sysprop files as their srcs, generating sources itself.
Inseob Kimac1e9862019-12-09 18:15:47 +0900505 ccProps := ccLibraryProperties{}
Inseob Kim07def122020-11-23 14:43:02 +0900506 ccProps.Name = proptools.StringPtr(m.CcImplementationModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900507 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900508 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
509 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
510 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
511 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900512 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
513 ccProps.Target.Android.Shared_libs = []string{"liblog"}
514 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900515 ccProps.Recovery_available = m.properties.Recovery_available
516 ccProps.Vendor_available = m.properties.Vendor_available
Justin Yun63e9ec72020-10-29 16:49:43 +0900517 ccProps.Product_available = m.properties.Product_available
Inseob Kim9da1f812021-06-14 12:03:59 +0900518 ccProps.Ramdisk_available = m.properties.Ramdisk_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900519 ccProps.Host_supported = m.properties.Host_supported
Paul Duffin7b3de8f2020-03-30 18:00:25 +0100520 ccProps.Apex_available = m.ApexProperties.Apex_available
Jooyung Han379660c2020-04-21 15:24:00 +0900521 ccProps.Min_sdk_version = m.properties.Cpp.Min_sdk_version
Steven Morelandc43a4ac2023-10-24 21:49:18 +0000522 ccProps.Cflags = m.properties.Cpp.Cflags
523 ccProps.Ldflags = m.properties.Cpp.Ldflags
Liz Kammer12dc96e2023-08-11 14:16:05 -0400524 ccProps.Bazel_module.Label = label
Colin Cross84dfc3d2019-09-25 11:33:01 -0700525 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900526
Inseob Kim988f53c2019-09-16 15:59:01 +0900527 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900528
Inseob Kimac1e9862019-12-09 18:15:47 +0900529 // We need to only use public version, if the partition where sysprop_library will be installed
530 // is different from owner.
Inseob Kimac1e9862019-12-09 18:15:47 +0900531 if ctx.ProductSpecific() {
Inseob Kim07def122020-11-23 14:43:02 +0900532 // Currently product partition can't own any sysprop_library. So product always uses public.
Inseob Kim988f53c2019-09-16 15:59:01 +0900533 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900534 } else if isOwnerPlatform && installedInVendorOrOdm {
535 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900536 scope = "public"
537 }
538
Inseob Kim07def122020-11-23 14:43:02 +0900539 // Generate a Java implementation library.
540 // Contrast to C++, syspropJavaGenRule module will generate srcjar and the srcjar will be fed
541 // to Java implementation library.
Inseob Kimac1e9862019-12-09 18:15:47 +0900542 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800543 Srcs: m.properties.Srcs,
544 Scope: scope,
545 Name: proptools.StringPtr(m.javaGenModuleName()),
546 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900547 })
548
549 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
550 // and allow any modules (even from different partition) to link against the sysprop_library.
551 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
Colin Cross75ce9ec2021-02-26 16:20:32 -0800552 var publicStub string
Inseob Kimac1e9862019-12-09 18:15:47 +0900553 if isOwnerPlatform && installedInSystem {
Colin Cross75ce9ec2021-02-26 16:20:32 -0800554 publicStub = m.javaPublicStubName()
555 }
556
557 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
558 Name: proptools.StringPtr(m.BaseModuleName()),
559 Srcs: []string{":" + m.javaGenModuleName()},
560 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
561 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
562 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
563 Installable: m.properties.Installable,
564 Sdk_version: proptools.StringPtr("core_current"),
565 Libs: []string{javaSyspropStub},
566 SyspropPublicStub: publicStub,
Jiyong Park5e914b22021-03-08 10:09:52 +0900567 Apex_available: m.ApexProperties.Apex_available,
568 Min_sdk_version: m.properties.Java.Min_sdk_version,
Liz Kammer12dc96e2023-08-11 14:16:05 -0400569 Bazel_module: struct {
570 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000571 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400572 }{
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000573 Label: proptools.StringPtr(
574 fmt.Sprintf("//%s:%s", ctx.ModuleDir(), m.bp2buildJavaImplementationModuleName())),
Liz Kammer12dc96e2023-08-11 14:16:05 -0400575 },
Colin Cross75ce9ec2021-02-26 16:20:32 -0800576 })
577
578 if publicStub != "" {
Inseob Kimac1e9862019-12-09 18:15:47 +0900579 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800580 Srcs: m.properties.Srcs,
581 Scope: "public",
582 Name: proptools.StringPtr(m.javaGenPublicStubName()),
583 Check_api: proptools.StringPtr(ctx.ModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900584 })
585
586 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
Colin Cross75ce9ec2021-02-26 16:20:32 -0800587 Name: proptools.StringPtr(publicStub),
Inseob Kimac1e9862019-12-09 18:15:47 +0900588 Srcs: []string{":" + m.javaGenPublicStubName()},
589 Installable: proptools.BoolPtr(false),
590 Sdk_version: proptools.StringPtr("core_current"),
Inseob Kim07def122020-11-23 14:43:02 +0900591 Libs: []string{javaSyspropStub},
Inseob Kimac1e9862019-12-09 18:15:47 +0900592 Stem: proptools.StringPtr(m.BaseModuleName()),
Liz Kammer12dc96e2023-08-11 14:16:05 -0400593 Bazel_module: struct {
594 Bp2build_available *bool
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000595 Label *string
Liz Kammer12dc96e2023-08-11 14:16:05 -0400596 }{
597 Bp2build_available: proptools.BoolPtr(false),
598 },
Inseob Kimac1e9862019-12-09 18:15:47 +0900599 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900600 }
Inseob Kim628d7ef2020-03-21 03:38:32 +0900601
Inseob Kim07def122020-11-23 14:43:02 +0900602 // syspropLibraries will be used by property_contexts to check types.
603 // Record absolute paths of sysprop_library to prevent soong_namespace problem.
Inseob Kim69cf09e2020-05-04 19:28:25 +0900604 if m.ExportedToMake() {
605 syspropLibrariesLock.Lock()
606 defer syspropLibrariesLock.Unlock()
Inseob Kim628d7ef2020-03-21 03:38:32 +0900607
Inseob Kim69cf09e2020-05-04 19:28:25 +0900608 libraries := syspropLibraries(ctx.Config())
609 *libraries = append(*libraries, "//"+ctx.ModuleDir()+":"+ctx.ModuleName())
610 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900611}
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000612
613// TODO(b/240463568): Additional properties will be added for API validation
Chris Parsons637458d2023-09-19 20:09:00 +0000614func (m *syspropLibrary) ConvertWithBp2build(ctx android.Bp2buildMutatorContext) {
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000615 if m.Owner() != "Platform" {
616 ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNSUPPORTED, "Only sysprop libraries owned by platform are supported at this time")
617 return
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000618 }
Trevor Radcliffe9b81d792023-09-29 15:22:52 +0000619 labels := bp2build.SyspropLibraryLabels{
620 SyspropLibraryLabel: m.BaseModuleName(),
621 CcSharedLibraryLabel: m.CcImplementationModuleName(),
622 CcStaticLibraryLabel: cc.BazelLabelNameForStaticModule(m.CcImplementationModuleName()),
623 JavaLibraryLabel: m.bp2buildJavaImplementationModuleName(),
624 }
625 bp2build.Bp2buildBaseSyspropLibrary(ctx, labels.SyspropLibraryLabel, bazel.MakeLabelListAttribute(android.BazelLabelForModuleSrc(ctx, m.properties.Srcs)))
626 bp2build.Bp2buildSyspropCc(ctx, labels, m.properties.Cpp.Min_sdk_version)
627 bp2build.Bp2buildSyspropJava(ctx, labels, m.properties.Java.Min_sdk_version)
Trevor Radcliffead3d1232022-09-01 16:25:10 +0000628}