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