-
Notifications
You must be signed in to change notification settings - Fork 638
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patch libalgobsec.a by repackaging archive
- Loading branch information
1 parent
f77e047
commit 35a92d9
Showing
9 changed files
with
197 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import os | ||
|
||
def bsec_inject_patcher(env): | ||
platform = env.PioPlatform() | ||
framework_dir = platform.get_package_dir("framework-arduinoespressif8266") | ||
bsec_dir = os.path.join( | ||
"${PROJECT_LIBDEPS_DIR}", "${PIOENV}", "BSEC Software Library", "src", "esp8266" | ||
) | ||
|
||
env.Append(LIBPATH=["${PROJECT_LIBDEPS_DIR}/${PIOENV}/BSEC Software Library/src/esp8266"]) | ||
env.Append(LIBS=["algobsec"]) | ||
|
||
patchflag_path = os.path.join(env.subst(bsec_dir), ".patching-done") | ||
|
||
if os.path.isfile(patchflag_path): | ||
print("Archive libalgosec.a has already been patched") | ||
return | ||
|
||
toolchain_ar = os.path.join( | ||
platform.get_package_dir("toolchain-xtensa"), "bin", "xtensa-lx106-elf-ar" | ||
) | ||
|
||
# Backup libalgobsec.a into libalgobsec.a.orig. | ||
backup_action = env.VerboseAction( | ||
" ".join( | ||
[ | ||
'/bin/mv', | ||
os.path.join(bsec_dir.replace(' ', '\\ '), "libalgobsec.a"), | ||
os.path.join(bsec_dir.replace(' ', '\\ '), "libalgobsec.a.orig") | ||
] | ||
), | ||
"Backing up BSEC libalgosec.a to libalgosec.a.orig", | ||
) | ||
|
||
# Extract libalgobsec.a into *.o files. | ||
extract_action = env.VerboseAction( | ||
" ".join( | ||
[ | ||
toolchain_ar, | ||
"-xv", | ||
os.path.join(bsec_dir.replace(' ', '\\ '), "libalgobsec.a.orig") | ||
] | ||
), | ||
"Extracting BSEC libalgobsec.a.orig", | ||
) | ||
|
||
# Archive *.c.o files into libalgobsec.a. | ||
archive_action = env.VerboseAction( | ||
" ".join( | ||
[ | ||
toolchain_ar, | ||
"-crv", | ||
os.path.join(bsec_dir.replace(' ', '\\ '), "libalgobsec.a"), | ||
"*.c.o" | ||
] | ||
), | ||
"Archiving BSEC libalgosec.a", | ||
) | ||
|
||
# Create patch flag file. | ||
touch_action = env.VerboseAction( | ||
" ".join( | ||
[ | ||
"/usr/bin/touch", | ||
patchflag_path.replace(' ', '\\ '), | ||
] | ||
), | ||
"Touching patch flag", | ||
) | ||
|
||
# Remove renamed files. | ||
cleanup_action = env.VerboseAction( | ||
" ".join( | ||
[ | ||
"rm", | ||
"*.c.o", | ||
] | ||
), | ||
"Cleaning up", | ||
) | ||
|
||
env.Execute(backup_action) | ||
env.Execute(extract_action) | ||
|
||
rename_files() | ||
|
||
env.Execute(archive_action) | ||
env.Execute(touch_action) | ||
env.Execute(cleanup_action) | ||
|
||
|
||
# Rename *.o to *.c.o so that the linker places this library | ||
# into ROM instead of RAM. | ||
def rename_files(): | ||
folder = os.getcwd(); | ||
for filename in os.listdir(folder): | ||
infilename = os.path.join(folder, filename) | ||
if not os.path.isfile(infilename): | ||
continue | ||
|
||
(name, ext) = os.path.splitext(filename) | ||
print(ext) | ||
if not ext == ".o": | ||
continue | ||
|
||
newname = filename.replace('.o', '.c.o') | ||
output = os.rename(infilename, newname) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import os | ||
import sys | ||
import pathlib | ||
|
||
from elftools.elf.elffile import ELFFile | ||
|
||
def get_section_names(filename, patterns): | ||
with open(filename, 'rb') as f: | ||
elffile = ELFFile(f) | ||
|
||
for section in elffile.iter_sections(): | ||
for pattern in patterns: | ||
if section.name.startswith(pattern): | ||
yield section.name | ||
|
||
|
||
if __name__ == '__main__': | ||
targets = {} | ||
|
||
for fobj in pathlib.Path.cwd().rglob(f'*.o'): | ||
for match in get_section_names(fobj.name, (".text", ".literal")): | ||
targets.setdefault(fobj.name, []) | ||
targets[fobj.name].append(match) | ||
|
||
print("mkdir updated") | ||
for name, sections in targets.items(): | ||
cmd = [sys.argv[1]] | ||
for section in sections: | ||
updated = section | ||
if updated.startswith(".text"): | ||
updated = updated[len(".text"):] | ||
updated = ".irom0.text{}".format(updated) | ||
elif updated.startswith(".literal"): | ||
updated = ".irom0.text{}".format(updated) | ||
cmd.extend([ | ||
'--rename-section', | ||
'{}={}'.format(section, updated) | ||
]) | ||
cmd.append(name) | ||
cmd.append("updated/{}".format(name)) | ||
print(' '.join(cmd)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters