Build system improvements
[ustl.git] / configure
blob84b476eb2627b4fa141ccf99e647b5652e35604e
1 #! /bin/sh
3 # Copyright (C) 2008 by Mike Sharov <msharov@users.sourceforge.net>
4 # This file is free software, distributed under the MIT License.
6 ######################################################################
7 #### Project Configuration ###########################################
8 ######################################################################
10 PKG_NAME="ustl"
11 PKG_MAJOR="1"
12 PKG_MINOR="2"
14 FILES="Config.mk config.h"
16 PKG_BUGREPORT="Mike Sharov <msharov@users.sourceforge.net>"
18 # Package options
19 COMPONENTS="
21 name=[without-shared]
22 desc=[Builds the shared library (if supported by the OS)]
23 seds=[s/BUILD_SHARED\t:= 1/#BUILD_SHARED\t:= 1/]
25 name=[with-static]
26 desc=[ Builds the static library]
27 seds=[s/#BUILD_STATIC\t:= 1/BUILD_STATIC\t:= 1/]
29 name=[with-debug]
30 desc=[ Compiles the library with debugging information]
31 seds=[s/#DEBUG\t\t:= 1/DEBUG\t\t:= 1/]
33 name=[without-bounds]
34 desc=[Disable runtime bounds checking on stream reads/writes]
35 seds=[s/#define WANT_STREAM_BOUNDS_CHECKING 1/#undef WANT_STREAM_BOUNDS_CHECKING/]
37 name=[without-fastcopy]
38 desc=[Disable specializations for copy/fill]
39 seds=[s/#define WANT_UNROLLED_COPY 1/#undef WANT_UNROLLED_COPY/]
41 name=[without-mmx]
42 desc=[ Disable use of MMX/SSE/3dNow! instructions]
43 seds=[s/#define WANT_MMX 1/#undef WANT_MMX/]
45 name=[with-libstdc++]
46 desc=[Link with libstdc++]
47 seds=[s/#define WITHOUT_LIBSTDCPP 1/#undef WITHOUT_LIBSTDCPP/\n
48 s/#NOLIBSTDCPP\t=/NOLIBSTDCPP\t=/]
49 }";
51 # Header files
52 HEADERS="assert.h ctype.h errno.h fcntl.h float.h inttypes.h limits.h locale.h
53 malloc.h alloca.h memory.h signal.h stdarg.h stddef.h stdint.h stdio.h
54 stdlib.h string.h strings.h sys/stat.h sys/types.h sys/wait.h time.h
55 unistd.h math.h stdlib.h";
57 # Libraries
58 LIBS="supc++ gcc_eh SystemStubs"
60 # First pair is used if nothing matches
61 PROGS="CC=gcc CC=cc CXX=g++ CXX=c++ DOXYGEN=doxygen
62 LD=ld AR=ar RANLIB=ranlib RANLIB=touch INSTALL=install"
64 # Environment variables
65 ENVIRONS="CXXFLAGS LDFLAGS"
67 # Automatic vars
68 PKG_VERSTR="$PKG_MAJOR.$PKG_MINOR"
69 PKG_STRING="$PKG_NAME $PKG_VERSTR"
71 # Miscellaneous substitutions
72 CUSTSUBS="s/@PACKAGE_NAME@/$PKG_NAME/g
73 s/@PACKAGE_VERSION@/$PKG_VERSTR/g
74 s/@PACKAGE_TARNAME@/$PKG_NAME/g
75 s/@PACKAGE_STRING@/$PKG_STRING/g
76 s/@PACKAGE_BUGREPORT@/$PKG_BUGREPORT/g
77 s/@LIBNAME@/$PKG_NAME/g
78 s/@LIB_MAJOR@/$PKG_MAJOR/g
79 s/@LIB_MINOR@/$PKG_MINOR/g
80 s/@LIB_BUILD@/0/g"
82 ######################################################################
83 #### The rest of the file is configuration code. Leave it alone. #####
84 ######################################################################
86 die() { rm -f config.sed config.cpu config.cpu.c; exit; }
88 #### Compile the configurator and generate initial config.sed ########
90 if [ -z "$CC" ]; then
91 for i in gcc g++ cc c++ c89 c99; do
92 CC=`which $i 2> /dev/null` && break
93 done
95 [ -z "$CC" ] && "No C compiler found" && die
97 # Determine gcc private directory
98 PSTDDEF=`echo "#include <stddef.h>"|$CC -E -|grep stddef.h|head -n1|cut -d' ' -f3|cut -d'"' -f2`
99 PINCDIR=`dirname $PSTDDEF`
100 PLIBDIR=`dirname $PINCDIR`
101 if [ -d $PLIBDIR/lib ]; then PLIBDIR=$PLIBDIR/lib; fi
103 # Create and build the C configurator
104 echo '#include <stdio.h>
105 #include <sys/types.h>
106 #include <sys/param.h>
108 #if defined(__GNUC__) && (__i386__ || __x86_64) && !defined(__PIC__)
109 static uint cpuid_supported (void)
111 unsigned long forig, fnew;
112 asm ("pushf\n\tpop\t%0\n\t"
113 "mov\t%0, %1\n\txor\t$0x200000, %0\n\t"
114 "push\t%0\n\tpopf\n\tpushf\n\tpop\t%0"
115 : "=r"(fnew), "=r"(forig));
116 return (fnew != forig);
119 static uint cpuid (void)
121 #define i_cpuid(a,r,c,d) asm("cpuid":"=a"(r),"=c"(c),"=d"(d):"0"(a):"ebx")
122 const uint amdBits = 0xC9480000, extFeatures = 0x80000000, amdExtensions = 0x80000001;
123 uint r, c, d, caps;
124 if (!cpuid_supported()) return (0);
125 i_cpuid (0, r, c, d);
126 if (!r) return (0);
127 i_cpuid (1, r, c, d);
128 caps = (d & ~amdBits);
129 i_cpuid (extFeatures, r, c, d);
130 if (r != extFeatures) {
131 i_cpuid (amdExtensions, r, c, d);
132 caps |= d & amdBits;
134 return (caps);
136 #else
137 static uint cpuid (void) { return (0); }
138 #endif
140 int main (void)
142 typedef struct {
143 char m_Bit;
144 char m_Sub [11];
145 } SCpuCaps;
146 static const short int boCheck = 0x0001;
147 static const char boNames[2][16] = { "BIG", "LITTLE" };
148 static const SCpuCaps s_CpuCaps [] = {
149 { 0, "FPU" },
150 { 2, "EXT_DEBUG" },
151 { 4, "TIMESTAMPC" },
152 { 5, "MSR" },
153 { 8, "CMPXCHG8" },
154 { 9, "APIC" },
155 { 11, "SYSCALL" },
156 { 12, "MTRR" },
157 { 15, "CMOV" },
158 { 16, "FCMOV" },
159 { 22, "SSE " },
160 { 23, "MMX" },
161 { 24, "FXSAVE" },
162 { 25, "SSE " },
163 { 26, "SSE2" },
164 { 30, "EXT_3DNOW" },
165 { 31, "3DNOW" }
167 uint i, caps;
169 #if __GNUC__ >= 3
170 printf ("s/@INLINE_OPTS@/");
171 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
172 printf ("--param max-inline-insns-single=1024"
173 " \\\\\\n\\t\\t--param large-function-growth=65535"
174 " \\\\\\n\\t\\t--param inline-unit-growth=1024");
175 #else
176 printf ("-finline-limit=65535");
177 #endif
178 #if __GNUC__ >= 4
179 printf (" \\\\\\n\\t\\t-fvisibility-inlines-hidden"
180 " \\\\\\n\\t\\t-fno-threadsafe-statics -fno-enforce-eh-specs");
181 #endif
182 printf ("/g\n");
183 #else
184 printf ("s/ \\?@INLINE_OPTS@//g\n"
185 "s/-Wredundant-decls/-Wno-redundant-decls/g\n");
186 #endif
187 #if __GNUC__ != 3
188 printf ("s/ \\?@libgcc_eh@//g\n");
189 #endif
190 #if __i386__
191 printf ("s/ \\?-fPIC//g\n");
192 #endif
193 #if defined(__GNUC__) || defined(__GLIBC_HAVE_LONG_LONG)
194 printf ("s/#undef HAVE_LONG_LONG/#define HAVE_LONG_LONG 1/g\n"
195 "s/#undef SIZE_OF_LONG_LONG/#define SIZE_OF_LONG_LONG %zd/g\n", sizeof(long long));
196 #endif
197 #if defined(__GNUC__) || (__WORDSIZE == 64) || defined(__ia64__)
198 #ifndef BSD
199 printf ("s/#undef HAVE_INT64_T/#define HAVE_INT64_T 1/g\n");
200 #endif
201 #endif
202 #ifndef __APPLE__
203 if (sizeof(size_t) == sizeof(unsigned long) && sizeof(size_t) != sizeof(uint))
204 #endif
205 printf ("s/#undef SIZE_T_IS_LONG/#define SIZE_T_IS_LONG 1/g\n");
206 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
207 printf ("s/#undef HAVE_VECTOR_EXTENSIONS/#define HAVE_VECTOR_EXTENSIONS 1/g\n");
208 #else
209 printf ("s/ \\?-Wshadow//g\n");
210 #endif
211 printf ("s/@BYTE_ORDER@/%s_ENDIAN/g\n"
212 "s/#undef RETSIGTYPE/#define RETSIGTYPE void/g\n"
213 "s/#undef const/\\/\\* #define const \\*\\//g\n"
214 "s/#undef inline/\\/\\* #define inline __inline \\*\\//g\n"
215 "s/#undef off_t/\\/\\* typedef long off_t; \\*\\//g\n"
216 "s/#undef size_t/\\/\\* typedef long size_t; \\*\\//g\n"
217 "s/#undef SIZE_OF_CHAR/#define SIZE_OF_CHAR %zd/g\n"
218 "s/#undef SIZE_OF_SHORT/#define SIZE_OF_SHORT %zd/g\n"
219 "s/#undef SIZE_OF_INT/#define SIZE_OF_INT %zd/g\n"
220 "s/#undef SIZE_OF_LONG/#define SIZE_OF_LONG %zd/g\n"
221 "s/#undef SIZE_OF_POINTER/#define SIZE_OF_POINTER %zd/g\n"
222 "s/#undef SIZE_OF_SIZE_T/#define SIZE_OF_SIZE_T %zd/g\n"
223 "s/#undef LSTAT_FOLLOWS_SLASHED_SYMLINK/#define LSTAT_FOLLOWS_SLASHED_SYMLINK 1/g\n"
224 "s/#undef HAVE_STAT_EMPTY_STRING_BUG/\\/* #undef HAVE_STAT_EMPTY_STRING_BUG *\\//g\n",
225 boNames [(uint)(*((const char*)&boCheck))], sizeof(char),
226 sizeof(short), sizeof(int), sizeof(long), sizeof(void*), sizeof(size_t));
228 caps = cpuid();
229 for (i = 0; i < sizeof(s_CpuCaps)/sizeof(SCpuCaps); ++i)
230 if (caps & (1 << s_CpuCaps[i].m_Bit))
231 printf ("s/#undef CPU_HAS_%s/#define CPU_HAS_%s 1/g\n", s_CpuCaps[i].m_Sub, s_CpuCaps[i].m_Sub);
232 #if __GNUC__ >= 3
233 printf ("s/ \\?@PROCESSOR_OPTS@/");
234 if (caps & (1 << 23))
235 printf (" -mmmx");
236 if (caps & ((1 << 22) | (1 << 25)))
237 printf (" -msse -mfpmath=sse");
238 if (caps & (1 << 26))
239 printf (" -msse2");
240 if (caps & ((1 << 30) | (1 << 31)))
241 printf (" -m3dnow");
242 printf ("/g\n");
243 #else
244 printf ("s/ \\?@PROCESSOR_OPTS@//g\n");
245 #endif
246 return (0);
247 }' >config.cpu.c
248 $CC -o config.cpu config.cpu.c
249 [ ! -x config.cpu ] && echo "Configurator build failed" && die
250 ./config.cpu > config.sed
252 #### Set host-dependent options ######################################
254 SYSNAME=`uname|tr "[A-Z]" "[a-z]"`
255 [ "`uname -m|tr '[A-Z]' '[a-z]'`" = "alpha" ] && SYSNAME="alpha"
256 case "$SYSNAME" in
257 *solaris*| *sun*) SYSNAME="sun";;
258 *darwin*| *osx*) SYSNAME="mac";;
259 *alpha*) SYSNAME="alpha";;
260 *bsd*) SYSNAME="bsd";;
261 *) SYSNAME="linux";;
262 esac
264 if [ "$SYSNAME" = "sun" ]; then
265 echo "s/-Wredundant-decls/-Wno-redundant-decls/g
266 s/@SHBLDFL@/-G/g" >>config.sed
267 else
268 echo "s/#undef HAVE_THREE_CHAR_TYPES/#define HAVE_THREE_CHAR_TYPES 1/g" >>config.sed
270 if [ "$SYSNAME" = "bsd" ]; then
271 echo "s/ \\?@libgcc_eh@//g
272 s/#define WITHOUT_LIBSTDCPP 1/#undef WITHOUT_LIBSTDCPP/g
273 s/NOLIBSTDCPP\\t= -nodefaultlibs /#NOLIBSTDCPP\\t= -nodefaultlibs/g
274 s/-Wredundant-decls/-Wno-redundant-decls/g
275 s/-Winline/-Wno-inline/g
276 s/#define HAVE_VA_COPY 1/#undef HAVE_VA_COPY/g" >>config.sed
278 if [ "$SYSNAME" = "linux" -o "$SYSNAME" = "bsd" ]; then
279 echo "s/@SHBLDFL@/-shared -Wl,-soname=\$1/g" >>config.sed
280 elif [ "$SYSNAME" = "alpha" ]; then
281 echo "s/BUILD_SHARED\\t= 1 /#BUILD_SHARED\\t= 1/g
282 s/#BUILD_STATIC\\t= 1/BUILD_STATIC\\t= 1 /g" >>config.sed
284 if [ "$SYSNAME" = "mac" ]; then
285 echo "s/ \\?@libgcc_eh@//g
286 s/@SYSWARNS@/-Wno-long-double/g
287 s/lib\$1.so/lib\$1.dylib/g
288 s/lib\$1.so.\$\{MAJOR\}.\$\{MINOR\}.\$\{BUILD\}/lib\$1.\$\{MAJOR\}.\$\{MINOR\}.\$\{BUILD\}.dylib/g
289 s/lib\$1.so.\$\{MAJOR\}.\$\{MINOR\}/lib\$1.\$\{MAJOR\}.\$\{MINOR\}.dylib/g
290 s/lib\$1.so.\$\{MAJOR\}/lib\$1.\$\{MAJOR\}.dylib/g
291 s/@SHBLDFL@/-Wl,-single_module -compatibility_version 1 -current_version 1 -install_name \$1 -Wl,-Y,1455 -dynamiclib -mmacosx-version-min=10.4/g" >>config.sed
292 else
293 echo "s/ \\?@SYSWARNS@//g" >>config.sed
295 if [ "$SYSNAME" = "alpha" -o "$SYSNAME" = "mac" ]; then
296 echo "s/#undef SIZE_OF_BOOL/#define SIZE_OF_BOOL SIZE_OF_LONG/g" >>config.sed
297 else
298 echo "s/#undef SIZE_OF_BOOL/#define SIZE_OF_BOOL SIZE_OF_CHAR/g" >>config.sed
300 if [ "$SYSNAME" = "linux" ]; then
301 echo "s/#undef HAVE_RINTF/#define HAVE_RINTF 1/g" >>config.sed
302 else
303 echo "s/ \\?-mfpmath=sse//g" >>config.sed
305 if [ "$SYSNAME" = "mac" -o "$SYSNAME" = "bsd" ]; then
306 echo "s/#define HAVE_STRSIGNAL 1/#undef HAVE_STRSIGNAL/g" >>config.sed
309 #### Printing helper functions #######################################
311 PrintComponents() {
312 local cc=$COMPONENTS
313 echo "Options:"
314 while [ ! -z "$cc" ]; do
315 local name=`expr "$cc" : '[^}]*name=\[\([^]]*\)\]'`
316 local desc=`expr "$cc" : '[^}]*desc=\[\([^]]*\)\]'`
317 echo " --$name $desc"
318 cc=`expr "$cc" : '[^}]*}\(.*\)'`
319 done
320 echo
323 PrintHelp() {
324 echo "This program configures $PKG_STRING to adapt to many kinds of systems.
326 Usage: configure [OPTION]...
328 Configuration:
329 -h, --help display this help and exit
330 -V, --version display version information and exit
332 Installation directories:
333 --prefix=PREFIX architecture-independent files [/usr/local]
334 --exec-prefix=EPREFIX architecture-dependent files [PREFIX]
335 --bindir=DIR user executables [EPREFIX/bin]
336 --sbindir=DIR system admin executables [EPREFIX/sbin]
337 --libexecdir=DIR program executables [EPREFIX/libexec]
338 --datadir=DIR read-only architecture-independent data [PREFIX/share]
339 --sysconfdir=DIR read-only single-machine data [PREFIX/etc]
340 --sharedstatedir=DIR modifiable architecture-independent data [PREFIX/com]
341 --localstatedir=DIR modifiable single-machine data [PREFIX/var]
342 --libdir=DIR object code libraries [EPREFIX/lib]
343 --includedir=DIR C header files [PREFIX/include]
344 --oldincludedir=DIR C header files for non-gcc [/usr/include]
345 --gccincludedir=DIR GCC internal header files [PREFIX/include]
346 --infodir=DIR info documentation [PREFIX/info]
347 --mandir=DIR man documentation [PREFIX/man]
349 System types:
350 --build=BUILD configure for building on BUILD [guessed]
351 --host=HOST cross-compile to build programs to run on HOST [BUILD]
353 PrintComponents
354 echo "Report bugs to $PKG_BUGREPORT."
357 PrintVersion() {
358 echo "$PKG_NAME configure $PKG_VERSTR
360 Copyright (c) 2003-2008, Mike Sharov <msharov@users.sourceforge.net>
361 This configure script is free software under the MIT license.
362 Unlimited permission to copy, distribute, and modify is granted."
365 SubVar() {
366 local esc2=`echo $2 | sed 's/\//\\\&/g'`
367 eval ac_var_$1='$esc2';
368 echo "s/@$1@/$esc2/g" >>config.sed
371 SubComp() {
372 local cc=$COMPONENTS
373 while [ ! -z "$cc" ]; do
374 local name=`expr "$cc" : '[^}]*name=\[\([^]]*\)\]'`
375 local seds=`expr "$cc" : '[^}]*seds=\[\([^]]*\)\]'`
376 [ "$name" = "$1" ] && echo -e $seds >>config.sed
377 cc=`expr "$cc" : '[^}]*}\(.*\)'`
378 done
381 for i in $*; do
382 case $i in
383 --) break;;
384 --version |-V) PrintVersion && die;;
385 --help |-h |-?) PrintHelp && die;;
386 --*=*) SubVar `expr "$i" : '--\([^=]*\)='` `expr "$i" : '[^=]*=\(.*\)'`;;
387 --*) SubComp `expr "$i" : '--\(.*\)'`;;
388 *) echo "Error: unrecognized option \"$i\"" && die;;
389 esac
390 done
392 #### Set directory prefixes ##########################################
394 echo "s/@prefix@/${ac_var_prefix:=\/usr\/local}/g
395 s/@exec_prefix@/${ac_var_exec_prefix:=$ac_var_prefix}/g
396 s/@bindir@/$ac_var_exec_prefix\/bin/g
397 s/@sbindir@/$ac_var_exec_prefix\/sbin/g
398 s/@libexecdir@/$ac_var_exec_prefix\/libexec/g
399 s/@datarootdir@/${ac_var_datarootdir:=$ac_var_prefix\/share}/g
400 s/@datadir@/$ac_var_datarootdir/g
401 s/@sysconfdir@/$ac_var_prefix\/etc/g
402 s/@sharedstatedir@/$ac_var_prefix\/com/g
403 s/@localstatedir@/$ac_var_prefix\/var/g
404 s/@includedir@/${ac_var_includedir:=$ac_var_prefix\/include}/g
405 s/@oldincludedir@/${ac_var_oldincludedir:=\/usr\/include}/g
406 s/@docdir@/${ac_var_docdir:=$ac_var_datarootdir\/doc\/$PKG_NAME}/g
407 s/@infodir@/$ac_var_datarootdir\/info/g
408 s/@htmldir@/$ac_var_docdir/g
409 s/@dvidir@/$ac_var_docdir/g
410 s/@pdfdir@/$ac_var_docdir/g
411 s/@psdir@/$ac_var_docdir/g
412 s/@libdir@/${ac_var_libdir:=$ac_var_exec_prefix\/lib}/g
413 s/@localedir@/$ac_var_datarootdir\/locale/g
414 s/@mandir@/$ac_var_datarootdir\/man/g
415 s/@gccincludedir@/${ac_var_gccincludedir:=`echo $PINCDIR | sed 's/\//\\\&/g'`}/g
416 s/@gcclibdir@/${ac_var_gcclibdir:=`echo $PLIBDIR | sed 's/\//\\\&/g'`}/g
417 s/@customincdir@/${ac_var_customincdir:=$ac_var_prefix\/include}/g
418 s/@customlibdir@/${ac_var_customlibdir:=$ac_var_prefix\/lib}/g" >>config.sed
420 if [ "$ac_var_prefix" != "\/usr\/local" -a "$ac_var_prefix" != "\/usr" ]; then
421 echo "s/ \?@CUSTOMINCDIR@/ -I$ac_var_customincdir/g
422 s/ \?@CUSTOMLIBDIR@/ -L$ac_var_customlibdir/g" >>config.sed
423 else
424 echo "s/ \?@CUSTOMINCDIR@//g
425 s/ \?@CUSTOMLIBDIR@//g" >>config.sed
428 #### Find headers, libs, programs, and subs ##########################
430 SubHeadLibsProgs() {
431 local INCPATH="$ac_var_oldincludedir $ac_var_includedir $ac_var_gccincludedir $ac_var_customincdir"
432 INCPATH=`echo $INCPATH | sed 's/\\\\//g'`
433 for i in $HEADERS; do
434 for p in $INCPATH; do
435 if [ -r "$p/$i" ]; then
436 local hmacn=`echo $i | tr "a-z/.-" "A-Z___"`
437 echo "s/#undef HAVE_$hmacn/#define HAVE_$hmacn 1/" >>config.sed
438 break
440 done
441 done
443 local LIBPATH="`echo $LD_LIBRARY_PATH | tr ':' ' '` $ac_var_libdir $ac_var_gcclibdir $ac_var_customlibdir /usr/lib /usr/local/lib /lib"
444 LIBPATH=`echo $LIBPATH | sed 's/\\\\//g'`
445 local LIBSUFFIX="so a la dylib"
446 for i in $LIBS; do
447 local found=
448 for p in $LIBPATH; do
449 for s in $LIBSUFFIX; do
450 if [ -r "$p/lib$i.$s" ]; then
451 found=" -l$i"
452 break
454 done
455 [ -z "$found" ] || break
456 done
457 echo "s/ \?@lib$i@/$found/g" >>config.sed
458 done
460 for i in $PROGS; do
461 local pname=`expr "$i" : '\([^=]*\)=[^=]*'`
462 local pcall=`expr "$i" : '[^=]*=\([^=]*\)'`
463 [ -x `which $pcall` ] && echo "s/@$pname@/$pcall/g" >>config.sed
464 done
465 # If nothing found in first loop, set the first pair anyway.
466 for i in $PROGS; do
467 local pname=`expr "$i" : '\([^=]*\)=[^=]*'`
468 local pcall=`expr "$i" : '[^=]*=\([^=]*\)'`
469 echo "s/@$pname@/$pcall/g" >>config.sed
470 done
471 # And, finally, the environment variables
472 for i in $ENVIRONS; do
473 local esciv=`eval echo '"'\$\{$i\}'"'`
474 esciv="`echo $esciv | sed 's/\//\\\&/g'`"
475 [ ! -z "$esciv" ] && esciv=" $esciv"
476 echo "s/ \?@$i@/$esciv/g" >>config.sed
477 done
478 echo "$CUSTSUBS" >>config.sed
480 SubHeadLibsProgs
482 #### Apply substitutions to all files ################################
484 for i in $FILES; do
485 sed -f config.sed $i.in > $i
486 done
488 touch config.status
489 cp config.status config.status.old
490 echo "#! /bin/sh
491 $0 $*
492 `tail -n+3 config.status.old`" > config.status
493 chmod u+x config.status
494 rm -f config.status.old