<?xml version="1.0" ?>
<!--

  This file is part of Bakefile (http://www.bakefile.org)

  Copyright (C) 2003-2008 Vaclav Slavik

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to
  deal in the Software without restriction, including without limitation the
  rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  sell copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  IN THE SOFTWARE.

  $Id: autoconf.bkl 1311 2009-05-31 07:40:33Z vaclavslavik $

-->
<makefile>

    <!-- =============================================================== -->
    <!--                       Configuration:                            -->
    <!-- =============================================================== -->

    <option name="SHARED_LD_CC"/>
    <option name="SHARED_LD_CXX"/>
    <option name="SHARED_LD_MODULE_CC"/>
    <option name="SHARED_LD_MODULE_CXX"/>
    <option name="LIBEXT"/>
    <option name="LIBPREFIX"/>
    <option name="SO_SUFFIX"/>
    <option name="SO_SUFFIX_MODULE"/>
    <option name="DLLIMP_SUFFIX"/>
    <option name="EXEEXT"/>
    <option name="LN_S"/>
    <option name="WINDRES"/>
    <option name="REZ"/>
    <option name="SETFILE"/>
    <option name="PIC_FLAG"/>
    <option name="WINDOWS_IMPLIB"><values>0,1</values></option>
    <option name="USE_SOVERSION"><values>0,1</values></option>
    <option name="USE_SOVERLINUX"><values>0,1</values></option>
    <option name="USE_SOVERSOLARIS"><values>0,1</values></option>
    <option name="USE_SOVERCYGWIN"><values>0,1</values></option>
    <option name="USE_SOTWOSYMLINKS"><values>0,1</values></option>
    <option name="USE_MACVERSION"><values>0,1</values></option>
    <option name="SONAME_FLAG"/>
    <option name="STRIP"/>
    <option name="NM"/>
    <option name="INSTALL"/>
    <option name="INSTALL_PROGRAM"/>
    <option name="INSTALL_DATA"/>
    <option name="INSTALL_DIR"/>

    <option name="DEPS_TRACKING"><values>0,1</values></option>
    <option name="GCC_PCH"><values>0,1</values></option>
    <option name="ICC_PCH"><values>0,1</values></option>
    <option name="ICC_PCH_USE_SWITCH"><values>-use-pch,-pch-use</values></option>
    <option name="USE_PCH"><values>0,1</values></option>
    <option name="BK_DEPS"/>
    <option name="BK_MAKE_PCH"/>

    <!-- Platforms: -->
    <option name="PLATFORM_UNIX"/>
    <option name="PLATFORM_WIN32"/>
    <option name="PLATFORM_MSDOS"/>
    <option name="PLATFORM_MAC"/>
    <option name="PLATFORM_MACOSX"/>
    <option name="PLATFORM_OS2"/>
    <option name="PLATFORM_BEOS"/>

    <option name="srcdir" never_empty="1"/>
    <option name="top_srcdir"/>
    <option name="top_builddir"/>

    <option name="prefix"/>
    <option name="exec_prefix"/>
    <option name="bindir"/>
    <option name="libdir"/>
    <option name="dlldir"/>
    <option name="datarootdir"/>
    <option name="datadir"/>
    <option name="includedir"/>
    <set var="PREFIX">$(prefix)</set>
    <set var="BINDIR">$(bindir)</set>
    <set var="LIBDIR">$(libdir)</set>
    <set var="DLLDIR">$(dlldir)</set>
    <set var="DATADIR">$(datadir)</set>
    <set var="INCLUDEDIR">$(includedir)</set>

    <!-- the initial value of SRCDIR set in dirs.bkl is incorrect for autoconf
         format, so correct it here: -->
    <set var="SRCDIR">$(srcdir)</set>

    <set var="DLLEXT">.$(SO_SUFFIX)</set>
    <set var="DLLEXT_MODULE">.$(SO_SUFFIX_MODULE)</set>
    <set var="DLLIMPEXT">.$(DLLIMP_SUFFIX)</set>
    <option name="DLLPREFIX"/>
    <option name="DLLPREFIX_MODULE"/>

    <option name="LIBS"/>
    <option name="LDFLAGS_GUI"/>
    <set var="__FLAG_EXE_GUI">$(LDFLAGS_GUI)</set>

    <set var="PIC_CFLAGS">$(PIC_FLAG)</set>
    <set var="__DLL_LINKER_CC" eval="0">$(SHARED_LD_CC)</set>
    <set var="__DLL_LINKER_CXX" eval="0">$(SHARED_LD_CXX)</set>
    <set var="__MODULE_LINKER_CC" eval="0">$(SHARED_LD_MODULE_CC)</set>
    <set var="__MODULE_LINKER_CXX" eval="0">$(SHARED_LD_MODULE_CXX)</set>


    <!-- List of additional checks that bakefile.m4 should run: -->
    <set var="AUTOCONF_CHECKS"/>

    <define-global-tag name="autoconf-needs-macro">
        <if cond="value not in AUTOCONF_CHECKS.split()">
            <set var="AUTOCONF_CHECKS" scope="global" append="1">$(value)</set>
        </if>
    </define-global-tag>

    <!-- =============================================================== -->
    <!--                     C/C++ dependency tracking:                  -->
    <!-- =============================================================== -->

    <set var="CCC" eval="0">
        <if cond="DEPS_TRACKING=='1'">$(BK_DEPS) $(CC)</if>
        <if cond="DEPS_TRACKING=='0'">$(CC)</if>
    </set>
    <set var="CXXC" eval="0">
        <if cond="DEPS_TRACKING=='1'">$(BK_DEPS) $(CXX)</if>
        <if cond="DEPS_TRACKING=='0'">$(CXX)</if>
    </set>

    <!-- =============================================================== -->
    <!--                    Load makefile meta-backend:                  -->
    <!-- =============================================================== -->

    <!-- gcc >= 3.4 precompiled headers support, see also below: -->
    <template id="__autoconf_baset">
        <set var="__pch_header"/>
        <set var="__pch_file"/>
    </template>
    <set var="__macexe_baset">__autoconf_baset</set>
    <set var="__dll_baset">__autoconf_baset</set>
    <set var="__lib_baset">__autoconf_baset</set>

    <include file="makefile_macres.bkl"/>
    <set var="__exe_baser">__macexe</set>

    <include file="unixlike.bkl"/>
    <!-- autoconf can be used to compile win32 apps, too: -->
    <include file="makefile_mingw.bkl"/>
    <!-- and OS/2 as well: -->
    <include file="makefile_os2.bkl"/>


    <set var="DESTDIR" make_var="1"></set>

    <!-- =============================================================== -->
    <!--                   Compiler-specific tags:                       -->
    <!-- =============================================================== -->

    <define-tag name="cppflags-unix" rules="exe,lib,dll,module">
        <cppflags>$(value)</cppflags>
    </define-tag>
    <define-tag name="cflags-unix" rules="exe,lib,dll,module">
        <cflags>$(value)</cflags>
    </define-tag>
    <define-tag name="cxxflags-unix" rules="exe,lib,dll,module">
        <cxxflags>$(value)</cxxflags>
    </define-tag>
    <define-tag name="ldflags-unix" rules="exe,dll,module">
        <ldflags>$(value)</ldflags>
    </define-tag>


    <!-- =============================================================== -->
    <!--               Shared libs/DLLs handling:                        -->
    <!-- =============================================================== -->

    <set var="__so_symlinks_cmd"/> <!-- default value -->
    <set var="__so_symlinks_inst_cmd"/> <!-- default value -->
    <set var="__so_symlinks_uninst_cmd"/> <!-- default value -->
    <set var="__mac_instname"/> <!-- default value -->

    <define-tag name="so_version" rules="dll">
        <set var="__soversion1">$(value.split('.')[0])</set>
        <set var="__soversion2">$(value.split('.')[1])</set>
        <set var="__soversion3">$(value.split('.')[2])</set>

        <set var="__linkname1">$(__linkname)</set>

        <set var="__targetsuf2">
            <if cond="USE_SOVERSION=='1' and PLATFORM_MACOSX=='0'">
                $(DLLEXT).$(__soversion1)
            </if>
            <if cond="USE_SOVERSION=='1' and PLATFORM_MACOSX=='1'">
                .$(__soversion1)$(DLLEXT)
            </if>
            <if cond="USE_SOVERSION=='0'">$(DLLEXT)</if>
        </set>
        <set var="__linkname2" eval="0">
            $(DLLPREFIX)$(__dllname)$(__targetsuf2)
        </set>

        <set var="__targetsuf3">
            <if cond="USE_SOVERSION=='1' and PLATFORM_MACOSX=='0' and USE_SOVERCYGWIN=='0'">
                $(DLLEXT).$(__soversion1).$(__soversion2).$(__soversion3)
            </if>
            <if cond="USE_SOVERSION=='1' and PLATFORM_MACOSX=='1'">
                .$(__soversion1).$(__soversion2).$(__soversion3)$(DLLEXT)
            </if>
            <if cond="USE_SOVERSION=='1' and USE_SOVERSOLARIS=='1'">
                $(DLLEXT).$(__soversion1)
            </if>
            <if cond="USE_SOVERSION=='1' and USE_SOVERCYGWIN=='1'">
                -$(__soversion1)$(DLLEXT)
            </if>
            <if cond="USE_SOVERSION=='0'">$(DLLEXT)</if>
        </set>
        <set var="__targetname" eval="0">
            $(DLLPREFIX)$(__dllname)$(__targetsuf3)
        </set>

        <set var="__soname_flags">
            <if cond="USE_SOVERLINUX=='1'"> <!-- Linux -->
                $(SONAME_FLAG)$(__linkname2)
            </if>
            <if cond="USE_SOVERSOLARIS=='1'"> <!-- Solaris -->
                $(SONAME_FLAG)$(__targetname)
            </if>
        </set>
        <ldflags>$(__soname_flags)</ldflags>

        <set var="__so_symlinks_cmd">
            <if cond="USE_SOTWOSYMLINKS=='1'">
                (cd $(pathPrefixToPath(__targetdir));
                $(RM) $(__linkname1) $(__linkname2);
                $(LN_S) $(__targetname) $(__linkname2);
                $(LN_S) $(__linkname2) $(__linkname1))
            </if>
            <if cond="USE_SOVERSOLARIS=='1'">
                (cd $(pathPrefixToPath(__targetdir));
                $(RM) $(__linkname1);
                $(LN_S) $(__targetname) $(__linkname1))
            </if>
        </set>
        <set var="__so_symlinks_inst_cmd">
            <if cond="USE_SOTWOSYMLINKS=='1'">
                $(RM) $(__linkname1) $(__linkname2);
                $(LN_S) $(__targetname) $(__linkname2);
                $(LN_S) $(__linkname2) $(__linkname1)
            </if>
            <if cond="USE_SOVERSOLARIS=='1'">
                $(RM) $(__linkname1);
                $(LN_S) $(__targetname) $(__linkname1);
            </if>
        </set>
        <set var="__so_symlinks_uninst_cmd">
            <if cond="USE_SOTWOSYMLINKS=='1'">
                $(RM) $(__linkname2) $(__linkname1)
            </if>
            <if cond="USE_SOVERSOLARIS=='1'">
                $(RM) $(__linkname1)
            </if>
        </set>
        <__command>$(LF)$(__so_symlinks_cmd)</__command>

        <set var="__mac_instname">$(__linkname2)</set>

        <clean-files>
            $(ref('__targetdir',id))$(ref('__linkname1', id)) $(ref('__targetdir',id))$(ref('__linkname2', id))
        </clean-files>
    </define-tag>

    <define-tag name="mac_version" rules="dll">
        <set var="__macvercur">$(value)</set>
        <set var="__macvercompat">$('.'.join(value.split('.')[0:2]))</set>
        <set var="__macver">
            <if cond="PLATFORM_MACOSX=='1'">
-compatibility_version $(__macvercompat) -current_version $(__macvercur)
            </if>
        </set>
        <ldflags>$(__macver)</ldflags>
    </define-tag>

    <define-tag name="install-to" rules="dll">
        <set var="__macinstname" eval="0">
            $(ifthenelse(__mac_instname=='',ref('__targetname',id),ref('__mac_instname',id)))
        </set>
        <set var="__macinstnamecmd">
            <if cond="PLATFORM_MACOSX=='1'">
                -install_name $(value)/$(__macinstname)
            </if>
        </set>
        <ldflags>$(__macinstnamecmd)</ldflags>

        <modify-target target="install_$(id)">
            <command>(cd $(DESTDIR)$(value) ; $(ref('__so_symlinks_inst_cmd',id)))</command>
        </modify-target>
        <modify-target target="uninstall_$(id)">
            <command>(cd $(DESTDIR)$(value) ; $(ref('__so_symlinks_uninst_cmd',id)))</command>
        </modify-target>
    </define-tag>

    <define-tag name="libname" rules="dll">
        <set var="__importlib">
            <if cond="WINDOWS_IMPLIB=='1'">-Wl,--out-implib=$(__targetdir)$(__linkname)</if>
            <if cond="PLATFORM_OS2=='1'">-import $(__targetdir)$(__linkname)</if>
        </set>
        <ldflags>$(__importlib)</ldflags>
    </define-tag>


    <define-tag name="library" rules="exe,dll,module">
        <!--
         The standard version which adds full filename of the library to
         linker's command line  doesn't work on BSD Unix where the linker
         doesn't like being given shared library as e.g. lib/foo.so - it's then
         recorded as lib/foo.so instead of foo.so in ELF binary.

         For details, see here:
         http://sourceforge.net/tracker/?func=detail&aid=1009662&group_id=83016&atid=568029
         -->

        <!-- It has to be inserted as the first -L, so the we won't
             link against library version in some other directory: -->
        <set var="__ldflags" prepend="1">
            $(substitute(value,
                         lambda x: '-L'+ref('__targetdir_dironly', x),
                         'LIBR'))
        </set>
        <ldlibs>
            $(substitute(value,
                         lambda x: '-l'+ref('__libname', x),
                         'LIBR'))
        </ldlibs>
    </define-tag>


    <!-- =============================================================== -->
    <!--              GCC >= 3.4 precompiled headers support:            -->
    <!-- =============================================================== -->

    <tag-info name="precomp-headers-file"
              position="before:precomp-headers-header"/>
    <tag-info name="precomp-headers-header"
        position="before:precomp-headers"/>

    <define-tag name="precomp-headers-file" rules="__compiled">
        <set var="__pch_file">$(value)</set>
    </define-tag>
    <define-tag name="precomp-headers-header" rules="__compiled">
        <set var="__pch_header">$(value)</set>
    </define-tag>

    <define-tag name="precomp-headers" rules="__compiled">
        <if cond="value=='on'">
            <autoconf-needs-macro>AC_BAKEFILE_PRECOMP_HEADERS</autoconf-needs-macro>
            <if cond="__pch_header==''">
                <error>
                Precompiled header must explicitly be provided using &lt;precomp-headers-header&gt; for Autoconf backend.
                </error>
            </if>

            <set var="__gcc_pch_dir">
                $(BUILDDIR)/.pch$(addPrefixIfNotEmpty('/', __pch_file))
            </set>
            <set var="__gcc_pch_file">
                $(__gcc_pch_dir)/$(__pch_header).gch
            </set>
            <!-- must look into this directory first: -->
            <set var="PCH_INC">
                <if cond="GCC_PCH=='1'">-I$(__gcc_pch_dir)</if>
                <if cond="ICC_PCH=='1'">$(ICC_PCH_USE_SWITCH) $(__gcc_pch_file)</if>
            </set>
            <set var="__cppflags">$(PCH_INC) $(__cppflags)</set>

            <set var="__gcc_pch_cmd" eval="0">
                $(ifthenelse(__uses_cxx,CXX,CC)+' '+ifthenelse(__uses_cxx,
                             '$(%s_CXXFLAGS)' % id.upper(),
                             '$(%s_CFLAGS)' % id.upper()))
            </set>

            <if cond="__gcc_pch_file not in targets">
                <add-target target="$(__gcc_pch_file)"
                            type="action" cond="USE_PCH=='1'"/>
                <modify-target target="$(__gcc_pch_file)">
                    <command>
                        $(BK_MAKE_PCH) $(__gcc_pch_file) $(__pch_header) $(ref("__gcc_pch_cmd",id))
                    </command>
                </modify-target>
            </if>

            <objects-depend>$(__gcc_pch_file)</objects-depend>
        </if>
    </define-tag>



    <!-- =============================================================== -->
    <!--                     "make install-strip":                       -->
    <!-- =============================================================== -->

    <action id="install-strip">
        <depends>install</depends>
    </action>

    <define-tag name="install-to" rules="dll,exe,module">
        <modify-target target="install-strip">
            <command>$(STRIP) $(DESTDIR)$(value)/$(ref('__targetname',id))</command>
        </modify-target>
    </define-tag>

    <tag-info name="installable" position="before:dir"/>

    <define-tag name="installable" rules="subproject">
        <set var="__installable">$(value)</set>
    </define-tag>
    <define-tag name="dir" rules="subproject">
            <if cond="__installable!='no'">
           <!-- create the (conditional) install-strip_$(id) target and add it, as a dependency,
                to the global INSTALL-STRIP target of the makefile -->
           <add-target target="install-strip_$(id)"
                            type="inst-action"
                            cond="target$(addPrefixIfNotEmpty(' and ',__install_if_cond))"/>
           <modify-target target="install-strip_$(id)">
                <command>(cd $(value) &amp;&amp; $(DOLLAR)(MAKE) install-strip)</command>
               <dependency-of>install-strip</dependency-of>
        </modify-target>
        </if>
    </define-tag>


    <!-- =============================================================== -->
    <!--                         "make clean":                           -->
    <!-- =============================================================== -->

    <action id="clean" template="__phony">
        <set var="__builddir" eval="0">$(BUILDDIR)</set>
        <command>
            rm -rf $(__builddir)/.deps $(__builddir)/.pch
            $(RM) $(__builddir)/*$(OBJEXT)
        </command>
    </action>

    <!-- =============================================================== -->
    <!--                       "make distclean":                         -->
    <!-- =============================================================== -->

    <action id="distclean" template="__phony">
        <depends>clean</depends>
        <command>
            $(RM) config.cache config.log config.status bk-deps bk-make-pch shared-ld-sh Makefile
        </command>
    </action>

    <!-- the <dir> tag needs to be specified for any <subproject> so we
         use it to add to "distclean" target the additional rm command -->
    <define-tag name="dir" rules="subproject">
        <set var="__subdir">$(nativePaths(value))</set>
        <modify-target target="distclean">
            <command>
                -(cd $(value) &amp;&amp; $(DOLLAR)(MAKE) distclean)
            </command>
        </modify-target>
    </define-tag>


    <!-- =============================================================== -->
    <!--                       Generate output:                          -->
    <!-- =============================================================== -->

    <!-- see format_features.bkl for explanation: -->
    <set var="FORMAT_OUTPUT_VARIABLES">
        __targetdir __targetname __deps __command __is_phony
        AUTOCONF_CHECKS DESTDIR
    </set>
    <set var="FORMAT_NEEDS_OPTION_VALUES_FOR_CONDITIONS">0</set>

    <set var="AUTOCONF_MACROS_FILE">
        $(os.path.join(os.path.dirname(OUTPUT_FILE),'autoconf_inc.m4'))
    </set>

    <output file="$(OUTPUT_FILE)" writer="autoconf.empy"/>

    <if cond="AUTOCONF_MACROS_FILE!=''">
        <output file="$(AUTOCONF_MACROS_FILE)"
                writer="autoconf-macros.empy"
                method="mergeBlocksWithFilelist"/>
    </if>

</makefile>
