-
Notifications
You must be signed in to change notification settings - Fork 0
/
pvcs.pl
executable file
·317 lines (275 loc) · 10.5 KB
/
pvcs.pl
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
#! /usr/bin/env perl
#
# ICON
#
# ---------------------------------------------------------------
# Copyright (C) 2004-2024, DWD, MPI-M, DKRZ, KIT, ETH, MeteoSwiss
# Contact information: icon-model.org
# See AUTHORS.TXT for a list of authors
# See LICENSES/ for license information
# SPDX-License-Identifier: BSD-3-Clause
# ---------------------------------------------------------------
#
#_____________________________________________________________________________
#
# Luis Kornblueh, MPIM, 2012-05-31
# Luis Kornblueh, MPIM, 2013-10-16
# - add functions to return strings with
# the revision information
# Luis Kornblueh, MPIM, 2024-01-15
# - remove svn support
# - add VERSION file replacing querying
# git when git is not available
#_____________________________________________________________________________
=head1 pvcs
pvcs - Enabling provenance collection for VCSs
=head1 SYNOPSIS
pvcs [options] [VCS working directory]
Options:
--srcdir <VCS working directory>
--version creates a VERSION file for VCS less handling of package
--help usage
=cut
=head1 OPTIONS
=over 8
=item B<--srcdir <VCS working directory>>
VCS handled top level directory which gets compiled
=item B<--version
Creates a VERSION file for VCS less handling of package
=item B<--help>
Prints the usage documentation and exits
=back
=cut
=head1 DESCRIPTION
This program will generate a c source file which contains persistent
information on the repository, branch, and revision of the compiled
source code. The file is suitable for being linked into the executable.
=cut
#_____________________________________________________________________________
use strict;
use warnings;
use constant { true => 1, false => 0 };
use Getopt::Long;
use Pod::Usage;
use File::Temp;
use File::Compare;
use File::Copy;
my $srcdir = '.';
my $version = 0;
my $help = 0;
GetOptions( 'help|?' => \$help, 'srcdir=s' => \$srcdir, 'version' => \$version) or pod2usage(-exitstatus => 2);
pod2usage(-exitstatus => 1) if $help;
my $version_fn = "VERSION";
my $remote_url = '';
my $branch = '';
my $revision = '';
my $git_tag = '';
my $art_branch = '';
my $art_revision = '';
my $art_remote_url = '';
if ( -d $srcdir."/.git" ) {
$remote_url = `git --git-dir $srcdir/.git config --get remote.origin.url`;
chomp($remote_url);
if (index("$remote_url", "gitlab.dkrz.de") != -1) {
my @branches = `git --git-dir $srcdir/.git branch`;
@branches = grep(/^\*/, @branches);
$branch = $branches[0];
$branch =~ s/\* *//;
$branch =~ s/ *\n//;
my @revisions = `git --git-dir $srcdir/.git --no-pager log --max-count=1`;
@revisions = grep(/commit/, @revisions);
$revision = $revisions[0];
$revision =~ s/commit *//;
$revision =~ s/ *\n//;
if ( -d $srcdir."/externals/art/.git" ) {
$art_remote_url = `git --git-dir $srcdir/externals/art/.git config --get remote.origin.url`;
chomp($art_remote_url);
my @art_branches = `git --git-dir $srcdir//externals/art/.git branch`;
@art_branches = grep(/^\*/, @art_branches);
$art_branch = $art_branches[0];
$art_branch =~ s/\* *//;
$art_branch =~ s/ *\n//;
my @art_revisions = `git --git-dir $srcdir/externals/art/.git --no-pager log --max-count=1`;
@art_revisions = grep(/commit/, @art_revisions);
$art_revision = $art_revisions[0];
$art_revision =~ s/commit *//;
$art_revision =~ s/ *\n//;
}
$git_tag = `git describe --tags --abbrev=0 --exact-match 2>/dev/null`;
if ($?) {
$git_tag = 'No GIT tag matches exactly.';
} else {
$git_tag =~ s/ *\n//;
}
}
} else {
if ( not -e $version_fn ) {
print "Unknown repository type or no working copy/repository: no support will be given.\n";
$remote_url = "Unknown";
$branch = "Unknown";
$revision = "Unknown";
}
}
if ( $version == 1 ){
my $version_v;
print "Write VERSION file ...\n";
open $version_v, ">", "$version_fn" or die "$0: open $version_fn: $!";
print $version_v "ICON remote URL: $remote_url\n";
print $version_v "ICON branch: $branch\n";
print $version_v "ICON revision: retrieved from VERSION file\n";
print $version_v "ICON tag: $git_tag\n";
print $version_v "ICON ART remote URL: $art_remote_url\n";
print $version_v "ICON ART branch: $art_branch\n";
print $version_v "ICON ART revision: $art_revision\n";
close $version_v;
exit 0;
}
my $need_to_compare = false;
my $version_c;
my $fname;
if ( -e $version_fn ) {
my $header;
open my $version_info, "<", "$version_fn" or die "Could not open $version_fn: $!";
($header, $remote_url) = split /:/, <$version_info>, 2; $remote_url =~ s/^\s+|\s+$//g;
($header, $branch) = split /:/, <$version_info>, 2; $branch =~ s/^\s+|\s+$//g;
($header, $revision) = split /:/, <$version_info>, 2; $revision =~ s/^\s+|\s+$//g;
($header, $git_tag) = split /:/, <$version_info>, 2; $git_tag =~ s/^\s+|\s+$//g;
($header, $art_remote_url) = split /:/, <$version_info>, 2; $art_remote_url =~ s/^\s+|\s+$//g;
($header, $art_branch) = split /:/, <$version_info>, 2; $art_branch =~ s/^\s+|\s+$//g;
($header, $art_revision) = split /:/, <$version_info>, 2; $art_revision =~ s/^\s+|\s+$//g;
close($version_info);
}
if ( -e "version.c") {
$need_to_compare = true;
$version_c = File::Temp->new(UNLINK => false);
$fname = $version_c->filename;
} else {
open $version_c, ">", "version.c" or die "$0: open version.c: $!";
}
print $version_c "#ifdef __xlc__\n";
print $version_c "#pragma comment(user,\"$remote_url,$branch,$revision\")\n";
print $version_c "#pragma comment(user,\"$art_remote_url,$art_branch,$art_revision\")\n";
print $version_c "#endif\n";
print $version_c "#include <string.h>\n";
print $version_c "\n";
print $version_c "const char remote_url[] = \"$remote_url\";\n";
print $version_c "const char branch[] = \"$branch\";\n";
print $version_c "const char revision[] = \"$revision\";\n";
print $version_c "const char git_tag[] = \"$git_tag\";\n";
print $version_c "const char art_remote_url[] = \"$art_remote_url\";\n";
print $version_c "const char art_branch[] = \"$art_branch\";\n";
print $version_c "const char art_revision[] = \"$art_revision\";\n";
print $version_c "\n";
print $version_c "void repository_url(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(remote_url) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, remote_url);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
print $version_c "void branch_name(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(branch) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, branch);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
print $version_c "void revision_key(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(revision) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, revision);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
print $version_c "void git_tag_name(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(git_tag) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, git_tag);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
print $version_c "void art_repository_url(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(art_remote_url) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, art_remote_url);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "void art_branch_name(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(art_branch) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, art_branch);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
print $version_c "void art_revision_key(char *name, int *actual_len)\n";
print $version_c "{\n";
print $version_c " if (strlen(art_revision) > *actual_len)\n";
print $version_c " {\n";
print $version_c " *actual_len = 0;\n";
print $version_c " }\n";
print $version_c " else\n";
print $version_c " {\n";
print $version_c " strcpy(name, art_revision);\n";
print $version_c " *actual_len = strlen(name);\n";
print $version_c " }\n";
print $version_c "\n";
print $version_c " return;\n";
print $version_c "}\n";
print $version_c "\n";
close $version_c;
if ($need_to_compare) {
if (compare($fname, "version.c") == 0) {
unlink $fname;
} else {
move($fname, "version.c") or die "Copy failed: $!";
}
}
exit 0;