2 # A simple system for making software releases
3 # (c) 2003--2010 Martin Mares <mj@ucw.cz>
13 my ($class,$basename) = @_;
15 "PACKAGE" => $basename,
17 # p=preprocess, s=subst, -=discard
18 '(^|/)(CVS|\.arch-ids|{arch}|\.git|tmp)/' => '-',
19 '\.(lsm|spec)$' => 'ps',
26 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
27 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
30 "archivedir" => "/home/mj/tmp/archives/$basename",
45 sub GetVersionFromFile($) {
46 my ($s,$file,$rx) = @_;
47 open F, $file or die "Unable to open $file for version autodetection";
52 print "Detected version $1 from $file\n" if $verbose;
57 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
58 return $s->{"VERSION"};
61 sub GetVersionsFromChangelog($) {
62 my ($s,$file,$rx) = @_;
63 open F, $file or die "Unable to open $file for version autodetection";
67 if (!defined $s->{"VERSION"}) {
69 print "Detected version $1 from $file\n" if $verbose;
70 } elsif ($s->{"VERSION"} eq $1) {
73 $s->{"OLDVERSION"} = $1;
74 print "Detected previous version $1 from $file\n" if $verbose;
80 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
81 return $s->{"VERSION"};
86 $s->{"DISTDIR"} = $dd;
87 print "Initializing dist directory $dd\n" if $verbose;
88 `rm -rf $dd`; die if $?;
89 `mkdir -p $dd`; die if $?;
94 if (defined $s->{$v}) {
97 die "Reference to unknown variable $v";
102 my ($s,$f,$dir,$action) = @_;
104 (my $d = $f) =~ s@(^|/)[^/]*$@@;
106 -d $d || `mkdir -p $d`; die if $?;
108 my $preprocess = ($action =~ /p/);
109 my $subst = ($action =~ /s/);
110 if ($preprocess || $subst) {
111 open I, "$f" or die "open($f): $?";
112 open O, ">$dir/$f" or die "open($dir/$f): $!";
113 my @ifs = (); # stack of conditions, 1=satisfied
114 my $empty = 0; # last line was empty
115 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
118 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
121 if (/^#/ || $is_makefile) {
122 if (/^#?ifdef\s+(\w+)/) {
123 if (defined ${$s->{"conditions"}}{$1}) {
124 push @ifs, ${$s->{"conditions"}}{$1};
128 } elsif (/^#ifndef\s+(\w+)/) {
129 if (defined ${$s->{"conditions"}}{$1}) {
130 push @ifs, -${$s->{"conditions"}}{$1};
134 } elsif (/^#if\s+/) {
136 } elsif (/^#?endif/) {
138 defined $x or die "Improper nesting of conditionals";
140 } elsif (/^#?else/) {
142 defined $x or die "Improper nesting of conditionals";
147 @ifs && $ifs[$#ifs] < 0 && next;
151 } else { $empty = 0; }
157 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
159 `cp -a "$f" "$dir/$f"`; die if $?;
165 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
166 my $dd = $s->{"DISTDIR"};
167 my $pkg = $s->{"PKG"};
168 my $dir = "$dd/$pkg";
169 print "Generating $dir\n";
171 FILES: foreach my $f (`find . -type f`) {
175 my @rules = @{$s->{"rules"}};
177 my $rule = shift @rules;
178 my $act = shift @rules;
184 ($action =~ /-/) && next FILES;
185 print "$f ($action)\n" if $verbose;
186 $s->CopyFile($f, $dir, $action);
189 foreach my $d (@{$s->{"directories"}}) {
190 `mkdir -p $dir/$d`; die if $?;
193 if (-f "$dir/Makefile") {
194 print "Cleaning up\n";
195 `cd $dir && make distclean >&2`; die if $?;
198 print "Creating $dd/$pkg.tar.gz\n";
199 my $tarvv = $verbose ? "vv" : "";
200 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
201 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
203 my $adir = $s->{"archivedir"};
204 my $afile = "$adir/$pkg.tar.gz";
205 print "Archiving to $afile\n";
206 -d $adir or `mkdir -p $adir`;
207 `cp $dd/$pkg.tar.gz $afile`; die if $?;
214 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
215 my $df = $s->{"DISTDIR"} . "/$f";
216 print "Generating $df\n";
217 `cp $sf $df`; die if $?;
218 push @{$s->{"distfiles"}}, $df;
227 --[no]verbose Be chatty about the inner workings of the release system {verbose}
228 --[no]test Test the package before uploading {do_test}
229 --[no]patch Make a patch against the previous version {do_patch}
230 --diff-against=<ver> Set which version we create the patch against
231 --[no]upload Upload released files {do_upload}
232 --[no]git-tag Tag the Git repository with "v<version>" {do_git_tag}
233 --force-git-tag Rewrite the Git tag if it already exists {force_git_tag}
236 return "(default: " . ($_ ? "on" : "off") . ")";
238 $usage =~ s[{(\w+)}][state($s->{$1})]ge;
242 sub ParseOptions($) {
244 $s->{"do_git_tag"} = 1 if (-d ".git");
246 "verbose!" => \$verbose,
247 "test!" => \$s->{"do_test"},
248 "patch!" => \$s->{"do_patch"},
249 "diff-against=s" => \$s->{"diff_against"},
250 "upload!" => \$s->{"do_upload"},
251 'git-tag!' => \$s->{"do_git_tag"},
252 'force-git-tag!' => \$s->{"force_git_tag"},
258 my $dd = $s->{"DISTDIR"};
259 my $pkg = $s->{"PKG"};
260 my $log = "$dd/$pkg.log";
261 print "Doing a test compilation\n";
262 `( cd $dd/$pkg && make ) >$log 2>&1`;
263 die "There were errors. Please inspect $log" if $?;
264 `grep -q [Ww]arning $log`;
265 $? or print "There were warnings! Please inspect $log.\n";
266 print "Cleaning up\n";
267 `cd $dd/$pkg && make distclean`; die if $?;
272 my $dd = $s->{"DISTDIR"};
273 my $pkg1 = $s->{"PKG"};
275 if ($s->{"diff_against"} ne "") {
276 $oldver = $s->{"diff_against"};
277 } elsif (defined $s->{"OLDVERSION"}) {
278 $oldver = $s->{"OLDVERSION"};
280 print "WARNING: No previous version known. No patch generated.\n";
283 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
285 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
286 -f $oldarch or die "MakePatch: $oldarch not found";
287 print "Unpacking $pkg0 from $oldarch\n";
288 `cd $dd && tar xzf $oldarch`; die if $?;
290 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
291 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
292 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
293 push @{$s->{"distfiles"}}, "$dd/$diff";
298 foreach my $u (@{$s->{"uploads"}}) {
299 my $url = $u->{"url"};
300 print "Upload to $url :\n";
302 my $filter = $u->{"filter"} || ".*";
303 foreach my $f (@{$s->{"distfiles"}}) {
309 print "<confirm> "; <STDIN>;
310 if ($url =~ m@^scp://([^/]+)(.*)@) {
316 my $cmd = "scp @files $host:$dir\n";
318 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
321 open FTP, "|ftp -v $host" or die;
322 print FTP "cd $dir\n";
323 foreach my $f (@files) {
324 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
325 print FTP "put $f $ff\n";
331 die "Don't know how to handle this URL scheme";
338 my $tag = 'v' . $s->{'VERSION'};
339 my $force = ($s->{'force_git_tag'} ? '--force' : '');
340 print "Tagging Git repository with $tag\n";
341 `git tag $tag $force`; die if $?;
342 print "Pushing the tags upstream\n";
343 `git push --tags`; die if $?;
348 $s->Test if $s->{"do_test"};
349 $s->MakePatch if $s->{"do_patch"};
350 $s->GitTag if $s->{"do_git_tag"};
351 $s->Upload if $s->{"do_upload"};