#! /usr/bin/perl
##
## Copyright (c) 2000 University of Utah and the Flux Group.
## All rights reserved.
## 
## This file is part of the Knit component composition software.
## 
## Permission to use, copy, modify, distribute, and sell this software and
## its documentation is hereby granted without fee, provided that the above
## copyright notice and this permission/disclaimer notice is retained in all
## copies or modified versions, and that both notices appear in supporting
## documentation.  THE COPYRIGHT HOLDERS PROVIDE THIS SOFTWARE "AS IS" AND
## WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION,
## THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
## PURPOSE.  THE COPYRIGHT HOLDERS DISCLAIM ANY LIABILITY OF ANY KIND FOR
## ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
## 
## Users are requested, but not required, to send to csl-dist@cs.utah.edu any
## improvements that they make and grant Univ. of Utah redistribution rights.
##

# icbinf stands for I can't believe it's not Flest.
#
# Like its namesake, icbinf bears no resemblance to either butter or
# to Flest (the Flux test harness).  Rather, it provides an easy (and
# easily customizable) way of building a large number of different
# units with one command.
#
# Usage: 
#
#   icbinf <knit arguments excluding the unit name> -- <list of units>
#
# This causes icbinf to:
# 1) Generate directories for each of the units.
#    The directory has the same name as the unit.
# 2) Create a script in each of these directories.
#    The script invokes knit and then invokes make.
#    make is invoked with flags suitable for a dual processor
#    machine with lots of memory.
# 3) execute the script and record its exit status.
#
# If the knit arguments include -X (which causes Knit to not generate
# a makefile), the script echoes the make command instead of executing it.
#
# A typical invocation is:
# 
#   SRC=/z/reid/oskit
#   OBJ=/z/reid/knit_oskit
#   icbinf OSKITDIR=$SRC BUILDDIR=$OBJ UNIT_PATH=$SRC/knit MAKEFILE=$SRC/knit/knit.mk Delta.unit -- Hello_Delta Timer_Delta Timer1_COM_Delta Timer2_COM_Delta MemFS_COM_Delta Blkio_Delta DiskPart_Delta MemFS_Posix_Delta NetBSD_Posix_Delta PingReply_Delta Cat_Delta
#
# Which will build 11 different OSKit kernels.

$make = "make";
while ($f = shift @ARGV) {
    last if $f eq "--";

    if ($f eq "-X") {
        $make = "echo make";
    }
    push(@KNIT_ARGS, $f);
}

die "usage: $0 [-X] <knit_args> -- unit names" if $f ne "--";

foreach $f (@ARGV) {
    $status{$f} = &do_test($f);
}

foreach $f (@ARGV) {
    if ($status{$f}) {
        print "Unit $f failed\n";
    } else {
        print "Unit $f succeeded\n";
    }
}

exit(0);

sub do_test{
    local ($unit_name) = @_;

    print "Testing $unit_name\n";

    mkdir($unit_name,0775);
    print "Generating $unit_name/script\n";
    open(SCRIPT, "> $unit_name/script");
    print SCRIPT << "EOF";
knit @KNIT_ARGS $unit_name || exit 1;
$make -r -j8 -s CFLAGS="-O2 -g -Wall" || exit 1;
EOF
    close(SCRIPT);
    print "Running $unit_name/script\n";
    return system("cd $unit_name && sh script");
}
