Makefile 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. -include mpconfigport.mk
  2. include ../../py/mkenv.mk
  3. FROZEN_DIR = scripts
  4. FROZEN_MPY_DIR = modules
  5. # define main target
  6. PROG = micropython
  7. # qstr definitions (must come before including py.mk)
  8. QSTR_DEFS = qstrdefsport.h
  9. # OS name, for simple autoconfig
  10. UNAME_S := $(shell uname -s)
  11. # include py core make definitions
  12. include $(TOP)/py/py.mk
  13. INC += -I.
  14. INC += -I$(TOP)
  15. INC += -I$(BUILD)
  16. # compiler settings
  17. CWARN = -Wall -Werror
  18. CWARN += -Wpointer-arith -Wuninitialized
  19. CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
  20. # Debugging/Optimization
  21. ifdef DEBUG
  22. CFLAGS += -g
  23. COPT = -O0
  24. else
  25. COPT = -Os -fdata-sections -ffunction-sections -DNDEBUG
  26. # _FORTIFY_SOURCE is a feature in gcc/glibc which is intended to provide extra
  27. # security for detecting buffer overflows. Some distros (Ubuntu at the very least)
  28. # have it enabled by default.
  29. #
  30. # gcc already optimizes some printf calls to call puts and/or putchar. When
  31. # _FORTIFY_SOURCE is enabled and compiling with -O1 or greater, then some
  32. # printf calls will also be optimized to call __printf_chk (in glibc). Any
  33. # printfs which get redirected to __printf_chk are then no longer synchronized
  34. # with printfs that go through mp_printf.
  35. #
  36. # In MicroPython, we don't want to use the runtime library's printf but rather
  37. # go through mp_printf, so that stdout is properly tied into streams, etc.
  38. # This means that we either need to turn off _FORTIFY_SOURCE or provide our
  39. # own implementation of __printf_chk. We've chosen to turn off _FORTIFY_SOURCE.
  40. # It should also be noted that the use of printf in MicroPython is typically
  41. # quite limited anyways (primarily for debug and some error reporting, etc
  42. # in the unix version).
  43. #
  44. # Information about _FORTIFY_SOURCE seems to be rather scarce. The best I could
  45. # find was this: https://securityblog.redhat.com/2014/03/26/fortify-and-you/
  46. # Original patchset was introduced by
  47. # https://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html .
  48. #
  49. # Turning off _FORTIFY_SOURCE is only required when compiling with -O1 or greater
  50. CFLAGS += -U _FORTIFY_SOURCE
  51. endif
  52. # On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
  53. # The unix port of MicroPython on OSX must be compiled with clang,
  54. # while cross-compile ports require gcc, so we test here for OSX and
  55. # if necessary override the value of 'CC' set in py/mkenv.mk
  56. ifeq ($(UNAME_S),Darwin)
  57. ifeq ($(MICROPY_FORCE_32BIT),1)
  58. CC = clang -m32
  59. else
  60. CC = clang
  61. endif
  62. # Use clang syntax for map file
  63. LDFLAGS_ARCH = -Wl,-map,$@.map -Wl,-dead_strip
  64. else
  65. # Use gcc syntax for map file
  66. LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref -Wl,--gc-sections
  67. endif
  68. LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
  69. ifeq ($(MICROPY_FORCE_32BIT),1)
  70. # Note: you may need to install i386 versions of dependency packages,
  71. # starting with linux-libc-dev:i386
  72. ifeq ($(MICROPY_PY_FFI),1)
  73. ifeq ($(UNAME_S),Linux)
  74. CFLAGS_MOD += -I/usr/include/i686-linux-gnu
  75. endif
  76. endif
  77. endif
  78. ifeq ($(MICROPY_USE_READLINE),1)
  79. INC += -I$(TOP)/lib/mp-readline
  80. CFLAGS_MOD += -DMICROPY_USE_READLINE=1
  81. LIB_SRC_C_EXTRA += mp-readline/readline.c
  82. endif
  83. ifeq ($(MICROPY_PY_TERMIOS),1)
  84. CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1
  85. SRC_MOD += modtermios.c
  86. endif
  87. ifeq ($(MICROPY_PY_SOCKET),1)
  88. CFLAGS_MOD += -DMICROPY_PY_SOCKET=1
  89. SRC_MOD += modusocket.c
  90. endif
  91. ifeq ($(MICROPY_PY_THREAD),1)
  92. CFLAGS_MOD += -DMICROPY_PY_THREAD=1 -DMICROPY_PY_THREAD_GIL=0
  93. LDFLAGS_MOD += -lpthread
  94. endif
  95. ifeq ($(MICROPY_PY_FFI),1)
  96. ifeq ($(MICROPY_STANDALONE),1)
  97. LIBFFI_CFLAGS_MOD := -I$(shell ls -1d $(TOP)/lib/libffi/build_dir/out/lib/libffi-*/include)
  98. ifeq ($(MICROPY_FORCE_32BIT),1)
  99. LIBFFI_LDFLAGS_MOD = $(TOP)/lib/libffi/build_dir/out/lib32/libffi.a
  100. else
  101. LIBFFI_LDFLAGS_MOD = $(TOP)/lib/libffi/build_dir/out/lib/libffi.a
  102. endif
  103. else
  104. LIBFFI_CFLAGS_MOD := $(shell pkg-config --cflags libffi)
  105. LIBFFI_LDFLAGS_MOD := $(shell pkg-config --libs libffi)
  106. endif
  107. ifeq ($(UNAME_S),Linux)
  108. LIBFFI_LDFLAGS_MOD += -ldl
  109. endif
  110. CFLAGS_MOD += $(LIBFFI_CFLAGS_MOD) -DMICROPY_PY_FFI=1
  111. LDFLAGS_MOD += $(LIBFFI_LDFLAGS_MOD)
  112. SRC_MOD += modffi.c
  113. endif
  114. ifeq ($(MICROPY_PY_JNI),1)
  115. # Path for 64-bit OpenJDK, should be adjusted for other JDKs
  116. CFLAGS_MOD += -I/usr/lib/jvm/java-7-openjdk-amd64/include -DMICROPY_PY_JNI=1
  117. SRC_MOD += modjni.c
  118. endif
  119. # source files
  120. SRC_C = \
  121. main.c \
  122. gccollect.c \
  123. unix_mphal.c \
  124. mpthreadport.c \
  125. input.c \
  126. file.c \
  127. modmachine.c \
  128. modos.c \
  129. moduos_vfs.c \
  130. modtime.c \
  131. moduselect.c \
  132. alloc.c \
  133. coverage.c \
  134. fatfs_port.c \
  135. $(SRC_MOD)
  136. LIB_SRC_C = $(addprefix lib/,\
  137. $(LIB_SRC_C_EXTRA) \
  138. timeutils/timeutils.c \
  139. )
  140. # FatFS VFS support
  141. LIB_SRC_C += $(addprefix lib/,\
  142. oofatfs/ff.c \
  143. oofatfs/option/unicode.c \
  144. )
  145. OBJ = $(PY_O)
  146. OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
  147. OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
  148. # List of sources for qstr extraction
  149. SRC_QSTR += $(SRC_C) $(LIB_SRC_C)
  150. # Append any auto-generated sources that are needed by sources listed in
  151. # SRC_QSTR
  152. SRC_QSTR_AUTO_DEPS +=
  153. ifneq ($(FROZEN_MPY_DIR),)
  154. # To use frozen bytecode, put your .py files in a subdirectory (eg frozen/) and
  155. # then invoke make with FROZEN_MPY_DIR=frozen (be sure to build from scratch).
  156. CFLAGS += -DMICROPY_QSTR_EXTRA_POOL=mp_qstr_frozen_const_pool
  157. CFLAGS += -DMICROPY_MODULE_FROZEN_MPY
  158. CFLAGS += -DMPZ_DIG_SIZE=16 # force 16 bits to work on both 32 and 64 bit archs
  159. MPY_CROSS_FLAGS += -mcache-lookup-bc
  160. endif
  161. include $(TOP)/py/mkrules.mk
  162. .PHONY: test
  163. test: $(PROG) $(TOP)/tests/run-tests
  164. $(eval DIRNAME=ports/$(notdir $(CURDIR)))
  165. cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/$(PROG) ./run-tests
  166. # install micropython in /usr/local/bin
  167. TARGET = micropython
  168. PREFIX = $(DESTDIR)/usr/local
  169. BINDIR = $(PREFIX)/bin
  170. install: micropython
  171. install -d $(BINDIR)
  172. install $(TARGET) $(BINDIR)/$(TARGET)
  173. # uninstall micropython
  174. uninstall:
  175. -rm $(BINDIR)/$(TARGET)
  176. # build synthetically fast interpreter for benchmarking
  177. fast:
  178. $(MAKE) COPT="-O2 -DNDEBUG -fno-crossjumping" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_fast.h>"' BUILD=build-fast PROG=micropython_fast
  179. # build a minimal interpreter
  180. minimal:
  181. $(MAKE) COPT="-Os -DNDEBUG" CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_minimal.h>"' \
  182. BUILD=build-minimal PROG=micropython_minimal FROZEN_DIR= FROZEN_MPY_DIR= \
  183. MICROPY_PY_BTREE=0 MICROPY_PY_FFI=0 MICROPY_PY_SOCKET=0 MICROPY_PY_THREAD=0 \
  184. MICROPY_PY_TERMIOS=0 MICROPY_PY_USSL=0 \
  185. MICROPY_USE_READLINE=0
  186. # build interpreter with nan-boxing as object model
  187. nanbox:
  188. $(MAKE) \
  189. CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_nanbox.h>"' \
  190. BUILD=build-nanbox \
  191. PROG=micropython_nanbox \
  192. MICROPY_FORCE_32BIT=1 \
  193. MICROPY_PY_USSL=0
  194. freedos:
  195. $(MAKE) \
  196. CC=i586-pc-msdosdjgpp-gcc \
  197. STRIP=i586-pc-msdosdjgpp-strip \
  198. SIZE=i586-pc-msdosdjgpp-size \
  199. CFLAGS_EXTRA='-DMP_CONFIGFILE="<mpconfigport_freedos.h>" -DMICROPY_NLR_SETJMP -Dtgamma=gamma -DMICROPY_EMIT_X86=0 -DMICROPY_NO_ALLOCA=1 -DMICROPY_PY_USELECT_POSIX=0' \
  200. BUILD=build-freedos \
  201. PROG=micropython_freedos \
  202. MICROPY_PY_SOCKET=0 \
  203. MICROPY_PY_FFI=0 \
  204. MICROPY_PY_JNI=0 \
  205. MICROPY_PY_BTREE=0 \
  206. MICROPY_PY_THREAD=0 \
  207. MICROPY_PY_USSL=0
  208. # build an interpreter for coverage testing and do the testing
  209. coverage:
  210. $(MAKE) \
  211. COPT="-O0" CFLAGS_EXTRA='$(CFLAGS_EXTRA) -DMP_CONFIGFILE="<mpconfigport_coverage.h>" \
  212. -fprofile-arcs -ftest-coverage \
  213. -Wdouble-promotion -Wformat -Wmissing-declarations -Wmissing-prototypes -Wsign-compare \
  214. -Wold-style-definition -Wpointer-arith -Wshadow -Wuninitialized -Wunused-parameter \
  215. -DMICROPY_UNIX_COVERAGE' \
  216. LDFLAGS_EXTRA='-fprofile-arcs -ftest-coverage' \
  217. FROZEN_DIR=coverage-frzstr FROZEN_MPY_DIR=coverage-frzmpy \
  218. BUILD=build-coverage PROG=micropython_coverage
  219. coverage_test: coverage
  220. $(eval DIRNAME=ports/$(notdir $(CURDIR)))
  221. cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests
  222. cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests -d thread
  223. cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests --emit native
  224. cd $(TOP)/tests && MICROPY_MICROPYTHON=../$(DIRNAME)/micropython_coverage ./run-tests --via-mpy -d basics float
  225. cat $(TOP)/tests/basics/0prelim.py | ./micropython_coverage | grep -q 'abc'
  226. gcov -o build-coverage/py $(TOP)/py/*.c
  227. gcov -o build-coverage/extmod $(TOP)/extmod/*.c
  228. # Value of configure's --host= option (required for cross-compilation).
  229. # Deduce it from CROSS_COMPILE by default, but can be overridden.
  230. ifneq ($(CROSS_COMPILE),)
  231. CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE))
  232. else
  233. CROSS_COMPILE_HOST =
  234. endif
  235. deplibs: libffi axtls
  236. # install-exec-recursive & install-data-am targets are used to avoid building
  237. # docs and depending on makeinfo
  238. libffi:
  239. cd $(TOP)/lib/libffi; git clean -d -x -f
  240. cd $(TOP)/lib/libffi; ./autogen.sh
  241. mkdir -p $(TOP)/lib/libffi/build_dir; cd $(TOP)/lib/libffi/build_dir; \
  242. ../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"; \
  243. $(MAKE) install-exec-recursive; $(MAKE) -C include install-data-am
  244. axtls: $(BUILD)/libaxtls.a
  245. $(BUILD)/libaxtls.a: $(TOP)/lib/axtls/README | $(OBJ_DIRS)
  246. cd $(TOP)/lib/axtls; cp config/upyconfig config/.config
  247. cd $(TOP)/lib/axtls; $(MAKE) oldconfig -B
  248. cd $(TOP)/lib/axtls; $(MAKE) clean
  249. cd $(TOP)/lib/axtls; $(MAKE) all CC="$(CC)" LD="$(LD)"
  250. cp $(TOP)/lib/axtls/_stage/libaxtls.a $@
  251. $(TOP)/lib/axtls/README:
  252. @echo "You cloned without --recursive, fetching submodules for you."
  253. (cd $(TOP); git submodule update --init --recursive)