fix: respect CFLAGS from environment, or set defaults
[tinycc.git] / configure
blob703ab63f5dd8091bae4771baf9d9d1b28a5c398f
1 #!/bin/sh
3 # tcc configure script (c) 2003 Fabrice Bellard
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
14 # bashism: TMPN="${TMPDIR1}/tcc-conf-${RANDOM}-$$-${RANDOM}.c"
16 TMPN="./conftest-$$"
17 TMPH=$TMPN.h
19 # default parameters
20 prefix=""
21 execprefix=""
22 bindir=""
23 libdir=""
24 tccdir=""
25 includedir=""
26 mandir=""
27 infodir=""
28 sysroot=""
29 cross_prefix=""
30 cc="gcc"
31 ar="ar"
32 bigendian="no"
33 mingw32="no"
34 LIBSUF=".a"
35 EXESUF=""
36 DLLSUF=".so"
37 tcc_usrinclude=""
38 tcc_sysincludepaths=""
39 tcc_libpaths=""
40 tcc_crtprefix=""
41 tcc_elfinterp=""
42 triplet=
43 tcc_lddir=
44 confvars=
45 suggest="yes"
46 gcc_major=0
47 gcc_minor=0
48 cc_name="gcc"
49 ar_set=
50 cpu=
51 cpuver=
52 dwarf=
53 targetos=
54 build_cross=
56 # use CC/AR from environment when set
57 test -n "$CC" && cc="$CC"
58 test -n "$AR" && ar="$AR"
60 # set default CFLAGS if unset in environment
61 test -z "$CFLAGS" && CFLAGS="-Wall -O2"
63 # find source path
64 source_path=${0%configure}
65 source_path=${source_path%/}
67 for opt do
68 eval opt=\"$opt\"
69 case "$opt" in
70 --prefix=*) prefix=`echo $opt | cut -d '=' -f 2-`
72 --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2-`
74 --tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2-`
76 --bindir=*) bindir=`echo $opt | cut -d '=' -f 2-`
78 --libdir=*) libdir=`echo $opt | cut -d '=' -f 2-`
80 --includedir=*) includedir=`echo $opt | cut -d '=' -f 2-`
82 --sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2-`
84 --mandir=*) mandir=`echo $opt | cut -d '=' -f 2-`
86 --infodir=*) infodir=`echo $opt | cut -d '=' -f 2-`
88 --docdir=*) docdir=`echo $opt | cut -d '=' -f 2-`
90 --sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2-`
92 --targetos=*) targetos=`echo $opt | cut -d '=' -f 2-`
94 --source-path=*) source_path=`echo $opt | cut -d '=' -f 2-`
96 --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2-`
98 --cc=*) cc=`echo $opt | cut -d '=' -f 2-`
100 --ar=*) ar=`echo $opt | cut -d '=' -f 2-` ; ar_set="yes"
102 --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
104 --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
106 --extra-libs=*) extralibs="${opt#--extra-libs=}"
108 --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2-`
110 --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2-`
112 --crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2-`
114 --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2-`
116 --triplet=*) triplet=`echo $opt | cut -d '=' -f 2-`
118 --cpu=*) cpu=`echo $opt | cut -d '=' -f 2-`
120 --dwarf=*) dwarf=`echo $opt | cut -d '=' -f 2-`
122 --enable-cross) confvars="$confvars cross"
124 --disable-static) confvars="$confvars static=no"
126 --enable-static) confvars="$confvars static"
128 --disable-rpath) confvars="$confvars rpath=no"
130 --debug) confvars="$confvars debug"
132 --with-libgcc) confvars="$confvars libgcc"
134 --with-selinux) confvars="$confvars selinux"
136 --tcc-switches=*) tcc_switches=`echo $opt | cut -d '=' -f 2-`
138 --config-mingw32*) mingw32=$(echo "$opt=yes" | cut -d '=' -f 2)
140 --config-*) confvars="$confvars ${opt#--config-}"; suggest="no"
142 --help|-h) show_help="yes"
144 *) echo "configure: WARNING: unrecognized option $opt"
146 esac
147 done
149 show_help() {
150 cat << EOF
151 Usage: configure [options]
152 Options: [defaults in brackets after descriptions]
154 Standard options:
155 --help print this message
156 --prefix=PREFIX install in PREFIX [$prefix]
157 --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
158 [same as prefix]
159 --bindir=DIR user executables in DIR [EPREFIX/bin]
160 --libdir=DIR object code libraries in DIR [EPREFIX/lib]
161 --tccdir=DIR installation directory [EPREFIX/lib/tcc]
162 --includedir=DIR C header files in DIR [PREFIX/include]
163 --sharedir=DIR documentation root DIR [PREFIX/share]
164 --docdir=DIR documentation in DIR [SHAREDIR/doc/tcc]
165 --mandir=DIR man documentation in DIR [SHAREDIR/man]
166 --infodir=DIR info documentation in DIR [SHAREDIR/info]
168 Advanced options (experts only):
169 --source-path=PATH path of source code [$source_path]
170 --sysroot=PREFIX prepend PREFIX to library/include paths [$sysroot]
171 --cc=CC use C compiler CC [$cc]
172 --ar=AR create archives using AR [$ar]
173 --extra-cflags= specify compiler flags [$CFLAGS]
174 --extra-ldflags= specify linker options [$LDFLAGS]
176 --debug include debug info with resulting binaries
177 --disable-static make libtcc.so instead of libtcc.a
178 --enable-static make libtcc.a instead of libtcc.dll (win32)
179 --disable-rpath disable use of -rpath with libtcc.so
180 --with-libgcc use libgcc_s.so.1 instead of libtcc1.a
181 --with-selinux use mmap for executable memory (tcc -run)
182 --enable-cross build cross compilers (see also 'make help')
184 --sysincludepaths=... specify system include paths, colon separated
185 --libpaths=... specify system library paths, colon separated
186 --crtprefix=... specify locations of crt?.o, colon separated
187 --elfinterp=... specify elf interpreter
188 --triplet=... specify system library/include directory triplet
189 --tcc-switches=... specify implicit switches passed to tcc
191 --config-uClibc,-musl enable system specific configurations
192 --config-mingw32 build on windows using msys, busybox, etc.
193 --config-backtrace=no disable stack backtraces (with -run or -bt)
194 --config-bcheck=no disable bounds checker (-b)
195 --config-predefs=no do not compile tccdefs.h, instead just include
196 --config-new_macho=no|yes Force apple object format (autodetect osx <= 10)
197 --config-codesign=no do not use codesign on apple to sign executables
198 --dwarf=x Use dwarf debug info instead of stabs (x=2..5)
200 Cross build options (experimental):
201 --cpu=CPU target CPU [$cpu]
202 --targetos=... target OS (Darwin,WIN32,Android/Termux) [$targetos]
203 --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix]
205 exit 1
208 default() # set variable unless already set
210 local v
211 eval v=\"\$$1\"
212 test -z "$v" && eval $1=\"$2\"
215 default_conf() # add to confvars unless already present
217 local v=${1%=*}
218 test "${confvars%$v*}" = "${confvars}" && confvars="$confvars $1"
221 if test -z "$source_path" -o "$source_path" = "." ; then
222 source_path=$(pwd)
223 source_path_used="no"
224 else
225 source_path_used="yes"
228 # OS specific
229 buildos=$(uname)
231 case $buildos in
232 Windows_NT|MINGW*|MSYS*|CYGWIN*)
233 buildos="WIN32"
235 Linux)
236 if test "$(uname -o)" = "Android"; then
237 buildos=Android
238 if test -n "$TERMUX_VERSION"; then
239 buildos=Termux
243 esac
245 if test "$mingw32" = "yes"; then
246 default targetos WIN32
247 else
248 default targetos "$buildos"
251 cpu_sys=$(uname -m)
252 default cpu "$cpu_sys"
253 cpu_set="$cpu"
255 # check for crpss build
256 if test "$cpu_set" != "$cpu_sys" \
257 -o "$targetos" != "$buildos" \
258 -o -n "$cross_prefix" ; then
259 build_cross="yes"
260 cc="${cross_prefix}${cc}"
261 ar="${cross_prefix}${ar}"
264 case "$cpu" in
265 x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386)
266 cpu="i386"
268 x86_64|amd64|x86-64)
269 cpu="x86_64"
271 evbarm)
272 case "`uname -p`" in
273 aarch64|arm64)
274 cpu="arm64"
276 earmv*)
277 cpu="arm"
279 esac
281 aarch64|arm64|evbarm)
282 cpu="arm64"
284 arm*)
285 case "$cpu" in
286 arm|armv4l)
287 cpuver=4
289 armv5tel|armv5tejl)
290 cpuver=5
292 armv6j|armv6l)
293 cpuver=6
295 armv7|armv7a|armv7l)
296 cpuver=7
298 esac
299 cpu="arm"
301 alpha)
302 cpu="alpha"
304 "Power Macintosh"|ppc|ppc64)
305 cpu="ppc"
307 mips)
308 cpu="mips"
310 s390)
311 cpu="s390"
313 riscv64)
314 cpu="riscv64"
317 echo "Unsupported CPU"
318 exit 1
320 esac
322 case $targetos in
323 Darwin)
324 dwarf=4
325 confvars="$confvars OSX"
326 default_conf "codesign"
327 DLLSUF=".dylib"
328 if test -z "$build_cross"; then
329 cc=`command -v cc`
330 cc=`readlink $cc || echo clang`
331 tcc_usrinclude="`xcrun --show-sdk-path`/usr/include"
332 if test "${confvars%new_macho*}" = "${confvars}"; then
333 # if new_macho was not specified and (known) ver <= 10, use old (=no)
334 osxver=$(sw_vers -productVersion 2>/dev/null) # X.Y.Z
335 osxver=${osxver%%.*} # major version (or empty on sw_vers error)
336 [ "${osxver:-11}" -ge 11 ] || confvars="$confvars new_macho=no"
339 # on OSX M1 with --cpu=x86_64, build a tcc to run under rosetta entirely
340 if test "$cpu" = "x86_64" -a "$cpu_sys" = "arm64"; then
341 CFLAGS="$CFLAGS -arch $cpu"
342 LDFLAGS="$LDFLAGS -arch $cpu"
345 DragonFly|OpenBSD|FreeBSD|NetBSD)
346 confvars="$confvars BSD ldl=no"
348 Android|Termux)
349 if test "$targetos" = "Termux"; then
350 targetos=Android
351 default sysroot "/data/data/com.termux/files/usr"
352 else
353 default sysroot "/usr"
355 default prefix "${sysroot}"
356 confvars="$confvars Android new-dtags rpath=no"
357 test "${cpu}" != "i386" && confvars="$confvars pie"
358 default_conf "static=no"
359 if test "${cpu}" = "arm"; then
360 default triplet "arm-linux-androideabi"
361 cpuver=7
362 else
363 default triplet "${cpu_set}-linux-android"
365 test "${cpu%64}" != "${cpu}" && S="64" || S=""
366 default tcc_sysincludepaths "{B}/include:{R}/include:{R}/include/${triplet}"
367 default tcc_libpaths "{B}:{R}/lib:/system/lib${S}"
368 default tcc_crtprefix "{R}/lib"
369 default tcc_elfinterp "/system/bin/linker${S}"
370 default tcc_switches "-Wl,-rpath=$sysroot/lib"
372 WIN32)
373 mingw32="yes"
374 confvars="$confvars WIN32"
375 default prefix "C:/Program Files/tcc"
376 default tccdir "${prefix}"
377 default bindir "${tccdir}"
378 default docdir "${tccdir}/doc"
379 default libdir "${tccdir}/libtcc"
380 test "$tccdir" = "$bindir" && tccdir_auto="yes"
381 LIBSUF=".lib"
382 EXESUF=".exe"
383 DLLSUF=".dll"
384 if test "$source_path_used" = "no"; then
385 source_path="."
390 esac
392 if test "$mingw32" = "no"; then
393 default prefix "/usr/local"
394 default execprefix "${prefix}"
395 default libdir "${execprefix}/lib"
396 default bindir "${execprefix}/bin"
397 default tccdir "${libdir}/tcc"
398 default includedir "${prefix}/include"
399 default sharedir "${prefix}/share"
400 default docdir "${sharedir}/doc"
401 default mandir "${sharedir}/man"
402 default infodir "${sharedir}/info"
405 if test x"$show_help" = "xyes" ; then
406 show_help
409 if test -z "$build_cross"; then
410 CONFTEST=./conftest$EXESUF
411 if ! $cc -o $CONFTEST $source_path/conftest.c ; then
412 echo "configure: error: '$cc' failed to compile conftest.c."
413 else
414 cc_name="$($CONFTEST compiler)"
415 gcc_major="$($CONFTEST version)"
416 gcc_minor="$($CONFTEST minor)"
417 bigendian="$($CONFTEST bigendian)"
418 _triplet="$($CONFTEST triplet)"
420 if test "$mingw32" = "no" ; then
421 if test -z "$triplet" -a -n "$_triplet"; then
422 if test -f "/usr/lib/$_triplet/crti.o"; then
423 triplet="$_triplet"
426 if test -z "$triplet"; then
427 if test $cpu = "x86_64" -o $cpu = "arm64" -o $cpu = "riscv64" ; then
428 if test -f "/usr/lib64/crti.o" ; then
429 tcc_lddir="lib64"
433 if test "$suggest" = "yes"; then
434 if test -f "/lib/ld-uClibc.so.0" ; then
435 echo "Perhaps you want ./configure --config-uClibc"
437 if test -f "/lib/ld-musl-$cpu.so.1"; then
438 echo "Perhaps you want ./configure --config-musl"
442 else
443 # can only make guesses about compiler and target
444 if test "${cc%tcc*}" != "$cc"; then
445 cc_name="tcc"
446 elif test "${cc%clang*}" != "$cc"; then
447 cc_name="clang"
449 case $cpu in
450 ppc|mips|s390) bigendian=yes;;
451 esac
452 case $targetos in
453 Linux)
454 default triplet "$cpu_set-linux-gnu"
455 esac
458 if test "$bigendian" = "yes" ; then
459 confvars="$confvars BIGENDIAN"
462 if test "$mingw32" = "yes" -a "$cc_name" = "gcc"; then
463 # avoid mingw dependencies such as 'libgcc_s_dw2-1.dll'
464 default LDFLAGS "-static"
467 if test "$cpu" = "arm"; then
468 if test "${triplet%eabihf}" != "$triplet" ; then
469 confvars="$confvars arm_eabihf arm_vfp"
470 elif test "${triplet%eabi}" != "$triplet" ; then
471 confvars="$confvars arm_eabi arm_vfp"
472 elif test -z "$build_cross"; then
473 if test "${_triplet%eabihf}" != "$_triplet" ; then
474 confvars="$confvars arm_eabihf arm_vfp"
475 elif test "${_triplet%eabi}" != "$_triplet" ; then
476 confvars="$confvars arm_eabi arm_vfp"
477 elif grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then
478 confvars="$confvars arm_vfp"
483 if test -n "$dwarf"; then
484 confvars="$confvars dwarf=$dwarf"
487 # a final configuration tuning
488 if test "$cc_name" != "tcc"; then
489 OPT1="-Wdeclaration-after-statement -fno-strict-aliasing"
490 # we want -Wno- but gcc does not always reject unknown -Wno- options
491 OPT2="-Wpointer-sign -Wsign-compare -Wunused-result -Wformat-truncation"
492 OPT2="$OPT2 -Wstringop-truncation"
493 if test "$cc_name" = "clang"; then
494 OPT1="$OPT1 -fheinous-gnu-extensions"
495 OPT2="$OPT2 -Wstring-plus-int -Wdeprecated-declarations"
497 $cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1
498 for o in $OPT1; do # enable these options
499 if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS $o"; fi
500 done
501 for o in $OPT2; do # disable these options
502 if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS -Wno-${o#-W*}"; fi
503 done
504 # cat cc_msg.txt
505 # echo $CFLAGS
506 rm -f cc_msg.txt a.out
507 else # cc is tcc
508 test "$ar_set" || ar="$cc -ar"
511 fcho() { if test -n "$2"; then echo "$1$2"; fi }
513 fcho "Binary directory " "$bindir"
514 fcho "TinyCC directory " "$tccdir"
515 fcho "Library directory " "$libdir"
516 fcho "Include directory " "$includedir"
517 fcho "Manual directory " "$mandir"
518 fcho "Info directory " "$infodir"
519 fcho "Doc directory " "$docdir"
520 fcho "Target root prefix " "$sysroot"
521 fcho "/usr/include dir " "$tcc_usrinclude"
522 echo "Source path $source_path"
523 echo "Build OS $(uname -m -s)"
524 echo "C compiler $cc ($gcc_major.$gcc_minor)"
525 echo "Target OS $targetos"
526 echo "CPU $cpu"
527 fcho "Triplet " "$triplet"
528 fcho "Libs " "$tcc_libpaths"
529 fcho "Sysinclude " "$tcc_sysincludepaths"
530 fcho "Crt " "$tcc_crtprefix"
531 fcho "Elfinterp " "$tcc_elfinterp"
532 fcho "Switches " "$tcc_switches"
533 fcho "Config " "${confvars# }"
534 echo "Creating config.mak and config.h"
536 cat >config.mak <<EOF
537 # Automatically generated by configure - do not modify
538 prefix=$prefix
539 bindir=\$(DESTDIR)$bindir
540 tccdir=\$(DESTDIR)$tccdir
541 libdir=\$(DESTDIR)$libdir
542 includedir=\$(DESTDIR)$includedir
543 mandir=\$(DESTDIR)$mandir
544 infodir=\$(DESTDIR)$infodir
545 docdir=\$(DESTDIR)$docdir
546 CC=$cc
547 CC_NAME=$cc_name
548 GCC_MAJOR=$gcc_major
549 GCC_MINOR=$gcc_minor
550 AR=$ar
551 CFLAGS=$CFLAGS
552 LDFLAGS=$LDFLAGS
553 LIBSUF=$LIBSUF
554 EXESUF=$EXESUF
555 DLLSUF=$DLLSUF
558 print_inc() {
559 local v="$2"
560 if test -n "$v"; then
561 test "$3" = "num" || v="\"$v\""
562 echo "#ifndef $1" >> $TMPH
563 echo "# define $1 $v" >> $TMPH
564 echo "#endif" >> $TMPH
568 print_mak() {
569 local v="$2"
570 if test -n "$v"; then
571 test "$3" = "num" || v="\"\\\"$v\\\"\""
572 echo "NATIVE_DEFINES+=-D$1=$v" >> config.mak
576 echo "/* Automatically generated by configure - do not modify */" > $TMPH
578 print_inc CONFIG_SYSROOT "$sysroot"
579 test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir"
580 print_inc DWARF_VERSION "$dwarf" num
581 print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"
582 print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
583 print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
584 print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
585 print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
586 print_mak CONFIG_TCC_SWITCHES "$tcc_switches"
587 print_mak CONFIG_LDDIR "$tcc_lddir"
588 print_mak CONFIG_TRIPLET "$triplet"
589 print_mak TCC_CPU_VERSION "$cpuver" num
591 echo "ARCH=$cpu" >> config.mak
592 echo "TARGETOS=$targetos" >> config.mak
593 echo "BUILDOS=$buildos" >> config.mak
595 predefs="1"
596 for v in $confvars ; do
597 test "$v" = "predefs=no" && predefs=""
598 if test "${v%=*}" = "$v"; then
599 echo "CONFIG_$v=yes" >> config.mak
600 else
601 echo "CONFIG_$v" >> config.mak
603 done
604 print_inc CONFIG_TCC_PREDEFS "$predefs" num
606 version=`head $source_path/VERSION`
607 echo "VERSION = $version" >> config.mak
608 echo "#define TCC_VERSION \"$version\"" >> $TMPH
609 echo "@set VERSION $version" > config.texi
611 if test "$source_path_used" = "yes" ; then
612 case $source_path in
613 /*) echo "TOPSRC=$source_path";;
614 *) echo "TOPSRC=\$(TOP)/$source_path";;
615 esac >>config.mak
616 else
617 echo 'TOPSRC=$(TOP)' >>config.mak
619 cat >>$TMPH <<EOF
620 #define GCC_MAJOR $gcc_major
621 #define GCC_MINOR $gcc_minor
622 #define CC_NAME CC_${cc_name}
625 diff $TMPH config.h >/dev/null 2>&1
626 if test $? -ne 0 ; then
627 mv -f $TMPH config.h
628 else
629 echo "config.h is unchanged"
632 rm -f $TMPN* $CONFTEST
634 # ---------------------------------------------------------------------------
635 # build tree in object directory if source path is different from current one
637 fn_makelink()
639 tgt=$1/$2
640 case $2 in
641 */*) dn=${2%/*}
642 test -d $dn || mkdir -p $dn
643 case $1 in
644 /*) ;;
645 *) while test $dn ; do
646 tgt=../$tgt; dn=${dn#${dn%%/*}}; dn=${dn#/}
647 done
649 esac
651 esac
653 ln -sfn $tgt $2 || ( echo "ln failed. Using cp instead."; cp -f $1/$2 $2 )
656 if test "$source_path_used" = "yes" ; then
657 FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile tests/pp/Makefile"
658 for f in $FILES ; do
659 fn_makelink $source_path $f
660 done
663 # ---------------------------------------------------------------------------