|
| 1 | +"""A Python script to build the Verible package for a given platform""" |
| 2 | + |
| 3 | +# This script is called from the github build workflow and runs |
| 4 | +# in the repo's top dir. It uses _upstream and _packages dirs |
| 5 | +# for input and output respectivly. |
| 6 | + |
| 7 | + |
| 8 | +import os |
| 9 | +import json |
| 10 | +import subprocess |
| 11 | +from dataclasses import dataclass |
| 12 | +from typing import List, Union |
| 13 | +import argparse |
| 14 | +import shutil |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +# -- Command line options. |
| 18 | +parser = argparse.ArgumentParser() |
| 19 | + |
| 20 | + |
| 21 | +# The platform id. E.g. "darwin-arm64" |
| 22 | +parser.add_argument("--platform-id", required=True, type=str, help="Platform to build") |
| 23 | + |
| 24 | + |
| 25 | +# Path to the properties file with the build info. |
| 26 | +parser.add_argument( |
| 27 | + "--build-info-json", required=True, type=str, help="JSON with build properties" |
| 28 | +) |
| 29 | + |
| 30 | +args = parser.parse_args() |
| 31 | + |
| 32 | + |
| 33 | +def run(cmd_args: Union[List[str], str], shell: bool = False) -> None: |
| 34 | + """Run a command and check that it succeeded. Select shell=true to enable |
| 35 | + shell features such as '*' glob.""" |
| 36 | + print(f"\nRun: {cmd_args}", flush=True) |
| 37 | + print(f"{shell=}", flush=True) |
| 38 | + subprocess.run(cmd_args, check=True, shell=shell) |
| 39 | + print("Run done\n", flush=True) |
| 40 | + |
| 41 | + |
| 42 | +@dataclass(frozen=True) |
| 43 | +class PlatformInfo: |
| 44 | + """Represents the properties of a platform.""" |
| 45 | + |
| 46 | + verible_base_filename: str |
| 47 | + verible_ext: str |
| 48 | + # The name of the wrapper dir when uncompressing Verible package. |
| 49 | + verible_wrapper_dir: str |
| 50 | + unarchive_cmd: str |
| 51 | + |
| 52 | + |
| 53 | +def get_platform_info(platform_id: str, verible_release_tag: str) -> PlatformInfo: |
| 54 | + """Extract (platform_id, platform_info)""" |
| 55 | + platforms = { |
| 56 | + "darwin-arm64": PlatformInfo( |
| 57 | + f"verible-{verible_release_tag}-macOS", |
| 58 | + "tar.gz", |
| 59 | + f"verible-{verible_release_tag}-macOS", |
| 60 | + ["tar", "zxf"], |
| 61 | + ), |
| 62 | + "darwin-x86-64": PlatformInfo( |
| 63 | + f"verible-{verible_release_tag}-macOS", |
| 64 | + "tar.gz", |
| 65 | + f"verible-{verible_release_tag}-macOS", |
| 66 | + ["tar", "zxf"], |
| 67 | + ), |
| 68 | + "linux-x86-64": PlatformInfo( |
| 69 | + f"verible-{verible_release_tag}-linux-static-x86_64", |
| 70 | + "tar.gz", |
| 71 | + f"verible-{verible_release_tag}", |
| 72 | + ["tar", "zxf"], |
| 73 | + ), |
| 74 | + "linux-aarch64": PlatformInfo( |
| 75 | + f"verible-{verible_release_tag}-linux-static-arm64", |
| 76 | + "tar.gz", |
| 77 | + f"verible-{verible_release_tag}", |
| 78 | + ["tar", "zxf"], |
| 79 | + ), |
| 80 | + "windows-amd64": PlatformInfo( |
| 81 | + f"verible-{verible_release_tag}-win64", |
| 82 | + "zip", |
| 83 | + f"verible-{verible_release_tag}-win64", |
| 84 | + ["unzip"], |
| 85 | + ), |
| 86 | + } |
| 87 | + |
| 88 | + return platforms[platform_id] |
| 89 | + |
| 90 | + |
| 91 | +def main(): |
| 92 | + """Builds the Apio oss-cad-suite package for one platform.""" |
| 93 | + |
| 94 | + # -- Save the start dir. It is assume to be at top of this repo. |
| 95 | + work_dir: Path = Path.cwd() |
| 96 | + print(f"\n{work_dir=}") |
| 97 | + |
| 98 | + # -- Get platform id. |
| 99 | + platform_id = args.platform_id |
| 100 | + print(f"{platform_id=}") |
| 101 | + |
| 102 | + # -- Get the build info |
| 103 | + with Path(args.build_info_json).open("r", encoding="utf-8") as f: |
| 104 | + build_info = json.load(f) |
| 105 | + |
| 106 | + print("\nOriginal build info:") |
| 107 | + print(json.dumps(build_info, indent=2)) |
| 108 | + |
| 109 | + # -- Determine if processing the windows package. |
| 110 | + is_windows = "windows" in platform_id |
| 111 | + print(f"\n{is_windows=}") |
| 112 | + |
| 113 | + # -- Extract build info params |
| 114 | + release_tag = build_info["release-tag"] |
| 115 | + package_tag = release_tag.replace("-", "") |
| 116 | + verible_release_tag = build_info["verible-release-tag"] |
| 117 | + # verible_package_tag = verible_release_tag.replace("-", "") |
| 118 | + platform_info = get_platform_info(platform_id, verible_release_tag) |
| 119 | + |
| 120 | + print() |
| 121 | + print(f"* {platform_id=}") |
| 122 | + print(f"* {release_tag=}") |
| 123 | + print(f"* {package_tag=}") |
| 124 | + print(f"* {platform_info=}") |
| 125 | + print(f"* {verible_release_tag=}") |
| 126 | + |
| 127 | + # -- Create folder for storing the upstream packages |
| 128 | + upstream_dir: Path = work_dir / "_upstream" / platform_id |
| 129 | + print(f"\n{upstream_dir=}") |
| 130 | + upstream_dir.mkdir(parents=True, exist_ok=True) |
| 131 | + |
| 132 | + # -- Create folder for storing the generated package file. |
| 133 | + package_dir: Path = work_dir / "_packages" / platform_id |
| 134 | + print(f"\n{package_dir=}") |
| 135 | + package_dir.mkdir(parents=True, exist_ok=True) |
| 136 | + |
| 137 | + # -- Construct target package file name |
| 138 | + parts = [ |
| 139 | + "apio-verible", |
| 140 | + "-", |
| 141 | + platform_id, |
| 142 | + "-", |
| 143 | + package_tag, |
| 144 | + ".tgz", |
| 145 | + ] |
| 146 | + package_filename = "".join(parts) |
| 147 | + print(f"\n{package_filename=}") |
| 148 | + build_info["target-platform"] = platform_id |
| 149 | + build_info["file-name"] = package_filename |
| 150 | + |
| 151 | + # Construct verible file name |
| 152 | + verible_fname = ( |
| 153 | + platform_info.verible_base_filename + "." + platform_info.verible_ext |
| 154 | + ) |
| 155 | + print(f"\n{verible_fname=}") |
| 156 | + |
| 157 | + # -- Construct Verible URL |
| 158 | + parts = [ |
| 159 | + "https://github.com/chipsalliance/verible/releases/download", |
| 160 | + "/", |
| 161 | + verible_release_tag, |
| 162 | + "/", |
| 163 | + verible_fname, |
| 164 | + ] |
| 165 | + verible_url = "".join(parts) |
| 166 | + print(f"\n{verible_url=}") |
| 167 | + |
| 168 | + # -- Download the Verible file. |
| 169 | + print(f"\nChanging to UPSTREAM_DIR: {str(upstream_dir)}") |
| 170 | + os.chdir(upstream_dir) |
| 171 | + print(f"\nDownloading {verible_url}") |
| 172 | + run(["wget", "-nv", verible_url]) |
| 173 | + run(["ls", "-al"]) |
| 174 | + |
| 175 | + # -- Uncompress the Verible archive |
| 176 | + print("Uncompressing the Verible file") |
| 177 | + run(platform_info.unarchive_cmd + [verible_fname]) |
| 178 | + run(["ls", "-al"]) |
| 179 | + |
| 180 | + # -- Delete the Verible archive, we don't need it anymore |
| 181 | + print("Deleting the Verible archive file") |
| 182 | + Path(verible_fname).unlink() |
| 183 | + run(["ls", "-al"]) |
| 184 | + |
| 185 | + # -- Determine the root dir of the Verible files, below the |
| 186 | + # -- wrapper dir. |
| 187 | + print(f"\n{Path.cwd()=}") |
| 188 | + verible_root = Path(platform_info.verible_wrapper_dir) |
| 189 | + print(f"{verible_root=}") |
| 190 | + print(f"{verible_root.absolute()=}") |
| 191 | + if is_windows: |
| 192 | + # -- Windows package has no 'bin' dir. |
| 193 | + assert (verible_root / "verible-verilog-format.exe").is_file() |
| 194 | + else: |
| 195 | + assert (verible_root / "bin").is_dir() |
| 196 | + |
| 197 | + # -- Copy the package files to the output directory. |
| 198 | + # -- We use rsync to copy all, including sim links, if any. |
| 199 | + # -- The does "/" matters. |
| 200 | + print("\nCopying package files.") |
| 201 | + # -- For windows, inset the missing 'bin' dir. |
| 202 | + dst = package_dir / "bin" if is_windows else package_dir |
| 203 | + run(["rsync", "-aq", f"{verible_root}/", f"{dst}/"]) |
| 204 | + |
| 205 | + # -- Delete the upstream dir |
| 206 | + print(f"\nDeleting upstream dir {verible_root}") |
| 207 | + shutil.rmtree(verible_root) |
| 208 | + |
| 209 | + # Write updated build info to the package |
| 210 | + print("Writing package build info.") |
| 211 | + output_json_file = package_dir / "BUILD-INFO.json" |
| 212 | + with output_json_file.open("w", encoding="utf-8") as f: |
| 213 | + json.dump(build_info, f, indent=2) |
| 214 | + f.write("\n") # Ensure the file ends with a newline |
| 215 | + run(["cat", "-n", output_json_file]) |
| 216 | + |
| 217 | + # Format the json file in the package dir |
| 218 | + print("Formatting package build info.") |
| 219 | + run(["json-align", "--in-place", "--spaces", "2", output_json_file]) |
| 220 | + run(["cat", "-n", output_json_file]) |
| 221 | + |
| 222 | + # -- Compress the package. We run in a shell for the '*' glob to expand. |
| 223 | + print("Compressing the package.") |
| 224 | + os.chdir(package_dir) |
| 225 | + run(f"tar zcf ../{package_filename} ./*", shell=True) |
| 226 | + |
| 227 | + # -- Delete the package dir |
| 228 | + print(f"\nDeleting package dir {package_dir}") |
| 229 | + shutil.rmtree(package_dir) |
| 230 | + |
| 231 | + # -- Final check |
| 232 | + os.chdir(work_dir) |
| 233 | + print(f"{Path.cwd()=}") |
| 234 | + run(["ls", "-al"]) |
| 235 | + run(["ls", "-al", "_packages"]) |
| 236 | + assert (Path("_packages") / package_filename).is_file() |
| 237 | + |
| 238 | + # -- All done |
| 239 | + |
| 240 | + |
| 241 | +if __name__ == "__main__": |
| 242 | + main() |
0 commit comments