Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/env python | |
| 2 # Copyright 2014 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 """Like env.py, but for building go-on-mobile. | |
| 7 | |
| 8 It wraps env.py, runs `gomobile init`, and adds mobile-specific env vars. | |
| 9 """ | |
| 10 | |
| 11 assert __name__ == '__main__' | |
| 12 | |
| 13 import os | |
| 14 import pipes | |
| 15 import platform | |
| 16 import subprocess | |
| 17 import sys | |
| 18 | |
| 19 | |
| 20 ANDROID_NDK_PATH = os.path.join( | |
| 21 os.path.abspath(os.path.dirname(__file__)), '.vendor', 'pkg', 'gomobile', | |
| 22 'android-ndk-r12b') | |
| 23 | |
| 24 # Used for mapping GOARCH values to NDK toolsets. | |
| 25 ANDROID_TOOLSETS = { | |
| 26 'arm': 'arm', | |
| 27 'arm64': 'aarch64', | |
| 28 '386': 'x86', | |
| 29 'amd64': 'x86_64', | |
| 30 } | |
| 31 | |
| 32 | |
| 33 def _get_android_env(env): | |
| 34 """Adds GO* and C* environment variables needed for building for android.""" | |
| 35 if platform.system() != 'Linux': | |
| 36 raise Exception( | |
| 37 'Only Linux hosts supported for android cross-compiling') | |
| 38 | |
| 39 arch = env.get('GOARCH') | |
| 40 if arch not in ANDROID_TOOLSETS: | |
| 41 raise Exception( | |
| 42 'Specified arch not currently supported on android: %s' % arch) | |
|
iannucci
2016/08/11 01:20:48
I'm still not comfortable with this check. The go
bpastene
2016/08/11 17:27:54
GOARCH needs to be specified during the call to mo
| |
| 43 | |
| 44 toolset = ANDROID_TOOLSETS[arch] | |
| 45 | |
| 46 # Needed when cross-compiling uses of cgo. | |
| 47 env['CGO_ENABLED'] = '1' | |
| 48 env['CC'] = os.path.join( | |
| 49 ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang' % toolset) | |
| 50 env['CXX'] = os.path.join( | |
| 51 ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang++' % toolset) | |
| 52 | |
| 53 # Compiler/linker needs access to android system headers in the NDK. | |
| 54 sysroot_path = os.path.join(ANDROID_NDK_PATH, arch, 'sysroot') | |
| 55 env['CGO_CFLAGS'] = '-I %s/usr/include --sysroot %s' % ( | |
| 56 sysroot_path, sysroot_path) | |
| 57 env['CGO_LDFLAGS'] = '-L %s/usr/lib --sysroot %s' % ( | |
| 58 sysroot_path, sysroot_path) | |
| 59 | |
| 60 | |
| 61 # Run `gomobile init` to fetch the android NDK. | |
| 62 if not os.path.exists(ANDROID_NDK_PATH): | |
| 63 cmd = [sys.executable, 'env.py', 'gomobile', 'init'] | |
| 64 subprocess.check_call(cmd) | |
| 65 | |
| 66 # Keep track of any changed env vars for printing to stdout later. | |
| 67 old = os.environ.copy() | |
| 68 new = os.environ.copy() | |
| 69 if 'android' in old.get('GOOS', ''): | |
| 70 _get_android_env(new) | |
| 71 | |
| 72 cwd = os.path.dirname(os.path.realpath(__file__)) | |
| 73 if len(sys.argv) == 1: | |
| 74 cmd = [sys.executable, 'env.py'] | |
| 75 print subprocess.check_output(cmd, env=new, cwd=cwd).strip() | |
| 76 for key, value in sorted(new.iteritems()): | |
| 77 if old.get(key) != value: | |
| 78 print 'export %s=%s' % (key, pipes.quote(value)) | |
| 79 else: | |
| 80 cmd = [sys.executable, 'env.py'] | |
| 81 cmd.extend(sys.argv[1:]) | |
| 82 sys.exit(subprocess.call(cmd, env=new, cwd=cwd)) | |
| OLD | NEW |