6 my $wanted_header="art/Framework/Services/Registry/ServiceMacros.h";
7 my @services_cc = `find . -name '*_service.*' -print`;
10 my $tmp_file = `mktemp "\${TMPDIR:-/tmp}/fix-service-macros.XXXXXXXXXX"`;
12 die "Unable to find temporary file name" if (!$tmp_file);
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);
23 my $service_cc = shift;
24 my $header = find_header($service_cc);
25 my $is_system_service = 0;
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";
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
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";
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) ... ";
55 // The DECLARE macro call should be moved to the header file, should you
60 next if ($is_system_service); # No creator definition.
65 system("mv \"${tmp_file}\" \"${service_cc}\"") == 0 or
66 die "Unable to move edited ${service_cc} into place from ${tmp_file}";
68 insert_declare_in_header($header, $declare_macro) if ${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))) {
88 sub ensure_servicemacros_include(\@) {
89 my $header_content = shift;
92 foreach my $line (reverse @{$header_content}) {
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}) {
108 splice @{$header_content}, $last_seen, 0, "#include \"$wanted_header\"\n";
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.
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.
124 foreach my $line (reverse @header_content) {
126 last if $line =~ m&^\s*#\s*endif&;
128 splice @header_content, $counter, 0, ("${declare_macro}\n");
129 print TMPOUT @header_content;
131 system("mv \"${tmp_file}\" \"${header}\"") == 0 or
132 die "Unable to move edited ${header} into place from ${tmp_file}";