import os import shutil import subprocess from lib.common import treedir from . import make as libmake from . import pkgcomposer def configure(rem: list[str] = []): subprocess.run(["./configure"] + rem, check=True) def cmake(rem: list[str] = []): source_dir = os.getcwd() os.makedirs("../cmake_build", exist_ok=True) os.chdir("../cmake_build") subprocess.run( [ "cmake", "-GNinja", "-DCMAKE_BUILD_TYPE=Release", "-DCMAKE_INSTALL_PREFIX=/", source_dir, ] + rem, check=True, ) 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"], check=False) def make(rem: list[str] = []): subprocess.run(["make", f"-j{os.cpu_count()}"] + rem, check=True) def make_install(rem: list[str] | None = None, chdir: bool = True, move: bool = True): default_rem = ["DESTDIR=" + os.getcwd() + "/../cpp_install"] if rem is None: try: shutil.rmtree("../cpp_install") except: pass subprocess.run(["make", "install"] + (rem or default_rem), check=True) if rem is None and chdir: os.chdir("../cpp_install") if chdir and move: _move_install() def ninja(rem: list[str] = []): subprocess.run(["ninja"] + rem, check=True) def ninja_install(rem: list[str] = [], chdir: bool = True, move: bool = True): if chdir: os.environ["DESTDIR"] = os.getcwd() + "/../cpp_install" subprocess.run(["ninja", "install"] + rem, check=True) if chdir: os.chdir(os.environ["DESTDIR"]) if chdir and move: _move_install() def meson_setup(rem: list[str] = []): subprocess.run(["meson", "setup", "_meson_build"] + rem, check=True) def meson_compile(rem: list[str] = []): subprocess.run(["meson", "compile", "-C", "_meson_build"] + rem, check=True) def meson_install(rem: list[str] | None = None, chdir: bool = True, move: bool = True): default_rem = ["--destdir", f"{os.getcwd()}/../cpp_install"] if not rem: os.makedirs("../cpp_install", exist_ok=True) subprocess.run( ["meson", "install", "-C", "_meson_build"] + (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 ("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) def use_auto_pack(profiles: list[str]): libmake.on_pack(lambda pc: _auto_pack(profiles, pc))