|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# run this from ../.. |
| 4 | + |
| 5 | +if [ "$1" == "--really" ]; then |
| 6 | + really_do_it=true |
| 7 | +else |
| 8 | + really_do_it=false |
| 9 | + echo "$0: this will not really do anything, use --really for that." |
| 10 | +fi |
| 11 | + |
| 12 | + |
| 13 | +cd src |
| 14 | + |
| 15 | +cat Makefile | perl -e ' @libdirs = (); while(<>){ if ($x && m/(\S+)\:/) { push @libdirs, $1; chop; print "$_ .phony\n"; } if (m/have inter-dependencies/) {$x=1;}} print("all: " . join(" ", @libdirs) . "\n"); print(".phony:\n")' > Makefile.temp |
| 16 | + |
| 17 | +# for each directory this automatic rule says, just print its name. |
| 18 | +echo '%:' >> Makefile.temp |
| 19 | +printf '\techo ${@F}\n' >> Makefile.temp |
| 20 | + |
| 21 | +# the following prints out the directory names in the order we want to |
| 22 | +# have them in the ADDLIBS in the individual Makefiles; note, 'tac' reverses |
| 23 | +# the order of its input lines. |
| 24 | +make -s -f Makefile.temp all | grep -v all | grep -v .phony | tac > library_order |
| 25 | + |
| 26 | +echo "Library order is:" |
| 27 | +cat library_order |
| 28 | + |
| 29 | +for f in */Makefile; do |
| 30 | + echo "$0: processing $f" |
| 31 | + cat $f | perl -e ' |
| 32 | + open(F, "<library_order") || die "opening file library_order"; |
| 33 | + $n = 1; |
| 34 | + while (<F>) { chop; $library_name_to_order{$_} = $n; $order_to_library_name{$n} = $_; $n++; } |
| 35 | + while(<>) { |
| 36 | + if (m/^ADDLIBS = (.+)/) { |
| 37 | + @addlibs = (); |
| 38 | + $cur_line = $1; |
| 39 | + while (1) { |
| 40 | + if ($cur_line =~ s/\\$//) { $had_backslash = 1; } else { $had_backslash = 0; } |
| 41 | + @A = split(" ", $cur_line); |
| 42 | + push @addlibs, @A; |
| 43 | + if (!$had_backslash) { last; } # break from the while loop. |
| 44 | + if (!($cur_line = <>)) { last; } |
| 45 | + } |
| 46 | + @weird_libs = (); |
| 47 | + %normal_lib_names = {}; |
| 48 | + foreach $lib (@addlibs) { |
| 49 | + if ($lib =~ m|^\.\./([a-z]+)/kaldi-([a-z]+)\.a$| && $1 == $2 && defined $library_name_to_order{$1}) { |
| 50 | + $normal_lib_names{$1} = 1; |
| 51 | + } else { push @weird_libs, $lib; } |
| 52 | + } |
| 53 | + @normalized_addlibs = (); |
| 54 | + for ($k = 1; $k < $n; $k++) { |
| 55 | + $test_name = $order_to_library_name{$k}; |
| 56 | + if (defined $normal_lib_names{$test_name}) { |
| 57 | + push @normalized_addlibs, "../$test_name/kaldi-$test_name.a"; |
| 58 | + } |
| 59 | + } |
| 60 | + if (@weird_libs > 0) { print STDERR "Unexpected libraries: " . join(":", @weird_libs); } |
| 61 | + # unexpected libraries that aren not part of the normal list will go last. |
| 62 | + push @normalized_addlibs, @weird_libs; |
| 63 | + @rearranged_lines = (); |
| 64 | + $cur_line = ""; |
| 65 | + $max_partial_line_size = 70; # after the initial "ADDLIBS = " or spaces. |
| 66 | + foreach $lib (@normalized_addlibs) { |
| 67 | + if (length($cur_line . $lib . " ") > $max_partial_line_size) { |
| 68 | + push @rearranged_lines, $cur_line; $cur_line = ""; |
| 69 | + } |
| 70 | + $cur_line .= ($lib . " "); |
| 71 | + } |
| 72 | + if ($cur_line ne "") { push @rearranged_lines, $cur_line; } |
| 73 | + $num_lines = @rearranged_lines; |
| 74 | + for ($k = 0; $k < $num_lines; $k++) { |
| 75 | + if ($k == 0) { print "ADDLIBS = "; } else { print " "; } |
| 76 | + print $rearranged_lines[$k]; |
| 77 | + if ($k + 1 < $num_lines) { print "\\\n"; } else { print "\n"; } |
| 78 | + } |
| 79 | + } else { |
| 80 | + print; |
| 81 | + } |
| 82 | + } ' > temp_makefile |
| 83 | + diff $f temp_makefile |
| 84 | + if $really_do_it; then |
| 85 | + cp temp_makefile $f |
| 86 | + fi |
| 87 | +done |
| 88 | + |
| 89 | +rm library_order Makefile.temp |
0 commit comments