blob: 62d2541a264fac3f7c2e26aefaf3076186f9a91d [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{
Steven Morelandfc2ddbf2021-07-29 16:52:34 -070046 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 Chada36d5a2021-01-20 00:43:34 +090047 `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 {
Jooyung Han29e54b92021-11-12 03:25:29 +090053 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 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 {
Jooyung Handf94f0f2021-06-07 18:57:10 +090093 version string
Jeongik Chada36d5a2021-01-20 00:43:34 +090094 dir android.Path
95 files android.Paths
96 hashFile android.OptionalPath
97}
98
Jooyung Handf94f0f2021-06-07 18:57:10 +090099func (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 Chada36d5a2021-01-20 00:43:34 +0900104func (m *aidlApi) createApiDumpFromSource(ctx android.ModuleContext) apiDump {
Jooyung Han60699b52021-05-26 22:20:42 +0900105 srcs, imports := getPaths(ctx, m.properties.Srcs, m.properties.AidlRoot)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900106
107 if ctx.Failed() {
108 return apiDump{}
109 }
110
Jooyung Handf94f0f2021-06-07 18:57:10 +0900111 // dumpapi uses imports for ToT("") version
112 deps := getDeps(ctx, m.getImports(ctx, m.nextVersion()))
Jooyung Han42c5f292021-06-04 19:49:34 +0900113 imports = append(imports, deps.imports...)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900114
115 var apiDir android.WritablePath
116 var apiFiles android.WritablePaths
117 var hashFile android.WritablePath
118
119 apiDir = android.PathForModuleOut(ctx, "dump")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900120 for _, src := range srcs {
Jooyung Hanaf45dbb2021-06-03 13:56:24 +0900121 outFile := android.PathForModuleOut(ctx, "dump", src.Rel())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900122 apiFiles = append(apiFiles, outFile)
123 }
124 hashFile = android.PathForModuleOut(ctx, "dump", ".hash")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900125
126 var optionalFlags []string
127 if m.properties.Stability != nil {
128 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
129 }
Jooyung Han252657e2021-02-27 02:51:39 +0900130 if proptools.Bool(m.properties.Dumpapi.No_license) {
131 optionalFlags = append(optionalFlags, "--no_license")
132 }
Jooyung Han42c5f292021-06-04 19:49:34 +0900133 optionalFlags = append(optionalFlags, wrap("-p", deps.preprocessed.Strings(), "")...)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900134
Jooyung Handf94f0f2021-06-07 18:57:10 +0900135 version := nextVersion(m.properties.Versions)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900136 ctx.Build(pctx, android.BuildParams{
Jooyung Han42c5f292021-06-04 19:49:34 +0900137 Rule: aidlDumpApiRule,
138 Outputs: append(apiFiles, hashFile),
139 Inputs: srcs,
140 Implicits: deps.preprocessed,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900141 Args: map[string]string{
142 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han60699b52021-05-26 22:20:42 +0900143 "imports": strings.Join(wrap("-I", imports, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900144 "outDir": apiDir.String(),
145 "hashFile": hashFile.String(),
Jooyung Handf94f0f2021-06-07 18:57:10 +0900146 "latestVersion": versionForHashGen(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900147 },
148 })
Jooyung Handf94f0f2021-06-07 18:57:10 +0900149 return apiDump{version, apiDir, apiFiles.Paths(), android.OptionalPathForPath(hashFile)}
Jeongik Chada36d5a2021-01-20 00:43:34 +0900150}
151
Jiyong Park72e08932021-05-21 00:18:20 +0900152func (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 Chada36d5a2021-01-20 00:43:34 +0900156 rb := android.NewRuleBuilder(pctx, ctx)
Jiyong Park72e08932021-05-21 00:18:20 +0900157
Steven Moreland3cdab5e2022-01-04 19:25:19 +0000158 var actionWord string
Jiyong Park72e08932021-05-21 00:18:20 +0900159 if creatingNewVersion {
Steven Moreland3cdab5e2022-01-04 19:25:19 +0000160 actionWord = "Making"
Jiyong Park72e08932021-05-21 00:18:20 +0900161 // We are asked to create a new version. But before doing that, check if the given
162 // dump is the same as the latest version. If so, don't create a new version,
Devin Moore15b8ebe2021-05-21 09:36:37 -0700163 // otherwise we will be unnecessarily creating many versions.
Jiyong Park72e08932021-05-21 00:18:20 +0900164 // Copy the given dump to the target directory only when the equality check failed
Devin Moore15b8ebe2021-05-21 09:36:37 -0700165 // (i.e. `has_development` file contains "1").
Jiyong Park72e08932021-05-21 00:18:20 +0900166 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700167 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jooyung Handf94f0f2021-06-07 18:57:10 +0900168 Text("mkdir -p " + targetDir + " && ").
Jiyong Park72e08932021-05-21 00:18:20 +0900169 Text("cp -rf " + dump.dir.String() + "/. " + targetDir).Implicits(dump.files).
170 Text("; fi")
171
172 // Also modify Android.bp file to add the new version to the 'versions' property.
173 rb.Command().
Devin Moore15b8ebe2021-05-21 09:36:37 -0700174 Text("if [ \"$(cat ").Input(m.hasDevelopment).Text(")\" = \"1\" ]; then").
Jiyong Park72e08932021-05-21 00:18:20 +0900175 BuiltTool("bpmodify").
Jeongik Chada36d5a2021-01-20 00:43:34 +0900176 Text("-w -m " + m.properties.BaseName).
177 Text("-parameter versions -a " + version).
Jiyong Park72e08932021-05-21 00:18:20 +0900178 Text(android.PathForModuleSrc(ctx, "Android.bp").String()).
179 Text("; fi")
180
Jeongik Chada36d5a2021-01-20 00:43:34 +0900181 } else {
Steven Moreland3cdab5e2022-01-04 19:25:19 +0000182 actionWord = "Updating"
Jiyong Park72e08932021-05-21 00:18:20 +0900183 // We are updating the current version. Don't copy .hash to the current dump
184 rb.Command().Text("mkdir -p " + targetDir)
185 rb.Command().Text("rm -rf " + targetDir + "/*")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900186 rb.Command().Text("cp -rf " + dump.dir.String() + "/* " + targetDir).Implicits(dump.files)
187 }
Jiyong Park72e08932021-05-21 00:18:20 +0900188
189 timestampFile := android.PathForModuleOut(ctx, "updateapi_"+version+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900190 rb.Command().Text("touch").Output(timestampFile)
191
Steven Moreland3cdab5e2022-01-04 19:25:19 +0000192 rb.Build("dump_aidl_api_"+m.properties.BaseName+"_"+version, actionWord+" AIDL API dump version "+version+" for "+m.properties.BaseName+" (see "+targetDir+")")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900193 return timestampFile
194}
195
Jooyung Han42c5f292021-06-04 19:49:34 +0900196type deps struct {
197 preprocessed android.Paths
198 implicits android.Paths
199 imports []string
200}
201
Jooyung Han60699b52021-05-26 22:20:42 +0900202// calculates import flags(-I) from deps.
203// When the target is ToT, use ToT of imported interfaces. If not, we use "current" snapshot of
204// imported interfaces.
Jooyung Handf94f0f2021-06-07 18:57:10 +0900205func getDeps(ctx android.ModuleContext, versionedImports map[string]string) deps {
Jooyung Han42c5f292021-06-04 19:49:34 +0900206 var deps deps
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900207 ctx.VisitDirectDeps(func(dep android.Module) {
Jooyung Han29e54b92021-11-12 03:25:29 +0900208 switch ctx.OtherModuleDependencyTag(dep).(type) {
209 case importInterfaceDepTag:
Jooyung Han60699b52021-05-26 22:20:42 +0900210 iface := dep.(*aidlInterface)
Jooyung Handf94f0f2021-06-07 18:57:10 +0900211 if version, ok := versionedImports[iface.BaseModuleName()]; ok {
212 if iface.preprocessed[version] == nil {
213 ctx.ModuleErrorf("can't import %v's preprocessed(version=%v)", iface.BaseModuleName(), version)
214 }
215 deps.preprocessed = append(deps.preprocessed, iface.preprocessed[version])
216 }
Jooyung Han29e54b92021-11-12 03:25:29 +0900217 case interfaceDepTag:
Jooyung Han60699b52021-05-26 22:20:42 +0900218 iface := dep.(*aidlInterface)
Jooyung Han42c5f292021-06-04 19:49:34 +0900219 deps.imports = append(deps.imports, iface.properties.Include_dirs...)
Jooyung Han29e54b92021-11-12 03:25:29 +0900220 case apiDepTag:
Jooyung Han60699b52021-05-26 22:20:42 +0900221 api := dep.(*aidlApi)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900222 // add imported module's checkapiTimestamps as implicits to make sure that imported apiDump is up-to-date
Jooyung Han42c5f292021-06-04 19:49:34 +0900223 deps.implicits = append(deps.implicits, api.checkApiTimestamps.Paths()...)
224 deps.implicits = append(deps.implicits, api.checkHashTimestamps.Paths()...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900225 }
226 })
Jooyung Han42c5f292021-06-04 19:49:34 +0900227 return deps
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900228}
229
Jooyung Hanb8a97772021-01-19 01:27:38 +0900230func (m *aidlApi) checkApi(ctx android.ModuleContext, oldDump, newDump apiDump, checkApiLevel string, messageFile android.Path) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900231 newVersion := newDump.dir.Base()
232 timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
Jeongik Chada36d5a2021-01-20 00:43:34 +0900233
Jooyung Handf94f0f2021-06-07 18:57:10 +0900234 // --checkapi(old,new) should use imports for "new"
235 deps := getDeps(ctx, m.getImports(ctx, newDump.version))
Jooyung Han42c5f292021-06-04 19:49:34 +0900236 var implicits android.Paths
237 implicits = append(implicits, deps.implicits...)
238 implicits = append(implicits, deps.preprocessed...)
239 implicits = append(implicits, oldDump.files...)
240 implicits = append(implicits, newDump.files...)
241 implicits = append(implicits, messageFile)
242
Jeongik Chada36d5a2021-01-20 00:43:34 +0900243 var optionalFlags []string
244 if m.properties.Stability != nil {
245 optionalFlags = append(optionalFlags, "--stability", *m.properties.Stability)
246 }
Jooyung Han42c5f292021-06-04 19:49:34 +0900247 optionalFlags = append(optionalFlags, wrap("-p", deps.preprocessed.Strings(), "")...)
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900248
Jeongik Chada36d5a2021-01-20 00:43:34 +0900249 ctx.Build(pctx, android.BuildParams{
250 Rule: aidlCheckApiRule,
251 Implicits: implicits,
252 Output: timestampFile,
253 Args: map[string]string{
254 "optionalFlags": strings.Join(optionalFlags, " "),
Jooyung Han42c5f292021-06-04 19:49:34 +0900255 "imports": strings.Join(wrap("-I", deps.imports, ""), " "),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900256 "old": oldDump.dir.String(),
257 "new": newDump.dir.String(),
258 "messageFile": messageFile.String(),
Jooyung Hanb8a97772021-01-19 01:27:38 +0900259 "checkApiLevel": checkApiLevel,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900260 },
261 })
262 return timestampFile
263}
264
Jooyung Hanb8a97772021-01-19 01:27:38 +0900265func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldDump, newDump apiDump) android.WritablePath {
266 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt")
267 return m.checkApi(ctx, oldDump, newDump, "compatible", messageFile)
268}
Jeongik Chada36d5a2021-01-20 00:43:34 +0900269
Jooyung Hanb8a97772021-01-19 01:27:38 +0900270func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldDump apiDump, newDump apiDump) android.WritablePath {
Jeongik Chada36d5a2021-01-20 00:43:34 +0900271 // Use different messages depending on whether platform SDK is finalized or not.
272 // In case when it is finalized, we should never allow updating the already frozen API.
273 // If it's not finalized, we let users to update the current version by invoking
274 // `m <name>-update-api`.
275 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt")
276 sdkIsFinal := !ctx.Config().DefaultAppTargetSdk(ctx).IsPreview()
277 if sdkIsFinal {
278 messageFile = android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality_release.txt")
279 }
280 formattedMessageFile := android.PathForModuleOut(ctx, "message_check_equality.txt")
281 rb := android.NewRuleBuilder(pctx, ctx)
282 rb.Command().Text("sed").Flag(" s/%s/" + m.properties.BaseName + "/g ").Input(messageFile).Text(" > ").Output(formattedMessageFile)
283 rb.Build("format_message_"+m.properties.BaseName, "")
284
Jooyung Hanb8a97772021-01-19 01:27:38 +0900285 return m.checkApi(ctx, oldDump, newDump, "equal", formattedMessageFile)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900286}
287
288func (m *aidlApi) checkIntegrity(ctx android.ModuleContext, dump apiDump) android.WritablePath {
289 version := dump.dir.Base()
290 timestampFile := android.PathForModuleOut(ctx, "checkhash_"+version+".timestamp")
291 messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_integrity.txt")
292
Jeongik Chada36d5a2021-01-20 00:43:34 +0900293 var implicits android.Paths
294 implicits = append(implicits, dump.files...)
295 implicits = append(implicits, dump.hashFile.Path())
296 implicits = append(implicits, messageFile)
297 ctx.Build(pctx, android.BuildParams{
298 Rule: aidlVerifyHashRule,
299 Implicits: implicits,
300 Output: timestampFile,
301 Args: map[string]string{
302 "apiDir": dump.dir.String(),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900303 "version": versionForHashGen(version),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900304 "hashFile": dump.hashFile.Path().String(),
305 "messageFile": messageFile.String(),
306 },
307 })
308 return timestampFile
309}
310
Devin Moore15b8ebe2021-05-21 09:36:37 -0700311func (m *aidlApi) checkForDevelopment(ctx android.ModuleContext, latestVersionDump *apiDump, totDump apiDump) android.WritablePath {
312 hasDevPath := android.PathForModuleOut(ctx, "has_development")
313 rb := android.NewRuleBuilder(pctx, ctx)
314 rb.Command().Text("rm -f " + hasDevPath.String())
315 if latestVersionDump != nil {
316 hasDevCommand := rb.Command()
317 hasDevCommand.BuiltTool("aidl").FlagWithArg("--checkapi=", "equal")
318 if m.properties.Stability != nil {
319 hasDevCommand.FlagWithArg("--stability ", *m.properties.Stability)
320 }
Jooyung Handf94f0f2021-06-07 18:57:10 +0900321 // checkapi(latest, tot) should use imports for nextVersion(=tot)
322 deps := getDeps(ctx, m.getImports(ctx, m.nextVersion()))
Devin Moore15b8ebe2021-05-21 09:36:37 -0700323 hasDevCommand.
Jooyung Han42c5f292021-06-04 19:49:34 +0900324 FlagForEachArg("-I", deps.imports).Implicits(deps.implicits).
325 FlagForEachInput("-p", deps.preprocessed).
Devin Moore15b8ebe2021-05-21 09:36:37 -0700326 Text(latestVersionDump.dir.String()).Implicits(latestVersionDump.files).
327 Text(totDump.dir.String()).Implicits(totDump.files).
Devin Moore45e93ca2021-06-04 13:57:38 -0700328 Text("2> /dev/null; echo $? >").Output(hasDevPath)
Devin Moore15b8ebe2021-05-21 09:36:37 -0700329 } else {
330 rb.Command().Text("echo 1 >").Output(hasDevPath)
331 }
332 rb.Build("check_for_development", "")
333 return hasDevPath
334}
335
Jeongik Chada36d5a2021-01-20 00:43:34 +0900336func (m *aidlApi) GenerateAndroidBuildActions(ctx android.ModuleContext) {
337 // An API dump is created from source and it is compared against the API dump of the
338 // 'current' (yet-to-be-finalized) version. By checking this we enforce that any change in
339 // the AIDL interface is gated by the AIDL API review even before the interface is frozen as
340 // a new version.
341 totApiDump := m.createApiDumpFromSource(ctx)
342 currentApiDir := android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion)
343 var currentApiDump apiDump
344 if currentApiDir.Valid() {
345 currentApiDump = apiDump{
Jooyung Handf94f0f2021-06-07 18:57:10 +0900346 version: nextVersion(m.properties.Versions),
Jeongik Chada36d5a2021-01-20 00:43:34 +0900347 dir: currentApiDir.Path(),
348 files: ctx.Glob(filepath.Join(currentApiDir.Path().String(), "**/*.aidl"), nil),
349 hashFile: android.ExistentPathForSource(ctx, ctx.ModuleDir(), m.apiDir(), currentVersion, ".hash"),
350 }
351 checked := m.checkEquality(ctx, currentApiDump, totApiDump)
352 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
353 } else {
354 // The "current" directory might not exist, in case when the interface is first created.
355 // Instruct user to create one by executing `m <name>-update-api`.
356 rb := android.NewRuleBuilder(pctx, ctx)
357 ifaceName := m.properties.BaseName
358 rb.Command().Text(fmt.Sprintf(`echo "API dump for the current version of AIDL interface %s does not exist."`, ifaceName))
359 rb.Command().Text(fmt.Sprintf(`echo Run "m %s-update-api", or add "unstable: true" to the build rule `+
360 `for the interface if it does not need to be versioned`, ifaceName))
361 // This file will never be created. Otherwise, the build will pass simply by running 'm; m'.
362 alwaysChecked := android.PathForModuleOut(ctx, "checkapi_current.timestamp")
363 rb.Command().Text("false").ImplicitOutput(alwaysChecked)
364 rb.Build("check_current_aidl_api", "")
365 m.checkApiTimestamps = append(m.checkApiTimestamps, alwaysChecked)
366 }
367
368 // Also check that version X is backwards compatible with version X-1.
369 // "current" is checked against the latest version.
370 var dumps []apiDump
371 for _, ver := range m.properties.Versions {
372 apiDir := filepath.Join(ctx.ModuleDir(), m.apiDir(), ver)
373 apiDirPath := android.ExistentPathForSource(ctx, apiDir)
374 if apiDirPath.Valid() {
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900375 hashFilePath := filepath.Join(apiDir, ".hash")
376 dump := apiDump{
Jooyung Handf94f0f2021-06-07 18:57:10 +0900377 version: ver,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900378 dir: apiDirPath.Path(),
379 files: ctx.Glob(filepath.Join(apiDirPath.String(), "**/*.aidl"), nil),
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900380 hashFile: android.ExistentPathForSource(ctx, hashFilePath),
381 }
382 if !dump.hashFile.Valid() {
Jeongik Chac6442172021-05-25 10:46:56 +0900383 // We should show the source path of hash_gen because aidl_hash_gen cannot be built due to build error.
384 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 +0900385 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",
386 m.properties.BaseName, ver, cmd)
387 }
388 dumps = append(dumps, dump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900389 } else if ctx.Config().AllowMissingDependencies() {
390 ctx.AddMissingDependencies([]string{apiDir})
391 } else {
392 ctx.ModuleErrorf("API version %s path %s does not exist", ver, apiDir)
393 }
394 }
Jiyong Park72e08932021-05-21 00:18:20 +0900395 var latestVersionDump *apiDump
396 if len(dumps) >= 1 {
397 latestVersionDump = &dumps[len(dumps)-1]
398 }
Jeongik Chada36d5a2021-01-20 00:43:34 +0900399 if currentApiDir.Valid() {
400 dumps = append(dumps, currentApiDump)
401 }
402 for i, _ := range dumps {
403 if dumps[i].hashFile.Valid() {
404 checkHashTimestamp := m.checkIntegrity(ctx, dumps[i])
405 m.checkHashTimestamps = append(m.checkHashTimestamps, checkHashTimestamp)
406 }
407
408 if i == 0 {
409 continue
410 }
411 checked := m.checkCompatibility(ctx, dumps[i-1], dumps[i])
412 m.checkApiTimestamps = append(m.checkApiTimestamps, checked)
413 }
414
Devin Moore15b8ebe2021-05-21 09:36:37 -0700415 // Check for active development on the unfrozen version
416 m.hasDevelopment = m.checkForDevelopment(ctx, latestVersionDump, totApiDump)
417
Jeongik Chada36d5a2021-01-20 00:43:34 +0900418 // API dump from source is updated to the 'current' version. Triggered by `m <name>-update-api`
Jiyong Park72e08932021-05-21 00:18:20 +0900419 m.updateApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, currentVersion, nil)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900420
421 // API dump from source is frozen as the next stable version. Triggered by `m <name>-freeze-api`
422 nextVersion := m.nextVersion()
Jiyong Park72e08932021-05-21 00:18:20 +0900423 m.freezeApiTimestamp = m.makeApiDumpAsVersion(ctx, totApiDump, nextVersion, latestVersionDump)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900424}
425
426func (m *aidlApi) AndroidMk() android.AndroidMkData {
427 return android.AndroidMkData{
428 Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
429 android.WriteAndroidMkData(w, data)
430 targetName := m.properties.BaseName + "-freeze-api"
431 fmt.Fprintln(w, ".PHONY:", targetName)
432 fmt.Fprintln(w, targetName+":", m.freezeApiTimestamp.String())
433
434 targetName = m.properties.BaseName + "-update-api"
435 fmt.Fprintln(w, ".PHONY:", targetName)
436 fmt.Fprintln(w, targetName+":", m.updateApiTimestamp.String())
437 },
438 }
439}
440
441func (m *aidlApi) DepsMutator(ctx android.BottomUpMutatorContext) {
Devin Moore15b8ebe2021-05-21 09:36:37 -0700442 ctx.AddReverseDependency(ctx.Module(), nil, aidlMetadataSingletonName)
Jeongik Chada36d5a2021-01-20 00:43:34 +0900443}
444
445func aidlApiFactory() android.Module {
446 m := &aidlApi{}
447 m.AddProperties(&m.properties)
448 android.InitAndroidModule(m)
449 return m
450}
451
452func addApiModule(mctx android.LoadHookContext, i *aidlInterface) string {
453 apiModule := i.ModuleBase.Name() + aidlApiSuffix
Jeongik Cha52e98022021-01-20 18:37:20 +0900454 srcs, aidlRoot := i.srcsForVersion(mctx, i.nextVersion())
Jeongik Chada36d5a2021-01-20 00:43:34 +0900455 mctx.CreateModule(aidlApiFactory, &nameProperties{
456 Name: proptools.StringPtr(apiModule),
457 }, &aidlApiProperties{
Jooyung Han29e54b92021-11-12 03:25:29 +0900458 BaseName: i.ModuleBase.Name(),
459 Srcs: srcs,
460 AidlRoot: aidlRoot,
461 Stability: i.properties.Stability,
462 Imports: i.properties.Imports,
463 Versions: i.properties.Versions,
464 Dumpapi: i.properties.Dumpapi,
Jeongik Chada36d5a2021-01-20 00:43:34 +0900465 })
466 return apiModule
467}
Jeongik Cha6cc16f32021-05-18 11:23:55 +0900468
469func versionForHashGen(ver string) string {
470 // aidlHashGen uses the version before current version. If it has never been frozen, return 'latest-version'.
471 verInt, _ := strconv.Atoi(ver)
472 if verInt > 1 {
473 return strconv.Itoa(verInt - 1)
474 }
475 return "latest-version"
476}
Jiyong Parke38dab42021-05-20 14:25:34 +0900477
478func init() {
479 android.RegisterSingletonType("aidl-freeze-api", freezeApiSingletonFactory)
480}
481
482func freezeApiSingletonFactory() android.Singleton {
483 return &freezeApiSingleton{}
484}
485
486type freezeApiSingleton struct{}
487
488func (f *freezeApiSingleton) GenerateBuildActions(ctx android.SingletonContext) {
489 var files android.Paths
490 ctx.VisitAllModules(func(module android.Module) {
491 if !module.Enabled() {
492 return
493 }
494 if m, ok := module.(*aidlApi); ok {
Devin Moore91d92492021-06-24 08:50:55 -0700495 ownersToFreeze := strings.Fields(ctx.Config().Getenv("AIDL_FREEZE_OWNERS"))
496 var shouldBeFrozen bool
497 if len(ownersToFreeze) > 0 {
498 shouldBeFrozen = android.InList(m.Owner(), ownersToFreeze)
499 } else {
500 shouldBeFrozen = m.Owner() == ""
501 }
502 if shouldBeFrozen {
503 files = append(files, m.freezeApiTimestamp)
504 }
Jiyong Parke38dab42021-05-20 14:25:34 +0900505 }
506 })
507 ctx.Phony("aidl-freeze-api", files...)
508}