| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 
 | 
 
 
 
 
 
 
 
 
 
 
 
 
 import shutil
 from os import SEEK_CUR, SEEK_END
 from os.path import basename, isfile, join
 
 from SCons.Script import Builder
 
 Import("env")
 
 board = env.BoardConfig()
 
 
 
 
 
 
 def extract_files(cppdefines, files_type):
 files = []
 if "build." + files_type in board:
 files.extend(
 [
 join("$PROJECT_DIR", f)
 for f in board.get("build." + files_type, "").split()
 if f
 ]
 )
 else:
 files_define = "COMPONENT_" + files_type.upper()
 for define in cppdefines:
 if files_define not in define:
 continue
 
 value = define[1]
 if not isinstance(define, tuple):
 print("Warning! %s macro cannot be empty!" % files_define)
 return []
 
 if not isinstance(value, str):
 print(
 "Warning! %s macro must contain "
 "a list of files separated by ':'" % files_define
 )
 return []
 
 for f in value.split(":"):
 if not f:
 continue
 files.append(join("$PROJECT_DIR", f))
 
 for f in files:
 if not isfile(env.subst(f)):
 print('Warning! Could not find file "%s"' % basename(f))
 
 return files
 
 
 def remove_config_define(cppdefines, files_type):
 for define in cppdefines:
 if files_type in define:
 env.ProcessUnFlags("-D%s" % "=".join(str(d) for d in define))
 return
 
 
 def prepare_file(source, target, env):
 filepath = source[0].get_abspath()
 shutil.copy(filepath, filepath + ".piobkp")
 
 with open(filepath, "rb+") as fp:
 fp.seek(-1, SEEK_END)
 if fp.read(1) != "\0":
 fp.seek(0, SEEK_CUR)
 fp.write(b"\0")
 
 
 def revert_original_file(source, target, env):
 filepath = source[0].get_abspath()
 if isfile(filepath + ".piobkp"):
 shutil.move(filepath + ".piobkp", filepath)
 
 
 def embed_files(files, files_type):
 for f in files:
 filename = basename(f) + ".txt.o"
 file_target = env.TxtToBin(join("$BUILD_DIR", filename), f)
 env.Depends("$PIOMAINPROG", file_target)
 if files_type == "embed_txtfiles":
 env.AddPreAction(file_target, prepare_file)
 env.AddPostAction(file_target, revert_original_file)
 env.AppendUnique(PIOBUILDFILES=[env.File(join("$BUILD_DIR", filename))])
 
 
 def transform_to_asm(target, source, env):
 files = [join("$BUILD_DIR", s.name + ".S") for s in source]
 return files, source
 
 
 env.Append(
 BUILDERS=dict(
 TxtToBin=Builder(
 action=env.VerboseAction(
 " ".join(
 [
 "riscv32-esp-elf-objcopy",
 "--input-target",
 "binary",
 "--output-target",
 "elf32-littleriscv",
 "--binary-architecture",
 "riscv",
 "--rename-section",
 ".data=.rodata.embedded",
 "$SOURCE",
 "$TARGET",
 ]
 ),
 "Converting $TARGET",
 ),
 suffix=".txt.o",
 ),
 TxtToAsm=Builder(
 action=env.VerboseAction(
 " ".join(
 [
 join(
 env.PioPlatform().get_package_dir("tool-cmake") or "",
 "bin",
 "cmake",
 ),
 "-DDATA_FILE=$SOURCE",
 "-DSOURCE_FILE=$TARGET",
 "-DFILE_TYPE=TEXT",
 "-P",
 join(
 env.PioPlatform().get_package_dir("framework-espidf") or "",
 "tools",
 "cmake",
 "scripts",
 "data_file_embed_asm.cmake",
 ),
 ]
 ),
 "Generating assembly for $TARGET",
 ),
 emitter=transform_to_asm,
 single_source=True,
 ),
 )
 )
 
 
 flags = env.get("CPPDEFINES")
 for files_type in ("embed_txtfiles", "embed_files"):
 if (
 "COMPONENT_" + files_type.upper() not in env.Flatten(flags)
 and "build." + files_type not in board
 ):
 continue
 
 files = extract_files(flags, files_type)
 if "espidf" in env.subst("$PIOFRAMEWORK"):
 env.Requires(join("$BUILD_DIR", "${PROGNAME}.elf"), env.TxtToAsm(files))
 else:
 embed_files(files, files_type)
 remove_config_define(flags, files_type)
 
 |