1 | #! /usr/bin/perl -w |
---|
2 | |
---|
3 | # $Id$ |
---|
4 | |
---|
5 | # Copyright (C) 2005, 2006 Ralf Corsépius, Ulm, Germany |
---|
6 | # |
---|
7 | # Permission to use, copy, modify, and distribute this software |
---|
8 | # is freely granted, provided that this notice is preserved. |
---|
9 | |
---|
10 | # Helper script to generate pre/tmpinstall rules for RTEMS Makefile.am. |
---|
11 | # |
---|
12 | # Usage: ampolish3 Makefile.am > preinstall.am |
---|
13 | # |
---|
14 | # Reads a Makefile.am from stdin and writes corresponding |
---|
15 | # pre/tmpinstall rules to stdout. |
---|
16 | |
---|
17 | sub replace($); |
---|
18 | sub print_dirstamp($$$); |
---|
19 | |
---|
20 | # Predefined directory mappings: |
---|
21 | # |
---|
22 | # final-installation directory => temp installation directory |
---|
23 | my %dirmap = ( |
---|
24 | '$(includedir)' => '$(PROJECT_INCLUDE)', |
---|
25 | '$(libdir)' => '$(PROJECT_LIB)', |
---|
26 | '$(project_libdir)' => '$(PROJECT_LIB)', |
---|
27 | '$(project_includedir)' => '$(PROJECT_INCLUDE)' |
---|
28 | ); |
---|
29 | |
---|
30 | # Conventions on automake primaries: |
---|
31 | # |
---|
32 | # *_HEADERS -> preinstall |
---|
33 | # noinst*_HEADERS -> noinst |
---|
34 | # noinst_*_LIBRARIES -> noinst |
---|
35 | # project_*_LIBRARIES -> tmpinstall |
---|
36 | # *_LIBRARIES -> ignore (no preinstallation) |
---|
37 | # dist_project_*_DATA -> preinstall (bsp_specs,linkcmds) |
---|
38 | # project_*_DATA -> tmpinstall (*.o, *.a) |
---|
39 | # dist_*_DATA -> ignore (no preinstallation) |
---|
40 | # *SCRIPTS -> ignore (no preinstallation) |
---|
41 | # noinst_*_PROGRAMS -> noinst |
---|
42 | # project_*_PROGRAMS -> tmpinstall |
---|
43 | # *_PROGRAMS -> ignore (no preinstallation) |
---|
44 | |
---|
45 | ## 1st pass: read in file |
---|
46 | my @buffer1 = () ; |
---|
47 | my %seen = (); |
---|
48 | my %predefs = (); |
---|
49 | |
---|
50 | { |
---|
51 | my $mode = 0 ; |
---|
52 | my $line = ''; |
---|
53 | |
---|
54 | while ( <> ) |
---|
55 | { |
---|
56 | if ( $mode == 0 ) |
---|
57 | { |
---|
58 | if ( /^([a-zA-Z0-9_]+\s*[\+]?[:=].*)\\$/o ) |
---|
59 | { |
---|
60 | $line = "$1" ; |
---|
61 | $mode = 1; |
---|
62 | } else { |
---|
63 | push @buffer1, $_ ; |
---|
64 | } |
---|
65 | } elsif ( $mode == 1 ) { |
---|
66 | if ( /^(.*)\\$/o ) { |
---|
67 | $line .= $1; |
---|
68 | } else { |
---|
69 | $line .= $_ ; |
---|
70 | push @buffer1, $line ; |
---|
71 | $line = ''; |
---|
72 | $mode = 0 ; |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | #foreach my $l ( @buffer1 ) { print STDERR "1:<$l>"; } |
---|
79 | |
---|
80 | # Filter out all Makefile code not relevant here |
---|
81 | my @buffer2 = (); |
---|
82 | foreach my $l ( @buffer1 ) { |
---|
83 | if ( $l=~ /^\t.*$/o ) |
---|
84 | { #ignore: Production of a make rule. |
---|
85 | } elsif ( $l =~ /^\s*([a-zA-Z0-9_]*dir)\s*\=\s*(.*)\s*$/o ) |
---|
86 | { # dirs |
---|
87 | push @buffer2, "$l"; |
---|
88 | $dirmap{"\$\($1\)"} = replace($2); |
---|
89 | } elsif ( $l =~ /^\s*noinst_(.*)\s*[\+]?\=(.*)$/o ) |
---|
90 | { |
---|
91 | #ignore: noinst_* are not relevant here. |
---|
92 | } elsif ( $l =~ /^\s*(nodist_|dist_|)(project_|)([a-zA-Z0-9_]+)_(HEADERS|LIBRARIES|DATA|SCRIPTS|PROGRAMS)\s*([\+]?\=)\s*(.*)/o ) |
---|
93 | { |
---|
94 | if ( ( "$5" eq '=' ) ) { |
---|
95 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
96 | if ( $v =~ /\$\(PROJECT_[^\)]+\)$/ ) |
---|
97 | { |
---|
98 | $predefs{"$v"} = 1; |
---|
99 | } |
---|
100 | } |
---|
101 | foreach my $f ( split(' ',$6) ) { |
---|
102 | push @buffer2, "$1$2$3_$4 +=$f\n"; |
---|
103 | } |
---|
104 | } elsif ( $l =~ /^\s*(if|else|endif)\s*.*$/o ) |
---|
105 | { # conditionals |
---|
106 | push @buffer2, "$l"; |
---|
107 | } |
---|
108 | |
---|
109 | # Check if Makefile.am already contains CLEANFILES or DISTCLEANFILES |
---|
110 | if ( $l =~ /^\s*(CLEANFILES|DISTCLEANFILES|SUBDIRS)\s*\=.*$/o ) |
---|
111 | { |
---|
112 | $predefs{"$1"} = 1; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | if ( $predefs{"\$(PROJECT_INCLUDE)"} ){ |
---|
117 | unshift @buffer2, "includedir = \$(includedir)\n"; |
---|
118 | } |
---|
119 | if ( $predefs{"\$(PROJECT_LIB)"} ){ |
---|
120 | unshift @buffer2, "libdir = \$(libdir)\n"; |
---|
121 | } |
---|
122 | |
---|
123 | # foreach my $l ( @buffer2 ) { print STDERR "2:<$l>"; } |
---|
124 | |
---|
125 | my @buffer3 = (); |
---|
126 | |
---|
127 | foreach my $l ( @buffer2 ) { |
---|
128 | if ( $l =~ /^\s*([a-zA-Z0-9_]*dir)\s*\=\s*(.*)\s*$/o ) |
---|
129 | { # dirs |
---|
130 | my $v = $dirmap{"\$\($1\)"}; |
---|
131 | print_dirstamp(\@buffer3,$v,"PREINSTALL_DIRS"); |
---|
132 | $seen{"PREINSTALL_DIRS"} = 1; |
---|
133 | } elsif ( $l =~ /^\s*(nodist_|dist_|)(project_|)([a-zA-Z0-9_]+)_HEADERS\s*\+\=(.*)/o ) |
---|
134 | { # preinstall |
---|
135 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
136 | my $f = $4; |
---|
137 | my $x ; my $i = rindex($f,'/'); |
---|
138 | if ($i < 0) { $x="$f"; |
---|
139 | } else { $x = substr($f,$i+1); |
---|
140 | } |
---|
141 | push @buffer3, |
---|
142 | "$v/$x: $f $v/\$(dirstamp)\n", |
---|
143 | "\t\$(INSTALL_DATA) \$< $v/$x\n", |
---|
144 | "PREINSTALL_FILES += $v/$x\n\n"; |
---|
145 | $seen{"PREINSTALL_FILES"} = 1; |
---|
146 | } elsif ( $l =~ /^\s*(nodist_|dist_|)(project_)([a-zA-Z0-9_]+)_LIBRARIES\s*\+\=(.*)/o ) |
---|
147 | { # tmpinstall |
---|
148 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
149 | my $f = $4; |
---|
150 | my $x ; my $i = rindex($f,'/'); |
---|
151 | if ($i < 0) { $x="$f"; |
---|
152 | } else { $x = substr($f,$i+1); |
---|
153 | } |
---|
154 | push @buffer3, |
---|
155 | "$v/$x: $f $v/\$(dirstamp)\n", |
---|
156 | "\t\$(INSTALL_DATA) \$< $v/$x\n", |
---|
157 | "TMPINSTALL_FILES += $v/$x\n\n"; |
---|
158 | $seen{"TMPINSTALL_FILES"} = 1; |
---|
159 | } elsif ( $l =~ /^\s*(nodist_|dist_|)([a-zA-Z0-9_]+)_LIBRARIES\s*\+\=(.*)/o ) |
---|
160 | { # ignore |
---|
161 | } elsif ( $l =~ /^\s*(dist_)(project_)([a-zA-Z0-9_]+)_DATA\s*\+\=(.*)/o ) |
---|
162 | { # preinstall |
---|
163 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
164 | my $f = $4; |
---|
165 | my $x ; my $i = rindex($f,'/'); |
---|
166 | if ($i < 0) { $x="$f"; |
---|
167 | } else { $x = substr($f,$i+1); |
---|
168 | } |
---|
169 | push @buffer3, |
---|
170 | "$v/$x: $f $v/\$(dirstamp)\n", |
---|
171 | "\t\$(INSTALL_DATA) \$< $v/$x\n", |
---|
172 | "PREINSTALL_FILES += $v/$x\n\n"; |
---|
173 | $seen{"PREINSTALL_FILES"} = 1; |
---|
174 | } elsif ( $l =~ /^\s*(nodist_|)(project_)([a-zA-Z0-9_]+)_DATA\s*\+\=(.*)/o ) |
---|
175 | { # tmpinstall |
---|
176 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
177 | my $f = $4; |
---|
178 | my $x ; my $i = rindex($f,'/'); |
---|
179 | if ($i < 0) { $x="$f"; |
---|
180 | } else { $x = substr($f,$i+1); |
---|
181 | } |
---|
182 | push @buffer3, |
---|
183 | "$v/$x: $f $v/\$(dirstamp)\n", |
---|
184 | "\t\$(INSTALL_DATA) \$< $v/$x\n", |
---|
185 | "TMPINSTALL_FILES += $v/$x\n\n"; |
---|
186 | $seen{"TMPINSTALL_FILES"} = 1; |
---|
187 | } elsif ( $l =~ /^\s*(dist_|)([a-zA-Z0-9_]+)_DATA\s*\+\=(.*)/o ) |
---|
188 | { # ignore |
---|
189 | } elsif ( $l =~ /^\s*(nodist_|dist_|)([a-zA-Z0-9_]+)_SCRIPTS\s*\+\=(.*)/o ) |
---|
190 | { # ignore |
---|
191 | } elsif ( $l =~ /^\s*(nodist_|dist_|)(project_)([a-zA-Z0-9_]+)_PROGRAMS\s*\+\=(.*)/o ) |
---|
192 | { # tmpinstall |
---|
193 | my $v = $dirmap{"\$\($3dir\)"}; |
---|
194 | |
---|
195 | my $f = $4; |
---|
196 | my $x ; my $i = rindex($f,'/'); |
---|
197 | if ($i < 0) { $x="$f"; |
---|
198 | } else { $x = substr($f,$i+1); |
---|
199 | } |
---|
200 | push @buffer3, |
---|
201 | "$v/$x: $f $v/\$(dirstamp)\n", |
---|
202 | "\t\$(INSTALL_PROGRAM) \$< $v/$x\n", |
---|
203 | "TMPINSTALL_FILES += $v/$x\n\n"; |
---|
204 | $seen{"TMPINSTALL_FILES"} = 1; |
---|
205 | } elsif ( $l =~ /^\s*(nodist_|dist_|)([a-zA-Z0-9_]+)_PROGRAMS\s*\+\=(.*)/o ) |
---|
206 | { # ignore |
---|
207 | } elsif ( $l =~ /^\s*(if|else|endif)\s*.*$/o ) |
---|
208 | { # conditionals |
---|
209 | push @buffer3, "$l"; |
---|
210 | } |
---|
211 | } |
---|
212 | |
---|
213 | # foreach my $l ( @buffer3 ) { print STDERR "3:<$l>"; } |
---|
214 | |
---|
215 | my $output; |
---|
216 | $output .= "## Automatically generated by ampolish3 - Do not edit\n\n"; |
---|
217 | $output .= "if AMPOLISH3\n"; |
---|
218 | $output .= "\$(srcdir)/preinstall.am: Makefile.am\n"; |
---|
219 | $output .= "\t\$(AMPOLISH3) \$(srcdir)/Makefile.am > \$(srcdir)/preinstall.am\n"; |
---|
220 | $output .= "endif\n\n"; |
---|
221 | |
---|
222 | foreach my $k ( keys %seen ) |
---|
223 | { |
---|
224 | if ( $k =~ /PREINSTALL_FILES/o ) { |
---|
225 | $output .= "all-am: \$(PREINSTALL_FILES)\n\n"; |
---|
226 | |
---|
227 | $output .= "$k =\n"; |
---|
228 | $output .= "CLEANFILES "; |
---|
229 | if ( $predefs{"CLEANFILES"} ) { $output .= "+"; } |
---|
230 | $output .= "= \$($k)\n"; |
---|
231 | $predefs{"CLEANFILES"} = 1; |
---|
232 | } elsif ( $k =~ /TMPINSTALL_FILES/o ) { |
---|
233 | |
---|
234 | $output .= "all-local: \$(TMPINSTALL_FILES)\n\n"; |
---|
235 | |
---|
236 | $output .= "$k =\n"; |
---|
237 | $output .= "CLEANFILES "; |
---|
238 | if ( $predefs{"CLEANFILES"} ) { $output .= "+"; } |
---|
239 | $output .= "= \$($k)\n"; |
---|
240 | $predefs{"CLEANFILES"} = 1; |
---|
241 | } elsif ( $k =~ /.*DIRS/o ) { |
---|
242 | $output .= "$k =\n"; |
---|
243 | $output .= "DISTCLEANFILES "; |
---|
244 | if ( $predefs{"DISTCLEANFILES"} ) { $output .= "+"; } |
---|
245 | $output .= "= \$($k)\n"; |
---|
246 | $predefs{"DISTCLEANFILES"} = 1; |
---|
247 | } |
---|
248 | $output .= "\n"; |
---|
249 | } |
---|
250 | |
---|
251 | # Pretty printing |
---|
252 | $output .= join ( '', @buffer3 ); |
---|
253 | $output =~ s/\nelse\n+endif/\nendif/g; |
---|
254 | $output =~ s/\n\n+endif/\nendif/g; |
---|
255 | $output =~ s/\nif [a-zA-Z0-9_!]+\n+endif//g; |
---|
256 | print STDOUT $output; |
---|
257 | |
---|
258 | exit 0; |
---|
259 | |
---|
260 | sub replace($) |
---|
261 | { |
---|
262 | my ($v) = @_; |
---|
263 | foreach my $i ( keys %dirmap ) |
---|
264 | { |
---|
265 | $v =~ s/\Q$i/$dirmap{$i}/g; |
---|
266 | } |
---|
267 | return $v; |
---|
268 | } |
---|
269 | |
---|
270 | sub print_dirstamp($$$) |
---|
271 | { |
---|
272 | my ($obuf,$file,$inst) = @_ ; |
---|
273 | push @{$obuf}, "$file/\$(dirstamp):\n\t\@\$\(mkdir_p\) $file\n" ; |
---|
274 | push @{$obuf}, "\t\@: \> $file/\$(dirstamp)\n" ; |
---|
275 | push @{$obuf}, "$inst += $file/\$(dirstamp)\n\n" ; |
---|
276 | } |
---|