Snap for 5647767 from 3ac5528f3d584505c6e3bd6d7365cc93c6534efb to qt-c2f2-release

Change-Id: I8cae06e56799730a666aa8851d64cff52129a666
diff --git a/build/aidl_interface.go b/build/aidl_interface.go
index dcda58f..ba022bf 100644
--- a/build/aidl_interface.go
+++ b/build/aidl_interface.go
@@ -85,20 +85,25 @@
 		blueprint.RuleParams{
 			Command: `mkdir -p ${to} && rm -rf ${to}/* && ` +
 				`${bpmodifyCmd} -w -m ${name} -parameter versions -a ${version} ${bp} && ` +
-				`cp -rf ${in}/* ${to} && touch ${out}`,
+				`cp -rf ${in}/* ${to} && ` +
+				`find ${to} -type f -exec bash -c ` +
+				`"cat ${apiPreamble} {} > {}.temp; mv {}.temp {}" \; && ` +
+				`touch ${out}`,
 			CommandDeps: []string{"${bpmodifyCmd}"},
-		}, "to", "name", "version", "bp")
+		}, "to", "name", "version", "bp", "apiPreamble")
 
 	aidlCheckApiRule = pctx.StaticRule("aidlCheckApiRule", blueprint.RuleParams{
-		Command:     `${aidlCmd} --checkapi ${old} ${new} && touch ${out}`,
+		Command: `(${aidlCmd} --checkapi ${old} ${new} && touch ${out}) || ` +
+			`(cat ${messageFile} && exit 1)`,
 		CommandDeps: []string{"${aidlCmd}"},
 		Description: "AIDL CHECK API: ${new} against ${old}",
-	}, "old", "new")
+	}, "old", "new", "messageFile")
 
 	aidlDiffApiRule = pctx.StaticRule("aidlDiffApiRule", blueprint.RuleParams{
-		Command:     `diff -r ${old} ${new} && touch ${out}`,
+		Command: `(diff -r -B -I '//.*' ${old} ${new} && touch ${out}) || ` +
+			`(cat ${messageFile} && exit 1)`,
 		Description: "Check equality of ${new} and ${old}",
-	}, "old", "new")
+	}, "old", "new", "messageFile")
 )
 
 func init() {
@@ -387,17 +392,23 @@
 
 	modulePath := android.PathForModuleSrc(ctx).String()
 
+	var implicits android.Paths
+	implicits = append(implicits, apiFiles...)
+
+	apiPreamble := android.PathForSource(ctx, "system/tools/aidl/build/api_preamble.txt")
+	implicits = append(implicits, apiPreamble)
+
 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 		Rule:        aidlFreezeApiRule,
 		Description: "Freezing AIDL API of " + m.properties.BaseName + " as version " + version,
-		Input:       apiDumpDir,
-		Implicits:   apiFiles,
+		Implicits:   implicits,
 		Output:      timestampFile,
 		Args: map[string]string{
-			"to":      filepath.Join(modulePath, m.apiDir(), version),
-			"name":    m.properties.BaseName,
-			"version": version,
-			"bp":      android.PathForModuleSrc(ctx, "Android.bp").String(),
+			"to":          filepath.Join(modulePath, m.apiDir(), version),
+			"name":        m.properties.BaseName,
+			"version":     version,
+			"bp":          android.PathForModuleSrc(ctx, "Android.bp").String(),
+			"apiPreamble": apiPreamble.String(),
 		},
 	})
 	return timestampFile
@@ -406,16 +417,19 @@
 func (m *aidlApi) checkCompatibility(ctx android.ModuleContext, oldApiDir android.Path, oldApiFiles android.Paths, newApiDir android.Path, newApiFiles android.Paths) android.WritablePath {
 	newVersion := newApiDir.Base()
 	timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
-	var allApiFiles android.Paths
-	allApiFiles = append(allApiFiles, oldApiFiles...)
-	allApiFiles = append(allApiFiles, newApiFiles...)
+	messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_compatibility.txt")
+	var implicits android.Paths
+	implicits = append(implicits, oldApiFiles...)
+	implicits = append(implicits, newApiFiles...)
+	implicits = append(implicits, messageFile)
 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 		Rule:      aidlCheckApiRule,
-		Implicits: allApiFiles,
+		Implicits: implicits,
 		Output:    timestampFile,
 		Args: map[string]string{
-			"old": oldApiDir.String(),
-			"new": newApiDir.String(),
+			"old":         oldApiDir.String(),
+			"new":         newApiDir.String(),
+			"messageFile": messageFile.String(),
 		},
 	})
 	return timestampFile
@@ -424,16 +438,19 @@
 func (m *aidlApi) checkEquality(ctx android.ModuleContext, oldApiDir android.Path, oldApiFiles android.Paths, newApiDir android.Path, newApiFiles android.Paths) android.WritablePath {
 	newVersion := newApiDir.Base()
 	timestampFile := android.PathForModuleOut(ctx, "checkapi_"+newVersion+".timestamp")
-	var allApiFiles android.Paths
-	allApiFiles = append(allApiFiles, oldApiFiles...)
-	allApiFiles = append(allApiFiles, newApiFiles...)
+	messageFile := android.PathForSource(ctx, "system/tools/aidl/build/message_check_equality.txt")
+	var implicits android.Paths
+	implicits = append(implicits, oldApiFiles...)
+	implicits = append(implicits, newApiFiles...)
+	implicits = append(implicits, messageFile)
 	ctx.ModuleBuild(pctx, android.ModuleBuildParams{
 		Rule:      aidlDiffApiRule,
-		Implicits: allApiFiles,
+		Implicits: implicits,
 		Output:    timestampFile,
 		Args: map[string]string{
-			"old": oldApiDir.String(),
-			"new": newApiDir.String(),
+			"old":         oldApiDir.String(),
+			"new":         newApiDir.String(),
+			"messageFile": messageFile.String(),
 		},
 	})
 	return timestampFile
diff --git a/build/api_preamble.txt b/build/api_preamble.txt
new file mode 100644
index 0000000..3a2c34d
--- /dev/null
+++ b/build/api_preamble.txt
@@ -0,0 +1,17 @@
+///////////////////////////////////////////////////////////////////////////////
+// THIS FILE IS IMMUTABLE. DO NOT EDIT IN ANY CASE.                          //
+///////////////////////////////////////////////////////////////////////////////
+
+// This file is a frozen snapshot of an AIDL interface (or parcelable). Do not
+// try to edit this file. It looks like you are doing that because you have
+// modified an AIDL interface in a backward-incompatible way, e.g., deleting a
+// function from an interface or a field from a parcelable and it broke the
+// build. That breakage is intended.
+//
+// You must not make a backward incompatible changes to the AIDL files built
+// with the aidl_interface module type with versions property set. The module
+// type is used to build AIDL files in a way that they can be used across
+// independently updatable components of the system. If a device is shipped
+// with such a backward incompatible change, it has a high risk of breaking
+// later when a module using the interface is updated, e.g., Mainline modules.
+
diff --git a/build/message_check_compatibility.txt b/build/message_check_compatibility.txt
new file mode 100644
index 0000000..1df7544
--- /dev/null
+++ b/build/message_check_compatibility.txt
@@ -0,0 +1,7 @@
+###############################################################################
+# ERROR: Backward incompatible change detected on AIDL API                    #
+###############################################################################
+Above AIDL file(s) has changed in a backward-incompatible way, e.g. removing
+a method from an interface or a field from a parcelable. If a device is shipped
+with this change by ignoring this message, it has a high risk of breaking later
+when a module using the interface is updated, e.g., Maineline modules.
diff --git a/build/message_check_equality.txt b/build/message_check_equality.txt
new file mode 100644
index 0000000..fc5d063
--- /dev/null
+++ b/build/message_check_equality.txt
@@ -0,0 +1,7 @@
+###############################################################################
+# ERROR: AIDL API change detected on a released platform                      #
+###############################################################################
+Above AIDL file(s) has changed and this is NEVER allowed on a release platform
+(i.e., PLATFORM_VERSION_CODENAME is REL). If a device is shipped with this
+change by ignoring this message, it has a high risk of breaking later when a
+module using the interface is updated, e.g., Maineline modules.