3 # Copyright 2005-2006 Timo Hirvonen
5 # This file is licensed under the GPLv2.
11 # LDFLAGS common linker flags for CC
17 # CXXLDFLAGS common linker flags for CXX
19 # Common for C and C++
20 # --------------------
21 # SOFLAGS flags for compiling position independent code (-fPIC)
22 # LDSOFLAGS flags for linking shared libraries
23 # LDDLFLAGS flags for linking dynamically loadable modules
27 printf "checking $@... "
40 # @program: program to check
41 # @name: name of variable where to store the full program name (optional)
43 # returns 0 on success and 1 on failure
46 argc check_program
$# 1 2
47 msg_checking
"for program $1"
48 __cp_file
=`path_find "$1"`
52 test $# -eq 2 && set_var
$2 "$__cp_file"
62 $CC $CFLAGS "$@" -S -o /dev
/null
-x c
/dev
/null
2> /dev
/null
68 $CXX $CXXFLAGS "$@" -S -o /dev
/null
-x c
/dev
/null
2> /dev
/null
72 # @flag: option flag(s) to check
74 # add @flag to EXTRA_CFLAGS if CC accepts it
75 # EXTRA_CFLAGS are added to CFLAGS in the end of configuration
78 argc check_cc_flag
$# 1
80 test -z "$CC" && die
"check_cc_flag: CC not set"
81 msg_checking
"for CFLAGS $*"
84 EXTRA_CFLAGS
="$EXTRA_CFLAGS $*"
93 # @flag: option flag(s) to check
95 # add @flag to EXTRA_CXXFLAGS if CXX accepts it
96 # EXTRA_CXXFLAGS are added to CXXFLAGS in the end of configuration
99 argc check_cxx_flag
$# 1
101 test -z "$CXX" && die
"check_cxx_flag: CXX not set"
102 msg_checking
"for CXXFLAGS $*"
105 EXTRA_CXXFLAGS
="$EXTRA_CXXFLAGS $*"
116 test "$cc_cxx_common_done" && return 0
117 cc_cxx_common_done
=yes
119 var_default SOFLAGS
"-fPIC"
120 var_default LDSOFLAGS
"-shared"
121 var_default LDDLFLAGS
"-shared"
128 common_cf
="$common_cf -I/usr/local/include"
129 common_lf
="$common_lf -L/usr/local/lib"
135 common_cf
="$common_cf -I/sw/include"
136 common_lf
="$common_lf -L/sw/lib"
139 if test -d /opt
/local
/lib
141 common_cf
="$common_cf -I/opt/local/include"
142 common_lf
="$common_lf -L/opt/local/lib"
145 case ${MACOSX_DEPLOYMENT_TARGET} in
147 LDDLFLAGS
="-bundle -flat_namespace -undefined suppress"
150 LDDLFLAGS
="-bundle -undefined dynamic_lookup"
153 LDDLFLAGS
="-bundle -flat_namespace -undefined suppress"
158 common_cf
="$common_cf -D__EXTENSIONS__ -I/usr/local/include"
159 common_lf
="$common_lf -R/usr/local/lib -L/usr/local/lib"
162 makefile_vars SOFLAGS LDSOFLAGS LDDLFLAGS
165 # CC, LD, CFLAGS, LDFLAGS, SOFLAGS, LDSOFLAGS, LDDLFLAGS
168 var_default CC
${CROSS}gcc
170 var_default CFLAGS
"-g -O2 -Wall"
171 var_default LDFLAGS
""
172 check_program
$CC ||
return 1
175 CFLAGS
="$CFLAGS $common_cf"
176 LDFLAGS
="$LDFLAGS $common_lf"
178 makefile_vars CC LD CFLAGS LDFLAGS
183 # CXX, CXXLD, CXXFLAGS, CXXLDFLAGS, SOFLAGS, LDSOFLAGS, LDDLFLAGS
186 var_default CXX
${CROSS}g
++
187 var_default CXXLD
$CXX
188 var_default CXXFLAGS
"-g -O2 -Wall"
189 var_default CXXLDFLAGS
""
190 check_program
$CXX ||
return 1
193 CXXFLAGS
="$CXXFLAGS $common_cf"
194 CXXLDFLAGS
="$CXXLDFLAGS $common_lf"
196 makefile_vars CXX CXXLD CXXFLAGS CXXLDFLAGS
201 # check if CC can generate dependencies (.dep-*.o files)
205 msg_checking
"if CC can generate dependency information"
206 if cc_supports
-MMD -MP -MF /dev
/null
208 EXTRA_CFLAGS
="$EXTRA_CFLAGS -MMD -MP -MF .dep-\$(subst /,-,\$@)"
216 # check if CXX can generate dependencies (.dep-*.o files)
220 msg_checking
"if CXX can generate dependency information"
221 if cxx_supports
-MMD -MP -MF /dev
/null
223 EXTRA_CXXFLAGS
="$EXTRA_CXXFLAGS -MMD -MP -MF .dep-\$(subst /,-,\$@)"
231 # adds AR to config.mk
234 var_default AR
${CROSS}ar
235 var_default ARFLAGS
"-cr"
238 makefile_vars AR ARFLAGS
244 # adds AS to config.mk
247 var_default AS
${CROSS}gcc
258 if test -z "$PKG_CONFIG"
260 if check_program pkg-config PKG_CONFIG
262 makefile_vars PKG_CONFIG
272 # check for library FOO and add FOO_CFLAGS and FOO_LIBS to config.mk
274 # @name: variable prefix (e.g. CURSES -> CURSES_CFLAGS, CURSES_LIBS)
275 # @cflags: CFLAGS for the lib
276 # @libs: LIBS to check
278 # adds @name_CFLAGS and @name_LIBS to config.mk
279 # CFLAGS are not checked, they are assumed to be correct
282 argc check_library
$# 3 3
283 msg_checking
"for ${1}_LIBS ($3)"
287 makefile_var
${1}_CFLAGS
"$2"
288 makefile_var
${1}_LIBS
"$3"
298 # @prefix: variable prefix (e.g. GLIB -> GLIB_CFLAGS, GLIB_LIBS)
299 # @modules: the argument for pkg-config
300 # @cflags: CFLAGS to use if pkg-config failed (optional)
301 # @libs: LIBS to use if pkg-config failed (optional)
303 # if pkg-config fails and @libs are given check_library is called
309 # pkg_config GLIB "glib-2.0 >= 2.2"
316 # GLIB_CFLAGS and GLIB_LIBS are automatically added to Makefile
319 argc pkg_config
$# 2 4
326 msg_checking
"for ${1}_LIBS (pkg-config)"
327 if test "$PKG_CONFIG" != "no" && $PKG_CONFIG --exists "$2" >/dev
/null
2>&1
329 # pkg-config is installed and the .pc file exists
330 __pc_libs
="`$PKG_CONFIG --libs ""$2""`"
331 msg_result
"$__pc_libs"
333 msg_checking
"for ${1}_CFLAGS (pkg-config)"
334 __pc_cflags
="`$PKG_CONFIG --cflags ""$2""`"
335 msg_result
"$__pc_cflags"
337 makefile_var
${1}_CFLAGS
"$__pc_cflags"
338 makefile_var
${1}_LIBS
"$__pc_libs"
342 # no pkg-config or .pc file
345 if test -z "$__pc_libs"
347 if test "$PKG_CONFIG" = "no"
349 # pkg-config not installed and no libs to check were given
350 msg_error
"pkg-config required for $1"
352 # pkg-config is installed but the required .pc file wasn't found
353 $PKG_CONFIG --errors-to-stdout --print-errors "$2" |
sed 's:^:*** :'
358 check_library
"$1" "$__pc_cflags" "$__pc_libs"
370 # @prefix: variable prefix (e.g. ARTS -> ARTS_CFLAGS, ARTS_LIBS)
371 # @program: the -config program
377 # app_config ARTS artsc-config
384 # ARTS_CFLAGS and ARTS_LIBS are automatically added to config.mk
387 argc app_config
$# 2 2
388 check_program
$2 ||
return 1
390 msg_checking
"for ${1}_CFLAGS"
391 __ac_cflags
="`$2 --cflags`"
392 msg_result
"$__ac_cflags"
394 msg_checking
"for ${1}_LIBS"
395 __ac_libs
="`$2 --libs`"
396 msg_result
"$__ac_libs"
398 makefile_var
${1}_CFLAGS
"$__ac_cflags"
399 makefile_var
${1}_LIBS
"$__ac_libs"
403 # @contents: file contents to compile
404 # @cflags: extra cflags (optional)
407 argc try_compile
$# 1
408 case $__check_lang in
410 __src
=`tmp_file prog.c`
411 __obj
=`tmp_file prog.o`
412 echo "$1" > $__src ||
exit 1
414 __cmd
="$CC -c $CFLAGS $@ $__src -o $__obj"
415 $CC -c $CFLAGS "$@" $__src -o $__obj 2>/dev
/null
418 __src
=`tmp_file prog.cc`
419 __obj
=`tmp_file prog.o`
420 echo "$1" > $__src ||
exit 1
422 __cmd
="$CXX -c $CXXFLAGS $@ $__src -o $__obj"
423 $CXX -c $CXXFLAGS "$@" $__src -o $__obj 2>/dev
/null
429 # @contents: file contents to compile and link
430 # @flags: extra flags (optional)
433 argc try_compile
$# 1
434 case $__check_lang in
436 __src
=`tmp_file prog.c`
437 __exe
=`tmp_file prog`
438 echo "$1" > $__src ||
exit 1
440 __cmd
="$CC $CFLAGS $LDFLAGS $@ $__src -o $__exe"
441 $CC $CFLAGS $LDFLAGS "$@" $__src -o $__exe 2>/dev
/null
444 __src
=`tmp_file prog.cc`
445 __exe
=`tmp_file prog`
446 echo "$1" > $__src ||
exit 1
448 __cmd
="$CXX $CXXFLAGS $CXXLDFLAGS $@ $__src -o $__exe"
449 $CXX $CXXFLAGS $CXXLDFLAGS "$@" $__src -o $__exe 2>/dev
/null
455 # optionally used after try_compile or try_compile_link
459 warn
"Failed to compile simple program:"
463 warn
"Command: $__cmd"
464 case $__check_lang in
466 warn
"Make sure your CC and CFLAGS are sane."
469 warn
"Make sure your CXX and CXXFLAGS are sane."
475 # tries to link against a lib
477 # @ldadd: something like -L/usr/X11R6/lib -lX11
480 try_compile_link
"int main(int argc, char *argv[]) { return 0; }" "$@"
486 # @code: simple program code to run
489 try_compile_link
"$1" || __compile_failed
494 # check if the architecture is big-endian
496 # defines WORDS_BIGENDIAN=y/n
499 msg_checking
"byte order"
501 int main(int argc, char *argv[])
507 msg_result
"big-endian"
510 msg_result
"little-endian"
516 # check if @header can be included
519 # @cflags -I/some/path (optional)
522 argc check_header
$# 1
525 msg_checking
"for header <$__header>"
526 if try_compile
"#include <$__header>" "$@"
537 # adds X11_LIBS (and empty X11_CFLAGS) to config.mk
540 for __libs
in "-lX11" "-L/usr/X11R6/lib -lX11"
542 check_library X11
"" "$__libs" && return 0
547 # check posix threads
549 # adds PTHREAD_CFLAGS and PTHREAD_LIBS to config.mk
552 for __libs
in "$PTHREAD_LIBS" -lpthread -lc_r -lkse
554 test -z "$__libs" && continue
555 check_library PTHREAD
"-D_REENTRANT" "$__libs" && return 0
557 echo "using -pthread gcc option"
558 makefile_var PTHREAD_CFLAGS
"-pthread -D_THREAD_SAFE"
559 makefile_var PTHREAD_LIBS
"-pthread"
563 # check dynamic linking loader
565 # adds DL_LIBS to config.mk
568 for DL_LIBS
in "-ldl -Wl,--export-dynamic" "-Wl,--export-dynamic" "-ldl"
570 check_library DL
"" "$DL_LIBS" && return 0
572 echo "assuming -ldl is not needed"
574 makefile_vars DL_LIBS DL_CFLAGS
580 # adds ICONV_CFLAGS and ICONV_LIBS to config.mk
583 check_library ICONV
"" "-liconv" && return 0
585 echo "assuming libc contains iconv"
586 makefile_var ICONV_CFLAGS
""
587 makefile_var ICONV_LIBS
""