OLD | NEW |
(Empty) | |
| 1 # Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """The function InvokeGo() is used to invoke the Go tool after setting up |
| 6 the environment and current working directory.""" |
| 7 |
| 8 import os |
| 9 import subprocess |
| 10 import sys |
| 11 |
| 12 NDK_PLATFORM = 'android-16' |
| 13 NDK_TOOLCHAIN = 'arm-linux-androideabi-4.9' |
| 14 |
| 15 def InvokeGo(go_tool, go_options, work_dir=None, src_root=None, |
| 16 out_root=None, cgo_cflags=None, cgo_ldflags=None, |
| 17 target_android=False): |
| 18 """ Invokes the go tool after setting up the environment. |
| 19 go_tool The path to the 'go' binary. Must be either an absolute path |
| 20 or a path relative to the current working directory. |
| 21 |
| 22 go_options A list of string arguments that will be passed to 'go' |
| 23 |
| 24 work_dir Optional specification of a directory to temporarily switch into |
| 25 before invoking the go tool. If set it must be a path relative to |
| 26 the current working directory or an absolute path. |
| 27 |
| 28 src_root Optional specification of the Mojo src root. If set it must be |
| 29 a path relative to the current working directory or an absolute |
| 30 path. This will be used to add additional elements to |
| 31 the GOPATH environment variable. |
| 32 |
| 33 out_root Optional specification of the Mojo out dir. If set it must be |
| 34 a path relative to the current working directory or an absolute |
| 35 path. This will be used to add additional elements to |
| 36 the GOPATH environment variable. |
| 37 |
| 38 cgo_cflags Optional value to set for the CGO_CFLAGS environment variable. |
| 39 |
| 40 cgo_ldflags Optional value to set for the CGO_LDFLAGS environment variable. |
| 41 |
| 42 target_android |
| 43 Set to true to target Android. |
| 44 """ |
| 45 go_tool = os.path.abspath(go_tool) |
| 46 env = os.environ.copy() |
| 47 env['GOROOT'] = os.path.dirname(os.path.dirname(go_tool)) |
| 48 |
| 49 go_path = '' |
| 50 if src_root is not None: |
| 51 # The src directory specified may be relative. |
| 52 src_root = os.path.abspath(src_root) |
| 53 # GOPATH must be absolute, and point to one directory up from |src_Root| |
| 54 go_path = os.path.abspath(os.path.join(src_root, '..')) |
| 55 # GOPATH also includes any third_party/go libraries that have been imported |
| 56 go_path += ':' + os.path.join(src_root, 'third_party', 'go') |
| 57 |
| 58 if out_root is not None: |
| 59 go_path += ':' + os.path.abspath(os.path.join(out_root, 'gen', 'go')) |
| 60 |
| 61 if 'MOJO_GOPATH' in os.environ: |
| 62 go_path += ':' + os.environ['MOJO_GOPATH'] |
| 63 |
| 64 if len(go_path) > 0 : |
| 65 env['GOPATH'] = go_path |
| 66 |
| 67 if cgo_cflags is not None: |
| 68 env['CGO_CFLAGS'] = cgo_cflags |
| 69 |
| 70 if cgo_ldflags is not None: |
| 71 env['CGO_LDFLAGS'] = cgo_ldflags |
| 72 |
| 73 if target_android: |
| 74 env['CGO_ENABLED'] = '1' |
| 75 env['GOOS'] = 'android' |
| 76 env['GOARCH'] = 'arm' |
| 77 env['GOARM'] = '7' |
| 78 # The Android go tool prebuilt binary has a default path to the compiler, |
| 79 # which with high probability points to an invalid path, so we override the |
| 80 # CC env var that will be used by the go tool. |
| 81 if 'CC' not in env: |
| 82 if src_root is None: |
| 83 raise Exception('src_root must be set if is_android is true and ' |
| 84 '"CC" is not in env.') |
| 85 # Note that src_root was made absolute above. |
| 86 ndk_path = os.path.join(src_root, 'third_party', 'android_tools', 'ndk') |
| 87 if sys.platform.startswith('linux'): |
| 88 arch = 'linux-x86_64' |
| 89 elif sys.platform == 'darwin': |
| 90 arch = 'darwin-x86_64' |
| 91 else: |
| 92 raise Exception('unsupported platform: ' + sys.platform) |
| 93 ndk_cc = os.path.join(ndk_path, 'toolchains', NDK_TOOLCHAIN, |
| 94 'prebuilt', arch, 'bin', 'arm-linux-androideabi-gcc') |
| 95 sysroot = os.path.join(ndk_path, 'platforms', NDK_PLATFORM, 'arch-arm') |
| 96 env['CGO_CFLAGS'] += ' --sysroot %s' % sysroot |
| 97 env['CGO_LDFLAGS'] += ' --sysroot %s' % sysroot |
| 98 env['CC'] = '%s --sysroot %s' % (ndk_cc, sysroot) |
| 99 |
| 100 save_cwd = os.getcwd() |
| 101 if work_dir is not None: |
| 102 os.chdir(work_dir) |
| 103 result = subprocess.call([go_tool] + go_options, env=env) |
| 104 if work_dir is not None: |
| 105 os.chdir(save_cwd) |
| 106 return result |
OLD | NEW |