Inseob Kim | c7c4746 | 2022-04-25 18:23:58 +0900 | [diff] [blame] | 1 | // Copyright 2022 Google Inc. All rights reserved. |
| 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 | |
| 15 | package android |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "strings" |
| 20 | |
| 21 | "github.com/google/blueprint/proptools" |
| 22 | ) |
| 23 | |
| 24 | func init() { |
| 25 | ctx := InitRegistrationContext |
| 26 | ctx.RegisterSingletonModuleType("buildinfo_prop", buildinfoPropFactory) |
| 27 | } |
| 28 | |
| 29 | type buildinfoPropProperties struct { |
| 30 | // Whether this module is directly installable to one of the partitions. Default: true. |
| 31 | Installable *bool |
| 32 | } |
| 33 | |
| 34 | type buildinfoPropModule struct { |
| 35 | SingletonModuleBase |
| 36 | |
| 37 | properties buildinfoPropProperties |
| 38 | |
| 39 | outputFilePath OutputPath |
| 40 | installPath InstallPath |
| 41 | } |
| 42 | |
| 43 | var _ OutputFileProducer = (*buildinfoPropModule)(nil) |
| 44 | |
| 45 | func (p *buildinfoPropModule) installable() bool { |
| 46 | return proptools.BoolDefault(p.properties.Installable, true) |
| 47 | } |
| 48 | |
| 49 | // OutputFileProducer |
| 50 | func (p *buildinfoPropModule) OutputFiles(tag string) (Paths, error) { |
| 51 | if tag != "" { |
| 52 | return nil, fmt.Errorf("unsupported tag %q", tag) |
| 53 | } |
| 54 | return Paths{p.outputFilePath}, nil |
| 55 | } |
| 56 | |
| 57 | func (p *buildinfoPropModule) GenerateAndroidBuildActions(ctx ModuleContext) { |
| 58 | p.outputFilePath = PathForModuleOut(ctx, p.Name()).OutputPath |
| 59 | if !ctx.Config().KatiEnabled() { |
| 60 | WriteFileRule(ctx, p.outputFilePath, "# no buildinfo.prop if kati is disabled") |
| 61 | return |
| 62 | } |
| 63 | |
| 64 | rule := NewRuleBuilder(pctx, ctx) |
| 65 | cmd := rule.Command().Text("(") |
| 66 | |
| 67 | writeString := func(str string) { |
| 68 | cmd.Text(`echo "` + str + `" && `) |
| 69 | } |
| 70 | |
| 71 | writeString("# begin build properties") |
| 72 | writeString("# autogenerated by build/soong/android/buildinfo_prop.go") |
| 73 | |
| 74 | writeProp := func(key, value string) { |
| 75 | if strings.Contains(key, "=") { |
| 76 | panic(fmt.Errorf("wrong property key %q: key must not contain '='", key)) |
| 77 | } |
| 78 | writeString(key + "=" + value) |
| 79 | } |
| 80 | |
| 81 | config := ctx.Config() |
| 82 | |
| 83 | writeProp("ro.build.version.sdk", config.PlatformSdkVersion().String()) |
| 84 | writeProp("ro.build.version.preview_sdk", config.PlatformPreviewSdkVersion()) |
| 85 | writeProp("ro.build.version.codename", config.PlatformSdkCodename()) |
| 86 | writeProp("ro.build.version.all_codenames", strings.Join(config.PlatformVersionActiveCodenames(), ",")) |
| 87 | writeProp("ro.build.version.release", config.PlatformVersionLastStable()) |
| 88 | writeProp("ro.build.version.release_or_codename", config.PlatformVersionName()) |
| 89 | writeProp("ro.build.version.security_patch", config.PlatformSecurityPatch()) |
| 90 | writeProp("ro.build.version.base_os", config.PlatformBaseOS()) |
| 91 | writeProp("ro.build.version.min_supported_target_sdk", config.PlatformMinSupportedTargetSdkVersion()) |
| 92 | |
| 93 | if config.Eng() { |
| 94 | writeProp("ro.build.type", "eng") |
| 95 | } else if config.Debuggable() { |
| 96 | writeProp("ro.build.type", "userdebug") |
| 97 | } else { |
| 98 | writeProp("ro.build.type", "user") |
| 99 | } |
| 100 | |
| 101 | // Currently, only a few properties are implemented to unblock microdroid use case. |
| 102 | // TODO(b/189164487): support below properties as well and replace build/make/tools/buildinfo.sh |
| 103 | /* |
| 104 | if $BOARD_USE_VBMETA_DIGTEST_IN_FINGERPRINT { |
| 105 | writeProp("ro.build.legacy.id", config.BuildID()) |
| 106 | } else { |
| 107 | writeProp("ro.build.id", config.BuildId()) |
| 108 | } |
| 109 | writeProp("ro.build.display.id", $BUILD_DISPLAY_ID) |
| 110 | writeProp("ro.build.version.incremental", $BUILD_NUMBER) |
| 111 | writeProp("ro.build.version.preview_sdk_fingerprint", $PLATFORM_PREVIEW_SDK_FINGERPRINT) |
| 112 | writeProp("ro.build.version.known_codenames", $PLATFORM_VERSION_KNOWN_CODENAMES) |
| 113 | writeProp("ro.build.version.release_or_preview_display", $PLATFORM_DISPLAY_VERSION) |
| 114 | writeProp("ro.build.date", `$DATE`) |
| 115 | writeProp("ro.build.date.utc", `$DATE +%s`) |
| 116 | writeProp("ro.build.user", $BUILD_USERNAME) |
| 117 | writeProp("ro.build.host", $BUILD_HOSTNAME) |
| 118 | writeProp("ro.build.tags", $BUILD_VERSION_TAGS) |
| 119 | writeProp("ro.build.flavor", $TARGET_BUILD_FLAVOR) |
| 120 | // These values are deprecated, use "ro.product.cpu.abilist" |
| 121 | // instead (see below). |
| 122 | writeString("# ro.product.cpu.abi and ro.product.cpu.abi2 are obsolete,") |
| 123 | writeString("# use ro.product.cpu.abilist instead.") |
| 124 | writeProp("ro.product.cpu.abi", $TARGET_CPU_ABI) |
| 125 | if [ -n "$TARGET_CPU_ABI2" ] { |
| 126 | writeProp("ro.product.cpu.abi2", $TARGET_CPU_ABI2) |
| 127 | } |
| 128 | |
| 129 | if [ -n "$PRODUCT_DEFAULT_LOCALE" ] { |
| 130 | writeProp("ro.product.locale", $PRODUCT_DEFAULT_LOCALE) |
| 131 | } |
| 132 | writeProp("ro.wifi.channels", $PRODUCT_DEFAULT_WIFI_CHANNELS) |
| 133 | writeString("# ro.build.product is obsolete; use ro.product.device") |
| 134 | writeProp("ro.build.product", $TARGET_DEVICE) |
| 135 | |
| 136 | writeString("# Do not try to parse description or thumbprint") |
| 137 | writeProp("ro.build.description", $PRIVATE_BUILD_DESC) |
| 138 | if [ -n "$BUILD_THUMBPRINT" ] { |
| 139 | writeProp("ro.build.thumbprint", $BUILD_THUMBPRINT) |
| 140 | } |
| 141 | */ |
| 142 | |
| 143 | writeString("# end build properties") |
| 144 | |
| 145 | cmd.Text("true) > ").Output(p.outputFilePath) |
| 146 | rule.Build("build.prop", "generating build.prop") |
| 147 | |
| 148 | if !p.installable() { |
| 149 | p.SkipInstall() |
| 150 | } |
| 151 | |
| 152 | p.installPath = PathForModuleInstall(ctx) |
| 153 | ctx.InstallFile(p.installPath, p.Name(), p.outputFilePath) |
| 154 | } |
| 155 | |
| 156 | func (f *buildinfoPropModule) GenerateSingletonBuildActions(ctx SingletonContext) { |
| 157 | // does nothing; buildinfo_prop is a singeton because two buildinfo modules don't make sense. |
| 158 | } |
| 159 | |
| 160 | func (p *buildinfoPropModule) AndroidMkEntries() []AndroidMkEntries { |
| 161 | return []AndroidMkEntries{AndroidMkEntries{ |
| 162 | Class: "ETC", |
| 163 | OutputFile: OptionalPathForPath(p.outputFilePath), |
| 164 | ExtraEntries: []AndroidMkExtraEntriesFunc{ |
| 165 | func(ctx AndroidMkExtraEntriesContext, entries *AndroidMkEntries) { |
| 166 | entries.SetString("LOCAL_MODULE_PATH", p.installPath.String()) |
| 167 | entries.SetString("LOCAL_INSTALLED_MODULE_STEM", p.outputFilePath.Base()) |
| 168 | entries.SetBoolIfTrue("LOCAL_UNINSTALLABLE_MODULE", !p.installable()) |
| 169 | }, |
| 170 | }, |
| 171 | }} |
| 172 | } |
| 173 | |
| 174 | // buildinfo_prop module generates a build.prop file, which contains a set of common |
| 175 | // system/build.prop properties, such as ro.build.version.*. Not all properties are implemented; |
| 176 | // currently this module is only for microdroid. |
| 177 | func buildinfoPropFactory() SingletonModule { |
| 178 | module := &buildinfoPropModule{} |
| 179 | module.AddProperties(&module.properties) |
| 180 | InitAndroidModule(module) |
| 181 | return module |
| 182 | } |