blob: 319c94cf53ecd358ddf7330a65c725295cdd6cd0 [file] [log] [blame]
#!/usr/bin/env vpython3
# 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, but for building go-on-mobile.
It wraps, runs `gomobile init`, and adds mobile-specific env vars.
assert __name__ == '__main__'
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',
# Used for mapping GOARCH values to (NDK toolset directory, compiler name)
'arm': ('arm', 'arm-linux-androideabi-clang'),
'arm64': ('arm64', 'aarch64-linux-android-clang'),
'386': ('x86', 'i686-linux-android-clang'),
'amd64': ('x86_64', 'x86_64-linux-android-clang'),
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')
if arch not in ANDROID_TOOLSETS:
raise Exception(
'Specified arch not currently supported on android: %s' % arch)
toolset_dir, compiler = ANDROID_TOOLSETS[arch]
# Needed when cross-compiling uses of cgo.
env['CGO_ENABLED'] = '1'
env['CC'] = os.path.join(
ANDROID_NDK_PATH, toolset_dir, 'bin', compiler)
env['CXX'] = os.path.join(
ANDROID_NDK_PATH, toolset_dir, 'bin', compiler + '++')
# Compiler/linker needs access to android system headers in the NDK.
sysroot_path = os.path.join(ANDROID_NDK_PATH, toolset_dir, '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.
cwd = os.path.dirname(os.path.realpath(__file__))
if not os.path.exists(ANDROID_NDK_PATH):
cmd = [sys.executable, '', 'gomobile', 'init']
subprocess.check_call(cmd, cwd=cwd)
# 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', ''):
cwd = os.path.dirname(os.path.realpath(__file__))
if len(sys.argv) == 1:
cmd = [sys.executable, '']
print(subprocess.check_output(cmd, env=new, cwd=cwd).strip())
for key, value in sorted(new.iteritems()):
if old.get(key) != value:
print('export %s=%s' % (key, pipes.quote(value)))
cmd = [sys.executable, '']
sys.exit(, env=new, cwd=cwd))