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