Debian Codegraph @

tools/syncfiles.pl

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