blob: 5f0b0a4ecb3ac149ce5fe7970905014b93c53201 [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 Han60699b52021-05-26 22:20:42 +0900105 importPaths, _ := getImportsFromDeps(ctx, true)
106 imports = append(imports, importPaths...)
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 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900126
127 ctx.Build(pctx, android.BuildParams{
128 Rule: aidlDumpApiRule,
129 Outputs: append(apiFiles, hashFile),
130 Inputs: srcs,
131 Args: map[string]string{
132 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han60699b52021-05-26 22:20:42 +0900133 "imports": strings.Join(wrap("-I", imports, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900134 "outDir": apiDir.String(),
135 "hashFile": hashFile.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900136 "latestVersion": versionForHashGen(nextVersion(m.properties.Versions)),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900137 },
138 })
139 return apiDump{apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)}
140}
141
Jiyong Park72e08932021-05-21 00:18:20 +0900142func (m *aidlApi) makeApiDumpAsVersion(ctx android.ModuleContext, dump apiDump, version string, latestVersionDump *apiDump) android.WritablePath {
143 creatingNewVersion := version != currentVersion
144 moduleDir := android.PathForModuleSrc(ctx).String()
145 targetDir := filepath.Join(moduleDir, m.apiDir(), version)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900146 rb := android.NewRuleBuilder(pctx, ctx)
Jiyong Park72e08932021-05-21 00:18:20 +0900147
148 if creatingNewVersion {
149 // We are asked to create a new version. But before doing that, check if the given
150 // dump is the same as the latest version. If so, don't create a new version,
Devin Moore15b8ebe2021-05-21 09:36:37 -0700151 // otherwise we will be unnecessarily creating many versions.
Jiyong Park72e08932021-05-21 00:18:20 +0900152 // Copy the given dump to the target directory only when the equality check failed
Devin Moore15b8ebe2021-05-21 09:36:37 -0700153 // (i.e. `has_development` file contains "1").
Jiyong Park72e08932021-05-21 00:18:20 +0900154 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700155 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jiyong Park72e08932021-05-21 00:18:20 +0900156 Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files).
157 Text("; fi")
158
159 // Also modify Android.bp file to add the new version to the 'versions' property.
160 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700161 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jiyong Park72e08932021-05-21 00:18:20 +0900162 BuiltTool("bpmodify").
Jeongik Chada36d5a2021-01-20 00:43:34 +0900163 Text("-w -m " + m.properties.BaseName).
164 Text("-parameter versions -a " + version).
Jiyong Park72e08932021-05-21 00:18:20 +0900165 Text(android.PathForModuleSrc(ctx, "Android.bp").String()).
166 Text("; fi")
167
Jeongik Chada36d5a2021-01-20 00:43:34 +0900168 } else {
Jiyong Park72e08932021-05-21 00:18:20 +0900169 // We are updating the current version. Don't copy .hash to the current dump
170 rb.Command().Text("mkdir -p " + targetDir)
171 rb.Command().Text("rm -rf " + targetDir + "/*")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900172 rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files)
173 }
Jiyong Park72e08932021-05-21 00:18:20 +0900174
175 timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900176 rb.Command().Text("touch").Output(timestampFile)
177
178 rb.Build("dump_aidl_api"+m.properties.BaseName+"_"+version,
179 "Making AIDL API of "+m.properties.BaseName+" as version "+version)
180 return timestampFile
181}
182
Jooyung Han60699b52021-05-26 22:20:42 +0900183// calculates import flags(-I) from deps.
184// When the target is ToT, use ToT of imported interfaces. If not, we use "current" snapshot of
185// imported interfaces.
186func getImportsFromDeps(ctx android.ModuleContext, targetIsToT bool) (importPaths []string, implicits android.Paths) {
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900187 ctx.VisitDirectDeps(func(dep android.Module) {
Jooyung Han60699b52021-05-26 22:20:42 +0900188 switch ctx.OtherModuleDependencyTag(dep) {
189 case importInterfaceDep:
190 iface := dep.(*aidlInterface)
191 if proptools.Bool(iface.properties.Unstable) || targetIsToT {
192 importPaths = append(importPaths, iface.properties.Full_import_paths...)
193 } else {
194 // use "current" snapshot from stable "imported" modules
195 currentDir := filepath.Join(ctx.OtherModuleDir(dep), aidlApiDir, iface.BaseModuleName(), currentVersion)
196 importPaths = append(importPaths, currentDir)
197 // TODO(b/189288369) this should be transitive
198 importPaths = append(importPaths, iface.properties.Include_dirs...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900199 }
Jooyung Han60699b52021-05-26 22:20:42 +0900200 case interfaceDep:
201 iface := dep.(*aidlInterface)
202 importPaths = append(importPaths, iface.properties.Include_dirs...)
203 case importApiDep, apiDep:
204 api := dep.(*aidlApi)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900205 // add imported module's checkapiTimestamps as implicits to make sure that imported apiDump is up-to-date
Jooyung Han60699b52021-05-26 22:20:42 +0900206 implicits = append(implicits, api.checkApiTimestamps.Paths()...)
207 implicits = append(implicits, api.checkHashTimestamps.Paths()...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900208 }
209 })
210 return
211}
212
Jooyung Hanb8a97772021-01-19 01:27:38 +0900213func (m *aidlApi) checkApi(ctx android.ModuleContext, oldDump, newDump apiDump, checkApiLevel string, messageFile android.Path) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900214 newVersion := newDump.dir.Base()
215 timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900216
217 var optionalFlags []string
218 if m.properties.Stability != nil {
219 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
220 }
221
Jooyung Han60699b52021-05-26 22:20:42 +0900222 importPaths, implicits := getImportsFromDeps(ctx, false)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900223 implicits = append(implicits, oldDump.files...)
224 implicits = append(implicits, newDump.files...)
225 implicits = append(implicits, messageFile)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900226
Jeongik Chada36d5a2021-01-20 00:43:34 +0900227 ctx.Build(pctx, android.BuildParams{
228 Rule: aidlCheckApiRule,
229 Implicits: implicits,
230 Output: timestampFile,
231 Args: map[string]string{
232 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900233 "imports": strings.Join(wrap("-I", importPaths, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900234 "old": oldDump.dir.String(),
235 "new": newDump.dir.String(),
236 "messageFile": messageFile.String(),
Jooyung Hanb8a97772021-01-19 01:27:38 +0900237 "checkApiLevel": checkApiLevel,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900238 },
239 })
240 return timestampFile
241}
242
Jooyung Hanb8a97772021-01-19 01:27:38 +0900243func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath {
244 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt")
245 return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile)
246}
Jeongik Chada36d5a2021-01-20 00:43:34 +0900247
Jooyung Hanb8a97772021-01-19 01:27:38 +0900248func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldDump apiDump, newDump apiDump) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900249 // Use different messages depending on whether platform SDK is finalized or not.
250 // In case when it is finalized, we should never allow updating the already frozen API.
251 // If it's not finalized, we let users to update the current version by invoking
252 // `m <name>-update-api`.
253 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt")
254 sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview()
255 if sdkIsFinal {
256 messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt")
257 }
258 formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt")
259 rb := android.NewRuleBuilder(pctx, ctx)
260 rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile)
261 rb.Build("format_message_"+m.properties.BaseName, "")
262
Jooyung Hanb8a97772021-01-19 01:27:38 +0900263 return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900264}
265
266func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath {
267 version := dump.dir.Base()
268 timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp")
269 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt")
270
Jeongik Chada36d5a2021-01-20 00:43:34 +0900271 var implicits android.Paths
272 implicits = append(implicits, dump.files...)
273 implicits = append(implicits, dump.hashFile.Path())
274 implicits = append(implicits, messageFile)
275 ctx.Build(pctx, android.BuildParams{
276 Rule: aidlVerifyHashRule,
277 Implicits: implicits,
278 Output: timestampFile,
279 Args: map[string]string{
280 "apiDir": dump.dir.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900281 "version": versionForHashGen(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900282 "hashFile": dump.hashFile.Path().String(),
283 "messageFile": messageFile.String(),
284 },
285 })
286 return timestampFile
287}
288
Devin Moore15b8ebe2021-05-21 09:36:37 -0700289func (m *aidlApi) checkForDevelopment(ctx android.ModuleContext, latestVersionDump *apiDump, totDump apiDump) android.WritablePath {
290 hasDevPath := android.PathForModuleOut(ctx, "has_development")
291 rb := android.NewRuleBuilder(pctx, ctx)
292 rb.Command().Text("rm -f " + hasDevPath.String())
293 if latestVersionDump != nil {
294 hasDevCommand := rb.Command()
295 hasDevCommand.BuiltTool("aidl").FlagWithArg("--checkapi=", "equal")
296 if m.properties.Stability != nil {
297 hasDevCommand.FlagWithArg("--stability ", *m.properties.Stability)
298 }
299 importPaths, implicits := getImportsFromDeps(ctx, false)
300 hasDevCommand.FlagForEachArg("-I", importPaths).Implicits(implicits)
301 hasDevCommand.
302 Text(latestVersionDump.dir.String()).Implicits(latestVersionDump.files).
303 Text(totDump.dir.String()).Implicits(totDump.files).
304 Text("; echo $? >").Output(hasDevPath)
305 } else {
306 rb.Command().Text("echo 1 >").Output(hasDevPath)
307 }
308 rb.Build("check_for_development", "")
309 return hasDevPath
310}
311
Jeongik Chada36d5a2021-01-20 00:43:34 +0900312func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) {
313 // An API dump is created from source and it is compared against the API dump of the
314 // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in
315 // the AIDL interface is gated by the AIDL API review even before the interface is frozen as
316 // a new version.
317 totApiDump := m.createApiDumpFromSource(ctx)
318 currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion)
319 var currentApiDump apiDump
320 if currentApiDir.Valid() {
321 currentApiDump = apiDump{
322 dir: currentApiDir.Path(),
323 files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil),
324 hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"),
325 }
326 checked := m.checkEquality(ctx, currentApiDump, totApiDump)
327 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
328 } else {
329 // The "current" directory might not exist, in case when the interface is first created.
330 // Instruct user to create one by executing `m <name>-update-api`.
331 rb := android.NewRuleBuilder(pctx, ctx)
332 ifaceName := m.properties.BaseName
333 rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName))
334 rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+
335 `for the interface if it does not need to be versioned`, ifaceName))
336 // This file will never be created. Otherwise, the build will pass simply by running 'm; m'.
337 alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp")
338 rb.Command().Text("false").ImplicitOutput(alwaysChecked)
339 rb.Build("check_current_aidl_api", "")
340 m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked)
341 }
342
343 // Also check that version X is backwards compatible with version X-1.
344 // "current" is checked against the latest version.
345 var dumps []apiDump
346 for _, ver := range m.properties.Versions {
347 apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver)
348 apiDirPath := android.ExistentPathForSource(ctx, apiDir)
349 if apiDirPath.Valid() {
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900350 hashFilePath := filepath.Join(apiDir, ".hash")
351 dump := apiDump{
Jeongik Chada36d5a2021-01-20 00:43:34 +0900352 dir: apiDirPath.Path(),
353 files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900354 hashFile: android.ExistentPathForSource(ctx, hashFilePath),
355 }
356 if !dump.hashFile.Valid() {
Jeongik Chac6442172021-05-25 10:46:56 +0900357 // We should show the source path of hash_gen because aidl_hash_gen cannot be built due to build error.
358 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 +0900359 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",
360 m.properties.BaseName, ver, cmd)
361 }
362 dumps = append(dumps, dump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900363 } else if ctx.Config().AllowMissingDependencies() {
364 ctx.AddMissingDependencies([]string{apiDir})
365 } else {
366 ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir)
367 }
368 }
Jiyong Park72e08932021-05-21 00:18:20 +0900369 var latestVersionDump *apiDump
370 if len(dumps) >= 1 {
371 latestVersionDump = &dumps[len(dumps)-1]
372 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900373 if currentApiDir.Valid() {
374 dumps = append(dumps, currentApiDump)
375 }
376 for i, _ := range dumps {
377 if dumps[i].hashFile.Valid() {
378 checkHashTimestamp := m.checkIntegrity(ctx, dumps[i])
379 m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp)
380 }
381
382 if i == 0 {
383 continue
384 }
385 checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i])
386 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
387 }
388
Devin Moore15b8ebe2021-05-21 09:36:37 -0700389 // Check for active development on the unfrozen version
390 m.hasDevelopment = m.checkForDevelopment(ctx, latestVersionDump, totApiDump)
391
Jeongik Chada36d5a2021-01-20 00:43:34 +0900392 // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api`
Jiyong Park72e08932021-05-21 00:18:20 +0900393 m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion, nil)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900394
395 // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api`
396 nextVersion := m.nextVersion()
Jiyong Park72e08932021-05-21 00:18:20 +0900397 m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion, latestVersionDump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900398}
399
400func (m *aidlApi) AndroidMk() android.AndroidMkData {
401 return android.AndroidMkData{
402 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
403 android.WriteAndroidMkData(w, data)
404 targetName := m.properties.BaseName + "-freeze-api"
405 fmt.Fprintln(w, ".PHONY:", targetName)
406 fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String())
407
408 targetName = m.properties.BaseName + "-update-api"
409 fmt.Fprintln(w, ".PHONY:", targetName)
410 fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String())
411 },
412 }
413}
414
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900415type depTag struct {
416 blueprint.BaseDependencyTag
417 name string
418}
419
420var (
Jooyung Han60699b52021-05-26 22:20:42 +0900421 apiDep = depTag{name: "api"}
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900422 interfaceDep = depTag{name: "interface"}
Jooyung Han60699b52021-05-26 22:20:42 +0900423
424 importApiDep = depTag{name: "imported-api"}
425 importInterfaceDep = depTag{name: "imported-interface"}
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900426)
427
Jeongik Chada36d5a2021-01-20 00:43:34 +0900428func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900429 ctx.AddDependency(ctx.Module(), importApiDep, wrap("", m.properties.ImportsWithoutVersion, aidlApiSuffix)...)
Jooyung Han60699b52021-05-26 22:20:42 +0900430 ctx.AddDependency(ctx.Module(), importInterfaceDep, wrap("", m.properties.ImportsWithoutVersion, aidlInterfaceSuffix)...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900431 ctx.AddDependency(ctx.Module(), interfaceDep, m.properties.BaseName+aidlInterfaceSuffix)
Devin Moore15b8ebe2021-05-21 09:36:37 -0700432 ctx.AddReverseDependency(ctx.Module(), nil, aidlMetadataSingletonName)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900433}
434
435func aidlApiFactory() android.Module {
436 m := &aidlApi{}
437 m.AddProperties(&m.properties)
438 android.InitAndroidModule(m)
439 return m
440}
441
442func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string {
443 apiModule := i.ModuleBase.Name() + aidlApiSuffix
Jeongik Cha52e98022021-01-20 18:37:20 +0900444 srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900445 mctx.CreateModule(aidlApiFactory, &nameProperties{
446 Name: proptools.StringPtr(apiModule),
447 }, &aidlApiProperties{
Jeongik Chaa01447d2021-03-17 17:43:12 +0900448 BaseName: i.ModuleBase.Name(),
449 Srcs: srcs,
450 AidlRoot: aidlRoot,
451 Stability: i.properties.Stability,
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900452 ImportsWithoutVersion: i.properties.ImportsWithoutVersion,
Jeongik Chaa01447d2021-03-17 17:43:12 +0900453 Versions: i.properties.Versions,
454 Dumpapi: i.properties.Dumpapi,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900455 })
456 return apiModule
457}
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900458
459func versionForHashGen(ver string) string {
460 // aidlHashGen uses the version before current version. If it has never been frozen, return 'latest-version'.
461 verInt, _ := strconv.Atoi(ver)
462 if verInt > 1 {
463 return strconv.Itoa(verInt - 1)
464 }
465 return "latest-version"
466}
Jiyong Parke38dab42021-05-20 14:25:34 +0900467
468func init() {
469 android.RegisterSingletonType("aidl-freeze-api", freezeApiSingletonFactory)
470}
471
472func freezeApiSingletonFactory() android.Singleton {
473 return &freezeApiSingleton{}
474}
475
476type freezeApiSingleton struct{}
477
478func (f *freezeApiSingleton) GenerateBuildActions(ctx android.SingletonContext) {
479 var files android.Paths
480 ctx.VisitAllModules(func(module android.Module) {
481 if !module.Enabled() {
482 return
483 }
484 if m, ok := module.(*aidlApi); ok {
485 files = append(files, m.freezeApiTimestamp)
486 }
487 })
488 ctx.Phony("aidl-freeze-api", files...)
489}