2 # A simple system for making software releases
3 # (c) 2003 Martin Mares <mj@ucw.cz>
13 my ($class,$basename) = @_;
15 "PACKAGE" => $basename,
17 # p=preprocess, s=subst, -=discard
18 '(^|/)(CVS|\.arch-ids|{arch}|tmp)/' => '-',
19 '\.(lsm|spec)$' => 'ps',
24 "DATE" => `date '+%Y-%m-%d' | tr -d '\n'`,
25 "LSMDATE" => `date '+%y%m%d' | tr -d '\n'`,
28 "archivedir" => "/home/mj/tmp/archives/$basename",
41 sub GetVersionFromFile($) {
42 my ($s,$file,$rx) = @_;
43 open F, $file or die "Unable to open $file for version autodetection";
48 print "Detected version $1 from $file\n" if $verbose;
53 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
54 return $s->{"VERSION"};
57 sub GetVersionsFromChangelog($) {
58 my ($s,$file,$rx) = @_;
59 open F, $file or die "Unable to open $file for version autodetection";
63 if (!defined $s->{"VERSION"}) {
65 print "Detected version $1 from $file\n" if $verbose;
66 } elsif ($s->{"VERSION"} eq $1) {
69 $s->{"OLDVERSION"} = $1;
70 print "Detected previous version $1 from $file\n" if $verbose;
76 if (!defined $s->{"VERSION"}) { die "Failed to auto-detect version"; }
77 return $s->{"VERSION"};
82 $s->{"DISTDIR"} = $dd;
83 print "Initializing dist directory $dd\n" if $verbose;
84 `rm -rf $dd`; die if $?;
85 `mkdir -p $dd`; die if $?;
90 if (defined $s->{$v}) {
93 die "Reference to unknown variable $v";
98 my ($s,$f,$dir,$action) = @_;
100 (my $d = $f) =~ s@(^|/)[^/]*$@@;
102 -d $d || `mkdir -p $d`; die if $?;
104 my $preprocess = ($action =~ /p/);
105 my $subst = ($action =~ /s/);
106 if ($preprocess || $subst) {
107 open I, "$f" or die "open($f): $?";
108 open O, ">$dir/$f" or die "open($dir/$f): $!";
109 my @ifs = (); # stack of conditions, 1=satisfied
110 my $empty = 0; # last line was empty
111 my $is_makefile = ($f =~ /(Makefile|.mk)$/);
114 s/@([0-9A-Za-z_]+)@/$s->ExpandVar($1)/ge;
117 if (/^#/ || $is_makefile) {
118 if (/^#?ifdef\s+(\w+)/) {
119 if (defined ${$s->{"conditions"}}{$1}) {
120 push @ifs, ${$s->{"conditions"}}{$1};
124 } elsif (/^#ifndef\s+(\w+)/) {
125 if (defined ${$s->{"conditions"}}{$1}) {
126 push @ifs, -${$s->{"conditions"}}{$1};
130 } elsif (/^#if\s+/) {
132 } elsif (/^#?endif/) {
134 defined $x or die "Improper nesting of conditionals";
136 } elsif (/^#?else/) {
138 defined $x or die "Improper nesting of conditionals";
143 @ifs && $ifs[$#ifs] < 0 && next;
147 } else { $empty = 0; }
153 ! -x $f or chmod(0755, "$dir/$f") or die "chmod($dir/$f): $!";
155 `cp -a $f $dir/$f`; die if $?;
161 $s->{"PKG"} = $s->{"PACKAGE"} . "-" . $s->{"VERSION"};
162 my $dd = $s->{"DISTDIR"};
163 my $pkg = $s->{"PKG"};
164 my $dir = "$dd/$pkg";
165 print "Generating $dir\n";
167 FILES: foreach my $f (`find . -type f`) {
171 my @rules = @{$s->{"rules"}};
173 my $rule = shift @rules;
174 my $act = shift @rules;
180 ($action =~ /-/) && next FILES;
181 print "$f ($action)\n" if $verbose;
182 $s->CopyFile($f, $dir, $action);
185 if (-f "$dir/Makefile") {
186 print "Cleaning up\n";
187 `cd $dir && make distclean >&2`; die if $?;
190 print "Creating $dd/$pkg.tar.gz\n";
191 my $tarvv = $verbose ? "vv" : "";
192 `cd $dd && tar cz${tarvv}f $pkg.tar.gz $pkg >&2`; die if $?;
193 push @{$s->{"distfiles"}}, "$dd/$pkg.tar.gz";
195 my $adir = $s->{"archivedir"};
196 my $afile = "$adir/$pkg.tar.gz";
197 print "Archiving to $afile\n";
198 -d $adir or `mkdir -p $adir`;
199 `cp $dd/$pkg.tar.gz $afile`; die if $?;
206 my $sf = $s->{"DISTDIR"} . "/" . $s->{"PKG"} . "/$f";
207 my $df = $s->{"DISTDIR"} . "/$f";
208 print "Generating $df\n";
209 `cp $sf $df`; die if $?;
210 push @{$s->{"distfiles"}}, $df;
213 sub ParseOptions($) {
216 "verbose!" => \$verbose,
217 "test!" => \$s->{"do_test"},
218 "patch!" => \$s->{"do_patch"},
219 "diff-against=s" => \$s->{"diff_against"},
220 "upload!" => \$s->{"do_upload"}
221 ) || die "Syntax: release [--verbose] [--test] [--nopatch] [--diff-against=<version>] [--noupload]";
226 my $dd = $s->{"DISTDIR"};
227 my $pkg = $s->{"PKG"};
228 my $log = "$dd/$pkg.log";
229 print "Doing a test compilation\n";
230 `( cd $dd/$pkg && make ) >$log 2>&1`;
231 die "There were errors. Please inspect $log" if $?;
232 `grep -q [Ww]arning $log`;
233 $? or print "There were warnings! Please inspect $log.\n";
234 print "Cleaning up\n";
235 `cd $dd/$pkg && make distclean`; die if $?;
240 my $dd = $s->{"DISTDIR"};
241 my $pkg1 = $s->{"PKG"};
243 if ($s->{"diff_against"} ne "") {
244 $oldver = $s->{"diff_against"};
245 } elsif (defined $s->{"OLDVERSION"}) {
246 $oldver = $s->{"OLDVERSION"};
248 die "MakePatch: Don't know which is the previous version";
250 my $pkg0 = $s->{"PACKAGE"} . "-" . $oldver;
252 my $oldarch = $s->{"archivedir"} . "/" . $pkg0 . ".tar.gz";
253 -f $oldarch or die "MakePatch: $oldarch not found";
254 print "Unpacking $pkg0 from $oldarch\n";
255 `cd $dd && tar xzf $oldarch`; die if $?;
257 my $diff = $s->{"PACKAGE"} . "-" . $oldver . "-" . $s->{"VERSION"} . ".diff.gz";
258 print "Creating a patch from $pkg0 to $pkg1: $diff\n";
259 `cd $dd && diff -ruN $pkg0 $pkg1 | gzip >$diff`; die if $?;
260 push @{$s->{"distfiles"}}, "$dd/$diff";
265 foreach my $u (@{$s->{"uploads"}}) {
266 my $url = $u->{"url"};
267 print "Upload to $url :\n";
269 my $filter = $u->{"filter"} || ".*";
270 foreach my $f (@{$s->{"distfiles"}}) {
276 print "<confirm> "; <STDIN>;
277 if ($url =~ m@^scp://([^/]+)(.*)@) {
283 my $cmd = "scp @files $host:$dir\n";
285 } elsif ($url =~ m@ftp://([^/]+)(.*)@) {
288 open FTP, "|ftp -v $host" or die;
289 print FTP "cd $dir\n";
290 foreach my $f (@files) {
291 (my $ff = $f) =~ s@.*\/([^/].*)@$1@;
292 print FTP "put $f $ff\n";
298 die "Don't know how to handle this URL scheme";
305 $s->Test if $s->{"do_test"};
306 $s->MakePatch if $s->{"do_patch"};
307 $s->Upload if $s->{"do_upload"};