openpkg-build.pl 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315
  1. ##
  2. ## openpkg-build.pl -- create build scripts from package index
  3. ##
  4. ## Copyright (c) 2000-2002 Cable & Wireless Deutschland GmbH
  5. ## Copyright (c) 2000-2002 The OpenPKG Project <http://www.openpkg.org/>
  6. ## Copyright (c) 2000-2002 Ralf S. Engelschall <rse@engelschall.com>
  7. ##
  8. ## Permission to use, copy, modify, and distribute this software for
  9. ## any purpose with or without fee is hereby granted, provided that
  10. ## the above copyright notice and this permission notice appear in all
  11. ## copies.
  12. ##
  13. ## THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  14. ## WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  15. ## MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  16. ## IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
  17. ## CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  18. ## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  19. ## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  20. ## USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. ## ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. ## OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  23. ## OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ## SUCH DAMAGE.
  25. ##
  26. require 5;
  27. $|=1; # autoflush
  28. use strict;
  29. use vars qw/$opt_R $opt_r $opt_f $opt_u $opt_U $opt_a $opt_A $opt_z $opt_Z $opt_P $opt_N $opt_E $opt_i $opt_D $opt_p $opt_q/;
  30. getopts('R:r:f:uUaAzZP:N:E:iD:p:q');
  31. ##########################################################################
  32. sub getopts ($) {
  33. my($opts) = @_;
  34. my(%optf) = map { /(\w)/; $1 => $_ } $opts =~ /(\w:|\w)/g;
  35. my(%opts,@argv,$optarg);
  36. foreach (@ARGV) {
  37. if (@argv) {
  38. push @argv, $_;
  39. } elsif (defined $optarg) {
  40. if (exists $opts{$optarg}) {
  41. $opts{$optarg} .= " $_";
  42. } else {
  43. $opts{$optarg} = $_;
  44. }
  45. $optarg = undef;
  46. } elsif (!/^[-]/) {
  47. push @argv, $_;
  48. } else {
  49. while (/^\-(\w)(.*)/) {
  50. if (exists $optf{$1}) {
  51. if (length($optf{$1}) > 1) {
  52. if ($2 ne '') {
  53. if (exists $opts{$1}) {
  54. $opts{$1} .= " $2";
  55. } else {
  56. $opts{$1} = $2;
  57. }
  58. } else {
  59. $optarg = $1;
  60. }
  61. last;
  62. } else {
  63. $opts{$1} = 1;
  64. }
  65. } else {
  66. warn "warning: unknown option $_\n";
  67. }
  68. $_ = "-$2";
  69. }
  70. }
  71. }
  72. if (defined $optarg) {
  73. warn "warning: option $optarg requires an argument\n";
  74. }
  75. foreach (keys %opts) {
  76. eval '$opt_'.$_.' = "'.quotemeta($opts{$_}).'";';
  77. }
  78. @ARGV = @argv;
  79. }
  80. my(%env) = ( '' => { } );
  81. if (open(FH, "< $ENV{'HOME'}/.openpkg/build")) {
  82. my($env) = $env{''};
  83. while (<FH>) {
  84. if (/^\s*\[([^\]]*)\]/) {
  85. $env{$1} = { } unless $env{$1};
  86. $env = $env{$1};
  87. } elsif (my($opt,$val) = /^\-([RfruUaAzPN])\s*(.*?)\s*$/) {
  88. $val = 1 unless defined $val;
  89. $env->{$opt} = $val;
  90. }
  91. }
  92. close(FH);
  93. }
  94. die "openpkg:build:USAGE: $0 [-R rpm] [-r repository] [-f index.rdf] [-uUzZiq] [-P priv-cmd] [-N non-priv-cmd] [-p platform] [-Dwith ...] [-Ename ...] ( [-aA] | patternlist )\n"
  95. unless $#ARGV >= 0 || ($#ARGV == -1 && ($opt_a || $opt_A));
  96. ##########################################################################
  97. sub conditional ($$) {
  98. my($cond,$with) = @_;
  99. my(@s,$res);
  100. return 1 if $cond eq '';
  101. foreach (split(/\s+/,$cond)) {
  102. if ($_ eq '+') {
  103. die "FATAL: stack underflow in: $cond\n" if scalar(@s)<2;
  104. my($a) = pop @s;
  105. my($b) = pop @s;
  106. push @s, $a && $b;
  107. } elsif ($_ eq '|') {
  108. die "FATAL: stack underflow in: $cond\n" if scalar(@s)<2;
  109. my($a) = pop @s;
  110. my($b) = pop @s;
  111. push @s, $a && $b;
  112. } elsif ($_ eq '!') {
  113. die "FATAL: stack underflow in: $cond\n" if scalar(@s)<1;
  114. my($a) = pop @s;
  115. push @s, !$a;
  116. } else {
  117. push @s, $with->{$_} eq 'yes';
  118. }
  119. }
  120. die "FATAL: stack underflow in: $cond\n" if scalar(@s)<1;
  121. $res = pop @s;
  122. die "FATAL: stack not empty in: $cond\n" if scalar(@s)>0;
  123. return $res;
  124. }
  125. ##########################################################################
  126. my($RPM,$RPM_PRIV,$RPM_NPRIV,$CURL,$PROG);
  127. $RPM = $opt_R || $env{''}->{opt}->{'R'} || '@l_prefix@/bin/rpm';
  128. $RPM = (`which $RPM` =~ m{^(/.*)})[0];
  129. die "FATAL: cannot locate rpm in path\n" unless $RPM =~ m{^/};
  130. # augment command line parameters
  131. foreach my $env (sort { $a cmp $b } grep { $RPM =~ /^\Q$_\E/ } keys %env) {
  132. while (my($opt,$val) = each %{$env{$env}}) {
  133. eval "\$opt_$opt = '$val' unless defined \$opt_$opt;";
  134. }
  135. }
  136. $RPM_PRIV = ($opt_P ? $opt_P." ".$RPM : $RPM);
  137. $RPM_NPRIV = ($opt_N ? $opt_N." ".$RPM : $RPM);
  138. $CURL = $RPM;
  139. $CURL =~ s/\/bin\/rpm$/\/lib\/openpkg\/curl/
  140. or die "FATAL: cannot deduce curl path from $RPM\n";
  141. ($PROG) = $0 =~ /(?:.*\/)?(.*)/;
  142. sub version_cmp ($$) {
  143. my($a,$b) = @_;
  144. my(@a,@b,$c);
  145. my($ax,$bx);
  146. @a = split(/\./, $a);
  147. @b = split(/\./, $b);
  148. while (@a && @b) {
  149. if ($a[0] =~ /^\d+$/ && $b[0] =~ /^\d+$/) {
  150. $c = $a[0] <=> $b[0];
  151. } elsif ((($a,$ax) = $a[0] =~ /^(\d+)(.*)$/) &&
  152. (($b,$bx) = $b[0] =~ /^(\d+)(.*)$/)) {
  153. $c = $a <=> $b;
  154. $c = $ax cmp $bx unless $c;
  155. } else {
  156. $c = $a[0] cmp $b[0];
  157. }
  158. return $c if $c;
  159. shift @a;
  160. shift @b;
  161. }
  162. $c = scalar(@a) <=> scalar(@b);
  163. return $c;
  164. }
  165. sub release_cmp ($$) {
  166. my($a,$b) = @_;
  167. return $a cmp $b;
  168. }
  169. sub vcmp ($$) {
  170. my($a,$b) = @_;
  171. my($av,$ar) = $a =~ /^(.*?)(?:\-([\d\.]+))?$/;
  172. my($bv,$br) = $b =~ /^(.*?)(?:\-([\d\.]+))?$/;
  173. my($c);
  174. if ((defined $ar) && (defined $br)) {
  175. $c = release_cmp($ar,$br);
  176. return $c if $c;
  177. }
  178. if ((defined $av) && (defined $bv)) {
  179. $c = version_cmp($av,$bv);
  180. return $c if $c;
  181. }
  182. return 0;
  183. }
  184. sub vs ($) {
  185. my($t) = @_;
  186. return "$t->{version}-$t->{release}";
  187. }
  188. sub vsn ($) {
  189. my($t) = @_;
  190. return "$t->{name}-$t->{version}-$t->{release}";
  191. }
  192. ##########################################################################
  193. sub get_config ()
  194. {
  195. my($c,@q);
  196. $c = `$RPM_NPRIV --eval '%{_rpmdir} %{_rpmfilename} %{_target_os} %{_target_cpu} %{_target_platform} %{_prefix}'`;
  197. chomp($c);
  198. (@q) = split(/\s+/,$c);
  199. $q[1] =~ s/%{OS}/$q[2]/;
  200. $q[1] =~ s/%{ARCH}/$q[3]/;
  201. return {
  202. rpmdir => $q[0],
  203. template => $q[1],
  204. platform => $q[4],
  205. prefix => $q[5]
  206. };
  207. }
  208. sub get_release () {
  209. my($rel,$url);
  210. ($rel) =`$RPM_NPRIV -qi openpkg` =~ /Version:\s*(\S+)/m;
  211. if ($rel =~ /^\d+$/) {
  212. print "# $PROG current($rel)\n";
  213. print "# using '$RPM_NPRIV' (build) and '$RPM_PRIV' (install)\n";
  214. $url = "ftp://ftp.openpkg.org/current/";
  215. } elsif ($rel =~ /^(\d+\.\d+)/) {
  216. $rel = $1;
  217. print "# $PROG release($rel)\n";
  218. $url = "ftp://ftp.openpkg.org/release/$rel/";
  219. } else {
  220. die "FATAL: don't know how to handle this release\n";
  221. }
  222. return $url;
  223. }
  224. sub get_installed () {
  225. my(%map);
  226. my(@l) = `$RPM_NPRIV --provides -qa`;
  227. foreach (@l) {
  228. /^(\S+)\s*(?:=\s*([^\s\-]+)-([^\s\+]+)(\+PROXY)?)?$/;
  229. push(@{$map{$1}->{"$2-$3"}}, {
  230. name => $1,
  231. version => (defined $2 ? $2 : '*'),
  232. release => (defined $3 ? $3 : '*'),
  233. PROXY => $4
  234. });
  235. }
  236. return \%map;
  237. }
  238. sub revdep ($$$) {
  239. my($rev,$t,$name) = @_;
  240. return 1 if $name eq $t->{name};
  241. foreach (@{$rev->{$_}}) {
  242. return 1 if revdep($rev,$t,$_->{name});
  243. }
  244. return -1;
  245. }
  246. sub get_revdep ($) {
  247. my($env) = @_;
  248. my($i) = $env->{'installed'};
  249. my($r) = $env->{'repository'};
  250. my($pkg, %rev);
  251. my(@vers,$t,@names);
  252. print "# computing reverse dependencies\n";
  253. foreach $pkg (keys %$i) {
  254. unless ($r->{$pkg}) {
  255. print "# ATTENTION: $pkg has no upgrade path\n";
  256. next;
  257. }
  258. @vers = get_versions($r->{$pkg}, sub { 1; });
  259. foreach (@vers) {
  260. foreach $t (@{$r->{$pkg}->{$_}}) {
  261. next unless $i->{$t->{name}};
  262. next unless $t->{depends};
  263. @names = grep { $_ ne '' }
  264. map { /^(\S+)/ }
  265. @{$t->{depends}};
  266. next unless @names;
  267. push @{$rev{$_}}, $t foreach @names;
  268. }
  269. }
  270. }
  271. foreach $pkg (keys %rev) {
  272. $rev{$pkg} = [
  273. sort {
  274. revdep(\%rev, $b, $a->{name});
  275. } @{$rev{$pkg}}
  276. ];
  277. }
  278. return \%rev;
  279. }
  280. sub parse_options ($) {
  281. my($l) = @_;
  282. $l = [ split(/\n+/, $l) ] unless ref $l;
  283. my(%with) = map { /--define\s*'(\S+)\s+(\S+?)'/ } @$l;
  284. return unless %with;
  285. return \%with;
  286. }
  287. sub override_options ($$) {
  288. my($old, $new) = @_;
  289. while (my ($k,$v) = each %$new) {
  290. $old->{$k} = $v if exists $old->{$k};
  291. }
  292. }
  293. sub get_with ($;$) {
  294. my($t,$fn) = @_;
  295. my(@l,%with);
  296. unless ($t->{OPTIONS}) {
  297. if (defined $fn) {
  298. @l = `$RPM_NPRIV -qi -p $fn`;
  299. } else {
  300. @l = `$RPM_NPRIV -qi $t->{name}`;
  301. }
  302. $t->{OPTIONS} = parse_options(\@l);
  303. }
  304. return $t->{OPTIONS};
  305. }
  306. sub relurl ($$$) {
  307. my($url,$fn,$suburl) = @_;
  308. my($subfn,$submap);
  309. unless ($suburl =~ /^\w+:\/\// || $suburl =~ /^\//) {
  310. if (defined $fn) {
  311. $subfn = $fn;
  312. $subfn =~ s/\/[^\/]*$//;
  313. $subfn .= '/' unless $subfn =~ /\/$/;
  314. $subfn .= $suburl;
  315. $suburl = $subfn;
  316. } else {
  317. $subfn = $url;
  318. $subfn =~ s/\/[^\/]*$//;
  319. $subfn .= '/' unless $subfn =~ /\/$/;
  320. $suburl = "$subfn$suburl";
  321. $subfn = undef;
  322. }
  323. }
  324. return ($suburl, $subfn);
  325. }
  326. sub xel($) {
  327. my($a) = @_;
  328. my($l) = $a->[0];
  329. return '' if ref $l;
  330. return $l;
  331. }
  332. sub get_index ($$$) {
  333. my($url,$fn,$with) = @_;
  334. my($ua,$req,$res,$rdf);
  335. my($bzip2,$path);
  336. my(%map,@include);
  337. my($fetch);
  338. $fetch = defined $fn ? $fn : $url;
  339. $bzip2 = $RPM;
  340. $bzip2 =~ s/bin\/rpm$/lib\/openpkg\/bzip2/
  341. or die "FATAL: cannot deduce bzip2 path from $RPM\n";
  342. $fetch !~ /\.bz2$/ || -x $bzip2
  343. or die "FATAL: $bzip2 not found\n";
  344. if ($fetch =~ /^\w+:/) { # looks like URL scheme
  345. print "# curling index $fetch\n";
  346. if ($fetch =~ /\.bz2$/) {
  347. $path = "$CURL -q -s -o - \"$fetch\" | $bzip2 -dc |";
  348. } else {
  349. $path = "$CURL -q -s -o - \"$fetch\" |";
  350. }
  351. } else {
  352. print "# reading index file $fn\n";
  353. if ($fetch =~ /\.bz2$/) {
  354. $path = "$bzip2 -dc $fetch |";
  355. } else {
  356. $path = "< $fetch";
  357. }
  358. }
  359. open(RFH, $path) or
  360. die "FATAL: cannot open '$fetch' ($!)\n";
  361. eval {
  362. require XML::Simple;
  363. };
  364. if ($@) {
  365. print "# using simple text parser\n";
  366. my($section);
  367. my($name,$version);
  368. my($href,$release,$desc);
  369. my(@prereq,@bprereq);
  370. my(@provides,@conflicts);
  371. my($platform,$prefix);
  372. my($rec);
  373. my($tag,$cond,$body);
  374. my($useit);
  375. while (<RFH>) {
  376. s/&gt;/>/g;
  377. s/&lt;/</g;
  378. if (!(defined $href) && /<rdf:Description.*?href="([^"]*)"/) {
  379. $href = $1;
  380. $section = undef;
  381. $name = undef;
  382. $release = undef;
  383. $desc = '';
  384. $platform = undef;
  385. $prefix = undef;
  386. @prereq = ();
  387. @bprereq = ();
  388. @provides = ();
  389. @conflicts = ();
  390. }
  391. if (!(defined $href) && /<Repository.*?href="([^"]*)"/) {
  392. push(@include, $1);
  393. next;
  394. }
  395. next unless defined $href;
  396. ($tag,$cond,$body) = /<(\/?[\w:]+)\s*(?:cond="([^"]+)")?>([^<]*)/;
  397. $useit = conditional($cond,$with);
  398. if ($tag eq 'Description') {
  399. $section = 'description';
  400. } elsif ($tag eq '/Description') {
  401. $section = undef;
  402. } elsif ($section eq 'description') {
  403. $desc .= $_;
  404. } elsif ($tag eq 'PreReq') {
  405. $section = 'prereq' if $useit;
  406. } elsif ($tag eq '/PreReq') {
  407. $section = undef;
  408. } elsif ($tag eq 'BuildPreReq') {
  409. $section = 'bprereq' if $useit;
  410. } elsif ($tag eq '/BuildPreReq') {
  411. $section = undef;
  412. } elsif ($tag eq 'Provides') {
  413. $section = 'provides' if $useit;
  414. } elsif ($tag eq '/Provides') {
  415. $section = undef;
  416. } elsif ($tag eq 'Conflicts') {
  417. $section = 'conflicts' if $useit;
  418. } elsif ($tag eq '/Conflicts') {
  419. $section = undef;
  420. } elsif ($tag eq 'Name') {
  421. $name = $body;
  422. } elsif ($tag eq 'Version') {
  423. $version = $body;
  424. } elsif ($tag eq 'Release') {
  425. $release = $body;
  426. } elsif ($tag eq 'Platform') {
  427. $platform = $body;
  428. } elsif ($tag eq 'Prefixes') {
  429. $prefix = $body;
  430. } elsif ($tag eq 'rdf:li') {
  431. if ($section eq 'prereq') {
  432. push(@prereq, $body);
  433. } elsif ($section eq 'bprereq') {
  434. push(@bprereq, $body);
  435. } elsif ($section eq 'provides') {
  436. push(@provides, $body);
  437. } elsif ($section eq 'conflicts') {
  438. push(@conflicts, $body);
  439. }
  440. } elsif ($tag eq '/rdf:Description') {
  441. if (defined $href &&
  442. defined $name &&
  443. defined $version &&
  444. defined $release) {
  445. @provides = map {
  446. /(\S+)\s*(?:=\s*(\S+?)\-(\S+))?$/;
  447. {
  448. name => $1,
  449. version => $2,
  450. release => $3
  451. }
  452. } @provides;
  453. unless (grep($_->{name} eq $name, @provides)) {
  454. push(@provides, {
  455. name => $name,
  456. version => $version,
  457. release => $release
  458. });
  459. }
  460. $rec = {
  461. href => (relurl($url, undef, $href))[0],
  462. name => $name,
  463. version => $version,
  464. release => $release,
  465. depends => [ @bprereq ],
  466. keeps => [ @prereq ],
  467. conflicts => [ @conflicts ],
  468. desc => $desc,
  469. platform => $platform,
  470. prefix => $prefix
  471. };
  472. $rec->{OPTIONS} = parse_options($rec->{desc});
  473. foreach (@provides) {
  474. push(@{$map{$_->{name}}->{vs($_)}}, $rec);
  475. }
  476. }
  477. $href = undef;
  478. }
  479. }
  480. } else {
  481. print "# using XML parser\n";
  482. my($xml) = XML::Simple::XMLin(\*RFH, forcearray => 1);
  483. my($desc) = $xml->{'Repository'}->[0]->{'rdf:Description'};
  484. my($sub) = $xml->{'Repository'}->[0]->{'Repository'};
  485. my($provides,@provides,$rec);
  486. my($href,$name,$version,$release);
  487. foreach (@$desc) {
  488. $href = $_->{'href'};
  489. $name = xel($_->{'Name'});
  490. $version = xel($_->{'Version'});
  491. $release = xel($_->{'Release'});
  492. next unless defined $href &&
  493. defined $name &&
  494. defined $version &&
  495. defined $release;
  496. $provides = $_->{'Provides'}->[0]->{'rdf:bag'}->[0]->{'rdf:li'};
  497. @provides = map {
  498. /(\S+)\s*(?:=\s*(\S+?)\-(\S+))?$/;
  499. {
  500. name => $1,
  501. version => $2,
  502. release => $3
  503. }
  504. } @$provides;
  505. unless (grep($_->{name} eq $name, @provides)) {
  506. push(@provides, {
  507. name => $name,
  508. version => $version,
  509. release => $release
  510. });
  511. }
  512. $rec = {
  513. href => (relurl($url, undef, $href))[0],
  514. name => $name,
  515. version => $version,
  516. release => $release,
  517. platform => xel($_->{'Platform'}),
  518. prefix => xel($_->{'Prefixes'}),
  519. depends =>
  520. ( $_->{'BuildPreReq'}->[0]->{'rdf:bag'}->[0]->{'rdf:li'}
  521. || [] ),
  522. keeps =>
  523. ( $_->{'PreReq'}->[0]->{'rdf:bag'}->[0]->{'rdf:li'}
  524. || [] ),
  525. desc => xel($_->{'Description'})
  526. };
  527. $rec->{OPTIONS} = parse_options($rec->{desc});
  528. foreach (@provides) {
  529. push(@{$map{$_->{name}}->{vs($_)}}, $rec);
  530. }
  531. }
  532. if ($sub) {
  533. @include = map { $_->{href} } @$sub;
  534. }
  535. }
  536. close(RFH)
  537. or die "FATAL: an I/O error occured\n";
  538. #
  539. # cannot do real recursions on file handles, so we simply append
  540. # all sub-RDFs, the result is flattend into a big hash anyway
  541. #
  542. foreach (@include) {
  543. my($submap);
  544. my($suburl,$subfn) = relurl($url,$fn,$_);
  545. $submap = get_index($suburl,$subfn,$with);
  546. while (my($name,$vmap) = each %$submap) {
  547. while (my($vs,$recs) = each %$vmap) {
  548. push @{$map{$name}->{$vs}}, @$recs;
  549. }
  550. }
  551. }
  552. return \%map;
  553. }
  554. #
  555. # grep all versions of a name that
  556. # satisfy a condition
  557. #
  558. sub get_versions ($$) {
  559. my($relmap, $cond) = @_;
  560. return grep { $cond->($_); }
  561. sort { vcmp($a,$b); } keys %$relmap;
  562. }
  563. #
  564. # there can be multiple sources for a target release
  565. #
  566. sub chose_source ($$@) {
  567. my($env, $name, $vmap, @vers) = @_;
  568. my(@recs,@nrecs,$rec);
  569. return unless @vers;
  570. @recs = grep {
  571. $env->{sourceonly} ? (
  572. !(defined $_->{'platform'})
  573. ) : (
  574. !(defined $_->{'platform'}) || (
  575. defined $_->{'prefix'} &&
  576. $_->{'platform'} eq $env->{config}->{platform} &&
  577. $_->{'prefix'} eq $env->{config}->{prefix}
  578. )
  579. )
  580. } map { @{$vmap->{$_}} } @vers;
  581. return unless @recs;
  582. if (scalar(@recs) > 1) {
  583. @nrecs = grep {
  584. $env->{built}->{$_->{name}} ||
  585. $env->{installed}->{$_->{name}}
  586. } @recs;
  587. @recs = @nrecs if @nrecs;
  588. }
  589. if (scalar(@recs) > 1 && !$env->{sourceonly}) {
  590. @nrecs = grep {
  591. defined $_->{'platform'}
  592. } @recs;
  593. @recs = @nrecs if @nrecs;
  594. }
  595. if (scalar(@recs) > 1) {
  596. print "# ambigous sources for $name\n";
  597. my($i) = 0;
  598. foreach (@recs) {
  599. print "# $i: ".vsn($_)." = $_->{href}\n";
  600. $i++;
  601. }
  602. die "ERROR: ambigous dependency\n";
  603. } else {
  604. if ($env->{upgrade}) {
  605. $rec = $recs[-1];
  606. } else {
  607. $rec = $recs[0];
  608. }
  609. }
  610. print "# source for $name is ".vsn($rec)."\n";
  611. return $rec;
  612. }
  613. #
  614. # see wether target is in map
  615. #
  616. sub target_exists ($$) {
  617. my($target, $map) = @_;
  618. my($vmap) = $map->{$target->{name}};
  619. return unless $vmap;
  620. return !defined $target->{version} ||
  621. defined $vmap->{vs($target)};
  622. }
  623. #
  624. # find target in map
  625. #
  626. sub find_target ($$) {
  627. my($name, $map) = @_;
  628. my($vmap) = $map->{$name};
  629. my(@vs);
  630. return unless $vmap;
  631. @vs = sort { vcmp($b,$a) } keys %$vmap;
  632. return $vmap->{$vs[0]}->[-1];
  633. }
  634. #
  635. # see wether target has conflicts in map
  636. #
  637. sub target_conflicts ($$) {
  638. my($target, $map) = @_;
  639. my($t);
  640. foreach (@{$target->{conflicts}}) {
  641. $t = find_target($_, $map);
  642. return $t if $t;
  643. }
  644. return;
  645. }
  646. #
  647. # retrieve build dependencies for target in map
  648. #
  649. sub target_depends ($$) {
  650. my($target, $map) = @_;
  651. my($vmap,$vers);
  652. die "FATAL: ",vsn($target)," not in depend map\n"
  653. unless
  654. ( $vmap = $map->{$target->{name}} ) &&
  655. ( defined $target->{version} ) &&
  656. ( $vers = $vmap->{vs($target)} ) &&
  657. @$vers;
  658. return $vers->[0]->{depends};
  659. }
  660. #
  661. # retrieve runtime dependencies for target in map
  662. #
  663. sub target_keeps ($$) {
  664. my($target, $map) = @_;
  665. my($vmap,$vers);
  666. die "FATAL: ",vsn($target)," not in keep map\n"
  667. unless
  668. ( $vmap = $map->{$target->{name}} ) &&
  669. ( defined $target->{version} ) &&
  670. ( $vers = $vmap->{vs($target)} ) &&
  671. @$vers;
  672. return $vers->[0]->{keeps};
  673. }
  674. #
  675. # test wether target could be upgraded
  676. #
  677. sub target_newer ($$) {
  678. my($target, $map) = @_;
  679. my($vs) = vs($target);
  680. my($vmap) = $map->{$target->{name}};
  681. return 1 unless $vmap;
  682. return !grep { vcmp($vs, $_) <= 0; } keys %$vmap;
  683. }
  684. #
  685. # check wether installed package matches
  686. # build options
  687. #
  688. sub target_suitable ($$) {
  689. my($target, $with) = @_;
  690. my($iwith);
  691. my($k,$v);
  692. $iwith = $target->{OPTIONS};
  693. while (($k,$v) = each %$with) {
  694. if (exists $iwith->{$k}) {
  695. return 0 if $iwith->{$k} ne $with->{$k};
  696. }
  697. }
  698. return 1;
  699. }
  700. #
  701. # report options that are not used for
  702. #
  703. sub warn_about_options ($$) {
  704. my($target, $with) = @_;
  705. my($iwith) = $target->{OPTIONS};
  706. my($k,$v);
  707. return unless defined $iwith;
  708. while (($k,$v) = each %$with) {
  709. if (!exists $iwith->{$k}) {
  710. print "# ATTENTION: $target->{name} ignores option '$k'\n";
  711. }
  712. }
  713. }
  714. #
  715. # locate target for a dependency
  716. #
  717. sub dep2target ($$) {
  718. my($dep, $env) = @_;
  719. my($name,@vers);
  720. my($i,$r,$b,$cond,$version);
  721. my($t);
  722. $dep =~ s/(\S+)\s*//;
  723. $name = $1;
  724. $i = $env->{installed}->{$name};
  725. $r = $env->{repository}->{$name};
  726. $b = $env->{built}->{$name};
  727. return unless $i || $r || $b;
  728. if ($dep =~ /^>=\s*(\S+)$/) {
  729. $version = $1;
  730. $cond = sub { vcmp($_[0],$version) >= 0; };
  731. } elsif ($dep =~ /^=\s*(\S+)$/) {
  732. $version = $1;
  733. $cond = sub { vcmp($_[0],$version) == 0; };
  734. } elsif ($dep =~ /^\s*$/) {
  735. $cond = sub { 1; };
  736. } else {
  737. die "FATAL: don't know how to handle PreReq: $name $dep\n";
  738. }
  739. if ($i && (@vers = get_versions($i, $cond))) {
  740. foreach (@vers) {
  741. $t = $i->{$_}->[0];
  742. if (get_with($t), target_suitable($t, $env->{with})) {
  743. if (!$env->{upgrade}) {
  744. return ($t, 1);
  745. }
  746. }
  747. }
  748. }
  749. if ($b && (@vers = get_versions($b, $cond))) {
  750. return ($b->{$vers[0]}->[0], 1);
  751. }
  752. return (chose_source($env, $name, $r, get_versions($r, $cond)), 0);
  753. }
  754. sub make_dep ($$$$$) {
  755. my($target,$depth,$env,$list,$blist) = @_;
  756. my($d,$k,%d,%k,$t,$old);
  757. if (target_exists($target, $env->{built})) {
  758. print "# $target->{name} is already in list\n";
  759. return;
  760. }
  761. if ($t = target_conflicts($target, $env->{installed})) {
  762. print "# $target->{name} conflicts with ",vsn($t),"\n";
  763. return;
  764. }
  765. if ($t = target_conflicts($target, $env->{built})) {
  766. print "# $target->{name} conflicts with ",vsn($t),"\n";
  767. return;
  768. }
  769. #
  770. # see if a target is already installed and requires a rebuild
  771. #
  772. if ($t = find_target($target->{name}, $env->{installed})) {
  773. if (exists $env->{exclude}->{$target->{name}}) {
  774. print "# excluding $target->{name} (no upgrade allowed)\n";
  775. return;
  776. }
  777. get_with($t);
  778. if ($target->{REBUILD}) {
  779. print "# rebuilding $target->{name} (dependency)\n";
  780. } elsif ($env->{zero}) {
  781. print "# rebuilding $target->{name} (zero)\n";
  782. } elsif (target_newer($target, $env->{installed})) {
  783. print "# rebuilding $target->{name} (upgrade)\n";
  784. } elsif (!target_suitable($t, $env->{with})) {
  785. print "# rebuilding $target->{name} (parameter mismatch)\n";
  786. } else {
  787. print "# $target->{name} is already installed\n";
  788. return;
  789. }
  790. # use options from installed base
  791. override_options($target->{OPTIONS}, $t->{OPTIONS});
  792. # remember this is a rebuild for a proxy package
  793. $target->{PROXY} = $t->{PROXY};
  794. $target->{REBUILD} = 1;
  795. }
  796. if (exists $env->{exclude}->{$target->{name}}) {
  797. die "FATAL: target ".vsn($target)." is forbidden\n";
  798. }
  799. # mark this as a target before reverse dependencies trigger
  800. # it again
  801. push(@{$env->{built}->{$target->{name}}->{vs($target)}}, $target);
  802. $d = target_depends($target, $env->{repository});
  803. $k = target_keeps($target, $env->{repository});
  804. #
  805. # recurse over dependencies
  806. #
  807. if (@$d || @$k) {
  808. %d = map { $_ => 1 } @$d, @$k;
  809. %k = map { $_ => 1 } @$k;
  810. foreach (keys %d) {
  811. # old index misses a OpenPKG provider in the index... skip it
  812. next if $_ eq 'OpenPKG';
  813. ($t,$old) = dep2target($_, $env);
  814. if ($t) {
  815. if ($old) {
  816. print "# $target->{name} uses ".vsn($t)." for $_\n";
  817. next;
  818. }
  819. # record which targets to keep in blist
  820. if ($k{$_}) {
  821. push(@$blist,$t);
  822. print "# $target->{name} installs ".vsn($t)." for $_\n";
  823. } else {
  824. print "# $target->{name} requires ".vsn($t)." for $_\n";
  825. }
  826. make_dep($t,$depth+1,$env,$list,$blist);
  827. } else {
  828. die "FATAL: $target->{name} searches for a frood called '$_'\n";
  829. }
  830. }
  831. }
  832. print "# adding ".vsn($target)." to list\n";
  833. push(@$list, $target);
  834. if (!$env->{quick} &&
  835. $target->{name} ne 'openpkg' &&
  836. $target->{REBUILD}) {
  837. unless ($env->{revdep}) {
  838. $env->{revdep} = get_revdep($env);
  839. }
  840. foreach $t (@{$env->{revdep}->{$target->{name}}}) {
  841. # this is a rebuild, triggering further revdeps
  842. $t->{REBUILD} = 1;
  843. # this is a rebuild, keep this installed
  844. push(@$blist, $t);
  845. print "# rebuilding revdep ".vsn($t)."\n";
  846. make_dep($t,$depth+1,$env,$list,$blist);
  847. }
  848. }
  849. }
  850. sub remove_list ($$$) {
  851. my($targets, $keeps, $installed) = @_;
  852. my(%keep);
  853. %keep = map { $_ => 1 } @$keeps;
  854. return [ grep {
  855. !$keep{$_} && !$installed->{$_->{name}}->{vs($_)};
  856. } @$targets
  857. ];
  858. }
  859. sub build_list ($$) {
  860. my($pattern, $env) = @_;
  861. my(@goals,@targets,@keeps,$bonly,$t);
  862. my($name,$r,$i,@vers);
  863. my(@todo);
  864. if (defined $pattern) {
  865. @todo = ();
  866. foreach (split(/\s+/,$pattern)) {
  867. next unless /\S/;
  868. if (s/\*+$//) {
  869. push @todo, '^'.quotemeta($_).'';
  870. } else {
  871. push @todo, '^'.quotemeta($_).'$';
  872. }
  873. }
  874. $pattern = join('|',@todo);
  875. @todo = grep(/$pattern/, keys %{$env->{repository}});
  876. } else {
  877. @todo = grep {
  878. my($n) = $_;
  879. (ref $env->{installed}->{$n}) &&
  880. grep { $_ ne '-' } keys %{$env->{installed}->{$n}}
  881. } keys %{$env->{repository}};
  882. }
  883. #
  884. # chose sources for goals from repository
  885. #
  886. foreach $name (@todo) {
  887. $t = undef;
  888. #
  889. # keeping installed packages for goals is ugly
  890. # -> we currently do not support installed source RPMs
  891. # -> source RPMs might already have expired from repository
  892. #
  893. # consequence:
  894. # -> goals are always upgraded to repository versions
  895. #
  896. #unless ($env->{upgrade}) {
  897. # $i = $env->{installed}->{$name};
  898. # if (@vers = get_versions($i, sub { 1; })) {
  899. # $t = chose_source($env, $name, $i, @vers);
  900. # }
  901. #}
  902. unless ($t) {
  903. $r = $env->{repository}->{$name};
  904. if (@vers = get_versions($r, sub { 1; })) {
  905. $t = chose_source($env, $name, $r, @vers);
  906. }
  907. }
  908. die "FATAL: no known source found for '$name'\n" unless $t;
  909. warn_about_options($t, $env->{with});
  910. push(@goals, $t);
  911. }
  912. return unless @goals;
  913. @targets = ();
  914. @keeps = @goals;
  915. foreach $t (@goals) {
  916. print "# recursing over dependencies for ".vsn($t)."\n";
  917. make_dep($t,0,$env,\@targets,\@keeps);
  918. }
  919. $bonly = remove_list(\@targets, \@keeps, $env->{installed});
  920. return (\@targets, $bonly);
  921. }
  922. #######################################################################
  923. sub target2rpm ($$) {
  924. my($target,$c) = @_;
  925. my($tmpl) = $c->{template};
  926. $tmpl =~ s/%{NAME}/$target->{name}/;
  927. $tmpl =~ s/%{VERSION}/$target->{version}/;
  928. $tmpl =~ s/%{RELEASE}/$target->{release}/;
  929. return $c->{rpmdir}.'/'.$tmpl;
  930. }
  931. #######################################################################
  932. sub binary_target ($$) {
  933. my($t, $fn) = @_;
  934. my(%target) = %$t;
  935. get_with(\%target, $fn);
  936. return \%target;
  937. }
  938. sub find_proxy ($$) {
  939. my($t,$bpkg) = @_;
  940. my(@l) = `$RPM_NPRIV -ql $t->{name}`;
  941. my($link) = (grep { $_ =~ /\/\.prefix-$t->{name}$/ } @l)[0];
  942. return unless defined $link;
  943. chomp $link;
  944. my($prefix) = readlink($link);
  945. return unless defined $prefix;
  946. $bpkg =~ s/.*\///;
  947. $bpkg =~ s/(\.[^-]+-[^-]+-)[^-]+\.rpm$/$1*.rpm/;
  948. return (glob("$prefix/RPM/PKG/$bpkg"))[0];
  949. }
  950. sub make_defines ($$) {
  951. my($old, $new) = @_;
  952. my($with);
  953. #
  954. # override old parameters with new parameters
  955. # drop new parameters that do not exist in old set
  956. #
  957. # if there is no old set at all (which happens if there
  958. # is no template and no installed package), just use the
  959. # new parameters and assume these are useful.
  960. #
  961. if ($old) {
  962. $old = { %$old };
  963. override_options($old, $new);
  964. } else {
  965. $old = $new;
  966. }
  967. #
  968. # convert parameters to --define command line options
  969. # skip parameter templates from index
  970. #
  971. $with = join(' ',map { "--define '$_ $old->{$_}'" }
  972. grep { $old->{$_} !~ /^%/ } keys %$old);
  973. $with = ' '.$with if $with ne '';
  974. return $with;
  975. }
  976. sub print_list1 ($$$@$) {
  977. my($list,$c,$uncond,$with,$ignore) = @_;
  978. my($spkg,$bpkg,$ppkg);
  979. my($opt);
  980. my($cmd1, $cmd2, $mark);
  981. $mark = '::::';
  982. foreach (@$list) {
  983. $spkg = $_->{href};
  984. $bpkg = target2rpm($_, $c);
  985. #
  986. # rebuild binary package IF
  987. #
  988. # 'unconditional' option
  989. # OR there is no binary package
  990. # OR dependency check found that installed package is not suitable
  991. # OR existing binary package doesn't satisfy wanted options
  992. #
  993. $cmd1 = undef;
  994. if ($uncond || !-f $bpkg || $_->{REBUILD} ||
  995. !target_suitable(binary_target($_, $bpkg),$with)) {
  996. $opt = make_defines($_->{OPTIONS}, $with);
  997. #
  998. # proxy packages are rebuilt from their maste
  999. # hierachy
  1000. #
  1001. # someone preferred a binary from the repository
  1002. # just copy it to the local store
  1003. #
  1004. if ($_->{PROXY}) {
  1005. $ppkg = find_proxy($_,$bpkg) or
  1006. die "FATAL: proxy package ",vsn($_)," does not exist\n";
  1007. $cmd1 = "$RPM_NPRIV$opt --makeproxy -o $bpkg $ppkg";
  1008. } elsif (defined $_->{platform}) {
  1009. $cmd1 = "$CURL -q -s -o $bpkg $spkg";
  1010. } else {
  1011. $cmd1 = "$RPM_NPRIV$opt --rebuild $spkg";
  1012. }
  1013. }
  1014. #
  1015. # if package exist force rpm to copy over new files
  1016. # better than erasing everything and losing configuration
  1017. # files
  1018. #
  1019. $opt = $_->{REBUILD} ? ' --force' : '';
  1020. $cmd2 = "$RPM_PRIV$opt -Uvh $bpkg";
  1021. if ($ignore) {
  1022. $cmd2 = "$cmd1 && \\\n$cmd2" if defined $cmd1;
  1023. } else {
  1024. if (defined $cmd1) {
  1025. $cmd2 = "$cmd1 || exit \$?\n$cmd2 || exit \$?"
  1026. } else {
  1027. $cmd2 = "$cmd2 || exit \$?";
  1028. }
  1029. }
  1030. print "echo $mark $spkg $mark\n$cmd2\necho $mark $spkg = \$? $mark\n";
  1031. }
  1032. }
  1033. sub print_list2 ($$) {
  1034. my($list,$c) = @_;
  1035. my($pkg);
  1036. foreach (@$list) {
  1037. $pkg = "$_->{name}-$_->{version}-$_->{release}";
  1038. print "$RPM_PRIV -e $pkg\n";
  1039. }
  1040. }
  1041. #######################################################################
  1042. my($config,$url,$repository,$installed,$list,$bonly);
  1043. my($pattern,%with,%exclude);
  1044. if ($opt_a) {
  1045. $pattern = undef;
  1046. } else {
  1047. $pattern = join(' ', @ARGV);
  1048. }
  1049. if ($opt_A) {
  1050. $pattern = '*';
  1051. }
  1052. %with = map {
  1053. /([^\s=]+)(?:\=(\S+))?/
  1054. ? ($1 => (defined $2 ? $2 : 'yes'))
  1055. : ()
  1056. } split(/\s+/, $opt_D);
  1057. %exclude = map { $_ => 1 } split(/\s+/, $opt_E);
  1058. $config = get_config();
  1059. if (defined $opt_p) {
  1060. $config->{platform} = $opt_p;
  1061. }
  1062. if (defined $opt_r) {
  1063. $url = $opt_r;
  1064. $url .= '/' unless $url =~ /\/$/;
  1065. } else {
  1066. $url = get_release();
  1067. }
  1068. #
  1069. # if we read the index from a file we can no longer deduce
  1070. # repository paths from index paths. For now lets assume
  1071. # that everything is below SRC/ to be compatible with
  1072. # existing file indexes.
  1073. #
  1074. if (defined $opt_f && !defined $opt_r) {
  1075. $url .= 'SRC/';
  1076. }
  1077. $installed = $opt_Z ? {} : get_installed();
  1078. $repository = get_index($url.'00INDEX.rdf',$opt_f,\%with);
  1079. ($list,$bonly) = build_list($pattern, {
  1080. config => $config,
  1081. installed => $installed,
  1082. repository => $repository,
  1083. built => {},
  1084. revdep => undef,
  1085. with => \%with,
  1086. exclude => \%exclude,
  1087. upgrade => ($opt_a || $opt_U),
  1088. zero => ($opt_z || $opt_Z),
  1089. quick => $opt_q,
  1090. sourceonly => (
  1091. $opt_u ||
  1092. $opt_U ||
  1093. $opt_z ||
  1094. $opt_Z ||
  1095. scalar(%with) > 0 )
  1096. });
  1097. die "FATAL: cannot find package\n" unless defined $list;
  1098. print_list1($list,$config,$opt_a || $opt_u || $opt_U,\%with,$opt_i);
  1099. print_list2($bonly,$config);