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 argparse |
| 17 import os |
| 18 import shutil |
| 19 import string |
| 20 import sys |
| 21 |
| 22 def main(): |
| 23 parser = argparse.ArgumentParser() |
| 24 parser.add_argument('go_binary') |
| 25 parser.add_argument('build_directory') |
| 26 parser.add_argument('output_file') |
| 27 parser.add_argument('src_root') |
| 28 parser.add_argument('cgo_cflags') |
| 29 parser.add_argument('cgo_ldflags') |
| 30 parser.add_argument('go_option', nargs='*') |
| 31 args = parser.parse_args() |
| 32 go_binary = args.go_binary |
| 33 build_dir = args.build_directory |
| 34 out_file = os.path.abspath(args.output_file) |
| 35 # The src directory specified is relative. We will later need this as an |
| 36 # absolute path. |
| 37 src_root = os.path.abspath(args.src_root) + "/" |
| 38 # GOPATH must be absolute, and point to one directory up from |src_Root| |
| 39 go_path = os.path.abspath(os.path.join(src_root, "..")) |
| 40 # CGO_CFLAGS and CGO_LDFLAGS contain a paths relative to |src_root| |
| 41 # We need to make these absolute paths by prepending the (absolute) |src_root| |
| 42 cgo_cflags = string.replace(" " + args.cgo_cflags, |
| 43 " -I", " -I" + src_root)[1:] |
| 44 cgo_ldflags = string.replace(" " + args.cgo_ldflags, |
| 45 " -L", " -L" + src_root)[1:] |
| 46 go_options = args.go_option |
| 47 try: |
| 48 shutil.rmtree(build_dir, True) |
| 49 os.mkdir(build_dir) |
| 50 except: |
| 51 pass |
| 52 old_directory = os.getcwd() |
| 53 os.chdir(build_dir) |
| 54 os.environ["GOPATH"] = go_path |
| 55 os.environ["CGO_CFLAGS"] = cgo_cflags |
| 56 os.environ["CGO_LDFLAGS"] = cgo_ldflags |
| 57 os.system("%s %s" % (go_binary, " ".join(go_options))) |
| 58 out_files = [ f for f in os.listdir(".") if os.path.isfile(f)] |
| 59 if (len(out_files) > 0): |
| 60 shutil.move(out_files[0], out_file) |
| 61 os.chdir(old_directory) |
| 62 try: |
| 63 shutil.rmtree(build_dir, True) |
| 64 except: |
| 65 pass |
| 66 |
| 67 if __name__ == '__main__': |
| 68 sys.exit(main()) |
OLD | NEW |