Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/buildtools/svnrev/svnrev.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10979 - (hide annotations) (download) (as text)
Mon Oct 9 13:07:25 2023 UTC (6 months ago) by nmaya
File MIME type: text/x-perl
File size: 8047 byte(s)
RELEASE の判定を変更

tt-version.h の TT_VERSION_SUBSTR がなければ RELEASE=1
1 zmatsuo 10347 use strict;
2     use warnings;
3     use utf8;
4     use Getopt::Long 'GetOptions';
5     use POSIX 'strftime';
6    
7     binmode STDOUT, ':encoding(utf8)';
8    
9 zmatsuo 10580 my $tt_version_major = 0;
10     my $tt_version_minor = 0;
11     my $tt_version_substr = "";
12     my $tt_version_h = "../../teraterm/common/tt-version.h";
13     my $version;
14 zmatsuo 10347 my $svn = "svn";
15     my $git = "git";
16     my $out_header = "svnversion.h";
17     my $out_bat = "sourcetree_info.bat";
18     my $out_cmake = "";
19     my $source_root = "..";
20     my $date = strftime "%Y%m%d", localtime;
21     my $time = strftime "%H%M%S", localtime;
22     my $verbose = 0;
23     my $script_name = $0;
24     my $header = "This file was generated by buildtools/svnrev/svnrev.pl";
25 zmatsuo 10565 my $overwrite = 0;
26 zmatsuo 10347 my %svninfo = (
27     name => '',
28     release => 0,
29     Revision => ''
30     );
31    
32     sub read_toolinfo {
33     my $info = "toolinfo.txt";
34     if (!-f $info) {
35     return;
36     }
37     open(my $FD, "<:utf8:crlf", $info);
38     while (my $l = <$FD>) {
39     chomp $l;
40     $l =~ s/^\x{FEFF}//; # remove BOM
41     if ($l =~ /^#/) {
42     next;
43     }
44     if ($l =~ /^svn=\s*(.*)$/) {
45     $svn = $1;
46     }
47     if ($l =~ /^git=\s*(.*)$/) {
48     $git = $1;
49     }
50     }
51     close($FD);
52     }
53    
54     sub search_svn {
55     my @test_list = (
56     "C:/Program Files (x86)/Subversion/bin/svn.exe",
57     "C:/Program Files/Subversion/bin/svn.exe",
58     "C:/Program Files/TortoiseSVN/bin/svn.exe",
59     "C:/cygwin64/bin/svn.exe",
60     "C:/cygwin/bin/svn.exe",
61     "/usr/bin/svn.exe" # msys, cygwin
62     );
63     for (my $i = 0; $i < @test_list; $i++) {
64     my $test = $test_list[$i];
65     if (-e $test) {
66     $svn = $test;
67     return;
68     }
69     }
70     }
71    
72     sub search_git {
73     my @test_list = (
74     "C:/Program Files/Git/bin/git.exe",
75     "C:/cygwin64/bin/git.exe",
76     "C:/cygwin/bin/git.exe",
77     "/usr/bin/git.exe" # msys, cygwin
78     );
79     for (my $i = 0; $i < @test_list; $i++) {
80     my $test = $test_list[$i];
81     if (-e $test) {
82     $git = $test;
83     return;
84     }
85     }
86     }
87    
88 zmatsuo 10564 sub dump_info {
89 zmatsuo 10347 my %info = @_;
90    
91     print "SVNREVISION $info{'Revision'}\n";
92     print "RELEASE $info{'release'}\n";
93     print "BRANCH_NAME $info{'name'}\n";
94     }
95    
96 zmatsuo 10565 sub read_whole_file {
97     my $fname = shift;
98     open(my $FD, '<', $fname) or die "error open $fname";
99    
100     my $text;
101     while (<$FD>) {
102     if (/DATE/ || /TIME/) {
103     ; #pass
104     } else {
105     $text .= $_;
106     }
107     }
108     close $FD;
109     return $text;
110     }
111    
112     sub compare_file {
113     my ($f1, $f2) = @_;
114     if (! -f $f1) {
115     return 1;
116     }
117     if (! -f $f2) {
118     return 1;
119     }
120     my $t1 = read_whole_file($f1);
121     my $t2 = read_whole_file($f2);
122     if ($t1 eq $t2) {
123     # return same
124     return 0;
125     }
126     return 1;
127     }
128    
129 zmatsuo 10564 sub write_info_header {
130 zmatsuo 10347 my ($out_header, %svninfo) = @_;
131     my $revision = $svninfo{'Revision'};
132    
133 zmatsuo 10564 open(my $FD, ">$out_header") or die "error $out_header";
134 zmatsuo 10347 print $FD "/* $header */\n";
135     print $FD "/* #define TT_VERSION_STR \"$version\" check teraterm/common/tt-version.h */\n";
136     if ($revision ne '') {
137     print $FD "#define SVNVERSION $revision\n";
138     } else {
139     print $FD "#undef SVNVERSION\n";
140     }
141     if ($svninfo{'release'}) {
142     print $FD "#define TERATERM_RELEASE 1\n";
143     } else {
144     print $FD "#undef TERATERM_RELEASE\n";
145     }
146     print $FD "#define BRANCH_NAME \"$svninfo{'name'}\"\n";
147     close($FD);
148     }
149    
150 zmatsuo 10564 sub write_info_bat {
151     my ($out_bat, %svninfo) = @_;
152 zmatsuo 10347 my $revision = $svninfo{'Revision'};
153    
154 zmatsuo 10564 open(my $FD, ">$out_bat") or die "error $out_bat";
155 zmatsuo 10347 print $FD "\@rem $header\n";
156     print $FD "set VERSION=$version\n";
157     if ($revision ne '') {
158     print $FD "set SVNVERSION=$revision\n";
159     } else {
160     print $FD "set SVNVERSION=unknown\n";
161     }
162     print $FD "set RELEASE=$svninfo{'release'}\n";
163     print $FD "set DATE=$date\n";
164     print $FD "set TIME=$time\n";
165     close($FD);
166     }
167    
168 zmatsuo 10564 sub write_info_cmake {
169     my ($out_cmake, %svninfo) = @_;
170 zmatsuo 10347 my $revision = $svninfo{'Revision'};
171    
172 zmatsuo 10564 open(my $FD, ">$out_cmake") or die "error $out_cmake";
173 zmatsuo 10347 print $FD "# $header\n";
174     print $FD "set(VERSION \"$version\")\n";
175     if ($revision ne '') {
176     print $FD "set(SVNVERSION \"$revision\")\n";
177     } else {
178     print $FD "#set(SVNVERSION \"0000\")\n";
179     }
180     print $FD "set(RELEASE $svninfo{'release'})\n";
181     print $FD "set(DATE \"$date\")\n";
182     print $FD "set(TIME \"$time\")\n";
183     close($FD);
184     }
185    
186 zmatsuo 10580 sub read_tt_version_h()
187     {
188     # ���������������������������������������
189     if (! -f $tt_version_h) {
190     printf("no header\n");
191     $tt_version_major = 0;
192     $tt_version_minor = 0;
193     $tt_version_substr = "no_header";
194     return;
195     }
196    
197     open(my $FH, '<', $tt_version_h) or die "error open $tt_version_h";
198     while (<$FH>) {
199 zmatsuo 10598 if (/^\s*#define\s+TT_VERSION_MAJOR\s+(\d+)/) {
200 zmatsuo 10580 $tt_version_major = $1;
201     }
202 zmatsuo 10598 elsif (/^\s*#define\s+TT_VERSION_MINOR\s+(\d+)/) {
203 zmatsuo 10580 $tt_version_minor = $1;
204     }
205 zmatsuo 10598 elsif (/^\s*#define\s+TT_VERSION_SUBSTR\s+\"(.+)\"/) {
206 zmatsuo 10580 $tt_version_substr = $1;
207     }
208     }
209     close $FH;
210     }
211    
212     &read_tt_version_h();
213 zmatsuo 10347 &search_svn();
214     &search_git();
215     &read_toolinfo();
216    
217     GetOptions(
218     'root=s' => \$source_root,
219     'svn=s' => \$svn,
220     'git=s' => \$git,
221     'header=s' => \$out_header,
222     'bat=s' => \$out_bat,
223     'cmake=s' => \$out_cmake,
224 zmatsuo 10565 'verbose' => \$verbose,
225     'overwrite' => \$overwrite
226 zmatsuo 10347 );
227    
228     $git =~ s/"//g;
229     $git =~ s/\\/\//g;
230     $svn =~ s/"//g;
231     $svn =~ s/\\/\//g;
232    
233 zmatsuo 10580 if ($tt_version_substr eq "") {
234     $version = "$tt_version_major.$tt_version_minor";
235     } else {
236     $version = "$tt_version_major.$tt_version_minor-$tt_version_substr";
237     }
238    
239 zmatsuo 10347 if ($verbose != 0) {
240     print "root=$source_root\n";
241 zmatsuo 10580 print "tt_version_major=$tt_version_major\n";
242     print "tt_version_minor=$tt_version_minor\n";
243     print "tt_version_substr=$tt_version_substr\n";
244 zmatsuo 10347 print "svn=\"$svn\"\n";
245     print "git=\"$git\"\n";
246     print "header=\"$out_header\"\n";
247     print "bat=\"$out_bat\"\n";
248     print "cmake=\"$out_cmake\"\n";
249 zmatsuo 10565 print "overwrite $overwrite\n";
250 zmatsuo 10347 }
251    
252     if (-d "$source_root/.svn" && $svn ne "") {
253     # svn info������������������������������������������
254     if (!open(my $FD, "-|", "\"$svn\" info --xml $source_root 2>&1")) {
255     # svn ���������������������
256     print "$script_name: '$svn' can not execute\n";
257     }
258     else {
259     # ������������������������������
260     my $text = do { local $/; <$FD> };
261     close($FD);
262    
263     # xml���������������������������������������������������������������������
264     # ������������������������������
265     if ($text =~ /<commit([^>]+)>/) {
266     my $commit = $1;
267     if ($commit =~ /revision=\"(\d+)\"/) {
268     $svninfo{'Revision'} = $1;
269     }
270     }
271     if ($text =~ /<relative-url>(.+)<\/relative-url>/) {
272     my $url = $1;
273     my $name = $url;
274     $name =~ s/^\^\/(.*)$/$1/; # "\/" ���������������
275     $svninfo{'name'} = $name;
276     }
277     }
278     }
279     elsif(-d "$source_root/.git" && $git ne "") {
280     my $branch = `\"$git\" rev-parse --abbrev-ref HEAD`;
281     if ($branch eq '') {
282     # git ���������������������
283     print "$script_name: \"$git\" can not execute\n";
284     }
285     else {
286     $branch =~ s/[\r\n]$//g;
287     $svninfo{'name'} = $branch;
288    
289     if (-d "$source_root/.git/svn") {
290     # use git svn log
291     my $revision = `\"$git\" svn log --oneline -1`;
292     $revision =~ s/^r(\d+).*$/$1/;
293     $svninfo{'Revision'} = $1;
294     }
295     else {
296 zmatsuo 10348 $svninfo{'Revision'} = '';
297 zmatsuo 10347 }
298     }
299     }
300     else {
301     # do not use VCS
302     }
303    
304 nmaya 10979 my $release = 0;
305     if ($tt_version_substr eq "") {
306     $release = 1;
307     }
308     $svninfo{'release'} = $release;
309    
310    
311 zmatsuo 10347 if ($verbose != 0) {
312     &dump_info(%svninfo);
313     }
314    
315 zmatsuo 10565 my $same;
316 zmatsuo 10347
317     # output for source(C,C++) header
318 zmatsuo 10565 $same = 0;
319     my $out_header_tmp = $out_header . ".tmp";
320     write_info_header($out_header_tmp, %svninfo);
321     if (!$overwrite && &compare_file($out_header, $out_header_tmp) == 0) {
322     # same
323     unlink $out_header_tmp;
324     $same = 1;
325     } else {
326     #diff
327     unlink $out_header;
328     rename $out_header_tmp, $out_header;
329 zmatsuo 10347 }
330 zmatsuo 10565 if ($verbose != 0) {
331     printf("%s '$out_header'\n", $same == 1 ? "exists" : "update" );
332     }
333 zmatsuo 10347
334     # output for bat file
335 zmatsuo 10565 $same = 0;
336     my $out_bat_tmp = $out_bat . ".tmp";
337     write_info_bat($out_bat_tmp, %svninfo);
338     if (!$overwrite && &compare_file($out_bat, $out_bat_tmp) == 0) {
339     # same
340     unlink $out_bat_tmp;
341     $same = 1;
342     } else {
343     #diff
344     unlink $out_bat;
345     rename $out_bat_tmp, $out_bat;
346     }
347 zmatsuo 10347 if ($verbose != 0) {
348 zmatsuo 10565 printf("%s '$out_bat'\n", $same == 1 ? "exists" : "update" );
349 zmatsuo 10347 }
350    
351     # output for cmake
352     if ($out_cmake ne "") {
353 zmatsuo 10565 my $out_cmake_tmp = $out_cmake . ".tmp";
354     &write_info_cmake($out_cmake_tmp, %svninfo);
355     $same = 0;
356     if (!$overwrite && &compare_file($out_cmake, $out_cmake_tmp) == 0) {
357     # same
358     unlink $out_cmake_tmp;
359     $same = 1;
360     } else {
361     # diff
362     unlink $out_cmake;
363     rename $out_cmake_tmp, $out_cmake;
364     }
365 zmatsuo 10347 if ($verbose != 0) {
366 zmatsuo 10565 printf("%s '$out_cmake'\n", $same == 1 ? "exists" : "update" );
367 zmatsuo 10347 }
368     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26