Fix tools/gen_binary_descriptors

- Actually run tool in presubmit
- Improve error messages
- Optionally find protoc automatically
- Update perfetto_config.descriptor.h

Change-Id: I13e6880b7d3f86464acfbb6a9134e2055ca860a6
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 9a4902f..3c11c8b 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -30,6 +30,7 @@
     results += input.canned_checks.CheckGNFormatted(input, output)
     results += CheckIncludeGuards(input, output)
     results += CheckAndroidBlueprint(input, output)
+    results += CheckBinaryDescriptors(input, output)
     results += CheckMergedTraceConfigProto(input, output)
     results += CheckWhitelist(input, output)
     return results
@@ -85,7 +86,7 @@
     tool = 'tools/gen_binary_descriptors'
     file_filter = lambda x: input_api.FilterSourceFile(
           x,
-          white_list=('.*[.]h$', '.*[.]proto$', tool))
+          white_list=('protos/perfetto/.*[.]proto$', '.*[.]h', tool))
     if not input_api.AffectedSourceFiles(file_filter):
         return []
     if subprocess.call([tool, '--check-only']):
diff --git a/src/perfetto_cmd/perfetto_config.descriptor.h b/src/perfetto_cmd/perfetto_config.descriptor.h
index 74bbfac..5f0361b 100644
--- a/src/perfetto_cmd/perfetto_config.descriptor.h
+++ b/src/perfetto_cmd/perfetto_config.descriptor.h
@@ -10,7 +10,7 @@
 // This file was autogenerated by tools/gen_binary_descriptors. Do not edit.
 
 // SHA1(tools/gen_binary_descriptors)
-// 39b24672017b75f5e5ac590c4dc03b2c41bf3eea
+// 2aabce73a7c0d2527e9cb33b9c3c05aeca7a9534
 // SHA1(protos/perfetto/config/perfetto_config.proto)
 // 91af0d676a39daa199e00b09a1b1e13670c0bea1
 
diff --git a/tools/gen_binary_descriptors b/tools/gen_binary_descriptors
index 513ef95..d608095 100755
--- a/tools/gen_binary_descriptors
+++ b/tools/gen_binary_descriptors
@@ -34,6 +34,7 @@
 
 SCRIPT_PATH = 'tools/gen_binary_descriptors'
 
+
 def hash_path(path):
   hash = hashlib.sha1()
   with open(os.path.join(ROOT_DIR, path)) as f:
@@ -41,8 +42,19 @@
   return hash.hexdigest()
 
 
+def find_protoc():
+  for root, dirs, files in os.walk(ROOT_DIR):
+    if 'protoc' in files:
+      return os.path.join(root, 'protoc')
+    for name in ('src', 'buildtools'):
+      if name in dirs:
+        dirs.remove(name)
+  return None
+
+
 def check(source, target):
-  assert os.path.exists(os.path.join(ROOT_DIR, target))
+  assert os.path.exists(os.path.join(ROOT_DIR, target)), \
+      'Output file {} does not exist and so cannot be checked'.format(target)
 
   with open(target, 'rb') as f:
     s = f.read()
@@ -52,7 +64,7 @@
   for path, expected_sha1 in hashes:
     actual_sha1 = hash_path(os.path.join(ROOT_DIR, path))
     assert actual_sha1 == expected_sha1, \
-        'Hash for path {} did not match'.format(path)
+        'In {} hash given for {} did not match'.format(target, path)
 
 
 def generate(source, target, protoc_path):
@@ -123,13 +135,21 @@
   parser.add_argument('--protoc')
   args = parser.parse_args()
 
-  for source, target in SOURCE_TARGET.iteritems():
-    if args.check_only:
-        check(source, target)
-    else:
-      protoc = args.protoc
-      assert os.path.exists(protoc)
-      generate(source, target, args.protoc)
+  try:
+    for source, target in SOURCE_TARGET.iteritems():
+      if args.check_only:
+          check(source, target)
+      else:
+        protoc = args.protoc or find_protoc()
+        assert protoc, 'protoc not found specific (--protoc PROTOC_PATH)'
+        assert os.path.exists(protoc), '{} does not exist'.format(protoc)
+        if protoc is not args.protoc:
+          print('Using protoc: {}'.format(protoc))
+        generate(source, target, protoc)
+  except AssertionError as e:
+    if not str(e):
+      raise
+    print('Error: {}'.format(e))
 
 if __name__ == '__main__':
   exit(main())