File: toolhash

package info (click to toggle)
libverilog-perl 3.482-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,728 kB
  • sloc: perl: 8,685; yacc: 3,387; cpp: 2,266; lex: 1,502; makefile: 8; fortran: 3
file content (291 lines) | stat: -rwxr-xr-x 7,304 bytes parent folder | download
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/perl -w

use Digest::SHA;
use File::Copy qw(copy);  # Core module
use IO::File;
use strict;

our $Debug;

# We don't use getopt, as want multiple in/outs and stop at first command
my @opt_name;
my @opt_in;  # We allow empty opt_in and opt_out so we can cache --version checks.
my @opt_out;
my @opt_cmd;
my @opt_vercmd;
my $Opt_Gen = "gen";
my $Opt_Verbose;
my $opt_skip_cmd = 0;
my $in_cmd;
my $list = \@opt_in;
while (defined(my $param=shift @ARGV)) {
    if ($in_cmd) { push @opt_cmd, $param; }
    elsif ($param =~ /^-?-debug/) { $Debug=1; }
    elsif ($param =~ /^-?-cmd/) { $in_cmd = 1; }
    elsif ($param =~ /^-?-in/) { $list = \@opt_in; }
    elsif ($param =~ /^-?-name/) { $list = \@opt_name; }
    elsif ($param =~ /^-?-skip-cmd/) { $opt_skip_cmd = shift @ARGV; }
    elsif ($param =~ /^-?-out/) { $list = \@opt_out; }
    elsif ($param =~ /^-?-verbose/) { $Opt_Verbose=1; }
    elsif ($param =~ /^-?-gen/) { $Opt_Gen = shift @ARGV; }
    elsif ($param =~ /^-?-vercmd/) { $list = \@opt_vercmd; }
    elsif ($param =~ /^-/) { die "%Error: Unexpected argument: $param,"; }
    else {
	push @$list, $param;
    }
}
$opt_name[0] ||= $opt_cmd[0];
$opt_vercmd[0] ||= $opt_cmd[0];
$Opt_Verbose = 1 if $Debug;

mkdir $Opt_Gen, 0777;

# Hash of command, including this program args
my $digest = Digest::SHA->new(1);
{
    my $str = 'toolhash_1.0';
    $str .= '----'.join('  ',@opt_in);
    $str .= '----'.join('  ',@opt_out);
    $str .= '----';
    my $i = $opt_skip_cmd;
    foreach (@opt_cmd) { next if ($i-- > 0); $str.='  '.$_; }
    $str .= '----';
    print "toolhash: Hashing $str\n" if $Debug;
    $digest->add($str);
}
foreach my $fn (@opt_in) {
    print "toolhash: Hashing $fn\n" if $Debug;
    my $fh = IO::File->new("<$fn") or die "toolhash: %Error: $! reading $fn\n";
    $digest->addfile($fh);
    $fh->close;
}
my $arcfn = $Opt_Gen."/".$opt_name[0];
my $hash = $digest->b64digest;

# Cache hit?  If so, fill as we go
remove_out();
my $hit = restore($hash, 1);
if ($hit) {
    print "toolhash: Cache hit running $opt_name[0]\n" if $Opt_Verbose;
    exit(0);
} else {
    print "toolhash: Cache miss running $opt_name[0]\n" if $Opt_Verbose;
}
remove_named();
my $out = run_cmd();
encache($hash, $out);

remove_out();
$hit = restore($hash, 0);
exit(0) if $hit;
die "toolhash: %Error: encaching failed, didn't hit second time\n";

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

sub restore {
    my $hash = shift;
    my $pass1 = shift;
    if ($pass1 && $ENV{TOOLHASH_RECACHE}) {
	print "toolhash: TOOLHASH_RECACHE set, missing\n" if $Debug;
	return 0;
    }

    # Returns hit
    my $hit = 1;
    my $fh = IO::File->new("<${arcfn}-0");
    if (!$fh) {
	print "toolhash: Cache hash empty $arcfn\n" if $Debug;
	return 0;
    }
    my $line = $fh->getline;
    chomp $line;
    print "toolhash: Cache hash test $arcfn $line ".$hash."\n" if $Debug;
    if ($line ne $hash) {
	print "toolhash: Cache hash miss\n" if $Debug;
	return 0;
    }

    my $n = 1;
    foreach my $fn (@opt_out) {
	my $digout = "${arcfn}-${n}";
	if (-r $digout) {
	    print "toolhash: Cache hit  $digout for $fn\n" if $Debug;
	    # Restore, assuming all hits.
	    copy($digout, $fn) or die "toolhash: %Error: $! on 'cp $digout $fn'\n";
	} else {
	    print "toolhash: Cache miss $digout for $fn\n" if $Debug;
	    $hit = 0; last;
	}
	$n++;
    }

    if ($hit) {
	print "toolhash: Cache hit\n" if $Debug;
	if (my $fh = IO::File->new("<${arcfn}-s")) {  # Dump stdout
	    print join('',$fh->getlines);
	    $fh->close;
	}
    }

    return $hit;
}

sub run_cmd {
    remove_out();
    my $cmd = join(' ',@opt_cmd);
    # We can't use system() as we need the output and fork() isn't portable
    # without pulling in yet another package, so punt on spaces
    foreach (@opt_cmd) {
	if (/ /) { die "%Error: unsupported: spaces in command: '$cmd',"; }
    }
    print "\t$cmd\n" if $Debug||1;
    my $out = `$cmd`;
    my $status = $?;
    print $out;
    if ($status) {
	remove_out();
	# See if bison/gcc/flex --version works
	my $vcmd = "$opt_vercmd[0] --version";
	print "\t$vcmd\n" if $Debug;
	`$vcmd`;
	if ($?) {
	    die "\n%Error: '$opt_cmd[0]' must be installed to build\n";
	}
	exit $status >> 8;
    }
    return $out;
}

sub encache {
    my $hash = shift;
    my $out = shift;

    print "toolhash: Encache ".$hash."\n" if $Debug;

    my $fh = IO::File->new(">${arcfn}-0") or die "toolhash: %Error: $! ${arcfn}-0\n";
    $fh->print($hash);
    $fh->close;

    if ($out ne "") {
	$fh = IO::File->new(">${arcfn}-s") or die "toolhash: %Error: $! ${arcfn}-s\n";
	$fh->print($out);
	$fh->close;
    }

    my $n = 1;
    foreach my $fn (@opt_out) {
	my $digout = "${arcfn}-${n}";
	copy($fn, $digout) or die "toolhash: %Error: $! on 'cp $fn $digout'\n";
	$n++;
    }
}

sub remove_out {
    unlink for (@opt_out);  # Ok if error
}

sub remove_named {
    unlink for (glob $Opt_Gen."/$opt_name[0]-*"); # Ok if error
}

#######################################################################
__END__

=pod

=head1 NAME

toolhash - Generate and hash files to avoid installation of build tools

=head1 SYNOPSIS

  toolhash --in foo.c --out foo.o --cmd gcc -c -o foo.o foo.c

=head1 DESCRIPTION

Toolhash is used to install Verilog-Perl and other tools.  It stores a
hash of generated files (aka the cons make utility) for distribution to
avoid building those files from scratch.

The hash isn't stored as part of the filename, so that the MANIFEST can
remain constant.

=head1 ARGUMENTS

=over 4

=item --cmd command args...

Command and arguments to run.  All further arguments are passed to the command.

=item --gen ARG

Specify location of generated file cache, defaults to "gen".

=item --in filenames...

Input filenames.

=item --name

Prefix for output files, or defaults to first --cmd argument.

=item --verbose

Print hit/miss messages.

=item --skip-cmd <num-args>

Disable hashing first num-arg components of the command.  This is used to
avoid commands like "/usr/bin/perl ...." from hash missing when the Perl
version and thus the path changes.

=item --out filenames...

Output filenames.

=item --vercmd command

Command to run to get --version.

=back

=head1 ENVIRONMENT

=over 4

=item TOOLHASH_RECACHE

Write the cache, but do not read from it.

=back

=head1 DISTRIBUTION

This is part of the L<https://www.veripool.org/> free Verilog EDA software
tool suite.  The latest version is available from CPAN and from
L<https://www.veripool.org/>.

Copyright 2010-2024 by Wilson Snyder.  This package is free software; you
can redistribute it and/or modify it under the terms of either the GNU
Lesser General Public License Version 3 or the Perl Artistic License Version 2.0.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
more details.

=head1 AUTHORS

Wilson Snyder <wsnyder@wsnyder.org>

=head1 SEE ALSO

C<make>

=cut

######################################################################
### Local Variables:
### compile-command: "echo 'void i() {}' > foo.c; ./toolhash toolhash --debug --in foo.c --out foo.o --cmd gcc -c -o foo.o foo.c ; ls -la foo.* gen/* ; rm foo.*"
### End: