Wednesday, August 11, 2010

More on 'mk' (plan9)

The following is a valid mk-file (with basically the same syntax as a makefile):

% cat mkfile
MKSHELL=bash
dinosaur:
        for i in a b c; do
            echo $i;
            echo next...;
        done
MKSHELL=rc
target:
        for(i in a b c) {
            echo $i
            echo next...
        }
MKSHELL=./wrap-python
foo:
        import sys
        for i in range(3):
          sys.stdout.write('Whoa!\n')
MKSHELL=./wrap-perl
bar:
        use Cwd;
        for my $i (1..3) {
            print getcwd() . "\n";
        }

% cat wrap-perl

#!/bin/sh
echo $*
exec perl

(The wrappers are needed only because mk passes '-e' to the MKSHELL program.)

Not only does it make sh easier to use than with 'make', but it also allows the recipes to be written in any language.  (If you use SHELL in 'make', each line is a separate invocation.)  If you want, you can use multiple languages within the same mk-file.  You have to admit that's pretty cool.

As for rc, because of complications with readline and command-history, I can't yet advocate it for an interactive shell.  Oh, well.

2 comments:

  1. Fortuitously, .ONESHELL lets GNU make do this, as of 3.82 (released last month). It does not allow the SHELL to be changed between rules, however.

    ReplyDelete
  2. Also see the new .SHELLFLAGS and .RECIPEPREFIX, plus 'private' for variables.

    ReplyDelete