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