5 use lib "$FindBin::Bin";
11 chdir "$FindBin::Bin/..";
12 $ENV{TOPDIR} //= getcwd();
14 $ENV{GIT_CONFIG_PARAMETERS}="'core.autocrlf=false'";
15 $ENV{GREP_OPTIONS}="";
17 my $mk=`which gmake 2>/dev/null`; # select the right 'make' program
18 chomp($mk); # trim trailing newline
19 $mk or $mk = "make"; # default to 'make'
21 # check version of make
22 my @mkver = split /\s+/, `$mk -v`, 4;
24 $mkver[0] =~ /^GNU/ or $valid_mk = 0;
25 $mkver[1] =~ /^Make/ or $valid_mk = 0;
27 my ($mkv1, $mkv2) = split /\./, $mkver[2];
28 ($mkv1 >= 4 || ($mkv1 == 3 && $mkv2 >= 81)) or $valid_mk = 0;
30 $valid_mk or die "Unsupported version of make found: $mk\n";
36 my %installed_targets;
39 my $feed_package = {};
42 my $feed_vpackage = {};
47 my ($fname, $existing) = @_;
51 open $fh, $fname or return undef;
58 my ($type, $flags, $name, $urls) = m!^src-([\w\-]+)((?:\s+--\w+(?:=\S+)?)*)\s+(\w+)(?:\s+(\S.*))?$!;
59 unless ($type && $name) {
60 die "Syntax error in $fname, line $line\n";
63 if ($existing->{$name}++) {
64 die "Duplicate feed name '$name' in '$fname' line: $line\n";
67 my @src = defined($urls) ? split /\s+/, $urls : ();
68 push @src, '' if @src == 0;
72 while ($flags =~ m!\s+--(\w+)(?:=(\S+))?!g) {
73 $flags{$1} = defined($2) ? $2 : 1;
77 if ($type eq "include") {
78 parse_file($urls, $existing) or
79 die "Unable to open included file '$urls'";
83 push @feeds, ["src-$type", $name, \@src, \%flags];
91 parse_file("feeds.conf", \%name) or
92 parse_file("feeds.conf.default", \%name) or
93 die "Unable to open feeds configuration";
96 sub update_location($$)
102 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
104 if( open LOC, "< ./feeds/$name.tmp/location" )
106 chomp($old_url = readline LOC);
110 if( !$old_url || $old_url ne $url )
112 if( open LOC, "> ./feeds/$name.tmp/location" )
114 print LOC $url, "\n";
117 return $old_url ? 1 : 0;
127 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
128 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
130 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
131 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"packageinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"package\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
132 system("$mk -s -f include/scan.mk IS_TTY=1 SCAN_TARGET=\"targetinfo\" SCAN_DIR=\"feeds/$name\" SCAN_NAME=\"target\" SCAN_DEPTH=5 SCAN_EXTRA=\"\" SCAN_MAKEOPTS=\"TARGET_BUILD=1\" TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
133 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
134 system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
139 my %update_method = (
141 'init' => "svn checkout '%s' '%s'",
142 'update' => "svn update",
143 'controldir' => ".svn",
144 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
146 'init' => "cp -Rf '%s' '%s'",
148 'revision' => "echo -n 'local'"},
150 'init' => "ln -s '%s' '%s'",
152 'revision' => "echo -n 'local'"},
154 'init' => "true '%s' && mkdir '%s'",
156 'revision' => "echo -n 'dummy'"},
158 'init' => "git clone --depth 1 '%s' '%s'",
159 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
160 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
161 'update' => "git pull --ff",
162 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; exit 1)",
163 'post_update' => "git submodule update --init --recursive",
164 'controldir' => ".git",
165 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
167 'init' => "git clone '%s' '%s'",
168 'init_branch' => "git clone --branch '%s' '%s' '%s'",
169 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
170 'update' => "git pull --ff",
171 'update_force' => "git pull --ff || (git reset --hard HEAD; git pull --ff; exit 1)",
172 'post_update' => "git submodule update --init --recursive",
173 'controldir' => ".git",
174 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
176 'init' => "git svn clone -r HEAD '%s' '%s'",
177 'update' => "git svn rebase",
178 'controldir' => ".git",
179 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
181 'init' => "bzr checkout --lightweight '%s' '%s'",
182 'update' => "bzr update",
183 'controldir' => ".bzr"},
185 'init' => "hg clone '%s' '%s'",
186 'update' => "hg pull --update",
187 'controldir' => ".hg"},
189 'init' => "darcs get '%s' '%s'",
190 'update' => "darcs pull -a",
191 'controldir' => "_darcs"},
194 # src-git: pull broken
195 # src-cpy: broken if `basename $src` != $name
197 sub update_feed_via($$$$$) {
201 my $relocate = shift;
204 my $m = $update_method{$type};
205 my $localpath = "./feeds/$name";
206 my $safepath = $localpath;
207 $safepath =~ s/'/'\\''/;
208 my ($base_branch, $branch) = split(/;/, $src, 2);
209 my ($base_commit, $commit) = split(/\^/, $src, 2);
211 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
212 system("rm -rf '$safepath'");
213 if ($m->{'init_branch'} and $branch) {
214 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
215 } elsif ($m->{'init_commit'} and $commit) {
216 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
218 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
220 } elsif ($m->{'init_commit'} and $commit) {
221 # in case git hash has been provided don't update the feed
223 my $update_cmd = $m->{'update'};
224 if ($force && exists $m->{'update_force'}) {
225 $update_cmd = $m->{'update_force'};
227 system("cd '$safepath'; $update_cmd") == 0 or return 1;
229 if ($m->{'post_update'}) {
230 my $cmd = $m->{'post_update'};
231 system("cd '$safepath'; $cmd") == 0 or return 1;
239 my @target = parse_target_metadata($file);
241 foreach my $target (@target) {
242 $target{$target->{id}} = $target;
250 if (!defined($feed_cache{$feed})) {
251 my $file = "./feeds/$feed.index";
255 print "Ignoring feed '$feed' - index missing\n";
258 parse_package_metadata($file) or return;
259 my %target = get_targets("./feeds/$feed.targetindex");
261 $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target }, { %vpackage } ];
264 $feed_package = $feed_cache{$feed}->[0];
265 $feed_src = $feed_cache{$feed}->[1];
266 $feed_target = $feed_cache{$feed}->[2];
267 $feed_vpackage = $feed_cache{$feed}->[3];
270 sub get_installed() {
271 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
273 parse_package_metadata("./tmp/.packageinfo");
274 %installed_pkg = %vpackage;
275 %installed = %srcpackage;
276 %installed_targets = get_targets("./tmp/.targetinfo");
284 return unless @substr > 0;
286 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
287 my $pkg = $feed_package->{$name};
291 foreach my $substr (@substr) {
293 foreach my $key (qw(name title description src)) {
294 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
296 $match or undef $pkgmatch;
300 print "Search results in feed '$feed':\n";
303 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
307 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
308 my $target = $feed_target->{$name};
311 foreach my $substr (@substr) {
313 foreach my $key (qw(id name description)) {
314 $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
316 $match or undef $targetmatch;
318 $targetmatch and do {
320 print "Search results in feed '$feed':\n";
323 printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
332 getopt('r:', \%opts);
333 foreach my $feed (@feeds) {
334 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
342 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
343 my $pkg = $feed_package->{$name};
345 printf "\%-32s\t\%s\n", $pkg->{name}, $pkg->{title};
349 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
350 my $target = $feed_target->{$name};
351 if($target->{name}) {
352 printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
362 getopts('r:d:nshf', \%opts);
368 foreach my $feed (@feeds) {
369 printf "%s\n", $feed->[1];
374 foreach my $feed (@feeds) {
375 my $localpath = "./feeds/$feed->[1]";
376 my $m = $update_method{$feed->[0]};
378 if (!-d "$localpath" || !$m->{'revision'}) {
381 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
382 $revision = `cd '$localpath'; $m->{'revision'}`;
388 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
391 my $uri = join(", ", @{$feed->[2]});
392 if ($revision ne "local" && $revision ne "X") {
394 $uri .= "^" . $revision;
396 printf "%s %s %s\n", $feed->[0], $feed->[1], $uri;
399 printf "\%-10s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
404 foreach my $feed (@feeds) {
405 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
410 sub do_install_src($$) {
414 my $path = $src->{makefile};
416 $path =~ s/\/Makefile$//;
418 -d "./package/feeds" or mkdir "./package/feeds";
419 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
420 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
422 warn "Package is not valid\n";
429 sub do_install_target($) {
431 my $path = $target->{makefile};
434 $path =~ s/\/Makefile$//;
437 my $dest = "./target/linux/$name";
440 warn "Path $dest already exists";
444 system("ln -sf ../../$path ./target/linux/");
446 warn "Target is not valid\n";
457 foreach my $feed ($feed, @feeds) {
458 next unless $feed->[1];
459 next unless $feed_cache{$feed->[1]};
460 $feed_cache{$feed->[1]}->[1]->{$src} and return $feed;
465 sub lookup_package($$) {
469 foreach my $feed ($feed, @feeds) {
470 next unless $feed->[1];
471 next unless $feed_cache{$feed->[1]};
472 $feed_cache{$feed->[1]}->[3]->{$package} and return $feed;
477 sub lookup_target($$) {
481 foreach my $feed ($feed, @feeds) {
482 next unless $feed->[1];
483 next unless $feed_cache{$feed->[1]};
484 $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
491 foreach my $file ("tmp/info/.packageinfo-$src", glob("tmp/info/.packageinfo-*_$src")) {
492 next unless index($file, "tmp/info/.packageinfo-feeds_");
493 return 1 if -s $file;
502 $installed_targets{$name} and return 0;
504 $feed = $feed_cache{$feed->[1]}->[2];
507 my $target = $feed->{$name};
510 warn "Installing target '$name'\n";
511 return do_install_target($target);
520 $feed = lookup_src($feed, $name);
522 $installed{$name} and return 0;
523 $feed_src->{$name} or warn "WARNING: No feed for source package '$name' found\n";
527 # switch to the metadata for the selected feed
528 get_feed($feed->[1]);
529 my $src = $feed_src->{$name} or return 1;
531 # enable force flag if feed src line was declared with --force
532 if (exists($feed->[3]{force})) {
536 # If it's a core package and we don't want to override, just return
538 if (is_core_src($name)) {
539 return 0 unless $force;
543 if ($installed{$name}) {
544 # newly installed packages set the source package to 1
545 return 0 if ($installed{$name} == 1);
546 return 0 unless ($override);
549 $installed{$name} = 1;
550 foreach my $pkg (@{$src->{packages}}) {
551 foreach my $vpkg (@{$pkg->{provides}}) {
552 $installed_pkg{$vpkg} = 1;
557 warn "Overriding core package '$name' with version from $feed->[1]\n";
559 warn "Installing package '$name' from $feed->[1]\n";
562 do_install_src($feed, $src) == 0 or do {
567 # install all dependencies referenced from the source package
569 @{$src->{builddepends}},
570 @{$src->{'builddepends/host'}},
576 install_src($feed, $dep, 0) == 0 or $ret = 1;
579 foreach my $pkg (@{$src->{packages}}) {
580 foreach my $dep (@{$pkg->{depends}}) {
585 install_package($feed, $dep, 0) == 0 or $ret = 1;
592 sub install_package {
597 $feed = lookup_package($feed, $name);
599 $installed_pkg{$name} and return 0;
600 $feed_vpackage->{$name} or warn "WARNING: No feed for package '$name' found\n";
604 # switch to the metadata for the selected feed
605 get_feed($feed->[1]);
606 my $pkg = $feed_vpackage->{$name} or return 1;
607 return install_src($feed, $pkg->[0]{src}{name}, $force);
610 sub install_target_or_package {
615 my $this_feed_target = lookup_target($feed, $name);
616 $this_feed_target and do {
617 return install_target($this_feed_target, $name);
620 my $this_feed_src = lookup_src($feed, $name);
621 $this_feed_src and do {
622 return install_src($this_feed_src, $name, $force);
625 return install_package($feed, $name, $force);
631 # Don't create .config if it doesn't already exist so that making a
632 # config only occurs when the user intends it do (however we do
633 # want to refresh an existing config).
634 return if not (-e '.config');
636 # workaround for timestamp check
637 system("rm -f tmp/.packageinfo");
641 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
643 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
653 getopts('ap:d:fh', \%opts);
662 foreach my $f (@feeds) {
666 # look up the preferred feed
667 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
671 foreach my $f (@feeds) {
672 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
673 printf "Installing all packages from feed %s.\n", $f->[1];
675 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_src) {
676 install_src($feed, $name, exists($opts{f})) == 0 or $ret = 1;
682 while ($name = shift @ARGV) {
683 install_target_or_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
687 # workaround for timestamp check
690 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
691 refresh_config($opts{d});
697 sub uninstall_target($) {
702 my $dest = readlink $dir;
703 return unless $dest =~ /..\/..\/feeds/;
704 warn "Uninstalling target '$name'\n";
713 getopts('ah', \%opts);
721 system("rm -rvf ./package/feeds");
722 foreach my $dir (glob "target/linux/*") {
724 uninstall_target($dir);
729 warn "WARNING: no package to uninstall\n";
733 while ($name = shift @ARGV) {
734 my $target = "target/linux/$name";
735 -l "$target" and do {
736 uninstall_target($target);
741 my $pkg = $installed{$name};
743 warn "WARNING: $name not installed\n";
746 $pkg->{src} and $name = $pkg->{src}{name};
747 warn "Uninstalling package '$name'\n";
748 system("rm -f ./package/feeds/*/$name");
752 $uninstall and refresh_config();
756 sub update_feed($$$$$)
761 my $perform_update=shift;
762 my $force_update=shift;
763 my $force_relocate=update_location( $name, "@$src" );
766 if( $force_relocate ) {
767 warn "Source of feed $name has changed, replacing copy\n";
769 $update_method{$type} or do {
770 warn "Unknown type '$type' in feed $name\n";
773 $perform_update and do {
775 foreach my $feedsrc (@$src) {
776 warn "Updating feed '$name' from '$feedsrc' ...\n";
777 if (update_feed_via($type, $name, $feedsrc, $force_relocate, $force_update) != 0) {
781 warn "failed, ignore.\n";
793 warn "Create index file './feeds/$name.index' \n";
794 update_index($name) == 0 or do {
804 my $perform_update=1;
807 $ENV{SCAN_COOKIE} = $$;
808 $ENV{OPENWRT_VERBOSE} = 's';
810 getopts('ahif', \%opts);
818 # don't update from (remote) repository
819 # only re-create index information
824 mkdir "feeds" or die "Unable to create the feeds directory";
827 if ( ($#ARGV == -1) or $opts{a}) {
828 foreach my $feed (@feeds) {
829 my ($type, $name, $src) = @$feed;
830 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
833 while ($feed_name = shift @ARGV) {
834 foreach my $feed (@feeds) {
835 my ($type, $name, $src) = @$feed;
836 if($feed_name ne $name) {
839 update_feed($type, $name, $src, $perform_update, $opts{f}) == 0 or $failed=1;
850 foreach my $feed (@feeds) {
851 my $installed = (-f "feeds/$feed->[1].index");
853 printf "\tconfig FEED_%s\n", $feed->[1];
854 printf "\t\ttristate \"Enable feed %s\"\n", $feed->[1];
855 printf "\t\tdepends on PER_FEED_REPO\n";
856 printf "\t\tdefault y\n" if $installed;
858 printf "\t\t Enable the \\\"%s\\\" feed in opkg distfeeds.conf.\n", $feed->[1];
859 printf "\t\t Say M to add the feed commented out.\n";
868 Usage: $0 <command> [options]
871 list [options]: List feeds, their content and revisions (if installed)
873 -n : List of feed names.
874 -s : List of feed names and their URL.
875 -r <feedname>: List packages of specified feed.
876 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
877 -f : List feeds in feeds.conf compatible format (when using -s).
879 install [options] <package>: Install a package
881 -a : Install all packages from all feeds or from the specified feed using the -p option.
882 -p <feedname>: Prefer this feed when installing packages.
883 -d <y|m|n>: Set default for newly installed packages.
884 -f : Install will be forced even if the package exists in core OpenWrt (override)
886 search [options] <substring>: Search for a package
888 -r <feedname>: Only search in this feed
890 uninstall -a|<package>: Uninstall a package
892 -a : Uninstalls all packages.
894 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
896 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
897 -i : Recreate the index only. No feed update from repository is performed.
898 -f : Force updating feeds even if there are changed, uncommitted files.
900 clean: Remove downloaded/generated files.
908 'update' => \&update,
909 'install' => \&install,
910 'search' => \&search,
911 'uninstall' => \&uninstall,
912 'feed_config' => \&feed_config,
914 system("rm -rf ./feeds ./package/feeds");
918 my $arg = shift @ARGV;
921 foreach my $cmd (keys %commands) {
922 $arg eq $cmd and do {
923 exit(&{$commands{$cmd}}());