5 use File::Temp qw/tempfile tempdir/;
7 # See: http://www.rpm.org/max-rpm/s1-rpm-file-format-rpm-file-format.html#S3-RPM-FILE-FORMAT-HEADER-TAG-LISTING
8 # cf http://search.cpan.org/~davecross/Parse-RPM-Spec-0.01/lib/Parse/RPM/Spec.pm
16 "DESCRIPTION" => 1005,
19 "INSTALLTIME" => 1008,
21 "DISTRIBUTION" => 1010,
38 "OLDFILENAMES" => 1027,
45 "FILELINKTOS" => 1036,
47 "FILEUSERNAME" => 1039,
48 "FILEGROUPNAME" => 1040,
51 "FILEVERIFYFLAGS" => 1045,
52 "ARCHIVESIZE" => 1046,
53 "PROVIDENAME" => 1047,
55 "REQUIREFLAGS" => 1048,
56 "REQUIRENAME" => 1049,
57 "REQUIREVERSION" => 1050,
60 "CONFLICTFLAGS" => 1053,
61 "CONFLICTNAME" => 1054,
62 "CONFLICTVERSION" => 1055,
63 "EXCLUDEARCH" => 1059,
65 "EXCLUSIVEARCH" => 1061,
66 "EXCLUSIVEOS" => 1062,
68 "TRIGGERSCRIPTS" => 1065,
69 "TRIGGERNAME" => 1066,
70 "TRIGGERVERSION" => 1067,
71 "TRIGGERFLAGS" => 1068,
72 "TRIGGERINDEX" => 1069,
73 "VERIFYSCRIPT" => 1079,
74 "CHANGELOGTIME" => 1080,
75 "CHANGELOGNAME" => 1081,
76 "CHANGELOGTEXT" => 1082,
82 "OBSOLETENAME" => 1090,
84 "VERIFYSCRIPTPROG" => 1091,
85 "TRIGGERSCRIPTPROG" => 1092,
87 "FILEDEVICES" => 1095,
91 "INSTPREFIXES" => 1099,
92 "SOURCEPACKAGE" => 1106,
93 "PROVIDEFLAGS" => 1112,
94 "PROVIDEVERSION" => 1113,
95 "OBSOLETEFLAGS" => 1114,
96 "OBSOLETEVERSION" => 1115,
102 "PAYLOADFORMAT" => 1124,
103 "PAYLOADCOMPRESSOR" => 1125,
104 "PAYLOADFLAGS" => 1126,
105 "INSTALLCOLOR" => 1127,
106 "INSTALLTID" => 1128,
108 "RHNPLATFORM" => 1131,
110 "PATCHESNAME" => 1133,
111 "PATCHESFLAGS" => 1134,
112 "PATCHESVERSION" => 1135,
113 "CACHECTIME" => 1136,
114 "CACHEPKGPATH" => 1137,
115 "CACHEPKGSIZE" => 1138,
116 "CACHEPKGMTIME" => 1139,
117 "FILECOLORS" => 1140,
120 "FILEDEPENDSX" => 1143,
121 "FILEDEPENDSN" => 1144,
122 "DEPENDSDICT" => 1145,
123 "SOURCEPKGID" => 1146,
126 "PRETRANSPROG" => 1153,
127 "POSTTRANSPROG" => 1154,
129 "SUGGESTSNAME" => 1156,
130 "SUGGESTSVERSION" => 1157,
131 "SUGGESTSFLAGS" => 1158,
132 "ENHANCESNAME" => 1159,
133 "ENHANCESVERSION" => 1160,
134 "ENHANCESFLAGS" => 1161,
139 # do not mix numeric tags with symbolic tags.
140 # special symbolic tag 'FILENAME' exists.
142 # This function seems to take a set of tags and populates a global
143 # hash-table (%res) with data obtained by doing a binary unpack() on
145 # http://www.rpm.org/max-rpm/s1-rpm-file-format-rpm-file-format.html
151 my $need_filenames = grep { $_ eq 'FILENAMES' } @stags;
152 push @stags, 'BASENAMES', 'DIRNAMES', 'DIRINDEXES', 'OLDFILENAMES' if $need_filenames;
153 @stags = grep { $_ ne 'FILENAMES' } @stags if $need_filenames;
154 my %stags = map {0+($STAG{$_} or $_) => $_} @stags;
156 my ($magic, $sigtype, $headmagic, $cnt, $cntdata, $lead, $head, $index, $data, $tag, $type, $offset, $count);
159 if (ref($rpm) eq 'ARRAY') {
160 ($headmagic, $cnt, $cntdata) = unpack('N@8NN', $rpm->[0]);
161 if ($headmagic != 0x8eade801) {
165 if (length($rpm->[0]) < 16 + $cnt * 16 + $cntdata) {
169 $index = substr($rpm->[0], 16, $cnt * 16);
170 $data = substr($rpm->[0], 16 + $cnt * 16, $cntdata);
172 return () unless open(RPM, "<$rpm");
173 if (read(RPM, $lead, 96) != 96) {
174 warn("Bad rpm $rpm\n");
178 ($magic, $sigtype) = unpack('N@78n', $lead);
179 if ($magic != 0xedabeedb || $sigtype != 5) {
180 warn("Bad rpm $rpm\n");
184 if (read(RPM, $head, 16) != 16) {
185 warn("Bad rpm $rpm\n");
189 ($headmagic, $cnt, $cntdata) = unpack('N@8NN', $head);
190 if ($headmagic != 0x8eade801) {
191 warn("Bad rpm $rpm\n");
195 if (read(RPM, $index, $cnt * 16) != $cnt * 16) {
196 warn("Bad rpm $rpm\n");
200 $cntdata = ($cntdata + 7) & ~7;
201 if (read(RPM, $data, $cntdata) != $cntdata) {
202 warn("Bad rpm $rpm\n");
210 if (ref($rpm) eq 'ARRAY' && @stags && @$rpm > 1) {
211 my %res2 = &rpmq_many([ $rpm->[1] ], @stags);
212 %res = (%res, %res2);
216 if (ref($rpm) ne 'ARRAY' && @stags) {
217 if (read(RPM, $head, 16) != 16) {
218 warn("Bad rpm $rpm\n");
222 ($headmagic, $cnt, $cntdata) = unpack('N@8NN', $head);
223 if ($headmagic != 0x8eade801) {
224 warn("Bad rpm $rpm\n");
228 if (read(RPM, $index, $cnt * 16) != $cnt * 16) {
229 warn("Bad rpm $rpm\n");
233 if (read(RPM, $data, $cntdata) != $cntdata) {
234 warn("Bad rpm $rpm\n");
239 close RPM if ref($rpm) ne 'ARRAY';
241 return %res unless @stags; # nothing to do
244 ($tag, $type, $offset, $count, $index) = unpack('N4a*', $index);
248 my $otag = $stags{$tag};
250 $res{$otag} = [ '' ];
251 } elsif ($type == 1) {
252 $res{$otag} = [ unpack("\@${offset}c$count", $data) ];
253 } elsif ($type == 2) {
254 $res{$otag} = [ unpack("\@${offset}c$count", $data) ];
255 } elsif ($type == 3) {
256 $res{$otag} = [ unpack("\@${offset}n$count", $data) ];
257 } elsif ($type == 4) {
258 $res{$otag} = [ unpack("\@${offset}N$count", $data) ];
259 } elsif ($type == 5) {
260 $res{$otag} = [ undef ];
261 } elsif ($type == 6) {
262 $res{$otag} = [ unpack("\@${offset}Z*", $data) ];
263 } elsif ($type == 7) {
264 $res{$otag} = [ unpack("\@${offset}a$count", $data) ];
265 } elsif ($type == 8 || $type == 9) {
266 my $d = unpack("\@${offset}a*", $data);
267 my @res = split("\0", $d, $count + 1);
268 $res{$otag} = [ splice @res, 0, $count ];
270 $res{$otag} = [ undef ];
274 warn("Bad rpm $rpm: $@\n");
280 if ($need_filenames) {
281 if ($res{'OLDFILENAMES'}) {
282 $res{'FILENAMES'} = [ @{$res{'OLDFILENAMES'}} ];
285 $res{'FILENAMES'} = [ map {"$res{'DIRNAMES'}->[$res{'DIRINDEXES'}->[$i++]]$_"} @{$res{'BASENAMES'}} ];
291 sub rpmq_add_flagsvers {
298 my @flags = @{$res->{$flags} || []};
299 my @vers = @{$res->{$vers} || []};
300 for (@{$res->{$name}}) {
301 if (@flags && ($flags[0] & 0xe) && @vers) {
303 $_ .= '<' if $flags[0] & 2;
304 $_ .= '>' if $flags[0] & 4;
305 $_ .= '=' if $flags[0] & 8;
314 Name Version Release Epoch Summary Copyright License Distribution
315 Disturl Vendor Group Packager Url Icon Prefixes
353 # Used for each package by
367 ($targetarch, $targettype) = split(':', $target, 2);
385 for (split("\n", $config)) {
388 next if $_ eq '' || $_ =~ /^#/;
390 s/\<targettype\>/$targettype/g;
391 s/\<targetarch\>/$targetarch/g;
392 s/\<name\>/$pkgname/g;
393 s/\<version\>/$pkgver/g;
394 s/\<prefix\>/$prefix/g;
395 s/\<extension\>/$extension/g;
396 s/\<configdir\>/$configdir/g;
397 s/\<match1\>/$match1/g;
400 next unless s/^arch\s+\Q$arch\E\s+//;
402 next if /^targets\s+/;
403 if (/\s+package\s+[-+_a-zA-Z0-9]+$/) {
404 $pkgmatches = 0; # XXX: hack
406 if (/\s+package\s+\/[-+_a-zA-Z0-9]+\/$/) {
407 $pkgmatches = 0; # XXX: hack
409 if (/^targettype\s+/) {
410 next unless s/^targettype\s+\Q$targettype\E\s+//;
412 if (/^targetarch\s+/) {
413 next unless s/^targetarch\s+\Q$targetarch\E\s+//;
415 if (/^prefix\s+(.*?)$/) { $prefix = $1; next; }
416 if (/^legacyversion\s+(.*?)$/) { $legacyversion = $1; next; }
417 if (/^extension\s+(.*?)$/) { $extension = $1; next; }
418 if (/^configdir\s+(.*?)$/) { $configdir= $1; next; }
419 if (/^targetname\s+(.*?)$/) { $targetname = $1; next; }
421 $_ = "baselib $_" if /^[\+\-\"]/;
422 $_ = "package $_" if /^[-+_a-zA-Z0-9]+$/;
423 if (/^package\s+\/(.*?)\/$/) {
425 $pkgmatches = $pkgname =~ /$pm/;
426 $match1 = $1 if defined $1;
427 $pkghasmatched |= $pkgmatches if $pkgname =~ /-debuginfo$/ && $target_matched{$target};
430 if (/^package\s+(.*?)$/) {
431 $pkgmatches = $1 eq $pkgname;
432 $pkghasmatched |= $pkgmatches;
435 next unless $pkgmatches;
436 return 0 if $_ eq 'block!';
437 if (/^provides\s+(.*?)$/) { push @provides, $1; next; }
438 if (/^requires\s+(.*?)$/) { push @requires, $1; next; }
439 if (/^recommends\s+(.*?)$/) { push @recommends, $1; next; }
440 if (/^supplements\s+(.*?)$/) { push @supplements, $1; next; }
441 if (/^suggests\s+(.*?)$/) { push @suggests, $1; next; }
442 if (/^prereq\s+(.*?)$/) { push @prerequires, $1; next; }
443 if (/^obsoletes\s+(.*?)$/) { push @obsoletes, $1; next; }
444 if (/^conflicts\s+(.*?)$/) { push @conflicts, $1; next; }
445 if (/^baselib\s+(.*?)$/) { push @baselib, $1; next; }
446 if (/^config\s+(.*?)$/) { push @config, $1; next; }
447 if (/^pre(in)?\s+(.*?)$/) { push @prein, $2; next; }
448 if (/^post(in)?\s+(.*?)$/) { push @postin, $2; next; }
449 if (/^preun\s+(.*?)$/) { push @preun, $1; next; }
450 if (/^postun\s+(.*?)$/) { push @preun, $1; next; }
451 if (/^autoreqprov\s+(.*?)$/) {$autoreqprov = $1; next; }
452 die("bad line: $_\n");
454 return $pkghasmatched;
460 open(F, "<$cfname") || die("$cfname: $!\n");
463 $config .= join('', @cf);
464 $config .= "\npackage __does_not_match__\n";
468 my $architecture = shift;
471 for (split("\n", $conf)) {
473 next unless s/^arch\s+\Q$architecture\E\s+//;
475 if (/^targets\s+(.*?)$/) {
476 $targets{$_} = 1 for split(' ', $1);
479 my @targets = sort keys %targets;
483 # Packages listed in config file
486 for (split("\n", $config)) {
487 if (/^(.*\s+)?package\s+([-+_a-zA-Z0-9]+)\s*$/) { # eg : arch ppc package libnuma-devel
489 } elsif (/^\s*([-+_a-zA-Z0-9]+)\s*$/) { # eg: readline-devel
493 return sort keys %rpms;
496 # Packages listed in config file - debian variant (can have "." in package names)
497 sub get_debpkgnames {
499 for (split("\n", $config)) {
500 if (/^(.*\s+)?package\s+([-+_a-zA-Z0-9.]+)\s*$/) { # eg : arch ppc package libnuma-devel
502 } elsif (/^\s*([-+_a-zA-Z0-9.]+)\s*$/) { # eg: readline-devel
506 return sort keys %debs;
512 my @stags = map {uc($_)} @preamble;
513 push @stags, 'DESCRIPTION';
514 push @stags, 'FILENAMES', 'FILEMODES', 'FILEUSERNAME', 'FILEGROUPNAME', 'FILEFLAGS', 'FILEVERIFYFLAGS';
515 push @stags, 'CHANGELOGTIME', 'CHANGELOGNAME', 'CHANGELOGTEXT';
516 push @stags, 'ARCH', 'SOURCERPM', 'RPMVERSION';
517 push @stags, 'BUILDTIME';
518 my %res = rpmq_many($rpm, @stags);
519 die("$rpm: bad rpm\n") unless $res{'NAME'};
521 my $rname = $res{'NAME'}->[0];
522 my $sname = $res{'SOURCERPM'}->[0];
523 die("$rpm is a sourcerpm\n") unless $sname;
524 die("bad sourcerpm: $sname\n") unless $sname =~ /^(.*)-([^-]+)-([^-]+)\.(no)?src\.rpm$/;
529 $arch = $res{'ARCH'}->[0];
530 my @targets = get_targets($arch, $config);
532 print "no targets for arch $arch, nothing to do\n";
535 for my $target (@targets) {
537 next unless parse_config($target, $res{'NAME'}->[0], $res{'VERSION'}->[0]);
538 die("targetname not set\n") unless $targetname;
539 $target_matched{$target} = 1;
542 my @rpmfiles = @{$res{'FILENAMES'}};
543 my @ff = @{$res{'FILEFLAGS'}};
545 $ghosts{$_} = 1 if $ff[0] & (1 << 6);
552 for my $r (@baselib) {
553 my $rr = substr($r, 1);
554 if (substr($r, 0, 1) eq '+') {
555 if ($rr =~ /^(.*?)\s*->\s*(.*?)$/) {
556 if (grep {$_ eq $1} @rpmfiles) {
561 for (grep {/$rr/} @rpmfiles) {
566 } elsif (substr($r, 0, 1) eq '-') {
567 delete $files{$_} for grep {/$rr/} keys %files;
568 } elsif (substr($r, 0, 1) eq '"') {
570 if ($rr =~ /^(.*?)\s*->\s*(.*?)$/) {
573 die("bad baselib string rule: $r\n");
576 die("bad baselib rule: $r\n");
580 for my $r (@config) {
581 my $rr = substr($r, 1);
582 if (substr($r, 0, 1) eq '+') {
583 $cfiles{$_} = 1 for grep {/$rr/} grep {!$ghosts{$_}} @rpmfiles;
584 } elsif (substr($r, 0, 1) eq '-') {
585 delete $cfiles{$_} for grep {/$rr/} keys %cfiles;
587 die("bad config rule: $r\n");
591 $files{$_} = 1 for keys %cfiles;
594 print "$rname($target): empty filelist, skipping rpm\n";
599 for (@{$res{'FILENAMES'}}) {
600 $files{$_} = $i if $files{$_};
606 next if $cfiles{$_} || $moves{$_};
608 next unless $fn =~ s/\/[^\/]+$//;
618 next unless $fn =~ s/\/[^\/]+$//;
621 next unless $fn =~ s/\/[^\/]+$//;
622 $alldirs{"$prefix$fn"} = 1;
625 $alldirs{$_} = 1 for keys %symlinks;
626 $alldirs{$configdir} = 1 if %cfiles;
628 for $ad (keys %alldirs) {
629 $alldirs{$ad} = 1 while $ad =~ s/\/[^\/]+$//;
635 delete $alldirs{$moves{$fn}};
637 delete $alldirs{"$prefix$fn"};
641 delete $alldirs{$ad};
642 delete $alldirs{$ad} while $ad =~ s/\/[^\/]+$//;
643 delete $alldirs{$_} for @filesystem;
645 print "$rname($target): writing specfile...\n";
646 my ($fh, $specfile) = tempfile(SUFFIX => ".spec");
647 open(SPEC, ">&=", $fh) || die("open: $!\n");
648 for my $p (@preamble) {
650 next unless $res{$pt};
651 my $d = $res{$pt}->[0];
654 print SPEC "Name: $sname\n";
657 if ($p eq 'Version') {
658 print SPEC "Version: $sversion\n";
661 if ($p eq 'Release') {
662 print SPEC "Release: $srelease\n";
665 if ($p eq 'Disturl') {
666 print SPEC "%define disturl $d\n";
669 print SPEC "$p: $d\n";
671 print SPEC "Source: $rpm\n";
672 print SPEC "NoSource: 0\n" if $res{'SOURCERPM'}->[0] =~ /\.nosrc\.rpm$/;
673 print SPEC "BuildRoot: %{_tmppath}/baselibs-%{name}-%{version}-build\n";
674 print SPEC "%define _target_cpu $targetarch\n";
675 print SPEC "%define __os_install_post %{nil}\n";
676 print SPEC "%description\nUnneeded main package. Ignore.\n\n";
677 print SPEC "%package -n $targetname\n";
678 for my $p (@preamble) {
679 next if $p eq 'Name' || $p eq 'Disturl';
681 next unless $res{$pt};
682 my $d = $res{$pt}->[0];
684 if ($pt eq 'VERSION' && $legacyversion) {
686 } elsif ($pt eq 'RELEASE' && $legacyversion) {
687 my @bt = localtime($res{'BUILDTIME'}->[0]);
690 $d = sprintf("%04d%02d%02d%02d%02d\n", @bt[5,4,3,2,1]);
692 print SPEC "$p: $d\n";
694 print SPEC "Autoreqprov: $autoreqprov\n";
696 for my $ar ([\@provides, 'provides'],
697 [\@prerequires, 'prereq'],
698 [\@requires, 'requires'],
699 [\@recommends, 'recommends'],
700 [\@supplements, 'supplements'],
701 [\@obsoletes, 'obsoletes'],
702 [\@conflicts, 'conflicts']) {
706 if (substr($_, 0, 1) eq '"') {
707 die("bad $ar->[1] rule: $_\n") unless /^\"(.*)\"$/;
709 } elsif (substr($_, 0, 1) eq '-') {
710 my $ra = substr($_, 1);
711 @na = grep {!/$ra/} @na;
713 die("bad $ar->[1] rule: $_\n");
716 print SPEC ucfirst($ar->[1]).": $_\n" for @na;
719 $cpiopre = './' if $res{'RPMVERSION'}->[0] !~ /^3/;
720 my $d = $res{'DESCRIPTION'}->[0];
722 if ($legacyversion) {
723 $d = "This rpm was re-packaged from $res{'NAME'}->[0]-$res{'VERSION'}->[0]-$res{'RELEASE'}->[0]\n\n$d";
725 print SPEC "\n%description -n $targetname\n";
727 print SPEC "%prep\n";
728 print SPEC "%build\n";
729 print SPEC "%install\n";
730 print SPEC "rm -rf \$RPM_BUILD_ROOT\n";
731 print SPEC "mkdir \$RPM_BUILD_ROOT\n";
732 print SPEC "cd \$RPM_BUILD_ROOT\n";
733 my @cfl = grep {!$cfiles{$_} && !$moves{$_}} sort keys %files;
736 print SPEC "mkdir -p \$RPM_BUILD_ROOT$prefix\n";
737 print SPEC "pushd \$RPM_BUILD_ROOT$prefix\n";
739 print SPEC "cat <<EOFL >.filelist\n";
740 print SPEC "$_\n" for map {$cpiopre.substr($_, 1)} @cfl;
742 print SPEC "mkdir -p \$RPM_BUILD_ROOT$prefix$_\n" for sort keys %cpiodirs;
743 print SPEC "rpm2cpio $rpm | cpio -i -d -v -E .filelist\n";
744 print SPEC "rm .filelist\n";
746 for my $fn (grep {$ghosts{$_}} @cfl) {
748 $fnm = '.' unless $fnm =~ s/\/[^\/]+$//;
749 print SPEC "mkdir -p \$RPM_BUILD_ROOT$prefix$fnm\n";
750 print SPEC "touch \$RPM_BUILD_ROOT$prefix$fn\n";
757 if (%cfiles || %moves) {
758 print SPEC "mkdir -p .cfiles\n";
759 print SPEC "pushd .cfiles\n";
760 print SPEC "cat <<EOFL >.filelist\n";
761 print SPEC "$_\n" for map {$cpiopre.substr($_, 1)} grep {$cfiles{$_} || $moves{$_}} sort keys %files;
763 print SPEC "rpm2cpio $rpm | cpio -i -d -v -E .filelist\n";
766 print SPEC "mkdir -p \$RPM_BUILD_ROOT$configdir\n";
767 print SPEC "mv .cfiles$_ \$RPM_BUILD_ROOT$configdir\n" for sort keys %cfiles;
769 for my $fn (sort keys %moves) {
770 my $fnm = $moves{$fn};
771 $fnm = '.' unless $fnm =~ s/\/[^\/]+$//;
772 print SPEC "mkdir -p \$RPM_BUILD_ROOT$fnm\n";
773 print SPEC "mv .cfiles$fn \$RPM_BUILD_ROOT$moves{$fn}\n";
775 print SPEC "rm -rf .cfiles\n";
777 for my $fn (sort keys %symlinks) {
779 $fnm = '.' unless $fnm =~ s/\/[^\/]+$//;
780 print SPEC "mkdir -p \$RPM_BUILD_ROOT$fnm\n";
781 print SPEC "ln -s $symlinks{$fn} \$RPM_BUILD_ROOT$fn\n";
783 if ($prefix ne '' && grep {/\.so.*$/} @cfl) {
784 @postin = () if @postin == 1 && $postin[0] =~ /^\"-p.*ldconfig/;
785 unshift @postin, "\"/sbin/ldconfig -r $prefix\"";
789 print SPEC "%pre -n $targetname";
790 print SPEC $prein[0] =~ /^\"-p/ ? " " : "\n";
792 die("bad prein rule: $_\n") unless /^\"(.*)\"$/;
797 print SPEC "%post -n $targetname";
798 print SPEC $postin[0] =~ /^\"-p/ ? " " : "\n";
800 die("bad postin rule: $_\n") unless /^\"(.*)\"$/;
805 print SPEC "%preun -n $targetname";
806 print SPEC $preun[0] =~ /^\"-p/ ? " " : "\n";
808 die("bad preun rule: $_\n") unless /^\"(.*)\"$/;
813 print SPEC "%postun -n $targetname";
814 print SPEC $postun[0] =~ /^\"-p/ ? " " : "\n";
816 die("bad postun rule: $_\n") unless /^\"(.*)\"$/;
821 print SPEC "\n%clean\n";
822 print SPEC "\nrm -rf \$RPM_BUILD_ROOT\n\n";
823 print SPEC "%files -n $targetname\n";
824 for my $file (sort keys %alldirs) {
825 print SPEC "%dir %attr(0755,root,root) $file\n";
827 for my $file (keys %files) {
828 my $fi = $files{$file};
829 my $fm = $res{'FILEMODES'}->[$fi];
830 my $fv = $res{'FILEVERIFYFLAGS'}->[$fi];
831 my $ff = $res{'FILEFLAGS'}->[$fi];
832 if (POSIX::S_ISDIR($fm)) {
835 if ($ff & ((1 << 3) | (1 << 4))) {
836 print SPEC "%config(missingok noreplace) ";
837 } elsif ($ff & (1 << 3)) {
838 print SPEC "%config(missingok) ";
839 } elsif ($ff & (1 << 4)) {
840 print SPEC "%config(noreplace) ";
841 } elsif ($ff & (1 << 0)) {
842 print SPEC "%config ";
844 print SPEC "%doc " if $ff & (1 << 1);
845 print SPEC "%ghost " if $ff & (1 << 6);
846 print SPEC "%license " if $ff & (1 << 7);
847 print SPEC "%readme " if $ff & (1 << 8);
848 if ($fv != 4294967295) {
849 print SPEC "%verify(";
850 if ($fv & 2147483648) {
854 print SPEC "md5 " if $fv & (1 << 0);
855 print SPEC "size " if $fv & (1 << 1);
856 print SPEC "link " if $fv & (1 << 2);
857 print SPEC "user " if $fv & (1 << 3);
858 print SPEC "group " if $fv & (1 << 4);
859 print SPEC "mtime " if $fv & (1 << 5);
860 print SPEC "mode " if $fv & (1 << 6);
861 print SPEC "rdev " if $fv & (1 << 7);
864 #sigh, no POSIX::S_ISLNK ...
865 if (($fm & 0170000) == 0120000) {
866 printf SPEC "%%attr(-,%s,%s) ", $res{'FILEUSERNAME'}->[$fi], $res{'FILEGROUPNAME'}->[$fi];
868 printf SPEC "%%attr(%03o,%s,%s) ", $fm & 07777, $res{'FILEUSERNAME'}->[$fi], $res{'FILEGROUPNAME'}->[$fi];
870 if ($cfiles{$file}) {
873 print SPEC "$configdir/$fn\n";
876 print SPEC "$moves{$file}\n";
878 print SPEC "$prefix$file\n";
882 for (keys %symlinks) {
883 printf SPEC "%%attr(-,root,root) $_\n";
886 if ($res{'CHANGELOGTEXT'}) {
887 print SPEC "\n%changelog -n $targetname\n";
888 my @ct = @{$res{'CHANGELOGTIME'}};
889 my @cn = @{$res{'CHANGELOGNAME'}};
890 my @wdays = qw{Sun Mon Tue Wed Thu Fri Sat};
891 my @months = qw{Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec};
892 for my $cc (@{$res{'CHANGELOGTEXT'}}) {
893 my @lt = localtime($ct[0]);
898 printf SPEC "* %s %s %02d %04d %s\n%s\n", $wdays[$lt[6]], $months[$lt[4]], $lt[3], 1900 + $lt[5], $cn2, $cc2;
904 close(SPEC) || die("$specfile: $!\n");
905 print "$rname($target): running build...\n";
906 if (system("rpmbuild -bb $specfile".($verbose ? '' : '>/dev/null 2>&1'))) {
907 print "rpmbuild failed: $?\n";
908 print "re-running in verbose mode:\n";
909 system("rpmbuild -bb $specfile 2>&1");
917 ################################################################
922 require Parse::DebControl;
925 print "mkbaselibs needs the perl module Parse::DebControl\n".
926 "Error. baselibs-deb.conf specified but mkbaselibs can't run\n".
927 "Please ensure that 'osc meta prjconf' contains the following line:\n".
928 " Support: libparse-debcontrol-perl\n";
934 # look in the config file to see if we should be doing anything
936 # Unpack the deb control data using dpkg-deb
938 # Unpack the deb control data *and* file data using dpkg-deb
939 # process the config file for this package modifying control and moving files
940 # repackage the target deb
943 # http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-binarycontrolfiles
944 # unpack the outer loop control file - this gives us eg: the arch
945 my $base = tempdir() || die("tempdir: $!\n");
946 system "dpkg -e $deb ${base}/DEBIAN" || die "dpkg -e failed on $deb";
947 my $controlParser = new Parse::DebControl;
948 $controlParser->DEBUG();
949 my $keys = $controlParser->parse_file("${base}/DEBIAN/control");
950 # print Dumper($keys);
951 # DebControl supports multiple paragraphs of control data but
952 # debian/control in a .deb only has one (whereas a debian/control
953 # in a build root contains many)
954 # So extract the ref to the first one.
955 my %control = %{@{$keys}[0]};
957 # Validate this is a binary deb and get the control data
958 my $d_name = $control{'Package'};
959 my $d_version = $control{'Version'};
961 $arch = $control{'Architecture'}; # set global $arch
964 # arch <arch> targets <target_arch>[:<target_type>] [<target_arch>[:<target_type>]...]
965 # line and get a list of target_arch-es
966 my @targets = get_targets($arch, $config);
968 print "no targets for arch $arch, nothing to do\n";
969 return; # there may be more debs to handle
972 for my $target (@targets) {
973 next unless parse_config($target, $d_name, $d_version);
974 die("targetname not set\n") unless $targetname; # set in the global_conf
975 $target_matched{$target} = 1;
977 my $baseTarget = "${base}/$target";
978 # Unpack a .deb to work on. We have to do this each time as we
979 # manipulate the unpacked files.
980 system "mkdir ${base}/$target";
981 system "dpkg -e $deb ${baseTarget}/DEBIAN" || die "dpkg -e failed on $deb";
982 # Note that extracting to $prefix does the clever move to /lib-x86/ or whatever
983 system "dpkg -x $deb ${baseTarget}/$prefix" || die "dpkg -x failed on $deb";
985 # Reset the control data
986 $keys = $controlParser->parse_file("${baseTarget}/DEBIAN/control");
987 %control = %{@{$keys}[0]};
989 # Force the architecture
990 $control{'Architecture'} = $targetarch;
992 # Currently this script does not manipulate any files
993 # If needed they are all unpacked in ${baseTarget}
995 # we don't need a dsc/spec file.. all done by just moving files around
996 # and running dpkg -b ${base} $NEW_DEB
998 # my $dscfile = "/usr/src/packages/DSCS/mkbaselibs$$.dsc";
1000 print "$d_name($target): writing dscfile...\n";
1001 # We can Use Parse::DebControl write_file to create the new control file
1002 # just modify tags in there
1004 # We'll use requires -> Depends:
1005 map s/^"(.*)"$/$1/, @requires; # remove leading/trailing "s
1006 $control{"Depends"} = @requires ? join(", ", @requires) : ""; # join array if exists or reset it to ""
1008 map s/^"(.*)"$/$1/, @prerequires;
1009 $control{"Pre-Depends"} = @prerequires ? join(", ", @prerequires) : "";
1011 map s/^"(.*)"$/$1/, @provides;
1012 $control{"Provides"} = @provides ? join(", ", @provides) : "";
1014 map s/^"(.*)"$/$1/, @recommends;
1015 $control{"Recommends"} = @recommends ? join(", ", @recommends) : "";
1017 map s/^"(.*)"$/$1/, @suggests;
1018 $control{"Suggests"} = @suggests ? join(", ", @suggests) : "";
1020 map s/^"(.*)"$/$1/, @obsoletes;
1021 $control{"Replaces"} = @obsoletes ? join(", ", @obsoletes) : "";
1023 map s/^"(.*)"$/$1/, @conflicts;
1024 $control{"Conflicts"} = @conflicts ? join(", ", @conflicts) : "";
1026 map s/^"(.*)"$/$1/, @supplements;
1027 $control{"Enhances"} = @supplements ? join(", ", @supplements) : "";
1030 # Tidy up the various control files.
1031 # the md5sums are regenerated by dpkg-deb when building
1032 foreach my $c_file qw(conffiles postinst postrm preinst prerm) {
1033 unlink "${baseTarget}/DEBIAN/$c_file";
1035 # Create them if needed
1037 map s/^"(.*)"$/$1/, @prein; # remove leading/trailing "s
1038 open(my $SCRIPT, ">${baseTarget}/DEBIAN/preinst");
1039 print $SCRIPT join("\n", @prein) ;
1040 chmod(0755, $SCRIPT);
1044 map s/^"(.*)"$/$1/, @postin;
1045 open(my $SCRIPT, ">${baseTarget}/DEBIAN/postinst");
1046 print $SCRIPT join("\n", @postin) ;
1047 chmod(0755, $SCRIPT);
1051 map s/^"(.*)"$/$1/, @preun;
1052 open(my $SCRIPT, ">${baseTarget}/DEBIAN/prerm");
1053 print $SCRIPT join("\n", @preun) ;
1054 chmod(0755, $SCRIPT);
1058 map s/^"(.*)"$/$1/, @postun;
1059 open(my $SCRIPT, ">${baseTarget}/DEBIAN/postrm");
1060 print $SCRIPT join("\n", @postun) ;
1061 chmod(0755, $SCRIPT);
1065 # Don't forget to rename the package - or it will replace/uninstall the /-based one
1066 $control{"Package"} = "${d_name}-${targettype}";
1068 $controlParser->write_file("${baseTarget}/DEBIAN/control", \%control, {clobberFile => 1, addNewline=>1 } );
1069 system "dpkg -b ${baseTarget} /usr/src/packages/DEBS/${d_name}-${targettype}_${d_version}_${targetarch}.deb" || die "dpkg -b failed on $deb";
1070 system "rm -rf ${baseTarget}";
1072 system "rm -rf ${base}";
1076 # args is a list of full pathnames to rpm/deb files
1077 die("Usage: mkbaselibs <rpms>\n") unless @ARGV;
1079 if ($ARGV[0] eq '-v') {
1083 while ($ARGV[0] eq '-c') {
1085 read_config($ARGV[0]);
1089 my %goodpkgs = map {$_ => 1} get_pkgnames(); # These are packages named in the config file
1093 for my $rpm (@pkgs) {
1096 warn ("$rpm does not exist, skipping\n");
1099 next if $rpm =~ /\.(no)?src\.rpm$/; # ignore source rpms
1100 next if $rpm =~ /\.spm$/;
1101 $rpmn =~ s/.*\///; # Remove leading path info
1102 $rpmn =~ s/-[^-]+-[^-]+\.[^\.]+\.rpm$/\.rpm/; # remove all version info
1103 $rpmn =~ s/\.rpm$//; # remove extension
1104 push @rpms, $rpm if $goodpkgs{$rpmn};
1105 if ($rpmn =~ s/-debuginfo$//) {
1106 push @debugrpms, $rpm if $goodpkgs{$rpmn};
1110 die("$_: need absolute path to package\n") unless /^\//;
1113 my %debs_to_process = map {$_ => 1} get_debpkgnames(); # These are packages named in the config file
1115 for my $deb (@pkgs) {
1117 next unless $debn =~ /\.deb$/;
1118 $debn =~ s/.*\///; # Remove leading path info
1119 $debn =~ s/_[^_]+_[^_]+\.deb$//; # remove all version info and extension
1120 push @debs, $deb if $debs_to_process{$debn};
1121 print "ignoring $deb as $debn not in baselibs.conf\n" if !$debs_to_process{$debn};
1124 die("$_: need absolute path to package\n") unless /^\//;
1127 exit 0 unless @rpms or @debs;
1130 @filesystem = split("\n", `rpm -ql filesystem 2>/dev/null`);
1131 die("filesystem rpm is not installed\n") unless @filesystem;
1134 handle_rpms(@debugrpms);