2
0

fixup-makefile.pl 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #!/usr/bin/env perl
  2. use strict;
  3. my $error;
  4. my %state;
  5. sub usage() {
  6. die <<EOF;
  7. Usage: $0 <file> <command> [<arguments>]
  8. Commands:
  9. add-hash <variable> <value>
  10. fix-hash <variable> <value>
  11. rename-var <variable> <name>
  12. EOF
  13. }
  14. sub set_var($) {
  15. my $var = shift;
  16. $state{var} = $var;
  17. if ($var =~ /(.*):(.*)/) {
  18. $state{template} = $1;
  19. $state{var} = $2;
  20. $state{related_var} = "URL";
  21. } else {
  22. $state{context} = 1;
  23. $state{related_var} = "PKG_SOURCE_URL";
  24. }
  25. }
  26. my %check_command = (
  27. "add-hash" => sub {
  28. set_var($ARGV[0]);
  29. $state{value} = $ARGV[1];
  30. length($ARGV[1]) == 64 or die "Invalid hash value\n";
  31. },
  32. "fix-hash" => sub {
  33. set_var($ARGV[0]);
  34. $state{value} = $ARGV[1];
  35. $state{prev_value} = $ARGV[2];
  36. length($ARGV[1]) == 64 or die "Invalid hash value\n";
  37. },
  38. "rename-var" => sub {
  39. set_var($ARGV[0]);
  40. $state{new_var} = $ARGV[1];
  41. $state{new_var} =~ s/.*://g;
  42. },
  43. );
  44. sub check_context($) {
  45. my $line = shift;
  46. return unless $state{template};
  47. $state{next} and do {
  48. $state{context} = 1;
  49. undef $state{next};
  50. return;
  51. };
  52. if (not $state{context}) {
  53. $line =~ /^\s*define\s+$state{template}/ and $state{next} = 1;
  54. } else {
  55. $line =~ /^\s*endef/ and do {
  56. $state{done} = 1;
  57. undef $state{context};
  58. }
  59. }
  60. }
  61. my %commands = (
  62. "add-hash" => sub {
  63. my $line = shift;
  64. check_context($line);
  65. return $line unless $state{context};
  66. # skip existing hash variable
  67. return "" if $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)\n/;
  68. # insert md5sum after related variable
  69. return $line unless $line =~ /^(\s*)$state{related_var}(\s*):?=(\s*)(.*)\n/;
  70. return "$line$1$state{var}$2:=$3$state{value}\n";
  71. },
  72. "fix-hash" => sub {
  73. my $line = shift;
  74. check_context($line);
  75. return $line unless $state{context};
  76. return $line unless $line =~ /^(\s*)$state{var}(\s*):?=(\s*)(.*)$state{prev_value}(.*)\n/;
  77. $state{done} = 1;
  78. $4 =~ /\$/ and do {
  79. warn "$state{var} contains a reference to another variable, can't fix automatically\n";
  80. return $line;
  81. };
  82. return "$1$state{var}$2:=$3$state{value}\n";
  83. },
  84. "rename-var" => sub {
  85. my $line = shift;
  86. check_context($line);
  87. return $line unless $state{context};
  88. return $line unless $line =~ /^(\s*)$state{var}(\s*:?=.*)\n/;
  89. return "$1$state{new_var}$2\n";
  90. },
  91. );
  92. my $file = shift @ARGV;
  93. my $command = shift @ARGV;
  94. ($file and $command and $check_command{$command}) or usage;
  95. &{$check_command{$command}}();
  96. -f $file or die "File $file not found\n";
  97. open IN, "<${file}" or die "Cannot open input file\n";
  98. open OUT, ">${file}.new" or die "Cannot open output file\n";
  99. my $cmd = $commands{$command};
  100. while (my $line = <IN>) {
  101. $line = &$cmd($line) unless $state{done};
  102. print OUT $line;
  103. last if $error;
  104. }
  105. close OUT;
  106. close IN;
  107. $error and do {
  108. unlink "${file}.new";
  109. exit 1;
  110. };
  111. rename "${file}.new", "$file";