blob: 65dbb22a94878f09c66e7d5145053b9aa4418609 [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
15package sysprop
16
17import (
Inseob Kim42882742019-07-30 17:55:33 +090018 "fmt"
19 "io"
20 "path"
Colin Crossf8b860a2019-04-16 14:43:28 -070021
Inseob Kimc0907f12019-02-08 21:00:45 +090022 "github.com/google/blueprint"
23 "github.com/google/blueprint/proptools"
Inseob Kim42882742019-07-30 17:55:33 +090024
25 "android/soong/android"
26 "android/soong/cc"
27 "android/soong/java"
Inseob Kimc0907f12019-02-08 21:00:45 +090028)
29
30type dependencyTag struct {
31 blueprint.BaseDependencyTag
32 name string
33}
34
Inseob Kim988f53c2019-09-16 15:59:01 +090035type syspropGenProperties struct {
36 Srcs []string `android:"path"`
37 Scope string
Inseob Kimac1e9862019-12-09 18:15:47 +090038 Name *string
Inseob Kim988f53c2019-09-16 15:59:01 +090039}
40
41type syspropJavaGenRule struct {
42 android.ModuleBase
43
44 properties syspropGenProperties
45
46 genSrcjars android.Paths
47}
48
49var _ android.OutputFileProducer = (*syspropJavaGenRule)(nil)
50
51var (
52 syspropJava = pctx.AndroidStaticRule("syspropJava",
53 blueprint.RuleParams{
54 Command: `rm -rf $out.tmp && mkdir -p $out.tmp && ` +
55 `$syspropJavaCmd --scope $scope --java-output-dir $out.tmp $in && ` +
56 `$soongZipCmd -jar -o $out -C $out.tmp -D $out.tmp && rm -rf $out.tmp`,
57 CommandDeps: []string{
58 "$syspropJavaCmd",
59 "$soongZipCmd",
60 },
61 }, "scope")
62)
63
64func init() {
65 pctx.HostBinToolVariable("soongZipCmd", "soong_zip")
66 pctx.HostBinToolVariable("syspropJavaCmd", "sysprop_java")
67
68 android.PreArchMutators(func(ctx android.RegisterMutatorsContext) {
69 ctx.BottomUp("sysprop_deps", syspropDepsMutator).Parallel()
70 })
71}
72
73func (g *syspropJavaGenRule) GenerateAndroidBuildActions(ctx android.ModuleContext) {
74 var checkApiFileTimeStamp android.WritablePath
75
76 ctx.VisitDirectDeps(func(dep android.Module) {
77 if m, ok := dep.(*syspropLibrary); ok {
78 checkApiFileTimeStamp = m.checkApiFileTimeStamp
79 }
80 })
81
82 for _, syspropFile := range android.PathsForModuleSrc(ctx, g.properties.Srcs) {
83 srcJarFile := android.GenPathWithExt(ctx, "sysprop", syspropFile, "srcjar")
84
85 ctx.Build(pctx, android.BuildParams{
86 Rule: syspropJava,
87 Description: "sysprop_java " + syspropFile.Rel(),
88 Output: srcJarFile,
89 Input: syspropFile,
90 Implicit: checkApiFileTimeStamp,
91 Args: map[string]string{
92 "scope": g.properties.Scope,
93 },
94 })
95
96 g.genSrcjars = append(g.genSrcjars, srcJarFile)
97 }
98}
99
100func (g *syspropJavaGenRule) OutputFiles(tag string) (android.Paths, error) {
101 switch tag {
102 case "":
103 return g.genSrcjars, nil
104 default:
105 return nil, fmt.Errorf("unsupported module reference tag %q", tag)
106 }
107}
108
109func syspropJavaGenFactory() android.Module {
110 g := &syspropJavaGenRule{}
111 g.AddProperties(&g.properties)
112 android.InitAndroidModule(g)
113 return g
114}
115
Inseob Kimc0907f12019-02-08 21:00:45 +0900116type syspropLibrary struct {
Inseob Kim42882742019-07-30 17:55:33 +0900117 android.ModuleBase
Inseob Kimc0907f12019-02-08 21:00:45 +0900118
Inseob Kim42882742019-07-30 17:55:33 +0900119 properties syspropLibraryProperties
120
121 checkApiFileTimeStamp android.WritablePath
122 latestApiFile android.Path
123 currentApiFile android.Path
124 dumpedApiFile android.WritablePath
Inseob Kimc0907f12019-02-08 21:00:45 +0900125}
126
127type syspropLibraryProperties struct {
128 // Determine who owns this sysprop library. Possible values are
129 // "Platform", "Vendor", or "Odm"
130 Property_owner string
Inseob Kimf63c2fb2019-03-05 14:22:30 +0900131
132 // list of package names that will be documented and publicized as API
133 Api_packages []string
Inseob Kimc0907f12019-02-08 21:00:45 +0900134
Inseob Kim42882742019-07-30 17:55:33 +0900135 // If set to true, allow this module to be dexed and installed on devices.
136 Installable *bool
137
138 // Make this module available when building for recovery
Jiyong Park854a9442019-02-26 10:27:13 +0900139 Recovery_available *bool
Inseob Kim42882742019-07-30 17:55:33 +0900140
141 // Make this module available when building for vendor
142 Vendor_available *bool
143
144 // list of .sysprop files which defines the properties.
145 Srcs []string `android:"path"`
Inseob Kimac1e9862019-12-09 18:15:47 +0900146
Inseob Kim89db15d2020-02-03 18:06:46 +0900147 // If set to true, build a variant of the module for the host. Defaults to false.
148 Host_supported *bool
149
Inseob Kimac1e9862019-12-09 18:15:47 +0900150 // Whether public stub exists or not.
151 Public_stub *bool `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +0900152}
153
154var (
Inseob Kim42882742019-07-30 17:55:33 +0900155 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900156 syspropCcTag = dependencyTag{name: "syspropCc"}
157)
158
159func init() {
160 android.RegisterModuleType("sysprop_library", syspropLibraryFactory)
161}
162
Inseob Kim42882742019-07-30 17:55:33 +0900163func (m *syspropLibrary) Name() string {
164 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900165}
166
Inseob Kimac1e9862019-12-09 18:15:47 +0900167func (m *syspropLibrary) Owner() string {
168 return m.properties.Property_owner
169}
170
Inseob Kim42882742019-07-30 17:55:33 +0900171func (m *syspropLibrary) CcModuleName() string {
172 return "lib" + m.BaseModuleName()
173}
174
Inseob Kimac1e9862019-12-09 18:15:47 +0900175func (m *syspropLibrary) JavaPublicStubName() string {
176 if proptools.Bool(m.properties.Public_stub) {
177 return m.BaseModuleName() + "_public"
178 }
179 return ""
180}
181
Inseob Kim988f53c2019-09-16 15:59:01 +0900182func (m *syspropLibrary) javaGenModuleName() string {
183 return m.BaseModuleName() + "_java_gen"
184}
185
Inseob Kimac1e9862019-12-09 18:15:47 +0900186func (m *syspropLibrary) javaGenPublicStubName() string {
187 return m.BaseModuleName() + "_java_gen_public"
188}
189
Inseob Kim42882742019-07-30 17:55:33 +0900190func (m *syspropLibrary) BaseModuleName() string {
191 return m.ModuleBase.Name()
192}
193
Inseob Kimac1e9862019-12-09 18:15:47 +0900194func (m *syspropLibrary) HasPublicStub() bool {
195 return proptools.Bool(m.properties.Public_stub)
196}
197
Inseob Kim42882742019-07-30 17:55:33 +0900198func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900199 baseModuleName := m.BaseModuleName()
200
201 for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
202 if syspropFile.Ext() != ".sysprop" {
203 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
204 }
205 }
206
207 if ctx.Failed() {
208 return
209 }
210
211 m.currentApiFile = android.PathForSource(ctx, ctx.ModuleDir(), "api", baseModuleName+"-current.txt")
212 m.latestApiFile = android.PathForSource(ctx, ctx.ModuleDir(), "api", baseModuleName+"-latest.txt")
Inseob Kim42882742019-07-30 17:55:33 +0900213
214 // dump API rule
215 rule := android.NewRuleBuilder()
216 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
217 rule.Command().
218 BuiltTool(ctx, "sysprop_api_dump").
219 Output(m.dumpedApiFile).
220 Inputs(android.PathsForModuleSrc(ctx, m.properties.Srcs))
Inseob Kim988f53c2019-09-16 15:59:01 +0900221 rule.Build(pctx, ctx, baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900222
223 // check API rule
224 rule = android.NewRuleBuilder()
225
226 // 1. current.txt <-> api_dump.txt
227 msg := fmt.Sprintf(`\n******************************\n`+
228 `API of sysprop_library %s doesn't match with current.txt\n`+
229 `Please update current.txt by:\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900230 `m %s-dump-api && rm -rf %q && cp -f %q %q\n`+
231 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kim42882742019-07-30 17:55:33 +0900232 m.currentApiFile.String(), m.dumpedApiFile.String(), m.currentApiFile.String())
233
234 rule.Command().
235 Text("( cmp").Flag("-s").
236 Input(m.dumpedApiFile).
237 Input(m.currentApiFile).
238 Text("|| ( echo").Flag("-e").
239 Flag(`"` + msg + `"`).
240 Text("; exit 38) )")
241
242 // 2. current.txt <-> latest.txt
243 msg = fmt.Sprintf(`\n******************************\n`+
244 `API of sysprop_library %s doesn't match with latest version\n`+
245 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900246 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900247
248 rule.Command().
249 Text("( ").
250 BuiltTool(ctx, "sysprop_api_checker").
251 Input(m.latestApiFile).
252 Input(m.currentApiFile).
253 Text(" || ( echo").Flag("-e").
254 Flag(`"` + msg + `"`).
255 Text("; exit 38) )")
256
257 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
258
259 rule.Command().
260 Text("touch").
261 Output(m.checkApiFileTimeStamp)
262
Inseob Kim988f53c2019-09-16 15:59:01 +0900263 rule.Build(pctx, ctx, baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900264}
265
266func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
267 return android.AndroidMkData{
268 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
269 // sysprop_library module itself is defined as a FAKE module to perform API check.
270 // Actual implementation libraries are created on LoadHookMutator
271 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
272 fmt.Fprintf(w, "LOCAL_MODULE := %s\n", m.Name())
273 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
274 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
275 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
276 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
277 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900278 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
279
280 // dump API rule
281 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900282
283 // check API rule
284 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900285 }}
286}
287
288// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
289// Both Java and C++ modules can link against sysprop_library, and API stability check
290// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
291// is performed.
Inseob Kimc0907f12019-02-08 21:00:45 +0900292func syspropLibraryFactory() android.Module {
293 m := &syspropLibrary{}
294
295 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900296 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900297 )
Inseob Kim42882742019-07-30 17:55:33 +0900298 android.InitAndroidModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900299 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900300 return m
301}
302
Inseob Kimac1e9862019-12-09 18:15:47 +0900303type ccLibraryProperties struct {
304 Name *string
305 Srcs []string
306 Soc_specific *bool
307 Device_specific *bool
308 Product_specific *bool
309 Sysprop struct {
310 Platform *bool
311 }
Inseob Kim89db15d2020-02-03 18:06:46 +0900312 Target struct {
313 Android struct {
314 Header_libs []string
315 Shared_libs []string
316 }
317 Host struct {
318 Static_libs []string
319 }
320 }
Inseob Kimac1e9862019-12-09 18:15:47 +0900321 Required []string
322 Recovery *bool
323 Recovery_available *bool
324 Vendor_available *bool
Inseob Kim89db15d2020-02-03 18:06:46 +0900325 Host_supported *bool
Inseob Kimac1e9862019-12-09 18:15:47 +0900326}
327
328type javaLibraryProperties struct {
329 Name *string
330 Srcs []string
331 Soc_specific *bool
332 Device_specific *bool
333 Product_specific *bool
334 Required []string
335 Sdk_version *string
336 Installable *bool
337 Libs []string
338 Stem *string
339}
340
Inseob Kimc0907f12019-02-08 21:00:45 +0900341func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900342 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900343 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
344 }
345
Inseob Kim42882742019-07-30 17:55:33 +0900346 missing_api := false
347
348 for _, txt := range []string{"-current.txt", "-latest.txt"} {
349 path := path.Join(ctx.ModuleDir(), "api", m.BaseModuleName()+txt)
350 file := android.ExistentPathForSource(ctx, path)
351 if !file.Valid() {
352 ctx.ModuleErrorf("API file %#v doesn't exist", path)
353 missing_api = true
354 }
355 }
356
357 if missing_api {
358 script := "build/soong/scripts/gen-sysprop-api-files.sh"
359 p := android.ExistentPathForSource(ctx, script)
360
361 if !p.Valid() {
362 panic(fmt.Sprintf("script file %s doesn't exist", script))
363 }
364
365 ctx.ModuleErrorf("One or more api files are missing. "+
366 "You can create them by:\n"+
367 "%s %q %q", script, ctx.ModuleDir(), m.BaseModuleName())
368 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900369 }
370
Inseob Kimac1e9862019-12-09 18:15:47 +0900371 // ctx's Platform or Specific functions represent where this sysprop_library installed.
372 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
373 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
374 isOwnerPlatform := false
Inseob Kim42882742019-07-30 17:55:33 +0900375 stub := "sysprop-library-stub-"
Inseob Kimc0907f12019-02-08 21:00:45 +0900376
Inseob Kimac1e9862019-12-09 18:15:47 +0900377 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900378 case "Platform":
379 // Every partition can access platform-defined properties
Inseob Kim42882742019-07-30 17:55:33 +0900380 stub += "platform"
Inseob Kimac1e9862019-12-09 18:15:47 +0900381 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900382 case "Vendor":
383 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900384 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900385 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
386 "System can't access sysprop_library owned by Vendor")
387 }
Inseob Kim42882742019-07-30 17:55:33 +0900388 stub += "vendor"
Inseob Kimc0907f12019-02-08 21:00:45 +0900389 case "Odm":
390 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900391 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900392 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
393 "Odm-defined properties should be accessed only in Vendor or Odm")
394 }
Inseob Kim42882742019-07-30 17:55:33 +0900395 stub += "vendor"
Inseob Kimc0907f12019-02-08 21:00:45 +0900396 default:
397 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900398 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900399 }
400
Inseob Kimac1e9862019-12-09 18:15:47 +0900401 ccProps := ccLibraryProperties{}
Inseob Kimc0907f12019-02-08 21:00:45 +0900402 ccProps.Name = proptools.StringPtr(m.CcModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900403 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900404 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
405 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
406 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
407 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kim89db15d2020-02-03 18:06:46 +0900408 ccProps.Target.Android.Header_libs = []string{"libbase_headers"}
409 ccProps.Target.Android.Shared_libs = []string{"liblog"}
410 ccProps.Target.Host.Static_libs = []string{"libbase", "liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900411 ccProps.Recovery_available = m.properties.Recovery_available
412 ccProps.Vendor_available = m.properties.Vendor_available
Inseob Kim89db15d2020-02-03 18:06:46 +0900413 ccProps.Host_supported = m.properties.Host_supported
Colin Cross84dfc3d2019-09-25 11:33:01 -0700414 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900415
Inseob Kim988f53c2019-09-16 15:59:01 +0900416 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900417
Inseob Kimac1e9862019-12-09 18:15:47 +0900418 // We need to only use public version, if the partition where sysprop_library will be installed
419 // is different from owner.
420
421 if ctx.ProductSpecific() {
422 // Currently product partition can't own any sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900423 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900424 } else if isOwnerPlatform && installedInVendorOrOdm {
425 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900426 scope = "public"
427 }
428
Inseob Kimac1e9862019-12-09 18:15:47 +0900429 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Inseob Kim988f53c2019-09-16 15:59:01 +0900430 Srcs: m.properties.Srcs,
431 Scope: scope,
432 Name: proptools.StringPtr(m.javaGenModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900433 })
434
435 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
436 Name: proptools.StringPtr(m.BaseModuleName()),
437 Srcs: []string{":" + m.javaGenModuleName()},
438 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
439 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
440 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
441 Installable: m.properties.Installable,
442 Sdk_version: proptools.StringPtr("core_current"),
443 Libs: []string{stub},
444 })
445
446 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
447 // and allow any modules (even from different partition) to link against the sysprop_library.
448 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
449 if isOwnerPlatform && installedInSystem {
450 m.properties.Public_stub = proptools.BoolPtr(true)
451 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
452 Srcs: m.properties.Srcs,
453 Scope: "public",
454 Name: proptools.StringPtr(m.javaGenPublicStubName()),
455 })
456
457 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
458 Name: proptools.StringPtr(m.JavaPublicStubName()),
459 Srcs: []string{":" + m.javaGenPublicStubName()},
460 Installable: proptools.BoolPtr(false),
461 Sdk_version: proptools.StringPtr("core_current"),
462 Libs: []string{stub},
463 Stem: proptools.StringPtr(m.BaseModuleName()),
464 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900465 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900466}
Inseob Kim988f53c2019-09-16 15:59:01 +0900467
468func syspropDepsMutator(ctx android.BottomUpMutatorContext) {
469 if m, ok := ctx.Module().(*syspropLibrary); ok {
470 ctx.AddReverseDependency(m, nil, m.javaGenModuleName())
Inseob Kimac1e9862019-12-09 18:15:47 +0900471
472 if proptools.Bool(m.properties.Public_stub) {
473 ctx.AddReverseDependency(m, nil, m.javaGenPublicStubName())
474 }
Inseob Kim988f53c2019-09-16 15:59:01 +0900475 }
476}