make_makefiles 20.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#!/usr/bin/perl -w
#
# Build the auto-generated parts of the Wine makefiles.
#
# Copyright 2006 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#

22 23
use strict;

24
# Make rules files
25 26 27 28 29 30 31 32 33
my %makerules =
(
 "MAKE_RULES" => "Make.rules",
 "MAKE_DLL_RULES" => "dlls/Makedll.rules",
 "MAKE_IMPLIB_RULES" => "dlls/Makeimplib.rules",
 "MAKE_TEST_RULES" => "dlls/Maketest.rules",
 "MAKE_PROG_RULES" => "programs/Makeprog.rules",
);

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
# Programs that we want to install in the bin directory too
my %bin_install =
(
  "msiexec" => 1,
  "notepad" => 1,
  "regedit" => 1,
  "regsvr32" => 1,
  "wineboot" => 1,
  "winecfg" => 1,
  "wineconsole" => 1,
  "winedbg" => 1,
  "winefile" => 1,
  "winemine" => 1,
  "winepath" => 1,
);

# Programs that we don't want to install at all
my %dont_install =
(
  "cmdlgtst" => 1,
  "view" => 1,
  "winetest" => 1,
);

58 59 60
# Default patterns for top-level .gitignore
my @ignores = (
    "*.[oa]",
61
    "*.ok",
62
    "*.res",
63 64 65 66 67 68 69
    "*.so",
    "/autom4te.cache",
    "/config.cache",
    "/config.log",
    "/config.status",
    "/TAGS",
    "/tags",
70
    "Makefile",
71
    "dlldata.c",
72 73 74
    "dlls/*/*.def",
    "dlls/*/tests/*crosstest.exe",
    "dlls/*/tests/testlist.c",
75
    "include/config.h",
76 77
    "include/stamp-h",
    "programs/winetest/*_test.exe",
78
    "programs/winetest/*_test.rc",
79
    "programs/winetest/build.rc",
80 81
);

82 83 84 85 86 87 88
# Source files and their resulting target to ignore
my @ignore_srcs = (
    [ 'BISON_SRCS',   '\.y',   '.tab.c' ],
    [ 'BISON_SRCS',   '\.y',   '.tab.h' ],
    [ 'LEX_SRCS',     '\.l',   '.yy.c' ],
    [ 'MC_SRCS',      '\.mc',  '.mc.rc' ],
    [ 'IDL_TLB_SRCS', '\.idl', '.tlb' ],
89
    [ 'IDL_H_SRCS',   '\.idl', '.h' ],
90 91 92 93
    [ 'IDL_C_SRCS',   '\.idl', '.h' ],
    [ 'IDL_I_SRCS',   '\.idl', '.h' ],
    [ 'IDL_P_SRCS',   '\.idl', '.h' ],
    [ 'IDL_S_SRCS',   '\.idl', '.h' ],
94 95 96 97 98 99
    [ 'IDL_C_SRCS',   '\.idl', '_c.c' ],
    [ 'IDL_I_SRCS',   '\.idl', '_i.c' ],
    [ 'IDL_P_SRCS',   '\.idl', '_p.c' ],
    [ 'IDL_S_SRCS',   '\.idl', '_s.c' ],
);

100 101 102 103 104 105 106
my %exported_wine_headers = (
    "wine/debug.h" => 1,
    "wine/exception.h" => 1,
    "wine/library.h" => 1,
    "wine/unicode.h" => 1,
    "wine/itss.idl" => 1,
    "wine/svcctl.idl" => 1,
107 108 109 110 111 112 113 114 115
);

my %private_idl_headers = (
    "axcore.idl" => 1,
    "axextend.idl" => 1,
    "dbinit.idl" => 1,
    "dbprop.idl" => 1,
    "dbs.idl" => 1,
    "devenum.idl" => 1,
116 117
    "dyngraph.idl" => 1,
    "vmrender.idl" => 1,
118
    "wine/wined3d.idl" => 1,
119
    "wine/winedxgi.idl" => 1,
120 121
);

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
my %ignored_source_files = (
    "dlls/wineps.drv/afm2c.c" => 1,
    "dlls/wineps.drv/mkagl.c" => 1,
    "programs/winetest/dist.rc" => 1,
);

my (@all_files, @makefiles, %makefiles);

sub dirname($)
{
    my $ret = shift;
    return "" unless $ret =~ /\//;
    $ret =~ s!/[^/]*$!!;
    return $ret;
}
137

138 139 140 141
# update a file if changed
sub update_file($)
{
    my $file = shift;
142
    my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
143 144 145 146 147 148 149 150
    if (!$ret)
    {
        unlink "$file.new";
    }
    else
    {
        rename "$file.new", "$file";
        print "$file updated\n";
151 152 153 154 155
        if ($file eq "configure.ac")
        {
            system "autoconf";
            print "configure updated\n";
        }
156 157 158 159 160 161 162 163 164 165 166 167 168
    }
    return $ret;
}

# replace some lines in a file between two markers
sub replace_in_file($$$@)
{
    my $file = shift;
    my $start = shift;
    my $end = shift;

    open NEW_FILE, ">$file.new" or die "cannot create $file.new";

169
    if (defined($start))
170
    {
171 172 173 174 175 176
        open OLD_FILE, "$file" or die "cannot open $file";
        while (<OLD_FILE>)
        {
            last if /$start/;
            print NEW_FILE $_;
        }
177 178 179 180 181 182 183 184 185 186 187 188 189 190
    }

    print NEW_FILE @_;

    if (defined($end))
    {
        my $skip=1;
        while (<OLD_FILE>)
        {
            print NEW_FILE $_ unless $skip;
            $skip = 0 if /$end/;
        }
    }

191
    close OLD_FILE if defined($start);
192 193 194 195
    close NEW_FILE;
    return update_file($file);
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
# replace a variable in a makefile
sub replace_makefile_variable($$)
{
    my ($file, $var) = @_;
    my $make = $makefiles{$file};

    return unless defined ${$make}{"=$var"};

    my @values = @{${$make}{"=$var"}};
    ${$make}{$var} = \@values;

    open NEW_FILE, ">$file.in.new" or die "cannot create $file.in.new";

    open OLD_FILE, "$file.in" or die "cannot open $file.in";
    while (<OLD_FILE>)
    {
        if (/^\s*($var\s+)=/)
        {
            # try to preserve formatting
            my $prefix = $1;
            my $multiline = /\\$/ || (@values > 1);
            while (/\\$/)
            {
                $_ = <OLD_FILE>;
                last unless $_;
            }
            if ($multiline)
            {
                print NEW_FILE "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
            }
            else
            {
                print NEW_FILE "$prefix= @values\n";
            }
            next;
        }
        print NEW_FILE $_;
    }
    close OLD_FILE;
    close NEW_FILE;
    return update_file("$file.in");
}

239 240 241 242
# parse the specified makefile to identify the rules file
sub parse_makefile($)
{
    my $file = shift;
243
    my %make;
244

245 246
    ($make{"=dir"} = $file) =~ s/[^\/]+$//;

247 248 249 250 251
    open MAKE, "$file.in" or die "cannot open $file.in\n";

    while (<MAKE>)
    {
        chomp;
252
        next if (/^\s*#/);
253
        while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines
254
        next if (/^\s*$/);
255 256 257 258

        if (/^\@(MAKE.*RULES)\@/)
        {
            my $var = $1;
259 260 261
            $make{"=rules"} = $makerules{$var};
            next;
        }
262
        if (/^\s*(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
263
        {
264
            $make{$1} = $2;
265 266
            next;
        }
267
        if (/^\s*(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|C_SRCS|MC_SRCS|RC_SRCS|SVG_SRCS|C_SRCS16|RC_SRCS16|SPEC_SRCS16|EXTRA_OBJS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
268 269 270 271 272
        {
            my @list = split(/\s+/, $2);
            $make{$1} = \@list;
            next;
        }
273
    }
274
    return %make;
275
}
276

277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
# assign source files to their respective makefile
sub assign_sources_to_makefiles()
{
    foreach my $file (@all_files)
    {
        next if defined $ignored_source_files{$file};
        my $dir = dirname( $file );

        while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
        next unless $dir;

        die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};

        my $make = $makefiles{"$dir/Makefile"};
        my $basename = substr( $file, length($dir) + 1 );

        if ($basename =~ /\.c$/) { push @{${$make}{"=C_SRCS"}}, $basename; }
        elsif ($basename =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $basename; }
        elsif ($basename =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $basename; }
        elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
        elsif ($basename =~ /\.rc$/) { push @{${$make}{"=RC_SRCS"}}, $basename; }
        elsif ($basename =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $basename; }
    }
}
301

302
################################################################
303
# update the makefile list in configure.ac
304

305
sub update_makefiles(@)
306
{
307
    my (@lines);
308 309 310

    foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
    {
311
        my $file = $makerules{$var};
312
        my %make = %{$makefiles{$file}};
313 314
        my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
        push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
315
    }
316
    push @lines, "\n";
317 318 319

    foreach my $file (sort @_)
    {
320 321
        my %make = %{$makefiles{$file}};
        my $rules = $make{"=rules"};
322
        my $args = "";
323 324 325 326 327
        if ($rules eq $makerules{"MAKE_DLL_RULES"})
        {
            $args = ",[dlls],[ALL_DLL_DIRS]";
            $args .= ",[enable_win16]" if $make{"MODULE"} =~ /(16|\.vxd)$/;
        }
328
        elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
329
        elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS],[enable_tests]"; }
330 331 332 333 334 335 336
        elsif ($rules eq $makerules{"MAKE_PROG_RULES"})
        {
            (my $name = $file) =~ s/^programs\/(.*)\/Makefile/$1/;
            $args = ",[programs],[ALL_PROGRAM_DIRS";
            $args .= ",ALL_PROGRAM_INSTALL_DIRS" unless $dont_install{$name};
            $args .= ",ALL_PROGRAM_BIN_INSTALL_DIRS" if $bin_install{$name};
            $args .= "]";
337
            $args .= ",[enable_win16]" if $make{"MODULE"} =~ /16$/;
338
        }
339
        elsif ($file =~ /^[^\/]*\/Makefile$/) { $args = ",[],[ALL_TOP_DIRS]"; }
340
        push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
341 342
    }

343 344 345 346 347 348 349 350 351 352 353 354 355 356
    # update the source variables in all the makefiles

    foreach my $file (sort @_)
    {
        my %make = %{$makefiles{$file}};

        replace_makefile_variable( $file, "LEX_SRCS" );
        replace_makefile_variable( $file, "BISON_SRCS" );
        replace_makefile_variable( $file, "MC_SRCS" );
        replace_makefile_variable( $file, "SVG_SRCS" );
        replace_makefile_variable( $file, "C_SRCS" ) unless defined $make{"C_SRCS16"};
        replace_makefile_variable( $file, "RC_SRCS" ) unless defined $make{"RC_SRCS16"};
    }

357 358
    push @lines, "\ndnl Build dependencies for test files compiled into winetest\n";
    replace_in_file( "configure.ac", '^WINE_CONFIG_MAKERULES', '^dnl Build dependencies for test files compiled into winetest$', @lines);
359 360
}

361 362 363 364 365

################################################################
# process ignore targets for generic source files

sub update_ignores(@)
366
{
367
    my @ignores;
368

369 370 371 372
    foreach my $file (sort @_)
    {
        my %makefile = %{$makefiles{$file}};
        my @list;
373

374 375 376 377 378 379
        foreach my $src (@ignore_srcs)
        {
            my @pattern = @{$src};
            next unless defined $makefile{$pattern[0]};
            push @list, map { (my $ret = $_) =~ s/$pattern[1]$/$pattern[2]/; $ret; } @{$makefile{$pattern[0]}};
        }
380 381 382 383
        foreach my $f (@list)
        {
            push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
        }
384 385 386
    }
    return @ignores;
}
387

388 389 390
################################################################
# update dlls/Makefile.in

391 392
sub update_dlls(@)
{
393
    my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
394
    my $text = "";
395
    my @ignores = ();
396 397 398

    foreach my $make (@_)
    {
399
        my %makefile = %{$makefiles{$make}};
400
        next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
401 402 403

        next unless defined $makefile{"MODULE"};
        my $module = $makefile{"MODULE"};
404
        (my $dir = $makefile{"=dir"}) =~ s/^dlls\/(.*)\//$1/;
405

406
        if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
407
        {
408
            $staticlib_dirs{$module} = $dir;
409
            die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
410 411 412
        }
        else
        {
413
            die "invalid module $module" unless $module =~ /\./;
414 415
            (my $mod = $module) =~ s/\.dll$//;
            die "invalid directory $dir for module $module\n" unless $mod eq $dir;
416
            $directories{$module} = $dir;
417 418 419 420
        }

        if (defined $makefile{"IMPORTLIB"})
        {
421
            if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
422 423 424 425 426 427 428 429 430 431 432 433 434
            {
                $importlibs{$module} = $1;
            }
            else
            {
                die "invalid importlib name $makefile{IMPORTLIB} in $make";
            }
        }

        $static_implibs{$module} = 1 if defined $makefile{"IMPLIB_SRCS"};

        if (defined $makefile{"SPEC_SRCS16"})
        {
435
            my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
436 437
            $altnames{$module} = \@list;
        }
438 439 440 441 442 443 444
        if (defined $makefile{"EXTRA_OBJS16"})
        {
            foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
            {
                if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
            }
        }
445 446
    }

447
    # output the list of 16-bit files
448

449
    my @targets16 = ();
450 451 452 453 454
    foreach my $mod (sort keys %directories)
    {
        next unless defined $altnames{$mod};
        foreach my $i (sort @{$altnames{$mod}})
        {
455
            push @targets16, $i . "16";
456 457
        }
    }
458
    $text .= "# 16-bit dlls\n\n";
459
    $text .= "WIN16_FILES = \\\n";
460 461 462 463 464
    $text .=  "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
    $text .= "\@MAKE_RULES\@\n\n";

    # output the all: target

465
    $text .= "# Main target\n\n";
466
    $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487

    # output the lib name -> directory rules

    $text .= "# Placeholders for 16-bit libraries\n\n";
    foreach my $mod (sort keys %directories)
    {
        next unless defined $altnames{$mod};
        $text .= sprintf "%s:\n", join(" ", map { $_ . "16"; } sort @{$altnames{$mod}});
        $text .= sprintf "\techo \"%s\" >\$\@\n\n", $mod;
    }

    # output the import libraries rules

    $text .= "# Import libraries\n\n";
    $text .= "STATIC_IMPLIBEXT = \$(IMPLIBEXT:def=def.a)\n\n";

    my @lib_symlinks = ();
    foreach my $mod (sort keys %importlibs)
    {
        my $dir = $directories{$mod};
        my $lib = $importlibs{$mod};
488
        if ($lib ne $dir) { push @lib_symlinks, $mod; }
489 490 491 492
    }
    $text .= "IMPORT_SYMLINKS =";
    foreach my $mod (sort @lib_symlinks)
    {
493
        $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
494 495 496 497 498
    }

    $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
    foreach my $mod (sort keys %staticlib_dirs)
    {
499
        $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
500 501 502
    }
    foreach my $mod (sort keys %importlibs)
    {
503
        $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
504
        next unless defined $static_implibs{$mod};
505
        $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
506
    }
507 508 509 510 511 512 513 514 515 516
    $text .= "\n\nCROSS_IMPLIBS =";
    foreach my $mod (sort @lib_symlinks)
    {
        $text .= sprintf " \\\n\tlib%s.a", $importlibs{$mod};
    }
    foreach my $mod (sort keys %importlibs)
    {
        next if defined $static_implibs{$mod};
        $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.a";
    }
517
    $text .= "\n\n";
518
    $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
519
    $text .= "implib: \$(IMPORT_LIBS)\n\n";
520 521
    $text .= "testsubdirs: \$(TESTSUBDIRS)\n\n";
    $text .= ".PHONY: implib testsubdirs\n\n";
522 523 524 525 526 527 528

    foreach my $mod (sort keys %importlibs)
    {
        my $dir = $directories{$mod};
        my $lib = $importlibs{$mod};
        my $spec = $mod;
        $spec =~ s/\.dll$//;
529 530 531 532 533 534 535 536 537
        if (defined($static_implibs{$mod}))
        {
            $text .= sprintf "%s/lib%s.def: %s/%s.spec \$(WINEBUILD)\n", $dir, $lib, $dir, $spec;
            $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.def\n\n", $dir, $lib;
            $text .= sprintf "%s/lib%s.\$(STATIC_IMPLIBEXT): dummy\n", $dir, $lib, $dir, $spec;
            $text .= sprintf "\t\@cd %s && \$(MAKE) lib%s.\$(STATIC_IMPLIBEXT)\n\n", $dir, $lib;
        }
        else
        {
538 539 540
            $text .= sprintf "%s/lib%s.def %s/lib%s.a: %s/%s.spec \$(WINEBUILD)\n",
                             $dir, $lib, $dir, $lib, $dir, $spec;
            $text .= sprintf "\t\@cd %s && \$(MAKE) `basename \$\@`\n\n", $dir;
541
        }
542 543 544 545
    }
    foreach my $mod (sort @lib_symlinks)
    {
        my $dir = $directories{$mod};
546 547 548 549 550
        my $lib = "lib" . $importlibs{$mod};
        $text .= sprintf "%s.a: %s/%s.a\n", $lib, $dir, $lib;
        $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.a \$@\n\n", $dir, $lib;
        $text .= sprintf "%s.def: %s/%s.def\n", $lib, $dir, $lib;
        $text .= sprintf "\t\$(RM) \$@ && \$(LN_S) %s/%s.def \$@\n\n", $dir, $lib;
551 552 553 554 555 556 557 558 559 560 561
    }

    $text .= "\$(BUILDSUBDIRS): \$(IMPORT_LIBS)\n";
    $text .= "\$(INSTALLSUBDIRS:%=%/__install__) \$(INSTALLSUBDIRS:%=%/__install-lib__): \$(IMPORT_LIBS)\n\n";

    # output the inter-dll dependencies and rules

    $text .= "# Map library name to the corresponding directory\n\n";

    foreach my $mod (sort keys %staticlib_dirs)
    {
562
        $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
563 564 565 566
    }
    $text .= "\n# Misc rules\n";

    replace_in_file( "dlls/Makefile.in",
567
                     '^# 16-bit dlls',
568 569 570 571 572 573 574
                     '^# Misc rules',
                     $text );

    # .gitignore file

    foreach my $mod (sort @lib_symlinks)
    {
575
        push @ignores, "dlls/lib$importlibs{$mod}.def";
576 577 578 579
    }
    foreach my $mod (sort keys %directories)
    {
        next unless defined $altnames{$mod};
580
        push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
581 582
    }

583
    return @ignores;
584 585
}

586

587 588 589 590 591
################################################################
# update include/Makefile.in

sub update_includes()
{
592
    my (@h_srcs, @private_idl_srcs, @public_idl_srcs, @tlb_srcs, %subdirs);
593
    my @includes = map { (my $ret = $_) =~ s/^include\///; $ret; } grep /^include\//, @all_files;
594 595 596
    foreach my $incl (@includes)
    {
        if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
597
        next if ($incl =~ /\.in$/);
598 599 600 601 602
        if ($incl =~ /^wine\// && !$exported_wine_headers{$incl})
        {
            if ($private_idl_headers{$incl}) { push @private_idl_srcs, $incl; }
            next;
        }
603 604 605
        if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
        elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
        elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
606
        elsif ($incl =~ /\.rh$/) { push @h_srcs, $incl; }
607
        elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
608
        elsif ($incl =~ /\.idl$/) { push @public_idl_srcs, $incl; }
609
        else { die "unknown file $incl in include dir"; }
610
    }
611 612 613 614 615
    replace_in_file( "include/Makefile.in", '^PRIVATE_IDL_H_SRCS\s*=', '^INSTALLDIRS',
                     "PRIVATE_IDL_H_SRCS = \\\n\t",
                     join( " \\\n\t", sort @private_idl_srcs ),
                     "\n\nPUBLIC_IDL_H_SRCS = \\\n\t",
                     join( " \\\n\t", sort @public_idl_srcs ),
616 617
                     "\n\nIDL_TLB_SRCS = \\\n\t",
                     join( " \\\n\t", sort @tlb_srcs ),
618
                     "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(PUBLIC_IDL_H_SRCS) \\\n\t",
619 620 621 622
                     join( " \\\n\t", sort @h_srcs ),
                     "\n\nEXTRASUBDIRS = ",
                     join( " ", sort keys %subdirs ),
                     "\n\nINSTALLDIRS = \\\n" );
623
    return map { s/(.*)\.idl$/include\/$1.h/; $_; } @public_idl_srcs, @private_idl_srcs;
624 625 626
}


627 628 629
################################################################
# update the main .gitignore

630
sub update_gitignore(@)
631
{
632
    my @ignores = values %makerules;
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

    foreach my $make (@makefiles)
    {
        my %makefile = %{$makefiles{$make}};
        my $dir = $makefile{"=dir"};
        if (defined $makefile{"MANPAGES"})
        {
            push @ignores, map { $dir . $_; } @{$makefile{"MANPAGES"}};
        }
        if (defined $makefile{"PROGRAMS"})
        {
            push @ignores, map { s/\$\(EXEEXT\)//; $dir . $_; } @{$makefile{"PROGRAMS"}};
        }
    }

    # prepend a slash to paths that don't have one
    @ignores = map { $_ =~ s/^([^\/]+)$/\/$1/; $_; } @ignores;

651 652 653
    # get rid of duplicates
    my %ignores = ();
    foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
654 655 656

    replace_in_file( ".gitignore", undef, undef,
                     "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
657
                     join("\n", sort keys %ignores), "\n" );
658 659 660
}


661 662 663 664
die "needs to be run from a git checkout" unless -d ".git";

@all_files = split /\0/, `git ls-files -c -z`;
@makefiles = map { my $ret = $_; $ret =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
665 666 667 668 669 670 671

foreach my $file (sort values %makerules, @makefiles)
{
    my %make = parse_makefile( $file );
    $makefiles{$file} = \%make;
}

672
assign_sources_to_makefiles();
673
update_makefiles( @makefiles );
674
push @ignores, update_includes();
675
push @ignores, update_ignores( @makefiles );
676 677
push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
update_gitignore( @ignores );