Skip to content

Commit 82462ed

Browse files
committed
ports/unix: Add guards for MICROPY_ENABLE_COMPILER=0 support.
Wrap compiler-dependent code (REPL, -O flag, -i flag, execute_from_lexer, etc.) with #if MICROPY_ENABLE_COMPILER guards to prepare for builds with the compiler disabled. Note: This cannot be used yet because the MicroPython build system unconditionally enables MICROPY_MODULE_FROZEN_STR which requires the compiler. This is preparatory work for a future core fix. Signed-off-by: Andrew Leech <[email protected]>
1 parent 2ce93dc commit 82462ed

File tree

1 file changed

+39
-6
lines changed

1 file changed

+39
-6
lines changed

ports/unix/main.c

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@
6161
#endif
6262

6363
// Command line options, with their defaults
64+
#if MICROPY_ENABLE_COMPILER
6465
static bool compile_only = false;
6566
static uint emit_opt = MP_EMIT_OPT_NONE;
67+
#endif
6668

6769
#if MICROPY_ENABLE_GC
6870
// Heap size of GC heap (if enabled)
@@ -113,6 +115,7 @@ static int handle_uncaught_exception(mp_obj_base_t *exc) {
113115
return 1;
114116
}
115117

118+
#if MICROPY_ENABLE_COMPILER
116119
#define LEX_SRC_STR (1)
117120
#define LEX_SRC_VSTR (2)
118121
#define LEX_SRC_FILENAME (3)
@@ -322,6 +325,7 @@ static int do_file(const char *file) {
322325
static int do_str(const char *str) {
323326
return execute_from_lexer(LEX_SRC_STR, str, MP_PARSE_FILE_INPUT, false);
324327
}
328+
#endif // MICROPY_ENABLE_COMPILER
325329

326330
#if !MICROPY_FROZEN_MAIN_MODULE
327331
static void print_help(char **argv) {
@@ -392,6 +396,7 @@ static void pre_process_options(int argc, char **argv) {
392396
exit(invalid_args());
393397
}
394398
if (0) {
399+
#if MICROPY_ENABLE_COMPILER
395400
} else if (strcmp(argv[a + 1], "compile-only") == 0) {
396401
compile_only = true;
397402
} else if (strcmp(argv[a + 1], "emit=bytecode") == 0) {
@@ -402,6 +407,7 @@ static void pre_process_options(int argc, char **argv) {
402407
} else if (strcmp(argv[a + 1], "emit=viper") == 0) {
403408
emit_opt = MP_EMIT_OPT_VIPER;
404409
#endif
410+
#endif // MICROPY_ENABLE_COMPILER
405411
#if MICROPY_ENABLE_GC
406412
} else if (strncmp(argv[a + 1], "heapsize=", sizeof("heapsize=") - 1) == 0) {
407413
char *end;
@@ -457,11 +463,13 @@ static void pre_process_options(int argc, char **argv) {
457463
}
458464
}
459465

466+
#if MICROPY_ENABLE_COMPILER
460467
static void set_sys_argv(char *argv[], int argc, int start_arg) {
461468
for (int i = start_arg; i < argc; i++) {
462469
mp_obj_list_append(mp_sys_argv, MP_OBJ_NEW_QSTR(qstr_from_str(argv[i])));
463470
}
464471
}
472+
#endif
465473

466474
#if MICROPY_PY_SYS_EXECUTABLE
467475
extern mp_obj_str_t mp_sys_executable_obj;
@@ -545,12 +553,14 @@ MP_NOINLINE int main_(int argc, char **argv) {
545553

546554
mp_init();
547555

556+
#if MICROPY_ENABLE_COMPILER
548557
#if MICROPY_EMIT_NATIVE
549558
// Set default emitter options
550559
MP_STATE_VM(default_emit_opt) = emit_opt;
551560
#else
552561
(void)emit_opt;
553562
#endif
563+
#endif
554564

555565
#if MICROPY_VFS_POSIX
556566
{
@@ -641,9 +651,13 @@ MP_NOINLINE int main_(int argc, char **argv) {
641651
sys_set_excecutable(argv[0]);
642652
#endif
643653

654+
#if MICROPY_ENABLE_COMPILER
644655
const int NOTHING_EXECUTED = -2;
645-
int ret = NOTHING_EXECUTED;
656+
#endif
657+
int ret = 0;
658+
#if MICROPY_ENABLE_COMPILER
646659
bool inspect = false;
660+
#endif
647661

648662
// Check if a frozen or romfs main module exists and should be run.
649663
// Priority: 1) frozen main module, 2) /rom/main.py or /rom/main.mpy
@@ -712,14 +726,18 @@ MP_NOINLINE int main_(int argc, char **argv) {
712726
// Process flags that affect execution
713727
for (int a = 1; a < argc; a++) {
714728
if (argv[a][0] == '-') {
729+
#if MICROPY_ENABLE_COMPILER
715730
if (strcmp(argv[a], "-i") == 0) {
716731
inspect = true;
717-
} else if (strcmp(argv[a], "-X") == 0 && a + 1 < argc) {
732+
} else
733+
#endif
734+
if (strcmp(argv[a], "-X") == 0 && a + 1 < argc) {
718735
a++;
719736
#if MICROPY_DEBUG_PRINTERS
720737
} else if (strcmp(argv[a], "-v") == 0) {
721738
mp_verbose_flag++;
722739
#endif
740+
#if MICROPY_ENABLE_COMPILER
723741
} else if (strncmp(argv[a], "-O", 2) == 0) {
724742
if (unichar_isdigit(argv[a][2])) {
725743
MP_STATE_VM(mp_optimise_value) = argv[a][2] & 0xf;
@@ -728,6 +746,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
728746
for (char *p = argv[a] + 1; *p && *p == 'O'; p++, MP_STATE_VM(mp_optimise_value)++) {;
729747
}
730748
}
749+
#endif
731750
} else {
732751
break;
733752
}
@@ -741,15 +760,22 @@ MP_NOINLINE int main_(int argc, char **argv) {
741760
bool in_positional = false;
742761
for (int a = 1; a < argc; a++) {
743762
if (!in_positional && argv[a][0] == '-') {
763+
#if MICROPY_ENABLE_COMPILER
744764
if (strcmp(argv[a], "-i") == 0) {
745765
continue;
766+
} else
767+
#endif
746768
#if MICROPY_DEBUG_PRINTERS
747-
} else if (strcmp(argv[a], "-v") == 0) {
769+
if (strcmp(argv[a], "-v") == 0) {
748770
continue;
771+
} else
749772
#endif
750-
} else if (strncmp(argv[a], "-O", 2) == 0) {
773+
#if MICROPY_ENABLE_COMPILER
774+
if (strncmp(argv[a], "-O", 2) == 0) {
751775
continue;
752-
} else if (strcmp(argv[a], "-X") == 0 && a + 1 < argc) {
776+
} else
777+
#endif
778+
if (strcmp(argv[a], "-X") == 0 && a + 1 < argc) {
753779
a++;
754780
continue;
755781
}
@@ -777,13 +803,17 @@ MP_NOINLINE int main_(int argc, char **argv) {
777803
} else {
778804
ret = handle_uncaught_exception(nlr.ret_val);
779805
}
780-
} else {
806+
}
807+
#if MICROPY_ENABLE_COMPILER
808+
else {
781809
// .py file in romfs: use lexer
782810
ret = do_file(main_path);
783811
}
812+
#endif
784813
goto done_execution;
785814
}
786815

816+
#if MICROPY_ENABLE_COMPILER
787817
for (int a = 1; a < argc; a++) {
788818
if (argv[a][0] == '-') {
789819
if (strcmp(argv[a], "-i") == 0) {
@@ -889,11 +919,13 @@ MP_NOINLINE int main_(int argc, char **argv) {
889919
break;
890920
}
891921
}
922+
#endif // MICROPY_ENABLE_COMPILER
892923

893924
#if MICROPY_MODULE_FROZEN || (MICROPY_VFS_ROM && MICROPY_VFS_ROM_IOCTL)
894925
done_execution:
895926
#endif
896927

928+
#if MICROPY_ENABLE_COMPILER
897929
const char *inspect_env = getenv("MICROPYINSPECT");
898930
if (inspect_env && inspect_env[0] != '\0') {
899931
inspect = true;
@@ -908,6 +940,7 @@ MP_NOINLINE int main_(int argc, char **argv) {
908940
ret = execute_from_lexer(LEX_SRC_STDIN, NULL, MP_PARSE_FILE_INPUT, false);
909941
}
910942
}
943+
#endif // MICROPY_ENABLE_COMPILER
911944

912945
#if MICROPY_PY_SYS_SETTRACE
913946
MP_STATE_THREAD(prof_trace_callback) = MP_OBJ_NULL;

0 commit comments

Comments
 (0)