Skip to content

Commit

Permalink
install go with builder
Browse files Browse the repository at this point in the history
  • Loading branch information
TingDaoK committed Jan 8, 2024
1 parent 62a1729 commit 2aa26dc
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
2 changes: 2 additions & 0 deletions builder/imports/awslc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, **kwargs):
del kwargs['name']
super().__init__(
library=True,
imports=['golang'],
account='aws',
name='aws-lc',
config=config,
Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(self, **kwargs):
del kwargs['name']
super().__init__(
account='aws',
imports=['golang'],
name='aws-lc',
**config,
**kwargs)
Expand Down
77 changes: 77 additions & 0 deletions builder/imports/golang.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0.

import os
from pathlib import Path
from packaging import version

from builder.core.fetch import fetch_and_extract, mirror_package
from builder.core.project import Import
import builder.core.util as util

# Only linux versions for now
URLs = {
'linux-armv6': 'https://go.dev/dl/go1.21.5.linux-armv6l.tar.gz',
'linux-armv7': 'https://go.dev/dl/go1.21.5.linux-armv6l.tar.gz',
'linux-armv8': 'https://go.dev/dl/go1.21.5.linux-arm64.tar.gz',
'linux-x86': 'https://go.dev/dl/go1.21.5.linux-386.tar.gz',
'linux-x64': 'https://go.dev/dl/go1.21.5.linux-amd64.tar.gz',
}


class GOLANG(Import):
def __init__(self, **kwargs):
super().__init__(
config={},
**kwargs)
self.path = None
self.installed = False

def resolved(self):
return True

def install(self, env):
if self.installed:
return

sh = env.shell

target = '{}-{}'.format(env.spec.target, env.spec.arch)

cross_compile = util.deep_get(env, 'toolchain.cross_compile', False)

# If this is a local build, check the local machine
if not cross_compile or target not in URLs:
# run `go version`
result = util.run_command('go', 'version')
if result.returncode == 0:
# check the version, we need version >=1.18
version_str = result.output.split(" ")[2][2:]
if version.parse(version_str) >= version.parse("1.18.0"):
return

if target not in URLs:
raise EnvironmentError(
'No pre-built binaries for {} are available, please install golang greater than 1.18'.format(target))

install_dir = os.path.join(env.deps_dir, self.name.lower())
# If path is going to be relative, it has to be relative to the source directory
self.path = str(Path(install_dir).relative_to(env.root_dir))
print('Installing pre-built golang binaries for {} to {}'.format(
target, install_dir))

sh.mkdir(install_dir)
url = URLs[target]
ext = '.tar.gz' if url.endswith('.tar.gz') else '.zip'
filename = '{}/golang{}'.format(install_dir, ext)
print('Downloading {}'.format(url))
fetch_and_extract(url, filename, install_dir)
os.remove(filename)

# Set PATH
go_path = '{}/go/bin'.format(install_dir)
sh.setenv('PATH', '{}{}{}'.format(go_path, os.pathsep, sh.getenv('PATH')))

def mirror(self, env):
for src_url in URLs.values():
mirror_package(self.name, src_url)

0 comments on commit 2aa26dc

Please sign in to comment.