68N/A#! /usr/perl5/5.12/bin/perl
68N/A#
68N/A# Copyright (c) 2008, 2011, Oracle and/or its affiliates. All rights reserved.
68N/A#
943N/A# Permission is hereby granted, free of charge, to any person obtaining a
68N/A# copy of this software and associated documentation files (the "Software"),
68N/A# to deal in the Software without restriction, including without limitation
919N/A# the rights to use, copy, modify, merge, publish, distribute, sublicense,
919N/A# and/or sell copies of the Software, and to permit persons to whom the
919N/A# Software is furnished to do so, subject to the following conditions:
919N/A#
919N/A# The above copyright notice and this permission notice (including the next
919N/A# paragraph) shall be included in all copies or substantial portions of the
919N/A# Software.
919N/A#
919N/A# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
919N/A# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
919N/A# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
919N/A# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
919N/A# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
919N/A# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
919N/A# DEALINGS IN THE SOFTWARE.
919N/A#
919N/A#
68N/A
68N/A#
68N/A# Set version information in ELF files to give hints to help in troubleshooting
68N/A#
493N/A# Usage: set-elf-comments.pl [-b] [-B <pkgversion>] -M "module version" \
68N/A# [-X <exclude regexp>] <path>
68N/A#
962N/A# <pkgversion> may either be a path to a file containing BUILD="<version>"
68N/A# or the string "hg id" to get the version from the hg id output.
911N/A#
962N/A# If the XBUILD_HG_ID environment variable is set, it is used for the hg id
962N/A# instead of forking a hg id process for every component in a full tree build
911N/A#
68N/A# If -X is specified, it gives a regular expresion for filenames to skip
87N/A# for this module.
68N/A
599N/Arequire 5.10.0; # needed for Time::Hires::stat
599N/A
599N/Ause strict;
68N/Ause warnings;
68N/Ause Getopt::Std;
68N/Ause POSIX qw(strftime);
970N/Ause File::Find;
970N/Ause Time::HiRes qw();
970N/A
970N/A$Getopt::Std::STANDARD_HELP_VERSION = 1;
970N/A
970N/Amy %opts;
68N/Agetopts('B:M:X:b', \%opts);
68N/A
493N/Amy $module_version_info = '@(#)';
68N/Aif (exists($opts{'M'})) {
68N/A $module_version_info .= $opts{'M'};
68N/A} else {
493N/A die qq(Must specify -M "module version");
68N/A}
68N/A
68N/Amy $exclude_regexp;
68N/Aif (exists($opts{'X'})) {
970N/A $exclude_regexp = $opts{'X'};
970N/A}
970N/A
68N/Aif (exists($opts{'b'})) {
# Add build info, including date & anything specified by -B
my $build_info = strftime("%e %b %Y", localtime);
if (exists($opts{'B'})) {
my $build_version_file = $opts{'B'};
if ($build_version_file eq 'hg id') {
my $hg_id = 'revision unavailable';
if (exists $ENV{'XBUILD_HG_ID'}) {
$hg_id = $ENV{'XBUILD_HG_ID'};
} else {
open my $VERS, '-|', $build_version_file
or die "Can't run $build_version_file: $!\n";
while ($_ = <$VERS>) {
chomp($_);
if ($_ =~ m/\S+/) {
my ($rev, $tag) = split(' ', $_, 2);
if ($tag eq 'tip') {
$hg_id = $rev;
} else {
$hg_id = $_;
}
}
}
close $VERS;
}
$build_info = "hg: $hg_id - $build_info";
} else {
open my $VERS, '<', $build_version_file
or die "Can't open $build_version_file for reading: $!\n";
while ($_ = <$VERS>) {
if ($_ =~ m/^BUILD="(.*)"/) {
my $v = $1 / 100.0;
if ($v >= 1.0) {
$build_info = "build $v - " . $build_info;
}
}
}
close $VERS;
}
}
$module_version_info .= " ($build_info)";
}
$module_version_info =~ s/\s+$//ms;
my %elf_files;
sub scan_file {
# skip sources & intermediate build files that we don't ship
return if $_ =~ m/\.[acho]$/ims;
# skip files matching specified regexp
if (defined $exclude_regexp) {
if ($_ =~ m{$exclude_regexp}so) {
print "Excluding $_\n";
return;
}
}
# If the file is not a symlink, is a regular file, and is at least 256 bytes
if ((! -l $_) && (-f _) && (-s _ > 256)) {
open my $IN, '<', $_
or die "Can't open $_ for reading: $!\n";
my $magic_number;
sysread($IN, $magic_number, 4)
or die "Can't read from $_: $!\n";
close $IN;
if ($magic_number eq "\177ELF") {
my @sb = Time::HiRes::stat($_);
$elf_files{$File::Find::name} = $sb[9]; # mtime
}
}
}
find(\&scan_file, @ARGV);
if (scalar(keys %elf_files) > 0) {
my @filelist = sort { $elf_files{$a} <=> $elf_files{$b} } keys %elf_files;
my @cmd = ('/usr/bin/mcs', '-a', $module_version_info, '-c', @filelist);
print join(' ', map { if ($_ =~ /\s+/) { qq("$_") } else { $_ } } @cmd),
"\n";
system(@cmd) == 0
or die "*** mcs failed: $?\n";
}