import os
import sys
import rtconfig
from esptool.bin_image import ELFFile, ImageSegment, LoadFirmwareImage
from esptool.targets import CHIP_DEFS, CHIP_LIST, ROM_LIST

def elf2image(target, source, env):
    e = ELFFile("rtthread.elf")

    print("Creating esp32c3 image...")

    image = CHIP_DEFS["esp32c3"].BOOTLOADER_IMAGE()
    image.min_rev = 3
    image.entrypoint = e.entrypoint
    image.flash_mode = 2 # flash_mode: dio

    # ELFSection is a subclass of ImageSegment, so can use interchangeably
    image.segments = e.sections
    image.flash_size_freq = image.ROM_LOADER.parse_flash_size_arg("4MB")
    image.flash_size_freq += image.ROM_LOADER.parse_flash_freq_arg("80m")

    image.elf_sha256 = e.sha256()
    image.elf_sha256_offset = 0xb0

    before = len(image.segments)
    image.merge_adjacent_segments()
    if len(image.segments) != before:
        delta = before - len(image.segments)
        print("Merged %d ELF section%s" % (delta, "s" if delta > 1 else ""))

    image.verify()

    image.save("rtthread.bin")

    print("Successfully created esp32c3 image.")

if os.getenv('RTT_ROOT'):
    RTT_ROOT = os.getenv('RTT_ROOT')
else:
    RTT_ROOT = os.path.join(os.getcwd(), '..', '..', '..')

sys.path = sys.path + [os.path.join(RTT_ROOT, 'tools')]
from building import *

TARGET = 'rtthread.' + rtconfig.TARGET_EXT
SDK_ROOT = os.path.abspath('./')

if os.path.exists(SDK_ROOT + '/libraries'):
    libraries_path_prefix = SDK_ROOT + '/libraries'
else:
    libraries_path_prefix = os.path.dirname(SDK_ROOT) + '/libraries'

SDK_LIB = libraries_path_prefix
Export('SDK_LIB')
DefaultEnvironment(tools=[])
env = Environment(tools = ['mingw'],
    AS   = rtconfig.AS, ASFLAGS = rtconfig.AFLAGS,
    CC   = rtconfig.CC, CFLAGS = rtconfig.CFLAGS,
    CXX  = rtconfig.CXX, CXXFLAGS = rtconfig.CXXFLAGS,
    AR   = rtconfig.AR, ARFLAGS = '-rc',
    LINK = rtconfig.LINK, LINKFLAGS = rtconfig.LFLAGS)
env.PrependENVPath('PATH', rtconfig.EXEC_PATH)
env['ASCOM'] = env['ASPPCOM']

Export('RTT_ROOT')
Export('rtconfig')

# prepare building environment
objs = PrepareBuilding(env, RTT_ROOT, remove_components = ['libc'])

objs.extend(SConscript(os.path.join(libraries_path_prefix, 'drivers', 'SConscript')))
# make a building
DoBuilding(TARGET, objs)

# 添加构建后的钩子函数
env.AddPostAction(TARGET, elf2image)
