From d4a8040a9c46752aa18e5a8884e25441ec93fafc Mon Sep 17 00:00:00 2001 From: sisungo Date: Sat, 13 Jun 2026 12:01:43 +0800 Subject: [PATCH] feat (lib): add autolink and autostrip Signed-off-by: sisungo --- hooks/__init__.py | 9 ++++++++- hooks/autolink.py | 31 +++++++++++++++++++++++++++++++ hooks/autostrip.py | 22 +++++++++++++++++++++- lib/make.py | 15 ++++++++++++++- x.py | 30 +++++++++++++++++++----------- 5 files changed, 93 insertions(+), 14 deletions(-) create mode 100644 hooks/autolink.py diff --git a/hooks/__init__.py b/hooks/__init__.py index ef7a4d4..091fc0c 100644 --- a/hooks/__init__.py +++ b/hooks/__init__.py @@ -1,9 +1,12 @@ from typing import Callable, TypeAlias -from . import autopatch, autostrip +from lib import pkgcomposer + +from . import autolink, autopatch, autostrip PreBuild: TypeAlias = Callable[[], None] PostBuild: TypeAlias = Callable[[], None] +PostPack: TypeAlias = Callable[[pkgcomposer.PkgComposer], None] pre_build: dict[str, PreBuild] = { "autopatch": autopatch.autopatch, @@ -12,3 +15,7 @@ pre_build: dict[str, PreBuild] = { post_build: dict[str, PostBuild] = { "autostrip": autostrip.autostrip, } + +post_pack: dict[str, PostPack] = { + "autolink": autolink.autolink, +} diff --git a/hooks/autolink.py b/hooks/autolink.py new file mode 100644 index 0000000..b3ee69b --- /dev/null +++ b/hooks/autolink.py @@ -0,0 +1,31 @@ +import os + +from lib import arch, common, make, pkgcomposer + + +def autolink(composer: pkgcomposer.PkgComposer): + os.chdir(composer.workdir + "/bundle/") + + try: + for i in common.treedir("bin"): + if i.is_file(): + make.link(f"bin/{i.name}", "/bin/" + i.name) + except Exception: + pass + + try: + for i in common.treedir("lib"): + if i.is_file(): + make.link(f"lib/{i.name}", "/lib/" + arch.get_target() + "/" + i.name) + except Exception: + pass + + try: + for i in common.treedir("include"): + if i.is_file(): + make.link( + f"include/{i.name}", + "/lib/" + arch.get_target() + "/include/" + i.name, + ) + except Exception: + pass diff --git a/hooks/autostrip.py b/hooks/autostrip.py index 0153027..7e790ef 100644 --- a/hooks/autostrip.py +++ b/hooks/autostrip.py @@ -1,2 +1,22 @@ +import os +import subprocess + +from lib import common + +strip = os.environ.get("STRIP", "strip") + + +def _is_elf(filepath): + with open(filepath, "rb") as f: + return f.read(4) == b"\x7fELF" + + def autostrip(): - pass + for ent in common.treedir("."): + if not ent.is_file(follow_symlinks=False): + continue + if not (os.access(ent.path, os.X_OK) or ".so" in ent.name): + continue + if not _is_elf(ent.path): + continue + subprocess.run([strip, ent.path], check=True) diff --git a/lib/make.py b/lib/make.py index 1c40374..818f998 100644 --- a/lib/make.py +++ b/lib/make.py @@ -7,10 +7,13 @@ _key_version = "version" _key_description = "description" _key_on_pack = "on_pack" _key_arch_any = "arch_any" +_key_links = "links" _current_pkgname: str = _key_global _pkginfo: dict[str, dict[str, typing.Any]] = { - _key_global: {}, + _key_global: { + _key_links: [], + }, } _supported_arch: typing.Callable[[str], bool] = lambda x: True _on_build: typing.Callable[[], None] | None = None @@ -36,6 +39,16 @@ def arch_any(): _pkginfo[_current_pkgname][_key_arch_any] = True +def link(src: str, dst: str, default: bool = True): + _pkginfo[_current_pkgname][_key_links] += [ + { + "from": src, + "to": dst, + "default": default, + } + ] + + def supported_arch(s: list[str] | typing.Callable[[str], bool]): global _supported_arch if isinstance(s, list): diff --git a/x.py b/x.py index bda3e26..f31424a 100644 --- a/x.py +++ b/x.py @@ -3,6 +3,7 @@ import importlib import os import shutil import sys +import typing import hooks import lib.arch @@ -29,6 +30,18 @@ else: lib.arch.set_target(lib.arch.get_target()) +def run_hooks(map, arg: typing.Any = None): + current_dir = os.getcwd() + for n in map.keys(): + if n in lib.make._blocked_hooks: + continue + if arg: + map[n](arg) + else: + map[n]() + os.chdir(current_dir) + + def run_build_package(package: str): try: os.chdir(f"packages/{package}") @@ -47,11 +60,7 @@ def run_build_package(package: str): exit(1) # Run `pre_build` hooks - for n in hooks.pre_build.keys(): - if n in lib.make._blocked_hooks: - continue - hooks.pre_build[n]() - os.chdir(package_dir) + run_hooks(hooks.pre_build) # Build the package if lib.make._on_build: @@ -61,12 +70,7 @@ def run_build_package(package: str): print("on_build(): Not defined") # Run `post_build` hooks - post_build_dir = os.getcwd() - for n in hooks.post_build.keys(): - if n in lib.make._blocked_hooks: - continue - hooks.post_build[n]() - os.chdir(post_build_dir) + run_hooks(hooks.post_build) # Pack packages for pkgname in lib.make._pkginfo.keys(): @@ -89,10 +93,14 @@ def run_build_package(package: str): pkginfo[lib.make._key_on_pack](composer) + # Run post_pack hooks + run_hooks(hooks.post_pack, composer) + manifest = { "name": pkgname, "version": pkgver, "arch": pkgarch, + "links": pkginfo[lib.make._key_links], } composer.write_manifest(manifest) composer.compose()