Makefile 10.5 KB
Newer Older
1
-include mpconfigport.mk
2
include ../py/mkenv.mk
3

4
# define main target
5
PROG = micropython
6

7
8
9
# qstr definitions (must come before including py.mk)
QSTR_DEFS = qstrdefsport.h

10
11
12
# OS name, for simple autoconfig
UNAME_S := $(shell uname -s)

13
14
# include py core make definitions
include ../py/py.mk
15

16
INC +=  -I.
17
INC +=  -I..
Robert HH's avatar
Robert HH committed
18
INC += -I../lib/timeutils
19
INC += -I$(BUILD)
20

21
# compiler settings
22
CWARN = -Wall -Werror
23
CWARN += -Wpointer-arith -Wuninitialized
24
CFLAGS = $(INC) $(CWARN) -ansi -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
25

26
27
28
29
30
# Debugging/Optimization
ifdef DEBUG
CFLAGS += -g
COPT = -O0
else
31
COPT = -Os -fdata-sections -ffunction-sections #-DNDEBUG
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# _FORTIFY_SOURCE is a feature in gcc/glibc which is intended to provide extra
# security for detecting buffer overflows. Some distros (Ubuntu at the very least)
# have it enabled by default.
#
# gcc already optimizes some printf calls to call puts and/or putchar. When
# _FORTIFY_SOURCE is enabled and compiling with -O1 or greater, then some
# printf calls will also be optimized to call __printf_chk (in glibc). Any
# printfs which get redirected to __printf_chk are then no longer synchronized
# with printfs that go through mp_printf.
#
# In MicroPython, we don't want to use the runtime library's printf but rather
# go through mp_printf, so that stdout is properly tied into streams, etc.
# This means that we either need to turn off _FORTIFY_SOURCE or provide our
# own implementation of __printf_chk. We've chosen to turn off _FORTIFY_SOURCE.
# It should also be noted that the use of printf in MicroPython is typically
# quite limited anyways (primarily for debug and some error reporting, etc
# in the unix version).
#
# Information about _FORTIFY_SOURCE seems to be rather scarce. The best I could
# find was this: https://securityblog.redhat.com/2014/03/26/fortify-and-you/
# Original patchset was introduced by
# https://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html .
#
# Turning off _FORTIFY_SOURCE is only required when compiling with -O1 or greater
CFLAGS += -U _FORTIFY_SOURCE
57
58
endif

59
60
61
62
# On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
# The unix port of micropython on OSX must be compiled with clang,
# while cross-compile ports require gcc, so we test here for OSX and 
# if necessary override the value of 'CC' set in py/mkenv.mk
blmorris's avatar
blmorris committed
63
64
ifeq ($(UNAME_S),Darwin)
CC = clang
65
# Use clang syntax for map file
66
LDFLAGS_ARCH = -Wl,-map,$@.map -Wl,-dead_strip
67
else
68
# Use gcc syntax for map file
69
LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
blmorris's avatar
blmorris committed
70
endif
71
LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
72

73
ifeq ($(MICROPY_FORCE_32BIT),1)
74
75
# Note: you may need to install i386 versions of dependency packages,
# starting with linux-libc-dev:i386
76
ifeq ($(MICROPY_PY_FFI),1)
77
78
79
80
81
82
ifeq ($(UNAME_S),Linux)
CFLAGS_MOD += -I/usr/include/i686-linux-gnu
endif
endif
endif

83
ifeq ($(MICROPY_USE_READLINE),1)
84
INC +=  -I../lib/mp-readline
85
CFLAGS_MOD += -DMICROPY_USE_READLINE=1
86
87
LIB_SRC_C_EXTRA += mp-readline/readline.c
endif
88
89
90
91
ifeq ($(MICROPY_PY_TERMIOS),1)
CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1
SRC_MOD += modtermios.c
endif
92
93
94
95
ifeq ($(MICROPY_PY_SOCKET),1)
CFLAGS_MOD += -DMICROPY_PY_SOCKET=1
SRC_MOD += modsocket.c
endif
96
ifeq ($(MICROPY_PY_THREAD),1)
97
CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0
98
99
LDFLAGS_MOD += -lpthread
endif
100

101
ifeq ($(MICROPY_PY_FFI),1)
102
103

ifeq ($(MICROPY_STANDALONE),1)
104
LIBFFI_CFLAGS_MOD := -I$(shell ls -1d ../lib/libffi/build_dir/out/lib/libffi-*/include)
105
 ifeq ($(MICROPY_FORCE_32BIT),1)
106
  LIBFFI_LDFLAGS_MOD = ../lib/libffi/build_dir/out/lib32/libffi.a
107
 else
108
  LIBFFI_LDFLAGS_MOD = ../lib/libffi/build_dir/out/lib/libffi.a
109
110
 endif
else
111
LIBFFI_CFLAGS_MOD := $(shell pkg-config --cflags libffi)
112
113
114
LIBFFI_LDFLAGS_MOD := $(shell pkg-config --libs libffi)
endif

115
ifeq ($(UNAME_S),Linux)
116
LIBFFI_LDFLAGS_MOD += -ldl
117
endif
118
119

CFLAGS_MOD += $(LIBFFI_CFLAGS_MOD) -DMICROPY_PY_FFI=1
120
LDFLAGS_MOD += $(LIBFFI_LDFLAGS_MOD)
121
SRC_MOD += modffi.c
122
123
endif

124
125
126
127
128
ifeq ($(MICROPY_PY_JNI),1)
# Path for 64-bit OpenJDK, should be adjusted for other JDKs
CFLAGS_MOD += -I/usr/lib/jvm/java-7-openjdk-amd64/include -DMICROPY_PY_JNI=1
SRC_MOD += modjni.c
endif
129

130
# source files
131
132
SRC_C = \
	main.c \
133
	gccollect.c \
134
	unix_mphal.c \
135
	mpthreadport.c \
Dave Hylands's avatar
Dave Hylands committed
136
	input.c \
137
	file.c \
138
	modmachine.c \
139
	modos.c \
140
	modtime.c \
141
	moduselect.c \
142
	alloc.c \
143
	coverage.c \
Paul Sokolovsky's avatar
Paul Sokolovsky committed
144
	fatfs_port.c \
145
	$(SRC_MOD)
146

147
148
149
150
151
# Include builtin package manager in the standard build (and coverage)
ifeq ($(PROG),micropython)
SRC_C += $(BUILD)/_frozen_upip.c
else ifeq ($(PROG),micropython_coverage)
SRC_C += $(BUILD)/_frozen_upip.c
152
153
else ifeq ($(PROG), micropython_nanbox)
SRC_C += $(BUILD)/_frozen_upip.c
pohmelie's avatar
pohmelie committed
154
155
else ifeq ($(PROG), micropython_freedos)
SRC_C += $(BUILD)/_frozen_upip.c
156
157
endif

158
159
LIB_SRC_C = $(addprefix lib/,\
	$(LIB_SRC_C_EXTRA) \
160
161
162
	timeutils/timeutils.c \
	)

163
# FatFS VFS support
164
LIB_SRC_C += $(addprefix lib/,\
Paul Sokolovsky's avatar
Paul Sokolovsky committed
165
166
	fatfs/ff.c \
	fatfs/option/ccsbcs.c \
167
	)
blmorris's avatar
blmorris committed
168

169
170
171
OBJ = $(PY_O)
OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
Paul Sokolovsky's avatar
Paul Sokolovsky committed
172
OBJ += $(addprefix $(BUILD)/, $(STMHAL_SRC_C:.c=.o))
173

174
175
176
177
178
179
# List of sources for qstr extraction
SRC_QSTR += $(SRC_C) $(LIB_SRC_C)
# Append any auto-generated sources that are needed by sources listed in
# SRC_QSTR
SRC_QSTR_AUTO_DEPS +=

180
181
182
183
184
ifneq ($(FROZEN_MPY_DIR),)
# To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and
# then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch).
MPY_CROSS = ../mpy-cross/mpy-cross
MPY_TOOL = ../tools/mpy-tool.py
185
FROZEN_MPY_PY_FILES := $(shell find -L $(FROZEN_MPY_DIR) -type f -name '*.py')
186
187
188
189
190
191
192
193
FROZEN_MPY_MPY_FILES := $(addprefix $(BUILD)/,$(FROZEN_MPY_PY_FILES:.py=.mpy))
CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
CFLAGS += -DMICROPY_OPT_CACHE_MAP_LOOKUP_IN_BYTECODE=0 # not supported
CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs
OBJ += $(BUILD)/$(BUILD)/frozen_mpy.o

# to build .mpy files from .py files
Damien George's avatar
Damien George committed
194
$(BUILD)/$(FROZEN_MPY_DIR)/%.mpy: $(FROZEN_MPY_DIR)/%.py
195
	@$(ECHO) "MPY $<"
Damien George's avatar
Damien George committed
196
197
	$(Q)$(MKDIR) -p $(dir $@)
	$(Q)$(MPY_CROSS) -o $@ -s $(^:$(FROZEN_MPY_DIR)/%=%) $^
198
199
200
201
202
203
204

# to build frozen_mpy.c from all .mpy files
$(BUILD)/frozen_mpy.c: $(FROZEN_MPY_MPY_FILES) $(BUILD)/genhdr/qstrdefs.generated.h
	@$(ECHO) "Creating $@"
	$(Q)$(PYTHON) $(MPY_TOOL) -f -q $(BUILD)/genhdr/qstrdefs.preprocessed.h $(FROZEN_MPY_MPY_FILES) > $@
endif

205

206
include ../py/mkrules.mk
207

208
209
210
211
.PHONY: test

test: $(PROG) ../tests/run-tests
	$(eval DIRNAME=$(notdir $(CURDIR)))
212
	cd ../tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests
Sebastian Plamauer's avatar
Sebastian Plamauer committed
213
214
215
216
217

# install micropython in /usr/local/bin
TARGET = micropython
PREFIX = $(DESTDIR)/usr/local
BINDIR = $(PREFIX)/bin
218
219
PIPSRC = ../tools/pip-micropython
PIPTARGET = pip-micropython
Sebastian Plamauer's avatar
Sebastian Plamauer committed
220
221

install: micropython
222
223
224
	install -d $(BINDIR)
	install $(TARGET) $(BINDIR)/$(TARGET)
	install $(PIPSRC) $(BINDIR)/$(PIPTARGET)
Sebastian Plamauer's avatar
Sebastian Plamauer committed
225
226
227
228

# uninstall micropython
uninstall:
	-rm $(BINDIR)/$(TARGET)
229
	-rm $(BINDIR)/$(PIPTARGET)
230
231
232

# build synthetically fast interpreter for benchmarking
fast:
233
234
235
236
	$(MAKE) COPT="-O2 -DNDEBUG -fno-crossjumping" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_fast.h>"' BUILD=build-fast PROG=micropython_fast

# build a minimal interpreter
minimal:
237
238
239
240
241
	$(MAKE) COPT="-Os -DNDEBUG" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_minimal.h>"' \
	    BUILD=build-minimal PROG=micropython_minimal \
	    MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_SOCKET=0 MICROPY_PY_THREAD=0 \
	    MICROPY_PY_TERMIOS=0 MICROPY_PY_USSL=0 \
	    MICROPY_USE_READLINE=0 MICROPY_FATFS=0
242

243
244
245
246
247
248
249
# build interpreter with nan-boxing as object model
nanbox:
	$(MAKE) \
	CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_nanbox.h>"' \
	BUILD=build-nanbox \
	PROG=micropython_nanbox \
	MICROPY_FORCE_32BIT=1 \
250
	MICROPY_PY_USSL=0
251

pohmelie's avatar
pohmelie committed
252
253
254
freedos:
	$(MAKE) \
	CC=i586-pc-msdosdjgpp-gcc \
255
256
	STRIP=i586-pc-msdosdjgpp-strip \
	SIZE=i586-pc-msdosdjgpp-size \
pohmelie's avatar
pohmelie committed
257
258
259
260
261
262
263
	CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_freedos.h>" -DMICROPY_NLR_SETJMP -Dtgamma=gamma -DMICROPY_EMIT_X86=0 -DMICROPY_NO_ALLOCA=1 -DMICROPY_PY_USELECT=0' \
	BUILD=build-freedos \
	PROG=micropython_freedos \
	MICROPY_PY_SOCKET=0 \
	MICROPY_PY_FFI=0 \
	MICROPY_PY_JNI=0

264
# build an interpreter for coverage testing and do the testing
265
266
# btree module is disabled, because with extra warnings enabled,
# building berkeleydb errors out
267
coverage:
Paul Sokolovsky's avatar
Paul Sokolovsky committed
268
	$(MAKE) COPT="-O0" MICROPY_PY_BTREE=0 CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_coverage.h>" -fprofile-arcs -ftest-coverage -Wdouble-promotion -Wformat -Wmissing-declarations -Wmissing-prototypes -Wold-style-definition -Wpointer-arith -Wshadow -Wsign-compare -Wuninitialized -Wunused-parameter -DMICROPY_UNIX_COVERAGE' LDFLAGS_EXTRA='-fprofile-arcs -ftest-coverage' BUILD=build-coverage PROG=micropython_coverage
269
270

coverage_test: coverage
271
272
	$(eval DIRNAME=$(notdir $(CURDIR)))
	cd ../tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests
273
	cd ../tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests --emit native
274
	gcov -o build-coverage/py ../py/*.c
275
	gcov -o build-coverage/extmod ../extmod/*.c
276

277
278
$(BUILD)/_frozen_upip.c: $(BUILD)/frozen_upip/upip.py
	../tools/make-frozen.py $(dir $^) > $@
279
280
281
282

# Select latest upip version available
UPIP_TARBALL := $(shell ls -1 -v ../tools/micropython-upip-*.tar.gz | tail -n1)

283
$(BUILD)/frozen_upip/upip.py: $(UPIP_TARBALL)
284
	$(ECHO) "MISC Preparing upip as frozen module"
285
	$(Q)mkdir -p $(BUILD)
286
	$(Q)rm -rf $(BUILD)/micropython-upip-*
287
	$(Q)tar -C $(BUILD) -xz -f $^
288
289
290
	$(Q)rm -rf $(dir $@)
	$(Q)mkdir -p $(dir $@)
	$(Q)cp $(BUILD)/micropython-upip-*/upip*.py $(dir $@)
291
292
293
294
295
296
297
298
299
300


# Value of configure's --host= option (required for cross-compilation).
# Deduce it from CROSS_COMPILE by default, but can be overriden.
ifneq ($(CROSS_COMPILE),)
CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE))
else
CROSS_COMPILE_HOST =
endif

301
deplibs: libffi axtls
302

Paul Sokolovsky's avatar
Paul Sokolovsky committed
303
304
# install-exec-recursive & install-data-am targets are used to avoid building
# docs and depending on makeinfo
305
306
307
libffi:
	cd ../lib/libffi; git clean -d -x -f
	cd ../lib/libffi; ./autogen.sh
308
	mkdir -p ../lib/libffi/build_dir; cd ../lib/libffi/build_dir; \
309
	../configure $(CROSS_COMPILE_HOST) --prefix=$$PWD/out --disable-structs CC="$(CC)" CXX="$(CXX)" LD="$(LD)" CFLAGS="-Os -fomit-frame-pointer -fstrict-aliasing -ffast-math -fno-exceptions"; \
Paul Sokolovsky's avatar
Paul Sokolovsky committed
310
	make install-exec-recursive; make -C include install-data-am
311

312
313
314
axtls: $(BUILD)/libaxtls.a

$(BUILD)/libaxtls.a: ../lib/axtls/README | $(OBJ_DIRS)
315
316
317
318
	cd ../lib/axtls; cp config/upyconfig config/.config
	cd ../lib/axtls; make oldconfig -B
	cd ../lib/axtls; make clean
	cd ../lib/axtls; make all CC="$(CC)" LD="$(LD)"
319
	cp ../lib/axtls/_stage/libaxtls.a $@
320
321
322
323

../lib/axtls/README:
	@echo "You cloned without --recursive, fetching submodules for you."
	(cd ..; git submodule update --init --recursive)