blob: ce404f8a71e70b08854ec0f33bda8c1ba1973fdb [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
147 // Whether public stub exists or not.
148 Public_stub *bool `blueprint:"mutated"`
Inseob Kimc0907f12019-02-08 21:00:45 +0900149}
150
151var (
Inseob Kim42882742019-07-30 17:55:33 +0900152 pctx = android.NewPackageContext("android/soong/sysprop")
Inseob Kimc0907f12019-02-08 21:00:45 +0900153 syspropCcTag = dependencyTag{name: "syspropCc"}
154)
155
156func init() {
157 android.RegisterModuleType("sysprop_library", syspropLibraryFactory)
158}
159
Inseob Kim42882742019-07-30 17:55:33 +0900160func (m *syspropLibrary) Name() string {
161 return m.BaseModuleName() + "_sysprop_library"
Inseob Kimc0907f12019-02-08 21:00:45 +0900162}
163
Inseob Kimac1e9862019-12-09 18:15:47 +0900164func (m *syspropLibrary) Owner() string {
165 return m.properties.Property_owner
166}
167
Inseob Kim42882742019-07-30 17:55:33 +0900168func (m *syspropLibrary) CcModuleName() string {
169 return "lib" + m.BaseModuleName()
170}
171
Inseob Kimac1e9862019-12-09 18:15:47 +0900172func (m *syspropLibrary) JavaPublicStubName() string {
173 if proptools.Bool(m.properties.Public_stub) {
174 return m.BaseModuleName() + "_public"
175 }
176 return ""
177}
178
Inseob Kim988f53c2019-09-16 15:59:01 +0900179func (m *syspropLibrary) javaGenModuleName() string {
180 return m.BaseModuleName() + "_java_gen"
181}
182
Inseob Kimac1e9862019-12-09 18:15:47 +0900183func (m *syspropLibrary) javaGenPublicStubName() string {
184 return m.BaseModuleName() + "_java_gen_public"
185}
186
Inseob Kim42882742019-07-30 17:55:33 +0900187func (m *syspropLibrary) BaseModuleName() string {
188 return m.ModuleBase.Name()
189}
190
Inseob Kimac1e9862019-12-09 18:15:47 +0900191func (m *syspropLibrary) HasPublicStub() bool {
192 return proptools.Bool(m.properties.Public_stub)
193}
194
Inseob Kim42882742019-07-30 17:55:33 +0900195func (m *syspropLibrary) GenerateAndroidBuildActions(ctx android.ModuleContext) {
Inseob Kim988f53c2019-09-16 15:59:01 +0900196 baseModuleName := m.BaseModuleName()
197
198 for _, syspropFile := range android.PathsForModuleSrc(ctx, m.properties.Srcs) {
199 if syspropFile.Ext() != ".sysprop" {
200 ctx.PropertyErrorf("srcs", "srcs contains non-sysprop file %q", syspropFile.String())
201 }
202 }
203
204 if ctx.Failed() {
205 return
206 }
207
208 m.currentApiFile = android.PathForSource(ctx, ctx.ModuleDir(), "api", baseModuleName+"-current.txt")
209 m.latestApiFile = android.PathForSource(ctx, ctx.ModuleDir(), "api", baseModuleName+"-latest.txt")
Inseob Kim42882742019-07-30 17:55:33 +0900210
211 // dump API rule
212 rule := android.NewRuleBuilder()
213 m.dumpedApiFile = android.PathForModuleOut(ctx, "api-dump.txt")
214 rule.Command().
215 BuiltTool(ctx, "sysprop_api_dump").
216 Output(m.dumpedApiFile).
217 Inputs(android.PathsForModuleSrc(ctx, m.properties.Srcs))
Inseob Kim988f53c2019-09-16 15:59:01 +0900218 rule.Build(pctx, ctx, baseModuleName+"_api_dump", baseModuleName+" api dump")
Inseob Kim42882742019-07-30 17:55:33 +0900219
220 // check API rule
221 rule = android.NewRuleBuilder()
222
223 // 1. current.txt <-> api_dump.txt
224 msg := fmt.Sprintf(`\n******************************\n`+
225 `API of sysprop_library %s doesn't match with current.txt\n`+
226 `Please update current.txt by:\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900227 `m %s-dump-api && rm -rf %q && cp -f %q %q\n`+
228 `******************************\n`, baseModuleName, baseModuleName,
Inseob Kim42882742019-07-30 17:55:33 +0900229 m.currentApiFile.String(), m.dumpedApiFile.String(), m.currentApiFile.String())
230
231 rule.Command().
232 Text("( cmp").Flag("-s").
233 Input(m.dumpedApiFile).
234 Input(m.currentApiFile).
235 Text("|| ( echo").Flag("-e").
236 Flag(`"` + msg + `"`).
237 Text("; exit 38) )")
238
239 // 2. current.txt <-> latest.txt
240 msg = fmt.Sprintf(`\n******************************\n`+
241 `API of sysprop_library %s doesn't match with latest version\n`+
242 `Please fix the breakage and rebuild.\n`+
Inseob Kim988f53c2019-09-16 15:59:01 +0900243 `******************************\n`, baseModuleName)
Inseob Kim42882742019-07-30 17:55:33 +0900244
245 rule.Command().
246 Text("( ").
247 BuiltTool(ctx, "sysprop_api_checker").
248 Input(m.latestApiFile).
249 Input(m.currentApiFile).
250 Text(" || ( echo").Flag("-e").
251 Flag(`"` + msg + `"`).
252 Text("; exit 38) )")
253
254 m.checkApiFileTimeStamp = android.PathForModuleOut(ctx, "check_api.timestamp")
255
256 rule.Command().
257 Text("touch").
258 Output(m.checkApiFileTimeStamp)
259
Inseob Kim988f53c2019-09-16 15:59:01 +0900260 rule.Build(pctx, ctx, baseModuleName+"_check_api", baseModuleName+" check api")
Inseob Kim42882742019-07-30 17:55:33 +0900261}
262
263func (m *syspropLibrary) AndroidMk() android.AndroidMkData {
264 return android.AndroidMkData{
265 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
266 // sysprop_library module itself is defined as a FAKE module to perform API check.
267 // Actual implementation libraries are created on LoadHookMutator
268 fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
269 fmt.Fprintf(w, "LOCAL_MODULE := %s\n", m.Name())
270 fmt.Fprintf(w, "LOCAL_MODULE_CLASS := FAKE\n")
271 fmt.Fprintf(w, "LOCAL_MODULE_TAGS := optional\n")
272 fmt.Fprintf(w, "include $(BUILD_SYSTEM)/base_rules.mk\n\n")
273 fmt.Fprintf(w, "$(LOCAL_BUILT_MODULE): %s\n", m.checkApiFileTimeStamp.String())
274 fmt.Fprintf(w, "\ttouch $@\n\n")
Inseob Kim988f53c2019-09-16 15:59:01 +0900275 fmt.Fprintf(w, ".PHONY: %s-check-api %s-dump-api\n\n", name, name)
276
277 // dump API rule
278 fmt.Fprintf(w, "%s-dump-api: %s\n\n", name, m.dumpedApiFile.String())
Inseob Kim42882742019-07-30 17:55:33 +0900279
280 // check API rule
281 fmt.Fprintf(w, "%s-check-api: %s\n\n", name, m.checkApiFileTimeStamp.String())
Inseob Kim42882742019-07-30 17:55:33 +0900282 }}
283}
284
285// sysprop_library creates schematized APIs from sysprop description files (.sysprop).
286// Both Java and C++ modules can link against sysprop_library, and API stability check
287// against latest APIs (see build/soong/scripts/freeze-sysprop-api-files.sh)
288// is performed.
Inseob Kimc0907f12019-02-08 21:00:45 +0900289func syspropLibraryFactory() android.Module {
290 m := &syspropLibrary{}
291
292 m.AddProperties(
Inseob Kim42882742019-07-30 17:55:33 +0900293 &m.properties,
Inseob Kimc0907f12019-02-08 21:00:45 +0900294 )
Inseob Kim42882742019-07-30 17:55:33 +0900295 android.InitAndroidModule(m)
Inseob Kimc0907f12019-02-08 21:00:45 +0900296 android.AddLoadHook(m, func(ctx android.LoadHookContext) { syspropLibraryHook(ctx, m) })
Inseob Kimc0907f12019-02-08 21:00:45 +0900297 return m
298}
299
Inseob Kimac1e9862019-12-09 18:15:47 +0900300type ccLibraryProperties struct {
301 Name *string
302 Srcs []string
303 Soc_specific *bool
304 Device_specific *bool
305 Product_specific *bool
306 Sysprop struct {
307 Platform *bool
308 }
309 Header_libs []string
310 Shared_libs []string
311 Required []string
312 Recovery *bool
313 Recovery_available *bool
314 Vendor_available *bool
315}
316
317type javaLibraryProperties struct {
318 Name *string
319 Srcs []string
320 Soc_specific *bool
321 Device_specific *bool
322 Product_specific *bool
323 Required []string
324 Sdk_version *string
325 Installable *bool
326 Libs []string
327 Stem *string
328}
329
Inseob Kimc0907f12019-02-08 21:00:45 +0900330func syspropLibraryHook(ctx android.LoadHookContext, m *syspropLibrary) {
Inseob Kim42882742019-07-30 17:55:33 +0900331 if len(m.properties.Srcs) == 0 {
Inseob Kim6e93ac92019-03-21 17:43:49 +0900332 ctx.PropertyErrorf("srcs", "sysprop_library must specify srcs")
333 }
334
Inseob Kim42882742019-07-30 17:55:33 +0900335 missing_api := false
336
337 for _, txt := range []string{"-current.txt", "-latest.txt"} {
338 path := path.Join(ctx.ModuleDir(), "api", m.BaseModuleName()+txt)
339 file := android.ExistentPathForSource(ctx, path)
340 if !file.Valid() {
341 ctx.ModuleErrorf("API file %#v doesn't exist", path)
342 missing_api = true
343 }
344 }
345
346 if missing_api {
347 script := "build/soong/scripts/gen-sysprop-api-files.sh"
348 p := android.ExistentPathForSource(ctx, script)
349
350 if !p.Valid() {
351 panic(fmt.Sprintf("script file %s doesn't exist", script))
352 }
353
354 ctx.ModuleErrorf("One or more api files are missing. "+
355 "You can create them by:\n"+
356 "%s %q %q", script, ctx.ModuleDir(), m.BaseModuleName())
357 return
Inseob Kimc0907f12019-02-08 21:00:45 +0900358 }
359
Inseob Kimac1e9862019-12-09 18:15:47 +0900360 // ctx's Platform or Specific functions represent where this sysprop_library installed.
361 installedInSystem := ctx.Platform() || ctx.SystemExtSpecific()
362 installedInVendorOrOdm := ctx.SocSpecific() || ctx.DeviceSpecific()
363 isOwnerPlatform := false
Inseob Kim42882742019-07-30 17:55:33 +0900364 stub := "sysprop-library-stub-"
Inseob Kimc0907f12019-02-08 21:00:45 +0900365
Inseob Kimac1e9862019-12-09 18:15:47 +0900366 switch m.Owner() {
Inseob Kimc0907f12019-02-08 21:00:45 +0900367 case "Platform":
368 // Every partition can access platform-defined properties
Inseob Kim42882742019-07-30 17:55:33 +0900369 stub += "platform"
Inseob Kimac1e9862019-12-09 18:15:47 +0900370 isOwnerPlatform = true
Inseob Kimc0907f12019-02-08 21:00:45 +0900371 case "Vendor":
372 // System can't access vendor's properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900373 if installedInSystem {
Inseob Kimc0907f12019-02-08 21:00:45 +0900374 ctx.ModuleErrorf("None of soc_specific, device_specific, product_specific is true. " +
375 "System can't access sysprop_library owned by Vendor")
376 }
Inseob Kim42882742019-07-30 17:55:33 +0900377 stub += "vendor"
Inseob Kimc0907f12019-02-08 21:00:45 +0900378 case "Odm":
379 // Only vendor can access Odm-defined properties
Inseob Kimac1e9862019-12-09 18:15:47 +0900380 if !installedInVendorOrOdm {
Inseob Kimc0907f12019-02-08 21:00:45 +0900381 ctx.ModuleErrorf("Neither soc_speicifc nor device_specific is true. " +
382 "Odm-defined properties should be accessed only in Vendor or Odm")
383 }
Inseob Kim42882742019-07-30 17:55:33 +0900384 stub += "vendor"
Inseob Kimc0907f12019-02-08 21:00:45 +0900385 default:
386 ctx.PropertyErrorf("property_owner",
Inseob Kimac1e9862019-12-09 18:15:47 +0900387 "Unknown value %s: must be one of Platform, Vendor or Odm", m.Owner())
Inseob Kimc0907f12019-02-08 21:00:45 +0900388 }
389
Inseob Kimac1e9862019-12-09 18:15:47 +0900390 ccProps := ccLibraryProperties{}
Inseob Kimc0907f12019-02-08 21:00:45 +0900391 ccProps.Name = proptools.StringPtr(m.CcModuleName())
Inseob Kim42882742019-07-30 17:55:33 +0900392 ccProps.Srcs = m.properties.Srcs
Inseob Kimac1e9862019-12-09 18:15:47 +0900393 ccProps.Soc_specific = proptools.BoolPtr(ctx.SocSpecific())
394 ccProps.Device_specific = proptools.BoolPtr(ctx.DeviceSpecific())
395 ccProps.Product_specific = proptools.BoolPtr(ctx.ProductSpecific())
396 ccProps.Sysprop.Platform = proptools.BoolPtr(isOwnerPlatform)
Inseob Kimda63a492019-06-10 23:03:16 +0900397 ccProps.Header_libs = []string{"libbase_headers"}
398 ccProps.Shared_libs = []string{"liblog"}
Inseob Kim42882742019-07-30 17:55:33 +0900399 ccProps.Recovery_available = m.properties.Recovery_available
400 ccProps.Vendor_available = m.properties.Vendor_available
Colin Cross84dfc3d2019-09-25 11:33:01 -0700401 ctx.CreateModule(cc.LibraryFactory, &ccProps)
Inseob Kim42882742019-07-30 17:55:33 +0900402
Inseob Kim988f53c2019-09-16 15:59:01 +0900403 scope := "internal"
Inseob Kim988f53c2019-09-16 15:59:01 +0900404
Inseob Kimac1e9862019-12-09 18:15:47 +0900405 // We need to only use public version, if the partition where sysprop_library will be installed
406 // is different from owner.
407
408 if ctx.ProductSpecific() {
409 // Currently product partition can't own any sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900410 scope = "public"
Inseob Kimac1e9862019-12-09 18:15:47 +0900411 } else if isOwnerPlatform && installedInVendorOrOdm {
412 // Vendor or Odm should use public version of Platform's sysprop_library.
Inseob Kim988f53c2019-09-16 15:59:01 +0900413 scope = "public"
414 }
415
Inseob Kimac1e9862019-12-09 18:15:47 +0900416 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
Inseob Kim988f53c2019-09-16 15:59:01 +0900417 Srcs: m.properties.Srcs,
418 Scope: scope,
419 Name: proptools.StringPtr(m.javaGenModuleName()),
Inseob Kimac1e9862019-12-09 18:15:47 +0900420 })
421
422 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
423 Name: proptools.StringPtr(m.BaseModuleName()),
424 Srcs: []string{":" + m.javaGenModuleName()},
425 Soc_specific: proptools.BoolPtr(ctx.SocSpecific()),
426 Device_specific: proptools.BoolPtr(ctx.DeviceSpecific()),
427 Product_specific: proptools.BoolPtr(ctx.ProductSpecific()),
428 Installable: m.properties.Installable,
429 Sdk_version: proptools.StringPtr("core_current"),
430 Libs: []string{stub},
431 })
432
433 // if platform sysprop_library is installed in /system or /system-ext, we regard it as an API
434 // and allow any modules (even from different partition) to link against the sysprop_library.
435 // To do that, we create a public stub and expose it to modules with sdk_version: system_*.
436 if isOwnerPlatform && installedInSystem {
437 m.properties.Public_stub = proptools.BoolPtr(true)
438 ctx.CreateModule(syspropJavaGenFactory, &syspropGenProperties{
439 Srcs: m.properties.Srcs,
440 Scope: "public",
441 Name: proptools.StringPtr(m.javaGenPublicStubName()),
442 })
443
444 ctx.CreateModule(java.LibraryFactory, &javaLibraryProperties{
445 Name: proptools.StringPtr(m.JavaPublicStubName()),
446 Srcs: []string{":" + m.javaGenPublicStubName()},
447 Installable: proptools.BoolPtr(false),
448 Sdk_version: proptools.StringPtr("core_current"),
449 Libs: []string{stub},
450 Stem: proptools.StringPtr(m.BaseModuleName()),
451 })
Inseob Kim988f53c2019-09-16 15:59:01 +0900452 }
Inseob Kimc0907f12019-02-08 21:00:45 +0900453}
Inseob Kim988f53c2019-09-16 15:59:01 +0900454
455func syspropDepsMutator(ctx android.BottomUpMutatorContext) {
456 if m, ok := ctx.Module().(*syspropLibrary); ok {
457 ctx.AddReverseDependency(m, nil, m.javaGenModuleName())
Inseob Kimac1e9862019-12-09 18:15:47 +0900458
459 if proptools.Bool(m.properties.Public_stub) {
460 ctx.AddReverseDependency(m, nil, m.javaGenPublicStubName())
461 }
Inseob Kim988f53c2019-09-16 15:59:01 +0900462 }
463}