summaryrefslogtreecommitdiff
path: root/samwise/sam.pl
blob: 511d11398bacdf9e75219c05718019d186de8145 (plain)
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
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# $Id$

use FindBin;
use lib $FindBin::Bin;
use PerlSam::Parser;
use PerlSam::Generator;

use strict;

package main;

$main::verbose = 0;

################################################################################

my $parser_name;
my @target_names;
my @file_names;

################################################################################
# Parse Args

sub print_usage ()
{
    print "Compiles SAM files into Makefiles/Projects\n";
    print "\n";
    print "sam.pl [-parser <name>] [-target <name>] [-target <name>]... [files...]\n";
    print "\n";
    print " -parser <name>      Use <name> parser instead of the default\n";
    print " -target <name>      Add <name> as a target\n";
    print " files               Files that should be compiled (defaults to sam.xml)\n";
}

# Read in ARGV

while ($#ARGV >= 0)
{
    if ($ARGV[0] =~ m/^-parser/i) {
        if (defined $parser_name) {
            print STDERR "Error:  Only one parser allowed\n";
            exit 1;
        }
        $parser_name = $ARGV[1];
        shift;
    }
    elsif ($ARGV[0] =~ m/^-target/i) {
        push @target_names, $ARGV[1];
        shift;
    }
    elsif ($ARGV[0] =~ m/^-v/i) {
        shift;
        if (!defined $ARGV[0] || $ARGV[0] =~ m/^-/) {
            print STDERR "Error:  Expecting a positive verbosity level\n";
            exit 1;
        }
        $main::verbose = $ARGV[0];
    }
    elsif ($ARGV[0] =~ m/^-(\?|h)/i) {
        print_usage ();
        exit;
    }
    elsif ($ARGV[0] =~ m/^-/) {
        print "Error: Unknown option $ARGV[0]\n";
        exit 1;
    }
    else {
        push @file_names, $ARGV[0];
    }
    shift;
}

# Check for valid parameters

if (!defined $parser_name) {
    $parser_name = PerlSam::Parser::GetDefault ();
}

if ($#target_names < 0) {
    @target_names = PerlSam::Generator::GetDefaults ();
}

if ($#file_names < 0) {
    @file_names = ('sam.xml');
}

# Print out verbose info

if ($main::verbose >= 1) {
    print "Parser Name: ", $parser_name, "\n";
    print "Target Names: ", join (' ', @target_names), "\n";
    print "File Names: ", join (' ', @file_names), "\n";
}

################################################################################
# Parse and Generate

if ($main::verbose >= 1) {
    print "=== Parse and Generate\n";
}

foreach my $file (@file_names) {
    my $parser = new PerlSam::Parser ($parser_name);
    my $generator = new PerlSam::Generator (@target_names);

    print "Compiling $file\n";
    my %data;
    my %libdata;

    if ($parser->Parse ($file, \%data)) {
        if ($parser->ParseLibraryFile ("$FindBin::Bin/libs.xml", \%libdata)) {
            $generator->SetLibraryInfo (\%libdata);
            $generator->GenerateDependencies (\%data);
            $generator->GenerateWorkspace (\%data);
            $generator->GenerateProjects (\%data);
        }
        else {
            print STDERR "Error: Unable to parse library file\n";
        }
    }
    else {
        print STDERR "Error: Unable to parse file <$file>\n";
    }
}