toolchain: make the default eglibc selection work even if TOOLCHAINOPTS is not enabled
[openwrt/svn-archive/archive.git] / scripts / feeds
1 #!/usr/bin/perl
2 use Getopt::Std;
3 use FindBin;
4 use Cwd;
5 use lib "$FindBin::Bin";
6 use metadata;
7 use warnings;
8 use strict;
9 use Cwd 'abs_path';
10
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR}=getcwd();
13 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
14
15 my $mk=`which gmake 2>/dev/null`; # select the right 'make' program
16 chomp($mk); # trim trailing newline
17 $mk or $mk = "make"; # default to 'make'
18
19 # check version of make
20 my @mkver = split /\s+/, `$mk -v`, 4;
21 my $valid_mk = 1;
22 $mkver[0] =~ /^GNU/ or $valid_mk = 0;
23 $mkver[1] =~ /^Make/ or $valid_mk = 0;
24 $mkver[2] >= "3.81" or $valid_mk = 0;
25 $valid_mk or die "Unsupported version of make found: $mk\n";
26
27 my @feeds;
28 my %build_packages;
29 my %installed;
30 my %feed_cache;
31
32 my $feed_package = {};
33 my $feed_src = {};
34
35 sub parse_config() {
36 my $line = 0;
37 my %name;
38
39 open FEEDS, "feeds.conf" or
40 open FEEDS, "feeds.conf.default" or
41 die "Unable to open feeds configuration";
42 while (<FEEDS>) {
43 chomp;
44 s/#.+$//;
45 next unless /\S/;
46 my @line = split /\s+/, $_, 3;
47 my @src;
48 $line++;
49
50 my $valid = 1;
51 $line[0] =~ /^src-\w+$/ or $valid = 0;
52 $line[1] =~ /^\w+$/ or $valid = 0;
53 @src = split /\s+/, $line[2];
54 $valid or die "Syntax error in feeds.conf, line: $line\n";
55
56 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
57 $name{$line[1]} = 1;
58
59 push @feeds, [$line[0], $line[1], \@src];
60 }
61 close FEEDS;
62 }
63
64 sub update_location($$)
65 {
66 my $name = shift;
67 my $url = shift;
68 my $old_url;
69
70 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
71
72 if( open LOC, "< ./feeds/$name.tmp/location" )
73 {
74 chomp($old_url = readline LOC);
75 close LOC;
76 }
77
78 if( !$old_url || $old_url ne $url )
79 {
80 if( open LOC, "> ./feeds/$name.tmp/location" )
81 {
82 print LOC $url, "\n";
83 close LOC;
84 }
85 return $old_url ? 1 : 0;
86 }
87
88 return 0;
89 }
90
91 sub update_index($)
92 {
93 my $name = shift;
94
95 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
96 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
97
98 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
99 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"packageinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"package\" SCAN_DEPS=\"$ENV{TOPDIR}/include/package*.mk\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
100 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
101
102 return 0;
103 }
104
105 my %update_method = (
106 'src-svn' => {
107 'init' => "svn checkout '%s' '%s'",
108 'update' => "svn update",
109 'controldir' => ".svn",
110 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
111 'src-cpy' => {
112 'init' => "cp -Rf '%s' '%s'",
113 'update' => "",
114 'revision' => "echo -n 'local'"},
115 'src-link' => {
116 'init' => "ln -s '%s' '%s'",
117 'update' => "",
118 'revision' => "echo -n 'local'"},
119 'src-git' => {
120 'init' => "git clone --depth 1 '%s' '%s'",
121 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
122 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
123 'update' => "git pull --ff",
124 'controldir' => ".git",
125 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
126 'src-gitsvn' => {
127 'init' => "git svn clone -r HEAD '%s' '%s'",
128 'update' => "git svn rebase",
129 'controldir' => ".git",
130 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
131 'src-bzr' => {
132 'init' => "bzr checkout --lightweight '%s' '%s'",
133 'update' => "bzr update",
134 'controldir' => ".bzr"},
135 'src-hg' => {
136 'init' => "hg clone '%s' '%s'",
137 'update' => "hg pull --update",
138 'controldir' => ".hg"},
139 'src-darcs' => {
140 'init' => "darcs get '%s' '%s'",
141 'update' => "darcs pull -a",
142 'controldir' => "_darcs"},
143 );
144
145 # src-git: pull broken
146 # src-cpy: broken if `basename $src` != $name
147
148 sub update_feed_via($$$$) {
149 my $type = shift;
150 my $name = shift;
151 my $src = shift;
152 my $relocate = shift;
153
154 my $m = $update_method{$type};
155 my $localpath = "./feeds/$name";
156 my $safepath = $localpath;
157 $safepath =~ s/'/'\\''/;
158 my ($base_branch, $branch) = split(/;/, $src, 2);
159 my ($base_commit, $commit) = split(/\^/, $src, 2);
160
161 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
162 system("rm -rf '$safepath'");
163 if ($m->{'init_branch'} and $branch) {
164 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
165 } elsif ($m->{'init_commit'} and $commit) {
166 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
167 } else {
168 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
169 }
170 } elsif ($m->{'init_commit'} and $commit) {
171 # in case git hash has been provided don't update the feed
172 } else {
173 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
174 }
175
176 return 0;
177 }
178
179 sub get_feed($) {
180 my $feed = shift;
181
182 if (!defined($feed_cache{$feed})) {
183 my $file = "./feeds/$feed.index";
184
185 clear_packages();
186 -f $file or do {
187 print "Ignoring feed '$feed' - index missing\n";
188 return;
189 };
190 parse_package_metadata($file) or return;
191 $feed_cache{$feed} = [ { %package }, { %srcpackage } ];
192 }
193
194 $feed_package = $feed_cache{$feed}->[0];
195 $feed_src = $feed_cache{$feed}->[1];
196 return $feed_cache{$feed}->[0];
197 }
198
199 sub get_installed() {
200 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
201 clear_packages();
202 parse_package_metadata("./tmp/.packageinfo");
203 %installed = %package;
204 }
205
206 sub search_feed {
207 my $feed = shift;
208 my @substr = @_;
209 my $display;
210
211 return unless @substr > 0;
212 get_feed($feed);
213 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
214 my $pkg = $feed_package->{$name};
215 my $substr;
216 my $pkgmatch = 1;
217
218 next if $pkg->{vdepends};
219 foreach my $substr (@substr) {
220 my $match;
221 foreach my $key (qw(name title description src)) {
222 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
223 }
224 $match or undef $pkgmatch;
225 };
226 $pkgmatch and do {
227 $display or do {
228 print "Search results in feed '$feed':\n";
229 $display = 1;
230 };
231 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
232 };
233 }
234 return 0;
235 }
236
237 sub search {
238 my %opts;
239
240 getopt('r:', \%opts);
241 foreach my $feed (@feeds) {
242 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
243 }
244 }
245
246 sub list_feed {
247 my $feed = shift;
248
249 get_feed($feed);
250 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
251 my $pkg = $feed_package->{$name};
252 next if $pkg->{vdepends};
253 if($pkg->{name}) {
254 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
255 }
256 }
257
258 return 0;
259 }
260
261 sub list {
262 my %opts;
263
264 getopts('r:d:sh', \%opts);
265 if ($opts{h}) {
266 usage();
267 return 0;
268 }
269 if ($opts{s}) {
270 foreach my $feed (@feeds) {
271 my $localpath = "./feeds/$feed->[1]";
272 my $m = $update_method{$feed->[0]};
273 my $revision;
274 if( !$m->{'revision'} ) {
275 $revision = "X";
276 }
277 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
278 $revision = `cd '$localpath'; $m->{'revision'}`;
279 }
280 else {
281 $revision = "local";
282 }
283 if ($opts{d}) {
284 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
285 }
286 else {
287 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
288 }
289 }
290 return 0;
291 }
292 foreach my $feed (@feeds) {
293 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
294 }
295 return 0;
296 }
297
298 sub install_generic() {
299 my $feed = shift;
300 my $pkg = shift;
301 my $path = $pkg->{makefile};
302
303 if($path) {
304 $path =~ s/\/Makefile$//;
305
306 -d "./package/feeds" or mkdir "./package/feeds";
307 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
308 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
309 } else {
310 warn "Package is not valid\n";
311 return 1;
312 }
313
314 return 0;
315 }
316
317 my %install_method = (
318 'src-svn' => \&install_generic,
319 'src-cpy' => \&install_generic,
320 'src-link' => \&install_generic,
321 'src-git' => \&install_generic,
322 'src-gitsvn' => \&install_generic,
323 'src-bzr' => \&install_generic,
324 'src-hg' => \&install_generic,
325 'src-darcs' => \&install_generic,
326 );
327
328 my %feed;
329
330 sub lookup_package($$) {
331 my $feed = shift;
332 my $package = shift;
333
334 foreach my $feed ($feed, @feeds) {
335 next unless $feed->[1];
336 next unless $feed{$feed->[1]};
337 $feed{$feed->[1]}->{$package} and return $feed;
338 }
339 return;
340 }
341
342 sub is_core_package($) {
343 my $package = shift;
344 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
345 next unless index($file, "tmp/info/.packageinfo-feeds_");
346 return 1 if -s $file;
347 }
348 return 0;
349 }
350
351 sub install_package {
352 my $feed = shift;
353 my $name = shift;
354 my $ret = 0;
355
356 $feed = lookup_package($feed, $name);
357 $feed or do {
358 $installed{$name} and return 0;
359 # TODO: check if it's already installed within ./package directory
360 $feed_src->{$name} or is_core_package($name) or warn "WARNING: No feed for package '$name' found, maybe it's already part of the standard packages?\n";
361 return 0;
362 };
363
364 # switch to the metadata for the selected feed
365 get_feed($feed->[1]);
366
367 my $pkg = $feed{$feed->[1]}->{$name} or return 1;
368 $pkg->{name} or do {
369 $installed{$name} and return 0;
370 # TODO: check if this is an alias package, maybe it's known by another name
371 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
372 return 0;
373 };
374 my $src = $pkg->{src};
375 my $type = $feed->[0];
376 $src or $src = $name;
377
378 # previously installed packages set the runtime package
379 # newly installed packages set the source package
380 $installed{$src} and return 0;
381
382 # check previously installed packages
383 $installed{$name} and return 0;
384 $installed{$src} = 1;
385 warn "Installing package '$src'\n";
386
387 $install_method{$type} or do {
388 warn "Unknown installation method: '$type'\n";
389 return 1;
390 };
391
392 &{$install_method{$type}}($feed, $pkg) == 0 or do {
393 warn "failed.\n";
394 return 1;
395 };
396
397 # install all dependencies referenced from the source package
398 foreach my $vpkg (@{$feed_src->{$src}}) {
399 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
400 next if $dep =~ /@/;
401 $dep =~ s/^\+//;
402 $dep =~ s/^.+://;
403 $dep =~ s/\/.+$//;
404 next unless $dep;
405 install_package($feed, $dep) == 0 or $ret = 1;
406 }
407 }
408
409 return $ret;
410 }
411
412 sub refresh_config {
413 my $default = shift;
414
415 # workaround for timestamp check
416 system("rm -f tmp/.packageinfo");
417
418 # refresh the config
419 if ($default) {
420 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
421 } else {
422 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
423 }
424 }
425
426 sub install {
427 my $name;
428 my %opts;
429 my $feed;
430 my $ret = 0;
431
432 getopts('ap:d:h', \%opts);
433
434 if ($opts{h}) {
435 usage();
436 return 0;
437 }
438
439 get_installed();
440
441 foreach my $f (@feeds) {
442 # index all feeds
443 $feed{$f->[1]} = get_feed($f->[1]);
444
445 # look up the preferred feed
446 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
447 }
448
449 if($opts{a}) {
450 foreach my $f (@feeds) {
451 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
452 printf "Installing all packages from feed %s.\n", $f->[1];
453 get_feed($f->[1]);
454 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
455 my $p = $feed_package->{$name};
456 next if $p->{vdepends};
457 if( $p->{name} ) {
458 install_package($feed, $p->{name}) == 0 or $ret = 1;
459 get_feed($f->[1]);
460 }
461 }
462 }
463 }
464 } else {
465 while ($name = shift @ARGV) {
466 install_package($feed, $name) == 0 or $ret = 1;
467 }
468 }
469
470 # workaround for timestamp check
471
472 # set the defaults
473 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
474 refresh_config($opts{d});
475 }
476
477 return $ret;
478 }
479
480 sub uninstall {
481 my %opts;
482 my $name;
483 my $uninstall;
484
485 getopts('ah', \%opts);
486
487 if ($opts{h}) {
488 usage();
489 return 0;
490 }
491
492 if ($opts{a}) {
493 system("rm -rvf ./package/feeds");
494 $uninstall = 1;
495 } else {
496 if($#ARGV == -1) {
497 warn "WARNING: no package to uninstall\n";
498 return 0;
499 }
500 get_installed();
501 while ($name = shift @ARGV) {
502 my $pkg = $installed{$name};
503 $pkg or do {
504 warn "WARNING: $name not installed\n";
505 next;
506 };
507 $pkg->{src} and $name = $pkg->{src};
508 warn "Uninstalling package '$name'\n";
509 system("rm -f ./package/feeds/*/$name");
510 $uninstall = 1;
511 }
512 }
513 $uninstall and refresh_config();
514 return 0;
515 }
516
517 sub update_feed($$$$)
518 {
519 my $type=shift;
520 my $name=shift;
521 my $src=shift;
522 my $perform_update=shift;
523 my $force_relocate=update_location( $name, "@$src" );
524
525 if( $force_relocate ) {
526 warn "Source of feed $name has changed, replacing copy\n";
527 }
528 $update_method{$type} or do {
529 warn "Unknown type '$type' in feed $name\n";
530 return 1;
531 };
532 $perform_update and do {
533 my $failed = 1;
534 foreach my $feedsrc (@$src) {
535 warn "Updating feed '$name' from '$feedsrc' ...\n";
536 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
537 $failed = 0;
538 last;
539 }
540 $failed and do {
541 warn "failed.\n";
542 return 1;
543 };
544 };
545 warn "Create index file './feeds/$name.index' \n";
546 update_index($name) == 0 or do {
547 warn "failed.\n";
548 return 1;
549 };
550 return 0;
551 }
552
553 sub update {
554 my %opts;
555 my $feed_name;
556 my $perform_update=1;
557
558 $ENV{SCAN_COOKIE} = $$;
559 $ENV{OPENWRT_VERBOSE} = 's';
560
561 getopts('ahi', \%opts);
562
563 if ($opts{h}) {
564 usage();
565 return 0;
566 }
567
568 if ($opts{i}) {
569 # don't update from (remote) repository
570 # only re-create index information
571 $perform_update=0;
572 }
573
574 -d "feeds" or do {
575 mkdir "feeds" or die "Unable to create the feeds directory";
576 };
577
578 if ( ($#ARGV == -1) or $opts{a}) {
579 foreach my $feed (@feeds) {
580 my ($type, $name, $src) = @$feed;
581 update_feed($type, $name, $src, $perform_update);
582 }
583 } else {
584 while ($feed_name = shift @ARGV) {
585 foreach my $feed (@feeds) {
586 my ($type, $name, $src) = @$feed;
587 if($feed_name ne $name) {
588 next;
589 }
590 update_feed($type, $name, $src, $perform_update);
591 }
592 }
593 }
594
595 refresh_config();
596
597 return 0;
598 }
599
600 sub usage() {
601 print <<EOF;
602 Usage: $0 <command> [options]
603
604 Commands:
605 list [options]: List feeds, their content and revisions (if installed)
606 Options:
607 -s : List of feed names and their URL.
608 -r <feedname>: List packages of specified feed.
609 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
610
611 install [options] <package>: Install a package
612 Options:
613 -a : Install all packages from all feeds or from the specified feed using the -p option.
614 -p <feedname>: Prefer this feed when installing packages.
615 -d <y|m|n>: Set default for newly installed packages.
616
617 search [options] <substring>: Search for a package
618 Options:
619 -r <feedname>: Only search in this feed
620
621 uninstall -a|<package>: Uninstall a package
622 Options:
623 -a : Uninstalls all packages.
624
625 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
626 Options:
627 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
628 -i : Recreate the index only. No feed update from repository is performed.
629
630 clean: Remove downloaded/generated files.
631
632 EOF
633 exit(1);
634 }
635
636 my %commands = (
637 'list' => \&list,
638 'update' => \&update,
639 'install' => \&install,
640 'search' => \&search,
641 'uninstall' => \&uninstall,
642 'clean' => sub {
643 system("rm -rf feeds");
644 }
645 );
646
647 my $arg = shift @ARGV;
648 $arg or usage();
649 parse_config;
650 foreach my $cmd (keys %commands) {
651 $arg eq $cmd and do {
652 exit(&{$commands{$cmd}}());
653 };
654 }
655 usage();