1818
1919from SCons .Script import Builder
2020
21- from platformio .util import cd
22-
2321Import ("env" )
2422
2523board = env .BoardConfig ()
2826# Embedded files helpers
2927#
3028
29+
3130def extract_files (cppdefines , files_type ):
3231 files = []
3332 if "build." + files_type in board :
3433 files .extend (
35- [join ("$PROJECT_DIR" , f ) for f in board .get (
36- "build." + files_type , "" ).split () if f ])
34+ [
35+ join ("$PROJECT_DIR" , f )
36+ for f in board .get ("build." + files_type , "" ).split ()
37+ if f
38+ ]
39+ )
3740 else :
3841 files_define = "COMPONENT_" + files_type .upper ()
3942 for define in cppdefines :
@@ -46,18 +49,20 @@ def extract_files(cppdefines, files_type):
4649 return []
4750
4851 if not isinstance (value , str ):
49- print ("Warning! %s macro must contain "
50- "a list of files separated by ':'" % files_define )
52+ print (
53+ "Warning! %s macro must contain "
54+ "a list of files separated by ':'" % files_define
55+ )
5156 return []
5257
53- for f in value .split (':' ):
58+ for f in value .split (":" ):
5459 if not f :
5560 continue
5661 files .append (join ("$PROJECT_DIR" , f ))
5762
5863 for f in files :
5964 if not isfile (env .subst (f )):
60- print (" Warning! Could not find file \ " %s\" " % basename (f ))
65+ print (' Warning! Could not find file "%s"' % basename (f ))
6166
6267 return files
6368
@@ -75,9 +80,9 @@ def prepare_file(source, target, env):
7580
7681 with open (filepath , "rb+" ) as fp :
7782 fp .seek (- 1 , SEEK_END )
78- if fp .read (1 ) != ' \0 ' :
83+ if fp .read (1 ) != " \0 " :
7984 fp .seek (0 , SEEK_CUR )
80- fp .write (b' \0 ' )
85+ fp .write (b" \0 " )
8186
8287
8388def revert_original_file (source , target , env ):
@@ -97,26 +102,74 @@ def embed_files(files, files_type):
97102 env .AppendUnique (PIOBUILDFILES = [env .File (join ("$BUILD_DIR" , filename ))])
98103
99104
105+ def transform_to_asm (target , source , env ):
106+ return [join ("$BUILD_DIR" , s .name + ".S" ) for s in source ], source
107+
100108env .Append (
101109 BUILDERS = dict (
102110 TxtToBin = Builder (
103- action = env .VerboseAction (" " .join ([
104- "xtensa-esp32-elf-objcopy" ,
105- "--input-target" , "binary" ,
106- "--output-target" , "elf32-xtensa-le" ,
107- "--binary-architecture" , "xtensa" ,
108- "--rename-section" , ".data=.rodata.embedded" ,
109- "$SOURCE" , "$TARGET"
110- ]), "Converting $TARGET" ),
111- suffix = ".txt.o" ))
111+ action = env .VerboseAction (
112+ " " .join (
113+ [
114+ "xtensa-esp32-elf-objcopy" ,
115+ "--input-target" ,
116+ "binary" ,
117+ "--output-target" ,
118+ "elf32-xtensa-le" ,
119+ "--binary-architecture" ,
120+ "xtensa" ,
121+ "--rename-section" ,
122+ ".data=.rodata.embedded" ,
123+ "$SOURCE" ,
124+ "$TARGET" ,
125+ ]
126+ ),
127+ "Converting $TARGET" ,
128+ ),
129+ suffix = ".txt.o" ,
130+ ),
131+ TxtToAsm = Builder (
132+ action = env .VerboseAction (
133+ " " .join (
134+ [
135+ join (
136+ env .PioPlatform ().get_package_dir ("tool-cmake" ) or "" ,
137+ "bin" ,
138+ "cmake" ,
139+ ),
140+ "-DDATA_FILE=$SOURCE" ,
141+ "-DSOURCE_FILE=$TARGET" ,
142+ "-DFILE_TYPE=TEXT" ,
143+ "-P" ,
144+ join (
145+ env .PioPlatform ().get_package_dir ("framework-espidf" ) or "" ,
146+ "tools" ,
147+ "cmake" ,
148+ "scripts" ,
149+ "data_file_embed_asm.cmake" ,
150+ ),
151+ ]
152+ ),
153+ "Generating assembly for $TARGET" ,
154+ ),
155+ emitter = transform_to_asm ,
156+ single_source = True
157+ ),
158+ )
112159)
113160
161+
114162flags = env .get ("CPPDEFINES" )
115163for files_type in ("embed_txtfiles" , "embed_files" ):
116- if "COMPONENT_" + files_type .upper () not in env .Flatten (
117- flags ) and "build." + files_type not in board :
164+ if (
165+ "COMPONENT_" + files_type .upper () not in env .Flatten (flags )
166+ and "build." + files_type not in board
167+ ):
118168 continue
119169
120170 files = extract_files (flags , files_type )
121- embed_files (files , files_type )
122- remove_config_define (flags , files_type )
171+ if "espidf" in env .subst ("$PIOFRAMEWORK" ):
172+ env .Requires (join ("$BUILD_DIR" , "${PROGNAME}.elf" ), env .TxtToAsm (files ))
173+ else :
174+ embed_files (files , files_type )
175+ remove_config_define (flags , files_type )
0 commit comments