Merge branch 'nasm-2.03.x'
[platform/upstream/nasm.git] / syncfiles.pl
1 #!/usr/bin/perl
2 #
3 # Sync the output file list between Makefiles
4 # Use the mkdep.pl parameters to get the filename syntax
5 #
6 # The first file is the source file; the other ones target.
7 #
8 %def_hints = ('object-ending' => '.o',
9               'path-separator' => '/',
10               'continuation' => "\\");
11
12 sub do_transform($$) {
13     my($l, $h) = @_;
14     my($ps) = $$h{'path-separator'};
15
16     $l =~ s/\x01/$$h{'object-ending'}/g;
17     $l =~ s/\x03/$$h{'continuation'}/g;
18
19     if ($ps eq '') {
20         # Remove the path separator and the preceeding directory
21         $l =~ s/\S*\x02//g;
22     } else {
23         # Convert the path separator
24         $l =~ s/\x02/$ps/g;
25     }
26
27     return $l;
28 }
29
30 @file_list = ();
31
32 $first = 1;
33 $first_file = $ARGV[0];
34 die unless (defined($first_file));
35
36 foreach $file (@ARGV) {
37     open(FILE, "< $file\0") or die;
38
39     # First, read the syntax hints
40     %hints = %def_hints;
41     while (defined($line = <FILE>)) {
42         if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
43             $hints{$1} = $2;
44         }
45     }
46
47     # Read and process the file
48     seek(FILE,0,0);
49     @lines = ();
50     $processing = 0;
51     while (defined($line = <FILE>)) {
52         chomp $line;
53         if ($processing) {
54             if ($line eq '#--- End File Lists ---#') {
55                 push(@lines, $line."\n");
56                 $processing = 0;
57             } elsif ($first) {
58                 my $xl = $line;
59                 my $oe = "\Q$hints{'object-ending'}";
60                 my $ps = "\Q$hints{'path-separator'}";
61                 my $cn = "\Q$hints{'continuation'}";
62
63                 $xl =~ s/${oe}(\s|$)/\x01$1/g;
64                 $xl =~ s/${ps}/\x02/g;
65                 $xl =~ s/${cn}$/\x03/;
66                 push(@file_list, $xl);
67                 push(@lines, $line);
68             }
69         } else {
70             push(@lines, $line."\n");
71             if ($line eq '#--- Begin File Lists ---#') {
72                 $processing = 1;
73                 if (!$first) {
74                     push(@lines, "# Edit in $first_file, not here!\n");
75                     foreach $l (@file_list) {
76                         push(@lines, do_transform($l, \%hints)."\n");
77                     }
78                 }
79             }
80         }
81     }
82     close(FILE);
83
84     # Write the file back out
85     if (!$first) {
86         open(FILE, "> $file\0") or die;
87         print FILE @lines;
88         close(FILE);
89     }
90
91     undef @lines;
92     $first = 0;
93 }