#!/usr/bin/perl -w
#
# Update the dll dependencies in the dlls main Makefile.in.
# Must be run in the dlls/ directory of the Wine tree.
#
# Copyright 2001 Alexandre Julliard
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

use strict;

my $makefiles = `find . -name Makefile.in -print`;

my %directories = ();
my %implib_dirs = ();
my %altnames = ();

# list of special dlls that can be switched on or off by configure
my %special_dlls =
(
  "ddraw"    => "XFILES",
  "glu32"    => "GLU32FILES",
  "glut32"   => "GLUT32FILES",
  "opengl32" => "OPENGLFILES",
  "d3d8"     => "OPENGLFILES",
  "d3d9"     => "OPENGLFILES",
  "d3dx8"    => "OPENGLFILES",
  "wined3d"  => "OPENGLFILES",
  "x11drv"   => "XFILES"
);

foreach my $i (split(/\s/,$makefiles))
{
    my $module;

    next if $i =~ /\/tests\/Makefile.in/;

    open MAKE,$i;

    $module = undef;
    while (<MAKE>)
    {
        chop;
        # EPP hack to disable this DLL... the MKDLL_SKIP comment must appear
        # at the very top of the Makefile.in
        last if (/^\#\s*MKDLL_SKIP/);

        if (/^MODULE\s*=\s*([a-zA-Z0-9_.]+)/)
        {
            $module = $1;
            if ($module =~ /^lib.*\.a$/)
            {
                ($implib_dirs{$module} = $i) =~ s/^\.\/(.*)\/[^\/]+$/$1/;
            }
            else
            {
                ($directories{$module} = $i) =~ s/^\.\/(.*)\/[^\/]+$/$1/;
            }
            next;
        }
        if (/^SPEC_SRCS16\s*=\s*(.*)/)
        {
            my $specs = $1;
            while ($specs =~ /\s*(.*)\\$/) { $specs = $1 . <MAKE>; }
            my @list = split(/\s+/,$specs);
            @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @list;
            $altnames{$module} = \@list;
            next;
        }
    }
    close MAKE;
}

open NEWMAKE,">Makefile.in.new" or die "cannot create Makefile.in.new";

################################################################
# makefile header

print NEWMAKE <<EOF;
# Automatically generated by make_dlls; DO NOT EDIT!!

TOPSRCDIR = \@top_srcdir\@
TOPOBJDIR = ..
SRCDIR    = \@srcdir\@
VPATH     = \@srcdir\@

EOF

################################################################
# output special dlls configure definitions

printf NEWMAKE "# special configure-dependent targets\n\n";
my %specials = ();
foreach my $mod (sort keys %special_dlls)
{
    $specials{$special_dlls{$mod}} .= " " . $mod;
}
foreach my $i (sort keys %specials)
{
    printf NEWMAKE "%s =%s\n", $i, $specials{$i};
}
printf NEWMAKE "EXTRADIRS =";
foreach my $i (sort keys %specials) { printf NEWMAKE " \@%s\@", $i; }
printf NEWMAKE "\n\n";


################################################################
# output the subdirs list

print NEWMAKE "# Subdir list\n\nBASEDIRS =";
foreach my $dir (sort values %directories)
{
    next if defined($special_dlls{$dir});  # skip special dlls
    printf NEWMAKE " \\\n\t%s", $dir;
}

printf NEWMAKE "\n\nSUBDIRS = \\\n\t\$(BASEDIRS)";
foreach my $dir (sort (keys %special_dlls, values %implib_dirs))
{
    printf NEWMAKE " \\\n\t%s", $dir;
}
printf NEWMAKE <<EOF;


BUILDSUBDIRS = \$(BASEDIRS) \$(EXTRADIRS)

INSTALLSUBDIRS = \$(BUILDSUBDIRS)
EOF

################################################################
# output the all: target

my %targets = ();  # use a hash to get rid of duplicate target names
my %targets16 = ();
foreach my $mod (sort keys %directories)
{
    next if defined($special_dlls{$directories{$mod}});  # skip special dlls
    $targets{$mod . ".so"} = 1;
    next unless defined $altnames{$mod};
    foreach my $i (sort @{$altnames{$mod}})
    {
        $targets16{sprintf("%s.so",$i)} = 1;
    }
}
foreach my $mod (sort keys %implib_dirs) { $targets{$mod} = 1; }

print NEWMAKE <<EOF;

\@MAKE_RULES\@

# Symbolic links

WIN16_FILES = \\
EOF
printf NEWMAKE "\t%s\n", join( " \\\n\t", sort keys %targets16 );

print NEWMAKE <<EOF;

SYMLINKS_SO = \\
	\$(EXTRADIRS:%=%.dll.so) \\
	\@WIN16_FILES\@ \\
EOF
printf NEWMAKE "\t%s\n", join( " \\\n\t", sort keys %targets );

print NEWMAKE <<EOF;

# Main target

all: symlinks\$(DLLEXT)

.PHONY: symlinks symlinks.so implib

symlinks.so: \$(SYMLINKS_SO)

symlinks: \$(BUILDSUBDIRS)

EOF

################################################################
# output the lib name -> directory rules

print NEWMAKE <<EOF;

# Map symlink name to the corresponding library

EOF

foreach my $mod (sort keys %directories)
{
    printf NEWMAKE "%s.so: %s/%s.so\n", $mod, $directories{$mod}, $mod;
    printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s/%s.so \$@\n\n", $directories{$mod}, $mod;

    if (defined $altnames{$mod})
    {
        my $count = 0;
        foreach my $i (sort @{$altnames{$mod}})
        {
            if ($count++ == 3) { printf NEWMAKE "\\\n  "; $count = 1; }
            printf NEWMAKE "%s.so ", $i;
        }
        printf NEWMAKE ": %s.so\n", $mod;
        printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s.so \$@\n\n", $mod;
    }
}
foreach my $mod (sort keys %implib_dirs)
{
    printf NEWMAKE "%s: %s/%s\n", $mod, $implib_dirs{$mod}, $mod;
    printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s/%s \$@\n\n", $implib_dirs{$mod}, $mod;
}

################################################################
# output the import libraries rules

my @implibs = grep /\.dll$/, keys %directories;
push @implibs, "winspool.drv";

print NEWMAKE "\n# Import libraries\n\nIMPORT_LIBS =";
foreach my $mod (sort @implibs)
{
    my $def = $mod;
    $def =~ s/\.(dll|drv)$//;
    printf NEWMAKE " \\\n\tlib%s.\$(IMPLIBEXT)", $def;
}
foreach my $mod (sort keys %implib_dirs)
{
    printf NEWMAKE " \\\n\t%s", $mod;
}
print NEWMAKE "\n\n";
print NEWMAKE "implib: \$(IMPORT_LIBS)\n\n";

foreach my $mod (sort @implibs)
{
    my $dir = $directories{$mod};
    my $def = $mod;
    my $spec = $mod;
    $spec =~ s/\.dll$//;
    $def =~ s/\.(dll|drv)$//;
    printf NEWMAKE "lib%s.def: %s/%s.spec.def\n", $def, $dir, $spec;
    printf NEWMAKE "\t\$(RM) \$@ && \$(LN_S) %s/%s.spec.def \$@\n", $dir, $spec;
    printf NEWMAKE "lib%s.a: %s/%s.spec.def\n", $def, $dir, $spec;
    printf NEWMAKE "\t\$(DLLTOOL) -k -l \$@ -d %s/%s.spec.def\n\n", $dir, $spec;
}
foreach my $mod (sort @implibs)
{
    my $dir = $directories{$mod};
    my $spec = $mod;
    $spec =~ s/\.dll$//;
    printf NEWMAKE "%s/%s.spec.def: \$(WINEBUILD)\n", $dir, $spec;
}


print NEWMAKE <<EOF;

\$(BUILDSUBDIRS): \$(IMPORT_LIBS)
\$(INSTALLSUBDIRS:%=%/__install__): \$(IMPORT_LIBS)

EOF

################################################################
# output the inter-dll dependencies and rules

print NEWMAKE "# Map library name to the corresponding directory\n\n";

foreach my $mod (sort keys %directories)
{
    printf NEWMAKE "%s/%s.so: %s\n", $directories{$mod}, $mod, $directories{$mod};
}
foreach my $mod (sort keys %implib_dirs)
{
    printf NEWMAKE "%s/%s: %s\n", $implib_dirs{$mod}, $mod, $implib_dirs{$mod};
}

################################################################
# makefile trailer

print NEWMAKE <<EOF;

# Rules for auto documentation

\$(SUBDIRS:%=%/__man__): dummy
	cd `dirname \$@` && \$(MAKE) man

man: \$(SUBDIRS:%=%/__man__)

\$(SUBDIRS:%=%/__doc_html__): dummy
	cd `dirname \$@` && \$(MAKE) doc-html

doc-html: \$(SUBDIRS:%=%/__doc_html__)

\$(SUBDIRS:%=%/__doc_sgml__): dummy
	cd `dirname \$@` && \$(MAKE) doc-sgml

doc-sgml: \$(SUBDIRS:%=%/__doc_sgml__)

.PHONY: man doc-html doc-sgml \$(SUBDIRS:%=%/__man__) \$(SUBDIRS:%=%/__doc_html__) \$(SUBDIRS:%=%/__doc_sgml__)

# Misc rules

install install-dev:: \$(IMPORT_LIBS)
	\$(MKINSTALLDIRS) \$(dlldir)
	for f in \$(IMPORT_LIBS); do \$(INSTALL_DATA) \$\$f \$(dlldir)/\$\$f; done

install install-lib:: \$(INSTALLSUBDIRS:%=%/__install__)

uninstall::
	-cd \$(dlldir) && \$(RM) \$(IMPORT_LIBS)
	-rmdir \$(dlldir)

clean::
	\$(RM) \$(IMPORT_LIBS) \$(SYMLINKS)

check test:: \$(BUILDSUBDIRS:%=%/__test__)

crosstest:: \$(BUILDSUBDIRS:%=%/__crosstest__)

checklink:: \$(BUILDSUBDIRS:%=%/__checklink__)

### Dependencies:
EOF

close NEWMAKE;
rename "Makefile.in.new", "Makefile.in";
printf "Successfully updated Makefile.in\n";
