Skip to content

Commit

Permalink
修复错误,优化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmozhijin committed Sep 22, 2024
1 parent e814d1a commit 0d5e538
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 deletions.
27 changes: 21 additions & 6 deletions build_helper/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import re
import shutil
import tarfile
import tempfile
import zipfile

from actions_toolkit import core
Expand Down Expand Up @@ -39,11 +38,11 @@ def get_cache_restore_key(openwrt: OpenWrt, cfg: dict) -> str:


def prepare(cfg: dict) -> None:
setup_env(cfg["name"] in ("base-builds", "build-packages", "build-ImageBuilder"), cfg["name"] in ("build-packages", "build-ImageBuilder"))
context = Context()
logger.debug("job: %s", context.job)
setup_env(context.job in ("build-packages", "build-ImageBuilder"), context.job in ("build-packages", "build-ImageBuilder"))

tmpdir = tempfile.TemporaryDirectory()
tmpdir = paths.get_tmpdir()

logger.info("还原openwrt源码...")
path = dl_artifact(f"openwrt-source-{cfg["name"]}", tmpdir.name)
Expand All @@ -53,7 +52,7 @@ def prepare(cfg: dict) -> None:
tar_ref.extractall(paths.workdir) # noqa: S202
openwrt = OpenWrt(os.path.join(paths.workdir, "openwrt"))

if context.job.startswith("base-builds"):
if context.job == "base-builds":
logger.info("构建toolchain缓存key...")
toolchain_key = f"toolchain-{hash_dirs((os.path.join(openwrt.path, "tools"), os.path.join(openwrt.path, "toolchain")))}"
target, subtarget = openwrt.get_target()
Expand All @@ -63,7 +62,7 @@ def prepare(cfg: dict) -> None:
toolchain_key += f"-{subtarget}"
core.set_output("toolchain-key", toolchain_key)

elif context.job.startswith(("build-packages", "build-ImageBuilder")):
elif context.job in ("build-packages", "build-ImageBuilder"):
if os.path.exists(os.path.join(openwrt.path, "staging_dir")):
shutil.rmtree(os.path.join(openwrt.path, "staging_dir"))
base_builds_path = dl_artifact(f"base-builds-{cfg['name']}", tmpdir.name)
Expand All @@ -72,7 +71,7 @@ def prepare(cfg: dict) -> None:
with tarfile.open(os.path.join(tmpdir.name, "builds.tar.gz"), "r:gz") as tar:
tar.extractall(openwrt.path) # noqa: S202

elif context.job.startswith("build-images"):
elif context.job == "build-images":
ib_path = dl_artifact(f"Image_Builder-{cfg["name"]}", tmpdir.name)
with zipfile.ZipFile(ib_path, "r") as zip_ref:
zip_ref.extract("openwrt-imagebuilder.tar.xz", tmpdir.name)
Expand Down Expand Up @@ -105,6 +104,12 @@ def prepare(cfg: dict) -> None:
def base_builds(cfg: dict) -> None:
openwrt = OpenWrt(os.path.join(paths.workdir, "openwrt"))

ccache_path = os.path.join(openwrt.path, ".ccache")
tmp_ccache_path = None
if os.path.exists(ccache_path):
tmp_ccache_path = paths.get_tmpdir()
shutil.move(ccache_path, tmp_ccache_path.name)

logger.info("修改配置(设置编译所有kmod)...")
openwrt.enable_kmods(cfg["compile"]["kmod_compile_exclude_list"])

Expand All @@ -118,6 +123,9 @@ def base_builds(cfg: dict) -> None:
openwrt.make("toolchain/install")

logger.info("开始编译内核...")
openwrt.make("clean")
if tmp_ccache_path:
shutil.move(tmp_ccache_path.name, ccache_path)
openwrt.make("target/compile")

logger.info("归档文件...")
Expand Down Expand Up @@ -156,6 +164,7 @@ def build_packages(cfg: dict) -> None:

def build_image_builder(cfg: dict) -> None:
openwrt = OpenWrt(os.path.join(paths.workdir, "openwrt"))
targetinfo = openwrt.get_targetinfo()

logger.info("修改配置(设置编译所有kmod/取消编译其他软件包/取消生成镜像/)...")
openwrt.enable_kmods(cfg["compile"]["kmod_compile_exclude_list"], only_kmods=True)
Expand All @@ -181,6 +190,12 @@ def build_image_builder(cfg: dict) -> None:
logger.info("开始生成软件包...")
openwrt.make("package/install")

if targetinfo and "bcm27xx-gpu-fw" in targetinfo["default_packages"]:
# 不知道为什么它就是没有被执行Build/InstallDev中的命令把东西复制到KERNEL_BUILD_DIR下
# https://github.com/openwrt/openwrt/blob/main/package/kernel/bcm27xx-gpu-fw/Makefile
logger.info("编译bcm27xx-gpu-fw...")
openwrt.make("package/bcm27xx-gpu-fw/compile", debug=True)

logger.info("制作Image Builder包...")
openwrt.make("target/install")

Expand Down
3 changes: 1 addition & 2 deletions build_helper/prepare.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import re
import shutil
import tarfile
import tempfile
from multiprocessing.pool import Pool
from typing import TYPE_CHECKING, Any

Expand Down Expand Up @@ -319,7 +318,7 @@ def prepare_cfg(config: dict[str, Any],
case _:
adg_arch, clash_arch = None, None

tmpdir = tempfile.TemporaryDirectory()
tmpdir = paths.get_tmpdir()
dl_tasks: list[SmartDL] = []
if adg_arch and openwrt.get_package_config("luci-app-adguardhome") == "y":
logger.info("%s下载架构为%s的AdGuardHome核心", cfg_name, adg_arch)
Expand Down
2 changes: 1 addition & 1 deletion build_helper/releases.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def releases(cfg: dict) -> None:
logger.info("下载artifact...")


tmpdir = tempfile.TemporaryDirectory()
tmpdir = paths.get_tmpdir()
pkgs_archive_path = dl_artifact(f"packages-{cfg['name']}", tmpdir.name)
shutil.move(pkgs_archive_path, os.path.join(paths.uploads, "packages.zip"))
pkgs_archive_path = os.path.join(paths.uploads, "packages.zip")
Expand Down
10 changes: 6 additions & 4 deletions build_helper/utils/openwrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,15 @@ def get_targetinfos(self) -> dict:

def get_targetinfo(self) -> dict | None:
targets = self.get_targetinfos()
target = None
targetinfos = None
with open(os.path.join(self.path, ".config")) as f:
for line in f:
if match := re.match(r"CONFIG_TARGET_(?P<target>[^=]+)=", line):
target = targets.get(match.group('target').replace("_", "/"), target)

return target
target = match.group('target').replace("_", "/")
targetinfos = targets.get(target, targetinfos)
if targetinfos and target:
targetinfos["target"] = target
return targetinfos

def enable_kmods(self, exclude_list: list[str], only_kmods: bool = False) -> None:
packages = self.get_packageinfos()
Expand Down
6 changes: 5 additions & 1 deletion build_helper/utils/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

from actions_toolkit import core

import tempfile

class Paths:

Expand Down Expand Up @@ -68,5 +68,9 @@ def errorinfo(self) -> str:
elif not os.path.isdir(errorinfo):
core.set_failed(f"错误信息路径 {errorinfo} 不是一个目录")
return errorinfo

def get_tmpdir(self) -> tempfile.TemporaryDirectory:
return tempfile.TemporaryDirectory(dir=os.path.join(self.root, "tmp"))


paths = Paths()
2 changes: 0 additions & 2 deletions build_helper/utils/upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,5 @@ def save(self) -> None:
logger.debug("Save UpLoad Action file to %s0", self.action_file)
with open(self.action_file, 'w', encoding='utf-8') as file:
yaml.dump(self.action, file, allow_unicode=True, sort_keys=False)
else:
logger.warning("No Artifact to Upload")

uploader = UpLoader()

0 comments on commit 0d5e538

Please sign in to comment.