Skip to content

Commit 74fb4e7

Browse files
committed
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.
1 parent 9b4c013 commit 74fb4e7

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

mpy-cross/main.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ STATIC void stderr_print_strn(void *env, const char *str, mp_uint_t len) {
5252

5353
STATIC const mp_print_t mp_stderr_print = {NULL, stderr_print_strn};
5454

55-
STATIC int compile_and_save(const char *file, const char *output_file) {
55+
STATIC int compile_and_save(const char *file, const char *output_file, const char *source_file) {
5656
mp_lexer_t *lex = mp_lexer_new_from_file(file);
5757
if (lex == NULL) {
5858
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) {
6161

6262
nlr_buf_t nlr;
6363
if (nlr_push(&nlr) == 0) {
64-
qstr source_name = lex->source_name;
64+
qstr source_name;
65+
if (source_file == NULL) {
66+
source_name = lex->source_name;
67+
} else {
68+
source_name = qstr_from_str(source_file);
69+
}
6570

6671
#if MICROPY_PY___FILE__
6772
if (input_kind == MP_PARSE_FILE_INPUT) {
@@ -97,7 +102,8 @@ STATIC int usage(char **argv) {
97102
printf(
98103
"usage: %s [<opts>] [-X <implopt>] <input filename>\n"
99104
"Options:\n"
100-
"-o : output file for compiled bytecode\n"
105+
"-o : output file for compiled bytecode (defaults to input with .mpy extension)\n"
106+
"-s : source filename to embed in the compiled bytecode (defaults to input file)\n"
101107
"-v : verbose (trace various operations); can be multiple\n"
102108
"-O[N] : apply bytecode optimizations of level N\n"
103109
"\n"
@@ -189,6 +195,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
189195

190196
const char *input_file = NULL;
191197
const char *output_file = NULL;
198+
const char *source_file = NULL;
192199

193200
// parse main options
194201
for (int a = 1; a < argc; a++) {
@@ -210,6 +217,12 @@ MP_NOINLINE int main_(int argc, char **argv) {
210217
}
211218
a += 1;
212219
output_file = argv[a];
220+
} else if (strcmp(argv[a], "-s") == 0) {
221+
if (a + 1 >= argc) {
222+
exit(usage(argv));
223+
}
224+
a += 1;
225+
source_file = argv[a];
213226
} else if (strncmp(argv[a], "-msmall-int-bits=", sizeof("-msmall-int-bits=") - 1) == 0) {
214227
char *end;
215228
mp_dynamic_compiler.small_int_bits =
@@ -243,7 +256,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
243256
exit(1);
244257
}
245258

246-
int ret = compile_and_save(input_file, output_file);
259+
int ret = compile_and_save(input_file, output_file, source_file);
247260

248261
#if MICROPY_PY_MICROPYTHON_MEM_INFO
249262
if (mp_verbose_flag) {

0 commit comments

Comments
 (0)