Skip to content

Commit

Permalink
Get hooks working and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eeems committed Dec 16, 2023
1 parent f97e7bc commit 6e18ebd
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 30 deletions.
2 changes: 1 addition & 1 deletion tests/test_toltec.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ def test_build_rmkit(self) -> None:
rec_dir,
],
capture_output=True,
check=False,
)

self.assertEqual(result.returncode, 0)
self.assertEqual(result.stdout.decode("utf-8"), "")
self.assertEqual(
Expand Down
13 changes: 13 additions & 0 deletions toltec/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import argparse
import os
import sys
import toltec.hooks
from importlib.util import find_spec, spec_from_file_location, module_from_spec
from typing import Dict, List, Optional
from toltec import parse_recipe
Expand Down Expand Up @@ -81,6 +82,18 @@ def main() -> int:
recipe_bundle = parse_recipe(args.recipe_dir)

with Builder(args.work_dir, args.dist_dir) as builder:
# Load built in hooks
for hook in toltec.hooks.__all__:
spec = find_spec(f"toltec.hooks.{hook}")
if spec:
module = module_from_spec(spec)
spec.loader.exec_module(module) # type: ignore
module.register(builder) # type: ignore
else:
raise RuntimeError(
f"Hook module 'toltec.hooks.{hook}' couldn’t be loaded"
)

if args.hook:
for ident in args.hook:
if ident and ident[0] in (".", "/"):
Expand Down
6 changes: 6 additions & 0 deletions toltec/hooks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
__all__ = [
"install_lib",
"patch_rm2fb",
"reload_oxide_apps",
"strip",
]
7 changes: 3 additions & 4 deletions toltec/hooks/install-lib.py → toltec/hooks/install_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ def post_package(
) -> None:
for name in (
"configure",
"postinstall",
"postremove",
"postupgrade",
"preinstall",
Expand All @@ -176,12 +175,12 @@ def post_package(
):
function = getattr(package, name)
methods = set()
for method, (src, depends) in METHODS:
for method, (src, depends) in METHODS.items():
if method in function:
methods |= set([method]) + set(depends)
methods |= set([method]) | set(depends)

for method in methods:
src = METHODS[method][0]
src, depends = METHODS[method]
setattr(
package,
name,
Expand Down
39 changes: 23 additions & 16 deletions toltec/hooks/patch_rm2fb.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""
Build hook for patching all binary objects to depend on librm2fb_client.so.1
after building a recipe.
Build hook for patching all binary objects that access /dev/fb0 to depend on
librm2fb_client.so.1 after building a recipe.
After the build() script is run, and before the artifacts are packaged, this
hook looks for ELF-files in the build directory and uses patchelf to add a
dependency on librm2fb_client.so.1 to them. This behavior is only enabled if the
recipe declares the 'patch_rm2fb' flag.
hook looks for ARM ELF-files in the build directory that access /dev/fb0.
It then uses patchelf to add a dependency on librm2fb_client.so.1 to the
binaries. This behavior is only enabled if the recipe declares the
'patch_rm2fb' flag.
"""
import os
import logging
Expand All @@ -20,6 +21,7 @@
logger = logging.getLogger(__name__)

MOUNT_SRC = "/src"
TOOLCHAIN = "toolchain:v1.3.1"


def register(builder: Builder) -> None:
Expand All @@ -37,11 +39,17 @@ def post_build(builder: Builder, recipe: Recipe, src_dir: str) -> None:
file_path = os.path.join(directory, file_name)

try:
info = ELFFile.load_from_path(file_path)
symtab = info.get_section_by_name(".symtab")
with open(file_path, "rb") as file:
info = ELFFile(file)
symtab = info.get_section_by_name(".symtab")

if symtab:
if info.get_machine_arch() == "ARM":
if symtab is None or info.get_machine_arch() != "ARM":
continue

dynamic = info.get_section_by_name(".dynamic")
rodata = info.get_section_by_name(".rodata")

if dynamic and rodata and rodata.data().find(b"/dev/fb0") != -1:
binaries.append(file_path)
except ELFError:
# Ignore non-ELF files
Expand Down Expand Up @@ -72,16 +80,15 @@ def docker_file_path(file_path: str) -> str:

for file_path in binaries:
original_mtime[file_path] = os.stat(file_path).st_mtime_ns
script.append(
"patchelf --add-needed librm2fb_client.so.1 "
+ " ".join(
docker_file_path(file_path) for file_path in binaries
)
)

script.append(
"patchelf --add-needed librm2fb_client.so.1 "
+ " ".join(docker_file_path(file_path) for file_path in binaries)
)

logs = bash.run_script_in_container(
builder.docker,
image=builder.IMAGE_PREFIX + "toolchain:v2.1",
image=builder.IMAGE_PREFIX + TOOLCHAIN,
mounts=[
docker.types.Mount(
type="bind",
Expand Down
File renamed without changes.
20 changes: 11 additions & 9 deletions toltec/hooks/strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
logger = logging.getLogger(__name__)

MOUNT_SRC = "/src"
TOOLCHAIN = "toolchain:v1.3.1"


def register(builder: Builder) -> None:
Expand All @@ -36,14 +37,15 @@ def post_build(builder: Builder, recipe: Recipe, src_dir: str) -> None:
file_path = os.path.join(directory, file_name)

try:
info = ELFFile.load_from_path(file_path)
symtab = info.get_section_by_name(".symtab")

if symtab:
if info.get_machine_arch() == "ARM":
strip_arm.append(file_path)
elif info.get_machine_arch() in ("x86", "x64"):
strip_x86.append(file_path)
with open(file_path, "rb") as file:
info = ELFFile(file)
symtab = info.get_section_by_name(".symtab")

if symtab:
if info.get_machine_arch() == "ARM":
strip_arm.append(file_path)
elif info.get_machine_arch() in ("x86", "x64"):
strip_x86.append(file_path)
except ELFError:
# Ignore non-ELF files
pass
Expand Down Expand Up @@ -102,7 +104,7 @@ def docker_file_path(file_path: str) -> str:

logs = bash.run_script_in_container(
builder.docker,
image=builder.IMAGE_PREFIX + "toolchain:v2.1",
image=builder.IMAGE_PREFIX + TOOLCHAIN,
mounts=[
docker.types.Mount(
type="bind",
Expand Down

0 comments on commit 6e18ebd

Please sign in to comment.