1dnl dolt, a replacement for libtool
2dnl Copyright © 2007-2008 Josh Triplett <josh@freedesktop.org>
3dnl Copying and distribution of this file, with or without modification,
4dnl are permitted in any medium without royalty provided the copyright
5dnl notice and this notice are preserved.
6dnl
7dnl To use dolt, invoke the DOLT macro immediately after the libtool macros.
8dnl Optionally, copy this file into acinclude.m4, to avoid the need to have it
9dnl installed when running autoconf on your project.
10
11AC_DEFUN([DOLT], [
12AC_REQUIRE([AC_CANONICAL_HOST])
13# dolt, a replacement for libtool
14# Josh Triplett <josh@freedesktop.org>
15AC_PATH_PROG(DOLT_BASH, bash)
16AC_MSG_CHECKING([if dolt supports this host])
17dolt_supported=yes
18if test x$DOLT_BASH = x; then
19    dolt_supported=no
20fi
21if test x$GCC != xyes; then
22    dolt_supported=no
23fi
24case $host in
25i?86-*-linux*|x86_64-*-linux*|powerpc-*-linux* \
26|amd64-*-freebsd*|i?86-*-freebsd*|ia64-*-freebsd*)
27    pic_options='-fPIC'
28    ;;
29i?86-apple-darwin*)
30    pic_options='-fno-common'
31    ;;
32*)
33    dolt_supported=no
34    ;;
35esac
36if test x$dolt_supported = xno ; then
37    AC_MSG_RESULT([no, falling back to libtool])
38    LTCOMPILE='$(LIBTOOL) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(COMPILE)'
39    LTCXXCOMPILE='$(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CXXCOMPILE)'
40else
41    AC_MSG_RESULT([yes, replacing libtool])
42
43dnl Start writing out doltcompile.
44    cat <<__DOLTCOMPILE__EOF__ >doltcompile
45#!$DOLT_BASH
46__DOLTCOMPILE__EOF__
47    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
48args=("$[]@")
49for ((arg=0; arg<${#args@<:@@@:>@}; arg++)) ; do
50    if test x"${args@<:@$arg@:>@}" = x-o ; then
51        objarg=$((arg+1))
52        break
53    fi
54done
55if test x$objarg = x ; then
56    echo 'Error: no -o on compiler command line' 1>&2
57    exit 1
58fi
59lo="${args@<:@$objarg@:>@}"
60obj="${lo%.lo}"
61if test x"$lo" = x"$obj" ; then
62    echo "Error: libtool object file name \"$lo\" does not end in .lo" 1>&2
63    exit 1
64fi
65objbase="${obj##*/}"
66__DOLTCOMPILE__EOF__
67
68dnl Write out shared compilation code.
69    if test x$enable_shared = xyes; then
70        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
71libobjdir="${obj%$objbase}.libs"
72if test ! -d "$libobjdir" ; then
73    mkdir_out="$(mkdir "$libobjdir" 2>&1)"
74    mkdir_ret=$?
75    if test "$mkdir_ret" -ne 0 && test ! -d "$libobjdir" ; then
76	echo "$mkdir_out" 1>&2
77        exit $mkdir_ret
78    fi
79fi
80pic_object="$libobjdir/$objbase.o"
81args@<:@$objarg@:>@="$pic_object"
82__DOLTCOMPILE__EOF__
83    cat <<__DOLTCOMPILE__EOF__ >>doltcompile
84"\${args@<:@@@:>@}" $pic_options -DPIC || exit \$?
85__DOLTCOMPILE__EOF__
86    fi
87
88dnl Write out static compilation code.
89dnl Avoid duplicate compiler output if also building shared objects.
90    if test x$enable_static = xyes; then
91        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
92non_pic_object="$obj.o"
93args@<:@$objarg@:>@="$non_pic_object"
94__DOLTCOMPILE__EOF__
95        if test x$enable_shared = xyes; then
96            cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
97"${args@<:@@@:>@}" >/dev/null 2>&1 || exit $?
98__DOLTCOMPILE__EOF__
99        else
100            cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
101"${args@<:@@@:>@}" || exit $?
102__DOLTCOMPILE__EOF__
103        fi
104    fi
105
106dnl Write out the code to write the .lo file.
107dnl The second line of the .lo file must match "^# Generated by .*libtool"
108    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
109{
110echo "# $lo - a libtool object file"
111echo "# Generated by doltcompile, not libtool"
112__DOLTCOMPILE__EOF__
113
114    if test x$enable_shared = xyes; then
115        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
116echo "pic_object='.libs/${objbase}.o'"
117__DOLTCOMPILE__EOF__
118    else
119        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
120echo pic_object=none
121__DOLTCOMPILE__EOF__
122    fi
123
124    if test x$enable_static = xyes; then
125        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
126echo "non_pic_object='${objbase}.o'"
127__DOLTCOMPILE__EOF__
128    else
129        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
130echo non_pic_object=none
131__DOLTCOMPILE__EOF__
132    fi
133
134    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
135} > "$lo"
136__DOLTCOMPILE__EOF__
137
138dnl Done writing out doltcompile; substitute it for libtool compilation.
139    chmod +x doltcompile
140    LTCOMPILE='$(top_builddir)/doltcompile $(COMPILE)'
141    LTCXXCOMPILE='$(top_builddir)/doltcompile $(CXXCOMPILE)'
142
143dnl automake ignores LTCOMPILE and LTCXXCOMPILE when it has separate CFLAGS for
144dnl a target, so write out a libtool wrapper to handle that case.
145dnl Note that doltlibtool does not handle inferred tags or option arguments
146dnl without '=', because automake does not use them.
147    cat <<__DOLTLIBTOOL__EOF__ > doltlibtool
148#!$DOLT_BASH
149__DOLTLIBTOOL__EOF__
150    cat <<'__DOLTLIBTOOL__EOF__' >>doltlibtool
151top_builddir_slash="${0%%doltlibtool}"
152: ${top_builddir_slash:=./}
153args=()
154modeok=false
155tagok=false
156for arg in "$[]@"; do
157    case "$arg" in
158        --silent) ;;
159        --mode=compile) modeok=true ;;
160        --tag=CC|--tag=CXX) tagok=true ;;
161        *) args@<:@${#args[@]}@:>@="$arg" ;;
162    esac
163done
164if $modeok && $tagok ; then
165    . ${top_builddir_slash}doltcompile "${args@<:@@@:>@}"
166else
167    exec ${top_builddir_slash}libtool "$[]@"
168fi
169__DOLTLIBTOOL__EOF__
170
171dnl Done writing out doltlibtool; substitute it for libtool.
172    chmod +x doltlibtool
173    LIBTOOL='$(top_builddir)/doltlibtool'
174fi
175AC_SUBST(LTCOMPILE)
176AC_SUBST(LTCXXCOMPILE)
177# end dolt
178])
179