syncfiles.pl 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. # Sync the output file list between Makefiles
  36. # Use the mkdep.pl parameters to get the filename syntax
  37. #
  38. # The first file is the source file; the other ones target.
  39. #
  40. %def_hints = ('object-ending' => '.o',
  41. 'path-separator' => '/',
  42. 'continuation' => "\\");
  43. sub do_transform($$) {
  44. my($l, $h) = @_;
  45. my($ps) = $$h{'path-separator'};
  46. $l =~ s/\x01/$$h{'object-ending'}/g;
  47. $l =~ s/\x03/$$h{'continuation'}/g;
  48. if ($ps eq '') {
  49. # Remove the path separator and the preceeding directory
  50. $l =~ s/[^\s\=]*\x02//g;
  51. } else {
  52. # Convert the path separator
  53. $l =~ s/\x02/$ps/g;
  54. }
  55. return $l;
  56. }
  57. undef %line_lists;
  58. $first = 1;
  59. $first_file = $ARGV[0];
  60. die unless (defined($first_file));
  61. foreach $file (@ARGV) {
  62. open(FILE, '<', $file) or die;
  63. # First, read the syntax hints
  64. %hints = %def_hints;
  65. while (defined($line = <FILE>)) {
  66. if ( $line =~ /^\s*\#\s*@([a-z0-9-]+):\s*\"([^\"]*)\"/ ) {
  67. $hints{$1} = $2;
  68. }
  69. }
  70. # Read and process the file
  71. seek(FILE,0,0);
  72. @lines = ();
  73. undef $processing;
  74. while (defined($line = <FILE>)) {
  75. chomp $line;
  76. if (defined($processing)) {
  77. if ($line =~ /^\#-- End ([^-\#]*[^-#\s]) --\#$/) {
  78. if ($1 ne $processing) {
  79. die "$0: $file: Mismatched Begin and End lines (\"$processing\" -> \"$1\"\n";
  80. }
  81. push(@lines, $line."\n");
  82. undef $processing;
  83. } elsif ($first) {
  84. my $xl = $line;
  85. my $oe = "\Q$hints{'object-ending'}";
  86. my $ps = "\Q$hints{'path-separator'}";
  87. my $cn = "\Q$hints{'continuation'}";
  88. $xl =~ s/${oe}(\s|$)/\x01$1/g;
  89. $xl =~ s/${ps}/\x02/g;
  90. $xl =~ s/${cn}$/\x03/;
  91. push(@{$line_lists{$processing}}, $xl);
  92. push(@lines, $line);
  93. }
  94. } else {
  95. push(@lines, $line."\n");
  96. if ($line =~ '#-- Begin ([^-\#]*[^-#\s]) --#') {
  97. $processing = $1;
  98. if ($first) {
  99. if (defined($line_lists{$processing})) {
  100. die "$0: $file: Repeated Begin block: $processing\n";
  101. }
  102. $line_lists{$processing} = [];
  103. } elsif (!$first) {
  104. if (!defined($line_lists{$processing})) {
  105. die "$0: $file: Begin block without template\n";
  106. }
  107. push(@lines, "# Edit in $first_file, not here!\n");
  108. foreach $l (@{$line_lists{$processing}}) {
  109. push(@lines, do_transform($l, \%hints)."\n");
  110. }
  111. }
  112. }
  113. }
  114. }
  115. close(FILE);
  116. # Write the file back out
  117. if (!$first) {
  118. open(FILE, '>', $file) or die;
  119. print FILE @lines;
  120. close(FILE);
  121. }
  122. undef @lines;
  123. $first = 0;
  124. }