make_makefiles 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #!/usr/bin/perl -w
  2. #
  3. # Build the auto-generated parts of the Wine makefiles.
  4. #
  5. # Copyright 2006 Alexandre Julliard
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2.1 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this library; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. #
  21. use strict;
  22. # Dlls and programs that are 16-bit specific
  23. my %modules16 =
  24. (
  25. "ifsmgr.vxd" => 1,
  26. "mmdevldr.vxd" => 1,
  27. "monodebg.vxd" => 1,
  28. "vdhcp.vxd" => 1,
  29. "vmm.vxd" => 1,
  30. "vnbt.vxd" => 1,
  31. "vnetbios.vxd" => 1,
  32. "vtdapi.vxd" => 1,
  33. "vwin32.vxd" => 1,
  34. "w32skrnl.dll" => 1,
  35. "winevdm.exe" => 1,
  36. "wow32.dll" => 1,
  37. );
  38. my %exported_wine_headers = (
  39. "wine/debug.h" => 1,
  40. "wine/exception.h" => 1,
  41. "wine/library.h" => 1,
  42. "wine/itss.idl" => 1,
  43. "wine/svcctl.idl" => 1,
  44. );
  45. my %ignored_source_files = (
  46. "dlls/wineps.drv/afm2c.c" => 1,
  47. "dlls/wineps.drv/mkagl.c" => 1,
  48. "include/config.h.in" => 1,
  49. "include/stamp-h.in" => 1,
  50. "programs/winetest/dist.rc" => 1,
  51. "tools/makedep.c" => 1,
  52. );
  53. my @source_vars = (
  54. "BISON_SRCS",
  55. "C_SRCS",
  56. "FONT_SRCS",
  57. "HEADER_SRCS",
  58. "IDL_SRCS",
  59. "IN_SRCS",
  60. "LEX_SRCS",
  61. "MANPAGES",
  62. "MC_SRCS",
  63. "OBJC_SRCS",
  64. "PO_SRCS",
  65. "RC_SRCS",
  66. "SOURCES",
  67. "SVG_SRCS",
  68. "XTEMPLATE_SRCS"
  69. );
  70. my (@makefiles, %makefiles);
  71. my @inf_files;
  72. my @nls_files;
  73. sub dirname($)
  74. {
  75. my $ret = shift;
  76. return "" unless $ret =~ /\//;
  77. $ret =~ s!/[^/]*$!!;
  78. return $ret;
  79. }
  80. # update a file if changed
  81. sub update_file($$)
  82. {
  83. my $file = shift;
  84. my $new = shift;
  85. open FILE, ">$file.new" or die "cannot create $file.new";
  86. print FILE $new;
  87. close FILE;
  88. rename "$file.new", "$file";
  89. print "$file updated\n";
  90. if ($file eq "configure.ac")
  91. {
  92. system "autoconf";
  93. print "configure updated\n";
  94. }
  95. }
  96. # replace some lines in a file between two markers
  97. sub replace_in_file($$$@)
  98. {
  99. my $file = shift;
  100. my $start = shift;
  101. my $end = shift;
  102. my ($old, $new);
  103. open OLD_FILE, "$file" or die "cannot open $file";
  104. while (<OLD_FILE>)
  105. {
  106. $old .= $_;
  107. last if /$start/;
  108. $new .= $_;
  109. }
  110. $new .= join "", @_;
  111. my $skip = 1;
  112. while (<OLD_FILE>)
  113. {
  114. $old .= $_;
  115. $new .= $_ unless $skip;
  116. $skip = 0 if /$end/;
  117. }
  118. close OLD_FILE;
  119. update_file($file, $new) if $old ne $new;
  120. }
  121. # replace all source variables in a makefile
  122. sub replace_makefile_variables($)
  123. {
  124. my $file = shift;
  125. my $make = $makefiles{$file};
  126. my $source_vars_regexp = join "|", @source_vars;
  127. my %replaced;
  128. my $old;
  129. my $new;
  130. open OLD_FILE, "$file.in" or die "cannot open $file.in";
  131. while (<OLD_FILE>)
  132. {
  133. $old .= $_;
  134. if (/^\s*($source_vars_regexp)(\s*)=/)
  135. {
  136. # try to preserve formatting
  137. my $var = $1;
  138. my $spaces = $2;
  139. my $replaced = 0;
  140. my @values;
  141. if (defined ${$make}{"=$var"})
  142. {
  143. @values = @{${$make}{"=$var"}};
  144. ${$make}{$var} = \@values;
  145. }
  146. else
  147. {
  148. undef ${$make}{$var};
  149. }
  150. my $multiline = /\\$/ || (@values > 1);
  151. my $old_str = $_;
  152. while (/\\$/)
  153. {
  154. $_ = <OLD_FILE>;
  155. last unless $_;
  156. $old .= $_;
  157. $old_str .= $_;
  158. }
  159. my $new_str = "";
  160. if (!@values)
  161. {
  162. # nothing
  163. }
  164. elsif ($multiline)
  165. {
  166. $new_str = "$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
  167. $new .= $new_str;
  168. }
  169. else
  170. {
  171. $new_str = "$var$spaces= @values\n";
  172. $new .= $new_str;
  173. }
  174. $replaced{$var} = 1;
  175. next;
  176. }
  177. $new .= $_;
  178. }
  179. # if we are using SOURCES, ignore the other variables
  180. unless ($replaced{"SOURCES"})
  181. {
  182. foreach my $var (@source_vars)
  183. {
  184. next if defined $replaced{$var};
  185. next if $var eq "SOURCES";
  186. next unless defined ${$make}{"=$var"};
  187. my @values = @{${$make}{"=$var"}};
  188. next unless @values;
  189. $new .= "\n$var = \\\n\t" . join(" \\\n\t", sort @values) . "\n";
  190. }
  191. }
  192. close OLD_FILE;
  193. update_file("$file.in", $new) if $old ne $new;
  194. }
  195. # parse the specified makefile and load the variables
  196. sub parse_makefile($)
  197. {
  198. my $file = shift;
  199. my %make;
  200. ($make{"=dir"} = $file) =~ s/[^\/]+$//;
  201. open MAKE, "$file.in" or die "cannot open $file.in\n";
  202. while (<MAKE>)
  203. {
  204. chomp;
  205. next if (/^\s*#/);
  206. while (/\\$/) { chop; $_ .= <MAKE>; chomp; } # merge continued lines
  207. next if (/^\s*$/);
  208. if (/\@[A-Z_]+\@/) # config.status substitution variable
  209. {
  210. die "Configure substitution is not allowed in $file" unless $file eq "Makefile";
  211. }
  212. if (/^\s*(MODULE|IMPORTLIB|TESTDLL|PARENTSRC|APPMODE|EXTRADLLFLAGS)\s*=\s*(.*)/)
  213. {
  214. my $var = $1;
  215. $make{$var} = $2;
  216. next;
  217. }
  218. my $source_vars_regexp = join "|", @source_vars;
  219. if (/^\s*($source_vars_regexp|PROGRAMS|EXTRA_TARGETS|EXTRA_OBJS|INSTALL_LIB|INSTALL_DEV)\s*=\s*(.*)/)
  220. {
  221. my $var = $1;
  222. my @list = split(/\s+/, $2);
  223. $make{$var} = \@list;
  224. next;
  225. }
  226. if (/^\s*(TOPSRCDIR|TOPOBJDIR|SRCDIR|VPATH)\s*=\s*(.*)/)
  227. {
  228. die "Variable $1 in $file.in is obsolete";
  229. }
  230. }
  231. return %make;
  232. }
  233. # read pragma makedep flags from a source file
  234. sub get_makedep_flags($)
  235. {
  236. my $file = shift;
  237. my %flags;
  238. open FILE, $file or die "cannot open $file";
  239. if ($file =~ /\.sfd$/)
  240. {
  241. while (<FILE>)
  242. {
  243. next unless /^UComments:\s*\"(.*)\"$/;
  244. foreach my $pragma (split /\+AAoA/, $1)
  245. {
  246. next unless $pragma =~ /^#\s*pragma\s+makedep\s+(.*)/;
  247. foreach my $flag (split /\s+/, $1)
  248. {
  249. $flags{$flag} = 1;
  250. last if $flag eq "font";
  251. }
  252. }
  253. }
  254. }
  255. else
  256. {
  257. while (<FILE>)
  258. {
  259. next unless /^#\s*pragma\s+makedep\s+(.*)/;
  260. foreach my $flag (split /\s+/, $1)
  261. {
  262. last if $flag eq "depend";
  263. $flags{$flag} = 1;
  264. }
  265. }
  266. }
  267. close FILE;
  268. return %flags;
  269. }
  270. sub get_parent_makefile($)
  271. {
  272. my $file = shift;
  273. my %make = %{$makefiles{$file}};
  274. my $reldir = $make{"PARENTSRC"} || "";
  275. return "" unless $reldir;
  276. (my $path = $file) =~ s/\/Makefile$/\//;
  277. while ($reldir =~ /^\.\.\//)
  278. {
  279. $reldir =~ s/^\.\.\///;
  280. $path =~ s/[^\/]+\/$//;
  281. }
  282. return "$path$reldir/Makefile";
  283. }
  284. # preserve shared source files that are listed in the existing makefile
  285. sub preserve_shared_source_files($$$)
  286. {
  287. my ($make, $parent, $var) = @_;
  288. my %srcs;
  289. return unless defined ${$parent}{"=$var"};
  290. foreach my $file (@{${$parent}{"=$var"}}) { $srcs{$file} = 1; }
  291. foreach my $file (@{${$make}{"=$var"}}) { $srcs{$file} = 0; }
  292. foreach my $file (@{${$make}{$var}})
  293. {
  294. next unless defined $srcs{$file} && $srcs{$file} == 1;
  295. push @{${$make}{"=$var"}}, $file;
  296. }
  297. }
  298. # assign source files to their respective makefile
  299. sub assign_sources_to_makefiles(@)
  300. {
  301. foreach my $file (@_)
  302. {
  303. next if defined $ignored_source_files{$file};
  304. next if $file =~ /Makefile\.in$/;
  305. my $dir = dirname( $file );
  306. my $subdir = $dir;
  307. while ($dir && !defined $makefiles{"$dir/Makefile"}) { $dir = dirname( $dir ); }
  308. $subdir =~ s/^$dir\/?//;
  309. next unless $dir;
  310. die "no makefile found for $file\n" unless defined $makefiles{"$dir/Makefile"};
  311. my $make = $makefiles{"$dir/Makefile"};
  312. my $name = substr( $file, length($dir) + 1 );
  313. next if $file =~ /^include\/wine\// && !get_makedep_flags($file) && !$exported_wine_headers{$name};
  314. if ($name =~ /\.m$/) { push @{${$make}{"=OBJC_SRCS"}}, $name; }
  315. elsif ($name =~ /\.l$/) { push @{${$make}{"=LEX_SRCS"}}, $name; }
  316. elsif ($name =~ /\.y$/) { push @{${$make}{"=BISON_SRCS"}}, $name; }
  317. elsif ($name =~ /\.svg$/) { push @{${$make}{"=SVG_SRCS"}}, $name; }
  318. elsif ($name =~ /\.sfd$/)
  319. {
  320. push @{${$make}{"=FONT_SRCS"}}, $name;
  321. }
  322. elsif ($name =~ /\.c$/)
  323. {
  324. push @{${$make}{"=C_SRCS"}}, $name;
  325. }
  326. elsif ($name =~ /\.h$/ || $name =~ /\.rh$/ || $name =~ /\.inl$/ || $name =~ /\.x$/)
  327. {
  328. next if $dir ne "include";
  329. }
  330. elsif ($name =~ /\.rc$/)
  331. {
  332. push @{${$make}{"=RC_SRCS"}}, $name;
  333. }
  334. elsif ($name =~ /\.mc$/)
  335. {
  336. push @{${$make}{"=MC_SRCS"}}, $name;
  337. }
  338. elsif ($name =~ /\.po$/)
  339. {
  340. push @{${$make}{"=PO_SRCS"}}, $name;
  341. }
  342. elsif ($name =~ /\.idl$/)
  343. {
  344. die "no makedep flags specified in $file" unless $dir eq "include" || get_makedep_flags($file);
  345. push @{${$make}{"=IDL_SRCS"}}, $name;
  346. }
  347. elsif ($name =~ /\.man\.in$/)
  348. {
  349. push @{${$make}{"=MANPAGES"}}, $name;
  350. }
  351. elsif ($name =~ /\.inf\.in$/)
  352. {
  353. push @inf_files, $name unless $name eq "wine.inf.in";
  354. }
  355. elsif ($name =~ /\.in$/)
  356. {
  357. push @{${$make}{"=IN_SRCS"}}, $name;
  358. }
  359. elsif ($name =~ /\.spec$/)
  360. {
  361. next unless defined ${$make}{"TESTDLL"};
  362. }
  363. elsif ($name =~ /\.nls$/)
  364. {
  365. push @nls_files, $name if $dir eq "nls";
  366. }
  367. elsif ($dir ne "loader") # loader dir contains misc files
  368. {
  369. next;
  370. }
  371. push @{${$make}{"=SOURCES"}}, $name;
  372. }
  373. # preserve shared source files from the parent makefile
  374. foreach my $file (@makefiles)
  375. {
  376. my $make = $makefiles{$file};
  377. my $parent = get_parent_makefile( $file );
  378. next unless $parent;
  379. preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "C_SRCS" );
  380. preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "RC_SRCS" );
  381. preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "IDL_SRCS" );
  382. preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "LEX_SRCS" );
  383. preserve_shared_source_files( $makefiles{$file}, $makefiles{$parent}, "BISON_SRCS" );
  384. }
  385. }
  386. ################################################################
  387. # update the makefile list in configure.ac
  388. sub update_makefiles(@)
  389. {
  390. my (@lines);
  391. foreach my $file (sort @_)
  392. {
  393. next if $file eq "Makefile";
  394. my %make = %{$makefiles{$file}};
  395. (my $dir = $file) =~ s/^(.*)\/Makefile/$1/;
  396. my $args = "";
  397. if (defined($make{"TESTDLL"})) # test
  398. {
  399. die "TESTDLL should not be defined in $file" unless $file =~ /\/tests\/Makefile$/;
  400. die "MODULE should not be defined in $file" if defined $make{"MODULE"};
  401. die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
  402. }
  403. elsif (defined($make{"MODULE"}) && $make{"MODULE"} =~ /\.a$/) # import lib
  404. {
  405. die "MODULE should not be defined as static lib in $file" unless $file =~ /^dlls\//;
  406. die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
  407. die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
  408. }
  409. elsif (defined($make{"MODULE"})) # dll or program
  410. {
  411. (my $name = $file) =~ s/^(dlls|programs)\/(.*)\/Makefile/$2/;
  412. my $dllflags = $make{"EXTRADLLFLAGS"} || "";
  413. if (defined $make{"APPMODE"}) { $dllflags .= " " . $make{"APPMODE"}; }
  414. die "MODULE should not be defined in $file" unless $file =~ /^(dlls|programs)\//;
  415. die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
  416. die "Invalid MODULE in $file" if $name =~ /\./ && $make{"MODULE"} ne $name;
  417. if ($file =~ /^programs\//)
  418. {
  419. die "EXTRADLLFLAGS should be defined in $file" unless $dllflags;
  420. die "EXTRADLLFLAGS should contain -mconsole or -mwindows in $file" unless $dllflags =~ /-m(console|windows)/;
  421. die "Invalid MODULE in $file" unless $name =~ /\./ || $make{"MODULE"} eq "$name.exe";
  422. }
  423. else
  424. {
  425. die "APPMODE should not be defined in $file" if defined $make{"APPMODE"} ;
  426. die "EXTRADLLFLAGS should not contain -mconsole or -mwindows in $file" if $dllflags =~ /-m(console|windows)/;
  427. die "Invalid MODULE in $file" unless $name =~ /\./ || $make{"MODULE"} eq "$name.dll";
  428. }
  429. if (defined $make{"IMPORTLIB"})
  430. {
  431. die "IMPORTLIB not allowed in programs\n" if $file =~ /^programs\//;
  432. die "Invalid IMPORTLIB name in $file" if $make{"IMPORTLIB"} =~ /\./;
  433. }
  434. $args = ",enable_win16" if $make{"MODULE"} =~ /16$/ || $modules16{$make{"MODULE"}};
  435. }
  436. elsif ($file =~ /^tools.*\/Makefile$/)
  437. {
  438. die "APPMODE should not be defined in $file" if defined $make{"APPMODE"};
  439. die "EXTRADLLFLAGS should not be defined in $file" if defined $make{"EXTRADLLFLAGS"};
  440. die "STATICLIB should not be defined in $file" if defined $make{"STATICLIB"};
  441. $args = ",,[test \"x\$enable_tools\" = xno]";
  442. }
  443. push @lines, "WINE_CONFIG_MAKEFILE($dir$args)\n";
  444. }
  445. # update the source variables in all the makefiles
  446. foreach my $file (sort @_) { replace_makefile_variables( $file ); }
  447. push @lines, "dnl End of auto-generated output commands\n";
  448. replace_in_file( "configure.ac", '^WINE_CONFIG_MAKEFILE', '^dnl End of auto-generated output commands\n$', @lines);
  449. }
  450. sub update_wine_inf()
  451. {
  452. my @lines;
  453. push @lines, "[InfFiles]", sort grep { s/\.in$//; } @inf_files;
  454. push @lines, "\n[NlsFiles]", sort grep(!/^sort/, @nls_files);
  455. push @lines, "\n[SortFiles]", sort grep(/^sort/, @nls_files);
  456. push @lines, "\n[WineSourceDirs]\n";
  457. replace_in_file "loader/wine.inf.in", '^\[InfFiles\]', '^\[WineSourceDirs\]', join( "\n", @lines );
  458. }
  459. my $git_dir = $ENV{GIT_DIR} || ".git";
  460. die "needs to be run from a git checkout" unless -e $git_dir;
  461. my @all_files = split /\0/, `git ls-files -c -z`;
  462. map { $ignored_source_files{$_} = 1; } split /\0/, `git ls-files -d -z`;
  463. @makefiles = map { (my $ret = $_) =~ s/\.in$//; $ret; } grep /Makefile.in$/, @all_files;
  464. foreach my $file (sort @makefiles)
  465. {
  466. my %make = parse_makefile( $file );
  467. $makefiles{$file} = \%make;
  468. }
  469. assign_sources_to_makefiles( @all_files );
  470. update_makefiles( @makefiles );
  471. update_wine_inf();