blob: 6ae212f76fd58daaa506b72259c16da4034e41e7 [file] [log] [blame]
Jeongik Chada36d5a2021-01-20 00:43:34 +09001// 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
15package aidl
16
17import (
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
30var (
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 Cha6cc16f32021-05-18 11:23:55 +090034 `${aidlHashGen} ${outDir} ${latestVersion} ${hashFile}`,
35 CommandDeps: []string{"${aidlCmd}", "${aidlHashGen}"},
Jeongik Chada36d5a2021-01-20 00:43:34 +090036 }, "optionalFlags", "imports", "outDir", "hashFile", "latestVersion")
37
38 aidlCheckApiRule = pctx.StaticRule("aidlCheckApiRule", blueprint.RuleParams{
Jooyung Han8e3b72c2021-05-22 02:54:37 +090039 Command: `(${aidlCmd} ${optionalFlags} --checkapi=${checkApiLevel} ${imports} ${old} ${new} && touch ${out}) || ` +
Jeongik Chada36d5a2021-01-20 00:43:34 +090040 `(cat ${messageFile} && exit 1)`,
41 CommandDeps: []string{"${aidlCmd}"},
42 Description: "AIDL CHECK API: ${new} against ${old}",
Jooyung Han8e3b72c2021-05-22 02:54:37 +090043 }, "optionalFlags", "imports", "old", "new", "messageFile", "checkApiLevel")
Jeongik Chada36d5a2021-01-20 00:43:34 +090044
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
52type aidlApiProperties struct {
Jeongik Chaa01447d2021-03-17 17:43:12 +090053 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 Chada36d5a2021-01-20 00:43:34 +090060}
61
62type 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 Moore15b8ebe2021-05-21 09:36:37 -070078
79 // for checking for active development on unfrozen version
80 hasDevelopment android.WritablePath
Jeongik Chada36d5a2021-01-20 00:43:34 +090081}
82
83func (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
88func (m *aidlApi) nextVersion() string {
Jeongik Cha52e98022021-01-20 18:37:20 +090089 return nextVersion(m.properties.Versions)
Jeongik Chada36d5a2021-01-20 00:43:34 +090090}
91
92type apiDump struct {
93 dir android.Path
94 files android.Paths
95 hashFile android.OptionalPath
96}
97
98func (m *aidlApi) createApiDumpFromSource(ctx android.ModuleContext) apiDump {
Jooyung Han60699b52021-05-26 22:20:42 +090099 srcs, imports := getPaths(ctx, m.properties.Srcs, m.properties.AidlRoot)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900100
101 if ctx.Failed() {
102 return apiDump{}
103 }
104
Jooyung Han42c5f292021-06-04 19:49:34 +0900105 deps := getDeps(ctx)
106 imports = append(imports, deps.imports...)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900107
108 var apiDir android.WritablePath
109 var apiFiles android.WritablePaths
110 var hashFile android.WritablePath
111
112 apiDir = android.PathForModuleOut(ctx, "dump")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900113 for _, src := range srcs {
Jooyung Hanaf45dbb2021-06-03 13:56:24 +0900114 outFile := android.PathForModuleOut(ctx, "dump", src.Rel())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900115 apiFiles = append(apiFiles, outFile)
116 }
117 hashFile = android.PathForModuleOut(ctx, "dump", ".hash")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900118
119 var optionalFlags []string
120 if m.properties.Stability != nil {
121 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
122 }
Jooyung Han252657e2021-02-27 02:51:39 +0900123 if proptools.Bool(m.properties.Dumpapi.No_license) {
124 optionalFlags = append(optionalFlags, "--no_license")
125 }
Jooyung Han42c5f292021-06-04 19:49:34 +0900126 optionalFlags = append(optionalFlags, wrap("-p", deps.preprocessed.Strings(), "")...)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900127
128 ctx.Build(pctx, android.BuildParams{
Jooyung Han42c5f292021-06-04 19:49:34 +0900129 Rule: aidlDumpApiRule,
130 Outputs: append(apiFiles, hashFile),
131 Inputs: srcs,
132 Implicits: deps.preprocessed,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900133 Args: map[string]string{
134 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han60699b52021-05-26 22:20:42 +0900135 "imports": strings.Join(wrap("-I", imports, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900136 "outDir": apiDir.String(),
137 "hashFile": hashFile.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900138 "latestVersion": versionForHashGen(nextVersion(m.properties.Versions)),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900139 },
140 })
141 return apiDump{apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)}
142}
143
Jiyong Park72e08932021-05-21 00:18:20 +0900144func (m *aidlApi) makeApiDumpAsVersion(ctx android.ModuleContext, dump apiDump, version string, latestVersionDump *apiDump) android.WritablePath {
145 creatingNewVersion := version != currentVersion
146 moduleDir := android.PathForModuleSrc(ctx).String()
147 targetDir := filepath.Join(moduleDir, m.apiDir(), version)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900148 rb := android.NewRuleBuilder(pctx, ctx)
Jiyong Park72e08932021-05-21 00:18:20 +0900149
150 if creatingNewVersion {
151 // We are asked to create a new version. But before doing that, check if the given
152 // dump is the same as the latest version. If so, don't create a new version,
Devin Moore15b8ebe2021-05-21 09:36:37 -0700153 // otherwise we will be unnecessarily creating many versions.
Jiyong Park72e08932021-05-21 00:18:20 +0900154 // Copy the given dump to the target directory only when the equality check failed
Devin Moore15b8ebe2021-05-21 09:36:37 -0700155 // (i.e. `has_development` file contains "1").
Jiyong Park72e08932021-05-21 00:18:20 +0900156 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700157 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jiyong Park72e08932021-05-21 00:18:20 +0900158 Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files).
159 Text("; fi")
160
161 // Also modify Android.bp file to add the new version to the 'versions' property.
162 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700163 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jiyong Park72e08932021-05-21 00:18:20 +0900164 BuiltTool("bpmodify").
Jeongik Chada36d5a2021-01-20 00:43:34 +0900165 Text("-w -m " + m.properties.BaseName).
166 Text("-parameter versions -a " + version).
Jiyong Park72e08932021-05-21 00:18:20 +0900167 Text(android.PathForModuleSrc(ctx, "Android.bp").String()).
168 Text("; fi")
169
Jeongik Chada36d5a2021-01-20 00:43:34 +0900170 } else {
Jiyong Park72e08932021-05-21 00:18:20 +0900171 // We are updating the current version. Don't copy .hash to the current dump
172 rb.Command().Text("mkdir -p " + targetDir)
173 rb.Command().Text("rm -rf " + targetDir + "/*")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900174 rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files)
175 }
Jiyong Park72e08932021-05-21 00:18:20 +0900176
177 timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900178 rb.Command().Text("touch").Output(timestampFile)
179
180 rb.Build("dump_aidl_api"+m.properties.BaseName+"_"+version,
181 "Making AIDL API of "+m.properties.BaseName+" as version "+version)
182 return timestampFile
183}
184
Jooyung Han42c5f292021-06-04 19:49:34 +0900185type deps struct {
186 preprocessed android.Paths
187 implicits android.Paths
188 imports []string
189}
190
Jooyung Han60699b52021-05-26 22:20:42 +0900191// calculates import flags(-I) from deps.
192// When the target is ToT, use ToT of imported interfaces. If not, we use "current" snapshot of
193// imported interfaces.
Jooyung Han42c5f292021-06-04 19:49:34 +0900194func getDeps(ctx android.ModuleContext) deps {
195 var deps deps
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900196 ctx.VisitDirectDeps(func(dep android.Module) {
Jooyung Han60699b52021-05-26 22:20:42 +0900197 switch ctx.OtherModuleDependencyTag(dep) {
198 case importInterfaceDep:
199 iface := dep.(*aidlInterface)
Jooyung Han42c5f292021-06-04 19:49:34 +0900200 deps.preprocessed = append(deps.preprocessed, iface.preprocessed)
Jooyung Han60699b52021-05-26 22:20:42 +0900201 case interfaceDep:
202 iface := dep.(*aidlInterface)
Jooyung Han42c5f292021-06-04 19:49:34 +0900203 deps.imports = append(deps.imports, iface.properties.Include_dirs...)
Jooyung Han60699b52021-05-26 22:20:42 +0900204 case importApiDep, apiDep:
205 api := dep.(*aidlApi)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900206 // add imported module's checkapiTimestamps as implicits to make sure that imported apiDump is up-to-date
Jooyung Han42c5f292021-06-04 19:49:34 +0900207 deps.implicits = append(deps.implicits, api.checkApiTimestamps.Paths()...)
208 deps.implicits = append(deps.implicits, api.checkHashTimestamps.Paths()...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900209 }
210 })
Jooyung Han42c5f292021-06-04 19:49:34 +0900211 return deps
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900212}
213
Jooyung Hanb8a97772021-01-19 01:27:38 +0900214func (m *aidlApi) checkApi(ctx android.ModuleContext, oldDump, newDump apiDump, checkApiLevel string, messageFile android.Path) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900215 newVersion := newDump.dir.Base()
216 timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900217
Jooyung Han42c5f292021-06-04 19:49:34 +0900218 deps := getDeps(ctx)
219 var implicits android.Paths
220 implicits = append(implicits, deps.implicits...)
221 implicits = append(implicits, deps.preprocessed...)
222 implicits = append(implicits, oldDump.files...)
223 implicits = append(implicits, newDump.files...)
224 implicits = append(implicits, messageFile)
225
Jeongik Chada36d5a2021-01-20 00:43:34 +0900226 var optionalFlags []string
227 if m.properties.Stability != nil {
228 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
229 }
Jooyung Han42c5f292021-06-04 19:49:34 +0900230 optionalFlags = append(optionalFlags, wrap("-p", deps.preprocessed.Strings(), "")...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900231
Jeongik Chada36d5a2021-01-20 00:43:34 +0900232 ctx.Build(pctx, android.BuildParams{
233 Rule: aidlCheckApiRule,
234 Implicits: implicits,
235 Output: timestampFile,
236 Args: map[string]string{
237 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han42c5f292021-06-04 19:49:34 +0900238 "imports": strings.Join(wrap("-I", deps.imports, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900239 "old": oldDump.dir.String(),
240 "new": newDump.dir.String(),
241 "messageFile": messageFile.String(),
Jooyung Hanb8a97772021-01-19 01:27:38 +0900242 "checkApiLevel": checkApiLevel,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900243 },
244 })
245 return timestampFile
246}
247
Jooyung Hanb8a97772021-01-19 01:27:38 +0900248func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath {
249 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt")
250 return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile)
251}
Jeongik Chada36d5a2021-01-20 00:43:34 +0900252
Jooyung Hanb8a97772021-01-19 01:27:38 +0900253func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldDump apiDump, newDump apiDump) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900254 // Use different messages depending on whether platform SDK is finalized or not.
255 // In case when it is finalized, we should never allow updating the already frozen API.
256 // If it's not finalized, we let users to update the current version by invoking
257 // `m <name>-update-api`.
258 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt")
259 sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview()
260 if sdkIsFinal {
261 messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt")
262 }
263 formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt")
264 rb := android.NewRuleBuilder(pctx, ctx)
265 rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile)
266 rb.Build("format_message_"+m.properties.BaseName, "")
267
Jooyung Hanb8a97772021-01-19 01:27:38 +0900268 return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900269}
270
271func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath {
272 version := dump.dir.Base()
273 timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp")
274 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt")
275
Jeongik Chada36d5a2021-01-20 00:43:34 +0900276 var implicits android.Paths
277 implicits = append(implicits, dump.files...)
278 implicits = append(implicits, dump.hashFile.Path())
279 implicits = append(implicits, messageFile)
280 ctx.Build(pctx, android.BuildParams{
281 Rule: aidlVerifyHashRule,
282 Implicits: implicits,
283 Output: timestampFile,
284 Args: map[string]string{
285 "apiDir": dump.dir.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900286 "version": versionForHashGen(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900287 "hashFile": dump.hashFile.Path().String(),
288 "messageFile": messageFile.String(),
289 },
290 })
291 return timestampFile
292}
293
Devin Moore15b8ebe2021-05-21 09:36:37 -0700294func (m *aidlApi) checkForDevelopment(ctx android.ModuleContext, latestVersionDump *apiDump, totDump apiDump) android.WritablePath {
295 hasDevPath := android.PathForModuleOut(ctx, "has_development")
296 rb := android.NewRuleBuilder(pctx, ctx)
297 rb.Command().Text("rm -f " + hasDevPath.String())
298 if latestVersionDump != nil {
299 hasDevCommand := rb.Command()
300 hasDevCommand.BuiltTool("aidl").FlagWithArg("--checkapi=", "equal")
301 if m.properties.Stability != nil {
302 hasDevCommand.FlagWithArg("--stability ", *m.properties.Stability)
303 }
Jooyung Han42c5f292021-06-04 19:49:34 +0900304 deps := getDeps(ctx)
Devin Moore15b8ebe2021-05-21 09:36:37 -0700305 hasDevCommand.
Jooyung Han42c5f292021-06-04 19:49:34 +0900306 FlagForEachArg("-I", deps.imports).Implicits(deps.implicits).
307 FlagForEachInput("-p", deps.preprocessed).
Devin Moore15b8ebe2021-05-21 09:36:37 -0700308 Text(latestVersionDump.dir.String()).Implicits(latestVersionDump.files).
309 Text(totDump.dir.String()).Implicits(totDump.files).
Devin Moore45e93ca2021-06-04 13:57:38 -0700310 Text("2> /dev/null; echo $? >").Output(hasDevPath)
Devin Moore15b8ebe2021-05-21 09:36:37 -0700311 } else {
312 rb.Command().Text("echo 1 >").Output(hasDevPath)
313 }
314 rb.Build("check_for_development", "")
315 return hasDevPath
316}
317
Jeongik Chada36d5a2021-01-20 00:43:34 +0900318func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) {
319 // An API dump is created from source and it is compared against the API dump of the
320 // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in
321 // the AIDL interface is gated by the AIDL API review even before the interface is frozen as
322 // a new version.
323 totApiDump := m.createApiDumpFromSource(ctx)
324 currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion)
325 var currentApiDump apiDump
326 if currentApiDir.Valid() {
327 currentApiDump = apiDump{
328 dir: currentApiDir.Path(),
329 files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil),
330 hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"),
331 }
332 checked := m.checkEquality(ctx, currentApiDump, totApiDump)
333 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
334 } else {
335 // The "current" directory might not exist, in case when the interface is first created.
336 // Instruct user to create one by executing `m <name>-update-api`.
337 rb := android.NewRuleBuilder(pctx, ctx)
338 ifaceName := m.properties.BaseName
339 rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName))
340 rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+
341 `for the interface if it does not need to be versioned`, ifaceName))
342 // This file will never be created. Otherwise, the build will pass simply by running 'm; m'.
343 alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp")
344 rb.Command().Text("false").ImplicitOutput(alwaysChecked)
345 rb.Build("check_current_aidl_api", "")
346 m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked)
347 }
348
349 // Also check that version X is backwards compatible with version X-1.
350 // "current" is checked against the latest version.
351 var dumps []apiDump
352 for _, ver := range m.properties.Versions {
353 apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver)
354 apiDirPath := android.ExistentPathForSource(ctx, apiDir)
355 if apiDirPath.Valid() {
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900356 hashFilePath := filepath.Join(apiDir, ".hash")
357 dump := apiDump{
Jeongik Chada36d5a2021-01-20 00:43:34 +0900358 dir: apiDirPath.Path(),
359 files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900360 hashFile: android.ExistentPathForSource(ctx, hashFilePath),
361 }
362 if !dump.hashFile.Valid() {
Jeongik Chac6442172021-05-25 10:46:56 +0900363 // We should show the source path of hash_gen because aidl_hash_gen cannot be built due to build error.
364 cmd := fmt.Sprintf(`(croot && system/tools/aidl/build/hash_gen.sh %s %s %s)`, apiDir, versionForHashGen(ver), hashFilePath)
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900365 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",
366 m.properties.BaseName, ver, cmd)
367 }
368 dumps = append(dumps, dump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900369 } else if ctx.Config().AllowMissingDependencies() {
370 ctx.AddMissingDependencies([]string{apiDir})
371 } else {
372 ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir)
373 }
374 }
Jiyong Park72e08932021-05-21 00:18:20 +0900375 var latestVersionDump *apiDump
376 if len(dumps) >= 1 {
377 latestVersionDump = &dumps[len(dumps)-1]
378 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900379 if currentApiDir.Valid() {
380 dumps = append(dumps, currentApiDump)
381 }
382 for i, _ := range dumps {
383 if dumps[i].hashFile.Valid() {
384 checkHashTimestamp := m.checkIntegrity(ctx, dumps[i])
385 m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp)
386 }
387
388 if i == 0 {
389 continue
390 }
391 checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i])
392 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
393 }
394
Devin Moore15b8ebe2021-05-21 09:36:37 -0700395 // Check for active development on the unfrozen version
396 m.hasDevelopment = m.checkForDevelopment(ctx, latestVersionDump, totApiDump)
397
Jeongik Chada36d5a2021-01-20 00:43:34 +0900398 // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api`
Jiyong Park72e08932021-05-21 00:18:20 +0900399 m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion, nil)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900400
401 // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api`
402 nextVersion := m.nextVersion()
Jiyong Park72e08932021-05-21 00:18:20 +0900403 m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion, latestVersionDump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900404}
405
406func (m *aidlApi) AndroidMk() android.AndroidMkData {
407 return android.AndroidMkData{
408 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
409 android.WriteAndroidMkData(w, data)
410 targetName := m.properties.BaseName + "-freeze-api"
411 fmt.Fprintln(w, ".PHONY:", targetName)
412 fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String())
413
414 targetName = m.properties.BaseName + "-update-api"
415 fmt.Fprintln(w, ".PHONY:", targetName)
416 fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String())
417 },
418 }
419}
420
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900421type depTag struct {
422 blueprint.BaseDependencyTag
423 name string
424}
425
426var (
Jooyung Han60699b52021-05-26 22:20:42 +0900427 apiDep = depTag{name: "api"}
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900428 interfaceDep = depTag{name: "interface"}
Jooyung Han60699b52021-05-26 22:20:42 +0900429
430 importApiDep = depTag{name: "imported-api"}
431 importInterfaceDep = depTag{name: "imported-interface"}
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900432)
433
Jeongik Chada36d5a2021-01-20 00:43:34 +0900434func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900435 ctx.AddDependency(ctx.Module(), interfaceDep, m.properties.BaseName+aidlInterfaceSuffix)
Jooyung Han42c5f292021-06-04 19:49:34 +0900436 ctx.AddDependency(ctx.Module(), importInterfaceDep, wrap("", m.properties.ImportsWithoutVersion, aidlInterfaceSuffix)...)
437 ctx.AddDependency(ctx.Module(), importApiDep, wrap("", m.properties.ImportsWithoutVersion, aidlApiSuffix)...)
Devin Moore15b8ebe2021-05-21 09:36:37 -0700438 ctx.AddReverseDependency(ctx.Module(), nil, aidlMetadataSingletonName)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900439}
440
441func aidlApiFactory() android.Module {
442 m := &aidlApi{}
443 m.AddProperties(&m.properties)
444 android.InitAndroidModule(m)
445 return m
446}
447
448func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string {
449 apiModule := i.ModuleBase.Name() + aidlApiSuffix
Jeongik Cha52e98022021-01-20 18:37:20 +0900450 srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900451 mctx.CreateModule(aidlApiFactory, &nameProperties{
452 Name: proptools.StringPtr(apiModule),
453 }, &aidlApiProperties{
Jeongik Chaa01447d2021-03-17 17:43:12 +0900454 BaseName: i.ModuleBase.Name(),
455 Srcs: srcs,
456 AidlRoot: aidlRoot,
457 Stability: i.properties.Stability,
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900458 ImportsWithoutVersion: i.properties.ImportsWithoutVersion,
Jeongik Chaa01447d2021-03-17 17:43:12 +0900459 Versions: i.properties.Versions,
460 Dumpapi: i.properties.Dumpapi,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900461 })
462 return apiModule
463}
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900464
465func versionForHashGen(ver string) string {
466 // aidlHashGen uses the version before current version. If it has never been frozen, return 'latest-version'.
467 verInt, _ := strconv.Atoi(ver)
468 if verInt > 1 {
469 return strconv.Itoa(verInt - 1)
470 }
471 return "latest-version"
472}
Jiyong Parke38dab42021-05-20 14:25:34 +0900473
474func init() {
475 android.RegisterSingletonType("aidl-freeze-api", freezeApiSingletonFactory)
476}
477
478func freezeApiSingletonFactory() android.Singleton {
479 return &freezeApiSingleton{}
480}
481
482type freezeApiSingleton struct{}
483
484func (f *freezeApiSingleton) GenerateBuildActions(ctx android.SingletonContext) {
485 var files android.Paths
486 ctx.VisitAllModules(func(module android.Module) {
487 if !module.Enabled() {
488 return
489 }
490 if m, ok := module.(*aidlApi); ok {
491 files = append(files, m.freezeApiTimestamp)
492 }
493 })
494 ctx.Phony("aidl-freeze-api", files...)
495}