SConstruct 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. SDK_ROOT = os.path.abspath('./')
  35. if os.path.exists(SDK_ROOT + '/libraries'):
  36. libraries_path_prefix = SDK_ROOT + '/libraries'
  37. else:
  38. libraries_path_prefix = os.path.dirname(SDK_ROOT) + '/libraries'
  39. SDK_LIB = libraries_path_prefix
  40. Export('SDK_LIB')
  41. DefaultEnvironment(tools=[])
  42. env = Environment(tools = ['mingw'],
  43. AS = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
  44. CC = rtconfig.CC, CFLAGS = rtconfig.CFLAGS,
  45. CXX = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
  46. AR = rtconfig.AR, ARFLAGS = '-rc',
  47. LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
  48. env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
  49. env['ASCOM'] = env['ASPPCOM']
  50. Export('RTT_ROOT')
  51. Export('rtconfig')
  52. # prepare building environment
  53. objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc'])
  54. objs.extend(SConscript(os.path.join(libraries_path_prefix, 'drivers', 'SConscript')))
  55. # make a building
  56. DoBuilding(TARGET, objs)
  57. # 添加构建后的钩子函数
  58. env.AddPostAction(TARGET, elf2image)