1#!/usr/bin/perl
2## --------------------------------------------------------------------------
3##
4##   Copyright 1996-2009 The NASM Authors - All Rights Reserved
5##   See the file AUTHORS included with the NASM distribution for
6##   the specific copyright holders.
7##
8##   Redistribution and use in source and binary forms, with or without
9##   modification, are permitted provided that the following
10##   conditions are met:
11##
12##   * Redistributions of source code must retain the above copyright
13##     notice, this list of conditions and the following disclaimer.
14##   * Redistributions in binary form must reproduce the above
15##     copyright notice, this list of conditions and the following
16##     disclaimer in the documentation and/or other materials provided
17##     with the distribution.
18##
19##     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
20##     CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
21##     INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22##     MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23##     DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24##     CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25##     SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26##     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27##     LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28##     HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29##     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30##     OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31##     EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32##
33## --------------------------------------------------------------------------
34
35#
36# Sync the output file list between Makefiles
37# Use the mkdep.pl parameters to get the filename syntax
38#
39# The first file is the source file; the other ones target.
40#
41%def_hints = ('object-ending' => '.o',
42	      'path-separator' => '/',
43	      'continuation' => "\\");
44
45sub do_transform($$) {
46    my($l, $h) = @_;
47    my($ps) = $$h{'path-separator'};
48
49    $l =~ s/\x01/$$h{'object-ending'}/g;
50    $l =~ s/\x03/$$h{'continuation'}/g;
51
52    if ($ps eq '') {
53	# Remove the path separator and the preceeding directory
54	$l =~ s/[^\s\=]*\x02//g;
55    } else {
56	# Convert the path separator
57	$l =~ s/\x02/$ps/g;
58    }
59
60    return $l;
61}
62
63undef %line_lists;
64
65$first = 1;
66$first_file = $ARGV[0];
67die unless (defined($first_file));
68
69foreach $file (@ARGV) {
70    open(FILE, '<', $file) or die;
71
72    # First, read the syntax hints
73    %hints = %def_hints;
74    while (defined($line = <FILE>)) {
75	if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
76	    $hints{$1} = $2;
77	}
78    }
79
80    # Read and process the file
81    seek(FILE,0,0);
82    @lines = ();
83    undef $processing;
84    while (defined($line = <FILE>)) {
85	chomp $line;
86	if (defined($processing)) {
87	    if ($line =~ /^\#-- End ([^-\#]*[^-#\s]) --\#$/) {
88		if ($1 ne $processing) {
89		    die "$0: $file: Mismatched Begin and End lines (\"$processing\" -> \"$1\"\n";
90		}
91		push(@lines, $line."\n");
92		undef $processing;
93	    } elsif ($first) {
94		my $xl = $line;
95		my $oe = "\Q$hints{'object-ending'}";
96		my $ps = "\Q$hints{'path-separator'}";
97		my $cn = "\Q$hints{'continuation'}";
98
99		$xl =~ s/${oe}(\s|$)/\x01$1/g;
100		$xl =~ s/${ps}/\x02/g;
101		$xl =~ s/${cn}$/\x03/;
102		push(@{$line_lists{$processing}}, $xl);
103		push(@lines, $line);
104	    }
105	} else {
106	    push(@lines, $line."\n");
107	    if ($line =~ '#-- Begin ([^-\#]*[^-#\s]) --#') {
108		$processing = $1;
109		if ($first) {
110		    if (defined($line_lists{$processing})) {
111			die "$0: $file: Repeated Begin block: $processing\n";
112		    }
113		    $line_lists{$processing} = [];
114		} elsif (!$first) {
115		    if (!defined($line_lists{$processing})) {
116			die "$0: $file: Begin block without template\n";
117		    }
118		    push(@lines, "# Edit in $first_file, not here!\n");
119		    foreach $l (@{$line_lists{$processing}}) {
120			push(@lines, do_transform($l, \%hints)."\n");
121		    }
122		}
123	    }
124	}
125    }
126    close(FILE);
127
128    # Write the file back out
129    if (!$first) {
130	open(FILE, '>', $file) or die;
131	print FILE @lines;
132	close(FILE);
133    }
134
135    undef @lines;
136    $first = 0;
137}
138