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|*solaris*)
28    pic_options='-fPIC'
29    ;;
30i?86-apple-darwin*)
31    pic_options='-fno-common'
32    ;;
33*cygwin*)
34    pic_options=''
35    ;;
36*)
37    dolt_supported=no
38    ;;
39esac
40if test x$dolt_supported = xno ; then
41    AC_MSG_RESULT([no, falling back to libtool])
42    LTCOMPILE='$(LIBTOOL) --tag=CC $(LIBTOOLFLAGS) --mode=compile $(COMPILE)'
43    LTCXXCOMPILE='$(LIBTOOL) --tag=CXX $(LIBTOOLFLAGS) --mode=compile $(CXXCOMPILE)'
44else
45    AC_MSG_RESULT([yes, replacing libtool])
46
47dnl Start writing out doltcompile.
48    cat <<__DOLTCOMPILE__EOF__ >doltcompile
49#!$DOLT_BASH
50__DOLTCOMPILE__EOF__
51    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
52args=("$[]@")
53for ((arg=0; arg<${#args@<:@@@:>@}; arg++)) ; do
54    if test x"${args@<:@$arg@:>@}" = x-o ; then
55        objarg=$((arg+1))
56        break
57    fi
58done
59if test x$objarg = x ; then
60    echo 'Error: no -o on compiler command line' 1>&2
61    exit 1
62fi
63lo="${args@<:@$objarg@:>@}"
64obj="${lo%.lo}"
65if test x"$lo" = x"$obj" ; then
66    echo "Error: libtool object file name \"$lo\" does not end in .lo" 1>&2
67    exit 1
68fi
69objbase="${obj##*/}"
70__DOLTCOMPILE__EOF__
71
72dnl Write out shared compilation code.
73    if test x$enable_shared = xyes; then
74        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
75libobjdir="${obj%$objbase}.libs"
76if test ! -d "$libobjdir" ; then
77    mkdir_out="$(mkdir "$libobjdir" 2>&1)"
78    mkdir_ret=$?
79    if test "$mkdir_ret" -ne 0 && test ! -d "$libobjdir" ; then
80	echo "$mkdir_out" 1>&2
81        exit $mkdir_ret
82    fi
83fi
84pic_object="$libobjdir/$objbase.o"
85args@<:@$objarg@:>@="$pic_object"
86__DOLTCOMPILE__EOF__
87    cat <<__DOLTCOMPILE__EOF__ >>doltcompile
88"\${args@<:@@@:>@}" $pic_options -DPIC || exit \$?
89__DOLTCOMPILE__EOF__
90    fi
91
92dnl Write out static compilation code.
93dnl Avoid duplicate compiler output if also building shared objects.
94dnl
95dnl (2009-03-17 Akiya Jouraku)
96dnl To reduce the compilation time, the static compilation code below
97dnl was modified such that the pic object file generated by the above
98dnl shared compilation code is just copied from .libs/${objbase}.o to
99dnl ${objbase}.o (i.e. the same PIC object file is used for shared and
100dnl static library which supports the PIC object file).
101dnl
102    if test x$enable_static = xyes; then
103        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
104non_pic_object="$obj.o"
105args@<:@$objarg@:>@="$non_pic_object"
106__DOLTCOMPILE__EOF__
107        if test x$enable_shared = xyes; then
108            cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
109cp .libs/${objbase}.o ${objbase}.o || exit $?
110__DOLTCOMPILE__EOF__
111        else
112            cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
113"${args@<:@@@:>@}" || exit $?
114__DOLTCOMPILE__EOF__
115        fi
116    else
117            cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
118cp .libs/${objbase}.o ${objbase}.o || exit $?
119__DOLTCOMPILE__EOF__
120    fi
121
122dnl Write out the code to write the .lo file.
123dnl The second line of the .lo file must match "^# Generated by .*libtool"
124    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
125{
126echo "# $lo - a libtool object file"
127echo "# Generated by doltcompile, not libtool"
128__DOLTCOMPILE__EOF__
129
130    if test x$enable_shared = xyes; then
131        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
132echo "pic_object='.libs/${objbase}.o'"
133__DOLTCOMPILE__EOF__
134    else
135        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
136echo pic_object=none
137__DOLTCOMPILE__EOF__
138    fi
139
140    if test x$enable_static = xyes; then
141        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
142echo "non_pic_object='${objbase}.o'"
143__DOLTCOMPILE__EOF__
144    else
145        cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
146echo non_pic_object=none
147__DOLTCOMPILE__EOF__
148    fi
149
150    cat <<'__DOLTCOMPILE__EOF__' >>doltcompile
151} > "$lo"
152__DOLTCOMPILE__EOF__
153
154dnl Done writing out doltcompile; substitute it for libtool compilation.
155    chmod +x doltcompile
156    LTCOMPILE='$(top_builddir)/doltcompile $(COMPILE)'
157    LTCXXCOMPILE='$(top_builddir)/doltcompile $(CXXCOMPILE)'
158
159dnl automake ignores LTCOMPILE and LTCXXCOMPILE when it has separate CFLAGS for
160dnl a target, so write out a libtool wrapper to handle that case.
161dnl Note that doltlibtool does not handle inferred tags or option arguments
162dnl without '=', because automake does not use them.
163    cat <<__DOLTLIBTOOL__EOF__ > doltlibtool
164#!$DOLT_BASH
165__DOLTLIBTOOL__EOF__
166    cat <<'__DOLTLIBTOOL__EOF__' >>doltlibtool
167top_builddir_slash="${0%%doltlibtool}"
168: ${top_builddir_slash:=./}
169args=()
170modeok=false
171tagok=false
172for arg in "$[]@"; do
173    case "$arg" in
174        --mode=compile) modeok=true ;;
175        --tag=CC|--tag=CXX) tagok=true ;;
176        *) args@<:@${#args[@]}@:>@="$arg" ;;
177    esac
178done
179if $modeok && $tagok ; then
180    . ${top_builddir_slash}doltcompile "${args@<:@@@:>@}"
181else
182    exec ${top_builddir_slash}libtool "$[]@"
183fi
184__DOLTLIBTOOL__EOF__
185
186dnl Done writing out doltlibtool; substitute it for libtool.
187    chmod +x doltlibtool
188    LIBTOOL='$(top_builddir)/doltlibtool'
189fi
190AC_SUBST(LTCOMPILE)
191AC_SUBST(LTCXXCOMPILE)
192# end dolt
193])
194