|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0. |
| 3 | + |
| 4 | +import os |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +from builder.core.fetch import fetch_and_extract, mirror_package |
| 8 | +from builder.core.project import Import |
| 9 | +import builder.core.util as util |
| 10 | +from builder.core.host import current_platform |
| 11 | + |
| 12 | +URLs = { |
| 13 | + 'linux-armv6': 'https://go.dev/dl/go1.21.5.linux-armv6l.tar.gz', |
| 14 | + 'linux-armv7': 'https://go.dev/dl/go1.21.5.linux-armv6l.tar.gz', |
| 15 | + 'linux-armv8': 'https://go.dev/dl/go1.21.5.linux-arm64.tar.gz', |
| 16 | + 'linux-x86': 'https://go.dev/dl/go1.21.5.linux-386.tar.gz', |
| 17 | + 'linux-x64': 'https://go.dev/dl/go1.21.5.linux-amd64.tar.gz', |
| 18 | + 'openbsd-x64': 'https://go.dev/dl/go1.21.5.linux-amd64.tar.gz', |
| 19 | + 'windows-x64': 'https://go.dev/dl/go1.21.5.windows-amd64.zip', |
| 20 | + 'windows-x86': 'https://go.dev/dl/go1.21.5.windows-386.zip', |
| 21 | + 'macos-x64': 'https://go.dev/dl/go1.21.5.darwin-amd64.tar.gz', |
| 22 | +} |
| 23 | + |
| 24 | + |
| 25 | +class GOLANG(Import): |
| 26 | + def __init__(self, **kwargs): |
| 27 | + super().__init__( |
| 28 | + config={}, |
| 29 | + **kwargs) |
| 30 | + self.path = None |
| 31 | + self.installed = False |
| 32 | + |
| 33 | + def resolved(self): |
| 34 | + return True |
| 35 | + |
| 36 | + def install(self, env): |
| 37 | + if self.installed: |
| 38 | + return |
| 39 | + |
| 40 | + sh = env.shell |
| 41 | + |
| 42 | + target = '{}-{}'.format(env.spec.target, env.spec.arch) |
| 43 | + |
| 44 | + cross_compile = util.deep_get(env, 'toolchain.cross_compile', False) |
| 45 | + |
| 46 | + # If this is a local build, check the local machine |
| 47 | + if not cross_compile or target not in URLs: |
| 48 | + # run `go version` |
| 49 | + result = util.run_command('go', 'version') |
| 50 | + if result.returncode == 0: |
| 51 | + # check the version, we need version >=1.18 |
| 52 | + version_str = result.output.split(" ")[2][2:] |
| 53 | + version_numbers = list(map(int, version_str.split('.'))) |
| 54 | + compare_version_numbers = list(map(int, "1.18.0".split('.'))) |
| 55 | + if version_numbers >= compare_version_numbers: |
| 56 | + return |
| 57 | + |
| 58 | + if target not in URLs: |
| 59 | + raise EnvironmentError( |
| 60 | + 'No pre-built binaries for {} are available, please install golang greater than 1.18'.format(target)) |
| 61 | + |
| 62 | + install_dir = os.path.join(env.deps_dir, self.name.lower()) |
| 63 | + # If path is going to be relative, it has to be relative to the source directory |
| 64 | + self.path = str(Path(install_dir).relative_to(env.root_dir)) |
| 65 | + print('Installing pre-built golang binaries for {} to {}'.format( |
| 66 | + target, install_dir)) |
| 67 | + |
| 68 | + sh.mkdir(install_dir) |
| 69 | + if cross_compile: |
| 70 | + # If cross compile using the go execuble for current platform instead to codegen |
| 71 | + url = URLs[current_platform()] |
| 72 | + else: |
| 73 | + url = URLs[target] |
| 74 | + ext = '.tar.gz' if url.endswith('.tar.gz') else '.zip' |
| 75 | + filename = '{}/golang{}'.format(install_dir, ext) |
| 76 | + print('Downloading {}'.format(url)) |
| 77 | + fetch_and_extract(url, filename, install_dir) |
| 78 | + os.remove(filename) |
| 79 | + |
| 80 | + # Set PATH |
| 81 | + if cross_compile: |
| 82 | + # Path to go binary |
| 83 | + env.variables['go_path'] = "/work/"+str(Path(os.path.join(install_dir, 'go/bin') |
| 84 | + ).relative_to(env.root_dir)) |
| 85 | + else: |
| 86 | + # export the PATH directly if not cross compile. |
| 87 | + # env.variables['go_path'] = '{}/go/bin'.format(install_dir) |
| 88 | + sh.setenv('PATH', '{}{}{}'.format('{}/go/bin'.format(install_dir), os.pathsep, sh.getenv('PATH'))) |
| 89 | + |
| 90 | + self.installed = True |
| 91 | + |
| 92 | + def mirror(self, env): |
| 93 | + for src_url in URLs.values(): |
| 94 | + mirror_package(self.name, src_url) |
0 commit comments