Skip to content

Commit

Permalink
hash target files and pass as define
Browse files Browse the repository at this point in the history
  • Loading branch information
bkleiner committed Nov 26, 2024
1 parent b656b0e commit f3c99a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
8 changes: 6 additions & 2 deletions script/post_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
Import("env")

try:
from target_inject import process_elf_action, inject_target, copy_hex
from target_inject import target_hash, process_elf_action, inject_target, copy_hex
except ImportError:
env.Execute("$PYTHONEXE -m pip install pyelftools pyyaml cbor2")
from target_inject import process_elf_action, inject_target, copy_hex
from target_inject import target_hash, process_elf_action, inject_target, copy_hex


def before_upload(source, target, env):
Expand All @@ -30,6 +30,10 @@ def before_upload(source, target, env):
while option in env[scope]:
env[scope].remove(option)

env.Append(
CPPDEFINES=[("TARGET_HASH", target_hash(env))],
)

objcopy_args = [
"$OBJCOPY",
"-S",
Expand Down
32 changes: 19 additions & 13 deletions script/pre_script.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# pylint: disable=missing-docstring,redefined-outer-name,unspecified-encoding

import os
import threading

Import("env")

Expand Down Expand Up @@ -70,18 +71,23 @@ def touch(fname, times=None):
os.utime(fname, times)


target_remote = "https://github.com/BossHobby/Targets.git"
target_dir = os.path.join(env["PROJECT_DIR"], "targets")
def fetch_thread():
target_remote = "https://github.com/BossHobby/Targets.git"
target_dir = os.path.join(env["PROJECT_DIR"], "targets")

if not os.path.exists(target_dir):
porcelain.clone(target_remote, target=target_dir, branch="targets")
touch(os.path.join(env["PROJECT_DIR"], "platformio.ini"))
else:
target_repo = porcelain.open_repo(target_dir)
target_ref_before = porcelain.describe(target_repo)
porcelain.fetch(target_repo)
porcelain.checkout_branch(target_repo, "targets")
target_ref_after = porcelain.describe(target_repo)

if target_ref_before != target_ref_after:
if not os.path.exists(target_dir):
porcelain.clone(target_remote, target=target_dir, branch="targets")
touch(os.path.join(env["PROJECT_DIR"], "platformio.ini"))
else:
target_repo = porcelain.open_repo(target_dir)
target_ref_before = porcelain.describe(target_repo)
porcelain.fetch(target_repo)
porcelain.checkout_branch(target_repo, "targets")
target_ref_after = porcelain.describe(target_repo)

if target_ref_before != target_ref_after:
touch(os.path.join(env["PROJECT_DIR"], "platformio.ini"))


t = threading.Thread(target=fetch_thread)
t.start()
13 changes: 13 additions & 0 deletions script/target_inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import shutil
import cbor2
import yaml
import hashlib

from elftools.elf.elffile import ELFFile

Expand Down Expand Up @@ -71,3 +72,15 @@ def copy_hex(source, target, env):
env.subst("$BUILD_DIR/${PROGNAME}.hex"),
env.subst(os.path.join("output", hex_name)),
)


def target_hash(env):
target_yaml = env.subst("targets/${PIOENV}.yaml")
if not os.path.isfile(target_yaml):
return ""

md5 = hashlib.md5()
with open(target_yaml, "rb") as f:
for chunk in iter(lambda: f.read(2**20), b""):
md5.update(chunk)
return md5.hexdigest()

0 comments on commit f3c99a2

Please sign in to comment.