Skip to content

Commit 671234e

Browse files
committed
Update support for embedding files // Issue #161, Resolves #220
1 parent cfe5140 commit 671234e

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

builder/frameworks/_embedtxt_files.py renamed to builder/frameworks/_embed_files.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,47 +12,57 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

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
1718

1819
from SCons.Script import Builder
1920

2021
from platformio.util import cd
2122

2223
Import("env")
2324

24-
2525
#
26-
# TXT files helpers
26+
# Embedded files helpers
2727
#
2828

2929

3030
def prepare_files(files):
3131
if not files:
3232
return
3333

34+
fixed_files = []
35+
build_dir = env.subst("$BUILD_DIR")
36+
if not isdir(build_dir):
37+
makedirs(build_dir)
3438
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:
3642
fp.seek(-1, SEEK_END)
3743
if fp.read(1) != '\0':
3844
fp.seek(0, SEEK_CUR)
3945
fp.write(b'\0')
4046

47+
fixed_files.append(fixed_file)
48+
49+
return fixed_files
50+
4151

42-
def extract_files(cppdefines):
52+
def extract_files(cppdefines, files_type):
4353
for define in cppdefines:
44-
if "COMPONENT_EMBED_TXTFILES" not in define:
54+
if files_type not in define:
4555
continue
4656

4757
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)
4959
return []
5060

5161
with cd(env.subst("$PROJECT_DIR")):
5262
value = define[1]
5363
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)
5666
return []
5767

5868
result = []
@@ -65,9 +75,9 @@ def extract_files(cppdefines):
6575
return result
6676

6777

68-
def remove_config_define(cppdefines):
78+
def remove_config_define(cppdefines, files_type):
6979
for define in cppdefines:
70-
if "COMPONENT_EMBED_TXTFILES" in define:
80+
if files_type in define:
7181
env.ProcessUnFlags("-D%s" % "=".join(str(d) for d in define))
7282
return
7383

@@ -94,9 +104,14 @@ def embed_files(files):
94104
suffix=".txt.o"))
95105
)
96106

107+
108+
97109
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)
101116
embed_files(files)
102-
remove_config_define(flags)
117+
remove_config_define(flags, component_files)

builder/frameworks/arduino.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
env = DefaultEnvironment()
3030

31-
SConscript("_embedtxt_files.py", exports="env")
31+
SConscript("_embed_files.py", exports="env")
3232

3333
if "espidf" not in env.subst("$PIOFRAMEWORK"):
3434
SConscript(

builder/frameworks/espidf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
platform = env.PioPlatform()
3434

3535
env.SConscript("_bare.py", exports="env")
36-
env.SConscript("_embedtxt_files.py", exports="env")
36+
env.SConscript("_embed_files.py", exports="env")
3737

3838
ulp_lib = None
3939
ulp_dir = join(env.subst("$PROJECT_DIR"), "ulp")

0 commit comments

Comments
 (0)