2019-06-04 16:21:15 -05:00
|
|
|
#!usr/bin/perl -w
|
|
|
|
use strict;
|
|
|
|
use Cwd;
|
|
|
|
#use Shell;
|
|
|
|
use FileHandle;
|
|
|
|
#Use the time
|
|
|
|
use Time::gmtime;
|
|
|
|
|
2019-06-14 12:44:44 -05:00
|
|
|
my $arch_file;
|
|
|
|
my $new_arch_file;
|
|
|
|
my $overwrite = "TRUE";
|
2019-06-04 16:21:15 -05:00
|
|
|
my $keyword = "OPENFPGAPATHKEYWORD";
|
2019-06-14 12:44:44 -05:00
|
|
|
my $default_keyword = "TRUE";
|
|
|
|
my $change_to;
|
2019-06-04 16:21:15 -05:00
|
|
|
my $folder_top = "OpenFPGA";
|
|
|
|
|
|
|
|
sub print_usage()
|
|
|
|
{
|
|
|
|
print "Usage:\n";
|
|
|
|
print " perl <script_name.pl> [-options]\n";
|
|
|
|
print " Options:(Mandatory!)\n";
|
2019-06-14 12:44:44 -05:00
|
|
|
print " -i <input_architecture_file_path>\n";
|
|
|
|
print " Options:(Optional)\n";
|
|
|
|
print " -o <output_architecture_file_path>\n";
|
|
|
|
print " -k <keyword> <new_value>\n";
|
2019-06-04 16:21:15 -05:00
|
|
|
print "\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub opts_read()
|
|
|
|
{
|
|
|
|
if ($#ARGV == -1){
|
|
|
|
print "Error: Not enough input argument!\n";
|
|
|
|
&print_usage();
|
2019-07-03 13:49:25 -05:00
|
|
|
exit(1);
|
2019-06-04 16:21:15 -05:00
|
|
|
} else {
|
|
|
|
for (my $iargv = 0; $iargv < $#ARGV+1; $iargv++){
|
2019-07-03 13:49:25 -05:00
|
|
|
if ("-i" eq $ARGV[$iargv]){
|
2019-06-14 12:44:44 -05:00
|
|
|
$arch_file = $ARGV[$iargv+1];
|
|
|
|
$iargv++;
|
2019-07-03 13:49:25 -05:00
|
|
|
} elsif ("-o" eq $ARGV[$iargv]){
|
2019-06-14 12:44:44 -05:00
|
|
|
$new_arch_file = $ARGV[$iargv+1];
|
|
|
|
$overwrite = "FALSE";
|
|
|
|
$iargv++;
|
2019-07-03 13:49:25 -05:00
|
|
|
} elsif ("-k" eq $ARGV[$iargv]){
|
2019-06-14 12:44:44 -05:00
|
|
|
$keyword = $ARGV[$iargv+1];
|
|
|
|
$change_to = $ARGV[$iargv+2];
|
|
|
|
$default_keyword = "FALSE";
|
|
|
|
$iargv++;
|
2019-06-04 16:21:15 -05:00
|
|
|
$iargv++;
|
|
|
|
} else {
|
|
|
|
die "WRONG ARGUMENT";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub rewriting_required_check($)
|
|
|
|
{
|
2019-06-14 12:44:44 -05:00
|
|
|
my ($arch) = @_;
|
|
|
|
open(F, $arch);
|
2019-06-04 16:21:15 -05:00
|
|
|
my @lines=<F>;
|
|
|
|
close F;
|
|
|
|
my $grep_result = grep ($keyword, @lines);
|
|
|
|
if($grep_result >= 1){
|
|
|
|
print "Rewrite needed\n";
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
print "Rewrite NOT needed\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub save_original($)
|
|
|
|
{
|
|
|
|
my ($template) = @_;
|
|
|
|
my $renamed_template = "$template".".bak";
|
|
|
|
rename($template, $renamed_template);
|
2019-07-03 13:49:25 -05:00
|
|
|
|
|
|
|
return $renamed_template;
|
2019-06-04 16:21:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
sub findPath(){
|
|
|
|
my $path;
|
|
|
|
my $dir = cwd;
|
|
|
|
my @folders = split("/", $dir);
|
2019-06-14 12:44:44 -05:00
|
|
|
for(my $count = 0; $count < ($#folders -1); $count++){
|
2019-06-04 16:21:15 -05:00
|
|
|
if($folders[$count] eq ""){
|
|
|
|
} else {
|
|
|
|
$path = "$path"."/"."$folders[$count]";
|
|
|
|
if($folders[$count] eq $folder_top){
|
2019-07-02 16:34:59 -05:00
|
|
|
#print "$path\n";
|
2019-06-04 16:21:15 -05:00
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
die "ERROR: Script launched from the outside of the $folder_top folder!\n";
|
|
|
|
}
|
|
|
|
|
2019-06-14 12:44:44 -05:00
|
|
|
sub rewrite_file($ $)
|
2019-06-04 16:21:15 -05:00
|
|
|
{
|
2019-06-14 12:44:44 -05:00
|
|
|
my ($arch, $template) = @_;
|
2019-06-04 16:21:15 -05:00
|
|
|
open(IN, '<'.$template);
|
2019-06-14 12:44:44 -05:00
|
|
|
open(OUT, '>'.$arch);
|
2019-07-03 13:49:25 -05:00
|
|
|
|
2019-06-14 12:44:44 -05:00
|
|
|
if($default_keyword eq "TRUE"){
|
|
|
|
my $myPath = &findPath();
|
|
|
|
while(<IN>){
|
|
|
|
$_ =~ s/$keyword/$myPath/g;
|
|
|
|
print OUT $_;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while(<IN>){
|
|
|
|
$_ =~ s/$keyword/$change_to/g;
|
|
|
|
print OUT $_;
|
|
|
|
}
|
2019-06-04 16:21:15 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sub main()
|
|
|
|
{
|
|
|
|
&opts_read();
|
2019-06-14 12:44:44 -05:00
|
|
|
my $rewrite_needed = &rewriting_required_check($arch_file);
|
2019-06-04 16:21:15 -05:00
|
|
|
if($rewrite_needed == 1){
|
2019-06-14 12:44:44 -05:00
|
|
|
if($overwrite eq "TRUE"){
|
2019-07-03 13:49:25 -05:00
|
|
|
my $template_file = &save_original($arch_file);
|
2019-06-14 12:44:44 -05:00
|
|
|
&rewrite_file($arch_file, $template_file);
|
|
|
|
} else {
|
|
|
|
&rewrite_file($new_arch_file, $arch_file);
|
|
|
|
}
|
2019-06-04 16:21:15 -05:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2019-07-03 13:49:25 -05:00
|
|
|
|
2019-06-04 16:21:15 -05:00
|
|
|
&main();
|
2019-07-02 16:34:59 -05:00
|
|
|
exit(0);
|