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