OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. |
| 5 |
| 6 """ |
| 7 This script invokes the go build tool. |
| 8 Must be called as follows: |
| 9 python go.py <go-binary> <build directory> <output file> <src directory> |
| 10 <CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options> |
| 11 eg. |
| 12 python go.py /usr/lib/google-golang/bin/go out/build out/a.out .. "-I." |
| 13 "-L. -ltest" test -c test/test.go |
| 14 """ |
| 15 |
| 16 import os |
| 17 import shutil |
| 18 import string |
| 19 import sys |
| 20 |
| 21 def main(): |
| 22 args = sys.argv |
| 23 go_binary = args[1] |
| 24 build_dir = args[2] |
| 25 out_file = os.path.abspath(args[3]) |
| 26 # The src directory specified is relative. We will later need this as an |
| 27 # absolute path. |
| 28 src_root = os.path.abspath(args[4]) + "/" |
| 29 # GOPATH must be absolute, and point to one directory up from |src_Root| |
| 30 go_path = os.path.abspath(src_root + "..") |
| 31 # CGO_CFLAGS and CGO_LDFLAGS contain a paths relative to |src_root| |
| 32 # We need to make these absolute paths by prepending the (absolute) |src_root| |
| 33 cgo_cflags = string.replace(" " + args[5], " -I", " -I" + src_root)[1:] |
| 34 cgo_ldflags = string.replace(" " + args[6], " -L", " -L" + src_root)[1:] |
| 35 go_options = args[7:] |
| 36 try: |
| 37 shutil.rmtree(build_dir, True) |
| 38 os.mkdir(build_dir) |
| 39 except: |
| 40 pass |
| 41 old_directory = os.getcwd() |
| 42 os.chdir(build_dir) |
| 43 os.environ["GOPATH"] = go_path |
| 44 os.environ["CGO_CFLAGS"] = cgo_cflags |
| 45 os.environ["CGO_LDFLAGS"] = cgo_ldflags |
| 46 os.system("%s %s" % (go_binary, " ".join(go_options))) |
| 47 out_files = [ f for f in os.listdir(".") if os.path.isfile(f)] |
| 48 if (len(out_files) > 0): |
| 49 shutil.move(out_files[0], out_file) |
| 50 os.chdir(old_directory) |
| 51 try: |
| 52 shutil.rmtree(build_dir, True) |
| 53 except: |
| 54 pass |
| 55 |
| 56 if __name__ == '__main__': |
| 57 sys.exit(main()) |
OLD | NEW |