Index: mojo/go/go.py |
diff --git a/mojo/go/go.py b/mojo/go/go.py |
index a0f93f213e2bda88fdcff9351885d96b56f5c7fd..1ecec4e0b128bd36559c74f478ee72d59d6949dd 100755 |
--- a/mojo/go/go.py |
+++ b/mojo/go/go.py |
@@ -37,29 +37,88 @@ def main(): |
go_tool = os.path.abspath(args.go_tool) |
build_dir = args.build_directory |
out_file = os.path.abspath(args.output_file) |
- # The src directory specified is relative. We need this as an absolute path. |
- src_root = os.path.abspath(args.src_root) |
- # GOPATH must be absolute, and point to one directory up from |src_Root| |
- go_path = os.path.abspath(os.path.join(src_root, '..')) |
- # GOPATH also includes any third_party/go libraries that have been imported |
- go_path += ':' + os.path.join(src_root, 'third_party', 'go') |
- go_path += ':' + os.path.abspath(os.path.join(args.out_root, 'gen', 'go')) |
- if 'MOJO_GOPATH' in os.environ: |
- go_path += ':' + os.environ['MOJO_GOPATH'] |
go_options = args.go_option |
try: |
shutil.rmtree(build_dir, True) |
os.mkdir(build_dir) |
except Exception: |
pass |
- old_directory = os.getcwd() |
- os.chdir(build_dir) |
+ |
+ call_result = InvokeGo(go_tool, go_options, |
+ work_dir=build_dir, |
+ src_root=args.src_root, |
+ out_root=args.out_root, |
+ cgo_cflags=args.cgo_cflags, |
+ cgo_ldflags=args.cgo_ldflags, |
+ target_android=args.android) |
+ if call_result != 0: |
+ return call_result |
+ out_files = sorted([ f for f in os.listdir(build_dir) if os.path.isfile(f)]) |
+ if (len(out_files) > 0): |
+ shutil.move(out_files[0], out_file) |
+ try: |
+ shutil.rmtree(build_dir, True) |
+ except Exception: |
+ pass |
+ |
+def InvokeGo(go_tool, go_options, work_dir=None, src_root=None, |
viettrungluu
2015/10/09 21:39:49
Probably this should be put under mojo/tools/mopy:
rudominer
2015/10/09 23:42:06
Done.
|
+ out_root=None, cgo_cflags=None, cgo_ldflags=None, |
+ target_android=False): |
+ """ Invokes the go tool after setting up the environment. |
+ go_tool The absolute path to the 'go' binary |
+ |
+ go_options A list of string arguments that will be passed to 'go' |
+ |
+ work_dir Optional specification of a directory to temporarily switch into |
+ before invoking the go tool. If set it must be a path relative to |
+ the current working directory or an absolute path. |
+ |
+ src_root Optional specification of the Mojo src root. If set it must be |
+ a path relative to the current working directory or an absolute |
+ path. This will be used to add additional elements to |
+ the GOPATH environment variable. |
+ |
+ out_root Optional specification of the Mojo out dir. If set it must be |
+ a path relative to the current working directory or an absolute |
+ path. This will be used to add additional elements to |
+ the GOPATH environment variable. |
+ |
+ cgo_cflags Optional value to set for the CGO_CFLAGS environment variable. |
+ |
+ cgo_ldflags Optional value to set for the CGO_LDFLAGS environment variable. |
+ |
+ target_android |
+ Set to true to target Android. |
+ """ |
+ assert os.path.isabs(go_tool) |
env = os.environ.copy() |
- env['GOPATH'] = go_path |
env['GOROOT'] = os.path.dirname(os.path.dirname(go_tool)) |
- env['CGO_CFLAGS'] = args.cgo_cflags |
- env['CGO_LDFLAGS'] = args.cgo_ldflags |
- if args.android: |
+ |
+ go_path = '' |
+ if src_root is not None: |
+ # The src directory specified is relative. We need this as an absolute path. |
+ src_root = os.path.abspath(src_root) |
+ # GOPATH must be absolute, and point to one directory up from |src_Root| |
+ go_path = os.path.abspath(os.path.join(src_root, '..')) |
+ # GOPATH also includes any third_party/go libraries that have been imported |
+ go_path += ':' + os.path.join(src_root, 'third_party', 'go') |
+ |
+ if out_root is not None: |
+ go_path += ':' + os.path.abspath(os.path.join(out_root, 'gen', 'go')) |
+ |
+ if 'MOJO_GOPATH' in os.environ: |
+ go_path += ':' + os.environ['MOJO_GOPATH'] |
+ |
+ if len(go_path) > 0 : |
+ env['GOPATH'] = go_path |
+ |
+ if cgo_cflags is not None: |
+ env['CGO_CFLAGS'] = cgo_cflags |
+ |
+ if cgo_ldflags is not None: |
+ env['CGO_LDFLAGS'] = cgo_ldflags |
+ |
+ if target_android: |
env['CGO_ENABLED'] = '1' |
env['GOOS'] = 'android' |
env['GOARCH'] = 'arm' |
@@ -68,6 +127,9 @@ def main(): |
# which with high probability points to an invalid path, so we override the |
# CC env var that will be used by the go tool. |
if 'CC' not in env: |
+ if src_root is None: |
+ raise Exception('src_root must be set if is_android is true and ' |
+ '"CC" is not in env.') |
ndk_path = os.path.join(src_root, 'third_party', 'android_tools', 'ndk') |
if sys.platform.startswith('linux'): |
arch = 'linux-x86_64' |
@@ -82,17 +144,13 @@ def main(): |
env['CGO_LDFLAGS'] += ' --sysroot %s' % sysroot |
env['CC'] = '%s --sysroot %s' % (ndk_cc, sysroot) |
- call_result = subprocess.call([go_tool] + go_options, env=env) |
- if call_result != 0: |
- return call_result |
- out_files = sorted([ f for f in os.listdir('.') if os.path.isfile(f)]) |
- if (len(out_files) > 0): |
- shutil.move(out_files[0], out_file) |
- os.chdir(old_directory) |
- try: |
- shutil.rmtree(build_dir, True) |
- except Exception: |
- pass |
+ save_cwd = os.getcwd() |
+ if work_dir is not None: |
+ os.chdir(work_dir) |
+ result = subprocess.call([go_tool] + go_options, env=env) |
+ if work_dir is not None: |
+ os.chdir(save_cwd) |
+ return result |
if __name__ == '__main__': |
sys.exit(main()) |