tccelf: avoid find_section() for known section
[tinycc.git] / configure
blobbc0d3e5da5d83553629b26dba2c44967760e2e16
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 darwin=
51 cpuver=
52 dwarf=
54 # use CC from environment when set
55 test -n "$CC" && cc="$CC"
57 # OS specific
58 cpu=`uname -m`
59 cpu_sys="$cpu"
60 targetos=`uname`
61 if test "$targetos" = "Linux" ; then
62 test "$(uname -o)" = "Android" && targetos=Android
65 case $targetos in
66 Darwin)
67 darwin=yes
68 confvars="$confvars OSX dll=no"
69 cc=`which cc`
70 cc=`readlink $cc || echo clang`
71 tcc_usrinclude="`xcrun --show-sdk-path`/usr/include"
72 DLLSUF=".dylib"
74 Windows_NT|MINGW*|MSYS*|CYGWIN*)
75 mingw32=yes
76 targetos=WIN32
78 DragonFly|OpenBSD|FreeBSD|NetBSD)
79 confvars="$confvars BSD ldl=no"
81 Android)
82 confvars="$confvars Android pie new-dtags"
83 if test -n "$TERMUX_VERSION"; then
84 prefix="$PREFIX" # "/data/data/com.termux/files/usr"
85 sysroot="$PREFIX"
86 test "$cpu" = "aarch64" -o "$cpu" = "x86_64" && S="64" || S=""
87 tcc_sysincludepaths="{B}/include:{R}/include/\\\"CONFIG_TRIPLET\\\":{R}/include"
88 tcc_libpaths="{B}:{R}/lib:/system/lib${S}"
89 tcc_crtprefix="{R}/lib"
90 tcc_elfinterp="/system/bin/linker${S}"
91 use_triplet="yes"
92 tcc_switches="-Wl,-rpath=$sysroot/lib,-section-alignment=0x1000"
97 esac
99 # find source path
100 source_path=${0%configure}
101 source_path=${source_path%/}
102 source_path_used="yes"
103 if test -z "$source_path" -o "$source_path" = "." ; then
104 source_path=`pwd`
105 source_path_used="no"
108 for opt do
109 eval opt=\"$opt\"
110 case "$opt" in
111 --prefix=*) prefix=`echo $opt | cut -d '=' -f 2-`
113 --exec-prefix=*) execprefix=`echo $opt | cut -d '=' -f 2-`
115 --tccdir=*) tccdir=`echo $opt | cut -d '=' -f 2-`
117 --bindir=*) bindir=`echo $opt | cut -d '=' -f 2-`
119 --libdir=*) libdir=`echo $opt | cut -d '=' -f 2-`
121 --includedir=*) includedir=`echo $opt | cut -d '=' -f 2-`
123 --sharedir=*) sharedir=`echo $opt | cut -d '=' -f 2-`
125 --mandir=*) mandir=`echo $opt | cut -d '=' -f 2-`
127 --infodir=*) infodir=`echo $opt | cut -d '=' -f 2-`
129 --docdir=*) docdir=`echo $opt | cut -d '=' -f 2-`
131 --sysroot=*) sysroot=`echo $opt | cut -d '=' -f 2-`
133 --source-path=*) source_path=`echo $opt | cut -d '=' -f 2-`
135 --cross-prefix=*) cross_prefix=`echo $opt | cut -d '=' -f 2-`
137 --cc=*) cc=`echo $opt | cut -d '=' -f 2-`
139 --ar=*) ar=`echo $opt | cut -d '=' -f 2-` ; ar_set="yes"
141 --extra-cflags=*) CFLAGS="${opt#--extra-cflags=}"
143 --extra-ldflags=*) LDFLAGS="${opt#--extra-ldflags=}"
145 --extra-libs=*) extralibs="${opt#--extra-libs=}"
147 --sysincludepaths=*) tcc_sysincludepaths=`echo $opt | cut -d '=' -f 2-`
149 --libpaths=*) tcc_libpaths=`echo $opt | cut -d '=' -f 2-`
151 --crtprefix=*) tcc_crtprefix=`echo $opt | cut -d '=' -f 2-`
153 --elfinterp=*) tcc_elfinterp=`echo $opt | cut -d '=' -f 2-`
155 --triplet=*) triplet=`echo $opt | cut -d '=' -f 2-`
157 --cpu=*) cpu=`echo $opt | cut -d '=' -f 2-`
159 --dwarf=*) dwarf=`echo $opt | cut -d '=' -f 2-`
161 --enable-cross) confvars="$confvars cross"
163 --disable-static) confvars="$confvars static=no"
165 --enable-static) confvars="$confvars static"
167 --disable-rpath) confvars="$confvars rpath=no"
169 --debug) confvars="$confvars debug"
171 --with-libgcc) confvars="$confvars libgcc"
173 --with-selinux) confvars="$confvars selinux"
175 --tcc-switches=*) tcc_switches=`echo $opt | cut -d '=' -f 2-`
177 --config-mingw32*) mingw32=$(echo "$opt=yes" | cut -d '=' -f 2)
179 --config-*) confvars="$confvars ${opt#--config-}"; suggest="no"
181 --help|-h) show_help="yes"
183 *) echo "configure: WARNING: unrecognized option $opt"
185 esac
186 done
188 cc="${cross_prefix}${cc}"
189 ar="${cross_prefix}${ar}"
191 # Checking for CFLAGS
192 test -z "$CFLAGS" && CFLAGS="-Wall -O2"
194 # on OSX M1 with --cpu=x86_64, build a tcc to run under rosetta entirely
195 if test "$darwin" = "yes" -a "$cpu" = "x86_64" -a "$cpu_sys" = "arm64"; then
196 CFLAGS="$CFLAGS -arch $cpu"
197 LDFLAGS="$LDFLAGS -arch $cpu"
200 case "$cpu" in
201 x86|i386|i486|i586|i686|i86pc|BePC|i686-AT386)
202 cpu="i386"
204 x86_64|amd64|x86-64)
205 cpu="x86_64"
207 evbarm)
208 case "`uname -p`" in
209 aarch64|arm64)
210 cpu="arm64"
212 earmv*)
213 cpu="arm"
215 esac
217 aarch64|arm64|evbarm)
218 cpu="arm64"
220 arm*)
221 case "$cpu" in
222 arm|armv4l)
223 cpuver=4
225 armv5tel|armv5tejl)
226 cpuver=5
228 armv6j|armv6l)
229 cpuver=6
231 armv7|armv7a|armv7l)
232 cpuver=7
234 esac
235 cpu="arm"
237 alpha)
238 cpu="alpha"
240 "Power Macintosh"|ppc|ppc64)
241 cpu="ppc"
243 mips)
244 cpu="mips"
246 s390)
247 cpu="s390"
249 riscv64)
250 cpu="riscv64"
253 echo "Unsupported CPU"
254 exit 1
256 esac
258 if test "$mingw32" = "yes" ; then
259 if test "$source_path_used" = "no"; then
260 source_path="."
262 test -z "$prefix" && prefix="C:/Program Files/tcc"
263 test -z "$tccdir" && tccdir="${prefix}" && tccdir_auto="yes"
264 test -z "$bindir" && bindir="${tccdir}"
265 test -z "$docdir" && docdir="${tccdir}/doc"
266 test -z "$libdir" && libdir="${tccdir}/libtcc"
267 confvars="$confvars WIN32"
268 LIBSUF=".lib"
269 EXESUF=".exe"
270 DLLSUF=".dll"
271 else
272 if test -z "$prefix" ; then
273 prefix="/usr/local"
275 if test -z "$sharedir" ; then
276 sharedir="${prefix}/share"
278 if test x"$execprefix" = x""; then
279 execprefix="${prefix}"
281 if test x"$libdir" = x""; then
282 libdir="${execprefix}/lib"
284 if test x"$bindir" = x""; then
285 bindir="${execprefix}/bin"
287 if test x"$docdir" = x""; then
288 docdir="${sharedir}/doc"
290 if test x"$mandir" = x""; then
291 mandir="${sharedir}/man"
293 if test x"$infodir" = x""; then
294 infodir="${sharedir}/info"
296 if test x"$tccdir" = x""; then
297 tccdir="${libdir}/tcc"
299 if test x"$includedir" = x""; then
300 includedir="${prefix}/include"
302 fi # mingw32
304 if test x"$show_help" = "xyes" ; then
305 cat << EOF
306 Usage: configure [options]
307 Options: [defaults in brackets after descriptions]
309 Standard options:
310 --help print this message
311 --prefix=PREFIX install in PREFIX [$prefix]
312 --exec-prefix=EPREFIX install architecture-dependent files in EPREFIX
313 [same as prefix]
314 --bindir=DIR user executables in DIR [EPREFIX/bin]
315 --libdir=DIR object code libraries in DIR [EPREFIX/lib]
316 --tccdir=DIR installation directory [EPREFIX/lib/tcc]
317 --includedir=DIR C header files in DIR [PREFIX/include]
318 --sharedir=DIR documentation root DIR [PREFIX/share]
319 --docdir=DIR documentation in DIR [SHAREDIR/doc/tcc]
320 --mandir=DIR man documentation in DIR [SHAREDIR/man]
321 --infodir=DIR info documentation in DIR [SHAREDIR/info]
323 Advanced options (experts only):
324 --source-path=PATH path of source code [$source_path]
325 --cross-prefix=PREFIX use PREFIX for compile tools [$cross_prefix]
326 --sysroot=PREFIX prepend PREFIX to library/include paths [$sysroot]
327 --cc=CC use C compiler CC [$cc]
328 --ar=AR create archives using AR [$ar]
329 --extra-cflags= specify compiler flags [$CFLAGS]
330 --extra-ldflags= specify linker options [$LDFLAGS]
331 --cpu=CPU CPU [$cpu]
333 --debug include debug info with resulting binaries
334 --disable-static make libtcc.so instead of libtcc.a
335 --enable-static make libtcc.a instead of libtcc.dll (win32)
336 --disable-rpath disable use of -rpath with libtcc.so
337 --with-libgcc use libgcc_s.so.1 instead of libtcc1.a
338 --with-selinux use mmap for executable memory (tcc -run)
339 --enable-cross build cross compilers (see also 'make help')
341 --sysincludepaths=... specify system include paths, colon separated
342 --libpaths=... specify system library paths, colon separated
343 --crtprefix=... specify locations of crt?.o, colon separated
344 --elfinterp=... specify elf interpreter
345 --triplet=... specify system library/include directory triplet
346 --tcc-switches=... specify implicit switches passed to tcc
348 --config-uClibc,-musl enable system specific configurations
349 --config-mingw32 build on windows using msys, busybox, etc.
350 --config-backtrace=no disable stack backtraces (with -run or -bt)
351 --config-bcheck=no disable bounds checker (-b)
352 --config-predefs=no do not compile tccdefs.h, instead just include
353 --config-new_macho Use apple new macho object format
354 --config-codesign Use codesign on apple to sign executables
355 --dwarf=x Use dwarf debug info instead of stabs (x=2..5)
357 exit 1
360 if test -z "$cross_prefix" ; then
361 CONFTEST=./conftest$EXESUF
362 if ! $cc -o $CONFTEST $source_path/conftest.c ; then
363 echo "configure: error: '$cc' failed to compile conftest.c."
364 else
365 cc_name="$($CONFTEST compiler)"
366 gcc_major="$($CONFTEST version)"
367 gcc_minor="$($CONFTEST minor)"
368 bigendian="$($CONFTEST bigendian)"
369 _triplet="$($CONFTEST triplet)"
371 if test "$mingw32" = "no" ; then
373 if test -z "$triplet" -a -n "$_triplet"; then
374 if test -f "/usr/lib/$_triplet/crti.o" -o -n "$use_triplet" ; then
375 triplet="$_triplet"
379 if test -z "$triplet"; then
380 if test $cpu = "x86_64" -o $cpu = "arm64" -o $cpu = "riscv64" ; then
381 if test -f "/usr/lib64/crti.o" ; then
382 tcc_lddir="lib64"
387 if test "$cpu" = "arm" ; then
388 if test "${triplet%eabihf}" != "$triplet" ; then
389 confvars="$confvars arm_eabihf arm_vfp"
390 elif test "${triplet%eabi}" != "$triplet" ; then
391 confvars="$confvars arm_eabi arm_vfp"
392 elif test "${_triplet%eabihf}" != "$_triplet" ; then
393 confvars="$confvars arm_eabihf arm_vfp"
394 elif test "${_triplet%eabi}" != "$_triplet" ; then
395 confvars="$confvars arm_eabi arm_vfp"
396 elif grep -s -q "^Features.* \(vfp\|iwmmxt\) " /proc/cpuinfo ; then
397 confvars="$confvars arm_vfp"
401 if test "$suggest" = "yes"; then
402 if test -f "/lib/ld-uClibc.so.0" ; then
403 echo "Perhaps you want ./configure --config-uClibc"
405 if test -f "/lib/ld-musl-$cpu.so.1"; then
406 echo "Perhaps you want ./configure --config-musl"
409 else # mingw32 = yes
410 if test "$cc_name" = "gcc"; then
411 # avoid mingw dependencies such as 'libgcc_s_dw2-1.dll'
412 test -z "$LDFLAGS" && LDFLAGS="-static"
415 else
416 # if cross compiling, cannot launch a program, so make a static guess
417 case $cpu in
418 ppc|mips|s390) bigendian=yes;;
419 esac
422 if test "$bigendian" = "yes" ; then
423 confvars="$confvars BIGENDIAN"
426 # a final configuration tuning
427 if test "$cc_name" != "tcc"; then
428 OPT1="-Wdeclaration-after-statement -fno-strict-aliasing"
429 # we want -Wno- but gcc does not always reject unknown -Wno- options
430 OPT2="-Wpointer-sign -Wsign-compare -Wunused-result -Wformat-truncation"
431 OPT2="$OPT2 -Wstringop-truncation"
432 if test "$cc_name" = "clang"; then
433 OPT1="$OPT1 -fheinous-gnu-extensions"
434 OPT2="$OPT2 -Wstring-plus-int"
436 $cc $OPT1 $OPT2 -o a.out -c -xc - < /dev/null > cc_msg.txt 2>&1
437 for o in $OPT1; do # enable these options
438 if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS $o"; fi
439 done
440 for o in $OPT2; do # disable these options
441 if ! grep -q -- $o cc_msg.txt; then CFLAGS="$CFLAGS -Wno-${o#-W*}"; fi
442 done
443 # cat cc_msg.txt
444 # echo $CFLAGS
445 rm -f cc_msg.txt a.out
446 else # cc is tcc
447 test "$ar_set" || ar="$cc -ar"
450 fcho() { if test -n "$2"; then echo "$1$2"; fi }
452 fcho "Binary directory " "$bindir"
453 fcho "TinyCC directory " "$tccdir"
454 fcho "Library directory " "$libdir"
455 fcho "Include directory " "$includedir"
456 fcho "Manual directory " "$mandir"
457 fcho "Info directory " "$infodir"
458 fcho "Doc directory " "$docdir"
459 fcho "Target root prefix " "$sysroot"
460 fcho "/usr/include dir " "$tcc_usrinclude"
461 echo "Source path $source_path"
462 echo "C compiler $cc ($gcc_major.$gcc_minor)"
463 echo "Target OS $targetos"
464 echo "CPU $cpu"
465 fcho "Triplet " "$triplet"
466 fcho "Config " "${confvars# }"
467 echo "Creating config.mak and config.h"
469 cat >config.mak <<EOF
470 # Automatically generated by configure - do not modify
471 prefix=$prefix
472 bindir=\$(DESTDIR)$bindir
473 tccdir=\$(DESTDIR)$tccdir
474 libdir=\$(DESTDIR)$libdir
475 includedir=\$(DESTDIR)$includedir
476 mandir=\$(DESTDIR)$mandir
477 infodir=\$(DESTDIR)$infodir
478 docdir=\$(DESTDIR)$docdir
479 CC=$cc
480 CC_NAME=$cc_name
481 GCC_MAJOR=$gcc_major
482 GCC_MINOR=$gcc_minor
483 AR=$ar
484 CFLAGS=$CFLAGS
485 LDFLAGS=$LDFLAGS
486 LIBSUF=$LIBSUF
487 EXESUF=$EXESUF
488 DLLSUF=$DLLSUF
491 print_inc() {
492 local v="$2"
493 if test -n "$v"; then
494 test "$3" = "num" || v="\"$v\""
495 echo "#ifndef $1" >> $TMPH
496 echo "# define $1 $v" >> $TMPH
497 echo "#endif" >> $TMPH
501 print_mak() {
502 local v="$2"
503 if test -n "$v"; then
504 test "$3" = "num" || v="\"\\\"$v\\\"\""
505 echo "NATIVE_DEFINES+=-D$1=$v" >> config.mak
509 echo "/* Automatically generated by configure - do not modify */" > $TMPH
511 print_inc CONFIG_SYSROOT "$sysroot"
512 test "$tccdir_auto" = "yes" || print_inc CONFIG_TCCDIR "$tccdir"
513 print_inc DWARF_VERSION "$dwarf" num
514 print_mak CONFIG_USR_INCLUDE "$tcc_usrinclude"
515 print_mak CONFIG_TCC_SYSINCLUDEPATHS "$tcc_sysincludepaths"
516 print_mak CONFIG_TCC_LIBPATHS "$tcc_libpaths"
517 print_mak CONFIG_TCC_CRTPREFIX "$tcc_crtprefix"
518 print_mak CONFIG_TCC_ELFINTERP "$tcc_elfinterp"
519 print_mak CONFIG_TCC_SWITCHES "$tcc_switches"
520 print_mak CONFIG_LDDIR "$tcc_lddir"
521 print_mak CONFIG_TRIPLET "$triplet"
522 print_mak TCC_CPU_VERSION "$cpuver" num
524 echo "ARCH=$cpu" >> config.mak
525 echo "TARGETOS=$targetos" >> config.mak
527 predefs="1"
528 for v in $confvars ; do
529 test "$v" = "predefs=no" && predefs=""
530 if test "${v%=*}" = "$v"; then
531 echo "CONFIG_$v=yes" >> config.mak
532 else
533 echo "CONFIG_$v" >> config.mak
535 done
536 print_inc CONFIG_TCC_PREDEFS "$predefs" num
538 version=`head $source_path/VERSION`
539 echo "VERSION = $version" >> config.mak
540 echo "#define TCC_VERSION \"$version\"" >> $TMPH
541 echo "@set VERSION $version" > config.texi
543 if test "$source_path_used" = "yes" ; then
544 case $source_path in
545 /*) echo "TOPSRC=$source_path";;
546 *) echo "TOPSRC=\$(TOP)/$source_path";;
547 esac >>config.mak
548 else
549 echo 'TOPSRC=$(TOP)' >>config.mak
551 cat >>$TMPH <<EOF
552 #define GCC_MAJOR $gcc_major
553 #define GCC_MINOR $gcc_minor
554 #define CC_NAME CC_${cc_name}
557 diff $TMPH config.h >/dev/null 2>&1
558 if test $? -ne 0 ; then
559 mv -f $TMPH config.h
560 else
561 echo "config.h is unchanged"
564 rm -f $TMPN* $CONFTEST
566 # ---------------------------------------------------------------------------
567 # build tree in object directory if source path is different from current one
569 fn_makelink()
571 tgt=$1/$2
572 case $2 in
573 */*) dn=${2%/*}
574 test -d $dn || mkdir -p $dn
575 case $1 in
576 /*) ;;
577 *) while test $dn ; do
578 tgt=../$tgt; dn=${dn#${dn%%/*}}; dn=${dn#/}
579 done
581 esac
583 esac
585 ln -sfn $tgt $2 || ( echo "ln failed. Using cp instead."; cp -f $1/$2 $2 )
588 if test "$source_path_used" = "yes" ; then
589 FILES="Makefile lib/Makefile tests/Makefile tests/tests2/Makefile tests/pp/Makefile"
590 for f in $FILES ; do
591 fn_makelink $source_path $f
592 done
595 # ---------------------------------------------------------------------------