From ad81a2e6cf349cba514e73dc4fe18b1d512b7ce4 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Mon, 27 Feb 2017 15:09:15 +1100
Subject: [PATCH] minimal: Add ability and description to build without the
 compiler.

---
 minimal/README.md      | 12 ++++++++++++
 minimal/main.c         |  6 ++++++
 minimal/mpconfigport.h |  5 +++++
 3 files changed, 23 insertions(+)

diff --git a/minimal/README.md b/minimal/README.md
index 7c42e082e..14b8c00a3 100644
--- a/minimal/README.md
+++ b/minimal/README.md
@@ -33,3 +33,15 @@ This version of the build will work out-of-the-box on a pyboard (and
 anything similar), and will give you a MicroPython REPL on UART1 at 9600
 baud.  Pin PA13 will also be driven high, and this turns on the red LED on
 the pyboard.
+
+## Building without the built-in MicroPython compiler
+
+This minimal port can be built with the built-in MicroPython compiler
+disabled.  This will reduce the firmware by about 20k on a Thumb2 machine,
+and by about 40k on 32-bit x86.  Without the compiler the REPL will be
+disabled, but pre-compiled scripts can still be executed.
+
+To test out this feature, change the `MICROPY_ENABLE_COMPILER` config
+option to "0" in the mpconfigport.h file in this directory.  Then
+recompile and run the firmware and it will execute the frozentest.py
+file.
diff --git a/minimal/main.c b/minimal/main.c
index 5e104e7e8..6b77b1a42 100644
--- a/minimal/main.c
+++ b/minimal/main.c
@@ -9,6 +9,7 @@
 #include "py/gc.h"
 #include "lib/utils/pyexec.h"
 
+#if MICROPY_ENABLE_COMPILER
 void do_str(const char *src, mp_parse_input_kind_t input_kind) {
     mp_lexer_t *lex = mp_lexer_new_from_str_len(MP_QSTR__lt_stdin_gt_, src, strlen(src), 0);
     if (lex == NULL) {
@@ -28,6 +29,7 @@ void do_str(const char *src, mp_parse_input_kind_t input_kind) {
         mp_obj_print_exception(&mp_plat_print, (mp_obj_t)nlr.ret_val);
     }
 }
+#endif
 
 static char *stack_top;
 static char heap[2048];
@@ -40,6 +42,7 @@ int main(int argc, char **argv) {
     gc_init(heap, heap + sizeof(heap));
     #endif
     mp_init();
+    #if MICROPY_ENABLE_COMPILER
     #if MICROPY_REPL_EVENT_DRIVEN
     pyexec_event_repl_init();
     for (;;) {
@@ -53,6 +56,9 @@ int main(int argc, char **argv) {
     #endif
     //do_str("print('hello world!', list(x+1 for x in range(10)), end='eol\\n')", MP_PARSE_SINGLE_INPUT);
     //do_str("for i in range(10):\r\n  print(i)", MP_PARSE_FILE_INPUT);
+    #else
+    pyexec_frozen_module("frozentest.py");
+    #endif
     mp_deinit();
     return 0;
 }
diff --git a/minimal/mpconfigport.h b/minimal/mpconfigport.h
index 5236babf6..e08943860 100644
--- a/minimal/mpconfigport.h
+++ b/minimal/mpconfigport.h
@@ -2,6 +2,11 @@
 
 // options to control how Micro Python is built
 
+// You can disable the built-in MicroPython compiler by setting the following
+// config option to 0.  If you do this then you won't get a REPL prompt, but you
+// will still be able to execute pre-compiled scripts, compiled with mpy-cross.
+#define MICROPY_ENABLE_COMPILER     (1)
+
 #define MICROPY_QSTR_BYTES_IN_HASH  (1)
 #define MICROPY_QSTR_EXTRA_POOL     mp_qstr_frozen_const_pool
 #define MICROPY_ALLOC_PATH_MAX      (256)
-- 
GitLab