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 takes linux Go binaries, builds android binaries with NDK tool chain |
| 8 configured with NDK_PLATFORM and NDK_TOOLCHAIN parameters, zips the stuff and |
| 9 uploads it to Google Cloud Storage at gs://mojo/go/tool. It also produces |
| 10 the VERSION file with the sha1 code of the uploaded archive. |
| 11 |
| 12 This script operates in the INSTALL_DIR directory, so it automatically updates |
| 13 your current installation of the go binaries on success. On failure it |
| 14 invalidates your current installation; to fix it, run download.py. |
| 15 |
| 16 In order to use it, you need: |
| 17 - depot_tools in your path |
| 18 - installed android build deps |
| 19 - WRITE access to GCS |
| 20 |
| 21 To update go tool binaries you need to |
| 22 1) run 'gsutil.py config' to update initialize gsutil's credentials |
| 23 2) run this script: python upload.py path/to/go/binaries.tar.gz |
| 24 3) push new version of file 'VERSION' |
| 25 |
| 26 This script doesn't check if current version is already up to date, as the |
| 27 produced tar.gz archive is slightly different every time since it includes |
| 28 timestamps. |
| 29 """ |
| 30 |
| 31 import hashlib |
| 32 import os |
| 33 import shutil |
| 34 import subprocess |
| 35 import sys |
| 36 import tarfile |
| 37 import tempfile |
| 38 |
| 39 NDK_PLATFORM = 'android-14' |
| 40 NDK_TOOLCHAIN = 'arm-linux-androideabi-4.9' |
| 41 |
| 42 # Path constants. (All of these should be absolute paths.) |
| 43 THIS_DIR = os.path.abspath(os.path.dirname(__file__)) |
| 44 MOJO_DIR = os.path.abspath(os.path.join(THIS_DIR, '..', '..')) |
| 45 # Should be the same as in download.py. |
| 46 INSTALL_DIR = os.path.join(MOJO_DIR, 'third_party', 'go', 'tool') |
| 47 |
| 48 sys.path.insert(0, os.path.join(MOJO_DIR, 'tools')) |
| 49 import find_depot_tools |
| 50 |
| 51 DEPOT_PATH = find_depot_tools.add_depot_tools_to_path() |
| 52 GSUTIL_PATH = os.path.join(DEPOT_PATH, 'gsutil.py') |
| 53 |
| 54 def RunCommand(command, env=None): |
| 55 """Run command and return success (True) or failure.""" |
| 56 |
| 57 print 'Running %s' % (str(command)) |
| 58 if subprocess.call(command, shell=False, env=env) == 0: |
| 59 return True |
| 60 print 'Failed.' |
| 61 return False |
| 62 |
| 63 def VersionFileName(): |
| 64 if sys.platform.startswith('linux'): |
| 65 platform_suffix = 'LINUX' |
| 66 elif sys.platform == 'darwin': |
| 67 platform_suffix = 'MACOSX' |
| 68 else: |
| 69 raise Exception('unsupported platform: ' + sys.platform) |
| 70 return 'VERSION_' + platform_suffix |
| 71 |
| 72 def HostArchName(): |
| 73 if sys.platform.startswith('linux'): |
| 74 arch = 'linux_amd64' |
| 75 elif sys.platform == 'darwin': |
| 76 arch = 'darwin_amd64' |
| 77 else: |
| 78 raise Exception('unsupported platform: ' + sys.platform) |
| 79 return arch |
| 80 |
| 81 def ExtractBinaries(archive_path): |
| 82 """Extracts go binaries from the given tar file to INSTALL_DIR.""" |
| 83 |
| 84 if os.path.exists(INSTALL_DIR): |
| 85 shutil.rmtree(INSTALL_DIR) |
| 86 os.mkdir(INSTALL_DIR) |
| 87 archive_path = os.path.abspath(archive_path) |
| 88 with tarfile.open(archive_path) as arch: |
| 89 arch.extractall(INSTALL_DIR) |
| 90 os.rename(os.path.join(INSTALL_DIR, 'go'), |
| 91 os.path.join(INSTALL_DIR, HostArchName())) |
| 92 |
| 93 def BuildGoAndroid(): |
| 94 go_host = os.path.join(INSTALL_DIR, HostArchName()) |
| 95 go_android = os.path.join(INSTALL_DIR, 'android_arm') |
| 96 # Copy go sources and remove binaries to keep only that we generate. |
| 97 shutil.copytree(go_host, go_android) |
| 98 shutil.rmtree(os.path.join(go_android, 'bin')) |
| 99 shutil.rmtree(os.path.join(go_android, 'pkg')) |
| 100 # Prepare the Android NDK tool chain. |
| 101 os.chdir(os.path.join(MOJO_DIR, 'third_party', 'android_tools', 'ndk')) |
| 102 ndk_out_dir = tempfile.mkdtemp(prefix='android_ndk') |
| 103 script_path = os.path.join('build', 'tools', 'make-standalone-toolchain.sh') |
| 104 make_toolchain_cmd = ['bash', script_path, '--platform=%s' % NDK_PLATFORM, |
| 105 '--toolchain=%s' % NDK_TOOLCHAIN, |
| 106 '--install-dir=%s' % ndk_out_dir] |
| 107 if not RunCommand(make_toolchain_cmd): |
| 108 print "Faild to build the Android NDK tool chain." |
| 109 sys.exit(1) |
| 110 # Configure environment variables. |
| 111 cc = os.path.join(ndk_out_dir, 'bin', 'arm-linux-androideabi-gcc') |
| 112 env = os.environ.copy() |
| 113 env["GOROOT"] = go_host |
| 114 env["GOROOT_BOOTSTRAP"] = go_host |
| 115 env["CC_FOR_TARGET"] = '%s' % cc |
| 116 env["CGO_ENABLED"] = '1' |
| 117 env["GOOS"] = 'android' |
| 118 env["GOARCH"] = 'arm' |
| 119 env["GOARM"] = '7' |
| 120 # Build go tool. |
| 121 os.chdir(os.path.join(go_android, 'src')) |
| 122 make_command = ['bash', 'make.bash'] |
| 123 if not RunCommand(make_command, env=env): |
| 124 print "Failed to make go tool for android." |
| 125 sys.exit(1) |
| 126 shutil.rmtree(ndk_out_dir) |
| 127 |
| 128 def Compress(): |
| 129 """Compresses the go tool into tar.gz and generates sha1 code, renames the |
| 130 archive to sha1.tar.gz and returns the sha1 code.""" |
| 131 |
| 132 print "Compressing go tool, this may take several minutes." |
| 133 os.chdir(INSTALL_DIR) |
| 134 with tarfile.open(os.path.join('a.tar.gz'), 'w|gz') as arch: |
| 135 arch.add('android_arm') |
| 136 arch.add(HostArchName()) |
| 137 |
| 138 sha1 = '' |
| 139 with open(os.path.join(INSTALL_DIR, 'a.tar.gz')) as f: |
| 140 sha1 = hashlib.sha1(f.read()).hexdigest() |
| 141 os.rename(os.path.join(INSTALL_DIR, 'a.tar.gz'), |
| 142 os.path.join(INSTALL_DIR, '%s.tar.gz' % sha1)) |
| 143 return sha1 |
| 144 |
| 145 def Upload(sha1): |
| 146 """Uploads INSTALL_DIR/sha1.tar.gz to Google Cloud Storage under |
| 147 gs://mojo/go/tool and writes sha1 to THIS_DIR/VERSION.""" |
| 148 |
| 149 file_name = '%s.tar.gz' % sha1 |
| 150 upload_cmd = ['python', GSUTIL_PATH, 'cp', |
| 151 '-n', # Do not upload if the file already exists. |
| 152 os.path.join(INSTALL_DIR, file_name), |
| 153 'gs://mojo/go/tool/%s' % file_name] |
| 154 |
| 155 print "Uploading go tool to GCS." |
| 156 if not RunCommand(upload_cmd): |
| 157 print "Failed to upload go tool to GCS." |
| 158 sys.exit(1) |
| 159 os.remove(os.path.join(INSTALL_DIR, file_name)) |
| 160 # Write versions as the last step. |
| 161 stamp_file = os.path.join(THIS_DIR, VersionFileName()) |
| 162 with open(stamp_file, 'w+') as stamp: |
| 163 stamp.write('%s\n' % sha1) |
| 164 |
| 165 stamp_file = os.path.join(INSTALL_DIR, VersionFileName()) |
| 166 with open(stamp_file, 'w+') as stamp: |
| 167 stamp.write('%s\n' % sha1) |
| 168 |
| 169 def main(): |
| 170 ExtractBinaries(sys.argv[1]) |
| 171 BuildGoAndroid() |
| 172 sha1 = Compress() |
| 173 Upload(sha1) |
| 174 print "Done." |
| 175 |
| 176 if __name__ == '__main__': |
| 177 sys.exit(main()) |
OLD | NEW |