| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 3 # Use of this source code is governed by a BSD-style license that can be |
| 4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
| 5 | 5 |
| 6 """ | 6 """ |
| 7 This script invokes the go build tool. | 7 This script invokes the go build tool. |
| 8 Must be called as follows: | 8 Must be called as follows: |
| 9 python go.py [--android] <go-tool> <build directory> <output file> | 9 python go.py [--android] <go-tool> <build directory> <output file> |
| 10 <src directory> <CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options> | 10 <src directory> <CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options> |
| 11 eg. | 11 eg. |
| 12 python go.py /usr/lib/google-golang/bin/go out/build out/a.out .. "-I." | 12 python go.py /usr/lib/google-golang/bin/go out/build out/a.out .. "-I." |
| 13 "-L. -ltest" test -c test/test.go | 13 "-L. -ltest" test -c test/test.go |
| 14 """ | 14 """ |
| 15 | 15 |
| 16 import argparse | 16 import argparse |
| 17 import os | 17 import os |
| 18 import shutil | 18 import shutil |
| 19 import subprocess | |
| 20 import sys | 19 import sys |
| 21 | 20 |
| 22 NDK_PLATFORM = 'android-16' | |
| 23 NDK_TOOLCHAIN = 'arm-linux-androideabi-4.9' | |
| 24 | |
| 25 def main(): | 21 def main(): |
| 26 parser = argparse.ArgumentParser() | 22 parser = argparse.ArgumentParser() |
| 27 parser.add_argument('--android', action='store_true') | 23 parser.add_argument('--android', action='store_true') |
| 28 parser.add_argument('go_tool') | 24 parser.add_argument('go_tool') |
| 29 parser.add_argument('build_directory') | 25 parser.add_argument('build_directory') |
| 30 parser.add_argument('output_file') | 26 parser.add_argument('output_file') |
| 31 parser.add_argument('src_root') | 27 parser.add_argument('src_root') |
| 32 parser.add_argument('out_root') | 28 parser.add_argument('out_root') |
| 33 parser.add_argument('cgo_cflags') | 29 parser.add_argument('cgo_cflags') |
| 34 parser.add_argument('cgo_ldflags') | 30 parser.add_argument('cgo_ldflags') |
| 35 parser.add_argument('go_option', nargs='*') | 31 parser.add_argument('go_option', nargs='*') |
| 36 args = parser.parse_args() | 32 args = parser.parse_args() |
| 37 go_tool = os.path.abspath(args.go_tool) | 33 go_tool = os.path.abspath(args.go_tool) |
| 38 build_dir = args.build_directory | 34 build_dir = args.build_directory |
| 39 out_file = os.path.abspath(args.output_file) | 35 out_file = os.path.abspath(args.output_file) |
| 40 # The src directory specified is relative. We need this as an absolute path. | |
| 41 src_root = os.path.abspath(args.src_root) | |
| 42 # GOPATH must be absolute, and point to one directory up from |src_Root| | |
| 43 go_path = os.path.abspath(os.path.join(src_root, '..')) | |
| 44 # GOPATH also includes any third_party/go libraries that have been imported | |
| 45 go_path += ':' + os.path.join(src_root, 'third_party', 'go') | |
| 46 go_path += ':' + os.path.abspath(os.path.join(args.out_root, 'gen', 'go')) | |
| 47 if 'MOJO_GOPATH' in os.environ: | |
| 48 go_path += ':' + os.environ['MOJO_GOPATH'] | |
| 49 go_options = args.go_option | 36 go_options = args.go_option |
| 50 try: | 37 try: |
| 51 shutil.rmtree(build_dir, True) | 38 shutil.rmtree(build_dir, True) |
| 52 os.mkdir(build_dir) | 39 os.mkdir(build_dir) |
| 53 except Exception: | 40 except Exception: |
| 54 pass | 41 pass |
| 55 old_directory = os.getcwd() | |
| 56 os.chdir(build_dir) | |
| 57 env = os.environ.copy() | |
| 58 env['GOPATH'] = go_path | |
| 59 env['GOROOT'] = os.path.dirname(os.path.dirname(go_tool)) | |
| 60 env['CGO_CFLAGS'] = args.cgo_cflags | |
| 61 env['CGO_LDFLAGS'] = args.cgo_ldflags | |
| 62 if args.android: | |
| 63 env['CGO_ENABLED'] = '1' | |
| 64 env['GOOS'] = 'android' | |
| 65 env['GOARCH'] = 'arm' | |
| 66 env['GOARM'] = '7' | |
| 67 # The Android go tool prebuilt binary has a default path to the compiler, | |
| 68 # which with high probability points to an invalid path, so we override the | |
| 69 # CC env var that will be used by the go tool. | |
| 70 if 'CC' not in env: | |
| 71 ndk_path = os.path.join(src_root, 'third_party', 'android_tools', 'ndk') | |
| 72 if sys.platform.startswith('linux'): | |
| 73 arch = 'linux-x86_64' | |
| 74 elif sys.platform == 'darwin': | |
| 75 arch = 'darwin-x86_64' | |
| 76 else: | |
| 77 raise Exception('unsupported platform: ' + sys.platform) | |
| 78 ndk_cc = os.path.join(ndk_path, 'toolchains', NDK_TOOLCHAIN, | |
| 79 'prebuilt', arch, 'bin', 'arm-linux-androideabi-gcc') | |
| 80 sysroot = os.path.join(ndk_path, 'platforms', NDK_PLATFORM, 'arch-arm') | |
| 81 env['CGO_CFLAGS'] += ' --sysroot %s' % sysroot | |
| 82 env['CGO_LDFLAGS'] += ' --sysroot %s' % sysroot | |
| 83 env['CC'] = '%s --sysroot %s' % (ndk_cc, sysroot) | |
| 84 | 42 |
| 85 call_result = subprocess.call([go_tool] + go_options, env=env) | 43 sys.path.append(os.path.join(args.src_root, 'mojo', 'tools')) |
| 44 from mopy.invoke_go import InvokeGo |
| 45 |
| 46 call_result = InvokeGo(go_tool, go_options, |
| 47 work_dir=build_dir, |
| 48 src_root=args.src_root, |
| 49 out_root=args.out_root, |
| 50 cgo_cflags=args.cgo_cflags, |
| 51 cgo_ldflags=args.cgo_ldflags, |
| 52 target_android=args.android) |
| 86 if call_result != 0: | 53 if call_result != 0: |
| 87 return call_result | 54 return call_result |
| 88 out_files = sorted([ f for f in os.listdir('.') if os.path.isfile(f)]) | 55 out_files = sorted([ f for f in os.listdir(build_dir) if os.path.isfile(f)]) |
| 89 if (len(out_files) > 0): | 56 if (len(out_files) > 0): |
| 90 shutil.move(out_files[0], out_file) | 57 shutil.move(out_files[0], out_file) |
| 91 os.chdir(old_directory) | |
| 92 try: | 58 try: |
| 93 shutil.rmtree(build_dir, True) | 59 shutil.rmtree(build_dir, True) |
| 94 except Exception: | 60 except Exception: |
| 95 pass | 61 pass |
| 96 | 62 |
| 97 if __name__ == '__main__': | 63 if __name__ == '__main__': |
| 98 sys.exit(main()) | 64 sys.exit(main()) |
| OLD | NEW |