blob: a04c101ca64576f44b6f55ba99d1078ba946a475 [file] [log] [blame]
Chong Gu26d45742022-06-10 14:49:171#!/usr/bin/env python3
Avi Drissman73a09d12022-09-08 20:33:382# Copyright 2022 The Chromium Authors
Chong Gu26d45742022-06-10 14:49:173# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5"""Check out the Fuchsia SDK from a given GCS path. Should be used in a
6'hooks_os' entry so that it only runs when .gclient's custom_vars includes
7'fuchsia'."""
8
9import argparse
Chong Gu679a31b2023-07-18 00:06:3910import json
Chong Gu26d45742022-06-10 14:49:1711import logging
Chong Gu6d068f42022-07-18 21:13:4412import os
Chong Gu26d45742022-06-10 14:49:1713import platform
Chong Gucb84f3802022-09-23 14:39:5714import subprocess
Chong Gu26d45742022-06-10 14:49:1715import sys
Andrew Mellen44a4a812022-10-10 16:24:0116from typing import Optional
Chong Gu26d45742022-06-10 14:49:1717
Andrew Mellen44a4a812022-10-10 16:24:0118from gcs_download import DownloadAndUnpackFromCloudStorage
Chong Gu26d45742022-06-10 14:49:1719
Chong Gu16f1add2023-03-20 18:51:1820sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__),
21 'test')))
22
Chong Gu619775d22023-06-07 17:36:4523from common import SDK_ROOT, get_host_os, make_clean_directory
24
Chong Gu679a31b2023-07-18 00:06:3925_VERSION_FILE = os.path.join(SDK_ROOT, 'meta', 'manifest.json')
Chong Gu16f1add2023-03-20 18:51:1826
Chong Gu26d45742022-06-10 14:49:1727
28def _GetHostArch():
29 host_arch = platform.machine()
30 # platform.machine() returns AMD64 on 64-bit Windows.
31 if host_arch in ['x86_64', 'AMD64']:
32 return 'amd64'
33 elif host_arch == 'aarch64':
34 return 'arm64'
35 raise Exception('Unsupported host architecture: %s' % host_arch)
36
37
Hzj_jie88585f22024-01-10 21:31:1138def GetSDKOverrideGCSPath() -> Optional[str]:
Hzj_jie07076132024-03-06 20:12:2639 """Fetches the sdk override path from a file or an environment variable.
Andrew Mellen44a4a812022-10-10 16:24:0140
Andrew Mellen44a4a812022-10-10 16:24:0141 Returns:
Hzj_jie07076132024-03-06 20:12:2642 The override sdk location, stripped of white space.
Andrew Mellen44a4a812022-10-10 16:24:0143 Example: gs://fuchsia-artifacts/development/some-id/sdk
44 """
Hzj_jie07076132024-03-06 20:12:2645 if os.getenv('FUCHSIA_SDK_OVERRIDE'):
46 return os.environ['FUCHSIA_SDK_OVERRIDE'].strip()
47
Hzj_jie88585f22024-01-10 21:31:1148 path = os.path.join(os.path.dirname(__file__), 'sdk_override.txt')
Andrew Mellen44a4a812022-10-10 16:24:0149
Hzj_jie07076132024-03-06 20:12:2650 if os.path.isfile(path):
51 with open(path, 'r') as f:
52 return f.read().strip()
Andrew Mellen44a4a812022-10-10 16:24:0153
Hzj_jie07076132024-03-06 20:12:2654 return None
Andrew Mellen44a4a812022-10-10 16:24:0155
56
Hzj_jie89357612024-02-23 23:59:3457def _GetCurrentVersionFromManifest() -> Optional[str]:
Wez261b5972024-02-22 13:05:5858 if not os.path.exists(_VERSION_FILE):
59 return None
60 with open(_VERSION_FILE) as f:
YJ Wanga6aeeddc42025-02-04 02:50:5261 try:
62 data = json.load(f)
63 except json.decoder.JSONDecodeError:
Zijie He79897082025-06-16 23:59:4164 logging.warning(
65 'manifest.json is not at the JSON format and may be empty.')
YJ Wanga6aeeddc42025-02-04 02:50:5266 return None
67 if 'id' not in data:
68 logging.warning('The key "id" does not exist in manifest.json')
Aaron Knobloch14451662025-01-17 00:21:5569 return None
70 return data['id']
Wez261b5972024-02-22 13:05:5871
72
Chong Gu26d45742022-06-10 14:49:1773def main():
74 parser = argparse.ArgumentParser()
Chong Gucb84f3802022-09-23 14:39:5775 parser.add_argument('--cipd-prefix', help='CIPD base directory for the SDK.')
76 parser.add_argument('--version', help='Specifies the SDK version.')
Chong Gu26d45742022-06-10 14:49:1777 parser.add_argument('--verbose',
78 '-v',
79 action='store_true',
80 help='Enable debug-level logging.')
Zijie He79897082025-06-16 23:59:4181 parser.add_argument('--ignore-gen-build-defs',
82 action='store_true',
83 help='Do not run gen_build_defs.py.')
Hzj_jie3cf2b8c2024-08-07 18:30:4184 parser.add_argument(
85 '--file',
86 help='Specifies the sdk tar.gz file name without .tar.gz suffix',
87 default='core')
Chong Gu26d45742022-06-10 14:49:1788 args = parser.parse_args()
89
90 logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
91
92 # Exit if there's no SDK support for this platform.
93 try:
Chong Gu16f1add2023-03-20 18:51:1894 host_plat = get_host_os()
Chong Gu26d45742022-06-10 14:49:1795 except:
96 logging.warning('Fuchsia SDK is not supported on this platform.')
97 return 0
98
Wez261b5972024-02-22 13:05:5899 # TODO(crbug.com/326004432): Remove this once DEPS have been fixed not to
100 # include the "version:" prefix.
101 if args.version.startswith('version:'):
102 args.version = args.version[len('version:'):]
103
Andrew Mellen44a4a812022-10-10 16:24:01104 gcs_tarball_prefix = GetSDKOverrideGCSPath()
Hzj_jie89357612024-02-23 23:59:34105 if not gcs_tarball_prefix:
106 # sdk_override contains the full path but not only the version id. But since
107 # the scenario is limited to dry-run, it's not worth complexity to extract
108 # the version id.
109 if args.version == _GetCurrentVersionFromManifest():
110 return 0
Wez261b5972024-02-22 13:05:58111
Chong Gu619775d22023-06-07 17:36:45112 make_clean_directory(SDK_ROOT)
Chong Gu6d068f42022-07-18 21:13:44113
Chong Gucb84f3802022-09-23 14:39:57114 # Download from CIPD if there is no override file.
Andrew Mellen44a4a812022-10-10 16:24:01115 if not gcs_tarball_prefix:
Chong Gucb84f3802022-09-23 14:39:57116 if not args.cipd_prefix:
117 parser.exit(1, '--cipd-prefix must be specified.')
118 if not args.version:
119 parser.exit(2, '--version must be specified.')
Chong Gu619775d22023-06-07 17:36:45120 logging.info('Downloading SDK from CIPD...')
Wez261b5972024-02-22 13:05:58121 ensure_file = '%s%s-%s version:%s' % (args.cipd_prefix, host_plat,
122 _GetHostArch(), args.version)
Chong Gu1bea78e2022-10-11 19:46:39123 subprocess.run(('cipd', 'ensure', '-ensure-file', '-', '-root', SDK_ROOT,
124 '-log-level', 'warning'),
Chong Gu401265d52022-09-27 06:54:47125 check=True,
126 text=True,
127 input=ensure_file)
Hzj_jie89357612024-02-23 23:59:34128
129 # Verify that the downloaded version matches the expected one.
130 downloaded_version = _GetCurrentVersionFromManifest()
131 if downloaded_version != args.version:
132 logging.error(
133 'SDK version after download does not match expected (downloaded:%s '
134 'vs expected:%s)', downloaded_version, args.version)
135 return 3
Chong Gu619775d22023-06-07 17:36:45136 else:
137 logging.info('Downloading SDK from GCS...')
Hzj_jie3cf2b8c2024-08-07 18:30:41138 DownloadAndUnpackFromCloudStorage(
139 f'{gcs_tarball_prefix}/{get_host_os()}-{_GetHostArch()}/'
140 f'{args.file}.tar.gz', SDK_ROOT)
Chong Gu6d068f42022-07-18 21:13:44141
Wez9f45a592023-06-23 16:09:50142 # Build rules (e.g. fidl_library()) depend on updates to the top-level
143 # manifest to spot when to rebuild for an SDK update. Ensure that ninja
144 # sees that the SDK manifest has changed, regardless of the mtime set by
145 # the download & unpack steps above, by setting mtime to now.
146 # See crbug.com/1457463
Zijie He79897082025-06-16 23:59:41147 os.utime(_VERSION_FILE, None)
Wez9f45a592023-06-23 16:09:50148
Zijie He79897082025-06-16 23:59:41149 if not args.ignore_gen_build_defs:
150 subprocess.run([
151 os.path.join(os.path.dirname(os.path.realpath(__file__)),
152 'gen_build_defs.py'),
153 ], check=True)
David Songd6fcdce2024-10-24 22:20:14154
Chong Gu26d45742022-06-10 14:49:17155 return 0
156
157
158if __name__ == '__main__':
159 sys.exit(main())