Debian Codegraph @

debian/make-first-existing-target

raw
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
4 use Getopt::Long;
5 use IPC::Open3;
6 use Carp;
7
8 our $VERSION = 1.0;
9
10 sub usage {
11 print STDERR 'usage: make-first-existing-target [-c cmd] '
12 . "target1 [target2 ...] -- [make-options]\n"
13 or croak "Could not print: $!";
14 exit 1;
15 } ## end sub usage
16
17 my ( @targets, @makeopts );
18 my $makecmd = 'make';
19
20 getopt();
21
22 # Observe make's stderr on a non-existing target.
23 my $dummy_target = 'make-first-existing-target-dummy-nonexistant-target';
24 my $dummy_result = observe_dummy();
25
26 foreach my $target (@targets) {
27 make( $target, $dummy_result );
28 }
29 error("*** No rules to make targets: @targets");
30
31 sub make {
32
33 # Runs make on a target, passing stdout, and observing stderr
34 # to see if it is similar to that observed when running the dummy
35 # target.
36 # Only returns if the target appears not to exist.
37 my $target = shift;
38
39 # make's stderr will vary from dummy by target name
40 my @dummy = map { s/$dummy_target/$target/msxg; $_ } split /\n/msx, shift;
41
42 my $same = 1;
43 my @stderr_buf;
44 my $code = make_stderr(
45 $target,
46 sub {
47 chomp;
48 if ( @dummy && $_ eq $dummy[0] ) {
49 push @stderr_buf, "$_\n";
50 shift @dummy;
51 }
52 else {
53 print STDERR @stderr_buf or croak "Could not print: $!";
54 print STDERR "$_\n" or croak "Could not print: $!";
55 $same = 0;
56 @stderr_buf = @dummy = ();
57 } ## end else [ if ( @dummy && $_ eq $dummy...)]
58 }
59 );
60
61 if ( !$same || @dummy ) {
62 print @stderr_buf or croak "Could not print: $!";
63 exit exitcode();
64 }
65 } ## end sub make
66
67 sub observe_dummy {
68 my $stderr = q{};
69 my $code = make_stderr( $dummy_target, sub { $stderr .= shift }, 1 );
70
71 if ( $code != 2 || !length $stderr ) {
72
73 # Could loop and try another target, but in the unlikely
74 # case the dummpy target exists, we don't know what it did,
75 # so best to treat this as a failure.
76 error("unexpected result running $dummy_target: $stderr");
77 } ## end if ( $code != 2 || !length...)
78
79 return $stderr;
80 } ## end sub observe_dummy
81
82 sub make_stderr {
83
84 # Runs make on a target, passing each line of stderr to a callback
85 # function. Returns make's exit code.
86 my $target = shift;
87 my $callback = shift;
88 my $silent = shift;
89
90 # Normally open3 will close the stdin filehandle when done.
91 # But we want to call it repeatedly until one target successfully
92 # runs; and that target should be able to read from stdin.
93 # So, make a dup filehandle, in order to leave stdin open.
94 open( MAKEIN, "<&STDIN" ) || die "$!";
95
96 if ( !$silent ) {
97 open( MAKEOUT, ">&STDOUT" ) || die "$!";
98 }
99 else {
100 open( MAKEOUT, ">/dev/null" ) || die "$!";
101 }
102
103 my $pid = open3( '<&MAKEIN', '>&MAKEOUT', \*MAKEERR,
104 $makecmd, @makeopts, $target );
105 while (<MAKEERR>) {
106 $callback->($_);
107 }
108 waitpid( $pid, 0 );
109 close MAKEIN;
110 close MAKEOUT;
111 return exitcode();
112 } ## end sub make_stderr
113
114 sub exitcode {
115 my $code = $? >> 8;
116 if ( !$code && $? ) {
117 $code = $?;
118 }
119 return $code;
120 } ## end sub exitcode
121
122 sub error {
123 print STDERR "make-first-existing-target: @_\n";
124 exit 2;
125 }
126
127 sub getopt {
128 GetOptions(
129 "h|help" => \&usage,
130 "c=s" => \$makecmd,
131 ) || usage();
132
133 # remainder are targets, possibly followed by makeopts
134 my $end = 0;
135 foreach my $a (@ARGV) {
136 if ( $end || $a =~ /^-/ ) {
137 $end = 1;
138 push @makeopts, $a;
139 }
140 else {
141 push @targets, $a;
142 }
143 } ## end foreach my $a (@ARGV)
144
145 @targets || usage();
146 } ## end sub getopt
147
148 __END__
149
150 =head1 NAME
151
152 make-first-existing-target - runs make on one of several targets
153
154 =head1 SYNOPSIS
155
156 make-first-existing-target [-c cmd] target1 [target2 ...] -- [make-options]
157
158 =cut
159
160 =head1 DESCRIPTION
161
162 The design of L<make(1)> causes difficulty when you know that a Makefile
163 probably has one of several standardized target names, and want build
164 machinery to run exactly one of them, propagating any errors. L<make(1)>
165 will exit 2 if a target does not exist, but an existing target may also
166 exit 2 due to some other failure. Makefiles cannot be reliably parsed
167 to find targets by anything less turing complete than make; and make itself
168 does not provide a way to enumerate the targets in a Makefile. It may not
169 even be possible to enumerate the targets in a Makefile without executing
170 part of it. (Proof of this is left as an exercise for the reader.)
171
172 This program avoids the problems described above, by attempting to call
173 each specified target in turn, until it observes make actually doing
174 something for one of them.
175
176 =head1 OPTIONS
177
178 =over 4
179
180 =item -c cmd
181
182 This can be used to specify the make command to run. Default is "make".
183
184 =back
185
186 =cut
187
188 =head1 EXIT STATUS
189
190 The exit status is 0 if at least one target existed and was successfully
191 run, and nonzero otherwise.
192
193 =head1 AUTHOR
194
195 Joey Hess <[email protected]>
196
197 =head1 LICENSE
198
199 Same as GNU make.
200
201 =head1 SEE ALSO
202
203 L<make(1)>
204
205 =cut