|
| 1 | +# Copyright 2014-present PlatformIO <[email protected]> |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import shutil |
| 16 | +from os import SEEK_CUR, SEEK_END |
| 17 | +from os.path import basename, isfile, join |
| 18 | + |
| 19 | +from SCons.Script import Builder |
| 20 | + |
| 21 | +from platformio.util import cd |
| 22 | + |
| 23 | +Import("env") |
| 24 | + |
| 25 | +board = env.BoardConfig() |
| 26 | + |
| 27 | +# |
| 28 | +# Embedded files helpers |
| 29 | +# |
| 30 | + |
| 31 | +def extract_files(cppdefines, files_type): |
| 32 | + files = [] |
| 33 | + if "build." + files_type in board: |
| 34 | + files.extend( |
| 35 | + [join("$PROJECT_DIR", f) for f in board.get( |
| 36 | + "build." + files_type, "").split() if f]) |
| 37 | + else: |
| 38 | + files_define = "COMPONENT_" + files_type.upper() |
| 39 | + for define in cppdefines: |
| 40 | + if files_define not in define: |
| 41 | + continue |
| 42 | + |
| 43 | + value = define[1] |
| 44 | + if not isinstance(define, tuple): |
| 45 | + print("Warning! %s macro cannot be empty!" % files_define) |
| 46 | + return [] |
| 47 | + |
| 48 | + if not isinstance(value, str): |
| 49 | + print("Warning! %s macro must contain " |
| 50 | + "a list of files separated by ':'" % files_define) |
| 51 | + return [] |
| 52 | + |
| 53 | + for f in value.split(':'): |
| 54 | + if not f: |
| 55 | + continue |
| 56 | + files.append(join("$PROJECT_DIR", f)) |
| 57 | + |
| 58 | + for f in files: |
| 59 | + if not isfile(env.subst(f)): |
| 60 | + print("Warning! Could not find file \"%s\"" % basename(f)) |
| 61 | + |
| 62 | + return files |
| 63 | + |
| 64 | + |
| 65 | +def remove_config_define(cppdefines, files_type): |
| 66 | + for define in cppdefines: |
| 67 | + if files_type in define: |
| 68 | + env.ProcessUnFlags("-D%s" % "=".join(str(d) for d in define)) |
| 69 | + return |
| 70 | + |
| 71 | + |
| 72 | +def prepare_file(source, target, env): |
| 73 | + filepath = source[0].get_abspath() |
| 74 | + shutil.copy(filepath, filepath + ".piobkp") |
| 75 | + |
| 76 | + with open(filepath, "rb+") as fp: |
| 77 | + fp.seek(-1, SEEK_END) |
| 78 | + if fp.read(1) != '\0': |
| 79 | + fp.seek(0, SEEK_CUR) |
| 80 | + fp.write(b'\0') |
| 81 | + |
| 82 | + |
| 83 | +def revert_original_file(source, target, env): |
| 84 | + filepath = source[0].get_abspath() |
| 85 | + if isfile(filepath + ".piobkp"): |
| 86 | + shutil.move(filepath + ".piobkp", filepath) |
| 87 | + |
| 88 | + |
| 89 | +def embed_files(files, files_type): |
| 90 | + for f in files: |
| 91 | + filename = basename(f) + ".txt.o" |
| 92 | + file_target = env.TxtToBin(join("$BUILD_DIR", filename), f) |
| 93 | + env.Depends("$PIOMAINPROG", file_target) |
| 94 | + if files_type == "embed_txtfiles": |
| 95 | + env.AddPreAction(file_target, prepare_file) |
| 96 | + env.AddPostAction(file_target, revert_original_file) |
| 97 | + env.Append(PIOBUILDFILES=[env.File(join("$BUILD_DIR", filename))]) |
| 98 | + |
| 99 | + |
| 100 | +env.Append( |
| 101 | + BUILDERS=dict( |
| 102 | + 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")) |
| 112 | +) |
| 113 | + |
| 114 | +flags = env.get("CPPDEFINES") |
| 115 | +for 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: |
| 118 | + continue |
| 119 | + |
| 120 | + files = extract_files(flags, files_type) |
| 121 | + embed_files(files, files_type) |
| 122 | + remove_config_define(flags, files_type) |
0 commit comments