Skip to content

Commit c89c681

Browse files
committed
Rework makefiles. Add proper dependency checking.
1 parent 2b2cb7b commit c89c681

9 files changed

Lines changed: 195 additions & 201 deletions

File tree

py/mkenv.mk

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
ifneq ($(lastword a b),b)
2+
$(error These Makefiles require make 3.81 or newer)
3+
endif
4+
5+
# Set TOP to be the path to get from the current directory (where make was
6+
# invoked) to the top of the tree. $(lastword $(MAKEFILE_LIST)) returns
7+
# the name of this makefile relative to where make was invoked.
8+
#
9+
# We assume that this file is in the py directory so we use $(dir ) twice
10+
# to get to the top of the tree.
11+
12+
THIS_MAKEFILE := $(lastword $(MAKEFILE_LIST))
13+
TOP := $(patsubst %/py/mkenv.mk,%,$(THIS_MAKEFILE))
14+
15+
# Turn on increased build verbosity by defining BUILD_VERBOSE in your main
16+
# Makefile or in your environment. You can also use V=1 on the make command
17+
# line.
18+
19+
ifeq ("$(origin V)", "command line")
20+
BUILD_VERBOSE=$(V)
21+
endif
22+
ifndef BUILD_VERBOSE
23+
BUILD_VERBOSE = 0
24+
endif
25+
ifeq ($(BUILD_VERBOSE),0)
26+
Q = @
27+
else
28+
Q =
29+
endif
30+
# Since this is a new feature, advertise it
31+
ifeq ($(BUILD_VERBOSE),0)
32+
$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.)
33+
endif
34+
35+
# default settings; can be overriden in main Makefile
36+
37+
PY_SRC ?= $(TOP)/py
38+
BUILD ?= build
39+
40+
RM = rm
41+
ECHO = @echo
42+
43+
AS = $(CROSS_COMPILE)as
44+
CC = $(CROSS_COMPILE)gcc
45+
LD = $(CROSS_COMPILE)ld
46+
OBJCOPY = $(CROSS_COMPILE)objcopy
47+
SIZE = $(CROSS_COMPILE)size
48+
49+
all:
50+
.PHONY: all
51+
52+
MKENV_INCLUDED = 1

py/mkrules.mk

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
ifneq ($(MKENV_INCLUDED),1)
2+
# We assume that mkenv is in the same directory as this file.
3+
THIS_MAKEFILE = $(lastword $(MAKEFILE_LIST))
4+
include $(dir $(THIS_MAKEFILE))mkenv.mk
5+
endif
6+
7+
# This file expects that OBJ contains a list of all of the object files.
8+
# The directory portion of each object file is used to locate the source
9+
# and should not contain any ..'s but rather be relative to the top of the
10+
# tree.
11+
#
12+
# So for example, py/map.c would have an object file name py/map.o
13+
# The object files will go into the build directory and mantain the same
14+
# directory structure as the source tree. So the final dependency will look
15+
# like this:
16+
#
17+
# build/py/map.o: py/map.c
18+
#
19+
# We set vpath to point to the top of the tree so that the source files
20+
# can be located. By following this scheme, it allows a single build rule
21+
# to be used to compile all .c files.
22+
23+
vpath %.S . $(TOP)
24+
$(BUILD)/%.o: %.S
25+
$(ECHO) "CC $<"
26+
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
27+
28+
vpath %.s . $(TOP)
29+
$(BUILD)/%.o: %.s
30+
$(ECHO) "AS $<"
31+
$(Q)$(AS) -o $@ $<
32+
33+
define compile_c
34+
$(ECHO) "CC $<"
35+
$(Q)$(CC) $(CFLAGS) -c -MD -o $@ $<
36+
@# The following fixes the dependency file.
37+
@# See http://make.paulandlesley.org/autodep.html for details.
38+
@cp $(@:.o=.d) $(@:.o=.P); \
39+
sed -e 's/#.*//' -e 's/^[^:]*: *//' -e 's/ *\\$$//' \
40+
-e '/^$$/ d' -e 's/$$/ :/' < $(@:.o=.d) >> $(@:.o=.P); \
41+
rm -f $(@:.o=.d)
42+
endef
43+
44+
vpath %.c . $(TOP)
45+
$(BUILD)/%.o: %.c
46+
$(call compile_c)
47+
48+
# The following rule uses | to create an order only prereuisite. Order only
49+
# prerequisites only get built if they don't exist. They don't cause timestamp
50+
# checkng to be performed.
51+
#
52+
# $(sort $(var)) removes duplicates
53+
#
54+
# The net effect of this, is it causes the objects to depend on the
55+
# object directories (but only for existance), and the object directories
56+
# will be created if they don't exist.
57+
OBJ_DIRS = $(sort $(dir $(OBJ)))
58+
$(OBJ): | $(OBJ_DIRS)
59+
$(OBJ_DIRS):
60+
mkdir -p $@
61+
62+
ifneq ($(PROG),)
63+
# Build a standalone executable (unix and unix-cpy do this)
64+
65+
all: $(PROG)
66+
67+
$(PROG): $(OBJ)
68+
$(ECHO) "LINK $<"
69+
$(Q)$(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS)
70+
ifndef DEBUG
71+
$(Q)strip $(PROG)
72+
endif
73+
$(Q)size $(PROG)
74+
75+
clean: clean-prog
76+
clean-prog:
77+
$(RM) -f $(PROG)
78+
79+
.PHONY: clean-prog
80+
endif
81+
82+
clean:
83+
$(RM) -rf $(BUILD)
84+
.PHONY: clean
85+
86+
print-cfg:
87+
$(ECHO) "PY_SRC = $(PY_SRC)"
88+
$(ECHO) "BUILD = $(BUILD)"
89+
$(ECHO) "OBJ = $(OBJ)"
90+
.PHONY: print-cfg
91+
92+
-include $(OBJ:.o=.P)

py/py.mk

Lines changed: 21 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,11 @@
1-
##########
2-
# The following should eventually go into a more central location
3-
# when a reorg is done.
4-
#
5-
# Turn on increased build verbosity by defining BUILD_VERBOSE in your main
6-
# Makefile or in your environment. You can also use V=1 on the make command
7-
# line.
8-
ifeq ("$(origin V)", "command line")
9-
BUILD_VERBOSE=$(V)
10-
endif
11-
ifndef BUILD_VERBOSE
12-
BUILD_VERBOSE = 0
13-
endif
14-
ifeq ($(BUILD_VERBOSE),0)
15-
Q = @
16-
else
17-
Q =
18-
endif
19-
# Since this is a new feature, advertise it
20-
ifeq ($(BUILD_VERBOSE),0)
21-
$(info Use make V=1 or set BUILD_VERBOSE in your environment to increase build verbosity.)
22-
endif
23-
#
24-
#########
25-
26-
# default settings; can be overriden in main Makefile
27-
28-
PY_SRC ?= ../py
29-
BUILD ?= build
30-
31-
# to create the build directory
32-
33-
$(BUILD):
34-
$(Q)mkdir -p $@
35-
361
# where py object files go (they have a name prefix to prevent filename clashes)
37-
38-
PY_BUILD = $(BUILD)/py.
2+
PY_BUILD = $(BUILD)/py
393

404
# file containing qstr defs for the core Python bit
415

426
PY_QSTR_DEFS = $(PY_SRC)/qstrdefs.h
437

448
# py object files
45-
469
PY_O_BASENAME = \
4710
nlrx86.o \
4811
nlrx64.o \
@@ -108,50 +71,37 @@ PY_O_BASENAME = \
10871
repl.o \
10972

11073
# prepend the build destination prefix to the py object files
111-
112-
PY_O = $(addprefix $(PY_BUILD), $(PY_O_BASENAME))
74+
PY_O = $(addprefix $(PY_BUILD)/, $(PY_O_BASENAME))
11375

11476
# qstr data
11577

116-
$(PY_BUILD)qstr.o: $(PY_BUILD)qstrdefs.generated.h
117-
118-
$(PY_BUILD)qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py
78+
# Adding an order only dependency on $(PY_BUILD) causes $(PY_BUILD) to get
79+
# created before we run the script to generate the .h
80+
$(PY_BUILD)/qstrdefs.generated.h: | $(PY_BUILD)
81+
$(PY_BUILD)/qstrdefs.generated.h: $(PY_QSTR_DEFS) $(QSTR_DEFS) $(PY_SRC)/makeqstrdata.py
11982
$(ECHO) "makeqstrdata $(PY_QSTR_DEFS) $(QSTR_DEFS)"
12083
$(Q)python $(PY_SRC)/makeqstrdata.py $(PY_QSTR_DEFS) $(QSTR_DEFS) > $@
12184

122-
# emitters
123-
124-
$(PY_BUILD)emitnx64.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
125-
$(ECHO) "CC $<"
126-
$(Q)$(CC) $(CFLAGS) -DN_X64 -c -o $@ $<
85+
# We don't know which source files actually need the generated.h (since
86+
# it is #included from str.h). The compiler generated dependencies will cause
87+
# the right .o's to get recompiled if the generated.h file changes. Adding
88+
# an order-only dependendency to all of the .o's will cause the generated .h
89+
# to get built before we try to compile any of them.
90+
$(PY_O): | $(PY_BUILD)/qstrdefs.generated.h
12791

128-
$(PY_BUILD)emitnthumb.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h
129-
$(ECHO) "CC $<"
130-
$(Q)$(CC) $(CFLAGS) -DN_THUMB -c -o $@ $<
131-
132-
# general source files
92+
# emitters
13393

134-
$(PY_BUILD)%.o: $(PY_SRC)/%.S
135-
$(ECHO) "CC $<"
136-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
94+
$(PY_BUILD)/emitnx64.o: CFLAGS += -DN_X64
95+
$(PY_BUILD)/emitnx64.o: py/emitnative.c
96+
$(call compile_c)
13797

138-
$(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h $(PY_SRC)/qstr.h $(PY_QSTR_DEFS) $(QSTR_DEFS)
139-
$(ECHO) "CC $<"
140-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
98+
$(PY_BUILD)/emitnthumb.o: CFLAGS += -DN_THUMB
99+
$(PY_BUILD)/emitnthumb.o: py/emitnative.c
100+
$(call compile_c)
141101

142102
# optimising gc for speed; 5ms down to 4ms on pybv2
143-
$(PY_BUILD)gc.o: $(PY_SRC)/gc.c
144-
$(ECHO) "CC $<"
145-
$(Q)$(CC) $(CFLAGS) -O3 -c -o $@ $<
103+
$(PY_BUILD)gc.o: CFLAGS += -O3
146104

147105
# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster)
148-
$(PY_BUILD)vm.o: $(PY_SRC)/vm.c
149-
$(ECHO) "CC $<"
150-
$(Q)$(CC) $(CFLAGS) -O3 -c -o $@ $<
151-
152-
# header dependencies
106+
$(PY_BUILD)vm.o: CFLAGS += -O3
153107

154-
$(PY_BUILD)parse.o: $(PY_SRC)/grammar.h
155-
$(PY_BUILD)compile.o: $(PY_SRC)/grammar.h
156-
$(PY_BUILD)emitcpy.o: $(PY_SRC)/emit.h
157-
$(PY_BUILD)emitbc.o: $(PY_SRC)/emit.h

py/qstr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const static qstr_pool_t const_pool = {
5555
(const byte*) "\0\0\0\0", // empty qstr
5656
#define Q(id, str) str,
5757
// TODO having 'build/' here is a bit of a hack, should take config variable from Makefile
58-
#include "build/py.qstrdefs.generated.h"
58+
#include "build/py/qstrdefs.generated.h"
5959
#undef Q
6060
},
6161
};

py/qstr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ enum {
99
MP_QSTR_ = 1, // the empty qstr
1010
#define Q(id, str) MP_QSTR_##id,
1111
// TODO having 'build/py.' here is a bit of a hack, should take config variable from Makefile
12-
#include "build/py.qstrdefs.generated.h"
12+
#include "build/py/qstrdefs.generated.h"
1313
#undef Q
1414
MP_QSTR_number_of,
1515
} category_t;

stm/Makefile

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
1-
# define main target
2-
all: all2
1+
include ../py/mkenv.mk
32

43
# qstr definitions (must come before including py.mk)
54
QSTR_DEFS = qstrdefsport.h
65

76
# include py core make definitions
87
include ../py/py.mk
98

10-
# program for deletion
11-
RM = /bin/rm
12-
ECHO = @echo
13-
149
CMSIS=cmsis
1510
STMSRC=lib
1611
#STMOTGSRC=usbhost
1712
FATFSSRC=fatfs
1813
CC3KSRC=cc3k
1914
DFU=../tools/dfu.py
2015

21-
AS = arm-none-eabi-as
22-
CC = arm-none-eabi-gcc
23-
LD = arm-none-eabi-ld
24-
OBJCOPY = arm-none-eabi-objcopy
25-
SIZE = arm-none-eabi-size
16+
CROSS_COMPILE = arm-none-eabi-
2617

2718
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
2819
CFLAGS = -I. -I$(PY_SRC) -I$(FATFSSRC) -I$(CMSIS) -I$(STMSRC) -Wall -ansi -std=gnu99 $(CFLAGS_CORTEX_M4)
@@ -71,11 +62,12 @@ SRC_S = \
7162
startup_stm32f40xx.s \
7263
gchelper.s \
7364

74-
SRC_FATFS = \
65+
SRC_FATFS = $(addprefix $(FATFSSRC)/,\
7566
ff.c \
7667
diskio.c \
68+
)
7769

78-
SRC_STM = \
70+
SRC_STM = $(addprefix $(STMSRC)/,\
7971
stm32f4xx_rcc.c \
8072
stm32f4xx_syscfg.c \
8173
stm_misc.c \
@@ -111,8 +103,9 @@ SRC_STM = \
111103
usbd_msc_data.c \
112104
usbd_msc_scsi.c \
113105
usbd_storage_msd.c \
106+
)
114107

115-
#SRC_STM_OTG = \
108+
#SRC_STM_OTG = $(addprefix $(STMSRC)/,\
116109
usb_hcd.c \
117110
usb_hcd_int.c \
118111
usbh_core.c \
@@ -124,8 +117,9 @@ SRC_STM = \
124117
usbh_hid_mouse.c \
125118
usbh_hid_keybd.c \
126119
# usb_otg.c \
120+
)
127121

128-
SRC_CC3K = \
122+
SRC_CC3K = $(addprefix $(CC3KSRC)/,\
129123
cc3000_common.c \
130124
evnt_handler.c \
131125
hci.c \
@@ -136,11 +130,12 @@ SRC_CC3K = \
136130
wlan.c \
137131
ccspi.c \
138132
pybcc3k.c \
133+
)
139134

140135
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(SRC_S:.s=.o) $(SRC_FATFS:.c=.o) $(SRC_STM:.c=.o)) # $(SRC_CC3K:.c=.o))
141136
#OBJ += $(addprefix $(BUILD)/, $(SRC_STM_OTG:.c=.o))
142137

143-
all2: $(BUILD) $(BUILD)/flash.dfu
138+
all: $(BUILD) $(BUILD)/flash.dfu
144139

145140
$(BUILD)/flash.dfu: $(BUILD)/flash0.bin $(BUILD)/flash1.bin
146141
$(ECHO) "Create $@"
@@ -157,31 +152,5 @@ $(BUILD)/flash.elf: $(OBJ)
157152
$(Q)$(LD) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
158153
$(Q)$(SIZE) $@
159154

160-
$(BUILD)/%.o: %.s
161-
$(ECHO) "AS $<"
162-
$(Q)$(AS) -o $@ $<
163-
164-
$(BUILD)/%.o: %.c $(QSTR_DEFS)
165-
$(ECHO) "CC $<"
166-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
167-
168-
$(BUILD)/%.o: $(FATFSSRC)/%.c
169-
$(ECHO) "CC $<"
170-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
171-
172-
$(BUILD)/%.o: $(STMSRC)/%.c
173-
$(ECHO) "CC $<"
174-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
175-
176-
#$(BUILD)/%.o: $(STMOTGSRC)/%.c
177-
# $(ECHO) "CC $<"
178-
# $(Q)$(CC) $(CFLAGS) -c -o $@ $<
179-
180-
$(BUILD)/%.o: $(CC3KSRC)/%.c
181-
$(ECHO) "CC $<"
182-
$(Q)$(CC) $(CFLAGS) -c -o $@ $<
183-
184-
clean:
185-
$(RM) -rf $(BUILD)
155+
include ../py/mkrules.mk
186156

187-
.PHONY: all all2 clean

0 commit comments

Comments
 (0)