Optimize cstr_reset() to only reset string to empty, not call free() and later malloc()
[tinycc.git] / Makefile
blobf35bdffcafeed273333a67a169ae2b4e0fcfc7ac
2 # Tiny C Compiler Makefile
5 TOP ?= .
6 include $(TOP)/config.mak
7 VPATH = $(top_srcdir)
9 CPPFLAGS_P=$(CPPFLAGS) -DCONFIG_TCC_STATIC
10 CFLAGS_P=$(CFLAGS) -pg -static
11 LIBS_P=
12 LDFLAGS_P=$(LDFLAGS)
14 ifneq ($(GCC_MAJOR),2)
15 CFLAGS+=-fno-strict-aliasing
16 ifneq ($(GCC_MAJOR),3)
17 CFLAGS+=-Wno-pointer-sign -Wno-sign-compare
19 # add -Wno-unused-result only on gcc >= 4.4
20 ifeq ($(GCC_MAJOR),4)
21 GCCGREATERTHEN44 := $(shell expr `gcc -dumpversion | cut -f2 -d.` \>= 4)
22 else
23 GCCGREATERTHEN44 := 1
24 endif
25 ifeq ($(GCCGREATERTHEN44),1)
26 CFLAGS+=-Wno-unused-result
27 endif
29 endif
30 endif
32 ifneq ($(TARGETOS),Darwin)
33 ifeq ($(ARCH),i386)
34 CFLAGS+=-mpreferred-stack-boundary=2
35 ifeq ($(GCC_MAJOR),2)
36 CFLAGS+=-m386 -malign-functions=0
37 else
38 CFLAGS+=-march=i386 -falign-functions=0
39 endif
40 endif
41 endif
43 ifdef CONFIG_WIN64
44 CONFIG_WIN32=yes
45 endif
47 ifndef CONFIG_WIN32
48 LIBS=-lm
49 ifndef CONFIG_NOLDL
50 LIBS+=-ldl
51 endif
52 endif
54 ifeq ($(ARCH),i386)
55 NATIVE_DEFINES=-DTCC_TARGET_I386
56 NATIVE_DEFINES+=$(if $(wildcard /lib/i386-linux-gnu),-DCONFIG_MULTIARCHDIR=\"i386-linux-gnu\")
57 NATIVE_DEFINES+=$(if $(wildcard /lib/i386-kfreebsd-gnu),-DCONFIG_MULTIARCHDIR=\"i386-kfreebsd-gnu\")
58 NATIVE_DEFINES+=$(if $(wildcard /lib/i386-gnu),-DCONFIG_MULTIARCHDIR=\"i386-gnu\")
59 CFLAGS+=-m32
60 else ifeq ($(ARCH),x86-64)
61 NATIVE_DEFINES=-DTCC_TARGET_X86_64
62 CFLAGS+=-m64
63 NATIVE_DEFINES+=$(if $(wildcard /usr/lib64),-DCONFIG_LDDIR=\"lib64\")
64 NATIVE_DEFINES+=$(if $(wildcard /lib/x86_64-linux-gnu),-DCONFIG_MULTIARCHDIR=\"x86_64-linux-gnu\")
65 NATIVE_DEFINES+=$(if $(wildcard /lib/x86_64-kfreebsd-gnu),-DCONFIG_MULTIARCHDIR=\"x86_64-kfreebsd-gnu\")
66 endif
68 ifeq ($(ARCH),arm)
69 NATIVE_DEFINES=-DTCC_TARGET_ARM
70 NATIVE_DEFINES+=-DWITHOUT_LIBTCC
71 ifneq (,$(wildcard /lib/ld-linux.so.3))
72 NATIVE_DEFINES+=-DTCC_ARM_EABI
73 NATIVE_DEFINES+=$(if $(wildcard /lib/arm-linux-gnueabi), -DCONFIG_MULTIARCHDIR=\"arm-linux-gnueabi\")
74 else ifneq (,$(wildcard /lib/ld-linux-armhf.so.3 /lib/arm-linux-gnueabihf/ld-linux.so.3))
75 NATIVE_DEFINES+=-DTCC_ARM_EABI -DTCC_ARM_HARDFLOAT
76 NATIVE_DEFINES+=$(if $(wildcard /lib/arm-linux-gnueabihf),-DCONFIG_MULTIARCHDIR=\"arm-linux-gnueabihf\")
77 endif
78 NATIVE_DEFINES+=$(if $(shell grep -l "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo),-DTCC_ARM_VFP)
79 endif
81 ifdef CONFIG_WIN32
82 NATIVE_DEFINES+=-DTCC_TARGET_PE
83 endif
85 ifneq ($(wildcard /lib/ld-uClibc.so.0),)
86 NATIVE_DEFINES+=-DTCC_UCLIBC
87 CONFIG_UCLIBC = 1
88 endif
90 # make libtcc as static or dynamic library?
91 ifdef DISABLE_STATIC
92 LIBTCC=libtcc.so.1.0
93 LINK_LIBTCC=-Wl,-rpath,"$(libdir)"
94 ifdef DISABLE_RPATH
95 LINK_LIBTCC=
96 endif
97 else
98 LIBTCC=libtcc.a
99 LINK_LIBTCC=
100 endif
102 ifeq ($(TOP),.)
104 PROGS=tcc$(EXESUF)
105 I386_CROSS = i386-tcc$(EXESUF)
106 WIN32_CROSS = i386-win32-tcc$(EXESUF)
107 WIN64_CROSS = x86_64-win32-tcc$(EXESUF)
108 WINCE_CROSS = arm-win32-tcc$(EXESUF)
109 X64_CROSS = x86_64-tcc$(EXESUF)
110 ARM_FPA_CROSS = arm-fpa-tcc$(EXESUF)
111 ARM_FPA_LD_CROSS = arm-fpa-ld-tcc$(EXESUF)
112 ARM_VFP_CROSS = arm-vfp-tcc$(EXESUF)
113 ARM_EABI_CROSS = arm-eabi-tcc$(EXESUF)
114 ARM_CROSS = $(ARM_FPA_CROSS) $(ARM_FPA_LD_CROSS) $(ARM_VFP_CROSS) $(ARM_EABI_CROSS)
115 C67_CROSS = c67-tcc$(EXESUF)
117 CORE_FILES = tcc.c libtcc.c tccpp.c tccgen.c tccelf.c tccasm.c tccrun.c
118 CORE_FILES += tcc.h config.h libtcc.h tcctok.h
119 I386_FILES = $(CORE_FILES) i386-gen.c i386-asm.c i386-asm.h i386-tok.h
120 WIN32_FILES = $(CORE_FILES) i386-gen.c i386-asm.c i386-asm.h i386-tok.h tccpe.c
121 WIN64_FILES = $(CORE_FILES) x86_64-gen.c i386-asm.c x86_64-asm.h tccpe.c
122 WINCE_FILES = $(CORE_FILES) arm-gen.c tccpe.c
123 X86_64_FILES = $(CORE_FILES) x86_64-gen.c i386-asm.c x86_64-asm.h
124 ARM_FILES = $(CORE_FILES) arm-gen.c
125 C67_FILES = $(CORE_FILES) c67-gen.c tcccoff.c
127 ifdef CONFIG_WIN64
128 PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF)
129 NATIVE_FILES=$(WIN64_FILES)
130 PROGS_CROSS=$(WIN32_CROSS) $(I386_CROSS) $(X64_CROSS) $(ARM_CROSS) $(C67_CROSS)
131 LIBTCC1_CROSS=lib/i386-win32/libtcc1.a
132 LIBTCC1=libtcc1.a
133 else ifdef CONFIG_WIN32
134 PROGS+=tiny_impdef$(EXESUF) tiny_libmaker$(EXESUF)
135 NATIVE_FILES=$(WIN32_FILES)
136 PROGS_CROSS=$(WIN64_CROSS) $(I386_CROSS) $(X64_CROSS) $(ARM_CROSS) $(C67_CROSS)
137 LIBTCC1_CROSS=lib/x86_64-win32/libtcc1.a
138 LIBTCC1=libtcc1.a
139 else ifeq ($(ARCH),i386)
140 NATIVE_FILES=$(I386_FILES)
141 PROGS_CROSS=$(X64_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(ARM_CROSS) $(C67_CROSS)
142 LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a
143 LIBTCC1=libtcc1.a
144 BCHECK_O=bcheck.o
145 else ifeq ($(ARCH),x86-64)
146 NATIVE_FILES=$(X86_64_FILES)
147 PROGS_CROSS=$(I386_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(ARM_CROSS) $(C67_CROSS)
148 LIBTCC1_CROSS=lib/i386-win32/libtcc1.a lib/x86_64-win32/libtcc1.a lib/i386/libtcc1.a
149 LIBTCC1=libtcc1.a
150 else ifeq ($(ARCH),arm)
151 NATIVE_FILES=$(ARM_FILES)
152 PROGS_CROSS=$(I386_CROSS) $(X64_CROSS) $(WIN32_CROSS) $(WIN64_CROSS) $(C67_CROSS)
153 endif
155 ifdef CONFIG_UCLIBC
156 BCHECK_O=
157 endif
158 ifeq ($(TARGETOS),Darwin)
159 BCHECK_O=
160 PROGS+=tiny_libmaker$(EXESUF)
161 endif
163 ifdef CONFIG_USE_LIBGCC
164 LIBTCC1=
165 endif
167 TCCLIBS = $(LIBTCC1) $(LIBTCC) $(BCHECK_O)
168 TCCDOCS = tcc.1 tcc-doc.html tcc-doc.info
170 ifdef CONFIG_CROSS
171 PROGS+=$(PROGS_CROSS)
172 TCCLIBS+=$(LIBTCC1_CROSS)
173 endif
175 all: $(PROGS) $(TCCLIBS) $(TCCDOCS)
177 # Host Tiny C Compiler
178 tcc$(EXESUF): tcc.o $(LIBTCC)
179 $(CC) -o $@ $^ $(LIBS) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) $(LINK_LIBTCC)
181 # Cross Tiny C Compilers
182 %-tcc$(EXESUF):
183 $(CC) -o $@ tcc.c -DONE_SOURCE $(DEFINES) $(CPPFLAGS) $(CFLAGS) $(LIBS) $(LDFLAGS)
185 $(I386_CROSS): DEFINES = -DTCC_TARGET_I386 \
186 -DCONFIG_TCCDIR="\"$(tccdir)/i386\""
187 $(X64_CROSS): DEFINES = -DTCC_TARGET_X86_64
188 $(WIN32_CROSS): DEFINES = -DTCC_TARGET_I386 -DTCC_TARGET_PE \
189 -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" \
190 -DCONFIG_TCC_LIBPATHS="\"{B}/lib/32;{B}/lib\""
191 $(WIN64_CROSS): DEFINES = -DTCC_TARGET_X86_64 -DTCC_TARGET_PE \
192 -DCONFIG_TCCDIR="\"$(tccdir)/win32\"" \
193 -DCONFIG_TCC_LIBPATHS="\"{B}/lib/64;{B}/lib\""
194 $(WINCE_CROSS): DEFINES = -DTCC_TARGET_PE
195 $(C67_CROSS): DEFINES = -DTCC_TARGET_C67
196 $(ARM_FPA_CROSS): DEFINES = -DTCC_TARGET_ARM
197 $(ARM_FPA_LD_CROSS)$(EXESUF): DEFINES = -DTCC_TARGET_ARM -DLDOUBLE_SIZE=12
198 $(ARM_VFP_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_VFP
199 $(ARM_EABI_CROSS): DEFINES = -DTCC_TARGET_ARM -DTCC_ARM_EABI
201 $(I386_CROSS): $(I386_FILES)
202 $(X64_CROSS): $(X86_64_FILES)
203 $(WIN32_CROSS): $(WIN32_FILES)
204 $(WIN64_CROSS): $(WIN64_FILES)
205 $(WINCE_CROSS): $(WINCE_FILES)
206 $(C67_CROSS): $(C67_FILES)
207 $(ARM_FPA_CROSS) $(ARM_FPA_LD_CROSS) $(ARM_VFP_CROSS) $(ARM_EABI_CROSS): $(ARM_FILES)
209 # libtcc generation and test
210 ifndef ONE_SOURCE
211 LIBTCC_OBJ = $(filter-out tcc.o,$(patsubst %.c,%.o,$(filter %.c,$(NATIVE_FILES))))
212 LIBTCC_INC = $(filter %.h,$(CORE_FILES)) $(filter-out $(CORE_FILES),$(NATIVE_FILES))
213 else
214 LIBTCC_OBJ = libtcc.o
215 LIBTCC_INC = $(NATIVE_FILES)
216 libtcc.o : NATIVE_DEFINES += -DONE_SOURCE
217 endif
219 $(LIBTCC_OBJ) tcc.o : %.o : %.c $(LIBTCC_INC)
220 $(CC) -o $@ -c $< $(NATIVE_DEFINES) $(CPPFLAGS) $(CFLAGS)
222 libtcc.a: $(LIBTCC_OBJ)
223 $(AR) rcs $@ $^
225 libtcc.so.1.0: $(LIBTCC_OBJ)
226 $(CC) -shared -Wl,-soname,$@ -o $@ $^ $(LDFLAGS)
228 libtcc.so.1.0: CFLAGS+=-fPIC
230 # profiling version
231 tcc_p$(EXESUF): $(NATIVE_FILES)
232 $(CC) -o $@ $< $(NATIVE_DEFINES) $(CPPFLAGS_P) $(CFLAGS_P) $(LIBS_P) $(LDFLAGS_P)
234 # windows utilities
235 tiny_impdef$(EXESUF): win32/tools/tiny_impdef.c
236 $(CC) -o $@ $< $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
237 tiny_libmaker$(EXESUF): win32/tools/tiny_libmaker.c
238 $(CC) -o $@ $< $(CPPFLAGS) $(CFLAGS) $(LDFLAGS)
240 # TinyCC runtime libraries
241 libtcc1.a : FORCE
242 @$(MAKE) -C lib native
243 lib/%/libtcc1.a : FORCE $(PROGS_CROSS)
244 @$(MAKE) -C lib cross TARGET=$*
245 bcheck.o : lib/bcheck.c
246 gcc -c $< -o $@ $(CPPFLAGS) $(CFLAGS)
247 FORCE:
249 # install
250 TCC_INCLUDES = stdarg.h stddef.h stdbool.h float.h varargs.h tcclib.h
251 INSTALL=install
252 ifdef STRIP_BINARIES
253 INSTALLBIN=$(INSTALL) -s
254 else
255 INSTALLBIN=$(INSTALL)
256 endif
258 ifndef CONFIG_WIN32
259 install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
260 mkdir -p "$(bindir)"
261 ifeq ($(CC),tcc)
262 $(INSTALL) -m755 $(PROGS) "$(bindir)"
263 else
264 $(INSTALLBIN) -m755 $(PROGS) "$(bindir)"
265 endif
266 mkdir -p "$(mandir)/man1"
267 -$(INSTALL) tcc.1 "$(mandir)/man1"
268 mkdir -p "$(infodir)"
269 -$(INSTALL) tcc-doc.info "$(infodir)"
270 mkdir -p "$(tccdir)"
271 mkdir -p "$(tccdir)/include"
272 ifneq ($(LIBTCC1),)
273 $(INSTALL) -m644 $(LIBTCC1) "$(tccdir)"
274 endif
275 ifneq ($(BCHECK_O),)
276 $(INSTALL) -m644 $(BCHECK_O) "$(tccdir)"
277 endif
278 $(INSTALL) -m644 $(addprefix $(top_srcdir)/include/,$(TCC_INCLUDES)) "$(tccdir)/include"
279 mkdir -p "$(libdir)"
280 $(INSTALL) -m755 $(LIBTCC) "$(libdir)"
281 ifdef DISABLE_STATIC
282 ln -sf "$(ln_libdir)/libtcc.so.1.0" "$(libdir)/libtcc.so.1"
283 ln -sf "$(ln_libdir)/libtcc.so.1.0" "$(libdir)/libtcc.so"
284 endif
285 mkdir -p "$(docdir)"
286 -$(INSTALL) -m644 tcc-doc.html "$(docdir)"
287 ifdef CONFIG_CROSS
288 mkdir -p "$(tccdir)/win32/lib/32"
289 mkdir -p "$(tccdir)/win32/lib/64"
290 ifeq ($(ARCH),x86-64)
291 mkdir -p "$(tccdir)/i386"
292 $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386"
293 cp -r "$(tccdir)/include" "$(tccdir)/i386"
294 endif
295 $(INSTALL) -m644 win32/lib/*.def "$(tccdir)/win32/lib"
296 $(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/win32/lib/32"
297 $(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/win32/lib/64"
298 cp -r win32/include/. "$(tccdir)/win32/include"
299 cp -r include/. "$(tccdir)/win32/include"
300 endif
302 uninstall:
303 rm -fv $(foreach P,$(PROGS),"$(bindir)/$P")
304 rm -fv $(foreach P,$(LIBTCC1) $(BCHECK_O),"$(tccdir)/$P")
305 rm -fv $(foreach P,$(TCC_INCLUDES),"$(tccdir)/include/$P")
306 rm -fv "$(docdir)/tcc-doc.html" "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info"
307 rm -fv "$(libdir)/$(LIBTCC)" "$(includedir)/libtcc.h"
308 rm -fv "$(libdir)/libtcc.so*"
309 rm -rf "$(tccdir)/win32"
310 -rmdir $(tccdir)/include
311 ifeq ($(ARCH),x86-64)
312 rm -rf "$(tccdir)/i386"
313 endif
314 else
315 # on windows
316 install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
317 mkdir -p "$(tccdir)"
318 mkdir -p "$(tccdir)/lib"
319 mkdir -p "$(tccdir)/include"
320 mkdir -p "$(tccdir)/examples"
321 mkdir -p "$(tccdir)/doc"
322 mkdir -p "$(tccdir)/libtcc"
323 $(INSTALLBIN) -m755 $(PROGS) "$(tccdir)"
324 $(INSTALL) -m644 $(LIBTCC1) win32/lib/*.def "$(tccdir)/lib"
325 cp -r win32/include/. "$(tccdir)/include"
326 cp -r win32/examples/. "$(tccdir)/examples"
327 $(INSTALL) -m644 $(addprefix include/,$(TCC_INCLUDES)) "$(tccdir)/include"
328 $(INSTALL) -m644 tcc-doc.html win32/tcc-win32.txt "$(tccdir)/doc"
329 $(INSTALL) -m644 $(LIBTCC) libtcc.h "$(tccdir)/libtcc"
330 ifdef CONFIG_CROSS
331 mkdir -p "$(tccdir)/lib/32"
332 mkdir -p "$(tccdir)/lib/64"
333 -$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/lib/32"
334 -$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/lib/64"
335 endif
337 uninstall:
338 rm -rfv "$(tccdir)/*"
339 endif
341 # documentation and man page
342 tcc-doc.html: tcc-doc.texi
343 -texi2html -monolithic -number $<
345 tcc.1: tcc-doc.texi
346 -$(top_srcdir)/texi2pod.pl $< tcc.pod
347 -pod2man --section=1 --center=" " --release=" " tcc.pod > $@
349 tcc-doc.info: tcc-doc.texi
350 -makeinfo $<
352 # in tests subdir
353 export LIBTCC1
355 %est:
356 $(MAKE) -C tests2 $@
357 $(MAKE) -C tests $@
359 clean:
360 rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF)
361 $(MAKE) -C tests2 $@
362 $(MAKE) -C tests $@
363 ifneq ($(LIBTCC1),)
364 $(MAKE) -C lib $@
365 endif
367 distclean: clean
368 rm -vf config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html
370 config.mak:
371 @echo "Please run ./configure."
372 @exit 1
374 # create release tarball from *current* git branch (including tcc-doc.html
375 # and converting two files to CRLF)
376 TCC-VERSION := tcc-$(shell cat $(top_srcdir)/VERSION)
377 tar: tcc-doc.html
378 mkdir $(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
389 .PHONY: all clean tar distclean install uninstall FORCE
391 endif # ifeq ($(TOP),.)