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