summaryrefslogtreecommitdiff
path: root/cesar/common/tools/build-info
blob: df1e6b100b5f8c347b0b4f93863619fa85b541a6 (plain)
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
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long qw(:config no_ignore_case bundling);
use Pod::Usage;

# Option parsing.
my $help;
my @info;
my $tag = 1;
my $blank = 0;
my $header;
GetOptions (
    'help|h' => \$help,
    'info|i=s' => \@info,
    'tag!' => \$tag,
    'blank-lines|b:9' => \$blank,
    'header|H=s' => \$header,
) or pod2usage (2);
pod2usage (1) if $help;
@ARGV == 0 or pod2usage (2);

# Define available information.

my %info = (
    project => \&info_project,
    version => \&info_version,
    implementation => \&info_implementation,
);

# Get project directory.
sub info_project
{
    use Cwd;
    my @dir = split '/', getcwd ();
    my @project;
    # Iteratively remove a directory part to find root.
    do
    {
	@dir or die "cannot find the base directory.\n";
	unshift @project, pop @dir;
    } while (!-r join ('/', @dir) . '/common/make/top.mk');
    return join ('/', @project);
}

# Get git version.
sub info_version
{
    my $version = qx/git describe --dirty --always/
	or die "cannot get version.\n";
    chomp $version;
    return $version;
}

# Get implementation version.
sub info_implementation
{
    my $version = info_version;
    if ($version =~ /^(av|eoc)-(\d+)\.(\d+)(?:\.(\d+))?-?/)
    {
        my $v = 0;
        $v |= 0x8000 if $1 eq 'eoc';
        if ($2 < 0x10 && $3 < 0x10 && (($4 && $4 < 0x10) || !$4))
        {
            $v |= $2 << 8;
            $v |= $3 << 4;
            $v |= $4 if $4;
        }
        return sprintf '0x%04x', $v;
    }
    return 0;
}

# Check --info parameter.
@info = qw/project version/ unless @info;
pod2usage ("unknown info requested.\n") if grep { !exists $info{$_} } @info;

if (!$header)
{
    # Output.
    print "\n" x $blank;
    for my $info (@info)
    {
	my $text = $info{$info} ();
	my $itag = '';
	($itag = "$info: ") =~ s/(\b[a-z])/\u$1/g if $tag;
	print "$itag$text\n";
    }
}
else
{
    # Header output.
    my $name = $header;
    $name =~ tr#/\.#__#;
    $name =~ s/\-/\_/g;
    my $new = <<EOF;
#ifndef $name
#define $name
/* This is an autogenerated file. */

EOF
    for my $info (@info)
    {
	my $text = $info{$info} ();
	$text =~ /^(?:0x[0-9a-f]+|\d+)$/
	    or $text = '"' . $text . '"';
	my $def = uc $info;
	$new .= "#define BUILD_INFO_$def $text\n";
    }
    $new .= <<EOF;

#endif /* $name */
EOF
    # Read old file and write only if do not match.
    my $old = '';
    if (open OLD, "<$header")
    {
	$old = join '', <OLD>;
	close OLD;
    }
    if ($new ne $old)
    {
	open NEW, ">$header"
	    or die "can not open \"$header\"\n";
	print NEW $new;
	close NEW;
    }
}

__END__

=head1 NAME

build-info - print build information

=head1 SYNOPSIS

build-info [options]

 Options:
   -h, --help			brief help message
   -i, --info=INFO		include specified INFO
   --no-tag			do not print information tag
   -b, --blank-lines[=N]	prepend N blank lines (default: 9)
   -H, --header=FILE		generate a C header file, file is only written
				if content has changed

=cut