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