Add a presubmit check to keep Android.bp up-to-date

When trying to upload a change which affects build files,
but does not update Android.bp to match, the following presubmit
error gets triggered:

   Android.bp is out of date. Please run tools/gen_android_bp to update it.

This patch also replaces Android.bp with the output from the generator.

Change-Id: If60c1c101cd640d91b477b022c2db83acbd0711f
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 3afea72..846927b 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -12,18 +12,51 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import subprocess
+
+
 def CheckChange(input, output):
+    # There apparently is no way to wrap strings in blueprints, so ignore long
+    # lines in them.
+    long_line_sources = lambda x: input.FilterSourceFile(
+            x, white_list=".*", black_list=['Android[.]bp'])
+
     results = []
     results += input.canned_checks.CheckDoNotSubmit(input, output)
     results += input.canned_checks.CheckChangeHasNoTabs(input, output)
-    results += input.canned_checks.CheckLongLines(input, output, 80)
+    results += input.canned_checks.CheckLongLines(
+            input, output, 80, source_file_filter=long_line_sources)
     results += input.canned_checks.CheckPatchFormatted(input, output)
     results += input.canned_checks.CheckGNFormatted(input, output)
+    results += CheckAndroidBlueprint(input, output)
     return results
 
+
 def CheckChangeOnUpload(input_api, output_api):
     return CheckChange(input_api, output_api)
 
+
 def CheckChangeOnCommit(input_api, output_api):
     return CheckChange(input_api, output_api)
 
+
+def CheckAndroidBlueprint(input_api, output_api):
+    # If no GN files were modified, bail out.
+    build_file_filter = lambda x: input_api.FilterSourceFile(
+          x,
+          white_list=('.*BUILD[.]gn$', '.*[.]gni$', 'tools/gen_android_bp'))
+    if not input_api.AffectedSourceFiles(build_file_filter):
+        return []
+
+    with open('Android.bp') as f:
+        current_blueprint = f.read()
+    new_blueprint = subprocess.check_output(
+        ['tools/gen_android_bp', '--output', '/dev/stdout'])
+
+    if current_blueprint != new_blueprint:
+        return [
+            output_api.PresubmitError(
+                'Android.bp is out of date. Please run tools/gen_android_bp '
+                'to update it.')
+        ]
+    return []