util.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import SCons
  2. from SCons.Subst import quote_spaces
  3. import re
  4. import os
  5. import random
  6. import string
  7. WINPATHSEP_RE = re.compile(r"\\([^\"'\\]|$)")
  8. def tempfile_arg_esc_func(arg):
  9. arg = quote_spaces(arg)
  10. if SCons.Platform.platform_default() != "win32":
  11. return arg
  12. # GCC requires double Windows slashes, let's use UNIX separator
  13. return WINPATHSEP_RE.sub(r"/\1", arg)
  14. def wrap_tempfile(env, command):
  15. env[command] = '${TEMPFILE("' + env[command] + '","$' + command + 'STR")}'
  16. def link_dir(target_path, source_path, is_windows):
  17. # print(f"link_dir: {target_path} -> {source_path}")
  18. if os.path.lexists(target_path) or os.path.exists(target_path):
  19. os.unlink(target_path)
  20. if is_windows:
  21. # Crete junction
  22. import _winapi
  23. if not os.path.isdir(source_path):
  24. raise Exception(f"Source directory {source_path} is not a directory")
  25. if not os.path.exists(target_path):
  26. _winapi.CreateJunction(source_path, target_path)
  27. else:
  28. os.symlink(source_path, target_path)
  29. def single_quote(arg_list):
  30. return " ".join(f"'{arg}'" if " " in arg else str(arg) for arg in arg_list)
  31. def link_elf_dir_as_latest(env, elf_node):
  32. elf_dir = elf_node.Dir(".")
  33. latest_dir = env.Dir("#build/latest")
  34. print(f"Setting {elf_dir} as latest built dir (./build/latest/)")
  35. return link_dir(latest_dir.abspath, elf_dir.abspath, env["PLATFORM"] == "win32")
  36. def should_gen_cdb_and_link_dir(env, requested_targets):
  37. explicitly_building_updater = False
  38. # Hacky way to check if updater-related targets were requested
  39. for build_target in requested_targets:
  40. if "updater" in str(build_target):
  41. explicitly_building_updater = True
  42. is_updater = not env["IS_BASE_FIRMWARE"]
  43. # If updater is explicitly requested, link to the latest updater
  44. # Otherwise, link to firmware
  45. return (is_updater and explicitly_building_updater) or (
  46. not is_updater and not explicitly_building_updater
  47. )