summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/mkenv.mk52
-rw-r--r--py/mkrules.mk92
-rw-r--r--py/py.mk92
-rw-r--r--py/qstr.c2
-rw-r--r--py/qstr.h2
5 files changed, 167 insertions, 73 deletions
diff --git a/py/mkenv.mk b/py/mkenv.mk
new file mode 100644
index 0000000000..ed4e22096d
--- /dev/null
+++ b/py/mkenv.mk
@@ -0,0 +1,52 @@
+ifneq ($(lastword a b),b)
+$(error These Makefiles require make 3.81 or newer)
+endif
+
+# Set TOP to be the path to get from the current directory (where make was
+# invoked) to the top of the tree. $(lastword $(MAKEFILE_LIST)) returns
+# the name of this makefile relative to where make was invoked.
+#
+# We assume that this file is in the py directory so we use $(dir ) twice
+# to get to the top of the tree.
+
+THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
+TOP := $(patsubst %/py/mkenv.mk,%,$(THIS_MAKEFILE))
+
+# Turn on increased build verbosity by defining BUILD_VERBOSE in your main
+# Makefile or in your environment. You can also use V=1 on the make command
+# line.
+
+ifeq ("$(origin V)", "command line")
+BUILD_VERBOSE=$(V)
+endif
+ifndef BUILD_VERBOSE
+BUILD_VERBOSE = 0
+endif
+ifeq ($(BUILD_VERBOSE),0)
+Q = @
+else
+Q =
+endif
+# Since this is a new feature, advertise it
+ifeq ($(BUILD_VERBOSE),0)
+$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.)
+endif
+
+# default settings; can be overriden in main Makefile
+
+PY_SRC ?= $(TOP)/py
+BUILD ?= build
+
+RM = rm
+ECHO = @echo
+
+AS = $(CROSS_COMPILE)as
+CC = $(CROSS_COMPILE)gcc
+LD = $(CROSS_COMPILE)ld
+OBJCOPY = $(CROSS_COMPILE)objcopy
+SIZE = $(CROSS_COMPILE)size
+
+all:
+.PHONY: all
+
+MKENV_INCLUDED = 1
diff --git a/py/mkrules.mk b/py/mkrules.mk
new file mode 100644
index 0000000000..4c1ea08332
--- /dev/null
+++ b/py/mkrules.mk
@@ -0,0 +1,92 @@
+ifneq ($(MKENV_INCLUDED),1)
+# We assume that mkenv is in the same directory as this file.
+THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST))
+include $(dir $(THIS_MAKEFILE))mkenv.mk
+endif
+
+# This file expects that OBJ contains a list of all of the object files.
+# The directory portion of each object file is used to locate the source
+# and should not contain any ..'s but rather be relative to the top of the
+# tree.
+#
+# So for example, py/map.c would have an object file name py/map.o
+# The object files will go into the build directory and mantain the same
+# directory structure as the source tree. So the final dependency will look
+# like this:
+#
+# build/py/map.o: py/map.c
+#
+# We set vpath to point to the top of the tree so that the source files
+# can be located. By following this scheme, it allows a single build rule
+# to be used to compile all .c files.
+
+vpath %.S . $(TOP)
+$(BUILD)/%.o: %.S
+ $(ECHO) "CC $<"
+ $(Q)$(CC) $(CFLAGS) -c -o $@ $<
+
+vpath %.s . $(TOP)
+$(BUILD)/%.o: %.s
+ $(ECHO) "AS $<"
+ $(Q)$(AS) -o $@ $<
+
+define compile_c
+$(ECHO) "CC $<"
+$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
+@# The following fixes the dependency file.
+@# See http://make.paulandlesley.org/autodep.html for details.
+@cp $(@:.o=.d) $(@:.o=.P); \
+ sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
+ -e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
+ rm -f $(@:.o=.d)
+endef
+
+vpath %.c . $(TOP)
+$(BUILD)/%.o: %.c
+ $(call compile_c)
+
+# The following rule uses | to create an order only prereuisite. Order only
+# prerequisites only get built if they don't exist. They don't cause timestamp
+# checkng to be performed.
+#
+# $(sort $(var)) removes duplicates
+#
+# The net effect of this, is it causes the objects to depend on the
+# object directories (but only for existance), and the object directories
+# will be created if they don't exist.
+OBJ_DIRS = $(sort $(dir $(OBJ)))
+$(OBJ): | $(OBJ_DIRS)
+$(OBJ_DIRS):
+ mkdir -p $@
+
+ifneq ($(PROG),)
+# Build a standalone executable (unix and unix-cpy do this)
+
+all: $(PROG)
+
+$(PROG): $(OBJ)
+ $(ECHO) "LINK $<"
+ $(Q)$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
+ifndef DEBUG
+ $(Q)strip $(PROG)
+endif
+ $(Q)size $(PROG)
+
+clean: clean-prog
+clean-prog:
+ $(RM) -f $(PROG)
+
+.PHONY: clean-prog
+endif
+
+clean:
+ $(RM) -rf $(BUILD)
+.PHONY: clean
+
+print-cfg:
+ $(ECHO) "PY_SRC = $(PY_SRC)"
+ $(ECHO) "BUILD = $(BUILD)"
+ $(ECHO) "OBJ = $(OBJ)"
+.PHONY: print-cfg
+
+-include $(OBJ:.o=.P)
diff --git a/py/py.mk b/py/py.mk
index 75394b3615..d4946e2838 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -1,48 +1,11 @@
-##########
-# The following should eventually go into a more central location
-# when a reorg is done.
-#
-# Turn on increased build verbosity by defining BUILD_VERBOSE in your main
-# Makefile or in your environment. You can also use V=1 on the make command
-# line.
-ifeq ("$(origin V)", "command line")
-BUILD_VERBOSE=$(V)
-endif
-ifndef BUILD_VERBOSE
-BUILD_VERBOSE = 0
-endif
-ifeq ($(BUILD_VERBOSE),0)
-Q = @
-else
-Q =
-endif
-# Since this is a new feature, advertise it
-ifeq ($(BUILD_VERBOSE),0)
-$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.)
-endif
-#
-#########
-
-# default settings; can be overriden in main Makefile
-
-PY_SRC ?= ../py
-BUILD ?= build
-
-# to create the build directory
-
-$(BUILD):
- $(Q)mkdir -p $@
-
# where py object files go (they have a name prefix to prevent filename clashes)
-
-PY_BUILD = $(BUILD)/py.
+PY_BUILD = $(BUILD)/py
# file containing qstr defs for the core Python bit
PY_QSTR_DEFS = $(PY_SRC)/qstrdefs.h
# py object files
-
PY_O_BASENAME = \
nlrx86.o \
nlrx64.o \
@@ -108,50 +71,37 @@ PY_O_BASENAME = \
repl.o \
# prepend the build destination prefix to the py object files
-
-PY_O = $(addprefix $(PY_BUILD), $(PY_O_BASENAME))
+PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))
# qstr data
-$(PY_BUILD)qstr.o: $(PY_BUILD)qstrdefs.generated.h
-
-$(PY_BUILD)qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py
+# Adding an order only dependency on $(PY_BUILD) causes $(PY_BUILD) to get
+# created before we run the script to generate the .h
+$(PY_BUILD)/qstrdefs.generated.h: | $(PY_BUILD)
+$(PY_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py
$(ECHO) "makeqstrdata $(PY_QSTR_DEFS) $(QSTR_DEFS)"
$(Q)python $(PY_SRC)/makeqstrdata.py $(PY_QSTR_DEFS) $(QSTR_DEFS) > $@
-# emitters
-
-$(PY_BUILD)emitnx64.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
+# We don't know which source files actually need the generated.h (since
+# it is #included from str.h). The compiler generated dependencies will cause
+# the right .o's to get recompiled if the generated.h file changes. Adding
+# an order-only dependendency to all of the .o's will cause the generated .h
+# to get built before we try to compile any of them.
+$(PY_O): | $(PY_BUILD)/qstrdefs.generated.h
-$(PY_BUILD)emitnthumb.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
-
-# general source files
+# emitters
-$(PY_BUILD)%.o: $(PY_SRC)/%.S
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -c -o $@ $<
+$(PY_BUILD)/emitnx64.o: CFLAGS += -DN_X64
+$(PY_BUILD)/emitnx64.o: py/emitnative.c
+ $(call compile_c)
-$(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h $(PY_SRC)/qstr.h $(PY_QSTR_DEFS) $(QSTR_DEFS)
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -c -o $@ $<
+$(PY_BUILD)/emitnthumb.o: CFLAGS += -DN_THUMB
+$(PY_BUILD)/emitnthumb.o: py/emitnative.c
+ $(call compile_c)
# optimising gc for speed; 5ms down to 4ms on pybv2
-$(PY_BUILD)gc.o: $(PY_SRC)/gc.c
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -O3 -c -o $@ $<
+$(PY_BUILD)gc.o: CFLAGS += -O3
# 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
- $(ECHO) "CC $<"
- $(Q)$(CC) $(CFLAGS) -O3 -c -o $@ $<
-
-# header dependencies
+$(PY_BUILD)vm.o: CFLAGS += -O3
-$(PY_BUILD)parse.o: $(PY_SRC)/grammar.h
-$(PY_BUILD)compile.o: $(PY_SRC)/grammar.h
-$(PY_BUILD)emitcpy.o: $(PY_SRC)/emit.h
-$(PY_BUILD)emitbc.o: $(PY_SRC)/emit.h
diff --git a/py/qstr.c b/py/qstr.c
index 268b3bafc9..f12cbceff6 100644
--- a/py/qstr.c
+++ b/py/qstr.c
@@ -55,7 +55,7 @@ const static qstr_pool_t const_pool = {
(const byte*) "\0\0\0\0", // empty qstr
#define Q(id, str) str,
// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
-#include "build/py.qstrdefs.generated.h"
+#include "build/py/qstrdefs.generated.h"
#undef Q
},
};
diff --git a/py/qstr.h b/py/qstr.h
index 271e2117c9..9224d48a19 100644
--- a/py/qstr.h
+++ b/py/qstr.h
@@ -9,7 +9,7 @@ enum {
MP_QSTR_ = 1, // the empty qstr
#define Q(id, str) MP_QSTR_##id,
// TODO having 'build/py.' here is a bit of a hack, should take config variable from Makefile
-#include "build/py.qstrdefs.generated.h"
+#include "build/py/qstrdefs.generated.h"
#undef Q
MP_QSTR_number_of,
} category_t;