util.py 2.0 KB

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