2019-09-09 10:49:02 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
#
|
|
|
|
# This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
#
|
|
|
|
# The MIT License (MIT)
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019 Damien P. George
|
|
|
|
#
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to deal
|
|
|
|
# in the Software without restriction, including without limitation the rights
|
|
|
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
# copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
#
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
# THE SOFTWARE.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# Public functions to be used in the manifest
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2021-02-12 00:11:38 -05:00
|
|
|
def include(manifest, **kwargs):
|
2019-09-09 10:49:02 -04:00
|
|
|
"""Include another manifest.
|
|
|
|
|
|
|
|
The manifest argument can be a string (filename) or an iterable of
|
|
|
|
strings.
|
2019-10-16 00:12:39 -04:00
|
|
|
|
|
|
|
Relative paths are resolved with respect to the current manifest file.
|
2021-02-12 00:11:38 -05:00
|
|
|
|
|
|
|
Optional kwargs can be provided which will be available to the
|
|
|
|
included script via the `options` variable.
|
|
|
|
|
|
|
|
e.g. include("path.py", extra_features=True)
|
|
|
|
|
|
|
|
in path.py:
|
|
|
|
options.defaults(standard_features=True)
|
|
|
|
|
|
|
|
# freeze minimal modules.
|
|
|
|
if options.standard_features:
|
|
|
|
# freeze standard modules.
|
|
|
|
if options.extra_features:
|
|
|
|
# freeze extra modules.
|
2019-09-09 10:49:02 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
if not isinstance(manifest, str):
|
|
|
|
for m in manifest:
|
|
|
|
include(m)
|
|
|
|
else:
|
2019-10-16 00:12:39 -04:00
|
|
|
manifest = convert_path(manifest)
|
2019-09-09 10:49:02 -04:00
|
|
|
with open(manifest) as f:
|
2019-10-16 00:12:39 -04:00
|
|
|
# Make paths relative to this manifest file while processing it.
|
|
|
|
# Applies to includes and input files.
|
|
|
|
prev_cwd = os.getcwd()
|
|
|
|
os.chdir(os.path.dirname(manifest))
|
2021-02-12 00:11:38 -05:00
|
|
|
exec(f.read(), globals(), {"options": IncludeOptions(**kwargs)})
|
2019-10-16 00:12:39 -04:00
|
|
|
os.chdir(prev_cwd)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def freeze(path, script=None, opt=0):
|
|
|
|
"""Freeze the input, automatically determining its type. A .py script
|
|
|
|
will be compiled to a .mpy first then frozen, and a .mpy file will be
|
|
|
|
frozen directly.
|
|
|
|
|
|
|
|
`path` must be a directory, which is the base directory to search for
|
|
|
|
files from. When importing the resulting frozen modules, the name of
|
|
|
|
the module will start after `path`, ie `path` is excluded from the
|
|
|
|
module name.
|
|
|
|
|
2019-10-16 00:12:39 -04:00
|
|
|
If `path` is relative, it is resolved to the current manifest.py.
|
|
|
|
Use $(MPY_DIR), $(MPY_LIB_DIR), $(PORT_DIR), $(BOARD_DIR) if you need
|
|
|
|
to access specific paths.
|
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
If `script` is None all files in `path` will be frozen.
|
|
|
|
|
|
|
|
If `script` is an iterable then freeze() is called on all items of the
|
|
|
|
iterable (with the same `path` and `opt` passed through).
|
|
|
|
|
2020-06-23 03:20:59 -04:00
|
|
|
If `script` is a string then it specifies the file or directory to
|
|
|
|
freeze, and can include extra directories before the file or last
|
|
|
|
directory. The file or directory will be searched for in `path`. If
|
|
|
|
`script` is a directory then all files in that directory will be frozen.
|
2019-09-09 10:49:02 -04:00
|
|
|
|
|
|
|
`opt` is the optimisation level to pass to mpy-cross when compiling .py
|
|
|
|
to .mpy.
|
|
|
|
"""
|
|
|
|
|
|
|
|
freeze_internal(KIND_AUTO, path, script, opt)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def freeze_as_str(path):
|
|
|
|
"""Freeze the given `path` and all .py scripts within it as a string,
|
|
|
|
which will be compiled upon import.
|
|
|
|
"""
|
|
|
|
|
|
|
|
freeze_internal(KIND_AS_STR, path, None, 0)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def freeze_as_mpy(path, script=None, opt=0):
|
|
|
|
"""Freeze the input (see above) by first compiling the .py scripts to
|
|
|
|
.mpy files, then freezing the resulting .mpy files.
|
|
|
|
"""
|
|
|
|
|
|
|
|
freeze_internal(KIND_AS_MPY, path, script, opt)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def freeze_mpy(path, script=None, opt=0):
|
|
|
|
"""Freeze the input (see above), which must be .mpy files that are
|
|
|
|
frozen directly.
|
|
|
|
"""
|
|
|
|
|
|
|
|
freeze_internal(KIND_MPY, path, script, opt)
|
|
|
|
|
|
|
|
|
|
|
|
###########################################################################
|
|
|
|
# Internal implementation
|
|
|
|
|
|
|
|
KIND_AUTO = 0
|
|
|
|
KIND_AS_STR = 1
|
|
|
|
KIND_AS_MPY = 2
|
|
|
|
KIND_MPY = 3
|
|
|
|
|
2019-10-16 00:12:39 -04:00
|
|
|
VARS = {}
|
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
manifest_list = []
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2021-02-12 00:11:38 -05:00
|
|
|
class IncludeOptions:
|
|
|
|
def __init__(self, **kwargs):
|
|
|
|
self._kwargs = kwargs
|
|
|
|
self._defaults = {}
|
|
|
|
|
|
|
|
def defaults(self, **kwargs):
|
|
|
|
self._defaults = kwargs
|
|
|
|
|
|
|
|
def __getattr__(self, name):
|
|
|
|
return self._kwargs.get(name, self._defaults.get(name, None))
|
|
|
|
|
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
class FreezeError(Exception):
|
|
|
|
pass
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def system(cmd):
|
|
|
|
try:
|
|
|
|
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)
|
|
|
|
return 0, output
|
|
|
|
except subprocess.CalledProcessError as er:
|
|
|
|
return -1, er.output
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def convert_path(path):
|
2019-10-16 00:12:39 -04:00
|
|
|
# Perform variable substituion.
|
|
|
|
for name, value in VARS.items():
|
2020-02-26 23:36:53 -05:00
|
|
|
path = path.replace("$({})".format(name), value)
|
2019-10-16 00:12:39 -04:00
|
|
|
# Convert to absolute path (so that future operations don't rely on
|
|
|
|
# still being chdir'ed).
|
|
|
|
return os.path.abspath(path)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def get_timestamp(path, default=None):
|
|
|
|
try:
|
|
|
|
stat = os.stat(path)
|
|
|
|
return stat.st_mtime
|
|
|
|
except OSError:
|
|
|
|
if default is None:
|
2020-02-26 23:36:53 -05:00
|
|
|
raise FreezeError("cannot stat {}".format(path))
|
2019-09-09 10:49:02 -04:00
|
|
|
return default
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def get_timestamp_newest(path):
|
|
|
|
ts_newest = 0
|
2019-11-04 18:16:37 -05:00
|
|
|
for dirpath, dirnames, filenames in os.walk(path, followlinks=True):
|
2019-09-09 10:49:02 -04:00
|
|
|
for f in filenames:
|
|
|
|
ts_newest = max(ts_newest, get_timestamp(os.path.join(dirpath, f)))
|
|
|
|
return ts_newest
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2020-08-21 11:07:57 -04:00
|
|
|
def mkdir(filename):
|
|
|
|
path = os.path.dirname(filename)
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
os.makedirs(path)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def freeze_internal(kind, path, script, opt):
|
|
|
|
path = convert_path(path)
|
2021-01-20 18:09:02 -05:00
|
|
|
if not os.path.isdir(path):
|
2021-04-25 08:45:06 -04:00
|
|
|
raise FreezeError("freeze path must be a directory: {}".format(path))
|
2019-09-09 10:49:02 -04:00
|
|
|
if script is None and kind == KIND_AS_STR:
|
|
|
|
manifest_list.append((KIND_AS_STR, path, script, opt))
|
2020-06-23 03:20:59 -04:00
|
|
|
elif script is None or isinstance(script, str) and script.find(".") == -1:
|
|
|
|
# Recursively search `path` for files to freeze, optionally restricted
|
|
|
|
# to a subdirectory specified by `script`
|
|
|
|
if script is None:
|
|
|
|
subdir = ""
|
|
|
|
else:
|
|
|
|
subdir = "/" + script
|
|
|
|
for dirpath, dirnames, filenames in os.walk(path + subdir, followlinks=True):
|
2019-09-09 10:49:02 -04:00
|
|
|
for f in filenames:
|
2020-02-26 23:36:53 -05:00
|
|
|
freeze_internal(kind, path, (dirpath + "/" + f)[len(path) + 1 :], opt)
|
2019-09-09 10:49:02 -04:00
|
|
|
elif not isinstance(script, str):
|
2020-06-23 03:20:59 -04:00
|
|
|
# `script` is an iterable of items to freeze
|
2019-09-09 10:49:02 -04:00
|
|
|
for s in script:
|
|
|
|
freeze_internal(kind, path, s, opt)
|
|
|
|
else:
|
2020-06-23 03:20:59 -04:00
|
|
|
# `script` should specify an individual file to be frozen
|
2020-02-26 23:36:53 -05:00
|
|
|
extension_kind = {KIND_AS_MPY: ".py", KIND_MPY: ".mpy"}
|
2019-09-09 10:49:02 -04:00
|
|
|
if kind == KIND_AUTO:
|
|
|
|
for k, ext in extension_kind.items():
|
|
|
|
if script.endswith(ext):
|
|
|
|
kind = k
|
|
|
|
break
|
|
|
|
else:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("warn: unsupported file type, skipped freeze: {}".format(script))
|
2019-11-05 20:06:10 -05:00
|
|
|
return
|
2019-09-09 10:49:02 -04:00
|
|
|
wanted_extension = extension_kind[kind]
|
|
|
|
if not script.endswith(wanted_extension):
|
2020-02-26 23:36:53 -05:00
|
|
|
raise FreezeError("expecting a {} file, got {}".format(wanted_extension, script))
|
2019-09-09 10:49:02 -04:00
|
|
|
manifest_list.append((kind, path, script, opt))
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
2021-12-11 06:40:21 -05:00
|
|
|
# Formerly make-frozen.py.
|
|
|
|
# This generates:
|
|
|
|
# - MP_FROZEN_STR_NAMES macro
|
|
|
|
# - mp_frozen_str_sizes
|
|
|
|
# - mp_frozen_str_content
|
2021-12-08 00:47:33 -05:00
|
|
|
def generate_frozen_str_content(paths):
|
|
|
|
def module_name(f):
|
|
|
|
return f
|
|
|
|
|
|
|
|
modules = []
|
2021-12-11 06:40:21 -05:00
|
|
|
output = [b"#include <stdint.h>\n"]
|
2021-12-08 00:47:33 -05:00
|
|
|
|
|
|
|
for path in paths:
|
|
|
|
root = path.rstrip("/")
|
|
|
|
root_len = len(root)
|
|
|
|
|
|
|
|
for dirpath, dirnames, filenames in os.walk(root):
|
|
|
|
for f in filenames:
|
|
|
|
fullpath = dirpath + "/" + f
|
|
|
|
st = os.stat(fullpath)
|
|
|
|
modules.append((path, fullpath[root_len + 1 :], st))
|
|
|
|
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b"#define MP_FROZEN_STR_NAMES \\\n")
|
2021-12-08 00:47:33 -05:00
|
|
|
for _path, f, st in modules:
|
|
|
|
m = module_name(f)
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b'"%s\\0" \\\n' % m.encode())
|
|
|
|
output.append(b"\n")
|
2021-12-08 00:47:33 -05:00
|
|
|
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b"const uint32_t mp_frozen_str_sizes[] = { ")
|
2021-12-08 00:47:33 -05:00
|
|
|
|
|
|
|
for _path, f, st in modules:
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b"%d, " % st.st_size)
|
|
|
|
output.append(b"0 };\n")
|
2021-12-08 00:47:33 -05:00
|
|
|
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b"const char mp_frozen_str_content[] = {\n")
|
2021-12-08 00:47:33 -05:00
|
|
|
for path, f, st in modules:
|
|
|
|
data = open(path + "/" + f, "rb").read()
|
|
|
|
|
|
|
|
# We need to properly escape the script data to create a C string.
|
|
|
|
# When C parses hex characters of the form \x00 it keeps parsing the hex
|
|
|
|
# data until it encounters a non-hex character. Thus one must create
|
|
|
|
# strings of the form "data\x01" "abc" to properly encode this kind of
|
|
|
|
# data. We could just encode all characters as hex digits but it's nice
|
|
|
|
# to be able to read the resulting C code as ASCII when possible.
|
|
|
|
|
|
|
|
data = bytearray(data) # so Python2 extracts each byte as an integer
|
2021-12-11 06:40:21 -05:00
|
|
|
esc_dict = {ord("\n"): b"\\n", ord("\r"): b"\\r", ord('"'): b'\\"', ord("\\"): b"\\\\"}
|
|
|
|
output.append(b'"')
|
2021-12-08 00:47:33 -05:00
|
|
|
break_str = False
|
|
|
|
for c in data:
|
|
|
|
try:
|
|
|
|
output.append(esc_dict[c])
|
|
|
|
except KeyError:
|
|
|
|
if 32 <= c <= 126:
|
|
|
|
if break_str:
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b'" "')
|
2021-12-08 00:47:33 -05:00
|
|
|
break_str = False
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(chr(c).encode())
|
2021-12-08 00:47:33 -05:00
|
|
|
else:
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b"\\x%02x" % c)
|
2021-12-08 00:47:33 -05:00
|
|
|
break_str = True
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b'\\0"\n')
|
2021-12-08 00:47:33 -05:00
|
|
|
|
2021-12-11 06:40:21 -05:00
|
|
|
output.append(b'"\\0"\n};\n\n')
|
|
|
|
return b"".join(output)
|
2021-12-08 00:47:33 -05:00
|
|
|
|
|
|
|
|
2019-09-09 10:49:02 -04:00
|
|
|
def main():
|
|
|
|
# Parse arguments
|
2019-10-16 00:12:39 -04:00
|
|
|
import argparse
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
cmd_parser = argparse.ArgumentParser(
|
|
|
|
description="A tool to generate frozen content in MicroPython firmware images."
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("-o", "--output", help="output path")
|
|
|
|
cmd_parser.add_argument("-b", "--build-dir", help="output path")
|
|
|
|
cmd_parser.add_argument(
|
|
|
|
"-f", "--mpy-cross-flags", default="", help="flags to pass to mpy-cross"
|
|
|
|
)
|
|
|
|
cmd_parser.add_argument("-v", "--var", action="append", help="variables to substitute")
|
2019-12-11 17:09:20 -05:00
|
|
|
cmd_parser.add_argument("--mpy-tool-flags", default="", help="flags to pass to mpy-tool")
|
2020-02-26 23:36:53 -05:00
|
|
|
cmd_parser.add_argument("files", nargs="+", help="input manifest list")
|
2019-10-16 00:12:39 -04:00
|
|
|
args = cmd_parser.parse_args()
|
|
|
|
|
|
|
|
# Extract variables for substitution.
|
|
|
|
for var in args.var:
|
2020-02-26 23:36:53 -05:00
|
|
|
name, value = var.split("=", 1)
|
2019-10-16 00:12:39 -04:00
|
|
|
if os.path.exists(value):
|
|
|
|
value = os.path.abspath(value)
|
|
|
|
VARS[name] = value
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
if "MPY_DIR" not in VARS or "PORT_DIR" not in VARS:
|
|
|
|
print("MPY_DIR and PORT_DIR variables must be specified")
|
2019-10-16 00:12:39 -04:00
|
|
|
sys.exit(1)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
|
|
|
# Get paths to tools
|
2020-02-26 23:36:53 -05:00
|
|
|
MPY_CROSS = VARS["MPY_DIR"] + "/mpy-cross/mpy-cross"
|
2020-03-03 14:21:13 -05:00
|
|
|
if sys.platform == "win32":
|
|
|
|
MPY_CROSS += ".exe"
|
|
|
|
MPY_CROSS = os.getenv("MICROPY_MPYCROSS", MPY_CROSS)
|
2020-02-26 23:36:53 -05:00
|
|
|
MPY_TOOL = VARS["MPY_DIR"] + "/tools/mpy-tool.py"
|
2019-10-16 00:12:39 -04:00
|
|
|
|
|
|
|
# Ensure mpy-cross is built
|
|
|
|
if not os.path.exists(MPY_CROSS):
|
2020-02-26 23:36:53 -05:00
|
|
|
print("mpy-cross not found at {}, please build it first".format(MPY_CROSS))
|
2019-10-16 00:12:39 -04:00
|
|
|
sys.exit(1)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
|
|
|
# Include top-level inputs, to generate the manifest
|
2019-10-16 00:12:39 -04:00
|
|
|
for input_manifest in args.files:
|
2019-09-09 10:49:02 -04:00
|
|
|
try:
|
2020-02-26 23:36:53 -05:00
|
|
|
if input_manifest.endswith(".py"):
|
2019-09-09 10:49:02 -04:00
|
|
|
include(input_manifest)
|
|
|
|
else:
|
|
|
|
exec(input_manifest)
|
|
|
|
except FreezeError as er:
|
|
|
|
print('freeze error executing "{}": {}'.format(input_manifest, er.args[0]))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Process the manifest
|
|
|
|
str_paths = []
|
|
|
|
mpy_files = []
|
|
|
|
ts_newest = 0
|
|
|
|
for kind, path, script, opt in manifest_list:
|
|
|
|
if kind == KIND_AS_STR:
|
|
|
|
str_paths.append(path)
|
|
|
|
ts_outfile = get_timestamp_newest(path)
|
|
|
|
elif kind == KIND_AS_MPY:
|
2020-02-26 23:36:53 -05:00
|
|
|
infile = "{}/{}".format(path, script)
|
|
|
|
outfile = "{}/frozen_mpy/{}.mpy".format(args.build_dir, script[:-3])
|
2019-09-09 10:49:02 -04:00
|
|
|
ts_infile = get_timestamp(infile)
|
|
|
|
ts_outfile = get_timestamp(outfile, 0)
|
|
|
|
if ts_infile >= ts_outfile:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("MPY", script)
|
2019-09-09 10:49:02 -04:00
|
|
|
mkdir(outfile)
|
2020-02-26 23:36:53 -05:00
|
|
|
res, out = system(
|
|
|
|
[MPY_CROSS]
|
|
|
|
+ args.mpy_cross_flags.split()
|
|
|
|
+ ["-o", outfile, "-s", script, "-O{}".format(opt), infile]
|
|
|
|
)
|
2019-09-09 10:49:02 -04:00
|
|
|
if res != 0:
|
2020-08-08 00:46:05 -04:00
|
|
|
print("error compiling {}:".format(infile))
|
|
|
|
sys.stdout.buffer.write(out)
|
2019-09-09 10:49:02 -04:00
|
|
|
raise SystemExit(1)
|
|
|
|
ts_outfile = get_timestamp(outfile)
|
|
|
|
mpy_files.append(outfile)
|
|
|
|
else:
|
|
|
|
assert kind == KIND_MPY
|
2020-02-26 23:36:53 -05:00
|
|
|
infile = "{}/{}".format(path, script)
|
2019-09-09 10:49:02 -04:00
|
|
|
mpy_files.append(infile)
|
|
|
|
ts_outfile = get_timestamp(infile)
|
|
|
|
ts_newest = max(ts_newest, ts_outfile)
|
|
|
|
|
|
|
|
# Check if output file needs generating
|
2019-10-16 00:12:39 -04:00
|
|
|
if ts_newest < get_timestamp(args.output, 0):
|
2019-09-09 10:49:02 -04:00
|
|
|
# No files are newer than output file so it does not need updating
|
|
|
|
return
|
|
|
|
|
|
|
|
# Freeze paths as strings
|
2021-12-08 00:47:33 -05:00
|
|
|
output_str = generate_frozen_str_content(str_paths)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
|
|
|
# Freeze .mpy files
|
2020-02-17 21:02:50 -05:00
|
|
|
if mpy_files:
|
2020-02-26 23:36:53 -05:00
|
|
|
res, output_mpy = system(
|
|
|
|
[
|
|
|
|
sys.executable,
|
|
|
|
MPY_TOOL,
|
|
|
|
"-f",
|
|
|
|
"-q",
|
|
|
|
args.build_dir + "/genhdr/qstrdefs.preprocessed.h",
|
|
|
|
]
|
2019-12-11 17:09:20 -05:00
|
|
|
+ args.mpy_tool_flags.split()
|
2020-02-26 23:36:53 -05:00
|
|
|
+ mpy_files
|
|
|
|
)
|
2020-02-17 21:02:50 -05:00
|
|
|
if res != 0:
|
2020-02-26 23:36:53 -05:00
|
|
|
print("error freezing mpy {}:".format(mpy_files))
|
2021-12-15 00:51:08 -05:00
|
|
|
print(output_mpy.decode())
|
2020-02-17 21:02:50 -05:00
|
|
|
sys.exit(1)
|
|
|
|
else:
|
|
|
|
output_mpy = (
|
|
|
|
b'#include "py/emitglue.h"\n'
|
2020-02-26 23:36:53 -05:00
|
|
|
b"extern const qstr_pool_t mp_qstr_const_pool;\n"
|
|
|
|
b"const qstr_pool_t mp_qstr_frozen_const_pool = {\n"
|
|
|
|
b" (qstr_pool_t*)&mp_qstr_const_pool, MP_QSTRnumber_of, 0, 0\n"
|
|
|
|
b"};\n"
|
2021-12-11 06:40:21 -05:00
|
|
|
b'const char mp_frozen_names[] = { MP_FROZEN_STR_NAMES "\\0"};\n'
|
|
|
|
b"const mp_raw_code_t *const mp_frozen_mpy_content[] = {NULL};\n"
|
2020-02-17 21:02:50 -05:00
|
|
|
)
|
2019-09-09 10:49:02 -04:00
|
|
|
|
|
|
|
# Generate output
|
2020-02-26 23:36:53 -05:00
|
|
|
print("GEN", args.output)
|
2019-10-16 00:12:39 -04:00
|
|
|
mkdir(args.output)
|
2020-02-26 23:36:53 -05:00
|
|
|
with open(args.output, "wb") as f:
|
|
|
|
f.write(b"//\n// Content for MICROPY_MODULE_FROZEN_STR\n//\n")
|
2021-12-11 06:40:21 -05:00
|
|
|
f.write(output_str)
|
2020-02-26 23:36:53 -05:00
|
|
|
f.write(b"//\n// Content for MICROPY_MODULE_FROZEN_MPY\n//\n")
|
2019-09-09 10:49:02 -04:00
|
|
|
f.write(output_mpy)
|
|
|
|
|
2020-02-26 23:36:53 -05:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2019-09-09 10:49:02 -04:00
|
|
|
main()
|