Makefile.upylib 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. MPTOP = ../..
  2. -include mpconfigport.mk
  3. include $(MPTOP)/py/mkenv.mk
  4. all: lib
  5. # OS name, for simple autoconfig
  6. UNAME_S := $(shell uname -s)
  7. # include py core make definitions
  8. include $(MPTOP)/py/py.mk
  9. INC += -I.
  10. INC += -I..
  11. INC += -I$(MPTOP)
  12. INC += -I$(MPTOP)/ports/unix
  13. INC += -I$(BUILD)
  14. # compiler settings
  15. CWARN = -Wall -Werror
  16. CWARN += -Wpointer-arith -Wuninitialized
  17. CFLAGS = $(INC) $(CWARN) -std=gnu99 -DUNIX $(CFLAGS_MOD) $(COPT) $(CFLAGS_EXTRA)
  18. # Debugging/Optimization
  19. ifdef DEBUG
  20. CFLAGS += -g
  21. COPT = -O0
  22. else
  23. COPT = -Os #-DNDEBUG
  24. # _FORTIFY_SOURCE is a feature in gcc/glibc which is intended to provide extra
  25. # security for detecting buffer overflows. Some distros (Ubuntu at the very least)
  26. # have it enabled by default.
  27. #
  28. # gcc already optimizes some printf calls to call puts and/or putchar. When
  29. # _FORTIFY_SOURCE is enabled and compiling with -O1 or greater, then some
  30. # printf calls will also be optimized to call __printf_chk (in glibc). Any
  31. # printfs which get redirected to __printf_chk are then no longer synchronized
  32. # with printfs that go through mp_printf.
  33. #
  34. # In MicroPython, we don't want to use the runtime library's printf but rather
  35. # go through mp_printf, so that stdout is properly tied into streams, etc.
  36. # This means that we either need to turn off _FORTIFY_SOURCE or provide our
  37. # own implementation of __printf_chk. We've chosen to turn off _FORTIFY_SOURCE.
  38. # It should also be noted that the use of printf in MicroPython is typically
  39. # quite limited anyways (primarily for debug and some error reporting, etc
  40. # in the unix version).
  41. #
  42. # Information about _FORTIFY_SOURCE seems to be rather scarce. The best I could
  43. # find was this: https://securityblog.redhat.com/2014/03/26/fortify-and-you/
  44. # Original patchset was introduced by
  45. # https://gcc.gnu.org/ml/gcc-patches/2004-09/msg02055.html .
  46. #
  47. # Turning off _FORTIFY_SOURCE is only required when compiling with -O1 or greater
  48. CFLAGS += -U _FORTIFY_SOURCE
  49. endif
  50. # On OSX, 'gcc' is a symlink to clang unless a real gcc is installed.
  51. # The unix port of MicroPython on OSX must be compiled with clang,
  52. # while cross-compile ports require gcc, so we test here for OSX and
  53. # if necessary override the value of 'CC' set in py/mkenv.mk
  54. ifeq ($(UNAME_S),Darwin)
  55. CC = clang
  56. # Use clang syntax for map file
  57. LDFLAGS_ARCH = -Wl,-map,$@.map
  58. else
  59. # Use gcc syntax for map file
  60. LDFLAGS_ARCH = -Wl,-Map=$@.map,--cref
  61. endif
  62. LDFLAGS = $(LDFLAGS_MOD) $(LDFLAGS_ARCH) -lm $(LDFLAGS_EXTRA)
  63. ifeq ($(MICROPY_FORCE_32BIT),1)
  64. # Note: you may need to install i386 versions of dependency packages,
  65. # starting with linux-libc-dev:i386
  66. ifeq ($(MICROPY_PY_FFI),1)
  67. ifeq ($(UNAME_S),Linux)
  68. CFLAGS_MOD += -I/usr/include/i686-linux-gnu
  69. endif
  70. endif
  71. endif
  72. ifeq ($(MICROPY_USE_READLINE),1)
  73. INC += -I$(MPTOP)/lib/mp-readline
  74. CFLAGS_MOD += -DMICROPY_USE_READLINE=1
  75. LIB_SRC_C_EXTRA += mp-readline/readline.c
  76. endif
  77. ifeq ($(MICROPY_USE_READLINE),2)
  78. CFLAGS_MOD += -DMICROPY_USE_READLINE=2
  79. LDFLAGS_MOD += -lreadline
  80. # the following is needed for BSD
  81. #LDFLAGS_MOD += -ltermcap
  82. endif
  83. ifeq ($(MICROPY_PY_TIME),1)
  84. CFLAGS_MOD += -DMICROPY_PY_TIME=1
  85. SRC_MOD += modtime.c
  86. endif
  87. ifeq ($(MICROPY_PY_TERMIOS),1)
  88. CFLAGS_MOD += -DMICROPY_PY_TERMIOS=1
  89. SRC_MOD += modtermios.c
  90. endif
  91. ifeq ($(MICROPY_PY_SOCKET),1)
  92. CFLAGS_MOD += -DMICROPY_PY_SOCKET=1
  93. SRC_MOD += modsocket.c
  94. endif
  95. ifeq ($(MICROPY_PY_FFI),1)
  96. ifeq ($(MICROPY_STANDALONE),1)
  97. LIBFFI_CFLAGS_MOD := -I$(shell ls -1d $(MPTOP)/lib/libffi/build_dir/out/lib/libffi-*/include)
  98. ifeq ($(MICROPY_FORCE_32BIT),1)
  99. LIBFFI_LDFLAGS_MOD = $(MPTOP)/lib/libffi/build_dir/out/lib32/libffi.a
  100. else
  101. LIBFFI_LDFLAGS_MOD = $(MPTOP)/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. MAIN_C = main.c
  115. # source files
  116. SRC_C = $(addprefix ports/unix/,\
  117. $(MAIN_C) \
  118. gccollect.c \
  119. unix_mphal.c \
  120. input.c \
  121. file.c \
  122. modmachine.c \
  123. modos.c \
  124. moduselect.c \
  125. alloc.c \
  126. coverage.c \
  127. fatfs_port.c \
  128. $(SRC_MOD) \
  129. )
  130. LIB_SRC_C = $(addprefix lib/,\
  131. $(LIB_SRC_C_EXTRA) \
  132. utils/printf.c \
  133. timeutils/timeutils.c \
  134. )
  135. ifeq ($(MICROPY_FATFS),1)
  136. LIB_SRC_C += $(addprefix lib/,\
  137. fatfs/ff.c \
  138. fatfs/option/ccsbcs.c \
  139. )
  140. endif
  141. OBJ = $(PY_O)
  142. OBJ += $(addprefix $(BUILD)/, $(SRC_C:.c=.o))
  143. OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
  144. # List of sources for qstr extraction
  145. SRC_QSTR += $(SRC_C) $(LIB_SRC_C)
  146. # Append any auto-generated sources that are needed by sources listed in
  147. # SRC_QSTR
  148. SRC_QSTR_AUTO_DEPS +=
  149. include $(MPTOP)/py/mkrules.mk
  150. # Value of configure's --host= option (required for cross-compilation).
  151. # Deduce it from CROSS_COMPILE by default, but can be overridden.
  152. ifneq ($(CROSS_COMPILE),)
  153. CROSS_COMPILE_HOST = --host=$(patsubst %-,%,$(CROSS_COMPILE))
  154. else
  155. CROSS_COMPILE_HOST =
  156. endif
  157. deplibs: libffi axtls
  158. # install-exec-recursive & install-data-am targets are used to avoid building
  159. # docs and depending on makeinfo
  160. libffi:
  161. cd $(MPTOP)/lib/libffi; git clean -d -x -f
  162. cd $(MPTOP)/lib/libffi; ./autogen.sh
  163. mkdir -p $(MPTOP)/lib/libffi/build_dir; cd $(MPTOP)/lib/libffi/build_dir; \
  164. ../configure $(CROSS_COMPILE_HOST) --prefix=$$PWD/out CC="$(CC)" CXX="$(CXX)" LD="$(LD)"; \
  165. make install-exec-recursive; make -C include install-data-am
  166. axtls: $(MPTOP)/lib/axtls/README
  167. cd $(MPTOP)/lib/axtls; cp config/upyconfig config/.config
  168. cd $(MPTOP)/lib/axtls; make oldconfig -B
  169. cd $(MPTOP)/lib/axtls; make clean
  170. cd $(MPTOP)/lib/axtls; make all CC="$(CC)" LD="$(LD)"
  171. $(MPTOP)/lib/axtls/README:
  172. @echo "You cloned without --recursive, fetching submodules for you."
  173. (cd $(MPTOP); git submodule update --init --recursive)