Debian Codegraph @

ports/unix/Makefile

raw ◀ ▶
1 Debian patch touch this file
1 # Select the variant to build for:
2 ifdef VARIANT_DIR
3 # Custom variant path - remove trailing slash and get the final component of
4 # the path as the variant name.
5 VARIANT ?= $(notdir $(VARIANT_DIR:/=))
6 else
7 # If not given on the command line, then default to standard.
8 VARIANT ?= standard
9 VARIANT_DIR ?= variants/$(VARIANT)
10 endif
11
12 ifeq ($(wildcard $(VARIANT_DIR)/.),)
13 $(error Invalid VARIANT specified: $(VARIANT_DIR))
14 endif
15
16 # If the build directory is not given, make it reflect the variant name.
17 BUILD ?= build-$(VARIANT)
18
19 include ../../py/mkenv.mk
20 -include mpconfigport.mk
21 include $(VARIANT_DIR)/mpconfigvariant.mk
22
23 # Use the default frozen manifest, variants may override this.
24 FROZEN_MANIFEST ?= variants/manifest.py
25
26 # This should be configured by the mpconfigvariant.mk
27 PROG ?= micropython
28
29 # qstr definitions (must come before including py.mk)
30 QSTR_DEFS += qstrdefsport.h
31 QSTR_GLOBAL_DEPENDENCIES += $(VARIANT_DIR)/mpconfigvariant.h
32
33 # OS name, for simple autoconfig
34 UNAME_S := $(shell uname -s)
35
36 # include py core make definitions
37 include $(TOP)/py/py.mk
38 include $(TOP)/extmod/extmod.mk
39
40 GIT_SUBMODULES += lib/berkeley-db-1.xx
41
42 INC += -I.
43 INC += -I$(TOP)
44 INC += -I$(BUILD)
45
46 # compiler settings
47 CWARN = -Wall -Werror
48 CWARN += -Wextra -Wno-unused-parameter -Wpointer-arith -Wdouble-promotion -Wfloat-conversion
49 CFLAGS += $(INC) $(CWARN) -std=gnu99 -DUNIX $(COPT) -I$(VARIANT_DIR) $(CFLAGS_EXTRA)
•50 ifdef MP_CONFIGFILE
51 CFLAGS += -DMP_CONFIGFILE="$(MP_CONFIGFILE)"
52 endif
53
54 # Force the use of 64-bits for file sizes in C library functions on 32-bit platforms.
55 # This option has no effect on 64-bit builds.
56 CFLAGS += -D_FILE_OFFSET_BITS=64
57
58 # Force the use of 64-bits for time_t in C library functions on 32-bit platforms.
59 # This option has no effect on 64-bit builds.
60 CFLAGS += -D_TIME_BITS=64
61
62 # Debugging/Optimization
63 ifdef DEBUG
64 COPT ?= -Og
65 else
66 COPT ?= -Os
67 COPT += -DNDEBUG
68 endif
69
70 # Remove unused sections.
71 COPT += -fdata-sections -ffunction-sections
72
73 # Note: Symbols and debug information will still be stripped from the final binary
74 # unless "DEBUG=1" or "STRIP=" is passed to make, see README.md for details.
75 CFLAGS += -g
76
77 ifndef DEBUG
78 # _FORTIFY_SOURCE is a feature in gcc/glibc which is intended to provide extra
79 # security for detecting buffer overflows. Some distros (Ubuntu at the very least)
80 # have it enabled by default.
81 #
82 # gcc already optimizes some printf calls to call puts and/or putchar. When
83 # _FORTIFY_SOURCE is enabled and compiling with -O1 or greater, then some
84 # printf calls will also be optimized to call __printf_chk (in glibc). Any
85 # printfs which get redirected to __printf_chk are then no longer synchronized
86 # with printfs that go through mp_printf.
87 #
88 # In MicroPython, we don't want to use the runtime library's printf but rather
89 # go through mp_printf, so that stdout is properly tied into streams, etc.
90 # This means that we either need to turn off _FORTIFY_SOURCE or provide our
91 # own implementation of __printf_chk. We've chosen to turn off _FORTIFY_SOURCE.
92 # It should also be noted that the use of printf in MicroPython is typically
93 # quite limited anyways (primarily for debug and some error reporting, etc
94 # in the unix version).
95 #
96 # Information about _FORTIFY_SOURCE seems to be rather scarce. The best I could
97 # find was this: https://securityblog.redhat.com/2014/03/26/fortify-and-you/
98 # Original patchset was introduced by
99 # https://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html .
100 #
101 # Turning off _FORTIFY_SOURCE is only required when compiling with -O1 or greater
102 CFLAGS += -U _FORTIFY_SOURCE
103 endif
104
105 # On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
106 # The unix port of MicroPython on OSX must be compiled with clang,
107 # while cross-compile ports require gcc, so we test here for OSX and
108 # if necessary override the value of 'CC' set in py/mkenv.mk
109 ifeq ($(UNAME_S),Darwin)
110 ifeq ($(MICROPY_FORCE_32BIT),1)
111 CC = clang -m32
112 else
113 CC = clang
114 endif
115 # Use clang syntax for map file
116 LDFLAGS_ARCH = -Wl,-map,[email protected] -Wl,-dead_strip
117 else
118 # Use gcc syntax for map file
119 LDFLAGS_ARCH = -Wl,[email protected],--cref -Wl,--gc-sections
120 endif
121 LDFLAGS += $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
122
123 # Flags to link with pthread library
124 LIBPTHREAD = -lpthread
125
126 ifeq ($(MICROPY_USE_READLINE),1)
127 INC += -I$(TOP)/shared/readline
128 CFLAGS += -DMICROPY_USE_READLINE=1
129 SHARED_SRC_C_EXTRA += readline/readline.c
130 endif
131 ifeq ($(MICROPY_PY_TERMIOS),1)
132 CFLAGS += -DMICROPY_PY_TERMIOS=1
133 endif
134 ifeq ($(MICROPY_PY_SOCKET),1)
135 CFLAGS += -DMICROPY_PY_SOCKET=1
136 endif
137 ifeq ($(MICROPY_PY_THREAD),1)
138 ifeq ($(MICROPY_PY_THREAD_GIL),1)
139 CFLAGS += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=1
140 else
141 CFLAGS += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0
142 endif
143 LDFLAGS += $(LIBPTHREAD)
144 endif
145
146 ifeq ($(MICROPY_PY_SSL),1)
147 ifeq ($(MICROPY_SSL_AXTLS),1)
148
149 endif
150 endif
151
152 # If the variant enables it, enable modbluetooth.
153 ifeq ($(MICROPY_PY_BLUETOOTH),1)
154 ifeq ($(MICROPY_BLUETOOTH_BTSTACK),1)
155 HAVE_LIBUSB := $(shell (which pkg-config > /dev/null && pkg-config --exists libusb-1.0) 2>/dev/null && echo '1')
156
157 # Figure out which BTstack transport to use.
158 ifeq ($(HAVE_LIBUSB),1)
159 # Default to btstack-over-usb.
160 MICROPY_BLUETOOTH_BTSTACK_USB ?= 1
161 else
162 # Fallback to HCI controller via a H4 UART (e.g. Zephyr on nRF) over a /dev/tty serial port.
163 MICROPY_BLUETOOTH_BTSTACK_H4 ?= 1
164 endif
165
166 SRC_BTSTACK_C += lib/btstack/platform/embedded/btstack_run_loop_embedded.c
167 endif
168 endif
169
170 ifeq ($(MICROPY_PY_FFI),1)
171
172 ifeq ($(MICROPY_STANDALONE),1)
173 # Build libffi from source.
174 GIT_SUBMODULES += lib/libffi
175 DEPLIBS += libffi
176 LIBFFI_CFLAGS := -I$(shell ls -1d $(BUILD)/lib/libffi/include)
177 ifeq ($(MICROPY_FORCE_32BIT),1)
178 LIBFFI_LDFLAGS = $(BUILD)/lib/libffi/out/lib32/libffi.a
179 else
180 LIBFFI_LDFLAGS = $(BUILD)/lib/libffi/out/lib/libffi.a
181 endif
182 else
183 # Use system version of libffi.
184 LIBFFI_CFLAGS := $(shell pkg-config --cflags libffi)
185 LIBFFI_LDFLAGS := $(shell pkg-config --libs libffi)
186 endif
187
188 ifeq ($(UNAME_S),Linux)
189 LIBFFI_LDFLAGS += -ldl
190 endif
191
192 CFLAGS += $(LIBFFI_CFLAGS) -DMICROPY_PY_FFI=1
193 LDFLAGS += $(LIBFFI_LDFLAGS)
194 endif
195
196 ifeq ($(MICROPY_PY_JNI),1)
197 # Path for 64-bit OpenJDK, should be adjusted for other JDKs
•198 CFLAGS += -I/usr/lib/jvm/default-java/include -I/usr/lib/jvm/default-java/include/linux -DMICROPY_PY_JNI=1
199 endif
200
201 # source files
202 SRC_C += \
203 main.c \
204 gccollect.c \
205 unix_mphal.c \
206 mpthreadport.c \
207 input.c \
208 alloc.c \
209 fatfs_port.c \
210 mpbthciport.c \
211 mpbtstackport_common.c \
212 mpbtstackport_h4.c \
213 mpbtstackport_usb.c \
214 mpnimbleport.c \
215 modtermios.c \
216 modsocket.c \
217 modffi.c \
218 modjni.c \
219 $(wildcard $(VARIANT_DIR)/*.c)
220
221 SHARED_SRC_C += $(addprefix shared/,\
222 runtime/gchelper_generic.c \
223 timeutils/timeutils.c \
224 $(SHARED_SRC_C_EXTRA) \
225 )
226
227 SRC_CXX += \
228
229 OBJ = $(PY_O)
230 OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
231 OBJ += $(addprefix $(BUILD)/, $(SRC_CXX:.cpp=.o))
232 OBJ += $(addprefix $(BUILD)/, $(SHARED_SRC_C:.c=.o))
233
234 # List of sources for qstr extraction
235 SRC_QSTR += $(SRC_C) $(SRC_CXX) $(SHARED_SRC_C)
236
237 ifneq ($(FROZEN_MANIFEST),)
238 CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs
239 endif
240
241 CXXFLAGS += $(filter-out -Wmissing-prototypes -Wold-style-definition -std=gnu99,$(CFLAGS) $(CXXFLAGS_MOD))
242
243 ifeq ($(MICROPY_FORCE_32BIT),1)
244 RUN_TESTS_MPY_CROSS_FLAGS = --mpy-cross-flags='-march=x86'
245 endif
246
247 ifeq ($(CROSS_COMPILE),arm-linux-gnueabi-)
248 # Force disable error text compression when compiling for ARM as the compiler
249 # cannot optimise out the giant strcmp list generated for MP_MATCH_COMPRESSED.
250 # Checked on:
251 # arm-linux-gnueabi-gcc (Ubuntu/Linaro 7.5.0-3ubuntu1~18.04) 7.5.0
252 # arm-linux-gnueabi-gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
253 # See https://github.com/micropython/micropython/pull/7659 for details.
254 $(info Detected arm-linux-gnueabi-gcc. Disabling error message compression.)
255 MICROPY_ROM_TEXT_COMPRESSION = 0
256 endif
257
258 include $(TOP)/py/mkrules.mk
259
260 .PHONY: test test_full_no_native test_full
261
262 test: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py
263 $(eval DIRNAME=ports/$(notdir $(CURDIR)))
•264 cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py $(TESTS_ARGS)
265
266 test_full_no_native: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py
267 $(eval DIRNAME=ports/$(notdir $(CURDIR)))
268 cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py
269 cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) -d basics float micropython
270 cat $(TOP)/tests/basics/0prelim.py | ./$(BUILD)/$(PROG) | grep -q 'abc'
271
272 test_full: $(BUILD)/$(PROG) $(TOP)/tests/run-tests.py test_full_no_native
273 $(eval DIRNAME=ports/$(notdir $(CURDIR)))
274 cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --emit native
275 cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(BUILD)/$(PROG) ./run-tests.py --via-mpy $(RUN_TESTS_MPY_CROSS_FLAGS) --emit native -d basics float micropython
276
277 test_gcov: test_full
278 gcov -o $(BUILD)/py $(TOP)/py/*.c
279 gcov -o $(BUILD)/extmod $(TOP)/extmod/*.c
280
281 # Value of configure's --host= option (required for cross-compilation).
282 # Deduce it from CROSS_COMPILE by default, but can be overridden.
283 ifneq ($(CROSS_COMPILE),)
284 CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE))
285 else
286 CROSS_COMPILE_HOST =
287 endif
288
289 deplibs: $(DEPLIBS)
290
291 libffi: $(BUILD)/lib/libffi/include/ffi.h
292
293 $(TOP)/lib/libffi/configure: $(TOP)/lib/libffi/autogen.sh
294 cd $(TOP)/lib/libffi; ./autogen.sh
295
296 # install-exec-recursive & install-data-am targets are used to avoid building
297 # docs and depending on makeinfo
298 $(BUILD)/lib/libffi/include/ffi.h: $(TOP)/lib/libffi/configure
299 mkdir -p $(BUILD)/lib/libffi; cd $(BUILD)/lib/libffi; \
300 $(abspath $(TOP))/lib/libffi/configure $(CROSS_COMPILE_HOST) --prefix=$$PWD/out --disable-shared --disable-structs CC="$(CC)" CXX="$(CXX)" LD="$(LD)" CFLAGS="-Os -fomit-frame-pointer -fstrict-aliasing -ffast-math -fno-exceptions"; \
301 $(MAKE) install-exec-recursive; $(MAKE) -C include install-data-am
302
303 PREFIX = /usr/local
304 BINDIR = $(DESTDIR)$(PREFIX)/bin
305
306 install: $(BUILD)/$(PROG)
307 install -d $(BINDIR)
308 install $(BUILD)/$(PROG) $(BINDIR)/$(PROG)
309
310 uninstall:
311 -rm $(BINDIR)/$(PROG)