5 use lib "$FindBin::Bin";
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR}=getcwd();
13 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
14 $ENV{GREP_OPTIONS}="";
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'
20 # check version of make
21 my @mkver = split /\s+/, `$mk -v`, 4;
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";
31 my %installed_targets;
34 my $feed_package = {};
42 open FEEDS, "feeds.conf" or
43 open FEEDS, "feeds.conf.default" or
44 die "Unable to open feeds configuration";
49 my @line = split /\s+/, $_, 3;
54 $line[0] =~ /^src-[\w-]+$/ or $valid = 0;
55 $line[1] =~ /^\w+$/ or $valid = 0;
56 @src = split /\s+/, $line[2];
57 $valid or die "Syntax error in feeds.conf, line: $line\n";
59 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
62 push @feeds, [$line[0], $line[1], \@src];
67 sub update_location($$)
73 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
75 if( open LOC, "< ./feeds/$name.tmp/location" )
77 chomp($old_url = readline LOC);
81 if( !$old_url || $old_url ne $url )
83 if( open LOC, "> ./feeds/$name.tmp/location" )
88 return $old_url ? 1 : 0;
98 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
99 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
101 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
102 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\"");
103 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPS=\"profiles/*.mk $ENV{TOPDIR}/include/target.mk\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
104 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
105 system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
110 my %update_method = (
112 'init' => "svn checkout '%s' '%s'",
113 'update' => "svn update",
114 'controldir' => ".svn",
115 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
117 'init' => "cp -Rf '%s' '%s'",
119 'revision' => "echo -n 'local'"},
121 'init' => "ln -s '%s' '%s'",
123 'revision' => "echo -n 'local'"},
125 'init' => "git clone --depth 1 '%s' '%s'",
126 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
127 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
128 'update' => "git pull --ff",
129 'controldir' => ".git",
130 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
132 'init' => "git clone '%s' '%s'",
133 'init_branch' => "git clone --branch '%s' '%s' '%s'",
134 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
135 'update' => "git pull --ff",
136 'controldir' => ".git",
137 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
139 'init' => "git svn clone -r HEAD '%s' '%s'",
140 'update' => "git svn rebase",
141 'controldir' => ".git",
142 'revision' => "git show --abbrev-commit HEAD | head -n 1 | cut -d ' ' -f 2 | tr -d '\n'"},
144 'init' => "bzr checkout --lightweight '%s' '%s'",
145 'update' => "bzr update",
146 'controldir' => ".bzr"},
148 'init' => "hg clone '%s' '%s'",
149 'update' => "hg pull --update",
150 'controldir' => ".hg"},
152 'init' => "darcs get '%s' '%s'",
153 'update' => "darcs pull -a",
154 'controldir' => "_darcs"},
157 # src-git: pull broken
158 # src-cpy: broken if `basename $src` != $name
160 sub update_feed_via($$$$) {
164 my $relocate = shift;
166 my $m = $update_method{$type};
167 my $localpath = "./feeds/$name";
168 my $safepath = $localpath;
169 $safepath =~ s/'/'\\''/;
170 my ($base_branch, $branch) = split(/;/, $src, 2);
171 my ($base_commit, $commit) = split(/\^/, $src, 2);
173 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
174 system("rm -rf '$safepath'");
175 if ($m->{'init_branch'} and $branch) {
176 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
177 } elsif ($m->{'init_commit'} and $commit) {
178 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
180 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
182 } elsif ($m->{'init_commit'} and $commit) {
183 # in case git hash has been provided don't update the feed
185 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
193 my @target = parse_target_metadata($file);
195 foreach my $target (@target) {
196 $target{$target->{id}} = $target;
204 if (!defined($feed_cache{$feed})) {
205 my $file = "./feeds/$feed.index";
209 print "Ignoring feed '$feed' - index missing\n";
212 parse_package_metadata($file) or return;
213 my %target = get_targets("./feeds/$feed.targetindex");
215 $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target } ];
218 $feed_package = $feed_cache{$feed}->[0];
219 $feed_src = $feed_cache{$feed}->[1];
220 $feed_target = $feed_cache{$feed}->[2];
221 return $feed_cache{$feed}->[0];
224 sub get_installed() {
225 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
227 parse_package_metadata("./tmp/.packageinfo");
228 %installed = %package;
229 %installed_targets = get_targets("./tmp/.targetinfo");
237 return unless @substr > 0;
239 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
240 my $pkg = $feed_package->{$name};
244 next if $pkg->{vdepends};
245 foreach my $substr (@substr) {
247 foreach my $key (qw(name title description src)) {
248 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
250 $match or undef $pkgmatch;
254 print "Search results in feed '$feed':\n";
257 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
261 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
262 my $target = $feed_target->{$name};
265 foreach my $substr (@substr) {
267 foreach my $key (qw(id name description)) {
268 $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
270 $match or undef $targetmatch;
272 $targetmatch and do {
274 print "Search results in feed '$feed':\n";
277 printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
286 getopt('r:', \%opts);
287 foreach my $feed (@feeds) {
288 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
296 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
297 my $pkg = $feed_package->{$name};
298 next if $pkg->{vdepends};
300 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
304 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
305 my $target = $feed_target->{$name};
306 if($target->{name}) {
307 printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
317 getopts('r:d:nshf', \%opts);
323 foreach my $feed (@feeds) {
324 printf "%s\n", $feed->[1];
329 foreach my $feed (@feeds) {
330 my $localpath = "./feeds/$feed->[1]";
331 my $m = $update_method{$feed->[0]};
333 if( !$m->{'revision'} ) {
336 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
337 $revision = `cd '$localpath'; $m->{'revision'}`;
343 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
346 my $uri = join(", ", @{$feed->[2]});
347 if ($revision ne "local" && $revision ne "X") {
349 $uri .= "^" . $revision;
351 printf "%s %s %s\n", $feed->[0], $feed->[1], $uri;
354 printf "\%-8s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
359 foreach my $feed (@feeds) {
360 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
365 sub do_install_package($$) {
368 my $path = $pkg->{makefile};
371 $path =~ s/\/Makefile$//;
373 -d "./package/feeds" or mkdir "./package/feeds";
374 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
375 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
377 warn "Package is not valid\n";
384 sub do_install_target($) {
386 my $path = $target->{makefile};
389 $path =~ s/\/Makefile$//;
392 my $dest = "./target/linux/$name";
395 warn "Path $dest already exists";
399 system("ln -sf ../../$path ./target/linux/");
401 warn "Target is not valid\n";
408 sub lookup_package($$) {
412 foreach my $feed ($feed, @feeds) {
413 next unless $feed->[1];
414 next unless $feed_cache{$feed->[1]};
415 $feed_cache{$feed->[1]}->[0]->{$package} and return $feed;
420 sub lookup_target($$) {
424 foreach my $feed ($feed, @feeds) {
425 next unless $feed->[1];
426 next unless $feed_cache{$feed->[1]};
427 $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
432 sub is_core_package($) {
434 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
435 next unless index($file, "tmp/info/.packageinfo-feeds_");
436 return 1 if -s $file;
445 $feed = $feed_cache{$feed->[1]}->[2];
448 my $target = $feed->{$name};
451 warn "Installing target '$name'\n";
452 return do_install_target($target);
455 sub install_package {
461 my $this_feed_target = lookup_target($feed, $name);
462 $this_feed_target and do {
463 $installed_targets{$name} and return 0;
464 install_target($this_feed_target, $name);
468 $feed = lookup_package($feed, $name);
470 $installed{$name} and return 0;
471 # TODO: check if it's already installed within ./package directory
472 $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";
476 # switch to the metadata for the selected feed
477 my $cur = get_feed($feed->[1]);
479 my $pkg = $cur->{$name} or return 1;
481 $installed{$name} and return 0;
482 # TODO: check if this is an alias package, maybe it's known by another name
483 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
486 my $src = $pkg->{src};
487 my $type = $feed->[0];
488 $src or $src = $name;
490 # If it's a core package and we don't want to override, just return
491 !$force and is_core_package($src) and return 0;
493 # previously installed packages set the runtime package
494 # newly installed packages set the source package to 1
495 $installed{$src} and $installed{$src} == 1 and return 0;
497 # we'll trigger the override only with the 3 conditions below:
498 # - override is allowed by command line (-f)
499 # - a package with the same src exists in the core packages list
500 # - the package previously installed is not from a feed
501 my $override = 1 if ($force and is_core_package($src) and !$installed{$name}->{feed});
503 # check previously installed packages
504 $installed{$name} and !$override and return 0;
505 $installed{$src} = 1;
507 defined($override) and $override == 1
508 and warn "Overriding core package '$src' with version from $feed->[1]\n"
509 or warn "Installing package '$src' from $feed->[1]\n";
511 do_install_package($feed, $pkg) == 0 or do {
516 # install all dependencies referenced from the source package
517 foreach my $vpkg (@{$feed_src->{$src}}) {
518 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
524 install_package($feed, $dep, 0) == 0 or $ret = 1;
534 # workaround for timestamp check
535 system("rm -f tmp/.packageinfo");
539 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
541 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
551 getopts('ap:d:fh', \%opts);
560 foreach my $f (@feeds) {
564 # look up the preferred feed
565 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
569 foreach my $f (@feeds) {
570 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
571 printf "Installing all packages from feed %s.\n", $f->[1];
573 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
574 my $p = $feed_package->{$name};
575 next if $p->{vdepends};
577 install_package($feed, $p->{name}, exists($opts{f})) == 0 or $ret = 1;
584 while ($name = shift @ARGV) {
585 install_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
589 # workaround for timestamp check
592 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
593 refresh_config($opts{d});
599 sub uninstall_target($) {
604 my $dest = readlink $dir;
605 return unless $dest =~ /..\/..\/feeds/;
606 warn "Uninstalling target '$name'\n";
615 getopts('ah', \%opts);
623 system("rm -rvf ./package/feeds");
624 foreach my $dir (glob "target/linux/*") {
626 uninstall_target($dir);
631 warn "WARNING: no package to uninstall\n";
635 while ($name = shift @ARGV) {
636 my $target = "target/linux/$name";
637 -l "$target" and do {
638 uninstall_target($target);
643 my $pkg = $installed{$name};
645 warn "WARNING: $name not installed\n";
648 $pkg->{src} and $name = $pkg->{src};
649 warn "Uninstalling package '$name'\n";
650 system("rm -f ./package/feeds/*/$name");
654 $uninstall and refresh_config();
658 sub update_feed($$$$)
663 my $perform_update=shift;
664 my $force_relocate=update_location( $name, "@$src" );
666 if( $force_relocate ) {
667 warn "Source of feed $name has changed, replacing copy\n";
669 $update_method{$type} or do {
670 warn "Unknown type '$type' in feed $name\n";
673 $perform_update and do {
675 foreach my $feedsrc (@$src) {
676 warn "Updating feed '$name' from '$feedsrc' ...\n";
677 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
686 warn "Create index file './feeds/$name.index' \n";
687 update_index($name) == 0 or do {
697 my $perform_update=1;
700 $ENV{SCAN_COOKIE} = $$;
701 $ENV{OPENWRT_VERBOSE} = 's';
703 getopts('ahi', \%opts);
711 # don't update from (remote) repository
712 # only re-create index information
717 mkdir "feeds" or die "Unable to create the feeds directory";
720 if ( ($#ARGV == -1) or $opts{a}) {
721 foreach my $feed (@feeds) {
722 my ($type, $name, $src) = @$feed;
723 update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
726 while ($feed_name = shift @ARGV) {
727 foreach my $feed (@feeds) {
728 my ($type, $name, $src) = @$feed;
729 if($feed_name ne $name) {
732 update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
743 foreach my $feed (@feeds) {
744 my $installed = (-f "feeds/$feed->[1].index");
746 printf "\tconfig FEED_%s\n", $feed->[1];
747 printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
748 printf "\t\tdepends on PER_FEED_REPO\n";
749 printf "\t\tdefault y\n" if $installed;
751 printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
760 Usage: $0 <command> [options]
763 list [options]: List feeds, their content and revisions (if installed)
765 -n : List of feed names.
766 -s : List of feed names and their URL.
767 -r <feedname>: List packages of specified feed.
768 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
769 -f : List feeds in feeds.conf compatible format (when using -s).
771 install [options] <package>: Install a package
773 -a : Install all packages from all feeds or from the specified feed using the -p option.
774 -p <feedname>: Prefer this feed when installing packages.
775 -d <y|m|n>: Set default for newly installed packages.
776 -f : Install will be forced even if the package exists in core OpenWrt (override)
778 search [options] <substring>: Search for a package
780 -r <feedname>: Only search in this feed
782 uninstall -a|<package>: Uninstall a package
784 -a : Uninstalls all packages.
786 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
788 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
789 -i : Recreate the index only. No feed update from repository is performed.
791 clean: Remove downloaded/generated files.
799 'update' => \&update,
800 'install' => \&install,
801 'search' => \&search,
802 'uninstall' => \&uninstall,
803 'feed_config' => \&feed_config,
805 system("rm -rf ./feeds ./package/feeds");
809 my $arg = shift @ARGV;
812 foreach my $cmd (keys %commands) {
813 $arg eq $cmd and do {
814 exit(&{$commands{$cmd}}());