blob: f452c8f76e4d234e4b536cda2762916f7a428256 [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
78}
79
80func (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
85func (m *aidlApi) nextVersion() string {
Jeongik Cha52e98022021-01-20 18:37:20 +090086 return nextVersion(m.properties.Versions)
Jeongik Chada36d5a2021-01-20 00:43:34 +090087}
88
89type apiDump struct {
90 dir android.Path
91 files android.Paths
92 hashFile android.OptionalPath
93}
94
95func (m *aidlApi) createApiDumpFromSource(ctx android.ModuleContext) apiDump {
96 srcs, imports := getPaths(ctx, m.properties.Srcs)
97
98 if ctx.Failed() {
99 return apiDump{}
100 }
101
102 var importPaths []string
103 importPaths = append(importPaths, imports...)
104 ctx.VisitDirectDeps(func(dep android.Module) {
105 if importedAidl, ok := dep.(*aidlInterface); ok {
106 importPaths = append(importPaths, importedAidl.properties.Full_import_paths...)
107 }
108 })
109
110 var apiDir android.WritablePath
111 var apiFiles android.WritablePaths
112 var hashFile android.WritablePath
113
114 apiDir = android.PathForModuleOut(ctx, "dump")
115 aidlRoot := android.PathForModuleSrc(ctx, m.properties.AidlRoot)
116 for _, src := range srcs {
117 baseDir := getBaseDir(ctx, src, aidlRoot)
118 relPath, _ := filepath.Rel(baseDir, src.String())
119 outFile := android.PathForModuleOut(ctx, "dump", relPath)
120 apiFiles = append(apiFiles, outFile)
121 }
122 hashFile = android.PathForModuleOut(ctx, "dump", ".hash")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900123
124 var optionalFlags []string
125 if m.properties.Stability != nil {
126 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
127 }
Jooyung Han252657e2021-02-27 02:51:39 +0900128 if proptools.Bool(m.properties.Dumpapi.No_license) {
129 optionalFlags = append(optionalFlags, "--no_license")
130 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900131
132 ctx.Build(pctx, android.BuildParams{
133 Rule: aidlDumpApiRule,
134 Outputs: append(apiFiles, hashFile),
135 Inputs: srcs,
136 Args: map[string]string{
137 "optionalFlags": strings.Join(optionalFlags, " "),
138 "imports": strings.Join(wrap("-I", importPaths, ""), " "),
139 "outDir": apiDir.String(),
140 "hashFile": hashFile.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900141 "latestVersion": versionForHashGen(nextVersion(m.properties.Versions)),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900142 },
143 })
144 return apiDump{apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)}
145}
146
Jiyong Park72e08932021-05-21 00:18:20 +0900147func (m *aidlApi) makeApiDumpAsVersion(ctx android.ModuleContext, dump apiDump, version string, latestVersionDump *apiDump) android.WritablePath {
148 creatingNewVersion := version != currentVersion
149 moduleDir := android.PathForModuleSrc(ctx).String()
150 targetDir := filepath.Join(moduleDir, m.apiDir(), version)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900151 rb := android.NewRuleBuilder(pctx, ctx)
Jiyong Park72e08932021-05-21 00:18:20 +0900152
153 if creatingNewVersion {
154 // We are asked to create a new version. But before doing that, check if the given
155 // dump is the same as the latest version. If so, don't create a new version,
156 // otherwise we will be unnecessarily creating many versions. `newVersionNeededFile`
157 // is created when the equality check fails.
158 newVersionNeededFile := android.PathForModuleOut(ctx, "updateapi_"+version+".needed")
159 rb.Command().Text("rm -f " + newVersionNeededFile.String())
160
161 if latestVersionDump != nil {
162 equalityCheckCommand := rb.Command()
163 equalityCheckCommand.BuiltTool("aidl").
164 FlagWithArg("--checkapi=", "equal")
165 if m.properties.Stability != nil {
166 equalityCheckCommand.FlagWithArg("--stability ", *m.properties.Stability)
167 }
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900168 importPaths, implicits := m.getImportsForCheckApi(ctx)
169 equalityCheckCommand.FlagForEachArg("-I", importPaths).Implicits(implicits)
Jiyong Park72e08932021-05-21 00:18:20 +0900170 equalityCheckCommand.
171 Text(latestVersionDump.dir.String()).Implicits(latestVersionDump.files).
172 Text(dump.dir.String()).Implicits(dump.files).
173 Text("&> /dev/null")
174 equalityCheckCommand.
175 Text("|| touch").
176 Text(newVersionNeededFile.String())
177 } else {
178 // If there is no latest version (i.e. we are creating the initial version)
179 // create the new version unconditionally
180 rb.Command().Text("touch").Text(newVersionNeededFile.String())
181 }
182
183 // Copy the given dump to the target directory only when the equality check failed
184 // (i.e. `newVersionNeededFile` exists).
185 rb.Command().
186 Text("if [ -f " + newVersionNeededFile.String() + " ]; then").
187 Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files).
188 Text("; fi")
189
190 // Also modify Android.bp file to add the new version to the 'versions' property.
191 rb.Command().
192 Text("if [ -f " + newVersionNeededFile.String() + " ]; then").
193 BuiltTool("bpmodify").
Jeongik Chada36d5a2021-01-20 00:43:34 +0900194 Text("-w -m " + m.properties.BaseName).
195 Text("-parameter versions -a " + version).
Jiyong Park72e08932021-05-21 00:18:20 +0900196 Text(android.PathForModuleSrc(ctx, "Android.bp").String()).
197 Text("; fi")
198
Jeongik Chada36d5a2021-01-20 00:43:34 +0900199 } else {
Jiyong Park72e08932021-05-21 00:18:20 +0900200 // We are updating the current version. Don't copy .hash to the current dump
201 rb.Command().Text("mkdir -p " + targetDir)
202 rb.Command().Text("rm -rf " + targetDir + "/*")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900203 rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files)
204 }
Jiyong Park72e08932021-05-21 00:18:20 +0900205
206 timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900207 rb.Command().Text("touch").Output(timestampFile)
208
209 rb.Build("dump_aidl_api"+m.properties.BaseName+"_"+version,
210 "Making AIDL API of "+m.properties.BaseName+" as version "+version)
211 return timestampFile
212}
213
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900214// calculates "import" flags(-I) for --checkapi command. The list of imports differs from --dumpapi
215// or --compile because --checkapi works with "apiDump"s.
216// For example, local_include_dirs is not provided because apiDump has all .aidl files.
217func (m *aidlApi) getImportsForCheckApi(ctx android.ModuleContext) (importPaths []string, implicits android.Paths) {
218 ctx.VisitDirectDeps(func(dep android.Module) {
219 if importedAidl, ok := dep.(*aidlInterface); ok {
220 switch ctx.OtherModuleDependencyTag(dep) {
221 case importDep:
222 if proptools.Bool(importedAidl.properties.Unstable) {
223 importPaths = append(importPaths, importedAidl.properties.Full_import_paths...)
224 } else {
225 // use "current" snapshot from stable "imported" modules
226 currentDir := filepath.Join(ctx.OtherModuleDir(dep), aidlApiDir, importedAidl.BaseModuleName(), currentVersion)
227 importPaths = append(importPaths, currentDir)
228 }
229 case interfaceDep:
230 importPaths = append(importPaths, importedAidl.properties.Include_dirs...)
231 }
232 } else if importedApi, ok := dep.(*aidlApi); ok {
233 // add imported module's checkapiTimestamps as implicits to make sure that imported apiDump is up-to-date
234 implicits = append(implicits, importedApi.checkApiTimestamps.Paths()...)
235 }
236 })
237 return
238}
239
Jooyung Hanb8a97772021-01-19 01:27:38 +0900240func (m *aidlApi) checkApi(ctx android.ModuleContext, oldDump, newDump apiDump, checkApiLevel string, messageFile android.Path) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900241 newVersion := newDump.dir.Base()
242 timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900243
244 var optionalFlags []string
245 if m.properties.Stability != nil {
246 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
247 }
248
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900249 importPaths, implicits := m.getImportsForCheckApi(ctx)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900250 implicits = append(implicits, oldDump.files...)
251 implicits = append(implicits, newDump.files...)
252 implicits = append(implicits, messageFile)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900253
Jeongik Chada36d5a2021-01-20 00:43:34 +0900254 ctx.Build(pctx, android.BuildParams{
255 Rule: aidlCheckApiRule,
256 Implicits: implicits,
257 Output: timestampFile,
258 Args: map[string]string{
259 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900260 "imports": strings.Join(wrap("-I", importPaths, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900261 "old": oldDump.dir.String(),
262 "new": newDump.dir.String(),
263 "messageFile": messageFile.String(),
Jooyung Hanb8a97772021-01-19 01:27:38 +0900264 "checkApiLevel": checkApiLevel,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900265 },
266 })
267 return timestampFile
268}
269
Jooyung Hanb8a97772021-01-19 01:27:38 +0900270func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath {
271 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt")
272 return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile)
273}
Jeongik Chada36d5a2021-01-20 00:43:34 +0900274
Jooyung Hanb8a97772021-01-19 01:27:38 +0900275func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldDump apiDump, newDump apiDump) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900276 // Use different messages depending on whether platform SDK is finalized or not.
277 // In case when it is finalized, we should never allow updating the already frozen API.
278 // If it's not finalized, we let users to update the current version by invoking
279 // `m <name>-update-api`.
280 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt")
281 sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview()
282 if sdkIsFinal {
283 messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt")
284 }
285 formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt")
286 rb := android.NewRuleBuilder(pctx, ctx)
287 rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile)
288 rb.Build("format_message_"+m.properties.BaseName, "")
289
Jooyung Hanb8a97772021-01-19 01:27:38 +0900290 return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900291}
292
293func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath {
294 version := dump.dir.Base()
295 timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp")
296 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt")
297
Jeongik Chada36d5a2021-01-20 00:43:34 +0900298 var implicits android.Paths
299 implicits = append(implicits, dump.files...)
300 implicits = append(implicits, dump.hashFile.Path())
301 implicits = append(implicits, messageFile)
302 ctx.Build(pctx, android.BuildParams{
303 Rule: aidlVerifyHashRule,
304 Implicits: implicits,
305 Output: timestampFile,
306 Args: map[string]string{
307 "apiDir": dump.dir.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900308 "version": versionForHashGen(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900309 "hashFile": dump.hashFile.Path().String(),
310 "messageFile": messageFile.String(),
311 },
312 })
313 return timestampFile
314}
315
316func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) {
317 // An API dump is created from source and it is compared against the API dump of the
318 // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in
319 // the AIDL interface is gated by the AIDL API review even before the interface is frozen as
320 // a new version.
321 totApiDump := m.createApiDumpFromSource(ctx)
322 currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion)
323 var currentApiDump apiDump
324 if currentApiDir.Valid() {
325 currentApiDump = apiDump{
326 dir: currentApiDir.Path(),
327 files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil),
328 hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"),
329 }
330 checked := m.checkEquality(ctx, currentApiDump, totApiDump)
331 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
332 } else {
333 // The "current" directory might not exist, in case when the interface is first created.
334 // Instruct user to create one by executing `m <name>-update-api`.
335 rb := android.NewRuleBuilder(pctx, ctx)
336 ifaceName := m.properties.BaseName
337 rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName))
338 rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+
339 `for the interface if it does not need to be versioned`, ifaceName))
340 // This file will never be created. Otherwise, the build will pass simply by running 'm; m'.
341 alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp")
342 rb.Command().Text("false").ImplicitOutput(alwaysChecked)
343 rb.Build("check_current_aidl_api", "")
344 m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked)
345 }
346
347 // Also check that version X is backwards compatible with version X-1.
348 // "current" is checked against the latest version.
349 var dumps []apiDump
350 for _, ver := range m.properties.Versions {
351 apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver)
352 apiDirPath := android.ExistentPathForSource(ctx, apiDir)
353 if apiDirPath.Valid() {
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900354 hashFilePath := filepath.Join(apiDir, ".hash")
355 dump := apiDump{
Jeongik Chada36d5a2021-01-20 00:43:34 +0900356 dir: apiDirPath.Path(),
357 files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900358 hashFile: android.ExistentPathForSource(ctx, hashFilePath),
359 }
360 if !dump.hashFile.Valid() {
Jeongik Chac6442172021-05-25 10:46:56 +0900361 // We should show the source path of hash_gen because aidl_hash_gen cannot be built due to build error.
362 cmd := fmt.Sprintf(`(croot && system/tools/aidl/build/hash_gen.sh %s %s %s)`, apiDir, versionForHashGen(ver), hashFilePath)
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900363 ctx.ModuleErrorf("A frozen aidl_interface must have '.hash' file, but %s-V%s doesn't have it. Use the command below to generate hash.\n%s\n",
364 m.properties.BaseName, ver, cmd)
365 }
366 dumps = append(dumps, dump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900367 } else if ctx.Config().AllowMissingDependencies() {
368 ctx.AddMissingDependencies([]string{apiDir})
369 } else {
370 ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir)
371 }
372 }
Jiyong Park72e08932021-05-21 00:18:20 +0900373 var latestVersionDump *apiDump
374 if len(dumps) >= 1 {
375 latestVersionDump = &dumps[len(dumps)-1]
376 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900377 if currentApiDir.Valid() {
378 dumps = append(dumps, currentApiDump)
379 }
380 for i, _ := range dumps {
381 if dumps[i].hashFile.Valid() {
382 checkHashTimestamp := m.checkIntegrity(ctx, dumps[i])
383 m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp)
384 }
385
386 if i == 0 {
387 continue
388 }
389 checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i])
390 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
391 }
392
393 // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api`
Jiyong Park72e08932021-05-21 00:18:20 +0900394 m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion, nil)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900395
396 // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api`
397 nextVersion := m.nextVersion()
Jiyong Park72e08932021-05-21 00:18:20 +0900398 m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion, latestVersionDump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900399}
400
401func (m *aidlApi) AndroidMk() android.AndroidMkData {
402 return android.AndroidMkData{
403 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
404 android.WriteAndroidMkData(w, data)
405 targetName := m.properties.BaseName + "-freeze-api"
406 fmt.Fprintln(w, ".PHONY:", targetName)
407 fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String())
408
409 targetName = m.properties.BaseName + "-update-api"
410 fmt.Fprintln(w, ".PHONY:", targetName)
411 fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String())
412 },
413 }
414}
415
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900416type depTag struct {
417 blueprint.BaseDependencyTag
418 name string
419}
420
421var (
422 importDep = depTag{name: "imported-interface"}
423 interfaceDep = depTag{name: "interface"}
424 importApiDep = depTag{name: "imported-api"}
425)
426
Jeongik Chada36d5a2021-01-20 00:43:34 +0900427func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) {
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900428 ctx.AddDependency(ctx.Module(), importApiDep, wrap("", m.properties.ImportsWithoutVersion, aidlApiSuffix)...)
429 ctx.AddDependency(ctx.Module(), importDep, wrap("", m.properties.ImportsWithoutVersion, aidlInterfaceSuffix)...)
430 ctx.AddDependency(ctx.Module(), interfaceDep, m.properties.BaseName+aidlInterfaceSuffix)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900431}
432
433func aidlApiFactory() android.Module {
434 m := &aidlApi{}
435 m.AddProperties(&m.properties)
436 android.InitAndroidModule(m)
437 return m
438}
439
440func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string {
441 apiModule := i.ModuleBase.Name() + aidlApiSuffix
Jeongik Cha52e98022021-01-20 18:37:20 +0900442 srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900443 mctx.CreateModule(aidlApiFactory, &nameProperties{
444 Name: proptools.StringPtr(apiModule),
445 }, &aidlApiProperties{
Jeongik Chaa01447d2021-03-17 17:43:12 +0900446 BaseName: i.ModuleBase.Name(),
447 Srcs: srcs,
448 AidlRoot: aidlRoot,
449 Stability: i.properties.Stability,
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900450 ImportsWithoutVersion: i.properties.ImportsWithoutVersion,
Jeongik Chaa01447d2021-03-17 17:43:12 +0900451 Versions: i.properties.Versions,
452 Dumpapi: i.properties.Dumpapi,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900453 })
454 return apiModule
455}
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900456
457func versionForHashGen(ver string) string {
458 // aidlHashGen uses the version before current version. If it has never been frozen, return 'latest-version'.
459 verInt, _ := strconv.Atoi(ver)
460 if verInt > 1 {
461 return strconv.Itoa(verInt - 1)
462 }
463 return "latest-version"
464}
Jiyong Parke38dab42021-05-20 14:25:34 +0900465
466func init() {
467 android.RegisterSingletonType("aidl-freeze-api", freezeApiSingletonFactory)
468}
469
470func freezeApiSingletonFactory() android.Singleton {
471 return &freezeApiSingleton{}
472}
473
474type freezeApiSingleton struct{}
475
476func (f *freezeApiSingleton) GenerateBuildActions(ctx android.SingletonContext) {
477 var files android.Paths
478 ctx.VisitAllModules(func(module android.Module) {
479 if !module.Enabled() {
480 return
481 }
482 if m, ok := module.(*aidlApi); ok {
483 files = append(files, m.freezeApiTimestamp)
484 }
485 })
486 ctx.Phony("aidl-freeze-api", files...)
487}