Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 1 | // Copyright (C) 2021 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 | |
| 15 | package aidl |
| 16 | |
| 17 | import ( |
| 18 | "android/soong/android" |
| 19 | |
| 20 | "fmt" |
| 21 | "io" |
| 22 | "path/filepath" |
| 23 | "strconv" |
| 24 | "strings" |
| 25 | |
| 26 | "github.com/google/blueprint" |
| 27 | "github.com/google/blueprint/proptools" |
| 28 | ) |
| 29 | |
| 30 | var ( |
| 31 | aidlDumpApiRule = pctx.StaticRule("aidlDumpApiRule", blueprint.RuleParams{ |
| 32 | Command: `rm -rf "${outDir}" && mkdir -p "${outDir}" && ` + |
| 33 | `${aidlCmd} --dumpapi --structured ${imports} ${optionalFlags} --out ${outDir} ${in} && ` + |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 34 | `${aidlHashGen} ${outDir} ${latestVersion} ${hashFile}`, |
| 35 | CommandDeps: []string{"${aidlCmd}", "${aidlHashGen}"}, |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 36 | }, "optionalFlags", "imports", "outDir", "hashFile", "latestVersion") |
| 37 | |
| 38 | aidlCheckApiRule = pctx.StaticRule("aidlCheckApiRule", blueprint.RuleParams{ |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 39 | Command: `(${aidlCmd} ${optionalFlags} --checkapi=${checkApiLevel} ${imports} ${old} ${new} && touch ${out}) || ` + |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 40 | `(cat ${messageFile} && exit 1)`, |
| 41 | CommandDeps: []string{"${aidlCmd}"}, |
| 42 | Description: "AIDL CHECK API: ${new} against ${old}", |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 43 | }, "optionalFlags", "imports", "old", "new", "messageFile", "checkApiLevel") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 44 | |
| 45 | aidlVerifyHashRule = pctx.StaticRule("aidlVerifyHashRule", blueprint.RuleParams{ |
| 46 | Command: `if [ $$(cd '${apiDir}' && { find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo ${version}; } | sha1sum | cut -d " " -f 1) = $$(read -r <'${hashFile}' hash extra; printf %s $$hash) ]; then ` + |
| 47 | `touch ${out}; else cat '${messageFile}' && exit 1; fi`, |
| 48 | Description: "Verify ${apiDir} files have not been modified", |
| 49 | }, "apiDir", "version", "messageFile", "hashFile") |
| 50 | ) |
| 51 | |
| 52 | type aidlApiProperties struct { |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame] | 53 | BaseName string |
| 54 | Srcs []string `android:"path"` |
| 55 | AidlRoot string // base directory for the input aidl file |
| 56 | Stability *string |
| 57 | ImportsWithoutVersion []string |
| 58 | Versions []string |
| 59 | Dumpapi DumpApiProperties |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | type aidlApi struct { |
| 63 | android.ModuleBase |
| 64 | |
| 65 | properties aidlApiProperties |
| 66 | |
| 67 | // for triggering api check for version X against version X-1 |
| 68 | checkApiTimestamps android.WritablePaths |
| 69 | |
| 70 | // for triggering updating current API |
| 71 | updateApiTimestamp android.WritablePath |
| 72 | |
| 73 | // for triggering check that files have not been modified |
| 74 | checkHashTimestamps android.WritablePaths |
| 75 | |
| 76 | // for triggering freezing API as the new version |
| 77 | freezeApiTimestamp android.WritablePath |
| 78 | } |
| 79 | |
| 80 | func (m *aidlApi) apiDir() string { |
| 81 | return filepath.Join(aidlApiDir, m.properties.BaseName) |
| 82 | } |
| 83 | |
| 84 | // `m <iface>-freeze-api` will freeze ToT as this version |
| 85 | func (m *aidlApi) nextVersion() string { |
Jeongik Cha | 52e9802 | 2021-01-20 18:37:20 +0900 | [diff] [blame] | 86 | return nextVersion(m.properties.Versions) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | type apiDump struct { |
| 90 | dir android.Path |
| 91 | files android.Paths |
| 92 | hashFile android.OptionalPath |
| 93 | } |
| 94 | |
| 95 | func (m *aidlApi) createApiDumpFromSource(ctx android.ModuleContext) apiDump { |
| 96 | srcs, imports := getPaths(ctx, m.properties.Srcs) |
| 97 | |
| 98 | if ctx.Failed() { |
| 99 | return apiDump{} |
| 100 | } |
| 101 | |
| 102 | var importPaths []string |
| 103 | importPaths = append(importPaths, imports...) |
| 104 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 105 | if importedAidl, ok := dep.(*aidlInterface); ok { |
| 106 | importPaths = append(importPaths, importedAidl.properties.Full_import_paths...) |
| 107 | } |
| 108 | }) |
| 109 | |
| 110 | var apiDir android.WritablePath |
| 111 | var apiFiles android.WritablePaths |
| 112 | var hashFile android.WritablePath |
| 113 | |
| 114 | apiDir = android.PathForModuleOut(ctx, "dump") |
| 115 | aidlRoot := android.PathForModuleSrc(ctx, m.properties.AidlRoot) |
| 116 | for _, src := range srcs { |
| 117 | baseDir := getBaseDir(ctx, src, aidlRoot) |
| 118 | relPath, _ := filepath.Rel(baseDir, src.String()) |
| 119 | outFile := android.PathForModuleOut(ctx, "dump", relPath) |
| 120 | apiFiles = append(apiFiles, outFile) |
| 121 | } |
| 122 | hashFile = android.PathForModuleOut(ctx, "dump", ".hash") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 123 | |
| 124 | var optionalFlags []string |
| 125 | if m.properties.Stability != nil { |
| 126 | optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability) |
| 127 | } |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame] | 128 | if proptools.Bool(m.properties.Dumpapi.No_license) { |
| 129 | optionalFlags = append(optionalFlags, "--no_license") |
| 130 | } |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 131 | |
| 132 | ctx.Build(pctx, android.BuildParams{ |
| 133 | Rule: aidlDumpApiRule, |
| 134 | Outputs: append(apiFiles, hashFile), |
| 135 | Inputs: srcs, |
| 136 | Args: map[string]string{ |
| 137 | "optionalFlags": strings.Join(optionalFlags, " "), |
| 138 | "imports": strings.Join(wrap("-I", importPaths, ""), " "), |
| 139 | "outDir": apiDir.String(), |
| 140 | "hashFile": hashFile.String(), |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 141 | "latestVersion": versionForHashGen(nextVersion(m.properties.Versions)), |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 142 | }, |
| 143 | }) |
| 144 | return apiDump{apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)} |
| 145 | } |
| 146 | |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 147 | func (m *aidlApi) makeApiDumpAsVersion(ctx android.ModuleContext, dump apiDump, version string, latestVersionDump *apiDump) android.WritablePath { |
| 148 | creatingNewVersion := version != currentVersion |
| 149 | moduleDir := android.PathForModuleSrc(ctx).String() |
| 150 | targetDir := filepath.Join(moduleDir, m.apiDir(), version) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 151 | rb := android.NewRuleBuilder(pctx, ctx) |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 152 | |
| 153 | if creatingNewVersion { |
| 154 | // We are asked to create a new version. But before doing that, check if the given |
| 155 | // dump is the same as the latest version. If so, don't create a new version, |
| 156 | // otherwise we will be unnecessarily creating many versions. `newVersionNeededFile` |
| 157 | // is created when the equality check fails. |
| 158 | newVersionNeededFile := android.PathForModuleOut(ctx, "updateapi_"+version+".needed") |
| 159 | rb.Command().Text("rm -f " + newVersionNeededFile.String()) |
| 160 | |
| 161 | if latestVersionDump != nil { |
| 162 | equalityCheckCommand := rb.Command() |
| 163 | equalityCheckCommand.BuiltTool("aidl"). |
| 164 | FlagWithArg("--checkapi=", "equal") |
| 165 | if m.properties.Stability != nil { |
| 166 | equalityCheckCommand.FlagWithArg("--stability ", *m.properties.Stability) |
| 167 | } |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 168 | importPaths, implicits := m.getImportsForCheckApi(ctx) |
| 169 | equalityCheckCommand.FlagForEachArg("-I", importPaths).Implicits(implicits) |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 170 | equalityCheckCommand. |
| 171 | Text(latestVersionDump.dir.String()).Implicits(latestVersionDump.files). |
| 172 | Text(dump.dir.String()).Implicits(dump.files). |
| 173 | Text("&> /dev/null") |
| 174 | equalityCheckCommand. |
| 175 | Text("|| touch"). |
| 176 | Text(newVersionNeededFile.String()) |
| 177 | } else { |
| 178 | // If there is no latest version (i.e. we are creating the initial version) |
| 179 | // create the new version unconditionally |
| 180 | rb.Command().Text("touch").Text(newVersionNeededFile.String()) |
| 181 | } |
| 182 | |
| 183 | // Copy the given dump to the target directory only when the equality check failed |
| 184 | // (i.e. `newVersionNeededFile` exists). |
| 185 | rb.Command(). |
| 186 | Text("if [ -f " + newVersionNeededFile.String() + " ]; then"). |
| 187 | Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files). |
| 188 | Text("; fi") |
| 189 | |
| 190 | // Also modify Android.bp file to add the new version to the 'versions' property. |
| 191 | rb.Command(). |
| 192 | Text("if [ -f " + newVersionNeededFile.String() + " ]; then"). |
| 193 | BuiltTool("bpmodify"). |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 194 | Text("-w -m " + m.properties.BaseName). |
| 195 | Text("-parameter versions -a " + version). |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 196 | Text(android.PathForModuleSrc(ctx, "Android.bp").String()). |
| 197 | Text("; fi") |
| 198 | |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 199 | } else { |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 200 | // We are updating the current version. Don't copy .hash to the current dump |
| 201 | rb.Command().Text("mkdir -p " + targetDir) |
| 202 | rb.Command().Text("rm -rf " + targetDir + "/*") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 203 | rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files) |
| 204 | } |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 205 | |
| 206 | timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 207 | rb.Command().Text("touch").Output(timestampFile) |
| 208 | |
| 209 | rb.Build("dump_aidl_api"+m.properties.BaseName+"_"+version, |
| 210 | "Making AIDL API of "+m.properties.BaseName+" as version "+version) |
| 211 | return timestampFile |
| 212 | } |
| 213 | |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 214 | // calculates "import" flags(-I) for --checkapi command. The list of imports differs from --dumpapi |
| 215 | // or --compile because --checkapi works with "apiDump"s. |
| 216 | // For example, local_include_dirs is not provided because apiDump has all .aidl files. |
| 217 | func (m *aidlApi) getImportsForCheckApi(ctx android.ModuleContext) (importPaths []string, implicits android.Paths) { |
| 218 | ctx.VisitDirectDeps(func(dep android.Module) { |
| 219 | if importedAidl, ok := dep.(*aidlInterface); ok { |
| 220 | switch ctx.OtherModuleDependencyTag(dep) { |
| 221 | case importDep: |
| 222 | if proptools.Bool(importedAidl.properties.Unstable) { |
| 223 | importPaths = append(importPaths, importedAidl.properties.Full_import_paths...) |
| 224 | } else { |
| 225 | // use "current" snapshot from stable "imported" modules |
| 226 | currentDir := filepath.Join(ctx.OtherModuleDir(dep), aidlApiDir, importedAidl.BaseModuleName(), currentVersion) |
| 227 | importPaths = append(importPaths, currentDir) |
| 228 | } |
| 229 | case interfaceDep: |
| 230 | importPaths = append(importPaths, importedAidl.properties.Include_dirs...) |
| 231 | } |
| 232 | } else if importedApi, ok := dep.(*aidlApi); ok { |
| 233 | // add imported module's checkapiTimestamps as implicits to make sure that imported apiDump is up-to-date |
| 234 | implicits = append(implicits, importedApi.checkApiTimestamps.Paths()...) |
| 235 | } |
| 236 | }) |
| 237 | return |
| 238 | } |
| 239 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 240 | func (m *aidlApi) checkApi(ctx android.ModuleContext, oldDump, newDump apiDump, checkApiLevel string, messageFile android.Path) android.WritablePath { |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 241 | newVersion := newDump.dir.Base() |
| 242 | timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 243 | |
| 244 | var optionalFlags []string |
| 245 | if m.properties.Stability != nil { |
| 246 | optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability) |
| 247 | } |
| 248 | |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 249 | importPaths, implicits := m.getImportsForCheckApi(ctx) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 250 | implicits = append(implicits, oldDump.files...) |
| 251 | implicits = append(implicits, newDump.files...) |
| 252 | implicits = append(implicits, messageFile) |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 253 | |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 254 | ctx.Build(pctx, android.BuildParams{ |
| 255 | Rule: aidlCheckApiRule, |
| 256 | Implicits: implicits, |
| 257 | Output: timestampFile, |
| 258 | Args: map[string]string{ |
| 259 | "optionalFlags": strings.Join(optionalFlags, " "), |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 260 | "imports": strings.Join(wrap("-I", importPaths, ""), " "), |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 261 | "old": oldDump.dir.String(), |
| 262 | "new": newDump.dir.String(), |
| 263 | "messageFile": messageFile.String(), |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 264 | "checkApiLevel": checkApiLevel, |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 265 | }, |
| 266 | }) |
| 267 | return timestampFile |
| 268 | } |
| 269 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 270 | func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath { |
| 271 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt") |
| 272 | return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile) |
| 273 | } |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 274 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 275 | func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldDump apiDump, newDump apiDump) android.WritablePath { |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 276 | // Use different messages depending on whether platform SDK is finalized or not. |
| 277 | // In case when it is finalized, we should never allow updating the already frozen API. |
| 278 | // If it's not finalized, we let users to update the current version by invoking |
| 279 | // `m <name>-update-api`. |
| 280 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt") |
| 281 | sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview() |
| 282 | if sdkIsFinal { |
| 283 | messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt") |
| 284 | } |
| 285 | formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt") |
| 286 | rb := android.NewRuleBuilder(pctx, ctx) |
| 287 | rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile) |
| 288 | rb.Build("format_message_"+m.properties.BaseName, "") |
| 289 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 290 | return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath { |
| 294 | version := dump.dir.Base() |
| 295 | timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp") |
| 296 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt") |
| 297 | |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 298 | var implicits android.Paths |
| 299 | implicits = append(implicits, dump.files...) |
| 300 | implicits = append(implicits, dump.hashFile.Path()) |
| 301 | implicits = append(implicits, messageFile) |
| 302 | ctx.Build(pctx, android.BuildParams{ |
| 303 | Rule: aidlVerifyHashRule, |
| 304 | Implicits: implicits, |
| 305 | Output: timestampFile, |
| 306 | Args: map[string]string{ |
| 307 | "apiDir": dump.dir.String(), |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 308 | "version": versionForHashGen(version), |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 309 | "hashFile": dump.hashFile.Path().String(), |
| 310 | "messageFile": messageFile.String(), |
| 311 | }, |
| 312 | }) |
| 313 | return timestampFile |
| 314 | } |
| 315 | |
| 316 | func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 317 | // An API dump is created from source and it is compared against the API dump of the |
| 318 | // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in |
| 319 | // the AIDL interface is gated by the AIDL API review even before the interface is frozen as |
| 320 | // a new version. |
| 321 | totApiDump := m.createApiDumpFromSource(ctx) |
| 322 | currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion) |
| 323 | var currentApiDump apiDump |
| 324 | if currentApiDir.Valid() { |
| 325 | currentApiDump = apiDump{ |
| 326 | dir: currentApiDir.Path(), |
| 327 | files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil), |
| 328 | hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"), |
| 329 | } |
| 330 | checked := m.checkEquality(ctx, currentApiDump, totApiDump) |
| 331 | m.checkApiTimestamps = append(m.checkApiTimestamps, checked) |
| 332 | } else { |
| 333 | // The "current" directory might not exist, in case when the interface is first created. |
| 334 | // Instruct user to create one by executing `m <name>-update-api`. |
| 335 | rb := android.NewRuleBuilder(pctx, ctx) |
| 336 | ifaceName := m.properties.BaseName |
| 337 | rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName)) |
| 338 | rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+ |
| 339 | `for the interface if it does not need to be versioned`, ifaceName)) |
| 340 | // This file will never be created. Otherwise, the build will pass simply by running 'm; m'. |
| 341 | alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp") |
| 342 | rb.Command().Text("false").ImplicitOutput(alwaysChecked) |
| 343 | rb.Build("check_current_aidl_api", "") |
| 344 | m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked) |
| 345 | } |
| 346 | |
| 347 | // Also check that version X is backwards compatible with version X-1. |
| 348 | // "current" is checked against the latest version. |
| 349 | var dumps []apiDump |
| 350 | for _, ver := range m.properties.Versions { |
| 351 | apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver) |
| 352 | apiDirPath := android.ExistentPathForSource(ctx, apiDir) |
| 353 | if apiDirPath.Valid() { |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 354 | hashFilePath := filepath.Join(apiDir, ".hash") |
| 355 | dump := apiDump{ |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 356 | dir: apiDirPath.Path(), |
| 357 | files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil), |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 358 | hashFile: android.ExistentPathForSource(ctx, hashFilePath), |
| 359 | } |
| 360 | if !dump.hashFile.Valid() { |
Jeongik Cha | c644217 | 2021-05-25 10:46:56 +0900 | [diff] [blame] | 361 | // We should show the source path of hash_gen because aidl_hash_gen cannot be built due to build error. |
| 362 | cmd := fmt.Sprintf(`(croot && system/tools/aidl/build/hash_gen.sh %s %s %s)`, apiDir, versionForHashGen(ver), hashFilePath) |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 363 | ctx.ModuleErrorf("A frozen aidl_interface must have '.hash' file, but %s-V%s doesn't have it. Use the command below to generate hash.\n%s\n", |
| 364 | m.properties.BaseName, ver, cmd) |
| 365 | } |
| 366 | dumps = append(dumps, dump) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 367 | } else if ctx.Config().AllowMissingDependencies() { |
| 368 | ctx.AddMissingDependencies([]string{apiDir}) |
| 369 | } else { |
| 370 | ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir) |
| 371 | } |
| 372 | } |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 373 | var latestVersionDump *apiDump |
| 374 | if len(dumps) >= 1 { |
| 375 | latestVersionDump = &dumps[len(dumps)-1] |
| 376 | } |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 377 | if currentApiDir.Valid() { |
| 378 | dumps = append(dumps, currentApiDump) |
| 379 | } |
| 380 | for i, _ := range dumps { |
| 381 | if dumps[i].hashFile.Valid() { |
| 382 | checkHashTimestamp := m.checkIntegrity(ctx, dumps[i]) |
| 383 | m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp) |
| 384 | } |
| 385 | |
| 386 | if i == 0 { |
| 387 | continue |
| 388 | } |
| 389 | checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i]) |
| 390 | m.checkApiTimestamps = append(m.checkApiTimestamps, checked) |
| 391 | } |
| 392 | |
| 393 | // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api` |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 394 | m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion, nil) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 395 | |
| 396 | // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api` |
| 397 | nextVersion := m.nextVersion() |
Jiyong Park | 72e0893 | 2021-05-21 00:18:20 +0900 | [diff] [blame] | 398 | m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion, latestVersionDump) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | func (m *aidlApi) AndroidMk() android.AndroidMkData { |
| 402 | return android.AndroidMkData{ |
| 403 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 404 | android.WriteAndroidMkData(w, data) |
| 405 | targetName := m.properties.BaseName + "-freeze-api" |
| 406 | fmt.Fprintln(w, ".PHONY:", targetName) |
| 407 | fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String()) |
| 408 | |
| 409 | targetName = m.properties.BaseName + "-update-api" |
| 410 | fmt.Fprintln(w, ".PHONY:", targetName) |
| 411 | fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String()) |
| 412 | }, |
| 413 | } |
| 414 | } |
| 415 | |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 416 | type depTag struct { |
| 417 | blueprint.BaseDependencyTag |
| 418 | name string |
| 419 | } |
| 420 | |
| 421 | var ( |
| 422 | importDep = depTag{name: "imported-interface"} |
| 423 | interfaceDep = depTag{name: "interface"} |
| 424 | importApiDep = depTag{name: "imported-api"} |
| 425 | ) |
| 426 | |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 427 | func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 428 | ctx.AddDependency(ctx.Module(), importApiDep, wrap("", m.properties.ImportsWithoutVersion, aidlApiSuffix)...) |
| 429 | ctx.AddDependency(ctx.Module(), importDep, wrap("", m.properties.ImportsWithoutVersion, aidlInterfaceSuffix)...) |
| 430 | ctx.AddDependency(ctx.Module(), interfaceDep, m.properties.BaseName+aidlInterfaceSuffix) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 431 | } |
| 432 | |
| 433 | func aidlApiFactory() android.Module { |
| 434 | m := &aidlApi{} |
| 435 | m.AddProperties(&m.properties) |
| 436 | android.InitAndroidModule(m) |
| 437 | return m |
| 438 | } |
| 439 | |
| 440 | func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string { |
| 441 | apiModule := i.ModuleBase.Name() + aidlApiSuffix |
Jeongik Cha | 52e9802 | 2021-01-20 18:37:20 +0900 | [diff] [blame] | 442 | srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion()) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 443 | mctx.CreateModule(aidlApiFactory, &nameProperties{ |
| 444 | Name: proptools.StringPtr(apiModule), |
| 445 | }, &aidlApiProperties{ |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame] | 446 | BaseName: i.ModuleBase.Name(), |
| 447 | Srcs: srcs, |
| 448 | AidlRoot: aidlRoot, |
| 449 | Stability: i.properties.Stability, |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 450 | ImportsWithoutVersion: i.properties.ImportsWithoutVersion, |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame] | 451 | Versions: i.properties.Versions, |
| 452 | Dumpapi: i.properties.Dumpapi, |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 453 | }) |
| 454 | return apiModule |
| 455 | } |
Jeongik Cha | 6cc16f3 | 2021-05-18 11:23:55 +0900 | [diff] [blame] | 456 | |
| 457 | func versionForHashGen(ver string) string { |
| 458 | // aidlHashGen uses the version before current version. If it has never been frozen, return 'latest-version'. |
| 459 | verInt, _ := strconv.Atoi(ver) |
| 460 | if verInt > 1 { |
| 461 | return strconv.Itoa(verInt - 1) |
| 462 | } |
| 463 | return "latest-version" |
| 464 | } |
Jiyong Park | e38dab4 | 2021-05-20 14:25:34 +0900 | [diff] [blame] | 465 | |
| 466 | func init() { |
| 467 | android.RegisterSingletonType("aidl-freeze-api", freezeApiSingletonFactory) |
| 468 | } |
| 469 | |
| 470 | func freezeApiSingletonFactory() android.Singleton { |
| 471 | return &freezeApiSingleton{} |
| 472 | } |
| 473 | |
| 474 | type freezeApiSingleton struct{} |
| 475 | |
| 476 | func (f *freezeApiSingleton) GenerateBuildActions(ctx android.SingletonContext) { |
| 477 | var files android.Paths |
| 478 | ctx.VisitAllModules(func(module android.Module) { |
| 479 | if !module.Enabled() { |
| 480 | return |
| 481 | } |
| 482 | if m, ok := module.(*aidlApi); ok { |
| 483 | files = append(files, m.freezeApiTimestamp) |
| 484 | } |
| 485 | }) |
| 486 | ctx.Phony("aidl-freeze-api", files...) |
| 487 | } |