2 # A simple system for making software releases
3 # (c) 2003--2006 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",
43 sub GetVersionFromFile($) {
44 my ($s,$file,$rx) = @_;
45 open F, $file or die "Unable to open $file for version autodetection";
50 print "Detected version $1 from $file\n" if $verbose;
55 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
56 return $s->{"VERSION"};
59 sub GetVersionsFromChangelog($) {
60 my ($s,$file,$rx) = @_;
61 open F, $file or die "Unable to open $file for version autodetection";
65 if (!defined $s->{"VERSION"}) {
67 print "Detected version $1 from $file\n" if $verbose;
68 } elsif ($s->{"VERSION"} eq $1) {
71 $s->{"OLDVERSION"} = $1;
72 print "Detected previous version $1 from $file\n" if $verbose;
78 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
79 return $s->{"VERSION"};
84 $s->{"DISTDIR"} = $dd;
85 print "Initializing dist directory $dd\n" if $verbose;
86 `rm -rf $dd`; die if $?;
87 `mkdir -p $dd`; die if $?;
92 if (defined $s->{$v}) {
95 die "Reference to unknown variable $v";
100 my ($s,$f,$dir,$action) = @_;
102 (my $d = $f) =~ s@(^|/)[^/]*$@@;
104 -d $d || `mkdir -p $d`; die if $?;
106 my $preprocess = ($action =~ /p/);
107 my $subst = ($action =~ /s/);
108 if ($preprocess || $subst) {
109 open I, "$f" or die "open($f): $?";
110 open O, ">$dir/$f" or die "open($dir/$f): $!";
111 my @ifs = (); # stack of conditions, 1=satisfied
112 my $empty = 0; # last line was empty
113 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
116 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
119 if (/^#/ || $is_makefile) {
120 if (/^#?ifdef\s+(\w+)/) {
121 if (defined ${$s->{"conditions"}}{$1}) {
122 push @ifs, ${$s->{"conditions"}}{$1};
126 } elsif (/^#ifndef\s+(\w+)/) {
127 if (defined ${$s->{"conditions"}}{$1}) {
128 push @ifs, -${$s->{"conditions"}}{$1};
132 } elsif (/^#if\s+/) {
134 } elsif (/^#?endif/) {
136 defined $x or die "Improper nesting of conditionals";
138 } elsif (/^#?else/) {
140 defined $x or die "Improper nesting of conditionals";
145 @ifs && $ifs[$#ifs] < 0 && next;
149 } else { $empty = 0; }
155 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
157 `cp -a $f $dir/$f`; die if $?;
163 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
164 my $dd = $s->{"DISTDIR"};
165 my $pkg = $s->{"PKG"};
166 my $dir = "$dd/$pkg";
167 print "Generating $dir\n";
169 FILES: foreach my $f (`find . -type f`) {
173 my @rules = @{$s->{"rules"}};
175 my $rule = shift @rules;
176 my $act = shift @rules;
182 ($action =~ /-/) && next FILES;
183 print "$f ($action)\n" if $verbose;
184 $s->CopyFile($f, $dir, $action);
187 foreach my $d (@{$s->{"directories"}}) {
188 `mkdir -p $dir/$d`; die if $?;
191 if (-f "$dir/Makefile") {
192 print "Cleaning up\n";
193 `cd $dir && make distclean >&2`; die if $?;
196 print "Creating $dd/$pkg.tar.gz\n";
197 my $tarvv = $verbose ? "vv" : "";
198 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
199 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
201 my $adir = $s->{"archivedir"};
202 my $afile = "$adir/$pkg.tar.gz";
203 print "Archiving to $afile\n";
204 -d $adir or `mkdir -p $adir`;
205 `cp $dd/$pkg.tar.gz $afile`; die if $?;
212 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
213 my $df = $s->{"DISTDIR"} . "/$f";
214 print "Generating $df\n";
215 `cp $sf $df`; die if $?;
216 push @{$s->{"distfiles"}}, $df;
219 sub ParseOptions($) {
222 "verbose!" => \$verbose,
223 "test!" => \$s->{"do_test"},
224 "patch!" => \$s->{"do_patch"},
225 "diff-against=s" => \$s->{"diff_against"},
226 "upload!" => \$s->{"do_upload"}
227 ) || die "Syntax: release [--verbose] [--test] [--nopatch] [--diff-against=<version>] [--noupload]";
232 my $dd = $s->{"DISTDIR"};
233 my $pkg = $s->{"PKG"};
234 my $log = "$dd/$pkg.log";
235 print "Doing a test compilation\n";
236 `( cd $dd/$pkg && make ) >$log 2>&1`;
237 die "There were errors. Please inspect $log" if $?;
238 `grep -q [Ww]arning $log`;
239 $? or print "There were warnings! Please inspect $log.\n";
240 print "Cleaning up\n";
241 `cd $dd/$pkg && make distclean`; die if $?;
246 my $dd = $s->{"DISTDIR"};
247 my $pkg1 = $s->{"PKG"};
249 if ($s->{"diff_against"} ne "") {
250 $oldver = $s->{"diff_against"};
251 } elsif (defined $s->{"OLDVERSION"}) {
252 $oldver = $s->{"OLDVERSION"};
254 print "WARNING: No previous version known. No patch generated.\n";
257 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
259 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
260 -f $oldarch or die "MakePatch: $oldarch not found";
261 print "Unpacking $pkg0 from $oldarch\n";
262 `cd $dd && tar xzf $oldarch`; die if $?;
264 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
265 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
266 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
267 push @{$s->{"distfiles"}}, "$dd/$diff";
272 foreach my $u (@{$s->{"uploads"}}) {
273 my $url = $u->{"url"};
274 print "Upload to $url :\n";
276 my $filter = $u->{"filter"} || ".*";
277 foreach my $f (@{$s->{"distfiles"}}) {
283 print "<confirm> "; <STDIN>;
284 if ($url =~ m@^scp://([^/]+)(.*)@) {
290 my $cmd = "scp @files $host:$dir\n";
292 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
295 open FTP, "|ftp -v $host" or die;
296 print FTP "cd $dir\n";
297 foreach my $f (@files) {
298 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
299 print FTP "put $f $ff\n";
305 die "Don't know how to handle this URL scheme";
312 $s->Test if $s->{"do_test"};
313 $s->MakePatch if $s->{"do_patch"};
314 $s->Upload if $s->{"do_upload"};