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