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} && ` + |
| 34 | `(cd ${outDir} && find ./ -name "*.aidl" -print0 | LC_ALL=C sort -z | xargs -0 sha1sum && echo ${latestVersion}) | sha1sum | cut -d " " -f 1 > ${hashFile} `, |
| 35 | CommandDeps: []string{"${aidlCmd}"}, |
| 36 | }, "optionalFlags", "imports", "outDir", "hashFile", "latestVersion") |
| 37 | |
| 38 | aidlCheckApiRule = pctx.StaticRule("aidlCheckApiRule", blueprint.RuleParams{ |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 39 | Command: `(${aidlCmd} ${optionalFlags} --checkapi=${checkApiLevel} ${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 | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 43 | }, "optionalFlags", "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") |
| 123 | latestVersion := "latest-version" |
| 124 | if len(m.properties.Versions) >= 1 { |
| 125 | latestVersion = m.properties.Versions[len(m.properties.Versions)-1] |
| 126 | } |
| 127 | |
| 128 | var optionalFlags []string |
| 129 | if m.properties.Stability != nil { |
| 130 | optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability) |
| 131 | } |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame] | 132 | if proptools.Bool(m.properties.Dumpapi.No_license) { |
| 133 | optionalFlags = append(optionalFlags, "--no_license") |
| 134 | } |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 135 | |
| 136 | ctx.Build(pctx, android.BuildParams{ |
| 137 | Rule: aidlDumpApiRule, |
| 138 | Outputs: append(apiFiles, hashFile), |
| 139 | Inputs: srcs, |
| 140 | Args: map[string]string{ |
| 141 | "optionalFlags": strings.Join(optionalFlags, " "), |
| 142 | "imports": strings.Join(wrap("-I", importPaths, ""), " "), |
| 143 | "outDir": apiDir.String(), |
| 144 | "hashFile": hashFile.String(), |
| 145 | "latestVersion": latestVersion, |
| 146 | }, |
| 147 | }) |
| 148 | return apiDump{apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)} |
| 149 | } |
| 150 | |
| 151 | func (m *aidlApi) makeApiDumpAsVersion(ctx android.ModuleContext, dump apiDump, version string) android.WritablePath { |
| 152 | timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp") |
| 153 | |
| 154 | modulePath := android.PathForModuleSrc(ctx).String() |
| 155 | |
| 156 | targetDir := filepath.Join(modulePath, m.apiDir(), version) |
| 157 | rb := android.NewRuleBuilder(pctx, ctx) |
| 158 | // Wipe the target directory and then copy the API dump into the directory |
| 159 | rb.Command().Text("mkdir -p " + targetDir) |
| 160 | rb.Command().Text("rm -rf " + targetDir + "/*") |
| 161 | if version != currentVersion { |
| 162 | rb.Command().Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files) |
| 163 | // If this is making a new frozen (i.e. non-current) version of the interface, |
| 164 | // modify Android.bp file to add the new version to the 'versions' property. |
| 165 | rb.Command().BuiltTool("bpmodify"). |
| 166 | Text("-w -m " + m.properties.BaseName). |
| 167 | Text("-parameter versions -a " + version). |
| 168 | Text(android.PathForModuleSrc(ctx, "Android.bp").String()) |
| 169 | } else { |
| 170 | // In this case (unfrozen interface), don't copy .hash |
| 171 | rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files) |
| 172 | } |
| 173 | rb.Command().Text("touch").Output(timestampFile) |
| 174 | |
| 175 | rb.Build("dump_aidl_api"+m.properties.BaseName+"_"+version, |
| 176 | "Making AIDL API of "+m.properties.BaseName+" as version "+version) |
| 177 | return timestampFile |
| 178 | } |
| 179 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 180 | 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] | 181 | newVersion := newDump.dir.Base() |
| 182 | timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp") |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 183 | |
| 184 | var optionalFlags []string |
| 185 | if m.properties.Stability != nil { |
| 186 | optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability) |
| 187 | } |
| 188 | |
| 189 | var implicits android.Paths |
| 190 | implicits = append(implicits, oldDump.files...) |
| 191 | implicits = append(implicits, newDump.files...) |
| 192 | implicits = append(implicits, messageFile) |
| 193 | ctx.Build(pctx, android.BuildParams{ |
| 194 | Rule: aidlCheckApiRule, |
| 195 | Implicits: implicits, |
| 196 | Output: timestampFile, |
| 197 | Args: map[string]string{ |
| 198 | "optionalFlags": strings.Join(optionalFlags, " "), |
| 199 | "old": oldDump.dir.String(), |
| 200 | "new": newDump.dir.String(), |
| 201 | "messageFile": messageFile.String(), |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 202 | "checkApiLevel": checkApiLevel, |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 203 | }, |
| 204 | }) |
| 205 | return timestampFile |
| 206 | } |
| 207 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 208 | func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath { |
| 209 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt") |
| 210 | return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile) |
| 211 | } |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 212 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 213 | 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] | 214 | // Use different messages depending on whether platform SDK is finalized or not. |
| 215 | // In case when it is finalized, we should never allow updating the already frozen API. |
| 216 | // If it's not finalized, we let users to update the current version by invoking |
| 217 | // `m <name>-update-api`. |
| 218 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt") |
| 219 | sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview() |
| 220 | if sdkIsFinal { |
| 221 | messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt") |
| 222 | } |
| 223 | formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt") |
| 224 | rb := android.NewRuleBuilder(pctx, ctx) |
| 225 | rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile) |
| 226 | rb.Build("format_message_"+m.properties.BaseName, "") |
| 227 | |
| 228 | var implicits android.Paths |
| 229 | implicits = append(implicits, oldDump.files...) |
| 230 | implicits = append(implicits, newDump.files...) |
| 231 | implicits = append(implicits, formattedMessageFile) |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 232 | return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath { |
| 236 | version := dump.dir.Base() |
| 237 | timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp") |
| 238 | messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt") |
| 239 | |
| 240 | i, _ := strconv.Atoi(version) |
| 241 | if i == 1 { |
| 242 | version = "latest-version" |
| 243 | } else { |
| 244 | version = strconv.Itoa(i - 1) |
| 245 | } |
| 246 | |
| 247 | var implicits android.Paths |
| 248 | implicits = append(implicits, dump.files...) |
| 249 | implicits = append(implicits, dump.hashFile.Path()) |
| 250 | implicits = append(implicits, messageFile) |
| 251 | ctx.Build(pctx, android.BuildParams{ |
| 252 | Rule: aidlVerifyHashRule, |
| 253 | Implicits: implicits, |
| 254 | Output: timestampFile, |
| 255 | Args: map[string]string{ |
| 256 | "apiDir": dump.dir.String(), |
| 257 | "version": version, |
| 258 | "hashFile": dump.hashFile.Path().String(), |
| 259 | "messageFile": messageFile.String(), |
| 260 | }, |
| 261 | }) |
| 262 | return timestampFile |
| 263 | } |
| 264 | |
| 265 | func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) { |
| 266 | // An API dump is created from source and it is compared against the API dump of the |
| 267 | // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in |
| 268 | // the AIDL interface is gated by the AIDL API review even before the interface is frozen as |
| 269 | // a new version. |
| 270 | totApiDump := m.createApiDumpFromSource(ctx) |
| 271 | currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion) |
| 272 | var currentApiDump apiDump |
| 273 | if currentApiDir.Valid() { |
| 274 | currentApiDump = apiDump{ |
| 275 | dir: currentApiDir.Path(), |
| 276 | files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil), |
| 277 | hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"), |
| 278 | } |
| 279 | checked := m.checkEquality(ctx, currentApiDump, totApiDump) |
| 280 | m.checkApiTimestamps = append(m.checkApiTimestamps, checked) |
| 281 | } else { |
| 282 | // The "current" directory might not exist, in case when the interface is first created. |
| 283 | // Instruct user to create one by executing `m <name>-update-api`. |
| 284 | rb := android.NewRuleBuilder(pctx, ctx) |
| 285 | ifaceName := m.properties.BaseName |
| 286 | rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName)) |
| 287 | rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+ |
| 288 | `for the interface if it does not need to be versioned`, ifaceName)) |
| 289 | // This file will never be created. Otherwise, the build will pass simply by running 'm; m'. |
| 290 | alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp") |
| 291 | rb.Command().Text("false").ImplicitOutput(alwaysChecked) |
| 292 | rb.Build("check_current_aidl_api", "") |
| 293 | m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked) |
| 294 | } |
| 295 | |
| 296 | // Also check that version X is backwards compatible with version X-1. |
| 297 | // "current" is checked against the latest version. |
| 298 | var dumps []apiDump |
| 299 | for _, ver := range m.properties.Versions { |
| 300 | apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver) |
| 301 | apiDirPath := android.ExistentPathForSource(ctx, apiDir) |
| 302 | if apiDirPath.Valid() { |
| 303 | dumps = append(dumps, apiDump{ |
| 304 | dir: apiDirPath.Path(), |
| 305 | files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil), |
| 306 | hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), ver, ".hash"), |
| 307 | }) |
| 308 | } else if ctx.Config().AllowMissingDependencies() { |
| 309 | ctx.AddMissingDependencies([]string{apiDir}) |
| 310 | } else { |
| 311 | ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir) |
| 312 | } |
| 313 | } |
| 314 | if currentApiDir.Valid() { |
| 315 | dumps = append(dumps, currentApiDump) |
| 316 | } |
| 317 | for i, _ := range dumps { |
| 318 | if dumps[i].hashFile.Valid() { |
| 319 | checkHashTimestamp := m.checkIntegrity(ctx, dumps[i]) |
| 320 | m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp) |
| 321 | } |
| 322 | |
| 323 | if i == 0 { |
| 324 | continue |
| 325 | } |
| 326 | checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i]) |
| 327 | m.checkApiTimestamps = append(m.checkApiTimestamps, checked) |
| 328 | } |
| 329 | |
| 330 | // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api` |
| 331 | m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion) |
| 332 | |
| 333 | // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api` |
| 334 | nextVersion := m.nextVersion() |
| 335 | m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion) |
| 336 | } |
| 337 | |
| 338 | func (m *aidlApi) AndroidMk() android.AndroidMkData { |
| 339 | return android.AndroidMkData{ |
| 340 | Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) { |
| 341 | android.WriteAndroidMkData(w, data) |
| 342 | targetName := m.properties.BaseName + "-freeze-api" |
| 343 | fmt.Fprintln(w, ".PHONY:", targetName) |
| 344 | fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String()) |
| 345 | |
| 346 | targetName = m.properties.BaseName + "-update-api" |
| 347 | fmt.Fprintln(w, ".PHONY:", targetName) |
| 348 | fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String()) |
| 349 | }, |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) { |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame^] | 354 | ctx.AddDependency(ctx.Module(), nil, wrap("", m.properties.ImportsWithoutVersion, aidlInterfaceSuffix)...) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | func aidlApiFactory() android.Module { |
| 358 | m := &aidlApi{} |
| 359 | m.AddProperties(&m.properties) |
| 360 | android.InitAndroidModule(m) |
| 361 | return m |
| 362 | } |
| 363 | |
| 364 | func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string { |
| 365 | apiModule := i.ModuleBase.Name() + aidlApiSuffix |
Jeongik Cha | 52e9802 | 2021-01-20 18:37:20 +0900 | [diff] [blame] | 366 | srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion()) |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 367 | mctx.CreateModule(aidlApiFactory, &nameProperties{ |
| 368 | Name: proptools.StringPtr(apiModule), |
| 369 | }, &aidlApiProperties{ |
Jeongik Cha | a01447d | 2021-03-17 17:43:12 +0900 | [diff] [blame^] | 370 | BaseName: i.ModuleBase.Name(), |
| 371 | Srcs: srcs, |
| 372 | AidlRoot: aidlRoot, |
| 373 | Stability: i.properties.Stability, |
| 374 | ImportsWithoutVersion: concat(i.properties.ImportsWithoutVersion, []string{i.ModuleBase.Name()}), |
| 375 | Versions: i.properties.Versions, |
| 376 | Dumpapi: i.properties.Dumpapi, |
Jeongik Cha | da36d5a | 2021-01-20 00:43:34 +0900 | [diff] [blame] | 377 | }) |
| 378 | return apiModule |
| 379 | } |