fix-service-macros.pl
Go to the documentation of this file.
1 #!/usr/bin/perl -w
2 
3 use strict;
4 use File::Basename;
5 
6 my $wanted_header="art/Framework/Services/Registry/ServiceMacros.h";
7 my @services_cc = `find . -name '*_service.*' -print`;
8 chomp @services_cc;
9 
10 my $tmp_file = `mktemp "\${TMPDIR:-/tmp}/fix-service-macros.XXXXXXXXXX"`;
11 chomp $tmp_file;
12 die "Unable to find temporary file name" if (!$tmp_file);
13 
14 foreach my $service_cc (@services_cc) {
15  next unless $service_cc =~ m&\.(?:C|cc|cpp|cxx)$&;
16  print "Fixing service definition in ${service_cc} ... ";
17  fix_macros($service_cc);
18 }
19 
20 1;
21 
22 sub fix_macros {
23  my $service_cc = shift;
24  my $header = find_header($service_cc);
25  my $is_system_service = 0;
26  my $declare_macro;
27  open(SERVICE_CC, "<${service_cc}") or die "Unable to open ${service_cc} for read";
28  open(TMPOUT, ">${tmp_file}") or die "Unable to open temporary file ${tmp_file} for write.";
29  while (<SERVICE_CC>) {
30  if (m&^\s*DECLARE_ART_(?:SERVICE|SYSTEM)&) {
31  print "(already done).\n";
32  # Already done.
33  close(TMPOUT);
34  return;
35  }
36  my ($service_spec, $args) = m&^\s*DEFINE_ART_((?:[^\s\(])+)(?:\s*\((.*)\))?&;
37  if (not $service_spec) {
38  ($header and m&^\s*#\s*include\s+["<]\Q$wanted_header\E[">]&o) or
39  print TMPOUT;
40  next;
41  }
42  if (not $args) {
43  print "FAIL.\n";
44  print STDERR "ERROR: unable to ascertain correct declare macro from ${service_cc}:\n",
45  " broken line in DEFINE_ART_${service_spec}? Remove line break and re-try.\n";
46  close(TMPOUT);
47  return;
48  }
49  chomp;
50  $is_system_service = ($service_spec eq "SYSTEM_SERVICE");
51  $declare_macro = "DECLARE_ART_${service_spec}($args, LEGACY)";
52  if (!$header) { # Put declare in .cc file for lack of anywhere else.
53  print "(no header) ... ";
54  print TMPOUT <<EOF;
55 // The DECLARE macro call should be moved to the header file, should you
56 // create one.
57 ${declare_macro}
58 EOF
59  }
60  next if ($is_system_service); # No creator definition.
61  print TMPOUT "$_\n";
62  }
63  close(SERVICE_CC);
64  close(TMPOUT);
65  system("mv \"${tmp_file}\" \"${service_cc}\"") == 0 or
66  die "Unable to move edited ${service_cc} into place from ${tmp_file}";
67  print "DONE.\n";
68  insert_declare_in_header($header, $declare_macro) if ${header};
69 }
70 
71 sub find_header {
72  my $service_cc = shift;
73  my ($header_stem) = (${service_cc} =~ m&^(.*)_service\.(?:C|cc|cpp|cxx)$&);
74  my $dir = dirname($header_stem);
75  $header_stem = basename($header_stem);
76  foreach my $ext qw(h H hh hpp hxx) {
77  foreach my $test_h ("${dir}/${header_stem}.${ext}",
78  sprintf("%s/inc/${header_stem}.${ext}", dirname($dir)),
79  sprintf("%s/include/${header_stem}.${ext}", dirname($dir))) {
80  if (-f "${test_h}") {
81  return ${test_h};
82  }
83  }
84  }
85  return;
86 }
87 
88 sub ensure_servicemacros_include(\@) {
89  my $header_content = shift;
90  my $counter = 0;
91  my $last_seen;
92  foreach my $line (reverse @{$header_content}) {
93  --$counter;
94  if ($line =~ m&^\s*#\s*include\s+["<]([^">]+)&o) {
95  my $found_header = ${1};
96  return if ${found_header} eq ${wanted_header};
97  next unless ${found_header} =~ m&/&o;
98  $last_seen = ${counter};
99  if (${found_header} =~ m&^art/&o and
100  ${found_header} lt ${wanted_header}) {
101  ++$last_seen;
102  last;
103  }
104  } else {
105  next;
106  }
107  }
108  splice @{$header_content}, $last_seen, 0, "#include \"$wanted_header\"\n";
109 }
110 
111 sub insert_declare_in_header {
112  my ($header, $declare_macro) = @_;
113  print "Fixing service header ${header} ... ";
114  open(HEADER, "<${header}") or die "Unable to open ${header} for read";
115  open(TMPOUT, ">${tmp_file}") or die "Unable to open temporary file ${tmp_file} for write.";
116  my @header_content = <HEADER>; # Slurp.
117  close(HEADER);
118  ensure_servicemacros_include(@header_content);
119  if (grep m&^\s*DECLARE_ART_(?:SERVICE|SYSTEM)&, @header_content) {
120  print "(already done).\n";
121  return; # Already done.
122  }
123  my $counter = 0;
124  foreach my $line (reverse @header_content) {
125  --$counter; # Yes.
126  last if $line =~ m&^\s*#\s*endif&;
127  }
128  splice @header_content, $counter, 0, ("${declare_macro}\n");
129  print TMPOUT @header_content;
130  close(TMPOUT);
131  system("mv \"${tmp_file}\" \"${header}\"") == 0 or
132  die "Unable to move edited ${header} into place from ${tmp_file}";
133  print "DONE.\n";
134 }
135 
136 __END__
137 
138 ### Local Variables:
139 ### mode: cperl
140 ### End: