Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.1 KiB

  1. import os
  2. import sys
  3. def libalgobsec_inject_patcher(env):
  4. libalgobsec_builder = next(
  5. (
  6. builder
  7. for builder in env.GetLibBuilders()
  8. if builder.name == "BSEC Software Library"
  9. ),
  10. None,
  11. )
  12. if libalgobsec_builder is None:
  13. return
  14. def process_archive(target, source, env):
  15. import subprocess
  16. # Allows `import espurna_utils` for external scripts, where we might not be within scons runtime
  17. from SCons.Script import Delete, Mkdir
  18. tmpdir = env.get(
  19. "ESPURNA_LIBALGOBSEC_PATCHER_TMPDIR",
  20. os.path.join(str(target[0].get_dir()), "_tmpdir"),
  21. )
  22. env.Execute(Mkdir(tmpdir))
  23. # XXX: $AR does not support output argument for the extraction
  24. # always switch into tmpdir when running commands
  25. def run(cmd):
  26. sys.stdout.write(" ".join(cmd))
  27. sys.stdout.write("\n")
  28. subprocess.check_call(cmd, cwd=tmpdir)
  29. run([env.subst("$AR"), "x", source[0].abspath])
  30. names = []
  31. for infilename in os.listdir(tmpdir):
  32. newname = infilename
  33. if not infilename.endswith(".c.o"):
  34. newname = infilename.replace(".o", ".c.o")
  35. os.rename(os.path.join(tmpdir, infilename), os.path.join(tmpdir, newname))
  36. names.append(newname)
  37. pack_cmd = [env.subst("$AR"), "cr", target[0].abspath]
  38. pack_cmd.extend(names)
  39. run(pack_cmd)
  40. env.Execute(Delete(tmpdir))
  41. # Instead of replacing the file in-place, link with the patched version
  42. libalgobsec_dir = os.path.join(libalgobsec_builder.src_dir, "esp8266")
  43. target = env.File(
  44. "libalgobsec.a", directory=env.subst("$BUILD_DIR/libalgobsec_patched")
  45. )
  46. source = env.File("libalgobsec.a", directory=libalgobsec_dir)
  47. command = env.Command(target, source, process_archive)
  48. patcher = env.Alias("patch-libalgobsec", command)
  49. env.Append(LIBPATH=[target.get_dir()])
  50. env.Append(LIBS=["algobsec"])
  51. env.Depends("$BUILD_DIR/${PROGNAME}.elf", patcher)