3 # Guess values for system-dependent variables and create Makefiles.
5 # Copyright (C) 2017-2020 Zhang Maiyun
7 # This file is part of the slib.
8 # The slib is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU Lesser General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Lesser General Public License for more details.
18 # You should have received a copy of the GNU Lesser General Public License
19 # along with this program. If not, see <https://www.gnu.org/licenses/>.
22 srcdir
="$(dirname "$0")"
23 export CONFIGROOT
="$srcdir/cmake"
24 # shellcheck source=cmake/acconf.sh
25 .
"$srcdir/cmake/acconf.sh"
28 # Plus which, cc, ar and sh, these are minimal requirement to build this project
29 check_progs
cat mkdir cut
sed expr find tr grep uname
install ln make
31 # Default/init value of variables
42 # shellcheck disable=SC2153 # CFLAGS is external
43 cflags
="${CFLAGS} ${CPPFLAGS} -Wall -Wextra -pedantic -O2 -DHAVE_CONFIG_H -I${srcdir}/include -I."
44 # shellcheck disable=SC2153 # LDFLAGS is external
45 ldflags
="-lm ${LDFLAGS}"
49 cd "${srcdir}" || exit 1
50 find src -name '*.c' -type f | tr '\n' ' '
52 hfiles
="$(find "${srcdir}/include
" -name '*.h' -type f | tr '\n' ' ')"
60 # Get constants defined in CMakeLists.txt
62 ver_major
="$(grep SBLLIB_VERSION "${srcdir}/include
/slib
/general.h
" | cut -f3 -d\ )"
63 ver_minor
="$(grep SBLLIB_MINOR "${srcdir}/include
/slib
/general.h
" | cut -f3 -d\ )"
64 ver_pl
="$(grep SBLLIB_PATCHLEVEL "${srcdir}/include
/slib
/general.h
" | cut -f3 -d\ )"
65 version
="${ver_major}.${ver_minor}.${ver_pl}"
66 description
="$(grep PROJECT_DESCRIPTION "${srcdir}/CMakeLists.txt
" | cut -d\" -f2)"
72 Usage: ${srcdir}/configure [option=<value> | --help]
75 --prefix=PREFIX set prefix [/usr/local]
76 --build=BUILD configure for building on BUILD [guessed]
77 --host=HOST cross-compile to build programs to run on HOST [BUILD]
78 --target=TARGET unused
79 --rm=RM file unlinking program(Should support \`-f') [rm]
80 --sosuffix=EXT_WITH_A_DOT set extension of shared objects
81 --exesuffix=EXT_WITH_A_DOT set extension of executables
82 --soflags=SOFLAGS set CFLAGS for shared objects
83 --*dir=DIR set bindir, libdir, includedir, mandir, infodir
84 --help,-h display this help information
87 --disable-abort disable abort on memory failure [enabled]
88 --enable-dmalloc enable dmalloc for debug [disabled]
90 Some influential environment variables:
96 Project maintained by Zhang Maiyun <me@maiyun.me>.
104 eval opt
=\""${opt}"\"
107 prefix
=$
(echo "${opt}" | cut
-d '=' -f 2)
110 cross
=$
(echo "${opt}" | cut
-d '=' -f 2)
112 --build=* |
--target=*) ;;
115 rm=$
(echo "${opt}" | cut
-d '=' -f 2)
118 sosuf
=$
(echo "${opt}" | cut
-d '=' -f 2)
121 exesuf
=$
(echo "${opt}" | cut
-d '=' -f 2)
124 bindir
=$
(echo "${opt}" | cut
-d '=' -f 2)
127 libdir
=$
(echo "${opt}" | cut
-d '=' -f 2)
130 includedir
=$
(echo "${opt}" | cut
-d '=' -f 2)
133 soflags
="${soflags} $(echo "${opt}" | cut -d '=' -f 2)"
151 echo configure
: Warning
: unrecognized option
"${opt}"
156 if [ "$enable_dmalloc" = "on" ]
158 ldflags
="${ldflags} -ldmalloc"
161 echo "Starting configuration for host $(get_proper_triplet "$cross")"
163 # Check for C compiler
169 # exesuf also determined here
179 check_shared_works
"$cross" "$version"
180 if [ "$append_version" = "true" ]
182 if [ "$sosuf" = ".so" ]
184 acsosuf
=${sosuf}.
${ver_major}
186 acsosuf
=.
${ver_major}${sosuf}
190 if check_header
"alloca.h"
197 if check_header
"fcntl.h"
204 if check_header
"stdbool.h"
211 if check_header
"stdint.h"
218 if check_header
"termios.h"
225 if check_header
"unistd.h"
232 if check_type
"size_t"
239 if check_type
"intptr_t"
246 if [ "$libdir" = "" ]
250 if [ "$bindir" = "" ]
254 if [ "$includedir" = "" ]
256 includedir
="$prefix/include"
261 echo Generating Makefile
262 cat >Makefile
<<ACEOF
263 # Makefile generated by configure
264 OBJECTS_SHARED=$(echo "${cfiles}" | sed s/\\.c/.c.shared.o/g)
265 OBJECTS_STATIC=$(echo "${cfiles}" | sed s/\\.c/.c.static.o/g)
270 all: libsbl${acsosuf} libsbl.a sbltool${exesuf}
272 libsbl${acsosuf}: \$(OBJECTS_SHARED)
273 \$(CC) ${soflags} \$(OBJECTS_SHARED) -o \$@ \$(LDFLAGS)
275 libsbl.a: \$(OBJECTS_STATIC)
276 ${ar} rcs ./libsbl.a \$(OBJECTS_STATIC)
278 sbltool${exesuf}: libsbl${acsosuf} ${srcdir}/sbltool.c
279 \$(CC) \$(CFLAGS) -o \$@ ${srcdir}/sbltool.c -L. -lsbl \$(LDFLAGS)
282 install -c -d -m 755 ${bindir} ${libdir} ${includedir} ${includedir}/slib
283 install -c -p -m 644 ${srcdir}/include/slib.h ${includedir}
284 install -c -p -m 644 ${srcdir}/include/slib/*.h ${includedir}/slib
285 install -c -p -m 644 sbl.pc ${libdir}/pkgconfig
286 install -c -p -m 644 libsbl.a ${libdir}
287 install -c -p -m 755 libsbl${acsosuf} ${libdir}
288 ln libsbl${acsosuf} ${libdir}/libsbl${sosuf}
289 install -c -p -m 755 sbltool${exesuf} ${bindir}
291 # The test, always ensure the built version is used
297 ${rm} -rf libsbl.*dylib libsbl.dll libsbl.*so* sbltool sbltool.exe testdrv.exe *.o */*.o *.a
300 -rmdir src testbin 2>/dev/null
301 ${rm} -f Makefile config.mk config.h sbl.pc testdrv.h
303 # Everything below is the same, you will not like to read it line by line
309 for file in "$srcdir"/test
/*-test.c
312 testname
=$
(echo "${file}" |
sed s
,.
*/,, | cut
-f1 -d-)
313 entries
="${entries}{\"${testname}\", ${testname}_main}, "
314 includes
="${includes}#include \"${testname}-test.c\"\\n"
317 echo Generating testdrv.h
318 entries
="${entries}{NULL, NULL}}"
319 # shellcheck disable=SC2059 # the escapes need to be interpreted
320 printf "${includes}" >testdrv.h
321 sed "s/@TESTS@/${entries}/;s,@TFILES@,," "${srcdir}/test/testdrv.h.in" >>testdrv.h
323 echo testdrv.exe
: "${srcdir}/test/testdrv.c" testdrv.h
"${tmp}" libsbl.a
324 echo " \$(CC) \$(CFLAGS) -I${srcdir}/test -I. -o \$@ ${srcdir}/test/testdrv.c libsbl.a \$(LDFLAGS)"
329 echo "${file}.shared.o: ${srcdir}/${file} ${hfiles}"
330 echo " \$(CC) \$(CFLAGS) -fPIC -Dsbl_EXPORTS -c -o \$@ ${srcdir}/${file}"
331 echo "${file}.static.o: ${srcdir}/${file} ${hfiles}"
332 echo " \$(CC) \$(CFLAGS) -c -o \$@ ${srcdir}/${file}"
335 echo Generating sbl.pc
336 sed "s,@CMAKE_INSTALL_PREFIX@,${prefix},;\
337 s,@CMAKE_INSTALL_LIBDIR@,lib,;\
338 s,@CMAKE_INSTALL_INCLUDEDIR@,include,;\
339 s,@PROJECT_NAME@,${name},;\
340 s,@PROJECT_DESCRIPTION@,${description},;\
341 s,@PROJECT_VERSION@,${version},;" \
342 "${srcdir}/sbl.pc.in" >sbl.pc
344 echo Generating config.h
345 if [ $alloca_h -eq 1 ]
347 echo "#define HAVE_ALLOCA_H 1" >config.h
349 echo "#undef HAVE_ALLOCA_H" >config.h
352 if [ $fcntl_h -eq 1 ]
354 echo "#define HAVE_FCNTL_H 1" >>config.h
356 echo "#undef HAVE_FCNTL_H" >>config.h
359 if [ $stdbool_h -eq 1 ]
361 echo "#define HAVE_STDBOOL_H 1" >>config.h
363 echo "#undef HAVE_STDBOOL_H" >>config.h
366 if [ $stdint_h -eq 1 ]
368 echo "#define HAVE_STDINT_H 1" >>config.h
370 echo "#undef HAVE_STDINT_H" >>config.h
373 if [ $termios_h -eq 1 ]
375 echo "#define HAVE_TERMIOS_H 1" >>config.h
377 echo "#undef HAVE_TERMIOS_H" >>config.h
380 if [ $unistd_h -eq 1 ]
382 echo "#define HAVE_UNISTD_H 1" >>config.h
384 echo "#undef HAVE_UNISTD_H" >>config.h
389 echo "#define size_t unsigned int" >>config.h
392 if [ $intptr_t -eq 0 ]
394 echo "#define intptr_t long int" >>config.h
397 if [ "$disable_abort" = "on" ]
399 echo "#define S_NOABRT 1" >>config.h
402 if [ "$enable_dmalloc" = "on" ]
404 echo "#define DMALLOC" >>config.h
405 echo "#define DMALLOC_FUNC_CHECK" >>config.h
408 echo "#define sbl_LIBRARY" >>config.h