Adjust testcase for PIE compilers
[tinycc.git] / Makefile
blob88881499dc0894996c411c3607a92942d756f7f0
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 arm64_FILES = $(CORE_FILES) arm64-gen.c arm64-link.c
168 c67_FILES = $(CORE_FILES) c67-gen.c c67-link.c tcccoff.c
170 # libtcc sources
171 LIBTCC_SRC = $(filter-out tcc.c tcctools.c,$(filter %.c,$($T_FILES)))
173 ifeq ($(ONE_SOURCE),yes)
174 LIBTCC_OBJ = $(X)libtcc.o
175 LIBTCC_INC = $($T_FILES)
176 TCC_FILES = $(X)tcc.o
177 tcc.o : DEFINES += -DONE_SOURCE=0
178 else
179 LIBTCC_OBJ = $(patsubst %.c,$(X)%.o,$(LIBTCC_SRC))
180 LIBTCC_INC = $(filter %.h %-gen.c %-link.c,$($T_FILES))
181 TCC_FILES = $(X)tcc.o $(LIBTCC_OBJ)
182 $(TCC_FILES) : DEFINES += -DONE_SOURCE=0
183 endif
185 # target specific object rule
186 $(X)%.o : %.c $(LIBTCC_INC)
187 $(CC) -o $@ -c $< $(DEFINES) $(CFLAGS)
189 # additional dependencies
190 $(X)tcc.o : tcctools.c
192 # Host Tiny C Compiler
193 tcc$(EXESUF): tcc.o $(LIBTCC)
194 $(CC) -o $@ $^ $(LIBS) $(LDFLAGS) $(LINK_LIBTCC)
196 # Cross Tiny C Compilers
197 %-tcc$(EXESUF): FORCE
198 @$(MAKE) --no-print-directory $@ CROSS_TARGET=$* ONE_SOURCE=$(or $(ONE_SOURCE),yes)
200 $(CROSS_TARGET)-tcc$(EXESUF): $(TCC_FILES)
201 $(CC) -o $@ $^ $(LIBS) $(LDFLAGS)
203 # profiling version
204 tcc_p$(EXESUF): $($T_FILES)
205 $(CC) -o $@ $< $(DEFINES) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P)
207 # static libtcc library
208 libtcc.a: $(LIBTCC_OBJ)
209 $(AR) rcs $@ $^
211 # dynamic libtcc library
212 libtcc.so: $(LIBTCC_OBJ)
213 $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS)
215 libtcc.so: CFLAGS+=-fPIC
216 libtcc.so: LDFLAGS+=-fPIC
218 # windows dynamic libtcc library
219 libtcc.dll : $(LIBTCC_OBJ)
220 $(CC) -shared -o $@ $^ $(LDFLAGS)
221 libtcc.dll : DEFINES += -DLIBTCC_AS_DLL
223 # import file for windows libtcc.dll
224 libtcc.def : libtcc.dll tcc$(EXESUF)
225 $(XTCC) -impdef $< -o $@
226 XTCC ?= ./tcc$(EXESUF)
228 # TinyCC runtime libraries
229 libtcc1.a : tcc$(EXESUF) FORCE
230 @$(MAKE) -C lib DEFINES='$(DEF-$T)'
232 # Cross libtcc1.a
233 %-libtcc1.a : %-tcc$(EXESUF) FORCE
234 @$(MAKE) -C lib DEFINES='$(DEF-$*)' CROSS_TARGET=$*
236 .PRECIOUS: %-libtcc1.a
237 FORCE:
239 # --------------------------------------------------------------------------
240 # documentation and man page
241 tcc-doc.html: tcc-doc.texi
242 -makeinfo --no-split --html --number-sections -o $@ $<
244 tcc.1: tcc-doc.texi
245 -$(TOPSRC)/texi2pod.pl $< tcc.pod
246 -pod2man --section=1 --center="Tiny C Compiler" --release="$(VERSION)" tcc.pod > $@
248 tcc-doc.info: tcc-doc.texi
249 -makeinfo $<
251 # --------------------------------------------------------------------------
252 # install
254 INSTALL = install -m644
255 INSTALLBIN = install -m755 $(STRIP_$(CONFIG_strip))
256 STRIP_yes = -s
258 LIBTCC1_W = $(filter %-win32-libtcc1.a %-wince-libtcc1.a,$(LIBTCC1_CROSS))
259 LIBTCC1_U = $(filter-out $(LIBTCC1_W),$(LIBTCC1_CROSS))
260 IB = $(if $1,mkdir -p $2 && $(INSTALLBIN) $1 $2)
261 IBw = $(call IB,$(wildcard $1),$2)
262 IF = $(if $1,mkdir -p $2 && $(INSTALL) $1 $2)
263 IFw = $(call IF,$(wildcard $1),$2)
264 IR = mkdir -p $2 && cp -r $1/. $2
266 # install progs & libs
267 install-unx:
268 $(call IBw,$(PROGS) $(PROGS_CROSS),"$(bindir)")
269 $(call IFw,$(LIBTCC1) $(LIBTCC1_U),"$(tccdir)")
270 $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
271 $(call $(if $(findstring .so,$(LIBTCC)),IBw,IFw),$(LIBTCC),"$(libdir)")
272 $(call IF,$(TOPSRC)/libtcc.h,"$(includedir)")
273 $(call IFw,tcc.1,"$(mandir)/man1")
274 $(call IFw,tcc-doc.info,"$(infodir)")
275 $(call IFw,tcc-doc.html,"$(docdir)")
276 ifneq "$(wildcard $(LIBTCC1_W))" ""
277 $(call IFw,$(TOPSRC)/win32/lib/*.def $(LIBTCC1_W),"$(tccdir)/win32/lib")
278 $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/win32/include")
279 $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/win32/include")
280 endif
282 # uninstall
283 uninstall-unx:
284 @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS),"$(bindir)/$P")
285 @rm -fv "$(libdir)/libtcc.a" "$(libdir)/libtcc.so" "$(includedir)/libtcc.h"
286 @rm -fv "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info"
287 @rm -fv "$(docdir)/tcc-doc.html"
288 rm -r "$(tccdir)"
290 # install progs & libs on windows
291 install-win:
292 $(call IBw,$(PROGS) $(PROGS_CROSS) $(subst libtcc.a,,$(LIBTCC)),"$(bindir)")
293 $(call IF,$(TOPSRC)/win32/lib/*.def,"$(tccdir)/lib")
294 $(call IFw,libtcc1.a $(LIBTCC1_W),"$(tccdir)/lib")
295 $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/include")
296 $(call IR,$(TOPSRC)/win32/include,"$(tccdir)/include")
297 $(call IR,$(TOPSRC)/win32/examples,"$(tccdir)/examples")
298 $(call IF,$(TOPSRC)/tests/libtcc_test.c,"$(tccdir)/examples")
299 $(call IFw,$(TOPSRC)/libtcc.h $(subst .dll,.def,$(LIBTCC)),"$(libdir)")
300 $(call IFw,$(TOPSRC)/win32/tcc-win32.txt tcc-doc.html,"$(docdir)")
301 ifneq "$(wildcard $(LIBTCC1_U))" ""
302 $(call IFw,$(LIBTCC1_U),"$(tccdir)/lib")
303 $(call IF,$(TOPSRC)/include/*.h $(TOPSRC)/tcclib.h,"$(tccdir)/lib/include")
304 endif
306 # the msys-git shell works to configure && make except it does not have install
307 ifeq ($(CONFIG_WIN32)-$(shell which install >/dev/null 2>&1 || echo no),yes-no)
308 install-win : INSTALL = cp
309 install-win : INSTALLBIN = cp
310 endif
312 # uninstall on windows
313 uninstall-win:
314 @rm -fv $(foreach P,$(PROGS) $(PROGS_CROSS) libtcc.dll,"$(bindir)/$P")
315 @rm -fv $(foreach F,tcc-doc.html tcc-win32.txt,"$(docdir)/$F")
316 @rm -fv $(foreach F,libtcc.h libtcc.def libtcc.a,"$(libdir)/$F")
317 rm -r "$(tccdir)"
319 # --------------------------------------------------------------------------
320 # other stuff
322 TAGFILES = *.[ch] include/*.h lib/*.[chS]
323 tags : ; ctags $(TAGFILES)
324 # cannot have both tags and TAGS on windows
325 ETAGS : ; etags $(TAGFILES)
327 # create release tarball from *current* git branch (including tcc-doc.html
328 # and converting two files to CRLF)
329 TCC-VERSION = tcc-$(VERSION)
330 tar: tcc-doc.html
331 mkdir $(TCC-VERSION)
332 ( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f )
333 cp tcc-doc.html $(TCC-VERSION)
334 for f in tcc-win32.txt build-tcc.bat ; do \
335 cat win32/$$f | sed 's,\(.*\),\1\r,g' > $(TCC-VERSION)/win32/$$f ; \
336 done
337 tar cjf $(TCC-VERSION).tar.bz2 $(TCC-VERSION)
338 rm -rf $(TCC-VERSION)
339 git reset
341 config.mak:
342 $(if $(wildcard $@),,@echo "Please run ./configure." && exit 1)
344 # run all tests
345 test:
346 $(MAKE) -C tests
347 # run test(s) from tests2 subdir (see make help)
348 tests2.%:
349 $(MAKE) -C tests/tests2 $@
351 clean:
352 rm -f $(PROGS) $(PROGS_CROSS) tcc_p$(EXESUF) tcc.pod
353 rm -f *~ *.o *.a *.so* *.out *.log lib*.def *.exe *.dll a.out tags TAGS
354 @$(MAKE) -C tests $@
355 @$(MAKE) -C lib $@
357 distclean: clean
358 rm -f config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html
360 .PHONY: all clean test tar tags ETAGS distclean install uninstall FORCE
362 help:
363 @echo "make"
364 @echo " build native compiler (from separate objects)"
365 @echo ""
366 @echo "make cross"
367 @echo " build cross compilers (from one source)"
368 @echo ""
369 @echo "make ONE_SOURCE=yes / no"
370 @echo " force building from one source / separate objects"
371 @echo ""
372 @echo "make cross-TARGET"
373 @echo " build one specific cross compiler for 'TARGET', as in"
374 @echo " $(TCC_X)"
375 @echo ""
376 @echo "Custom configuration:"
377 @echo " The makefile includes a file 'config-extra.mak' if it is present."
378 @echo " This file may contain some custom configuration. For example:"
379 @echo ""
380 @echo " NATIVE_DEFINES += -D..."
381 @echo ""
382 @echo " Or for example to configure the search paths for a cross-compiler"
383 @echo " that expects the linux files in <tccdir>/i386-linux:"
384 @echo ""
385 @echo " ROOT-i386 = {B}/i386-linux"
386 @echo " CRT-i386 = {B}/i386-linux/usr/lib"
387 @echo " LIB-i386 = {B}/i386-linux/lib:{B}/i386-linux/usr/lib"
388 @echo " INC-i386 = {B}/lib/include:{B}/i386-linux/usr/include"
389 @echo " DEF-i386 += -D__linux__"
390 @echo ""
391 @echo "make test"
392 @echo " run all tests"
393 @echo ""
394 @echo "make tests2.all / make tests2.37 / make tests2.37+"
395 @echo " run all/single test(s) from tests2, optionally update .expect"
396 @echo ""
397 @echo "Other supported make targets:"
398 @echo " install install-strip tags ETAGS tar clean distclean help"
399 @echo ""
401 # --------------------------------------------------------------------------
402 endif # ($(INCLUDED),no)