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)/' => '-',
20 '\.(lsm|spec)$' => 'ps',
27 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
28 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
31 "archivedir" => $ENV{HOME} . "/archives/sw/$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 if ($s->{'do_sign'}) {
204 print "Signing package\n";
205 system "gpg", "--armor", "--detach-sig", "$dd/$pkg.tar.gz";
207 rename "$dd/$pkg.tar.gz.asc", "$dd/$pkg.tar.gz.sign" or die "No signature produced!?\n";
208 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz.sign";
211 my $adir = $s->{"archivedir"};
212 my $afile = "$adir/$pkg.tar.gz";
213 print "Archiving to $afile\n";
214 -d $adir or `mkdir -p $adir`;
215 `cp $dd/$pkg.tar.gz $afile`; die if $?;
222 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
223 my $df = $s->{"DISTDIR"} . "/$f";
224 print "Generating $df\n";
225 `cp $sf $df`; die if $?;
226 push @{$s->{"distfiles"}}, $df;
229 sub ParseOptions($) {
232 "verbose!" => \$verbose,
233 "test!" => \$s->{"do_test"},
234 "patch!" => \$s->{"do_patch"},
235 "diff-against=s" => \$s->{"diff_against"},
236 "upload!" => \$s->{"do_upload"},
237 "sign!" => \$s->{"do_sign"},
238 ) || die "Syntax: release [--verbose] [--test] [--nopatch] [--diff-against=<version>] [--noupload] [--nosign]";
243 my $dd = $s->{"DISTDIR"};
244 my $pkg = $s->{"PKG"};
245 my $log = "$dd/$pkg.log";
246 print "Doing a test compilation\n";
247 `( cd $dd/$pkg && make ) >$log 2>&1`;
248 die "There were errors. Please inspect $log" if $?;
249 `grep -q [Ww]arning $log`;
250 $? or print "There were warnings! Please inspect $log.\n";
251 print "Cleaning up\n";
252 `cd $dd/$pkg && make distclean`; die if $?;
257 my $dd = $s->{"DISTDIR"};
258 my $pkg1 = $s->{"PKG"};
260 if ($s->{"diff_against"} ne "") {
261 $oldver = $s->{"diff_against"};
262 } elsif (defined $s->{"OLDVERSION"}) {
263 $oldver = $s->{"OLDVERSION"};
265 print "WARNING: No previous version known. No patch generated.\n";
268 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
270 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
271 -f $oldarch or die "MakePatch: $oldarch not found";
272 print "Unpacking $pkg0 from $oldarch\n";
273 `cd $dd && tar xzf $oldarch`; die if $?;
275 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
276 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
277 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
278 push @{$s->{"distfiles"}}, "$dd/$diff";
283 foreach my $u (@{$s->{"uploads"}}) {
284 my $url = $u->{"url"};
285 print "Upload to $url :\n";
287 my $filter = $u->{"filter"} || ".*";
288 foreach my $f (@{$s->{"distfiles"}}) {
294 print "<confirm> "; <STDIN>;
295 if ($url =~ m@^scp://([^/]+)(.*)@) {
301 my $cmd = "scp @files $host:$dir\n";
303 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
306 open FTP, "|ftp -v $host" or die;
307 print FTP "cd $dir\n";
308 foreach my $f (@files) {
309 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
310 print FTP "put $f $ff\n";
316 die "Don't know how to handle this URL scheme";
323 $s->Test if $s->{"do_test"};
324 $s->MakePatch if $s->{"do_patch"};
325 $s->Upload if $s->{"do_upload"};