Update action steps
[slib.git] / configure.plain
blob4227aa9d0a05f1366c738555c9fcffc347071c23
1 #!/bin/sh
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"
27 # Kept for no reason
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
32 prefix="/usr/local"
33 bindir=""
34 libdir=""
35 includedir=""
37 cross=""
38 cc="${CC}"
39 ar="${AR}"
40 rm="rm"
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}"
46 soflags=""
48 cfiles="$(
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' ' ')"
54 exesuf=""
55 sosuf=""
57 disable_abort=""
58 enable_dmalloc=""
60 # Get constants defined in CMakeLists.txt
61 name="libsbl"
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)"
68 # Help
69 help_me()
71 cat <<EON
72 Usage: ${srcdir}/configure [option=<value> | --help]
74 Options:
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
86 Optional Features:
87 --disable-abort disable abort on memory failure [enabled]
88 --enable-dmalloc enable dmalloc for debug [disabled]
90 Some influential environment variables:
91 AR libmaker
92 CC C compiler
93 CFLAGS C flags
94 LDFLAGS LD flags
96 Project maintained by Zhang Maiyun <me@maiyun.me>.
97 EON
98 exit 0
101 # Parse arguments
102 for opt
104 eval opt=\""${opt}"\"
105 case "${opt}" in
106 --prefix=*)
107 prefix=$(echo "${opt}" | cut -d '=' -f 2)
109 --host=*)
110 cross=$(echo "${opt}" | cut -d '=' -f 2)
112 --build=* | --target=*) ;;
114 --rm=*)
115 rm=$(echo "${opt}" | cut -d '=' -f 2)
117 --sosuffix=*)
118 sosuf=$(echo "${opt}" | cut -d '=' -f 2)
120 --exesuffix=*)
121 exesuf=$(echo "${opt}" | cut -d '=' -f 2)
123 --bindir=*)
124 bindir=$(echo "${opt}" | cut -d '=' -f 2)
126 --libdir=*)
127 libdir=$(echo "${opt}" | cut -d '=' -f 2)
129 --includedir=*)
130 includedir=$(echo "${opt}" | cut -d '=' -f 2)
132 --soflags=*)
133 soflags="${soflags} $(echo "${opt}" | cut -d '=' -f 2)"
135 --disable-abort)
136 disable_abort=on
138 --enable-abort)
139 disable_abort=""
141 --disable-dmalloc)
142 enable_dmalloc=""
144 --enable-dmalloc)
145 enable_dmalloc=on
147 --help | -h)
148 help_me
151 echo configure: Warning: unrecognized option "${opt}"
153 esac
154 done
156 if [ "$enable_dmalloc" = "on" ]
157 then
158 ldflags="${ldflags} -ldmalloc"
161 echo "Starting configuration for host $(get_proper_triplet "$cross")"
163 # Check for C compiler
164 if [ "$cc" = "" ]
165 then
166 check_cc "$cross"
169 # exesuf also determined here
170 check_cc_works
172 # Check for ar
173 if [ "$ar" = "" ]
174 then
175 check_ar "$cross"
178 # For sosuf
179 check_shared_works "$cross" "$version"
180 if [ "$append_version" = "true" ]
181 then
182 if [ "$sosuf" = ".so" ]
183 then
184 acsosuf=${sosuf}.${ver_major}
185 else
186 acsosuf=.${ver_major}${sosuf}
190 if check_header "alloca.h"
191 then
192 alloca_h=1
193 else
194 alloca_h=0
197 if check_header "fcntl.h"
198 then
199 fcntl_h=1
200 else
201 fcntl_h=0
204 if check_header "stdbool.h"
205 then
206 stdbool_h=1
207 else
208 stdbool_h=0
211 if check_header "stdint.h"
212 then
213 stdint_h=1
214 else
215 stdint_h=0
218 if check_header "termios.h"
219 then
220 termios_h=1
221 else
222 termios_h=0
225 if check_header "unistd.h"
226 then
227 unistd_h=1
228 else
229 unistd_h=0
232 if check_type "size_t"
233 then
234 size_t=0
235 else
236 size_t=1
239 if check_type "intptr_t"
240 then
241 intptr_t=0
242 else
243 intptr_t=1
246 if [ "$libdir" = "" ]
247 then
248 libdir="$prefix/lib"
250 if [ "$bindir" = "" ]
251 then
252 bindir="$prefix/bin"
254 if [ "$includedir" = "" ]
255 then
256 includedir="$prefix/include"
259 # Write output
260 mkdir -p src
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)
266 CC=${cc}
267 CFLAGS=${cflags}
268 LDFLAGS=${ldflags}
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)
281 install: all
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
292 test: testdrv.exe
293 ./testdrv.exe
295 .PHONY: clean
296 clean:
297 ${rm} -rf libsbl.*dylib libsbl.dll libsbl.*so* sbltool sbltool.exe testdrv.exe *.o */*.o *.a
299 distclean: clean
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
304 ACEOF
306 tmp=''
307 includes=''
308 entries='{'
309 for file in "$srcdir"/test/*-test.c
311 tmp="${tmp} ${file}"
312 testname=$(echo "${file}" | sed s,.*/,, | cut -f1 -d-)
313 entries="${entries}{\"${testname}\", ${testname}_main}, "
314 includes="${includes}#include \"${testname}-test.c\"\\n"
315 done
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)"
325 } >>Makefile
327 for file in $cfiles
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}"
333 done >> Makefile
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 ]
346 then
347 echo "#define HAVE_ALLOCA_H 1" >config.h
348 else
349 echo "#undef HAVE_ALLOCA_H" >config.h
352 if [ $fcntl_h -eq 1 ]
353 then
354 echo "#define HAVE_FCNTL_H 1" >>config.h
355 else
356 echo "#undef HAVE_FCNTL_H" >>config.h
359 if [ $stdbool_h -eq 1 ]
360 then
361 echo "#define HAVE_STDBOOL_H 1" >>config.h
362 else
363 echo "#undef HAVE_STDBOOL_H" >>config.h
366 if [ $stdint_h -eq 1 ]
367 then
368 echo "#define HAVE_STDINT_H 1" >>config.h
369 else
370 echo "#undef HAVE_STDINT_H" >>config.h
373 if [ $termios_h -eq 1 ]
374 then
375 echo "#define HAVE_TERMIOS_H 1" >>config.h
376 else
377 echo "#undef HAVE_TERMIOS_H" >>config.h
380 if [ $unistd_h -eq 1 ]
381 then
382 echo "#define HAVE_UNISTD_H 1" >>config.h
383 else
384 echo "#undef HAVE_UNISTD_H" >>config.h
387 if [ $size_t -eq 0 ]
388 then
389 echo "#define size_t unsigned int" >>config.h
392 if [ $intptr_t -eq 0 ]
393 then
394 echo "#define intptr_t long int" >>config.h
397 if [ "$disable_abort" = "on" ]
398 then
399 echo "#define S_NOABRT 1" >>config.h
402 if [ "$enable_dmalloc" = "on" ]
403 then
404 echo "#define DMALLOC" >>config.h
405 echo "#define DMALLOC_FUNC_CHECK" >>config.h
408 echo "#define sbl_LIBRARY" >>config.h