Relicensing TinyCC
[tinycc.git] / configure
1 #!/bin/sh
2 #
3 # tcc configure script (c) 2003 Fabrice Bellard
4
5 # set temporary file name
6 # if test ! -z "$TMPDIR" ; then
7 #     TMPDIR1="${TMPDIR}"
8 # elif test ! -z "$TEMPDIR" ; then
9 #     TMPDIR1="${TEMPDIR}"
10 # else
11 #     TMPDIR1="/tmp"
12 # fi
13 #
14 # bashism: TMPN="${TMPDIR1}/tcc-conf-${RANDOM}-$$-${RANDOM}.c"
15
16 TMPN="./conftest-$$"
17 TMPH=$TMPN.h
18
19 # default parameters
20 build_cross="no"
21 use_libgcc="no"
22 enable_assert="no"
23 prefix=""
24 execprefix=""
25 bindir=""
26 libdir=""
27 tccdir=""
28 includedir=""
29 mandir=""
30 infodir=""
31 sysroot=""
32 cross_prefix=""
33 cc="gcc"
34 host_cc="gcc"
35 ar="ar"
36 strip="strip"
37 cygwin="no"
38 gprof="no"
39 bigendian="no"
40 mingw32="no"
41 LIBSUF=".a"
42 EXESUF=""
43 tcc_sysincludepaths=""
44 tcc_libpaths=""
45 tcc_crtprefix=""
46 tcc_elfinterp=""
47 tcc_lddir=
48 confvars=
49
50 cpu=`uname -m`
51
52 # OS specific
53 targetos=`uname -s`
54 case $targetos in
55   MINGW32*)  mingw32=yes;;
56   DragonFly) noldl=yes;;
57   OpenBSD)   noldl=yes;;
58   FreeBSD)   noldl=yes;;
59   *) ;;
60 esac
61
62 # find source path
63 # XXX: we assume an absolute path is given when launching configure, 
64 # except in './configure' case.
65 source_path=${0%configure}
66 source_path=${source_path%/}
67 source_path_used="yes"
68 if test -z "$source_path" -o "$source_path" = "." ; then
69     source_path=`pwd`
70     source_path_used="no"
71 fi
72
73 case "$cpu" in
74   i386|i486|i586|i686|i86pc|BePC|i686-AT386)
75     cpu="x86"
76   ;;
77   x86_64|amd64)
78     cpu="x86-64"
79   ;;
80   arm*)
81     case "$cpu" in
82       arm|armv4l)
83         cpuver=4
84       ;;
85       armv5tel|armv5tejl)
86         cpuver=5
87       ;;
88       armv6j|armv6l)
89         cpuver=6
90       ;;
91       armv7a|armv7l)
92         cpuver=7
93       ;;
94     esac
95     cpu="armv4l"
96   ;;
97   alpha)
98     cpu="alpha"
99   ;;
100   "Power Macintosh"|ppc|ppc64)
101     cpu="powerpc"
102   ;;
103   mips)
104     cpu="mips"
105   ;;
106   s390)
107     cpu="s390"
108   ;;
109   *)
110     cpu="unknown"
111   ;;
112 esac
113
114 for opt do
115   eval opt=\"$opt\"
116   case "$opt" in
117   --prefix=*) prefix=`echo $opt | cut -d '=' -f 2`
118   ;;
119   --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2`
120   ;;
121   --tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2`
122   ;;
123   --bindir=*) bindir=`echo $opt | cut -d '=' -f 2`
124   ;;
125   --libdir=*) libdir=`echo $opt | cut -d '=' -f 2`
126   ;;
127   --includedir=*) includedir=`echo $opt | cut -d '=' -f 2`
128   ;;
129   --sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2`
130   ;;
131   --mandir=*) mandir=`echo $opt | cut -d '=' -f 2`
132   ;;
133   --infodir=*) infodir=`echo $opt | cut -d '=' -f 2`
134   ;;
135   --docdir=*) docdir=`echo $opt | cut -d '=' -f 2`
136   ;;
137   --sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2`
138   ;;
139   --source-path=*) source_path=`echo $opt | cut -d '=' -f 2`
140   ;;
141   --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2`
142   ;;
143   --cc=*) cc=`echo $opt | cut -d '=' -f 2`
144   ;;
145   --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
146   ;;
147   --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
148   ;;
149   --extra-libs=*) extralibs=${opt#--extra-libs=}
150   ;;
151   --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2`
152   ;;
153   --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2`
154   ;;
155   --crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2`
156   ;;
157   --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2`
158   ;;
159   --cpu=*) cpu=`echo $opt | cut -d '=' -f 2`
160   ;;
161   --enable-gprof) gprof="yes"
162   ;;
163   --enable-mingw32) mingw32="yes" ; cross_prefix="i686-pc-mingw32-" ; cpu=x86
164   ;;
165   --enable-cygwin) mingw32="yes" ; cygwin="yes" ; cross_prefix="mingw32-" ; cpu=x86
166   ;;
167   --enable-cross) build_cross="yes"
168   ;;
169   --enable-assert) enable_assert="yes"
170   ;;
171   --disable-static) disable_static="yes"
172   ;;
173   --disable-rpath) disable_rpath="yes"
174   ;;
175   --strip-binaries) strip_binaries="yes"
176   ;;
177   --with-libgcc) use_libgcc="yes"
178   ;;
179   --with-selinux) have_selinux="yes"
180   ;;
181   --help|-h) show_help="yes"
182   ;;
183   *) echo "configure: WARNING: unrecognized option $opt"
184   ;;
185   esac
186 done
187
188 # Checking for CFLAGS
189 if test -z "$CFLAGS"; then
190     CFLAGS="-Wall -g -O2"
191 fi
192
193 if test "$mingw32" = "yes" ; then
194     if test x"$tccdir" = x""; then
195     tccdir="tcc"
196     fi
197     if test -z "$prefix" ; then
198     prefix="C:/Program Files/${tccdir}"
199     fi
200     if test -z "$sharedir" ; then
201     sharedir="${prefix}"
202     fi
203     execprefix="$prefix"
204     bindir="${prefix}"
205     tccdir="${prefix}"
206     libdir="${prefix}/lib"
207     docdir="${sharedir}/doc"
208     mandir="${sharedir}/man"
209     infodir="${sharedir}/info"
210     LIBSUF=".lib"
211     EXESUF=".exe"
212 else
213     if test -z "$prefix" ; then
214     prefix="/usr/local"
215     fi
216     if test -z "$sharedir" ; then
217     sharedir="${prefix}/share"
218     fi
219     if test x"$execprefix" = x""; then
220     execprefix="${prefix}"
221     fi
222     if test x"$libdir" = x""; then
223     libdir="${execprefix}/lib"
224     fi
225     if test x"$bindir" = x""; then
226     bindir="${execprefix}/bin"
227     fi
228     if test x"$tccdir" = x""; then
229     tccdir="tcc"
230     fi
231     if test x"$docdir" = x""; then
232     docdir="${sharedir}/doc/${tccdir}"
233     fi
234     if test x"$mandir" = x""; then
235     mandir="${sharedir}/man"
236     fi
237     if test x"$infodir" = x""; then
238     infodir="${sharedir}/info"
239     fi
240     tccdir="${libdir}/${tccdir}"
241 fi # mingw32
242
243 if test x"$includedir" = x""; then
244 includedir="${prefix}/include"
245 fi
246
247 if test x"$show_help" = "xyes" ; then
248 cat << EOF
249 Usage: configure [options]
250 Options: [defaults in brackets after descriptions]
251
252 Standard options:
253   --help                   print this message
254   --prefix=PREFIX          install in PREFIX [$prefix]
255   --exec-prefix=EPREFIX    install architecture-dependent files in EPREFIX
256                            [same as prefix]
257   --bindir=DIR             user executables in DIR [EPREFIX/bin]
258   --libdir=DIR             object code libraries in DIR [EPREFIX/lib]
259   --tccdir=DIR             installation directory [EPREFIX/lib/tcc]
260   --includedir=DIR         C header files in DIR [PREFIX/include]
261   --sharedir=DIR           documentation root DIR [PREFIX/share]
262   --docdir=DIR             documentation in DIR [SHAREDIR/doc/tcc]
263   --mandir=DIR             man documentation in DIR [SHAREDIR/man]
264   --infodir=DIR            info documentation in DIR [SHAREDIR/info]
265
266 Advanced options (experts only):
267   --source-path=PATH       path of source code [$source_path]
268   --cross-prefix=PREFIX    use PREFIX for compile tools [$cross_prefix]
269   --sysroot=PREFIX         prepend PREFIX to library/include paths []
270   --cc=CC                  use C compiler CC [$cc]
271   --extra-cflags=          specify compiler flags [$CFLAGS]
272   --extra-ldflags=         specify linker options []
273   --strip-binaries         strip symbol tables from resulting binaries
274   --disable-static         make libtcc.so instead of libtcc.a
275   --disable-rpath          disable use of -rpath with the above
276   --with-libgcc            use /lib/libgcc_s.so.1 instead of libtcc.a
277   --enable-mingw32         build windows version on linux with mingw32
278   --enable-cygwin          build windows version on windows with cygwin
279   --enable-cross           build cross compilers
280   --enable-assert          enable debug assertions
281   --with-selinux           use mmap for exec mem [needs writable /tmp]
282   --sysincludepaths=...    specify system include paths, colon separated
283   --libpaths=...           specify system library paths, colon separated
284   --crtprefix=...          specify locations of crt?.o, colon separated
285   --elfinterp=...          specify elf interpreter
286 EOF
287 #echo "NOTE: The object files are build at the place where configure is launched"
288 exit 1
289 fi
290
291 cc="${cross_prefix}${cc}"
292 ar="${cross_prefix}${ar}"
293 strip="${cross_prefix}${strip}"
294
295 CONFTEST=./conftest$EXESUF
296
297 if test -z "$cross_prefix" ; then
298   if ! $cc -o $CONFTEST $source_path/conftest.c 2>/dev/null ; then
299     echo "configure: error: '$cc' failed to compile conftest.c."
300   else
301     bigendian="$($CONFTEST bigendian)"
302     gcc_major="$($CONFTEST version)"
303     gcc_minor="$($CONFTEST minor)"
304     if test "$mingw32" = "no" ; then
305       triplet="$($CONFTEST triplet)"
306       if test -f "/usr/lib/$triplet/crti.o" ; then
307         tcc_lddir="lib/$triplet"
308         multiarch_triplet="$triplet"
309       elif test -f "/usr/lib64/crti.o" ; then
310         tcc_lddir="lib64"
311       fi
312
313       if test "$cpu" = "armv4l" ; then
314         if test "${triplet%eabihf}" != "$triplet" ; then
315            confvars="$confvars arm_eabihf"
316         elif test "${triplet%eabi}" != "$triplet" ; then
317            confvars="$confvars arm_eabi"
318         fi
319         if grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then
320            confvars="$confvars arm_vfp"
321         fi
322       fi
323
324 #      multiarch_triplet=${libc_dir#*/}
325 #      multiarch_triplet=${multiarch_triplet%/}
326 #      tcc_lddir="${libc_dir%%/*}"
327 #      if test -n "$multiarch_triplet" ; then
328 #        tcc_lddir="$tcc_lddir/$multiarch_triplet"
329 #      fi
330
331       if test -f "/lib/ld-uClibc.so.0" ; then
332         confvars="$confvars uClibc"
333       fi
334 # gr: maybe for after the release:
335 #       tcc_elfinterp="$(ldd $CONFTEST | grep 'ld.*.so' | sed 's,\s*\(\S\+\).*,\1,')"
336        # echo "elfinterp           $tcc_elfinterp"
337
338     fi
339   fi
340 else
341   # if cross compiling, cannot launch a program, so make a static guess
342   case $cpu in
343     powerpc|mips|s390)  bigendian=yes;;
344   esac
345 fi
346
347 cat <<EOF
348 Binary  directory   $bindir
349 TinyCC directory    $tccdir
350 Library directory   $libdir
351 Include directory   $includedir
352 Manual directory    $mandir
353 Info directory      $infodir
354 Doc directory       $docdir
355 Target root prefix  $sysroot
356 Source path      $source_path
357 C compiler       $cc
358 Target OS        $targetos
359 CPU              $cpu
360 Big Endian       $bigendian
361 gprof enabled    $gprof
362 cross compilers  $build_cross
363 use libgcc       $use_libgcc
364 EOF
365
366 echo "Creating config.mak and config.h"
367
368 cat >config.mak <<EOF
369 # Automatically generated by configure - do not modify
370 prefix=$prefix
371 bindir=\$(DESTDIR)$bindir
372 tccdir=\$(DESTDIR)$tccdir
373 libdir=\$(DESTDIR)$libdir
374 ln_libdir=$libdir
375 includedir=\$(DESTDIR)$includedir
376 mandir=\$(DESTDIR)$mandir
377 infodir=\$(DESTDIR)$infodir
378 docdir=\$(DESTDIR)$docdir
379 CC=$cc
380 GCC_MAJOR=$gcc_major
381 GCC_MINOR=$gcc_minor
382 HOST_CC=$host_cc
383 AR=$ar
384 STRIP=$strip -s -R .comment -R .note
385 CFLAGS=$CFLAGS
386 LDFLAGS=$LDFLAGS
387 LIBSUF=$LIBSUF
388 EXESUF=$EXESUF
389 EOF
390
391 print_inc() {
392   if test -n "$2"; then
393     echo "#ifndef $1" >> $TMPH
394     echo "# define $1 \"$2\"" >> $TMPH
395     echo "#endif" >> $TMPH
396   fi
397 }
398 print_mak() {
399   if test -n "$2"; then
400     echo "NATIVE_DEFINES+=-D$1=\"\\\"$2\\\"\"" >> config.mak
401   fi
402 }
403
404 echo "/* Automatically generated by configure - do not modify */" > $TMPH
405
406 print_inc CONFIG_SYSROOT "$sysroot"
407 print_inc CONFIG_TCCDIR "$tccdir"
408 print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
409 print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
410 print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
411 print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
412 print_mak CONFIG_LDDIR "$tcc_lddir"
413 print_mak CONFIG_MULTIARCHDIR "$multiarch_triplet"
414
415 echo "#define GCC_MAJOR $gcc_major" >> $TMPH
416 echo "#define GCC_MINOR $gcc_minor" >> $TMPH
417
418 if test "$cpu" = "x86" ; then
419   echo "ARCH=i386" >> config.mak
420   echo "#define HOST_I386 1" >> $TMPH
421 elif test "$cpu" = "x86-64" ; then
422   echo "ARCH=x86-64" >> config.mak
423   echo "#define HOST_X86_64 1" >> $TMPH
424 elif test "$cpu" = "armv4l" ; then
425   echo "ARCH=arm" >> config.mak
426   echo "#define HOST_ARM 1" >> $TMPH
427   echo "#define TCC_ARM_VERSION $cpuver" >> $TMPH
428 elif test "$cpu" = "powerpc" ; then
429   echo "ARCH=ppc" >> config.mak
430   echo "#define HOST_PPC 1" >> $TMPH
431 elif test "$cpu" = "mips" ; then
432   echo "ARCH=mips" >> config.mak
433   echo "#define HOST_MIPS 1" >> $TMPH
434 elif test "$cpu" = "s390" ; then
435   echo "ARCH=s390" >> config.mak
436   echo "#define HOST_S390 1" >> $TMPH
437 elif test "$cpu" = "alpha" ; then
438   echo "ARCH=alpha" >> config.mak
439   echo "#define HOST_ALPHA 1" >> $TMPH
440 else
441   echo "Unsupported CPU"
442   exit 1
443 fi
444
445 echo "TARGETOS=$targetos" >> config.mak
446
447 for v in $confvars ; do
448   echo "CONFIG_$v=yes" >> config.mak
449 done
450 if test "$noldl" = "yes" ; then
451   echo "CONFIG_NOLDL=yes" >> config.mak
452 fi
453 if test "$mingw32" = "yes" ; then
454   echo "CONFIG_WIN32=yes" >> config.mak
455   echo "#define CONFIG_WIN32 1" >> $TMPH
456 fi
457 if test "$cygwin" = "yes" ; then
458   echo "#ifndef _WIN32" >> $TMPH
459   echo "# define _WIN32" >> $TMPH
460   echo "#endif" >> $TMPH
461   echo "AR=ar" >> config.mak
462 fi
463 if test "$bigendian" = "yes" ; then
464   echo "WORDS_BIGENDIAN=yes" >> config.mak
465   echo "#define WORDS_BIGENDIAN 1" >> $TMPH
466 fi
467 if test "$gprof" = "yes" ; then
468   echo "TARGET_GPROF=yes" >> config.mak
469   echo "#define HAVE_GPROF 1" >> $TMPH
470 fi
471 if test "$build_cross" = "yes" ; then
472   echo "CONFIG_CROSS=yes" >> config.mak
473 fi
474 if test "$disable_static" = "yes" ; then
475   echo "DISABLE_STATIC=yes" >> config.mak
476 fi
477 if test "$disable_rpath" = "yes" ; then
478   echo "DISABLE_RPATH=yes" >> config.mak
479 fi
480 if test "$strip_binaries" = "yes" ; then
481   echo "STRIP_BINARIES=yes" >> config.mak
482 fi
483 if test "$use_libgcc" = "yes" ; then
484   echo "#define CONFIG_USE_LIBGCC" >> $TMPH
485   echo "CONFIG_USE_LIBGCC=yes" >> config.mak
486 fi
487 if test "$have_selinux" = "yes" ; then
488   echo "#define HAVE_SELINUX" >> $TMPH
489   echo "HAVE_SELINUX=yes" >> config.mak
490 fi
491 if test "$enable_assert" = "yes" ; then
492   echo "#define CONFIG_TCC_ASSERT" >> $TMPH
493 fi
494
495 version=`head $source_path/VERSION`
496 echo "VERSION=$version" >>config.mak
497 echo "#define TCC_VERSION \"$version\"" >> $TMPH
498 echo "@set VERSION $version" > config.texi
499 echo "SRC_PATH=$source_path" >>config.mak
500
501 if test "$source_path_used" = "yes" ; then
502     case $source_path in
503        /*) echo "top_srcdir=$source_path";;
504         *) echo "top_srcdir=\$(TOP)/$source_path";;
505      esac >>config.mak
506 else
507      echo 'top_srcdir=$(TOP)' >>config.mak
508 fi
509 echo 'top_builddir=$(TOP)' >>config.mak
510
511 diff $TMPH config.h >/dev/null 2>&1
512 if test $? -ne 0 ; then
513     mv -f $TMPH config.h
514 else
515     echo "config.h is unchanged"
516 fi
517
518 rm -f $TMPN* $CONFTEST
519
520 # ---------------------------------------------------------------------------
521 # build tree in object directory if source path is different from current one
522
523 fn_makelink()
524 {
525     tgt=$1/$2
526     case $2 in
527     */*) dn=${2%/*}
528          test -d $dn || mkdir -p $dn
529          case $1 in
530          /*) ;;
531           *) while test $dn ; do
532                 tgt=../$tgt; dn=${dn#${dn%%/*}}; dn=${dn#/}
533              done
534              ;;
535          esac
536          ;;
537     esac
538     ln -sfn $tgt $2
539 }
540
541 if test "$source_path_used" = "yes" ; then
542   FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile"
543   for f in $FILES ; do
544     fn_makelink $source_path $f
545   done
546 fi
547
548 # ---------------------------------------------------------------------------