Makewrapper.pl

From CDOT Wiki
Jump to: navigation, search
#!/usr/bin/perl
#
# blah blah... license info blah blah seems most other source files have this header comment format
#
# The Original Code was written for:
#     Delta Debugging Framework.
#
# The Initial Developer of the Original Code:
#     Richard Chu
#
# Contributor(s):
#     Elizabeth Chak - Documentation
#
# Date Created: October 10, 2006
# Date Last Modified: October 22, 2006
#
# Version: 0.02
#
# Description:
# A light wrapper around the GNU make utility used to "make" file(s) - compiling source code
# files into object code files using a compiler; then linking the object code into executables
# or libraries. According to Wikipedia.
#
# The exit code of the make utility is one of:
#     0    make is successful
#     2    make encountered an error
#     1    you used the '-q' flag and make determined that some target is not already up to date.
#
# However, I have not figured out how to capture the exit code of make after the command is executed
# to determine whether it was successful or not.
#
# References: http://www.gnu.org/software/make/manual/html_node/Running.html#Running
#
# Change History:
#     October 10, 2006:
#         - make class created. Initial support for GNU make utility and its multitude of options.
#
#     October 22, 2006:
#         - removed the debug print statements.
#
###############################################################################

use warnings;
use strict;
package Build::GNUMake;

####
# Create a generic Build element. The values of the anonymous hash ($self) 
# is initially undefined and will be used to hold the properties for Build. 
# It may be populated with GNU make options via the addOption subroutine.
####
sub new () {
    my $class = shift; # first element in @_ is class name
    my $self = { };
    bless ($self, $class); #change data type of anonymous hash to $class
    return $self;
};

#####
# Build subroutine that builds the files depending on the options added via the 
# addOption subroutine. When I figure out, it will return GNU make's exit code.
# 
# @return rc         make's command code
#####
sub build () {
    my $self = shift;
    my $rc = -1;

    my $key;
    my $value;
    my $options;

    while (($key, $value)=each %$self) {
        $options .= "$self->{$key} ";
    }

    $rc = `make $options`;

    return $rc;
}

####
# addOption subroutine that allows users to add GNU make options which customizes the build.
# GNU make options are added to the hash which will then be concatenated to the GNU make command code. 
# Most GNU make options are covered in this subroutine.
# No error checking or validation is done for the input.
# Options are based on this reference: http://www.gnu.org/software/make/manual/html_node/Options-Summary.html#Options-Summary
####
sub addOption ($;$) {
    my $self = shift;
    my ($option, $value) = @_;

    if (!defined($value)) {
        $value = "";
    }

    if ($option eq "-B" || $option eq "--always-make") {
        $self->{"--always-make"} = "--always-make";
    }

    # NOT SURE about the correctness of this option
    elsif ($option eq "-C" || $option eq "--directory") {
        $self->{"--directory"} .= " --directory=$value";
    }

    elsif ($option eq "-d" || $option eq "--debug") {
        if ($option eq "-d") {
            $value = "a";
        }
        if ($value eq "a" || $value eq "b" || $value eq "v"
            || $value eq "i" || $value eq "j" || $value eq "m") {

            $self->{"--debug"} = "--debug=$value";

        }
    }
    elsif ($option eq "-e" || $option eq "--environment-overrides") {
        $self->{"--environment-overrides"} = "--environment-overrides";
    }

    elsif ($option eq "-f" || $option eq "--file" || $option eq "--makefile") {
        $self->{"--makefile"} = "--makefile=$value";
    }
    elsif ($option eq "-i" || $option eq "--ignore-errors") {
        $self->{"--ignore-errors"} = "--ignore-errors";
    }

    # NOT SURE about the correctness of this option
    elsif ($option eq "-I" || $option eq "--include-dir") {
        $self->{"--include-dir"} .= " --include-dir=$value";
    }

    elsif ($option eq "-j" || $option eq "--jobs") {
        $self->{"--jobs"} = "--jobs=$value";
    }

    elsif ($option eq "-k" || $option eq "--keep-going") {
        $self->{"--keep-going"} = "--keep-going";
    }

    #assuming these are aliases...
    elsif ($option eq "-l" || $option eq "--load-average" || $option eq "--max-load") {
        $self->{"-l"} = "-l $value";
    }
    elsif ($option eq "-n" || $option eq "--just-print" || $option eq "--dry-run" || $option eq "--recon") {
        $self->{"--just-print"} = "--just-print";
    }
    elsif ($option eq "-o" || $option eq "--old-file" || $option eq "--assume-old") {
        $self->{"--old-file"} = "--old-file=$value";
    }
    elsif ($option eq "-s" || $option eq "--silent" || $option eq "--quiet") {
        $self->{"--silent"} = "--silent";
    }
}

####
# This subroutine prints the options that have been added via the addOption subroutine
# This method is used for debugging purposes.
####
sub printOptions () {
    my $self = shift;
    my $key;
    my $value;

    while (($key, $value)=each %$self) {
        print " $self->{$key}";
    }
}

1; # last statement in file returns true