import os import shlex import shutil import subprocess from lib.common import treedir from . import make as libmake from . import pkgcomposer from . import logging # Common helpers def write_prev_configure(args: list[str]): with open("_cpp_configure", "w", encoding="utf-8") as file: file.write(shlex.join(args) + "\n") def read_prev_configure() -> list[str] | None: try: with open("_cpp_configure", "r", encoding="utf-8") as file: return shlex.split(file.read()) except FileNotFoundError: return None def _move_install(): subprocess.run(["sh", "-c", "cp -r ./pkg/*/* . 2>/dev/null || true"], check=False) subprocess.run( ["sh", "-c", "cp -r ./usr/local/* . 2>/dev/null || true"], check=False ) subprocess.run(["sh", "-c", "cp -r ./usr/* . 2>/dev/null || true"], check=False) subprocess.run(["rm", "-rf", "./pkg", "./usr", "./local"], check=False) # AutoTools & GNU Make def configure(rem: list[str] = []): if read_prev_configure() == rem: logging.step("Skipping previously completed configuration.") else: logging.step("Configuring project via AutoTools...") logging.run_volatile(["./configure"] + rem, check=True) write_prev_configure(rem) def make(rem: list[str] = []): logging.step("Building project via Unix Makefile...") logging.run_volatile(["make", f"-j{os.cpu_count()}"] + rem, check=True) def make_install(rem: list[str] | None = None, chdir: bool = True, move: bool = True): logging.step("Installing project to temporary directory via Unix Makefile...") default_rem = ["DESTDIR=" + os.getcwd() + "/../cpp_install"] if rem is None: try: shutil.rmtree("../cpp_install") except: pass logging.run_volatile(["make", "install"] + (rem or default_rem), check=True) if chdir: os.chdir("../cpp_install") if chdir and move: _move_install() # CMake def cmake(rem: list[str] = []): source_dir = os.getcwd() os.makedirs("../cmake_build", exist_ok=True) os.chdir("../cmake_build") if read_prev_configure() == rem: logging.step("Skipping previously completed configuration.") else: logging.step("Configuring project via CMake...") logging.run_volatile( ["cmake", "-GNinja", "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_INSTALL_PREFIX=/", source_dir] + rem, check=True, ) write_prev_configure(rem) # Ninja def ninja(rem: list[str] = []): logging.step("Building project via Ninja...") logging.run_volatile(["ninja"] + rem, check=True) def ninja_install(rem: list[str] = [], chdir: bool = True, move: bool = True): logging.step("Installing project to temporary directory via Ninja...") if chdir: os.environ["DESTDIR"] = os.getcwd() + "/../cpp_install" logging.run_volatile(["ninja", "install"] + rem, check=True) if chdir: os.chdir(os.environ["DESTDIR"]) if chdir and move: _move_install() # Meson def meson_setup(rem: list[str] = []): if read_prev_configure() == rem: logging.step("Skipping previously completed configuration.") else: logging.step("Configuring project via Meson...") logging.run_volatile(["meson", "setup", "../_meson_build"] + rem + ["--buildtype=release"], check=True) write_prev_configure(rem) os.chdir("../_meson_build") def meson_compile(rem: list[str] = []): logging.step("Building project via Meson...") logging.run_volatile(["meson", "compile"] + rem, check=True) def meson_install(rem: list[str] | None = None, chdir: bool = True, move: bool = True): logging.step("Install project to temporary directory via Meson...") default_rem = ["--destdir", f"{os.getcwd()}/../cpp_install"] if not rem: os.makedirs("../cpp_install", exist_ok=True) logging.run_volatile( ["meson", "install"] + (rem or default_rem), check=True ) if chdir: os.chdir("../cpp_install") if chdir and move: _move_install() def _do_auto_pack(pc: pkgcomposer.PkgComposer, srcdir: str, dstdir: str, filter): if not os.path.exists(srcdir): return try: pc.makedir(dstdir) except Exception: pass for ent in treedir(srcdir): if filter(ent): if ent.is_file(follow_symlinks=False): pc.makedirs(os.path.dirname(f"{dstdir}/{ent.name}")) pc.addfile(ent.path, f"{dstdir}/{ent.name}") elif ent.is_symlink() and not os.path.isabs(os.readlink(ent.path)): try: pc.addfile(ent.path, f"{dstdir}/{ent.name}") except Exception: pass def _is_dev_file(ent) -> bool: return ( ent.name.endswith(".o") or ent.name.endswith(".a") or ent.name.endswith(".la") or ent.name.endswith(".pc") or ent.name.endswith(".h") or ent.name.endswith(".hpp") or ent.name.endswith(".cmake") or ent.name.endswith(".inc") or ent.name.endswith(".c") or ent.name.endswith(".cc") or ent.name.endswith(".cxx") or ent.name.endswith(".cpp") or ent.name.endswith(".hpp") or ("Makefile" in ent.name) ) def _autopack_filter_lib(ent) -> bool: return not _is_dev_file(ent) def _autopack_filter_bin(ent) -> bool: return os.access(ent.path, os.X_OK) and ent.is_file(follow_symlinks=False) def _autopack_filter_dev(ent): return _is_dev_file(ent) def _auto_pack(profiles: list[str], pc: pkgcomposer.PkgComposer): if "lib" in profiles: _do_auto_pack(pc, "lib", "lib", _autopack_filter_lib) if "bin" in profiles: _do_auto_pack(pc, "bin", "bin", _autopack_filter_bin) _do_auto_pack(pc, "sbin", "bin", _autopack_filter_bin) if "dev" in profiles: _do_auto_pack(pc, "lib", "lib", _autopack_filter_dev) _do_auto_pack(pc, "include", "include", _autopack_filter_dev) if os.path.exists("share/gir-1.0"): pc.makedirs("include/gir") pc.add_dir("share/gir-1.0", "include/gir/gir-1.0") def use_auto_pack(profiles: list[str]): libmake.on_pack(lambda pc: _auto_pack(profiles, pc))