2 # A simple system for making software releases
3 # (c) 2003--2011 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",
44 sub GetVersionFromFile($) {
45 my ($s,$file,$rx) = @_;
46 open F, $file or die "Unable to open $file for version autodetection";
51 print "Detected version $1 from $file\n" if $verbose;
56 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
57 return $s->{"VERSION"};
60 sub GetVersionsFromChangelog($) {
61 my ($s,$file,$rx) = @_;
62 open F, $file or die "Unable to open $file for version autodetection";
66 if (!defined $s->{"VERSION"}) {
68 print "Detected version $1 from $file\n" if $verbose;
69 } elsif ($s->{"VERSION"} eq $1) {
72 $s->{"OLDVERSION"} = $1;
73 print "Detected previous version $1 from $file\n" if $verbose;
79 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
80 return $s->{"VERSION"};
85 $s->{"DISTDIR"} = $dd;
86 print "Initializing dist directory $dd\n" if $verbose;
87 `rm -rf $dd`; die if $?;
88 `mkdir -p $dd`; die if $?;
93 if (defined $s->{$v}) {
96 die "Reference to unknown variable $v";
101 my ($s,$f,$dir,$action) = @_;
103 (my $d = $f) =~ s@(^|/)[^/]*$@@;
105 -d $d || `mkdir -p $d`; die if $?;
107 my $preprocess = ($action =~ /p/);
108 my $subst = ($action =~ /s/);
109 if ($preprocess || $subst) {
110 open I, "$f" or die "open($f): $?";
111 open O, ">$dir/$f" or die "open($dir/$f): $!";
112 my @ifs = (); # stack of conditions, 1=satisfied
113 my $empty = 0; # last line was empty
114 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
117 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
120 if (/^#/ || $is_makefile) {
121 if (/^#?ifdef\s+(\w+)/) {
122 if (defined ${$s->{"conditions"}}{$1}) {
123 push @ifs, ${$s->{"conditions"}}{$1};
127 } elsif (/^#ifndef\s+(\w+)/) {
128 if (defined ${$s->{"conditions"}}{$1}) {
129 push @ifs, -${$s->{"conditions"}}{$1};
133 } elsif (/^#if\s+/) {
135 } elsif (/^#?endif/) {
137 defined $x or die "Improper nesting of conditionals";
139 } elsif (/^#?else/) {
141 defined $x or die "Improper nesting of conditionals";
146 @ifs && $ifs[$#ifs] < 0 && next;
150 } else { $empty = 0; }
156 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
158 `cp -a "$f" "$dir/$f"`; die if $?;
164 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
165 my $dd = $s->{"DISTDIR"};
166 my $pkg = $s->{"PKG"};
167 my $dir = "$dd/$pkg";
168 print "Generating $dir\n";
170 FILES: foreach my $f (`find . -type f`) {
174 my @rules = @{$s->{"rules"}};
176 my $rule = shift @rules;
177 my $act = shift @rules;
183 ($action =~ /-/) && next FILES;
184 print "$f ($action)\n" if $verbose;
185 $s->CopyFile($f, $dir, $action);
188 foreach my $d (@{$s->{"directories"}}) {
189 `mkdir -p $dir/$d`; die if $?;
192 if (-f "$dir/Makefile") {
193 print "Cleaning up\n";
194 `cd $dir && make distclean >&2`; die if $?;
197 print "Creating $dd/$pkg.tar.gz\n";
198 my $tarvv = $verbose ? "vv" : "";
199 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
200 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
202 if ($s->{'do_sign'}) {
203 print "Signing package\n";
204 system "gpg", "--armor", "--detach-sig", "$dd/$pkg.tar.gz";
206 rename "$dd/$pkg.tar.gz.asc", "$dd/$pkg.tar.gz.sign" or die "No signature produced!?\n";
207 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz.sign";
210 my $adir = $s->{"archivedir"};
211 my $afile = "$adir/$pkg.tar.gz";
212 print "Archiving to $afile\n";
213 -d $adir or `mkdir -p $adir`;
214 `cp $dd/$pkg.tar.gz $afile`; die if $?;
221 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
222 my $df = $s->{"DISTDIR"} . "/$f";
223 print "Generating $df\n";
224 `cp $sf $df`; die if $?;
225 push @{$s->{"distfiles"}}, $df;
228 sub ParseOptions($) {
231 "verbose!" => \$verbose,
232 "test!" => \$s->{"do_test"},
233 "patch!" => \$s->{"do_patch"},
234 "diff-against=s" => \$s->{"diff_against"},
235 "upload!" => \$s->{"do_upload"},
236 "sign!" => \$s->{"do_sign"},
237 ) || die "Syntax: release [--verbose] [--test] [--nopatch] [--diff-against=<version>] [--noupload] [--nosign]";
242 my $dd = $s->{"DISTDIR"};
243 my $pkg = $s->{"PKG"};
244 my $log = "$dd/$pkg.log";
245 print "Doing a test compilation\n";
246 `( cd $dd/$pkg && make ) >$log 2>&1`;
247 die "There were errors. Please inspect $log" if $?;
248 `grep -q [Ww]arning $log`;
249 $? or print "There were warnings! Please inspect $log.\n";
250 print "Cleaning up\n";
251 `cd $dd/$pkg && make distclean`; die if $?;
256 my $dd = $s->{"DISTDIR"};
257 my $pkg1 = $s->{"PKG"};
259 if ($s->{"diff_against"} ne "") {
260 $oldver = $s->{"diff_against"};
261 } elsif (defined $s->{"OLDVERSION"}) {
262 $oldver = $s->{"OLDVERSION"};
264 print "WARNING: No previous version known. No patch generated.\n";
267 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
269 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
270 -f $oldarch or die "MakePatch: $oldarch not found";
271 print "Unpacking $pkg0 from $oldarch\n";
272 `cd $dd && tar xzf $oldarch`; die if $?;
274 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
275 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
276 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
277 push @{$s->{"distfiles"}}, "$dd/$diff";
282 foreach my $u (@{$s->{"uploads"}}) {
283 my $url = $u->{"url"};
284 print "Upload to $url :\n";
286 my $filter = $u->{"filter"} || ".*";
287 foreach my $f (@{$s->{"distfiles"}}) {
293 print "<confirm> "; <STDIN>;
294 if ($url =~ m@^scp://([^/]+)(.*)@) {
300 my $cmd = "scp @files $host:$dir\n";
302 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
305 open FTP, "|ftp -v $host" or die;
306 print FTP "cd $dir\n";
307 foreach my $f (@files) {
308 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
309 print FTP "put $f $ff\n";
315 die "Don't know how to handle this URL scheme";
322 $s->Test if $s->{"do_test"};
323 $s->MakePatch if $s->{"do_patch"};
324 $s->Upload if $s->{"do_upload"};