Tuesday, April 1, 2008

Make me a Sandwich

I've decided to turn a popular XKCD into an actual Makefile:

Makefile

sandwich:
if [ -f sandwich-tmp ]; then \
if [ "`awk '{ out = out $$_ }; END { print out }' sandwich-tmp`" == "mea" ]; then \
if [ `id -u` -eq 0 ]; then \
echo "Okay."; \
else \
echo "What? Make it yourself."; \
fi; \
fi; \
fi
me:
echo "me" > sandwich-tmp
a:
echo "a" >> sandwich-tmp
% make me a sandwich
echo "me" > sandwich-tmp
echo "a" >> sandwich-tmp
if [ -f sandwich-tmp ]; then \
if [ "`awk '{ out = out $_ }; END { print out }' sandwich-tmp`" == "mea" ]; then \
if [ `id -u` -eq 0 ]; then \
echo "Okay."; \
else \
echo "What? Make it yourself."; \
fi; \
fi; \
fi
What? Make it yourself.
% sudo make me a sandwich
echo "me" > sandwich-tmp
echo "a" >> sandwich-tmp
if [ -f sandwich-tmp ]; then \
if [ "`awk '{ out = out $_ }; END { print out }' sandwich-tmp`" == "mea" ]; then \
if [ `id -u` -eq 0 ]; then \
echo "Okay."; \
else \
echo "What? Make it yourself."; \
fi; \
fi; \
fi
Okay.

This script was partially autogenerated by this other Perl script:

smake.pl

#!/usr/bin/perl -w
use strict;
use warnings;

scalar @ARGV >= 2 || die "Usage: $0 target1 target2 [ ... ]\n";

my @targets = @ARGV;
my $final = pop @targets;
my $comp = '';
($comp .= $_) foreach(@targets);
my @lines = ("$final:", "\tif [ -f $final-tmp ]; then", "\t\tif [ \"\`awk '{ out = out \$_ }; END { print out }' $final-tmp\`\" == \"$comp\" ]; then", "\t\t\techo \"Okay.\"", "\t\tfi", "\tfi");
my $first = shift @targets;
push(@lines, ("$first:", "\techo \"$first\" > $final-tmp"));
push(@lines, ("$_:", "\techo \"$_\" >> $final")) foreach(@targets);
print "$_\n" foreach(@lines);

No comments: