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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: go/mobile_env.py
diff --git a/go/mobile_env.py b/go/mobile_env.py
new file mode 100755
index 0000000000000000000000000000000000000000..7fab6d4fa9112918962296fe292d67373739448d
--- /dev/null
+++ b/go/mobile_env.py
@@ -0,0 +1,79 @@
+#!/usr/bin/env python
+# Copyright 2014 The Chromium Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
+
+"""Like env.py, but for building go-on-mobile.
+
+It wraps env.py, runs `gomobile init`, and adds mobile-specific env vars.
+"""
+
+import os
+import pipes
+import platform
+import subprocess
+import sys
+
+
+ANDROID_NDK_PATH = os.path.join(
+ os.path.abspath(os.path.dirname(__file__)), '.vendor', 'pkg', 'gomobile',
+ '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
+
+# Used for mapping GOARCH values to NDK toolsets.
+ANDROID_TOOLSETS = {
+ 'arm': 'arm',
+ 'arm64': 'aarch64',
+ '386': 'x86',
+ 'amd64': 'x86_64',
+}
+
+
+def _get_android_env(env):
+ """Adds GO* and C* environment variables needed for building for android."""
+ if platform.system() != 'Linux':
+ raise Exception(
+ 'Only Linux hosts supported for android cross-compiling')
+
+ 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
+ if arch not in ANDROID_TOOLSETS:
+ raise Exception(
+ 'Specified arch not currently supported on android: %s' % arch)
+
+ toolset = ANDROID_TOOLSETS[arch]
+
+ # Needed when cross-compiling uses of cgo.
+ env['CGO_ENABLED'] = '1'
+ env['CC'] = os.path.join(
+ ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang' % toolset)
+ env['CXX'] = os.path.join(
+ ANDROID_NDK_PATH, arch, 'bin', '%s-linux-androideabi-clang++' % toolset)
+
+ # Compiler/linker needs access to android system headers in the NDK.
+ sysroot_path = os.path.join(ANDROID_NDK_PATH, arch, 'sysroot')
+ env['CGO_CFLAGS'] = '-I %s/usr/include --sysroot %s' % (
+ sysroot_path, sysroot_path)
+ env['CGO_LDFLAGS'] = '-L %s/usr/lib --sysroot %s' % (
+ sysroot_path, sysroot_path)
+
+
+# Run `gomobile init` to fetch the android NDK.
+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.
+ cmd = [sys.executable, 'env.py', 'gomobile', 'init']
+ subprocess.check_call(cmd)
+
+# Keep track of any changed env vars for printing to stdout later.
+old = os.environ.copy()
+new = os.environ.copy()
+if 'android' in old.get('GOOS', ''):
+ _get_android_env(new)
+
+if len(sys.argv) == 1:
+ cmd = [sys.executable, 'env.py']
+ 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.
+ for key, value in sorted(new.iteritems()):
+ if old.get(key) != value:
+ print 'export %s=%s' % (key, pipes.quote(value))
+else:
+ cmd = [sys.executable, 'env.py']
+ cmd.extend(sys.argv[1:])
+ sys.exit(subprocess.call(cmd, env=new))
« 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