Preserve any config logs on failure.

Test: replaced cc with ar, saw logs in dist dir
Bug: None
Change-Id: I5a20387d738290975188cb9cfbc14c74e2e07420
diff --git a/OWNERS b/OWNERS
index 88b3a39..8fbd6c9 100644
--- a/OWNERS
+++ b/OWNERS
@@ -4,3 +4,5 @@
 manojgupta@google.com
 rahulchaudhry@google.com
 yunlian@google.com
+
+per-file *.py = danalbert@google.com
diff --git a/do_build.py b/do_build.py
index a23fa4f..1067c99 100755
--- a/do_build.py
+++ b/do_build.py
@@ -19,6 +19,7 @@
 import logging
 import multiprocessing
 import os
+from pathlib import Path
 import shutil
 import site
 import subprocess
@@ -187,6 +188,20 @@
     subprocess.check_call(cmd)
 
 
+def copy_logs_to_dist_dir(build_dir: Path, base_log_dir: Path) -> None:
+    """Preserves any relevant log files from the build directory."""
+    log_file = 'config.log'
+    log_dir = base_log_dir / 'autoconf'
+    for root, _, files in os.walk(build_dir):
+        root_path = Path(root)
+        if log_file not in files:
+            continue
+        rel_path = Path(root).relative_to(build_dir)
+        dest_dir = log_dir / rel_path
+        dest_dir.mkdir(parents=True, exist_ok=True)
+        shutil.copyfile(str(root_path / log_file), str(dest_dir / log_file))
+
+
 def parse_args():
     """Parse command line arguments."""
     parser = argparse.ArgumentParser()
@@ -259,6 +274,9 @@
         install_timer = ndk.timer.Timer()
         with install_timer:
             install(args.jobs, args.arch, args.host, install_dir)
+    except subprocess.CalledProcessError:
+        copy_logs_to_dist_dir(Path(build_dir), Path(dist_dir) / 'logs')
+        raise
     finally:
         chdir(orig_dir)