make_makefiles 16.8 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
# Make rules files
23 24 25 26 27 28 29 30 31
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",
);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# Programs that we want to install in the bin directory too
my %bin_install =
(
  "msiexec" => 1,
  "notepad" => 1,
  "progman" => 1,
  "regedit" => 1,
  "regsvr32" => 1,
  "uninstaller" => 1,
  "wineboot" => 1,
  "winebrowser" => 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,
);

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

120 121
my (@makefiles, %makefiles);

122 123 124 125
# update a file if changed
sub update_file($)
{
    my $file = shift;
126
    my $ret = !(-f $file) || system "cmp $file $file.new >/dev/null";
127 128 129 130 131 132 133 134
    if (!$ret)
    {
        unlink "$file.new";
    }
    else
    {
        rename "$file.new", "$file";
        print "$file updated\n";
135 136 137 138 139
        if ($file eq "configure.ac")
        {
            system "autoconf";
            print "configure updated\n";
        }
140 141 142 143 144 145 146 147 148 149 150 151 152
    }
    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";

153
    if (defined($start))
154
    {
155 156 157 158 159 160
        open OLD_FILE, "$file" or die "cannot open $file";
        while (<OLD_FILE>)
        {
            last if /$start/;
            print NEW_FILE $_;
        }
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    }

    print NEW_FILE @_;

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

175
    close OLD_FILE if defined($start);
176 177 178 179
    close NEW_FILE;
    return update_file($file);
}

180 181 182 183
# parse the specified makefile to identify the rules file
sub parse_makefile($)
{
    my $file = shift;
184
    my %make;
185

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

188 189 190 191 192 193 194 195 196 197
    open MAKE, "$file.in" or die "cannot open $file.in\n";

    while (<MAKE>)
    {
        chomp;
        while (/\\$/) { chop; $_ .= <MAKE>; chomp; }  # merge continued lines

        if (/^\@(MAKE.*RULES)\@/)
        {
            my $var = $1;
198 199 200
            $make{"=rules"} = $makerules{$var};
            next;
        }
201
        if (/^(MODULE|IMPORTLIB|TESTDLL)\s*=\s*(.*)/)
202
        {
203
            $make{$1} = $2;
204 205
            next;
        }
206
        if (/^(BISON_SRCS|LEX_SRCS|IDL_[CHIPS]_SRCS|IDL_TLB_SRCS|IMPLIB_SRCS|MC_SRCS|RC_SRCS|RC_SRCS16|RC_BINARIES|SPEC_SRCS16|EXTRA_OBJS16|MANPAGES|PROGRAMS)\s*=\s*(.*)/)
207 208 209 210 211
        {
            my @list = split(/\s+/, $2);
            $make{$1} = \@list;
            next;
        }
212
    }
213
    return %make;
214
}
215 216


217
################################################################
218
# update the makefile list in configure.ac
219

220
sub update_makefiles(@)
221
{
222
    my (@lines);
223 224 225

    foreach my $var (sort { $makerules{$a} cmp $makerules{$b}; } keys %makerules)
    {
226
        my $file = $makerules{$var};
227
        my %make = %{$makefiles{$file}};
228 229
        my $rules = $make{"=rules"} ? ",[$make{\"=rules\"}]" : "";
        push @lines, "WINE_CONFIG_MAKERULES([$file],[$var]$rules)\n";
230
    }
231
    push @lines, "\n";
232 233 234

    foreach my $file (sort @_)
    {
235 236
        my %make = %{$makefiles{$file}};
        my $rules = $make{"=rules"};
237 238 239 240
        my $args = "";
        if ($rules eq $makerules{"MAKE_DLL_RULES"}) { $args = ",[dlls],[ALL_DLL_DIRS]"; }
        elsif ($rules eq $makerules{"MAKE_IMPLIB_RULES"}) { $args = ",[dlls],[ALL_IMPLIB_DIRS]"; }
        elsif ($rules eq $makerules{"MAKE_TEST_RULES"}) { $args = ",[dlls],[ALL_TEST_DIRS]"; }
241 242 243 244 245 246 247 248
        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 .= "]";
        }
249
        elsif ($file =~ /^[^\/]*\/Makefile$/) { $args = ",[],[ALL_TOP_DIRS]"; }
250
        push @lines, "WINE_CONFIG_MAKEFILE([$file],[$rules]$args)\n";
251 252
    }

253 254
    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);
255 256
}

257 258 259 260 261

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

sub update_ignores(@)
262
{
263
    my @ignores;
264

265 266 267 268
    foreach my $file (sort @_)
    {
        my %makefile = %{$makefiles{$file}};
        my @list;
269

270 271 272 273 274 275
        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]}};
        }
276 277 278 279
        foreach my $f (@list)
        {
            push @ignores, $makefile{"=dir"} . $f unless $f =~ /\$\(.*\)/;  # skip make variables
        }
280 281 282
    }
    return @ignores;
}
283

284 285 286
################################################################
# update dlls/Makefile.in

287 288
sub update_dlls(@)
{
289
    my (%directories, %importlibs, %static_implibs, %staticlib_dirs, %altnames);
290
    my $text = "";
291
    my @ignores = ();
292 293 294

    foreach my $make (@_)
    {
295
        my %makefile = %{$makefiles{$make}};
296
        next if ($makefile{"=rules"} eq $makerules{"MAKE_TEST_RULES"});
297 298 299

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

302
        if ($makefile{"=rules"} eq $makerules{"MAKE_IMPLIB_RULES"})
303
        {
304
            $staticlib_dirs{$module} = $dir;
305
            die "invalid module $module in dir $staticlib_dirs{$module}\n" if "$staticlib_dirs{$module}" ne $module;
306 307 308
        }
        else
        {
309
            die "invalid module $module" unless $module =~ /\./;
310 311
            (my $mod = $module) =~ s/\.dll$//;
            die "invalid directory $dir for module $module\n" unless $mod eq $dir;
312
            $directories{$module} = $dir;
313 314 315 316
        }

        if (defined $makefile{"IMPORTLIB"})
        {
317
            if ($makefile{"IMPORTLIB"} =~ /^([a-zA-Z0-9_.]+)/)
318 319 320 321 322 323 324 325 326 327 328 329 330
            {
                $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"})
        {
331
            my @list = map { $_ =~ s/\.spec$//; $_ .= ".dll" unless $_ =~ /\./; $_; } @{$makefile{"SPEC_SRCS16"}};
332 333
            $altnames{$module} = \@list;
        }
334 335 336 337 338 339 340
        if (defined $makefile{"EXTRA_OBJS16"})
        {
            foreach my $obj (@{$makefile{"EXTRA_OBJS16"}})
            {
                if ($obj =~ /^(.*\.(exe|mod))\.o/) { push @{$altnames{$module}}, $1; }
            }
        }
341 342
    }

343
    # output the list of 16-bit files
344

345
    my @targets16 = ();
346 347 348 349 350
    foreach my $mod (sort keys %directories)
    {
        next unless defined $altnames{$mod};
        foreach my $i (sort @{$altnames{$mod}})
        {
351
            push @targets16, $i . "16";
352 353
        }
    }
354
    $text .= "# 16-bit dlls\n\n";
355
    $text .= "WIN16_FILES = \\\n";
356 357 358 359 360
    $text .=  "\t" . join( " \\\n\t", sort @targets16 ) . "\n\n";
    $text .= "\@MAKE_RULES\@\n\n";

    # output the all: target

361
    $text .= "# Main target\n\n";
362
    $text .= "all: \$(BUILDSUBDIRS) \@WIN16_FILES\@\n\n";
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383

    # 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};
384
        if ($lib ne $dir) { push @lib_symlinks, $mod; }
385 386 387 388
    }
    $text .= "IMPORT_SYMLINKS =";
    foreach my $mod (sort @lib_symlinks)
    {
389
        $text .= sprintf " \\\n\tlib%s.\$(IMPLIBEXT)", $importlibs{$mod};
390 391 392 393 394
    }

    $text .= "\n\nIMPORT_LIBS = \\\n\t\$(IMPORT_SYMLINKS)";
    foreach my $mod (sort keys %staticlib_dirs)
    {
395
        $text .= sprintf " \\\n\t%s/lib%s.a", $staticlib_dirs{$mod}, $mod;
396 397 398
    }
    foreach my $mod (sort keys %importlibs)
    {
399
        $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(IMPLIBEXT)";
400
        next unless defined $static_implibs{$mod};
401
        $text .= " \\\n\t$directories{$mod}/lib$importlibs{$mod}.\$(STATIC_IMPLIBEXT)";
402
    }
403 404 405 406 407 408 409 410 411 412
    $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";
    }
413
    $text .= "\n\n";
414
    $text .= "\$(TESTSUBDIRS:%=%/__crosstest__): \$(CROSS_IMPLIBS)\n\n";
415
    $text .= "implib: \$(IMPORT_LIBS)\n\n";
416
    $text .= ".PHONY: implib\n\n";
417 418 419 420 421 422 423

    foreach my $mod (sort keys %importlibs)
    {
        my $dir = $directories{$mod};
        my $lib = $importlibs{$mod};
        my $spec = $mod;
        $spec =~ s/\.dll$//;
424 425 426 427 428 429 430 431 432
        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
        {
433 434 435
            $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;
436
        }
437 438 439 440
    }
    foreach my $mod (sort @lib_symlinks)
    {
        my $dir = $directories{$mod};
441 442 443 444 445
        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;
446 447 448 449 450 451 452 453 454 455 456
    }

    $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)
    {
457
        $text .= sprintf "%s/lib%s.a: %s\n", $staticlib_dirs{$mod}, $mod, $staticlib_dirs{$mod};
458 459 460 461
    }
    $text .= "\n# Misc rules\n";

    replace_in_file( "dlls/Makefile.in",
462
                     '^# 16-bit dlls',
463 464 465 466 467 468 469
                     '^# Misc rules',
                     $text );

    # .gitignore file

    foreach my $mod (sort @lib_symlinks)
    {
470
        push @ignores, "dlls/lib$importlibs{$mod}.def";
471 472 473 474
    }
    foreach my $mod (sort keys %directories)
    {
        next unless defined $altnames{$mod};
475
        push @ignores, map { "dlls/" . $_ . "16"; } @{$altnames{$mod}};
476 477
    }

478
    return @ignores;
479 480
}

481

482 483 484 485 486 487 488 489 490 491 492
################################################################
# update include/Makefile.in

sub update_includes()
{
    return unless -d ".git";
    my (@h_srcs, @idl_srcs, @tlb_srcs, %subdirs);
    my @includes = map { s/^include\///; $_; } split /\0/, `git ls-files -c -z include`;
    foreach my $incl (@includes)
    {
        if ($incl =~ /(.*)\//) { $subdirs{$1} = 1; }
493
        next if ($incl =~ /^wine\// && !$exported_wine_headers{$incl});
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
        if ($incl =~ /stdole2\.idl$/) { push @tlb_srcs, $incl; }
        elsif ($private_idl_headers{$incl}) { push @h_srcs, $incl; }
        elsif ($incl =~ /\.h$/) { push @h_srcs, $incl; }
        elsif ($incl =~ /\.inl$/) { push @h_srcs, $incl; }
        elsif ($incl =~ /\.idl$/) { push @idl_srcs, $incl; }
    }
    replace_in_file( "include/Makefile.in", '^IDL_H_SRCS\s*=', '^INSTALLDIRS',
                     "IDL_H_SRCS = \\\n\t",
                     join( " \\\n\t", sort @idl_srcs ),
                     "\n\nIDL_TLB_SRCS = \\\n\t",
                     join( " \\\n\t", sort @tlb_srcs ),
                     "\n\nSRCDIR_INCLUDES = \\\n\t\$(IDL_TLB_SRCS) \\\n\t\$(IDL_H_SRCS) \\\n\t",
                     join( " \\\n\t", sort @h_srcs ),
                     "\n\nEXTRASUBDIRS = ",
                     join( " ", sort keys %subdirs ),
                     "\n\nINSTALLDIRS = \\\n" );
}


513 514 515
################################################################
# update the main .gitignore

516
sub update_gitignore(@)
517
{
518
    my @ignores = values %makerules;
519 520 521 522 523 524 525 526 527 528 529 530 531

    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"}};
        }
532 533 534 535
        if ($dir =~ /^programs\/(.*)\/$/)
        {
            push @ignores, "$dir$1";
        }
536 537 538 539 540
    }

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

541 542 543
    # get rid of duplicates
    my %ignores = ();
    foreach my $i (@ignores, @_) { $ignores{$i} = 1; }
544 545 546

    replace_in_file( ".gitignore", undef, undef,
                     "# Automatically generated by make_makefiles; DO NOT EDIT!!\n",
547
                     join("\n", sort keys %ignores), "\n" );
548 549 550
}


551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
if (-d ".git")
{
    @makefiles = map { s/\.in$//; $_; } split /\0/, `git ls-files -c -z Makefile.in \\*/Makefile.in`;
}
else
{
    @makefiles = map { s/^\.\/(.*)\.in/$1/; $_; } split(/\s/,`find . -name Makefile.in -print`);
}

update_includes();

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

568 569
update_makefiles( @makefiles );
push @ignores, update_ignores( @makefiles );
570 571
push @ignores, update_dlls( sort grep /^dlls\//, @makefiles );
update_gitignore( @ignores );