|
| 1 | +#!/usr/bin/env perl |
| 2 | + |
| 3 | +use warnings; |
| 4 | +use strict; |
| 5 | + |
| 6 | +use Getopt::Long; |
| 7 | +use File::Temp qw/ tempfile tempdir /; |
| 8 | +use File::Basename; |
| 9 | + |
| 10 | +my $filename_arg; |
| 11 | +my $coverity_token_arg; |
| 12 | +my $dry_run_arg = 0; |
| 13 | +my $verbose_arg = 0; |
| 14 | +my $debug_arg = 0; |
| 15 | +my $logfile_dir_arg; |
| 16 | +my $configure_args = ""; |
| 17 | +my $make_args = "-j 32"; |
| 18 | +my $help_arg = 0; |
| 19 | + |
| 20 | +&Getopt::Long::Configure("bundling"); |
| 21 | +my $ok = Getopt::Long::GetOptions("filename=s" => \$filename_arg, |
| 22 | + "coverity-token=s" => \$coverity_token_arg, |
| 23 | + "logfile-dir=s" => \$logfile_dir_arg, |
| 24 | + "configure-args=s" => \$configure_args, |
| 25 | + "make-args=s" => \$make_args, |
| 26 | + "dry-run!" => \$dry_run_arg, |
| 27 | + "verbose!" => \$verbose_arg, |
| 28 | + "debug!" => \$debug_arg, |
| 29 | + "help|h" => \$help_arg); |
| 30 | + |
| 31 | +$ok = 0 |
| 32 | + if (!defined($filename_arg)); |
| 33 | +$ok = 0 |
| 34 | + if (!defined($coverity_token_arg)); |
| 35 | +if (!$ok || $help_arg) { |
| 36 | + print "Usage: $0 --filename=FILENAME --coverity-token=TOKEN [--dry-run] [--verbose] [--help]\n"; |
| 37 | + exit($ok); |
| 38 | +} |
| 39 | + |
| 40 | +die "Cannot read $filename_arg" |
| 41 | + if (! -r $filename_arg); |
| 42 | + |
| 43 | +$verbose_arg = 1 |
| 44 | + if ($debug_arg); |
| 45 | + |
| 46 | +###################################################################### |
| 47 | + |
| 48 | +sub verbose { |
| 49 | + print @_ |
| 50 | + if ($verbose_arg); |
| 51 | +} |
| 52 | + |
| 53 | +# run a command and save the stdout / stderr |
| 54 | +sub safe_system { |
| 55 | + my $allowed_to_fail = shift; |
| 56 | + my $cmd = shift; |
| 57 | + my $stdout_file = shift; |
| 58 | + |
| 59 | + # Redirect stdout if requested or not verbose |
| 60 | + if (defined($stdout_file)) { |
| 61 | + $stdout_file = "$logfile_dir_arg/$stdout_file"; |
| 62 | + unlink($stdout_file); |
| 63 | + $cmd .= " >$stdout_file"; |
| 64 | + } elsif (!$debug_arg) { |
| 65 | + $cmd .= " >/dev/null"; |
| 66 | + } |
| 67 | + $cmd .= " 2>&1"; |
| 68 | + |
| 69 | + my $rc = system($cmd); |
| 70 | + if (0 != $rc && !$allowed_to_fail) { |
| 71 | + # If we die/fail, ensure to change out of the temp tree so |
| 72 | + # that it can be removed upon exit. |
| 73 | + chdir("/"); |
| 74 | + print "Command $cmd failed: exit status $rc\n"; |
| 75 | + if (-f $stdout_file) { |
| 76 | + print "Last command output:\n"; |
| 77 | + system("cat $stdout_file"); |
| 78 | + } |
| 79 | + die "Cannot continue"; |
| 80 | + } |
| 81 | + system("cat $stdout_file") |
| 82 | + if ($debug_arg && defined($stdout_file) && -f $stdout_file); |
| 83 | +} |
| 84 | + |
| 85 | +###################################################################### |
| 86 | + |
| 87 | +# Make an area to work |
| 88 | + |
| 89 | +my $dir = tempdir(CLEANUP => 1); |
| 90 | +chdir($dir); |
| 91 | +verbose "*** Working in $dir\n"; |
| 92 | + |
| 93 | +###################################################################### |
| 94 | + |
| 95 | +# Get the coverity tool, put it in our path |
| 96 | + |
| 97 | +verbose "*** Downloading coverity tool\n"; |
| 98 | +safe_system(0, "wget https://scan.coverity.com/download/linux-64 --post-data \"token=$coverity_token_arg\&project=hwloc\" -O coverity_tool.tgz"); |
| 99 | +safe_system(0, "tar xf coverity_tool.tgz"); |
| 100 | +opendir(my $dh, ".") || |
| 101 | + die "Can't opendir ."; |
| 102 | +my @files = grep { /^cov/ && -d "./$_" } readdir($dh); |
| 103 | +closedir($dh); |
| 104 | + |
| 105 | +my $cov_dir = "$dir/$files[0]/bin"; |
| 106 | +$ENV{PATH} = "$cov_dir:$ENV{PATH}"; |
| 107 | + |
| 108 | +###################################################################### |
| 109 | + |
| 110 | +# Expand the HWLOC tarball, build it |
| 111 | + |
| 112 | +verbose "*** Extracting HWLOC tarball\n"; |
| 113 | +safe_system(0, "tar xf $filename_arg"); |
| 114 | +my $tarball_filename = basename($filename_arg); |
| 115 | +$tarball_filename =~ m/^hwloc-(.+)\.tar.+$/; |
| 116 | +my $hwloc_ver = $1; |
| 117 | +chdir("hwloc-$hwloc_ver"); |
| 118 | + |
| 119 | +verbose "*** Configuring HWLOC tarball\n"; |
| 120 | +safe_system(0, "./configure $configure_args", "configure"); |
| 121 | + |
| 122 | +verbose "*** Building HWLOC tarball\n"; |
| 123 | +safe_system(0, "cov-build --dir cov-int make $make_args", "cov-build"); |
| 124 | + |
| 125 | +verbose "*** Checking HWLOC tarball\n"; |
| 126 | +safe_system(0, "cov-build --dir cov-int make check $make_args", "cov-build-check"); |
| 127 | + |
| 128 | +# Tar up the Coverity results |
| 129 | +verbose "*** Tarring up results\n"; |
| 130 | +safe_system(0, "tar jcf $hwloc_ver-analyzed.tar.bz2 cov-int"); |
| 131 | + |
| 132 | +# If not dry-run, submit to Coverity |
| 133 | +if ($dry_run_arg) { |
| 134 | + verbose "*** Would have submitted, but this is a dry run\n"; |
| 135 | +} else { |
| 136 | + verbose "*** Submitting results\n"; |
| 137 | + safe_system(0, "curl --form token=$coverity_token_arg " . |
| 138 | + "--form email=jsquyres\@cisco.com " . |
| 139 | + "--form file=\@$hwloc_ver-analyzed.tar.bz2 " . |
| 140 | + "--form version=$hwloc_ver " . |
| 141 | + "--form description=nightly-master " . |
| 142 | + "https://scan.coverity.com/builds?project=hwloc", |
| 143 | + "coverity-submit"); |
| 144 | +} |
| 145 | + |
| 146 | +verbose("*** All done\n"); |
| 147 | + |
| 148 | +# Chdir out of the tempdir so that it can be removed |
| 149 | +chdir("/"); |
| 150 | + |
| 151 | +exit(0); |
0 commit comments