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