import glob import os import subprocess upstream_version = "22.1.8" [upstream_major, upstream_minor, upstream_rev] = upstream_version.split(".") # Begin of file lists LLVM_TOOLS = [ "bugpoint", "dsymutil", "llc", "lli", "llvm-addr2line", "llvm-ar", "llvm-as", "llvm-bcanalyzer", "llvm-bitcode-strip", "llvm-bolt", "llvm-bolt-binary-analysis", "llvm-bolt-heatmap", "llvm-boltdiff", "llvm-c-test", "llvm-cas", "llvm-cat", "llvm-cfi-verify", "llvm-cgdata", "llvm-config", "llvm-cov", "llvm-ctxprof-util", "llvm-cvtres", "llvm-cxxdump", "llvm-cxxfilt", "llvm-cxxmap", "llvm-debuginfo-analyzer", "llvm-debuginfod", "llvm-debuginfod-find", "llvm-diff", "llvm-dis", "llvm-dlltool", "llvm-dwarfdump", "llvm-dwarfutil", "llvm-dwp", "llvm-exegesis", "llvm-extract", "llvm-gsymutil", "llvm-ifs", "llvm-install-name-tool", "llvm-ir2vec", "llvm-jitlink", "llvm-lib", "llvm-libtool-darwin", "llvm-link", "llvm-lipo", "llvm-lto", "llvm-lto2", "llvm-mc", "llvm-mca", "llvm-ml", "llvm-ml64", "llvm-modextract", "llvm-nm", "llvm-objcopy", "llvm-objdump", "llvm-offload-binary", "llvm-offload-wrapper", "llvm-opt-report", "llvm-otool", "llvm-pdbutil", "llvm-profdata", "llvm-profgen", "llvm-ranlib", "llvm-rc", "llvm-readelf", "llvm-readobj", "llvm-readtapi", "llvm-reduce", "llvm-remarkutil", "llvm-rtdyld", "llvm-sim", "llvm-size", "llvm-split", "llvm-stress", "llvm-strings", "llvm-strip", "llvm-symbolizer", "llvm-tblgen", "llvm-tli-checker", "llvm-undname", "llvm-windres", "llvm-xray", "merge-fdata", "opt", "perf2bolt", "reduce-chunk-list", "sancov", "sanstats", "verify-uselistorder", ] LLDB_BINARIES = [ "lldb", "lldb-argdumper", "lldb-dap", "lldb-instr", "lldb-mcp", "lldb-server", "yaml2macho-core", ] LLD_BINARIES = ["lld", "lld-link", "ld64.lld", "wasm-ld", "wrapper://ld.lld~ld.lld"] CLANG_BINARIES = [ f"clang-{upstream_major}~llvm-clang", "wrapper://clang~clang", "wrapper://clang++~clang++", f"wrapper://clang~clang-{upstream_major}", "clang-check", "clang-cl", "clang-cpp", "clang-extdef-mapping", "clang-format", "clang-installapi", "clang-linker-wrapper", "clang-nvlink-wrapper", "clang-offload-bundler", "clang-offload-packager", "clang-refactor", "clang-repl", "clang-scan-deps", "clang-sycl-linker", "clang-tblgen", "diagtool", "git-clang-format", "hmaptool", "intercept-build", "amdgpu-arch", "c-index-test", "analyze-build", "nvptx-arch", "offload-arch", "scan-build", "scan-build-py", "scan-view", "libexec/analyze-c++", "libexec/analyze-cc", "libexec/c++-analyzer", "libexec/ccc-analyzer", "libexec/intercept-c++", "libexec/intercept-cc", ] # End of file lists import lib.cpp as cpp from lib import arch from lib.make import * version(upstream_version) source_url( f"https://github.com/llvm/llvm-project/releases/download/llvmorg-{upstream_version}/llvm-project-{upstream_version}.src.tar.xz" ) def llvm_host_triple(): return arch.get_target().replace("-semios-linux", "-semios-linux-musl") def build(): os.environ["CXXFLAGS"] = os.environ.get("CXXFLAGS", "") + " --rtlib=compiler-rt" subprocess.run( [ "cmake", "-G", "Ninja", "-S", "llvm", "-B", "build", "-DCMAKE_BUILD_TYPE=Release", f"-DLLVM_DEFAULT_TARGET_TRIPLE={llvm_host_triple()}", f"-DLLVM_HOST_TRIPLE={llvm_host_triple()}", "-DLLVM_ENABLE_PROJECTS=clang;lldb;lld;bolt", "-DLLVM_ENABLE_RUNTIMES=libcxx;libcxxabi;libunwind;compiler-rt", "-DLLVM_ENABLE_EH=ON", "-DLLVM_ENABLE_RTTI=ON", "-DCMAKE_INSTALL_PREFIX=/", "-DLLVM_ENABLE_ASSERTIONS=OFF", "-DLLVM_BUILD_LLVM_DYLIB=ON", "-DLLVM_ENABLE_LIBCXX=ON", "-DLLVM_ENABLE_LIBEDIT=ON", "-DLLVM_ENABLE_LLD=ON", "-DLLVM_LINK_LLVM_DYLIB=ON", "-DLIBCXX_HAS_MUSL_LIBC=ON", "-DCOMPILER_RT_BUILD_GWP_ASAN=OFF", ], check=True, ) subprocess.run(["cmake", "--build", "build"], check=True) os.environ["DESTDIR"] = os.getcwd() + "/cpp_install" subprocess.run(["cmake", "--install", "build"], check=True) os.chdir("cpp_install") on_build(build) # Begin of helper functions package_dir = os.getcwd() def install_wrapper(composer, name: str, dst: str): composer.addfile(f"{package_dir}/wrapper/{name}.wrapper", dst) with open(f"{composer.workdir}/bundle/{dst}", "r") as f: s = f.read() s = s.replace("LLVM_VERSION=", f"LLVM_VERSION={upstream_version}").replace( "ARCHITECTURE=", "ARCHITECTURE=" + arch.get_target() ).replace("LLVM_ARCH=", "LLVM_ARCH=" + llvm_host_triple()) with open(f"{composer.workdir}/bundle/{dst}", "w") as f: f.write(s) os.chmod(f"{composer.workdir}/bundle/{dst}", 0o755) def auto_pack_llvm_runtime(composer, prefix, dev): composer.makedirs("lib") parent_dir = f"./lib/{llvm_host_triple()}" for i in os.listdir(parent_dir): fullpath = f"{parent_dir}/{i}" if i.split(".")[0] != prefix: continue if dev: if ".so" in i: continue else: if not ".so" in i: continue composer.addfile(fullpath, f"lib/{i}") if not dev: return def auto_pack_llvm_runtime_dev(composer, condition): composer.makedir("include") parent_include = f"./usr/include" for i in condition: if i.startswith("lib:"): auto_pack_llvm_runtime(composer, i.replace("lib:", ""), True) continue i = i.replace("include:", "") for file in glob.glob(f"{parent_include}/{i}"): name = file.replace(f"{parent_include}/", "") if "/" in name: composer.makedirs(f"include/{os.path.dirname(name)}") if os.path.isdir(file): composer.add_dir(file, f"include/{name}") else: composer.addfile(file, f"include/{name}") def use_llvm_runtime_auto_pack(prefix: str): on_pack(lambda composer: auto_pack_llvm_runtime(composer, prefix, False)) def use_llvm_runtime_dev_auto_pack(des: list[str]): on_pack(lambda composer: auto_pack_llvm_runtime_dev(composer, des)) def auto_pack_llvm_libs(composer, prefixes: list[str]): composer.makedirs("lib") parent_dir = f"./lib/" for i in os.listdir(parent_dir): fullpath = f"{parent_dir}/{i}" if not i.split(".")[0] in prefixes: continue if not ".so" in i: continue composer.addfile(fullpath, f"lib/{i}") def use_llvm_libs_auto_pack(prefixes: list[str]): on_pack(lambda composer: auto_pack_llvm_libs(composer, prefixes)) def auto_pack_bin(composer, conditions: list[str]): for i in conditions: prefix = "bin" if i.startswith("libexec/"): i = i.replace("libexec/", "") prefix = "libexec" composer.makedirs(prefix) src = i dst = f"{prefix}/{i}" if len(i.split("~")) == 2: src = i.split("~")[0] dst = f"bin/{i.split('~')[1]}" if src.startswith("wrapper://"): src = src.replace("wrapper://", "") if len(i.split("~")) == 1: dst = f"bin/{src}.wrapper" install_wrapper(composer, src, dst) continue if os.path.exists(f"{prefix}/{src}"): src = f"{prefix}/{src}" elif os.path.exists(f"usr/{prefix}/{src}"): src = f"usr/{prefix}/{src}" composer.addfile(src, dst) def use_bin_auto_pack(conditions: list[str]): on_pack(lambda composer: auto_pack_bin(composer, conditions)) # End of helper functions # Begin of packages package("libunwind") dep("semi-libc @same-arch") use_llvm_runtime_auto_pack("libunwind") package("libunwind-dev") dep("libunwind (same)") use_llvm_runtime_dev_auto_pack( ["lib:libunwind", "include:mach-o", "include:*unwind*.h"] ) package("libcxxabi") dep("libunwind (same)") use_llvm_runtime_auto_pack("libc++abi") package("libcxxabi-dev") dep("libcxxabi (same)") use_llvm_runtime_dev_auto_pack(["lib:libc++abi"]) package("libcxx") dep("libcxxabi (same)") use_llvm_runtime_auto_pack("libc++") package("libcxx-dev") dep("libcxx (same)") use_llvm_runtime_dev_auto_pack( [ "lib:libc++", "lib:libc++experimental", f"include:{llvm_host_triple()}/c++/v1/__config_site", "include:c++/v1", ] ) package("libllvm") dep("libcxx @same-arch") use_llvm_libs_auto_pack(["libLLVM", "libLTO", "libRemarks"]) package("llvm") dep("libllvm (same)") use_bin_auto_pack(LLVM_TOOLS) for i in ["strip", "ar", "ranlib", "readelf", "objcopy", "objdump", "strings", "nm"]: link(f"bin/llvm-{i}", f"/bin/{i}") package("lld") dep("libcxx @same-arch") use_bin_auto_pack(LLD_BINARIES) link("bin/ld.lld", "/bin/ld") package("liblldb") dep("libcxx @same-arch") use_llvm_libs_auto_pack(["liblldb", "liblldbIntelFeatures"]) package("lldb") dep("libcxx @same-arch") dep("liblldb (same)") use_bin_auto_pack(LLDB_BINARIES) package("libclang") dep("libcxx @same-arch") dep("libllvm (same)") use_llvm_libs_auto_pack(["libclang", "libclang-cpp"]) def pack_clang(composer): auto_pack_bin(composer, CLANG_BINARIES) composer.makedirs(f"lib/clang/{upstream_major}/lib") composer.add_dir( f"lib/clang/{upstream_major}/lib/{llvm_host_triple()}", f"lib/clang/{upstream_major}/lib/{llvm_host_triple()}", ) package("clang") dep("libclang (same)") on_pack(pack_clang) # End of packages