summaryrefslogtreecommitdiffstatshomepage
path: root/mpy-cross/main.c
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-05-23 13:25:54 +0100
committerDamien George <damien.p.george@gmail.com>2016-05-23 13:25:54 +0100
commit74fb4e795b6a709a21803b30df7dbb66aa88b354 (patch)
treef284dba7d0e18c6a76629732ff5be3339c36d675 /mpy-cross/main.c
parent9b4c013823fb04e31412a20b4d53a397be912625 (diff)
downloadmicropython-74fb4e795b6a709a21803b30df7dbb66aa88b354.tar.gz
micropython-74fb4e795b6a709a21803b30df7dbb66aa88b354.zip
mpy-cross: Add -s option to specify the embedded source filename.
.mpy files contain the name of the source file that they were compiled from. This patch adds a way to change this name to an arbitrary string, specified on the command line with the -s option. The default is to use the full name of the input filename. This new -s option is useful to strip off a leading directory name so that mpy-tool.py can freeze packages.
Diffstat (limited to 'mpy-cross/main.c')
-rw-r--r--mpy-cross/main.c21
1 files changed, 17 insertions, 4 deletions
diff --git a/mpy-cross/main.c b/mpy-cross/main.c
index 952ff52184..4393d00ff0 100644
--- a/mpy-cross/main.c
+++ b/mpy-cross/main.c
@@ -52,7 +52,7 @@ STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
STATIC const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
-STATIC int compile_and_save(const char *file, const char *output_file) {
+STATIC int compile_and_save(const char *file, const char *output_file, const char *source_file) {
mp_lexer_t *lex = mp_lexer_new_from_file(file);
if (lex == NULL) {
printf("could not open file '%s' for reading\n", file);
@@ -61,7 +61,12 @@ STATIC int compile_and_save(const char *file, const char *output_file) {
nlr_buf_t nlr;
if (nlr_push(&nlr) == 0) {
- qstr source_name = lex->source_name;
+ qstr source_name;
+ if (source_file == NULL) {
+ source_name = lex->source_name;
+ } else {
+ source_name = qstr_from_str(source_file);
+ }
#if MICROPY_PY___FILE__
if (input_kind == MP_PARSE_FILE_INPUT) {
@@ -97,7 +102,8 @@ STATIC int usage(char **argv) {
printf(
"usage: %s [<opts>] [-X <implopt>] <input filename>\n"
"Options:\n"
-"-o : output file for compiled bytecode\n"
+"-o : output file for compiled bytecode (defaults to input with .mpy extension)\n"
+"-s : source filename to embed in the compiled bytecode (defaults to input file)\n"
"-v : verbose (trace various operations); can be multiple\n"
"-O[N] : apply bytecode optimizations of level N\n"
"\n"
@@ -189,6 +195,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
const char *input_file = NULL;
const char *output_file = NULL;
+ const char *source_file = NULL;
// parse main options
for (int a = 1; a < argc; a++) {
@@ -210,6 +217,12 @@ MP_NOINLINE int main_(int argc, char **argv) {
}
a += 1;
output_file = argv[a];
+ } else if (strcmp(argv[a], "-s") == 0) {
+ if (a + 1 >= argc) {
+ exit(usage(argv));
+ }
+ a += 1;
+ source_file = argv[a];
} else if (strncmp(argv[a], "-msmall-int-bits=", sizeof("-msmall-int-bits=") - 1) == 0) {
char *end;
mp_dynamic_compiler.small_int_bits =
@@ -243,7 +256,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
exit(1);
}
- int ret = compile_and_save(input_file, output_file);
+ int ret = compile_and_save(input_file, output_file, source_file);
#if MICROPY_PY_MICROPYTHON_MEM_INFO
if (mp_verbose_flag) {