build: fix out-of-tree build
[tinycc.git] / Makefile
bloba227452becd221b911f2f4e7de3ce3397a12f4e4
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 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 "$(includedir)"
286 $(INSTALL) -m644 libtcc.h "$(includedir)"
287 mkdir -p "$(docdir)"
288 -$(INSTALL) -m644 tcc-doc.html "$(docdir)"
289 ifdef CONFIG_CROSS
290 mkdir -p "$(tccdir)/win32/lib/32"
291 mkdir -p "$(tccdir)/win32/lib/64"
292 ifeq ($(ARCH),x86-64)
293 mkdir -p "$(tccdir)/i386"
294 $(INSTALL) -m644 lib/i386/libtcc1.a "$(tccdir)/i386"
295 cp -r "$(tccdir)/include" "$(tccdir)/i386"
296 endif
297 $(INSTALL) -m644 win32/lib/*.def "$(tccdir)/win32/lib"
298 $(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/win32/lib/32"
299 $(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/win32/lib/64"
300 cp -r win32/include/. "$(tccdir)/win32/include"
301 cp -r include/. "$(tccdir)/win32/include"
302 endif
304 uninstall:
305 rm -fv $(foreach P,$(PROGS),"$(bindir)/$P")
306 rm -fv $(foreach P,$(LIBTCC1) $(BCHECK_O),"$(tccdir)/$P")
307 rm -fv $(foreach P,$(TCC_INCLUDES),"$(tccdir)/include/$P")
308 rm -fv "$(docdir)/tcc-doc.html" "$(mandir)/man1/tcc.1" "$(infodir)/tcc-doc.info"
309 rm -fv "$(libdir)/$(LIBTCC)" "$(includedir)/libtcc.h"
310 rm -fv "$(libdir)/libtcc.so*"
311 rm -rf "$(tccdir)/win32"
312 -rmdir $(tccdir)/include
313 ifeq ($(ARCH),x86-64)
314 rm -rf "$(tccdir)/i386"
315 endif
316 else
317 # on windows
318 install: $(PROGS) $(TCCLIBS) $(TCCDOCS)
319 mkdir -p "$(tccdir)"
320 mkdir -p "$(tccdir)/lib"
321 mkdir -p "$(tccdir)/include"
322 mkdir -p "$(tccdir)/examples"
323 mkdir -p "$(tccdir)/doc"
324 mkdir -p "$(tccdir)/libtcc"
325 $(INSTALLBIN) -m755 $(PROGS) "$(tccdir)"
326 $(INSTALL) -m644 $(LIBTCC1) win32/lib/*.def "$(tccdir)/lib"
327 cp -r win32/include/. "$(tccdir)/include"
328 cp -r win32/examples/. "$(tccdir)/examples"
329 $(INSTALL) -m644 $(addprefix include/,$(TCC_INCLUDES)) "$(tccdir)/include"
330 $(INSTALL) -m644 tcc-doc.html win32/tcc-win32.txt "$(tccdir)/doc"
331 $(INSTALL) -m644 $(LIBTCC) libtcc.h "$(tccdir)/libtcc"
332 ifdef CONFIG_CROSS
333 mkdir -p "$(tccdir)/lib/32"
334 mkdir -p "$(tccdir)/lib/64"
335 -$(INSTALL) -m644 lib/i386-win32/libtcc1.a "$(tccdir)/lib/32"
336 -$(INSTALL) -m644 lib/x86_64-win32/libtcc1.a "$(tccdir)/lib/64"
337 endif
339 uninstall:
340 rm -rfv "$(tccdir)/*"
341 endif
343 # documentation and man page
344 tcc-doc.html: tcc-doc.texi
345 -texi2html -monolithic -number $<
347 tcc.1: tcc-doc.texi
348 -$(top_srcdir)/texi2pod.pl $< tcc.pod
349 -pod2man --section=1 --center=" " --release=" " tcc.pod > $@
351 tcc-doc.info: tcc-doc.texi
352 -makeinfo $<
354 # in tests subdir
355 export LIBTCC1
357 %est:
358 $(MAKE) -C tests2 $@
359 $(MAKE) -C tests $@
361 clean:
362 rm -vf $(PROGS) tcc_p$(EXESUF) tcc.pod *~ *.o *.a *.so* *.out *.exe libtcc_test$(EXESUF)
363 $(MAKE) -C tests2 $@
364 $(MAKE) -C tests $@
365 ifneq ($(LIBTCC1),)
366 $(MAKE) -C lib $@
367 endif
369 distclean: clean
370 rm -vf config.h config.mak config.texi tcc.1 tcc-doc.info tcc-doc.html
372 config.mak:
373 @echo "Please run ./configure."
374 @exit 1
376 # create release tarball from *current* git branch (including tcc-doc.html
377 # and converting two files to CRLF)
378 TCC-VERSION := tcc-$(shell cat $(top_srcdir)/VERSION)
379 tar: tcc-doc.html
380 mkdir $(TCC-VERSION)
381 ( cd $(TCC-VERSION) && git --git-dir ../.git checkout -f )
382 cp tcc-doc.html $(TCC-VERSION)
383 for f in tcc-win32.txt build-tcc.bat ; do \
384 cat win32/$$f | sed 's,\(.*\),\1\r,g' > $(TCC-VERSION)/win32/$$f ; \
385 done
386 tar cjf $(TCC-VERSION).tar.bz2 $(TCC-VERSION)
387 rm -rf $(TCC-VERSION)
388 git reset
391 .PHONY: all clean tar distclean install uninstall FORCE
393 endif # ifeq ($(TOP),.)