|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Build missing LLVM tools required to build Swift SDK for WebAssembly. |
| 4 | +# |
| 5 | + |
| 6 | +import argparse |
| 7 | +import pathlib |
| 8 | +import subprocess |
| 9 | +from typing import List, Optional |
| 10 | + |
| 11 | +from build_support.actions import REPO_ROOT |
| 12 | + |
| 13 | + |
| 14 | +WASM_SPECIFIC_TOOLS = [ |
| 15 | + 'wasm-ld', |
| 16 | + 'llvm-ar', |
| 17 | + 'llvm-ranlib', |
| 18 | +] |
| 19 | + |
| 20 | + |
| 21 | +def compute_missing_tools(toolchain_path: pathlib.Path): |
| 22 | + |
| 23 | + missing_tools = [] |
| 24 | + for tool in WASM_SPECIFIC_TOOLS: |
| 25 | + if not (toolchain_path / 'usr' / 'bin' / tool).exists(): |
| 26 | + missing_tools.append(tool) |
| 27 | + |
| 28 | + return missing_tools |
| 29 | + |
| 30 | + |
| 31 | +def compute_build_products(toolchain_path: pathlib.Path): |
| 32 | + build_products = [] |
| 33 | + missing_tools = compute_missing_tools(toolchain_path) |
| 34 | + |
| 35 | + if 'llvm-ranlib' in missing_tools and \ |
| 36 | + (toolchain_path / 'usr' / 'bin' / 'llvm-ar').exists(): |
| 37 | + build_products.append( |
| 38 | + SymlinkTool(toolchain_path, |
| 39 | + tool_name='llvm-ranlib', target='llvm-ar')) |
| 40 | + missing_tools.remove('llvm-ranlib') |
| 41 | + |
| 42 | + if len(missing_tools) > 0: |
| 43 | + build_products.append(BuildLLVMTools(toolchain_path, missing_tools)) |
| 44 | + |
| 45 | + # Symlink tools for existing tools |
| 46 | + for tool in WASM_SPECIFIC_TOOLS: |
| 47 | + if tool in missing_tools: |
| 48 | + continue |
| 49 | + |
| 50 | + build_products.append(SymlinkTool(toolchain_path, tool)) |
| 51 | + |
| 52 | + return build_products |
| 53 | + |
| 54 | + |
| 55 | +class BuildLLVMTools: |
| 56 | + |
| 57 | + def __init__(self, toolchain_path: pathlib.Path, tools: List[str]): |
| 58 | + self.toolchain_path = toolchain_path |
| 59 | + self.tools = tools |
| 60 | + |
| 61 | + def configure( |
| 62 | + self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path): |
| 63 | + cmake_options = [] |
| 64 | + if 'wasm-ld' in self.tools: |
| 65 | + cmake_options.append('-DLLVM_ENABLE_PROJECTS=lld') |
| 66 | + subprocess.check_call([ |
| 67 | + 'cmake', |
| 68 | + '-G', 'Ninja', |
| 69 | + '-DCMAKE_BUILD_TYPE=Release', |
| 70 | + '-DLLVM_TARGETS_TO_BUILD=WebAssembly', |
| 71 | + '-DCMAKE_INSTALL_PREFIX=' + str(self.toolchain_path), |
| 72 | + '-DLLVM_TOOLCHAIN_TOOLS=' + ';'.join(self.tools), |
| 73 | + llvm_project_dir / 'llvm', |
| 74 | + ], cwd=build_dir) |
| 75 | + |
| 76 | + def build(self, build_dir: pathlib.Path): |
| 77 | + print(f'Building LLVM tools in {build_dir}') |
| 78 | + subprocess.check_call(['ninja', 'install'], cwd=build_dir) |
| 79 | + |
| 80 | + def __str__(self): |
| 81 | + return 'Build LLVM tools (' + ', '.join(self.tools) + ')' |
| 82 | + |
| 83 | + |
| 84 | +class SymlinkTool: |
| 85 | + |
| 86 | + def __init__( |
| 87 | + self, toolchain_path: pathlib.Path, |
| 88 | + tool_name: str, target: Optional[str] = None): |
| 89 | + self.toolchain_path = toolchain_path |
| 90 | + self.tool_name = tool_name |
| 91 | + self.target = target |
| 92 | + |
| 93 | + def configure( |
| 94 | + self, build_dir: pathlib.Path, llvm_project_dir: pathlib.Path): |
| 95 | + pass |
| 96 | + |
| 97 | + def build(self, build_dir: pathlib.Path): |
| 98 | + bin_dir = build_dir / 'bin' |
| 99 | + bin_dir.mkdir(parents=True, exist_ok=True) |
| 100 | + target_name = self.target if self.target else self.tool_name |
| 101 | + target = self.toolchain_path / 'usr' / 'bin' / target_name |
| 102 | + tool = bin_dir / self.tool_name |
| 103 | + if not tool.exists(): |
| 104 | + tool.symlink_to(target) |
| 105 | + |
| 106 | + def __str__(self): |
| 107 | + if self.target: |
| 108 | + return f'Symlink {self.tool_name} to {self.target}' |
| 109 | + return 'Symlink ' + self.tool_name |
| 110 | + |
| 111 | + |
| 112 | +def main(): |
| 113 | + parser = argparse.ArgumentParser() |
| 114 | + parser.add_argument('--toolchain', required=True) |
| 115 | + args = parser.parse_args() |
| 116 | + |
| 117 | + toolchain_path = pathlib.Path(args.toolchain) |
| 118 | + missing_tools = compute_missing_tools(toolchain_path) |
| 119 | + |
| 120 | + if len(missing_tools) == 0: |
| 121 | + print('All required tools are available') |
| 122 | + return |
| 123 | + |
| 124 | + print('Missing tools:') |
| 125 | + for tool in missing_tools: |
| 126 | + print(f' * {tool}') |
| 127 | + |
| 128 | + repo_root = pathlib.Path(REPO_ROOT) |
| 129 | + build_dir = repo_root / '..' / 'build' / 'llvm-tools' |
| 130 | + build_dir.mkdir(parents=True, exist_ok=True) |
| 131 | + |
| 132 | + llvm_project_dir = repo_root / '..' / 'llvm-project' |
| 133 | + |
| 134 | + products = compute_build_products(toolchain_path) |
| 135 | + for product in products: |
| 136 | + print(product) |
| 137 | + product.configure(build_dir, llvm_project_dir) |
| 138 | + product.build(build_dir) |
| 139 | + |
| 140 | + |
| 141 | +if __name__ == '__main__': |
| 142 | + main() |
0 commit comments