~ubuntu-branches/debian/sid/freehdl/sid

« back to all changes in this revision

Viewing changes to v2cc/gvhdl-local

  • Committer: Bazaar Package Importer
  • Author(s): José L. Redrejo Rodríguez
  • Date: 2007-03-17 11:53:16 UTC
  • Revision ID: james.westby@ubuntu.com-20070317115316-4dar2jcct6hz0f6f
Tags: upstream-0.0.4
Import upstream version 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/perl
 
2
use Socket;
 
3
use strict; 
 
4
 
 
5
my $cc = "g++";
 
6
my $libtool = "FREEHDL/libtool";
 
7
my $libtool_options = "--mode=link";
 
8
my $vhdl_source_name = "";
 
9
my $source = "";
 
10
my $includes = "";
 
11
my $cpplibs = "-lm FREEHDL/kernel/libfreehdl-kernel.la FREEHDL/std/libfreehdl-std.la";
 
12
my $vhdl_library = ""; # Library the design entity is compiled into
 
13
my $cpp_options = "-static ";
 
14
my $vhdl_options = "";
 
15
my $no_cpp_compile = 0;
 
16
my $verbose = 0;
 
17
my $use_sockets = 0;
 
18
my $vhdl_libdir = "FREEHDL";
 
19
my $freehdl_root_path="";
 
20
 
 
21
# set root path of the FreeHDL system
 
22
sub set_freehdl_root_path {
 
23
  $freehdl_root_path = $ENV{'FREEHDL'};
 
24
  if ($freehdl_root_path ne "") { return; }
 
25
  # if environment variable is not set then test
 
26
  # each parent directory
 
27
  my $path = ".";
 
28
  while (1) {
 
29
    # test whether $path is a valid path
 
30
    if (not -d "$path/") { die "Could not find FreeHDL root path! Please set environment FREEHDL."; }
 
31
    # is there a file named v2cc.libs in the directory?
 
32
    if (not -f "$path/v2cc.libs") { $path = $path . "/.."; next; }
 
33
    # open file and analyze content
 
34
    open INFILE, "$path/v2cc.libs" or die "Could not read $path/v2cc.libs\n";
 
35
    my $text = ""; 
 
36
    while (<INFILE>) { 
 
37
      s/\n//g;
 
38
      $text = $text . " " . $_; 
 
39
    }
 
40
    my $found = 1;
 
41
    if ($text !~ /std\s*:\s*std/i) { $found = 0; }
 
42
    if ($text !~ /ieee\s*:\s*ieee/i) { $found = 0; }
 
43
    close(INFILE);
 
44
    # return result if freehdl root path was found
 
45
    if ($found == 1) { 
 
46
      $freehdl_root_path = $path;
 
47
      return;
 
48
    }
 
49
    # try next path
 
50
    $path = $path . "/..";
 
51
  }
 
52
};
 
53
 
 
54
 
 
55
sub update {
 
56
  $_ = $_[0];
 
57
  s/FREEHDL/$freehdl_root_path/g;
 
58
  return $_;
 
59
}
 
60
 
 
61
 
 
62
sub execute_cmd {
 
63
  my $cmd = $_[0];
 
64
  my $label_text = $_[1];
 
65
  my $out_file = $_[2];
 
66
  open(COMMAND, "$cmd 2>&1 |");
 
67
  while (<COMMAND>) {
 
68
    print $out_file, "$label_text$_";
 
69
  }
 
70
  return close(COMMAND)? 0 : 256;
 
71
}
 
72
 
 
73
 
 
74
# The main program
 
75
sub main {
 
76
  # determine root path of freehdl installation
 
77
  set_freehdl_root_path;
 
78
  print "gvhdl: FreeHDL root path is '$freehdl_root_path'.\n";
 
79
  $includes = "-I $freehdl_root_path";
 
80
  my $MSTREAM;
 
81
  my $PROGRESS_BAR;
 
82
  open(MSTREAM, ">&STDOUT");
 
83
  open(PROGRESS_BAR, ">/dev/null");
 
84
  
 
85
  $cpplibs = update($cpplibs);
 
86
  $libtool = update($libtool);
 
87
  $vhdl_libdir = update($vhdl_libdir);
 
88
 
 
89
  my $arg_index = 0;
 
90
  my $do_link = 1;
 
91
  my $create_simulator = 1;
 
92
  my @source_files;
 
93
  my $object_files;
 
94
  while ($arg_index < scalar(@ARGV)) {
 
95
    my $argument = $ARGV[$arg_index];
 
96
    if ($argument =~ /^\-l/) {
 
97
      die "gvhdl: Missing argument for '-l'!\n" if (++$arg_index >= scalar(@ARGV));
 
98
      $vhdl_library = "-l " . $ARGV[$arg_index];
 
99
    } elsif ($argument =~ /^\-C/) {
 
100
      $cc = $ARGV[++$arg_index];
 
101
    } elsif ($argument =~ /^\-FHDLgui/) {
 
102
      # gvhdl will communiate with is caller via 2 sockets. The socket
 
103
      # file names are generated from the base string passed over as a
 
104
      # argument to option FHDLgui. The actual file names are derived
 
105
      # by appending channel number 0 or 4 to the base name. Channel 0
 
106
      # is used for printing messages generated during code
 
107
      # generation/compilation/linking while channel 4 is used to
 
108
      # print progress information.
 
109
      my $socket_base_name = $ARGV[++$arg_index];
 
110
 
 
111
      my $socket_name = $socket_base_name . "0";
 
112
      socket(MSTREAM, PF_UNIX, SOCK_STREAM, 0)  || die "socket: $!";
 
113
      connect(MSTREAM, sockaddr_un($socket_name)) || die "connect: $!";
 
114
 
 
115
      my $socket_name = $socket_base_name . "4";
 
116
      socket(PROGRESS_BAR, PF_UNIX, SOCK_STREAM, 0)  || die "socket: $!";
 
117
      connect(PROGRESS_BAR, sockaddr_un($socket_name)) || die "connect: $!";
 
118
 
 
119
      $use_sockets = 1;
 
120
 
 
121
    } elsif ($argument =~ /^\-L/) {
 
122
      $vhdl_libdir .= " -L " . $ARGV[++$arg_index];
 
123
    } elsif ($argument =~ /^\-R/) {
 
124
      $vhdl_options = $vhdl_options . " -R";
 
125
    } elsif ($argument =~ /^\-g/) {
 
126
      $vhdl_options = $vhdl_options . " -g";
 
127
      $cpp_options = $cpp_options . " -g";
 
128
    } elsif ($argument =~ /^\-c/) {
 
129
      $do_link = 0;
 
130
      $create_simulator = 0;
 
131
      $cpp_options = $cpp_options . " -c";
 
132
    } elsif ($argument =~ /^\-G/) {
 
133
      $cpp_options = $cpp_options . " -g";
 
134
    } elsif ($argument =~ /^\-v/) {
 
135
      $verbose = 1;
 
136
    } elsif ($argument =~ /^\-V/) {
 
137
      $vhdl_options = $vhdl_options . " -v";
 
138
    } elsif ($argument =~ /^\-D/) {
 
139
      $vhdl_options = $vhdl_options . " -D";
 
140
    } elsif ($argument =~ /^\--relaxed-component-visibility/) {
 
141
      $vhdl_options = $vhdl_options . " --relaxed-component-visibility";
 
142
    } elsif ($argument =~ /^\--libieee/) {
 
143
        $cpplibs .= " " . update ("FREEHDL/ieee/libieee.la");
 
144
    } elsif ($argument =~ /^\-/) {
 
145
      $cpp_options = $cpp_options . " " . $argument;
 
146
    } else {
 
147
      push @source_files, $argument;
 
148
    }
 
149
    ++$arg_index;
 
150
  }
 
151
 
 
152
 
 
153
  my $steps = scalar(@source_files);
 
154
  if ($no_cpp_compile == 0) { $steps *= 2; }
 
155
  if ($do_link == 1) { $steps += 1; }
 
156
  $steps = int(99 / $steps);
 
157
  my $progress_value = 0;
 
158
 
 
159
  ##############################################################
 
160
  # Create name for main file
 
161
  ##############################################################
 
162
  my $main_file_name;
 
163
  my $main_vhdl_file;
 
164
  if ($create_simulator == 1) {
 
165
    $_ = $source_files [0];
 
166
    if ($_ =~ /\.vhdl$/) { $main_vhdl_file = $_; }
 
167
    s/\.[^\.]*$/\._main_/i;
 
168
    $main_file_name = $_;
 
169
  }
 
170
 
 
171
  ##############################################################
 
172
  # process each source file 
 
173
  ##############################################################
 
174
  foreach $vhdl_source_name (@source_files) {
 
175
    # skip object (*.o) files
 
176
    if ($vhdl_source_name =~ /\.o$/) { 
 
177
      $object_files = $object_files . " " . $vhdl_source_name;
 
178
      next;
 
179
    }
 
180
    # skip object (*.a) files
 
181
    if ($vhdl_source_name =~ /\.a$/) { 
 
182
      $object_files = $object_files . " " . $vhdl_source_name;
 
183
      next;
 
184
    }
 
185
    ##############################################################
 
186
    # Comiling vhdl -> cc
 
187
    ##############################################################
 
188
    $_ = $vhdl_source_name;
 
189
    s/\.[^\.]*$/\.cc/i;
 
190
    my $out_file_name = $_;
 
191
  
 
192
    my $cmd;
 
193
    if ($create_simulator == 1 && 
 
194
        $main_vhdl_file == $vhdl_source_name) {
 
195
      $cmd = "$freehdl_root_path/v2cc/freehdl-v2cc -m $main_file_name.cc -L $vhdl_libdir $vhdl_library $vhdl_options -o $out_file_name $vhdl_source_name";
 
196
    } else {
 
197
      $cmd = "$freehdl_root_path/v2cc/freehdl-v2cc -L $vhdl_libdir $vhdl_library $vhdl_options -o $out_file_name $vhdl_source_name";
 
198
    }
 
199
    print MSTREAM "gvhdl: executing '$cmd'\n";
 
200
    if (execute_cmd ($cmd, "", $MSTREAM)/256 != 0) {
 
201
      print MSTREAM "gvhdl: Compilation failed!\n";
 
202
      print PROGRESS_BAR "99";
 
203
      die;
 
204
    }
 
205
 
 
206
    $progress_value = int($progress_value + $steps);
 
207
    print PROGRESS_BAR $progress_value;
 
208
 
 
209
    ##############################################################
 
210
    # Comiling cc -> o
 
211
    ##############################################################
 
212
    $_ = $out_file_name;
 
213
    s/\.[^\.]*/\.o/i;
 
214
    $object_files .= " " . $_;
 
215
    
 
216
    if ($no_cpp_compile == 0) {
 
217
      my $cmd = "$cc $cpp_options $includes -c $out_file_name";
 
218
      print MSTREAM "gvhdl:\n";
 
219
      print MSTREAM "gvhdl: ================================\n";
 
220
      print MSTREAM "gvhdl: Compiling '$out_file_name'...\n";
 
221
      print MSTREAM "gvhdl: ================================\n";
 
222
      print MSTREAM "gvhdl: $cmd\n";
 
223
      if (execute_cmd ($cmd, "c++: ", $MSTREAM)/256 != 0) {
 
224
        print MSTREAM "gvhdl: Compilation failed!\n";
 
225
        print PROGRESS_BAR "99";
 
226
        die;
 
227
      }
 
228
 
 
229
      $progress_value = int($progress_value + $steps);
 
230
      print PROGRESS_BAR $progress_value;
 
231
    }
 
232
  }
 
233
 
 
234
  ##############################################################
 
235
  # Comiling main cc file -> o
 
236
  ##############################################################
 
237
  if ($create_simulator == 1) {
 
238
    my $out_file_name = $main_file_name . ".cc";
 
239
    my $cmd = "$cc $cpp_options $includes -c $out_file_name";
 
240
    print MSTREAM "gvhdl:\n";
 
241
    print MSTREAM "gvhdl: ================================\n";
 
242
    print MSTREAM "gvhdl: Compiling simulator main file '$out_file_name'...\n";
 
243
    print MSTREAM "gvhdl: ================================\n";
 
244
    print MSTREAM "gvhdl: $cmd\n";
 
245
    if (execute_cmd ($cmd, "c++: ", $MSTREAM)/256 != 0) {
 
246
      print MSTREAM "gvhdl: Compilation failed!\n";
 
247
      print PROGRESS_BAR "99";
 
248
      die;
 
249
    }
 
250
    
 
251
    $progress_value = int($progress_value + $steps);
 
252
    print PROGRESS_BAR $progress_value;
 
253
  }
 
254
 
 
255
  ##############################################################
 
256
  # link
 
257
  ##############################################################
 
258
  if ($do_link == 1) {
 
259
    $_ = $source_files[0];
 
260
    s/\.[^\.]*//i;
 
261
    my $target_name = $_;
 
262
 
 
263
    my $cmd = "$libtool $libtool_options $cc $cpp_options $main_file_name.o $object_files $cpplibs -o $target_name";
 
264
    print MSTREAM "gvhdl: Linking simulator '$target_name'...\n";
 
265
    print MSTREAM "gvhdl: $cmd\n";
 
266
    if (execute_cmd ($cmd, "linker: ", $MSTREAM)/256 != 0) {
 
267
      print $MSTREAM "gvhdl: Linking failed!\n"; 
 
268
      die;
 
269
    }
 
270
    print MSTREAM "gvhdl: ================================\n";
 
271
    print MSTREAM "gvhdl: Simulator '$target_name' created.\n";
 
272
    print MSTREAM "gvhdl: ================================\n";
 
273
 
 
274
    print PROGRESS_BAR "99";
 
275
  }
 
276
}
 
277
 
 
278
 
 
279
&main;