Update build system

This commit is contained in:
Isaac 2024-11-05 15:33:06 +01:00
parent 6e414bd747
commit 095b068f63
16 changed files with 110 additions and 7 deletions

View File

@ -1,10 +1,41 @@
import os
import stat
import sys
from urllib.parse import urlparse, urlunparse
import tempfile
import hashlib
import shutil
from BuildEnvironment import is_apple_silicon, resolve_executable, call_executable, BuildEnvironmentVersions
from BuildEnvironment import is_apple_silicon, resolve_executable, call_executable, run_executable_with_status, BuildEnvironmentVersions
def locate_bazel(base_path):
def transform_cache_host_into_http(grpc_url):
parsed_url = urlparse(grpc_url)
new_scheme = "http"
new_port = 8080
transformed_url = urlunparse((
new_scheme,
f"{parsed_url.hostname}:{new_port}",
parsed_url.path,
parsed_url.params,
parsed_url.query,
parsed_url.fragment
))
return transformed_url
def calculate_sha256(file_path):
sha256_hash = hashlib.sha256()
with open(file_path, "rb") as file:
# Read the file in chunks to avoid using too much memory
for byte_block in iter(lambda: file.read(4096), b""):
sha256_hash.update(byte_block)
return sha256_hash.hexdigest()
def locate_bazel(base_path, cache_host):
build_input_dir = '{}/build-input'.format(base_path)
if not os.path.isdir(build_input_dir):
os.mkdir(build_input_dir)
@ -17,6 +48,33 @@ def locate_bazel(base_path):
bazel_name = 'bazel-{version}-{arch}'.format(version=versions.bazel_version, arch=arch)
bazel_path = '{}/build-input/{}'.format(base_path, bazel_name)
if not os.path.isfile(bazel_path):
if cache_host is not None and versions.bazel_version_sha256 is not None:
http_cache_host = transform_cache_host_into_http(cache_host)
with tempfile.NamedTemporaryFile(delete=True) as temp_output_file:
call_executable([
'curl',
'-L',
'{cache_host}/cache/cas/{hash}'.format(
cache_host=http_cache_host,
hash=versions.bazel_version_sha256
),
'--output',
temp_output_file.name
], check_result=False)
test_sha256 = calculate_sha256(temp_output_file.name)
if test_sha256 == versions.bazel_version_sha256:
shutil.copyfile(temp_output_file.name, bazel_path)
if os.path.isfile(bazel_path) and versions.bazel_version_sha256 is not None:
test_sha256 = calculate_sha256(bazel_path)
if test_sha256 != versions.bazel_version_sha256:
print(f"Bazel at {bazel_path} does not match SHA256 {versions.bazel_version_sha256}, removing")
os.remove(bazel_path)
if not os.path.isfile(bazel_path):
call_executable([
'curl',
@ -29,6 +87,27 @@ def locate_bazel(base_path):
bazel_path
])
if os.path.isfile(bazel_path) and versions.bazel_version_sha256 is not None:
test_sha256 = calculate_sha256(bazel_path)
if test_sha256 != versions.bazel_version_sha256:
print(f"Bazel at {bazel_path} does not match SHA256 {versions.bazel_version_sha256}, removing")
os.remove(bazel_path)
if cache_host is not None and versions.bazel_version_sha256 is not None:
http_cache_host = transform_cache_host_into_http(cache_host)
print(f"Uploading bazel@{versions.bazel_version_sha256} to bazel-remote")
call_executable([
'curl',
'-X',
'PUT',
'-T',
bazel_path,
'{cache_host}/cache/cas/{hash}'.format(
cache_host=http_cache_host,
hash=versions.bazel_version_sha256
)
], check_result=False)
if not os.access(bazel_path, os.X_OK):
st = os.stat(bazel_path)
os.chmod(bazel_path, st.st_mode | stat.S_IEXEC)

View File

@ -62,7 +62,7 @@ class BuildConfiguration:
def build_configuration_from_json(path):
if not os.path.exists(path):
print('Could not load build configuration from {}'.format(path))
print('Could not load build configuration from non-existing path {}'.format(path))
sys.exit(1)
with open(path) as file:
configuration_dict = json.load(file)

View File

@ -65,6 +65,28 @@ def run_executable_with_output(path, arguments, decode=True, input=None, stderr_
return output_data
def run_executable_with_status(arguments, use_clean_environment=True):
executable_path = resolve_executable(arguments[0])
if executable_path is None:
raise Exception(f'Could not resolve {arguments[0]} to a valid executable file')
if use_clean_environment:
resolved_env = get_clean_env()
else:
resolved_env = os.environ
resolved_arguments = [executable_path] + arguments[1:]
result = subprocess.run(
resolved_arguments,
env=resolved_env,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
return result.returncode
def call_executable(arguments, use_clean_environment=True, check_result=True):
executable_path = resolve_executable(arguments[0])
if executable_path is None:
@ -135,7 +157,9 @@ class BuildEnvironmentVersions:
if configuration_dict['bazel'] is None:
raise Exception('Missing bazel version in {}'.format(configuration_path))
else:
self.bazel_version = configuration_dict['bazel']
bazel_version, bazel_version_sha256 = configuration_dict['bazel'].split(':')
self.bazel_version = bazel_version
self.bazel_version_sha256 = bazel_version_sha256
if configuration_dict['xcode'] is None:
raise Exception('Missing xcode version in {}'.format(configuration_path))
else:

View File

@ -985,7 +985,7 @@ if __name__ == '__main__':
bazel_path = None
if args.bazel is None:
bazel_path = locate_bazel(base_path=os.getcwd())
bazel_path = locate_bazel(base_path=os.getcwd(), cache_host=args.cacheHost)
else:
bazel_path = args.bazel

View File

@ -161,7 +161,7 @@ def remote_build(darwin_containers_path, darwin_containers_host, macos_version,
sys.exit(1)
DarwinContainers.run_remote_ssh(credentials=credentials, command='')
sys.exit(0)
#sys.exit(0)
def handle_stopped():
pass

View File

@ -1,6 +1,6 @@
{
"app": "11.3",
"xcode": "16.0",
"bazel": "7.3.1",
"bazel": "7.3.1:981f82a470bad1349322b6f51c9c6ffa0aa291dab1014fac411543c12e661dff",
"macos": "15.0"
}