Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(137)

Side by Side Diff: go/mobile_env.py

Issue 2226873002: Add support for building Go for Android. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Switch to using the sdk in gomobile. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 import os
12 import pipes
13 import platform
14 import subprocess
15 import sys
16
17
18 ANDROID_NDK_PATH = os.path.join(
19 os.path.abspath(os.path.dirname(__file__)), '.vendor', 'pkg', 'gomobile',
20 'android-ndk-r12b')
iannucci 2016/08/10 01:40:36 is this path variable? Is there a way we could ext
bpastene 2016/08/10 22:56:06 It would probably change as frequently as chromium
21
22 # Used for mapping GOARCH values to NDK toolsets.
23 ANDROID_TOOLSETS = {
24 'arm': 'arm',
25 'arm64': 'aarch64',
26 '386': 'x86',
27 'amd64': 'x86_64',
28 }
29
30
31 def _get_android_env(env):
32 """Adds GO* and C* environment variables needed for building for android."""
33 if platform.system() != 'Linux':
34 raise Exception(
35 'Only Linux hosts supported for android cross-compiling')
36
37 arch = env.get('GOARCH')
iannucci 2016/08/10 01:40:35 does env.py set this? If not, this would be a very
bpastene 2016/08/10 22:56:06 I'd say GOARCH is a very typical thing to have in
38 if arch not in ANDROID_TOOLSETS:
39 raise Exception(
40 'Specified arch not currently supported on android: %s' % arch)
41
42 toolset = ANDROID_TOOLSETS[arch]
43
44 # Needed when cross-compiling uses of cgo.
45 env['CGO_ENABLED'] = '1'
46 env['CC'] = os.path.join(
47 ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang' % toolset)
48 env['CXX'] = os.path.join(
49 ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang++' % toolset)
50
51 # Compiler/linker needs access to android system headers in the NDK.
52 sysroot_path = os.path.join(ANDROID_NDK_PATH, arch, 'sysroot')
53 env['CGO_CFLAGS'] = '-I %s/usr/include --sysroot %s' % (
54 sysroot_path, sysroot_path)
55 env['CGO_LDFLAGS'] = '-L %s/usr/lib --sysroot %s' % (
56 sysroot_path, sysroot_path)
57
58
59 # Run `gomobile init` to fetch the android NDK.
60 if not os.path.exists(ANDROID_NDK_PATH):
iannucci 2016/08/10 01:40:35 This should be in a `main` function with the `if _
Vadim Sh. 2016/08/10 01:51:01 env.py does "assert __name__ == '__main__'" on top
bpastene 2016/08/10 22:56:06 Added it here anyway.
61 cmd = [sys.executable, 'env.py', 'gomobile', 'init']
62 subprocess.check_call(cmd)
63
64 # Keep track of any changed env vars for printing to stdout later.
65 old = os.environ.copy()
66 new = os.environ.copy()
67 if 'android' in old.get('GOOS', ''):
68 _get_android_env(new)
69
70 if len(sys.argv) == 1:
71 cmd = [sys.executable, 'env.py']
72 print subprocess.check_output(cmd, env=new).strip()
iannucci 2016/08/10 01:40:35 we should set cwd == our directory here.
bpastene 2016/08/10 22:56:06 Done.
73 for key, value in sorted(new.iteritems()):
74 if old.get(key) != value:
75 print 'export %s=%s' % (key, pipes.quote(value))
76 else:
77 cmd = [sys.executable, 'env.py']
78 cmd.extend(sys.argv[1:])
79 sys.exit(subprocess.call(cmd, env=new))
OLDNEW
« go/deps.lock ('K') | « go/deps.yaml ('k') | go/src/infra/tools/device_watchdog/main.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698