12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from os import SEEK_CUR , SEEK_END
16
- from os .path import basename , isfile , join
15
+ import shutil
16
+ from os import SEEK_CUR , SEEK_END , makedirs
17
+ from os .path import basename , isfile , isdir , join
17
18
18
19
from SCons .Script import Builder
19
20
20
21
from platformio .util import cd
21
22
22
23
Import ("env" )
23
24
24
-
25
25
#
26
- # TXT files helpers
26
+ # Embedded files helpers
27
27
#
28
28
29
29
30
30
def prepare_files (files ):
31
31
if not files :
32
32
return
33
33
34
+ fixed_files = []
35
+ build_dir = env .subst ("$BUILD_DIR" )
36
+ if not isdir (build_dir ):
37
+ makedirs (build_dir )
34
38
for f in files :
35
- with open (env .subst (f ), "rb+" ) as fp :
39
+ fixed_file = join (build_dir , basename (f ))
40
+ shutil .copy (env .subst (f ), fixed_file )
41
+ with open (fixed_file , "rb+" ) as fp :
36
42
fp .seek (- 1 , SEEK_END )
37
43
if fp .read (1 ) != '\0 ' :
38
44
fp .seek (0 , SEEK_CUR )
39
45
fp .write (b'\0 ' )
40
46
47
+ fixed_files .append (fixed_file )
48
+
49
+ return fixed_files
50
+
41
51
42
- def extract_files (cppdefines ):
52
+ def extract_files (cppdefines , files_type ):
43
53
for define in cppdefines :
44
- if "COMPONENT_EMBED_TXTFILES" not in define :
54
+ if files_type not in define :
45
55
continue
46
56
47
57
if not isinstance (define , tuple ):
48
- print ("Warning! COMPONENT_EMBED_TXTFILES macro cannot be empty!" )
58
+ print ("Warning! %s macro cannot be empty!" % files_type )
49
59
return []
50
60
51
61
with cd (env .subst ("$PROJECT_DIR" )):
52
62
value = define [1 ]
53
63
if not isinstance (value , str ):
54
- print ("Warning! COMPONENT_EMBED_TXTFILES macro must contain "
55
- "a list of files separated by ':'" )
64
+ print ("Warning! %s macro must contain "
65
+ "a list of files separated by ':'" % files_type )
56
66
return []
57
67
58
68
result = []
@@ -65,9 +75,9 @@ def extract_files(cppdefines):
65
75
return result
66
76
67
77
68
- def remove_config_define (cppdefines ):
78
+ def remove_config_define (cppdefines , files_type ):
69
79
for define in cppdefines :
70
- if "COMPONENT_EMBED_TXTFILES" in define :
80
+ if files_type in define :
71
81
env .ProcessUnFlags ("-D%s" % "=" .join (str (d ) for d in define ))
72
82
return
73
83
@@ -94,9 +104,14 @@ def embed_files(files):
94
104
suffix = ".txt.o" ))
95
105
)
96
106
107
+
108
+
97
109
flags = env .get ("CPPDEFINES" )
98
- if "COMPONENT_EMBED_TXTFILES" in env .Flatten (flags ):
99
- files = extract_files (flags )
100
- prepare_files (files )
110
+ for component_files in ("COMPONENT_EMBED_TXTFILES" , "COMPONENT_EMBED_FILES" ):
111
+ if component_files not in env .Flatten (flags ):
112
+ continue
113
+ files = extract_files (flags , component_files )
114
+ if component_files == "COMPONENT_EMBED_TXTFILES" :
115
+ files = prepare_files (files )
101
116
embed_files (files )
102
- remove_config_define (flags )
117
+ remove_config_define (flags , component_files )
0 commit comments