Index: fusl/tools/populate_crt.py |
diff --git a/fusl/tools/populate_crt.py b/fusl/tools/populate_crt.py |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d9c7d679be7858ef27a23ffc11c729d3418e74d9 |
--- /dev/null |
+++ b/fusl/tools/populate_crt.py |
@@ -0,0 +1,85 @@ |
+# Copyright 2016 The Chromium Authors. All rights reserved. |
+# Use of this source code is governed by a BSD-style license that can be |
+# found in the LICENSE file. |
+ |
+import os.path |
+import shutil |
+import subprocess |
+import sys |
+ |
+ |
+"""Populate the compiler-provided portions of the fusl sysroot. |
+ |
+In particular, crtbegin.o, crtend.o, and libgcc.a must be copied from |
+the host sysroot to our sysroot. |
+""" |
+ |
+SYSROOT_ARTEFACTS = [ |
+ 'crtbegin.o', |
viettrungluu
2016/02/01 21:15:55
I think we mostly format Python code with a 2-spac
|
+ 'crtend.o', |
+ 'libgcc.a', |
+] |
+ |
+ |
+def parse_clang_dirs(clang): |
+ """Parse the output of |clang -print-search-dirs|. |
+ |
+ This is used to find library search paths. One line of the output |
+ is formatted something like: |
+ |
+ libraries: =/usr/lib/gcc/4.8:/lib:/usr/lib |
+ |
+ This functions strips the 'libraries: =' prefix and splits on the |
+ colon, returning the list: |
+ |
+ [ '/usr/lib/gcc/4.8', '/lib', '/usr/lib' ] |
+ |
+ """ |
+ |
+ clang_search_dirs = subprocess.check_output([clang, '-print-search-dirs']) |
+ |
+ library_line = None |
+ library_line_prefix = 'libraries: =' |
+ for line in clang_search_dirs.split('\n'): |
+ if line.startswith(library_line_prefix): |
+ prefix_length = len(library_line_prefix) |
+ library_line = line[prefix_length:] |
+ break |
+ assert(library_line) |
+ return library_line.split(':') |
+ |
+ |
+def try_populate(library_path, target): |
+ """Returns whether this path contained all the libs and we were able |
+ to copy them.""" |
+ |
+ # Check that everything is here. |
viettrungluu
2016/02/01 21:15:55
Is it possible to find some things, but not all? (
kulakowski
2016/02/01 21:29:29
Ah, yeah. Didn't think too much about it, since in
|
+ for artefact in SYSROOT_ARTEFACTS: |
viettrungluu
2016/02/01 21:15:55
In 'merika, "we" spell artifact with an "i".
kulakowski
2016/02/01 21:29:29
Done.
|
+ path = os.path.join(library_path, artefact) |
+ if not os.path.isfile(path): |
+ return False |
+ |
+ # All here, so copy! |
+ for artefact in SYSROOT_ARTEFACTS: |
+ source = os.path.join(library_path, artefact) |
+ shutil.copy(source, target) |
+ |
+ return True |
+ |
+ |
+def main(): |
+ clang = sys.argv[1] |
+ target = sys.argv[2] |
+ |
+ library_paths = parse_clang_dirs(clang) |
+ for library_path in library_paths: |
+ if try_populate(library_path, target): |
+ # If we successfully populated with one path, we are done. |
+ exit(0) |
+ |
+ # No path had what we needed, so fail. |
+ exit(1) |
+ |
+ |
+if __name__ == '__main__': |
+ main() |