This file is indexed.

/usr/bin/make-first-existing-target is in make 4.1-6.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/perl
use warnings;
use strict;
use Getopt::Long;
use IPC::Open3;
use Carp;

our $VERSION = 1.0;

sub usage {
  print STDERR 'usage: make-first-existing-target [-c cmd] '
    . "target1 [target2 ...] -- [make-options]\n"
    or croak "Could not print: $!";
  exit 1;
} ## end sub usage

my ( @targets, @makeopts );
my $makecmd = 'make';

getopt();

# Observe make's stderr on a non-existing target.
my $dummy_target = 'make-first-existing-target-dummy-nonexistant-target';
my $dummy_result = observe_dummy();

foreach my $target (@targets) {
  make( $target, $dummy_result );
}
error("*** No rules to make targets: @targets");

sub make {

  # Runs make on a target, passing stdout, and observing stderr
  # to see if it is similar to that observed when running the dummy
  # target.
  # Only returns if the target appears not to exist.
  my $target = shift;

  # make's stderr will vary from dummy by target name
  my @dummy = map { s/$dummy_target/$target/msxg; $_ } split /\n/msx, shift;

  my $same = 1;
  my @stderr_buf;
  my $code = make_stderr(
    $target,
    sub {
      chomp;
      if ( @dummy && $_ eq $dummy[0] ) {
        push @stderr_buf, "$_\n";
        shift @dummy;
      }
      else {
        print STDERR @stderr_buf or croak "Could not print: $!";
        print STDERR "$_\n" or croak "Could not print: $!";
        $same = 0;
        @stderr_buf = @dummy = ();
      } ## end else [ if ( @dummy && $_ eq $dummy...)]
    }
  );

  if ( !$same || @dummy ) {
    print @stderr_buf or croak "Could not print: $!";
    exit exitcode();
  }
} ## end sub make

sub observe_dummy {
  my $stderr = q{};
  my $code = make_stderr( $dummy_target, sub { $stderr .= shift }, 1 );

  if ( $code != 2 || !length $stderr ) {

    # Could loop and try another target, but in the unlikely
    # case the dummpy target exists, we don't know what it did,
    # so best to treat this as a failure.
    error("unexpected result running $dummy_target: $stderr");
  } ## end if ( $code != 2 || !length...)

  return $stderr;
} ## end sub observe_dummy

sub make_stderr {

  # Runs make on a target, passing each line of stderr to a callback
  # function. Returns make's exit code.
  my $target   = shift;
  my $callback = shift;
  my $silent   = shift;

  # Normally open3 will close the stdin filehandle when done.
  # But we want to call it repeatedly until one target successfully
  # runs; and that target should be able to read from stdin.
  # So, make a dup filehandle, in order to leave stdin open.
  open( MAKEIN, "<&STDIN" ) || die "$!";

  if ( !$silent ) {
    open( MAKEOUT, ">&STDOUT" ) || die "$!";
  }
  else {
    open( MAKEOUT, ">/dev/null" ) || die "$!";
  }

  my $pid = open3( '<&MAKEIN', '>&MAKEOUT', \*MAKEERR,
    $makecmd, @makeopts, $target );
  while (<MAKEERR>) {
    $callback->($_);
  }
  waitpid( $pid, 0 );
  close MAKEIN;
  close MAKEOUT;
  return exitcode();
} ## end sub make_stderr

sub exitcode {
  my $code = $? >> 8;
  if ( !$code && $? ) {
    $code = $?;
  }
  return $code;
} ## end sub exitcode

sub error {
  print STDERR "make-first-existing-target: @_\n";
  exit 2;
}

sub getopt {
  GetOptions(
    "h|help" => \&usage,
    "c=s"    => \$makecmd,
  ) || usage();

  # remainder are targets, possibly followed by makeopts
  my $end = 0;
  foreach my $a (@ARGV) {
    if ( $end || $a =~ /^-/ ) {
      $end = 1;
      push @makeopts, $a;
    }
    else {
      push @targets, $a;
    }
  } ## end foreach my $a (@ARGV)

  @targets || usage();
} ## end sub getopt

__END__

=head1 NAME

make-first-existing-target - runs make on one of several targets

=head1 SYNOPSIS

make-first-existing-target [-c cmd] target1 [target2 ...] -- [make-options]

=cut

=head1 DESCRIPTION

The design of L<make(1)> causes difficulty when you know that a Makefile
probably has one of several standardized target names, and want build
machinery to run exactly one of them, propagating any errors. L<make(1)>
will exit 2 if a target does not exist, but an existing target may also
exit 2 due to some other failure. Makefiles cannot be reliably parsed
to find targets by anything less turing complete than make; and make itself
does not provide a way to enumerate the targets in a Makefile. It may not
even be possible to enumerate the targets in a Makefile without executing
part of it. (Proof of this is left as an exercise for the reader.)

This program avoids the problems described above, by attempting to call
each specified target in turn, until it observes make actually doing
something for one of them.

=head1 OPTIONS

=over 4

=item -c cmd

This can be used to specify the make command to run. Default is "make".

=back

=cut

=head1 EXIT STATUS

The exit status is 0 if at least one target existed and was successfully
run, and nonzero otherwise.

=head1 AUTHOR

Joey Hess <joey@kitenet.net>

=head1 LICENSE

Same as GNU make.

=head1 SEE ALSO

L<make(1)>

=cut