Makefile 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. # --------------------------------------------------------------------------
  2. #
  3. # Tiny C Compiler Makefile
  4. #
  5. ifndef TOP
  6. TOP = .
  7. INCLUDED = no
  8. endif
  9. include $(TOP)/config.mak
  10. ifeq (-$(CC)-$(GCC_MAJOR)-$(findstring $(GCC_MINOR),56789)-,-gcc-4--)
  11. CFLAGS += -D_FORTIFY_SOURCE=0
  12. endif
  13. LIBTCC = libtcc.a
  14. LIBTCC1 = libtcc1.a
  15. LINK_LIBTCC =
  16. LIBS =
  17. CFLAGS += -I$(TOP)
  18. CFLAGS += $(CPPFLAGS)
  19. VPATH = $(TOPSRC)
  20. ifdef CONFIG_WIN32
  21. ifneq ($(CONFIG_static),yes)
  22. LIBTCC = libtcc$(DLLSUF)
  23. LIBTCCDEF = libtcc.def
  24. endif
  25. CFGWIN = -win
  26. NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32)
  27. else
  28. LIBS=-lm
  29. ifneq ($(CONFIG_ldl),no)
  30. LIBS+=-ldl
  31. endif
  32. # make libtcc as static or dynamic library?
  33. ifeq ($(CONFIG_static),no)
  34. LIBTCC=libtcc$(DLLSUF)
  35. export LD_LIBRARY_PATH := $(CURDIR)/$(TOP)
  36. ifneq ($(CONFIG_rpath),no)
  37. LINK_LIBTCC += -Wl,-rpath,"$(libdir)"
  38. endif
  39. endif
  40. CFGWIN =-unx
  41. NATIVE_TARGET = $(ARCH)
  42. ifdef CONFIG_OSX
  43. NATIVE_TARGET = $(ARCH)-osx
  44. LDFLAGS += -flat_namespace -undefined warning
  45. export MACOSX_DEPLOYMENT_TARGET := 10.2
  46. endif
  47. endif
  48. # run local version of tcc with local libraries and includes
  49. TCCFLAGS-unx = -B$(TOP) -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP)
  50. TCCFLAGS-win = -B$(TOPSRC)/win32 -I$(TOPSRC)/include -I$(TOPSRC) -I$(TOP) -L$(TOP)
  51. TCCFLAGS = $(TCCFLAGS$(CFGWIN))
  52. TCC = $(TOP)/tcc$(EXESUF) $(TCCFLAGS)
  53. ifdef CONFIG_OSX
  54. TCCFLAGS += -D_ANSI_SOURCE
  55. endif
  56. CFLAGS_P = $(CFLAGS) -pg -static -DCONFIG_TCC_STATIC -DTCC_PROFILE
  57. LIBS_P = $(LIBS)
  58. LDFLAGS_P = $(LDFLAGS)
  59. CONFIG_$(ARCH) = yes
  60. NATIVE_DEFINES_$(CONFIG_i386) += -DTCC_TARGET_I386
  61. NATIVE_DEFINES_$(CONFIG_x86_64) += -DTCC_TARGET_X86_64
  62. NATIVE_DEFINES_$(CONFIG_WIN32) += -DTCC_TARGET_PE
  63. NATIVE_DEFINES_$(CONFIG_OSX) += -DTCC_TARGET_MACHO
  64. NATIVE_DEFINES_$(CONFIG_uClibc) += -DTCC_UCLIBC
  65. NATIVE_DEFINES_$(CONFIG_musl) += -DTCC_MUSL
  66. NATIVE_DEFINES_$(CONFIG_libgcc) += -DCONFIG_USE_LIBGCC
  67. NATIVE_DEFINES_$(CONFIG_selinux) += -DHAVE_SELINUX
  68. NATIVE_DEFINES_$(CONFIG_arm) += -DTCC_TARGET_ARM
  69. NATIVE_DEFINES_$(CONFIG_arm_eabihf) += -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
  70. NATIVE_DEFINES_$(CONFIG_arm_eabi) += -DTCC_ARM_EABI
  71. NATIVE_DEFINES_$(CONFIG_arm_vfp) += -DTCC_ARM_VFP
  72. NATIVE_DEFINES_$(CONFIG_arm64) += -DTCC_TARGET_ARM64
  73. NATIVE_DEFINES += $(NATIVE_DEFINES_yes)
  74. ifeq ($(INCLUDED),no)
  75. # --------------------------------------------------------------------------
  76. # running top Makefile
  77. PROGS = tcc$(EXESUF)
  78. TCCLIBS = $(LIBTCC1) $(LIBTCC) $(LIBTCCDEF)
  79. TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info
  80. all: $(PROGS) $(TCCLIBS) $(TCCDOCS)
  81. # cross compiler targets to build
  82. TCC_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince c67
  83. # TCC_X += arm-fpa arm-fpa-ld arm-vfp arm-eabi
  84. # cross libtcc1.a targets to build
  85. LIBTCC1_X = i386 x86_64 i386-win32 x86_64-win32 x86_64-osx arm arm64 arm-wince
  86. PROGS_CROSS = $(foreach X,$(TCC_X),$X-tcc$(EXESUF))
  87. LIBTCC1_CROSS = $(foreach X,$(LIBTCC1_X),$X-libtcc1.a)
  88. # build cross compilers & libs
  89. cross: $(LIBTCC1_CROSS) $(PROGS_CROSS)
  90. # build specific cross compiler & lib
  91. cross-%: %-tcc$(EXESUF) %-libtcc1.a ;
  92. install: ; @$(MAKE) --no-print-directory install$(CFGWIN)
  93. install-strip: ; @$(MAKE) --no-print-directory install$(CFGWIN) CONFIG_strip=yes
  94. uninstall: ; @$(MAKE) --no-print-directory uninstall$(CFGWIN)
  95. ifdef CONFIG_cross
  96. all : cross
  97. endif
  98. # --------------------------------------------
  99. T = $(or $(CROSS_TARGET),$(NATIVE_TARGET),unknown)
  100. X = $(if $(CROSS_TARGET),$(CROSS_TARGET)-)
  101. DEF-i386 = -DTCC_TARGET_I386
  102. DEF-x86_64 = -DTCC_TARGET_X86_64
  103. DEF-i386-win32 = -DTCC_TARGET_PE -DTCC_TARGET_I386
  104. DEF-x86_64-win32= -DTCC_TARGET_PE -DTCC_TARGET_X86_64
  105. DEF-x86_64-osx = -DTCC_TARGET_MACHO -DTCC_TARGET_X86_64
  106. DEF-arm-wince = -DTCC_TARGET_PE -DTCC_TARGET_ARM -DTCC_ARM_EABI -DTCC_ARM_VFP -DTCC_ARM_HARDFLOAT
  107. DEF-arm64 = -DTCC_TARGET_ARM64
  108. DEF-c67 = -DTCC_TARGET_C67 -w # disable warnigs
  109. DEF-arm-fpa = -DTCC_TARGET_ARM
  110. DEF-arm-fpa-ld = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12
  111. DEF-arm-vfp = -DTCC_TARGET_ARM -DTCC_ARM_VFP
  112. DEF-arm-eabi = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI
  113. DEF-arm-eabihf = -DTCC_TARGET_ARM -DTCC_ARM_VFP -DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
  114. DEF-arm = $(DEF-arm-eabihf)
  115. DEF-$(NATIVE_TARGET) = $(NATIVE_DEFINES)
  116. DEFINES += $(DEF-$T) $(DEF-all)
  117. DEFINES += $(if $(ROOT-$T),-DCONFIG_SYSROOT="\"$(ROOT-$T)\"")
  118. DEFINES += $(if $(CRT-$T),-DCONFIG_TCC_CRTPREFIX="\"$(CRT-$T)\"")
  119. DEFINES += $(if $(LIB-$T),-DCONFIG_TCC_LIBPATHS="\"$(LIB-$T)\"")
  120. DEFINES += $(if $(INC-$T),-DCONFIG_TCC_SYSINCLUDEPATHS="\"$(INC-$T)\"")
  121. DEFINES += $(DEF-$(or $(findstring win,$T),unx))
  122. ifneq ($(X),)
  123. ifeq ($(CONFIG_WIN32),yes)
  124. DEF-win += -DTCC_LIBTCC1="\"$(X)libtcc1.a\""
  125. DEF-unx += -DTCC_LIBTCC1="\"lib/$(X)libtcc1.a\""
  126. else
  127. DEF-all += -DTCC_LIBTCC1="\"$(X)libtcc1.a\""
  128. DEF-win += -DCONFIG_TCCDIR="\"$(tccdir)/win32\""
  129. endif
  130. endif
  131. # include custom configuration (see make help)
  132. -include config-extra.mak
  133. CORE_FILES = tcc.c tcctools.c libtcc.c tccpp.c tccgen.c tccelf.c tccasm.c tccrun.c
  134. CORE_FILES += tcc.h config.h libtcc.h tcctok.h
  135. i386_FILES = $(CORE_FILES) i386-gen.c i386-link.c i386-asm.c i386-asm.h i386-tok.h
  136. i386-win32_FILES = $(i386_FILES) tccpe.c
  137. x86_64_FILES = $(CORE_FILES) x86_64-gen.c x86_64-link.c i386-asm.c x86_64-asm.h
  138. x86_64-win32_FILES = $(x86_64_FILES) tccpe.c
  139. x86_64-osx_FILES = $(x86_64_FILES)
  140. arm_FILES = $(CORE_FILES) arm-gen.c arm-link.c arm-asm.c
  141. arm-wince_FILES = $(arm_FILES) tccpe.c
  142. arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c
  143. c67_FILES = $(CORE_FILES) c67-gen.c c67-link.c tcccoff.c
  144. # libtcc sources
  145. LIBTCC_SRC = $(filter-out tcc.c tcctools.c,$(filter %.c,$($T_FILES)))
  146. ifeq ($(ONE_SOURCE),yes)
  147. LIBTCC_OBJ = $(X)libtcc.o
  148. LIBTCC_INC = $($T_FILES)
  149. TCC_FILES = $(X)tcc.o
  150. tcc.o : DEFINES += -DONE_SOURCE=0
  151. else
  152. LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC))
  153. LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES))
  154. TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ)
  155. $(TCC_FILES) : DEFINES += -DONE_SOURCE=0
  156. endif
  157. # target specific object rule
  158. $(X)%.o : %.c $(LIBTCC_INC)
  159. $(CC) -o $@ -c $< $(DEFINES) $(CFLAGS)
  160. # additional dependencies
  161. $(X)tcc.o : tcctools.c
  162. # Host Tiny C Compiler
  163. tcc$(EXESUF): tcc.o $(LIBTCC)
  164. $(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC)
  165. # Cross Tiny C Compilers
  166. %-tcc$(EXESUF): FORCE
  167. @$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or $(ONE_SOURCE),yes)
  168. $(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES)
  169. $(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
  170. # profiling version
  171. tcc_p$(EXESUF): $($T_FILES)
  172. $(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P)
  173. # static libtcc library
  174. libtcc.a: $(LIBTCC_OBJ)
  175. $(AR) rcs $@ $^
  176. # dynamic libtcc library
  177. libtcc.so: $(LIBTCC_OBJ)
  178. $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS)
  179. libtcc.so: CFLAGS+=-fPIC
  180. libtcc.so: LDFLAGS+=-fPIC
  181. # windows dynamic libtcc library
  182. libtcc.dll : $(LIBTCC_OBJ)
  183. $(CC) -shared -o $@ $^ $(LDFLAGS)
  184. libtcc.dll : DEFINES += -DLIBTCC_AS_DLL
  185. # import file for windows libtcc.dll
  186. libtcc.def : libtcc.dll tcc$(EXESUF)
  187. $(XTCC) -impdef $< -o $@
  188. XTCC ?= ./tcc$(EXESUF)
  189. # TinyCC runtime libraries
  190. libtcc1.a : tcc$(EXESUF) FORCE
  191. @$(MAKE) -C lib DEFINES='$(DEF-$T)'
  192. # Cross libtcc1.a
  193. %-libtcc1.a : %-tcc$(EXESUF) FORCE
  194. @$(MAKE) -C lib DEFINES='$(DEF-$*)' CROSS_TARGET=$*
  195. .PRECIOUS: %-libtcc1.a
  196. FORCE:
  197. # --------------------------------------------------------------------------
  198. # documentation and man page
  199. tcc-doc.html: tcc-doc.texi
  200. makeinfo --no-split --html --number-sections -o $@ $< || true
  201. tcc.1: tcc-doc.texi
  202. $(TOPSRC)/texi2pod.pl $< tcc.pod \
  203. && pod2man --section=1 --center="Tiny C Compiler" --release="$(VERSION)" tcc.pod >tmp.1 \
  204. && mv tmp.1 $@ || rm -f tmp.1
  205. tcc-doc.info: tcc-doc.texi
  206. makeinfo $< || true
  207. # --------------------------------------------------------------------------
  208. # install
  209. INSTALL = install -m644
  210. INSTALLBIN = install -m755 $(STRIP_$(CONFIG_strip))
  211. STRIP_yes = -s
  212. LIBTCC1_W = $(filter %-win32-libtcc1.a %-wince-libtcc1.a,$(LIBTCC1_CROSS))
  213. LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(LIBTCC1_CROSS))
  214. IB = $(if $1,mkdir -p $2 && $(INSTALLBIN) $1 $2)
  215. IBw = $(call IB,$(wildcard $1),$2)
  216. IF = $(if $1,mkdir -p $2 && $(INSTALL) $1 $2)
  217. IFw = $(call IF,$(wildcard $1),$2)
  218. IR = mkdir -p $2 && cp -r $1/. $2
  219. # install progs & libs
  220. install-unx:
  221. $(call IBw,$(PROGS) $(PROGS_CROSS),"$(bindir)")
  222. $(call IFw,$(LIBTCC1) $(LIBTCC1_U),"$(tccdir)")
  223. $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
  224. $(call $(if $(findstring .so,$(LIBTCC)),IBw,IFw),$(LIBTCC),"$(libdir)")
  225. $(call IF,$(TOPSRC)/libtcc.h,"$(includedir)")
  226. $(call IFw,tcc.1,"$(mandir)/man1")
  227. $(call IFw,tcc-doc.info,"$(infodir)")
  228. $(call IFw,tcc-doc.html,"$(docdir)")
  229. ifneq "$(wildcard $(LIBTCC1_W))" ""
  230. $(call IFw,$(TOPSRC)/win32/lib/*.def $(LIBTCC1_W),"$(tccdir)/win32/lib")
  231. $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/win32/include")
  232. $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/win32/include")
  233. endif
  234. # uninstall
  235. uninstall-unx:
  236. @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS),"$(bindir)/$P")
  237. @rm -fv "$(libdir)/libtcc.a" "$(libdir)/libtcc.so" "$(includedir)/libtcc.h"
  238. @rm -fv "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info"
  239. @rm -fv "$(docdir)/tcc-doc.html"
  240. rm -r "$(tccdir)"
  241. # install progs & libs on windows
  242. install-win:
  243. $(call IBw,$(PROGS) $(PROGS_CROSS) $(subst libtcc.a,,$(LIBTCC)),"$(bindir)")
  244. $(call IF,$(TOPSRC)/win32/lib/*.def,"$(tccdir)/lib")
  245. $(call IFw,libtcc1.a $(LIBTCC1_W),"$(tccdir)/lib")
  246. $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
  247. $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/include")
  248. $(call IR,$(TOPSRC)/win32/examples,"$(tccdir)/examples")
  249. $(call IF,$(TOPSRC)/tests/libtcc_test.c,"$(tccdir)/examples")
  250. $(call IFw,$(TOPSRC)/libtcc.h $(subst .dll,.def,$(LIBTCC)),"$(libdir)")
  251. $(call IFw,$(TOPSRC)/win32/tcc-win32.txt tcc-doc.html,"$(docdir)")
  252. ifneq "$(wildcard $(LIBTCC1_U))" ""
  253. $(call IFw,$(LIBTCC1_U),"$(tccdir)/lib")
  254. $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/lib/include")
  255. endif
  256. # the msys-git shell works to configure && make except it does not have install
  257. ifeq "$(and $(CONFIG_WIN32),$(shell which install >/dev/null 2>&1 || echo no))" "no"
  258. install-win : INSTALL = cp
  259. install-win : INSTALLBIN = cp
  260. endif
  261. # uninstall on windows
  262. uninstall-win:
  263. @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS) libtcc.dll,"$(bindir)/$P")
  264. @rm -fv $(foreach F,tcc-doc.html tcc-win32.txt,"$(docdir)/$F")
  265. @rm -fv $(foreach F,libtcc.h libtcc.def libtcc.a,"$(libdir)/$F")
  266. rm -r "$(tccdir)"
  267. # --------------------------------------------------------------------------
  268. # other stuff
  269. TAGFILES = *.[ch] include/*.h lib/*.[chS]
  270. tags : ; ctags $(TAGFILES)
  271. # cannot have both tags and TAGS on windows
  272. ETAGS : ; etags $(TAGFILES)
  273. # create release tarball from *current* git branch (including tcc-doc.html
  274. # and converting two files to CRLF)
  275. TCC-VERSION = tcc-$(VERSION)
  276. tar: tcc-doc.html
  277. mkdir $(TCC-VERSION)
  278. ( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f )
  279. cp tcc-doc.html $(TCC-VERSION)
  280. for f in tcc-win32.txt build-tcc.bat ; do \
  281. cat win32/$$f | sed 's,\(.*\),\1\r,g' > $(TCC-VERSION)/win32/$$f ; \
  282. done
  283. tar cjf $(TCC-VERSION).tar.bz2 $(TCC-VERSION)
  284. rm -rf $(TCC-VERSION)
  285. git reset
  286. config.mak:
  287. $(if $(wildcard $@),,@echo "Please run ./configure." && exit 1)
  288. # run all tests
  289. test:
  290. $(MAKE) -C tests
  291. # run test(s) from tests2 subdir (see make help)
  292. tests2.%:
  293. $(MAKE) -C tests/tests2 $@
  294. testspp.%:
  295. $(MAKE) -C tests/pp $@
  296. clean:
  297. rm -f tcc$(EXESUF) tcc_p$(EXESUF) *-tcc$(EXESUF) tcc.pod
  298. rm -f *~ *.o *.a *.so* *.out *.log lib*.def *.exe *.dll a.out tags TAGS
  299. @$(MAKE) -C lib $@
  300. @$(MAKE) -C tests $@
  301. distclean: clean
  302. rm -f config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html
  303. .PHONY: all clean test tar tags ETAGS distclean install uninstall FORCE
  304. help:
  305. @echo "make"
  306. @echo " build native compiler (from separate objects)"
  307. @echo ""
  308. @echo "make cross"
  309. @echo " build cross compilers (from one source)"
  310. @echo ""
  311. @echo "make ONE_SOURCE=yes / no"
  312. @echo " force building from one source / separate objects"
  313. @echo ""
  314. @echo "make cross-TARGET"
  315. @echo " build one specific cross compiler for 'TARGET', as in"
  316. @echo " $(TCC_X)"
  317. @echo ""
  318. @echo "Custom configuration:"
  319. @echo " The makefile includes a file 'config-extra.mak' if it is present."
  320. @echo " This file may contain some custom configuration. For example:"
  321. @echo ""
  322. @echo " NATIVE_DEFINES += -D..."
  323. @echo ""
  324. @echo " Or for example to configure the search paths for a cross-compiler"
  325. @echo " that expects the linux files in <tccdir>/i386-linux:"
  326. @echo ""
  327. @echo " ROOT-i386 = {B}/i386-linux"
  328. @echo " CRT-i386 = {B}/i386-linux/usr/lib"
  329. @echo " LIB-i386 = {B}/i386-linux/lib:{B}/i386-linux/usr/lib"
  330. @echo " INC-i386 = {B}/lib/include:{B}/i386-linux/usr/include"
  331. @echo " DEF-i386 += -D__linux__"
  332. @echo ""
  333. @echo "make test"
  334. @echo " run all tests"
  335. @echo ""
  336. @echo "make tests2.all / make tests2.37 / make tests2.37+"
  337. @echo " run all/single test(s) from tests2, optionally update .expect"
  338. @echo "make testspp.all / make testspp.17"
  339. @echo " run all/single test(s) from tests/pp"
  340. @echo ""
  341. @echo "Other supported make targets:"
  342. @echo " install install-strip tags ETAGS tar clean distclean help"
  343. @echo ""
  344. # --------------------------------------------------------------------------
  345. endif # ($(INCLUDED),no)