2
0

switch-timer-api 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use FindBin;
  6. my @legacy = qw(qemu_clock_ptr qemu_get_clock_ns qemu_get_clock_ms qemu_register_clock_reset_notifier qemu_unregister_clock_reset_notifier qemu_new_timer qemu_free_timer qemu_del_timer qemu_mod_timer_ns qemu_mod_timer qemu_run_timers qemu_new_timer_ns qemu_new_timer_us qemu_new_timer_ms);
  7. my $legacyre = '\b('.join('|', @legacy).')\b';
  8. my $option_git;
  9. my $option_dryrun;
  10. my $option_quiet;
  11. my $option_rtc;
  12. my $suffix=".tmp.$$";
  13. my @files;
  14. my $getfiles = 'git grep -l -E \'\b((host|rt|vm|rtc)_clock\b|qemu_\w*timer)\' | egrep \'\.[ch]$\' | egrep -v \'qemu-timer\.c$|include/qemu/timer\.h$\'';
  15. sub Syntax
  16. {
  17. print STDERR <<STOP;
  18. Usage: $FindBin::Script [options] FILE ...
  19. Translate each FILE to the new QEMU timer API. If no files
  20. are passed, a reasonable guess is taken.
  21. Options:
  22. -q, --quiet Do not show warnings etc
  23. -d, --dry-run Do a dry run
  24. -g, --git Generate a git commit for each change
  25. -r, --rtc Only fix up rtc usage
  26. -h, --help Print this message
  27. STOP
  28. return;
  29. }
  30. sub ParseOptions
  31. {
  32. if (!GetOptions (
  33. "dry-run|d" => \$option_dryrun,
  34. "git|g" => \$option_git,
  35. "quiet|q" => \$option_quiet,
  36. "rtc|r" => \$option_rtc,
  37. "help|h" => sub { Syntax(); exit(0); }
  38. ))
  39. {
  40. Syntax();
  41. die "Bad options";
  42. }
  43. if ($#ARGV >=0)
  44. {
  45. @files = @ARGV;
  46. }
  47. else
  48. {
  49. @files = split(/\s+/, `$getfiles`);
  50. }
  51. foreach my $file (@files)
  52. {
  53. die "Cannot find $file" unless (-f $file && -r $file);
  54. }
  55. }
  56. sub DoWarn
  57. {
  58. my $text = shift @_;
  59. my $line = shift @_;
  60. return if ($option_quiet);
  61. chomp ($line);
  62. print STDERR "$text\n";
  63. print STDERR "$line\n\n";
  64. }
  65. sub Process
  66. {
  67. my $ifn = shift @_;
  68. my $ofn = $ifn.$suffix;
  69. my $intext;
  70. my $outtext;
  71. my $linenum = 0;
  72. open my $input, "<", $ifn || die "Cannot open $ifn for read: $!";
  73. while (<$input>)
  74. {
  75. my $line = $_;
  76. $intext .= $line;
  77. $linenum++;
  78. # fix the specific uses
  79. unless ($option_rtc)
  80. {
  81. $line =~ s/\bqemu_new_timer(_[num]s)\s*\((vm_|rt_|host_)clock\b/timer_new$1(XXX_$2clock/g;
  82. $line =~ s/\bqemu_new_timer\s*\((vm_|rt_|host_)clock\b/timer_new(XXX_$1clock/g;
  83. $line =~ s/\bqemu_get_clock(_[num]s)\s*\((vm_|rt_|host_)clock\b/qemu_clock_get$1(XXX_$2clock/g;
  84. }
  85. # rtc is different
  86. $line =~ s/\bqemu_new_timer(_[num]s)\s*\(rtc_clock\b/timer_new$1(rtc_clock/g;
  87. $line =~ s/\bqemu_new_timer\s*\(rtc_clock\b/timer_new(rtc_clock/g;
  88. $line =~ s/\bqemu_get_clock(_[num]s)\s*\(rtc_clock\b/qemu_clock_get$1(rtc_clock/g;
  89. $line =~ s/\bqemu_register_clock_reset_notifier\s*\(rtc_clock\b/qemu_register_clock_reset_notifier(qemu_clock_ptr(rtc_clock)/g;
  90. unless ($option_rtc)
  91. {
  92. # fix up comments
  93. $line =~ s/\b(vm_|rt_|host_)clock\b/XXX_$1clock/g if ($line =~ m,^[/ ]+\*,);
  94. # spurious fprintf error reporting
  95. $line =~ s/: qemu_new_timer_ns failed/: timer_new_ns failed/g;
  96. # these have just changed name
  97. $line =~ s/\bqemu_mod_timer\b/timer_mod/g;
  98. $line =~ s/\bqemu_mod_timer_(ns|us|ms)\b/timer_mod_$1/g;
  99. $line =~ s/\bqemu_free_timer\b/timer_free/g;
  100. $line =~ s/\bqemu_del_timer\b/timer_del/g;
  101. }
  102. # fix up rtc_clock
  103. $line =~ s/QEMUClock \*rtc_clock;/QEMUClockType rtc_clock;/g;
  104. $line =~ s/\brtc_clock = (vm_|rt_|host_)clock\b/rtc_clock = XXX_$1clock/g;
  105. unless ($option_rtc)
  106. {
  107. # replace any more general uses
  108. $line =~ s/\b(vm_|rt_|host_)clock\b/qemu_clock_ptr(XXX_$1clock)/g;
  109. }
  110. # fix up the place holders
  111. $line =~ s/\bXXX_vm_clock\b/QEMU_CLOCK_VIRTUAL/g;
  112. $line =~ s/\bXXX_rt_clock\b/QEMU_CLOCK_REALTIME/g;
  113. $line =~ s/\bXXX_host_clock\b/QEMU_CLOCK_HOST/g;
  114. unless ($option_rtc)
  115. {
  116. DoWarn("$ifn:$linenum WARNING: timer $1 not fixed up", $line) if ($line =~ /\b((vm_|rt_|host_)clock)\b/);
  117. DoWarn("$ifn:$linenum WARNING: function $1 not fixed up", $line) if ($line =~ /\b(qemu_new_timer\w+)\b/);
  118. DoWarn("$ifn:$linenum WARNING: legacy function $1 remains", $line) if ($line =~ /$legacyre/o);
  119. }
  120. $outtext .= $line;
  121. }
  122. close $input;
  123. if ($intext ne $outtext)
  124. {
  125. print STDERR "Patching $ifn\n" unless ($option_quiet);
  126. unless ($option_dryrun)
  127. {
  128. open my $output, ">", $ofn || die "Cannot open $ofn for write: $!";
  129. print $output $outtext;
  130. close $output;
  131. rename ($ofn, $ifn) || die "Cannot rename temp file to $ifn: $!";
  132. return 1;
  133. }
  134. }
  135. return 0;
  136. }
  137. sub DoCommit
  138. {
  139. my $file = shift @_;
  140. open (my $git, "| git commit -F - $file") || die "Cannot run git commit on $file: $!";
  141. print $git "timers api: use new timer api in $file\n\nConvert $file to use new timer API.\nThis is an automated commit made by scripts/switch-timer-api\n";
  142. close ($git);
  143. }
  144. ParseOptions;
  145. foreach my $file (@files)
  146. {
  147. my $changed = Process ($file);
  148. DoCommit($file) if ($changed && $option_git);
  149. }