Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #!/usr/bin/python | |
| 2 # Copyright 2015 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 """ | |
| 7 This script downloads GO_VERSION linux binaries from golang.org, build android | |
| 8 binaries with NDK tool chain configured with NDK_PLATFORM and NDK_TOOLCHAIN | |
| 9 parameters, zips the stuff and uploads it to Google Cloud Storage at | |
| 10 gs://mojo/go/tool. It also produces VERSION file with sha1 code of the uploaded | |
| 11 archive. | |
| 12 | |
| 13 In order to use it, you need: | |
| 14 - depot_tools in your path | |
| 15 - installed android build deps | |
| 16 - WRITE access to GCS | |
| 17 | |
| 18 To update go tool binaries you need to | |
| 19 1) run 'gsutil.py config' to update initialize gsutil's credentials | |
| 20 2) run this script | |
| 21 3) push new version of file 'VERSION' | |
| 22 | |
| 23 This script doesn't check if current version is already up to date, as the | |
| 24 produced tar.gz archive is slightly different every time since it includes | |
| 25 timestamps. | |
| 26 """ | |
| 27 | |
| 28 import hashlib | |
| 29 import os | |
| 30 import shutil | |
| 31 import subprocess | |
| 32 import sys | |
| 33 import tarfile | |
| 34 import tempfile | |
| 35 | |
| 36 GO_VERSION = 'go1.4.2' | |
| 37 NDK_PLATFORM = 'android-14' | |
| 38 NDK_TOOLCHAIN = 'arm-linux-androideabi-4.9' | |
| 39 | |
| 40 # Path constants. (All of these should be absolute paths.) | |
| 41 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) | |
| 42 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..')) | |
| 43 | |
| 44 | |
| 45 def RunCommand(command, env=os.environ): | |
|
viettrungluu
2015/04/08 21:54:19
The default value for subprocess.call/Popen's env
rogulenko
2015/04/08 22:33:19
Done.
| |
| 46 """Run command and return success (True) or failure.""" | |
| 47 | |
| 48 print 'Running %s' % (str(command)) | |
| 49 if subprocess.call(command, shell=False, env=env) == 0: | |
| 50 return True | |
| 51 print 'Failed.' | |
| 52 return False | |
| 53 | |
| 54 def DownloadGoLinux(tmpdir): | |
| 55 """Downloads and go linux_amd64 go binaries from golang.org and extracts them | |
| 56 to provided tmpdir. The extracted files appear under | |
| 57 tmpdir/tool/linux_amd64.""" | |
| 58 | |
| 59 archive_name = '%s.linux-amd64.tar.gz' % GO_VERSION | |
| 60 tool_dir = os.path.join(tmpdir, 'tool') | |
| 61 os.mkdir(tool_dir) | |
| 62 archive_path = os.path.join(tool_dir, archive_name) | |
| 63 # Download the Linux x64 Go binaries from golang.org. | |
| 64 # '-C -': Resume transfer if possible. | |
| 65 # '--location': Follow Location: redirects. | |
| 66 # '-o': Output file. | |
| 67 curl_command = ['curl', | |
| 68 '-C', '-', | |
| 69 '--location', | |
| 70 '-o', archive_path, | |
| 71 'http://golang.org/dl/%s' % archive_name] | |
| 72 if not RunCommand(curl_command): | |
| 73 print "Failed to linux go binaries from server." | |
| 74 sys.exit(1) | |
| 75 with tarfile.open(archive_path) as arch: | |
| 76 arch.extractall(tool_dir) | |
| 77 os.remove(archive_path) | |
| 78 os.rename(os.path.join(tool_dir, 'go'), | |
| 79 os.path.join(tool_dir, 'linux_amd64')) | |
| 80 | |
| 81 def ConfigureAndroidNDK(tmpdir): | |
| 82 """Creates a standalone tool chain under tmpdir. Returns path to ndk cc | |
| 83 compiler.""" | |
| 84 | |
| 85 ndk_root = os.path.join(tmpdir, 'ndk-toolchain') | |
| 86 os.chdir(os.path.join(MOJO_DIR, 'third_party', 'android_tools', 'ndk')) | |
| 87 script = os.path.join('build', 'tools', 'make-standalone-toolchain.sh') | |
| 88 make_command = ['bash', script, | |
| 89 '--platform=%s' % NDK_PLATFORM, | |
| 90 '--toolchain=%s' % NDK_TOOLCHAIN, | |
| 91 '--install-dir=%s' % ndk_root] | |
| 92 if not RunCommand(make_command): | |
| 93 print "Failed to generate NDK tool chain." | |
| 94 sys.exit(1) | |
| 95 return os.path.join(ndk_root, 'bin', 'arm-linux-androideabi-gcc') | |
| 96 | |
| 97 def BuildGoAndroid(tmpdir): | |
| 98 ndk_cc = ConfigureAndroidNDK(tmpdir) | |
| 99 go_linux = os.path.join(tmpdir, 'tool', 'linux_amd64') | |
| 100 go_android = os.path.join(tmpdir, 'tool', 'android_arm') | |
| 101 # Copy go sources and remove binaries to keep only that we generate. | |
| 102 shutil.copytree(go_linux, go_android) | |
| 103 shutil.rmtree(os.path.join(go_android, 'bin')) | |
| 104 shutil.rmtree(os.path.join(go_android, 'pkg')) | |
| 105 # Configure environment variables. | |
| 106 env = os.environ.copy() | |
| 107 env["GOPATH"] = go_linux | |
| 108 env["GOROOT"] = go_linux | |
| 109 env["CC_FOR_TARGET"] = ndk_cc | |
| 110 env["GOOS"] = 'android' | |
| 111 env["GOARCH"] = 'arm' | |
| 112 env["GOARM"] = '7' | |
| 113 # Build go tool. | |
| 114 os.chdir(os.path.join(go_android, 'src')) | |
| 115 make_command = ['bash', 'make.bash'] | |
| 116 if not RunCommand(make_command, env=env): | |
| 117 print "Failed to make go tool for android." | |
| 118 sys.exit(1) | |
| 119 | |
| 120 def Compress(tmpdir): | |
| 121 """Compresses the go tool into tar.gz and generates sha1 code, renames the | |
| 122 archive to sha1.tar.gz and returns the sha1 code.""" | |
| 123 | |
| 124 print "Compressing go tool, this may take several minutes." | |
| 125 os.chdir(os.path.join(tmpdir, 'tool')) | |
| 126 with tarfile.open(os.path.join('..', 'a.tar.gz'), 'w|gz') as arch: | |
| 127 arch.add('android_arm') | |
| 128 arch.add('linux_amd64') | |
| 129 | |
| 130 sha1 = '' | |
| 131 with open(os.path.join(tmpdir, 'a.tar.gz')) as f: | |
| 132 sha1 = hashlib.sha1(f.read()).hexdigest() | |
| 133 os.rename(os.path.join(tmpdir, 'a.tar.gz'), | |
| 134 os.path.join(tmpdir, '%s.tar.gz' % sha1)) | |
| 135 return sha1 | |
| 136 | |
| 137 def Upload(tmpdir, sha1): | |
| 138 """Uploads tmpdir/sha1.tar.gz to Google Cloud Storage under gs://mojo/go/tool | |
| 139 and writes sha1 to THIS_DIR/VERSION.""" | |
| 140 | |
| 141 stamp_file = os.path.join(THIS_DIR, 'VERSION') | |
| 142 with open(stamp_file, 'w+') as stamp: | |
| 143 stamp.write('%s\n' % sha1) | |
| 144 file_name = '%s.tar.gz' % sha1 | |
| 145 upload_cmd = ['gsutil.py', 'cp', | |
| 146 '-n', # Do not upload if the file already exists. | |
| 147 os.path.join(tmpdir, file_name), | |
| 148 'gs://mojo/go/tool/%s' % file_name] | |
| 149 | |
| 150 print "Uploading go tool to GCS." | |
| 151 if not RunCommand(upload_cmd): | |
| 152 print "Failed to upload go tool to GCS." | |
| 153 sys.exit(1) | |
| 154 | |
| 155 def main(): | |
| 156 tmpdir = tempfile.mkdtemp(prefix='mojo') | |
| 157 print "Created temp dir %s" % tmpdir | |
| 158 DownloadGoLinux(tmpdir) | |
| 159 BuildGoAndroid(tmpdir) | |
| 160 sha1 = Compress(tmpdir) | |
| 161 Upload(tmpdir, sha1) | |
| 162 shutil.rmtree(tmpdir) | |
| 163 print "Done." | |
| 164 | |
| 165 if __name__ == '__main__': | |
| 166 sys.exit(main()) | |
| OLD | NEW |