Some edits to the Perl script

This commit is contained in:
Andrew Johnson
2015-07-08 16:46:34 -05:00
parent 7fd4ac5a67
commit 60823bd2fb
+70 -51
View File
@@ -1,128 +1,147 @@
#!/usr/bin/env perl
#*************************************************************************
# Copyright (c) 2014 Brookhaven National Laboratory.
# EPICS BASE is distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution.
#*************************************************************************
#
# Generate a C header file which
# defines a macro with a string
# describing the VCS revision
#
use FindBin qw($Bin);
use lib "$Bin/../../lib/perl";
use EPICS::Getopts;
use POSIX qw(strftime);
use strict;
our($opt_v,$opt_t,$opt_N,$opt_V);
# RFC 8601 date+time w/ zone (eg "2014-08-29T09:42:47-0700")
my $tfmt = '%Y-%m-%dT%H:%M:%S';
$tfmt .= '%z' unless $^O eq 'MSWin32'; # %z returns zone name on Windows
our ($opt_h, $opt_v);
our $opt_t = '.';
our $opt_N = 'VCSVERSION';
our $opt_V = strftime($tfmt, localtime);
$opt_N = "MODVERSION";
$opt_t = ".";
my $foundvcs = 0;
my $result;
getopts("vt:N:V:") or
die "Usage: genVersionHeader.pl [-t top] [-N NAME] [-V VERSION] output.h";
getopts('hvt:N:V:') && @ARGV == 1
or HELP_MESSAGE();
my ($outfile) = @ARGV;
chomp($opt_V);
if(!$opt_V) {
# RFC 8601 date+time w/ zone (eg "2014-08-29T09:42:47-0700")
$opt_V = strftime "%Y-%m-%dT%H:%M:%S%z", localtime;
}
if(!$foundvcs && -d "$opt_t/_darcs") { # Darcs
if (!$foundvcs && -d "$opt_t/_darcs") { # Darcs
# v1-4-dirty
# is tag 'v1' plus 4 patches
# with uncommited modifications
$result = `cd "$opt_t" && echo "\$(darcs show tags | head -1)-\$((\$(darcs changes --count --from-tag .)-1))"`;
chomp($result);
if(!$? && length($result)>1) {
chomp $result;
if (!$? && length($result) > 1) {
$opt_V = $result;
$foundvcs = 1;
# see if working copy has modifications, additions, removals, or missing files
my $hasmod = `darcs whatsnew --repodir="$opt_t" -l`;
if(!$?) {
$opt_V = "$opt_V-dirty";
}
$opt_V .= '-dirty' unless $?;
}
}
if(!$foundvcs && -d "$opt_t/.hg") { # Mercurial
if (!$foundvcs && -d "$opt_t/.hg") { # Mercurial
# v1-4-abcdef-dirty
# is 4 commits after tag 'v1' with short hash abcdef
# with uncommited modifications
$result = `hg --cwd "$opt_t" tip --template '{latesttag}-{latesttagdistance}-{node|short}\n'`;
chomp($result);
if(!$? && length($result)>1) {
chomp $result;
if (!$? && length($result)>1) {
$opt_V = $result;
$foundvcs = 1;
# see if working copy has modifications, additions, removals, or missing files
my $hasmod = `hg --cwd "$opt_t" status -m -a -r -d`;
chomp($hasmod);
if(length($hasmod)>0) {
$opt_V = "$opt_V-dirty";
}
chomp $hasmod;
$opt_V .= '-dirty' if length($hasmod) > 0;
}
}
if(!$foundvcs && -d "$opt_t/.git") {
if (!$foundvcs && -d "$opt_t/.git") {
# same format as Mercurial
$result = `git --git-dir="$opt_t/.git" describe --tags --dirty`;
chomp($result);
if(!$? && length($result)>1) {
chomp $result;
if (!$? && length($result) > 1) {
$opt_V = $result;
$foundvcs = 1;
}
}
if(!$foundvcs && -d "$opt_t/.svn") {
if (!$foundvcs && -d "$opt_t/.svn") {
# 12345
$result = `cd "$opt_t" && svn info --non-interactive`;
chomp($result);
if(!$? && $result =~ /^Revision:\s*(\d+)/m) {
chomp $result;
if (!$? && $result =~ /^Revision:\s*(\d+)/m) {
$opt_V = $1;
$foundvcs = 1;
# see if working copy has modifications, additions, removals, or missing files
my $hasmod = `cd "$opt_t" && svn status -q --non-interactive`;
chomp($hasmod);
if(length($hasmod)>0) {
$opt_V = "$opt_V-dirty";
}
chomp $hasmod;
$opt_V .= '-dirty' if length($hasmod) > 0;
}
}
if(!$foundvcs && -d "$opt_t/.bzr") {
if (!$foundvcs && -d "$opt_t/.bzr") {
# 12444-anj@aps.anl.gov-20131003210403-icfd8mc37g8vctpf
$result = `cd "$opt_t" && bzr version-info -q --custom --template="{revno}-{revision_id}"`;
chomp($result);
print "BZR $result";
if(!$? && length($result)>1) {
chomp $result;
if (!$? && length($result)>1) {
$opt_V = $result;
$foundvcs = 1;
# see if working copy has modifications, additions, removals, or missing files
# unfortunately "bzr version-info --check-clean ..." doesn't seem to work as documented
my $hasmod = `cd "$opt_t" && bzr status -SV`;
chomp($hasmod);
if(length($hasmod)>0) {
$opt_V = "$opt_V-dirty";
}
chomp $hasmod;
$opt_V .= '-dirty' if length($hasmod) > 0;
}
}
my $output = "#ifndef $opt_N\n# define $opt_N \"$opt_V\"\n#endif\n";
my $output = << "__END";
/* Generated file, do not edit! */
#ifndef $opt_N
# define $opt_N \"$opt_V\"
#endif
__END
print "== would\n$output" if $opt_v;
my $DST;
if(open($DST, '+<', $outfile)) {
my $actual = join("", <$DST>);
if (open($DST, '+<', $outfile)) {
my $actual = join('', <$DST>);
print "== have\n$actual" if $opt_v;
if($actual eq $output) {
if ($actual eq $output) {
print "Keeping existing VCS version header $outfile with \"$opt_V\"\n";
exit(0)
exit 0;
}
print "Updating VCS version header $outfile with \"$opt_V\"\n";
} else {
print "Creating VCS version header $outfile with \"$opt_V\"\n";
open($DST, '>', $outfile) or die "Unable to open or create VCS version header $outfile";
open($DST, '>', $outfile)
or die "Unable to open or create VCS version header $outfile";
}
seek($DST,0,0);
truncate($DST,0);
seek $DST, 0, 0;
truncate $DST, 0;
print $DST $output;
close $DST;
sub HELP_MESSAGE {
print STDERR <<EOF;
Usage:
genVersionHeader.pl -h
Display this Usage message
genVersionHeader.pl [-v] [-t top] [-N NAME] [-V version] output.h";
Generate or update the header file output.h
-t top - Path to the module's top (default '$opt_t')
-N NAME - Macro name to be defined (default '$opt_N')
-v version - Version number if no VCS (default '$opt_V')
EOF
exit $opt_h ? 0 : 1;
}
close($DST);