From d3ebe4829d920542e5af462e75179e4f0cadb946 Mon Sep 17 00:00:00 2001
From: Damien George <damien.p.george@gmail.com>
Date: Tue, 7 Jan 2014 15:20:33 +0000
Subject: [PATCH] Factor and simplify Makefile's and mpconfig, part 2.

---
 py/asmx64.c             |  8 ++--
 py/gc.c                 |  4 ++
 py/lexerunix.c          |  5 +++
 py/mpconfig.h           | 15 +++++++
 py/py.mk                |  5 +++
 py/showbc.c             |  4 ++
 stm/Makefile            | 95 ++++++-----------------------------------
 stm/mpconfigport.h      |  1 +
 unix-cpy/mpconfigport.h |  3 +-
 unix/mpconfigport.h     |  1 +
 10 files changed, 56 insertions(+), 85 deletions(-)

diff --git a/py/asmx64.c b/py/asmx64.c
index 226d4efee..d3158170f 100644
--- a/py/asmx64.c
+++ b/py/asmx64.c
@@ -1,16 +1,18 @@
 #include <stdio.h>
 #include <assert.h>
-#include <sys/types.h>
-#include <sys/mman.h>
 #include <string.h>
 
 #include "misc.h"
-#include "asmx64.h"
 #include "mpconfig.h"
 
 // wrapper around everything in this file
 #if MICROPY_EMIT_X64
 
+#include <sys/types.h>
+#include <sys/mman.h>
+
+#include "asmx64.h"
+
 #if defined(__OpenBSD__) || defined(__MACH__)
 #define MAP_ANONYMOUS MAP_ANON
 #endif
diff --git a/py/gc.c b/py/gc.c
index 7d4cac6e7..054a3cc31 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -6,6 +6,8 @@
 #include "mpconfig.h"
 #include "gc.h"
 
+#if MICROPY_ENABLE_GC
+
 // a machine word is big enough to hold a pointer
 /*
 #define BYTES_PER_WORD (8)
@@ -380,3 +382,5 @@ int main(void) {
     gc_dump_at();
 }
 */
+
+#endif // MICROPY_ENABLE_GC
diff --git a/py/lexerunix.c b/py/lexerunix.c
index 14c28c16d..5336610ba 100644
--- a/py/lexerunix.c
+++ b/py/lexerunix.c
@@ -4,8 +4,11 @@
 #include <fcntl.h>
 
 #include "misc.h"
+#include "mpconfig.h"
 #include "lexer.h"
 
+#if MICROPY_ENABLE_LEXER_UNIX
+
 typedef struct _str_buf_t {
     bool free;                  // free src_beg when done
     const char *src_beg;        // beginning of source
@@ -78,3 +81,5 @@ mp_lexer_t *mp_import_open_file(qstr mod_name) {
     vstr_printf(vstr, "%s.py", qstr_str(mod_name));
     return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here?
 }
+
+#endif // MICROPY_ENABLE_LEXER_UNIX
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 8ce432a61..2017ba366 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -39,14 +39,29 @@
 #define MICROPY_MEM_STATS (0)
 #endif
 
+// Whether to build code to show byte code
+#ifndef MICROPY_SHOW_BC
+#define MICROPY_SHOW_BC (0)
+#endif
+
 /*****************************************************************************/
 /* Fine control over Python features                                         */
 
+// Whether to include the garbage collector
+#ifndef MICROPY_ENABLE_GC
+#define MICROPY_ENABLE_GC (0)
+#endif
+
 // Whether to include REPL helper function
 #ifndef MICROPY_ENABLE_REPL_HELPERS
 #define MICROPY_ENABLE_REPL_HELPERS (0)
 #endif
 
+// Whether to include lexer helper function for unix
+#ifndef MICROPY_ENABLE_LEXER_UNIX
+#define MICROPY_ENABLE_LEXER_UNIX (0)
+#endif
+
 // Whether to support float and complex types
 #ifndef MICROPY_ENABLE_FLOAT
 #define MICROPY_ENABLE_FLOAT (0)
diff --git a/py/py.mk b/py/py.mk
index e673c713c..3ed8a3e3d 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -24,6 +24,7 @@ PY_O_BASENAME = \
 	nlrx64.o \
 	nlrthumb.o \
 	malloc.o \
+	gc.o \
 	qstr.o \
 	vstr.o \
 	unicode.o \
@@ -88,6 +89,10 @@ $(PY_BUILD)%.o: $(PY_SRC)/%.S
 $(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h
 	$(CC) $(CFLAGS) -c -o $@ $<
 
+# optimising gc for speed; 5ms down to 4ms on pybv2
+$(PY_BUILD)gc.o: $(PY_SRC)/gc.c
+	$(CC) $(CFLAGS) -O3 -c -o $@ $<
+
 # optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
 $(PY_BUILD)vm.o: $(PY_SRC)/vm.c
 	$(CC) $(CFLAGS) -O3 -c -o $@ $<
diff --git a/py/showbc.c b/py/showbc.c
index eb7d41b24..aea0aff67 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -8,6 +8,8 @@
 #include "mpconfig.h"
 #include "bc0.h"
 
+#if MICROPY_SHOW_BC
+
 #define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0)
 #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0)
 #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0)
@@ -363,3 +365,5 @@ void mp_show_byte_code(const byte *ip, int len) {
         printf("\n");
     }
 }
+
+#endif // MICROPY_SHOW_BC
diff --git a/stm/Makefile b/stm/Makefile
index cd998dd88..fecd52527 100644
--- a/stm/Makefile
+++ b/stm/Makefile
@@ -1,9 +1,16 @@
+# define main target
+all: all2
+
+# include py core make definitions
+include ../py/py.mk
+
+# program for deletion
+RM = /bin/rm
+
 STMSRC=lib
 #STMOTGSRC=lib-otg
 FATFSSRC=fatfs
 CC3KSRC=cc3k
-PYSRC=../py
-BUILD=build
 DFU=../tools/dfu.py
 TARGET=PYBOARD
 
@@ -11,7 +18,7 @@ AS = arm-none-eabi-as
 CC = arm-none-eabi-gcc
 LD = arm-none-eabi-ld
 CFLAGS_CORTEX_M4 = -mthumb -mtune=cortex-m4 -mabi=aapcs-linux -mcpu=cortex-m4 -mfpu=fpv4-sp-d16 -mfloat-abi=hard -fsingle-precision-constant -Wdouble-promotion -DSTM32F40XX -DHSE_VALUE=8000000
-CFLAGS = -I. -I$(PYSRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
+CFLAGS = -I. -I$(PY_SRC) -I$(FATFSSRC) -I$(STMSRC) -Wall -ansi -std=gnu99 -Os -DNDEBUG $(CFLAGS_CORTEX_M4) -D$(TARGET)
 #CFLAGS += -I$(STMOTGSRC) -DUSE_HOST_MODE -DUSE_OTG_MODE
 LDFLAGS = --nostdlib -T stm32f405.ld
 
@@ -43,53 +50,6 @@ SRC_S = \
 	startup_stm32f40xx.s \
 	gchelper.s \
 
-PY_O = \
-	nlrthumb.o \
-	gc.o \
-	malloc.o \
-	qstr.o \
-	vstr.o \
-	unicode.o \
-	lexer.o \
-	parse.o \
-	scope.o \
-	compile.o \
-	emitcommon.o \
-	emitpass1.o \
-	emitbc.o \
-	asmthumb.o \
-	emitnthumb.o \
-	emitinlinethumb.o \
-	runtime.o \
-	map.o \
-	obj.o \
-	objbool.o \
-	objboundmeth.o \
-	objcell.o \
-	objclass.o \
-	objclosure.o \
-	objcomplex.o \
-	objdict.o \
-	objexcept.o \
-	objfloat.o \
-	objfun.o \
-	objgenerator.o \
-	objinstance.o \
-	objint.o \
-	objlist.o \
-	objmodule.o \
-	objnone.o \
-	objrange.o \
-	objset.o \
-	objslice.o \
-	objstr.o \
-	objtuple.o \
-	objtype.o \
-	builtin.o \
-	builtinimport.o \
-	vm.o \
-	repl.o \
-
 SRC_FATFS = \
 	ff.c \
 	diskio.c \
@@ -146,10 +106,10 @@ SRC_CC3K = \
 	ccspi.c \
 	pybcc3k.c \
 
-OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(PY_O) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o))
+OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o) $(SRC_CC3K:.c=.o)) $(PY_O)
 #OBJ += $(addprefix $(BUILD)/, $(SRC_STM_OTG:.c=.o))
 
-all: $(BUILD) $(BUILD)/flash.dfu
+all2: $(BUILD) $(BUILD)/flash.dfu
 
 $(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
 	python $(DFU) -b 0x08000000:$(BUILD)/flash0.bin -b 0x08020000:$(BUILD)/flash1.bin $@
@@ -164,9 +124,6 @@ $(BUILD)/flash.elf: $(OBJ)
 	$(LD) $(LDFLAGS) -o $@ $(OBJ)
 	arm-none-eabi-size $@
 
-$(BUILD):
-	mkdir -p $@
-
 $(BUILD)/%.o: %.s
 	$(AS) -o $@ $<
 
@@ -185,31 +142,7 @@ $(BUILD)/%.o: $(STMSRC)/%.c
 $(BUILD)/%.o: $(CC3KSRC)/%.c
 	$(CC) $(CFLAGS) -c -o $@ $<
 
-$(BUILD)/%.o: $(PYSRC)/%.s
-	$(AS) -o $@ $<
-
-$(BUILD)/%.o: $(PYSRC)/%.S
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h
-	$(CC) $(CFLAGS) -c -o $@ $<
-
-$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h
-	$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
-
-# optimising gc for speed; 5ms down to 4ms
-$(BUILD)/gc.o: $(PYSRC)/gc.c
-	$(CC) $(CFLAGS) -O3 -c -o $@ $<
-
-# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
-$(BUILD)/vm.o: $(PYSRC)/vm.c
-	$(CC) $(CFLAGS) -O3 -c -o $@ $<
-
-$(BUILD)/parse.o: $(PYSRC)/grammar.h
-$(BUILD)/compile.o: $(PYSRC)/grammar.h
-$(BUILD)/emitbc.o: $(PYSRC)/emit.h
-
 clean:
-	/bin/rm -rf $(BUILD)
+	$(RM) -rf $(BUILD)
 
-.PHONY: all clean
+.PHONY: all all2 clean
diff --git a/stm/mpconfigport.h b/stm/mpconfigport.h
index ee90db321..dfa46cc50 100644
--- a/stm/mpconfigport.h
+++ b/stm/mpconfigport.h
@@ -4,6 +4,7 @@
 
 #define MICROPY_EMIT_THUMB          (1)
 #define MICROPY_EMIT_INLINE_THUMB   (1)
+#define MICROPY_ENABLE_GC           (1)
 #define MICROPY_ENABLE_REPL_HELPERS (1)
 #define MICROPY_ENABLE_FLOAT        (1)
 
diff --git a/unix-cpy/mpconfigport.h b/unix-cpy/mpconfigport.h
index 9e3e32bb4..3fc186677 100644
--- a/unix-cpy/mpconfigport.h
+++ b/unix-cpy/mpconfigport.h
@@ -1,7 +1,8 @@
 // options to control how Micro Python is built
 
-#define MICROPY_ENABLE_FLOAT        (1)
 #define MICROPY_EMIT_CPYTHON        (1)
+#define MICROPY_ENABLE_LEXER_UNIX   (1)
+#define MICROPY_ENABLE_FLOAT        (1)
 
 // type definitions for the specific machine
 
diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h
index b7901069a..832764121 100644
--- a/unix/mpconfigport.h
+++ b/unix/mpconfigport.h
@@ -10,6 +10,7 @@
 #define MICROPY_EMIT_INLINE_THUMB   (0)
 #define MICROPY_MEM_STATS           (1)
 #define MICROPY_ENABLE_REPL_HELPERS (1)
+#define MICROPY_ENABLE_LEXER_UNIX   (1)
 #define MICROPY_ENABLE_FLOAT        (1)
 
 // type definitions for the specific machine
-- 
GitLab