make_makefiles 15 KB

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