SConstruct 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import os
  2. import sys
  3. import rtconfig
  4. from esptool.bin_image import ELFFile, ImageSegment, LoadFirmwareImage
  5. from esptool.targets import CHIP_DEFS, CHIP_LIST, ROM_LIST
  6. def elf2image(target, source, env):
  7. e = ELFFile("rtthread.elf")
  8. print("Creating esp32c3 image...")
  9. image = CHIP_DEFS["esp32c3"].BOOTLOADER_IMAGE()
  10. image.min_rev = 3
  11. image.entrypoint = e.entrypoint
  12. image.flash_mode = 2 # flash_mode: dio
  13. # ELFSection is a subclass of ImageSegment, so can use interchangeably
  14. image.segments = e.sections
  15. image.flash_size_freq = image.ROM_LOADER.parse_flash_size_arg("4MB")
  16. image.flash_size_freq += image.ROM_LOADER.parse_flash_freq_arg("80m")
  17. image.elf_sha256 = e.sha256()
  18. image.elf_sha256_offset = 0xb0
  19. before = len(image.segments)
  20. image.merge_adjacent_segments()
  21. if len(image.segments) != before:
  22. delta = before - len(image.segments)
  23. print("Merged %d ELF section%s" % (delta, "s" if delta > 1 else ""))
  24. image.verify()
  25. image.save("rtthread.bin")
  26. print("Successfully created esp32c3 image.")
  27. if os.getenv('RTT_ROOT'):
  28. RTT_ROOT = os.getenv('RTT_ROOT')
  29. else:
  30. RTT_ROOT = os.path.join(os.getcwd(), '..', '..')
  31. sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
  32. from building import *
  33. TARGET = 'rtthread.' + rtconfig.TARGET_EXT
  34. DefaultEnvironment(tools=[])
  35. env = Environment(tools = ['mingw'],
  36. AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
  37. CC = rtconfig.CC, CFLAGS = rtconfig.CFLAGS,
  38. CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
  39. AR = rtconfig.AR, ARFLAGS = '-rc',
  40. LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
  41. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  42. env['ASCOM'] = env['ASPPCOM']
  43. Export('RTT_ROOT')
  44. Export('rtconfig')
  45. # prepare building environment
  46. objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc'])
  47. # make a building
  48. DoBuilding(TARGET, objs)
  49. # 添加构建后的钩子函数
  50. env.AddPostAction(TARGET, elf2image)