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;
26 my ($mkv1, $mkv2) = split /\./, $mkver[2];
27 ($mkv1 >= 4 || ($mkv1 == 3 && $mkv2 >= 81)) or $valid_mk = 0;
29 $valid_mk or die "Unsupported version of make found: $mk\n";
34 my %installed_targets;
37 my $feed_package = {};
45 open FEEDS, "feeds.conf" or
46 open FEEDS, "feeds.conf.default" or
47 die "Unable to open feeds configuration";
52 my @line = split /\s+/, $_, 3;
57 $line[0] =~ /^src-[\w-]+$/ or $valid = 0;
58 $line[1] =~ /^\w+$/ or $valid = 0;
59 @src = split /\s+/, $line[2];
60 $valid or die "Syntax error in feeds.conf, line: $line\n";
62 $name{$line[1]} and die "Duplicate feed name '$line[1]', line: $line\n";
65 push @feeds, [$line[0], $line[1], \@src];
70 sub update_location($$)
76 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
78 if( open LOC, "< ./feeds/$name.tmp/location" )
80 chomp($old_url = readline LOC);
84 if( !$old_url || $old_url ne $url )
86 if( open LOC, "> ./feeds/$name.tmp/location" )
91 return $old_url ? 1 : 0;
101 -d "./feeds/$name.tmp" or mkdir "./feeds/$name.tmp" or return 1;
102 -d "./feeds/$name.tmp/info" or mkdir "./feeds/$name.tmp/info" or return 1;
104 system("$mk -s prepare-mk OPENWRT_BUILD= TMP_DIR=\"$ENV{TOPDIR}/feeds/$name.tmp\"");
105 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\"");
106 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\"");
107 system("ln -sf $name.tmp/.packageinfo ./feeds/$name.index");
108 system("ln -sf $name.tmp/.targetinfo ./feeds/$name.targetindex");
113 my %update_method = (
115 'init' => "svn checkout '%s' '%s'",
116 'update' => "svn update",
117 'controldir' => ".svn",
118 'revision' => "svn info | grep 'Revision' | cut -d ' ' -f 2 | tr -d '\n'"},
120 'init' => "cp -Rf '%s' '%s'",
122 'revision' => "echo -n 'local'"},
124 'init' => "ln -s '%s' '%s'",
126 'revision' => "echo -n 'local'"},
128 'init' => "git clone --depth 1 '%s' '%s'",
129 'init_branch' => "git clone --depth 1 --branch '%s' '%s' '%s'",
130 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
131 'update' => "git pull --ff",
132 'controldir' => ".git",
133 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
135 'init' => "git clone '%s' '%s'",
136 'init_branch' => "git clone --branch '%s' '%s' '%s'",
137 'init_commit' => "git clone '%s' '%s' && cd '%s' && git checkout -b '%s' '%s' && cd -",
138 'update' => "git pull --ff",
139 'controldir' => ".git",
140 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
142 'init' => "git svn clone -r HEAD '%s' '%s'",
143 'update' => "git svn rebase",
144 'controldir' => ".git",
145 'revision' => "git rev-parse --short HEAD | tr -d '\n'"},
147 'init' => "bzr checkout --lightweight '%s' '%s'",
148 'update' => "bzr update",
149 'controldir' => ".bzr"},
151 'init' => "hg clone '%s' '%s'",
152 'update' => "hg pull --update",
153 'controldir' => ".hg"},
155 'init' => "darcs get '%s' '%s'",
156 'update' => "darcs pull -a",
157 'controldir' => "_darcs"},
160 # src-git: pull broken
161 # src-cpy: broken if `basename $src` != $name
163 sub update_feed_via($$$$) {
167 my $relocate = shift;
169 my $m = $update_method{$type};
170 my $localpath = "./feeds/$name";
171 my $safepath = $localpath;
172 $safepath =~ s/'/'\\''/;
173 my ($base_branch, $branch) = split(/;/, $src, 2);
174 my ($base_commit, $commit) = split(/\^/, $src, 2);
176 if( $relocate || !$m->{'update'} || !-d "$localpath/$m->{'controldir'}" ) {
177 system("rm -rf '$safepath'");
178 if ($m->{'init_branch'} and $branch) {
179 system(sprintf($m->{'init_branch'}, $branch, $base_branch, $safepath)) == 0 or return 1;
180 } elsif ($m->{'init_commit'} and $commit) {
181 system(sprintf($m->{'init_commit'}, $base_commit, $safepath, $safepath, $commit, $commit)) == 0 or return 1;
183 system(sprintf($m->{'init'}, $src, $safepath)) == 0 or return 1;
185 } elsif ($m->{'init_commit'} and $commit) {
186 # in case git hash has been provided don't update the feed
188 system("cd '$safepath'; $m->{'update'}") == 0 or return 1;
196 my @target = parse_target_metadata($file);
198 foreach my $target (@target) {
199 $target{$target->{id}} = $target;
207 if (!defined($feed_cache{$feed})) {
208 my $file = "./feeds/$feed.index";
212 print "Ignoring feed '$feed' - index missing\n";
215 parse_package_metadata($file) or return;
216 my %target = get_targets("./feeds/$feed.targetindex");
218 $feed_cache{$feed} = [ { %package }, { %srcpackage }, { %target } ];
221 $feed_package = $feed_cache{$feed}->[0];
222 $feed_src = $feed_cache{$feed}->[1];
223 $feed_target = $feed_cache{$feed}->[2];
224 return $feed_cache{$feed}->[0];
227 sub get_installed() {
228 system("$mk -s prepare-tmpinfo OPENWRT_BUILD=");
230 parse_package_metadata("./tmp/.packageinfo");
231 %installed = %package;
232 %installed_targets = get_targets("./tmp/.targetinfo");
240 return unless @substr > 0;
242 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
243 my $pkg = $feed_package->{$name};
247 next if $pkg->{vdepends};
248 foreach my $substr (@substr) {
250 foreach my $key (qw(name title description src)) {
251 $pkg->{$key} and $substr and $pkg->{$key} =~ m/$substr/i and $match = 1;
253 $match or undef $pkgmatch;
257 print "Search results in feed '$feed':\n";
260 printf "\%-25s\t\%s\n", $pkg->{name}, $pkg->{title};
264 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_target) {
265 my $target = $feed_target->{$name};
268 foreach my $substr (@substr) {
270 foreach my $key (qw(id name description)) {
271 $target->{$key} and $substr and $target->{$key} =~ m/$substr/i and $match = 1;
273 $match or undef $targetmatch;
275 $targetmatch and do {
277 print "Search results in feed '$feed':\n";
280 printf "TARGET: \%-17s\t\%s\n", $target->{id}, $target->{name};
289 getopt('r:', \%opts);
290 foreach my $feed (@feeds) {
291 search_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
299 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
300 my $pkg = $feed_package->{$name};
301 next if $pkg->{vdepends};
303 printf "\%-32s\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};
309 if($target->{name}) {
310 printf "TARGET: \%-24s\t\%s\n", $target->{id}, $target->{name};
320 getopts('r:d:nshf', \%opts);
326 foreach my $feed (@feeds) {
327 printf "%s\n", $feed->[1];
332 foreach my $feed (@feeds) {
333 my $localpath = "./feeds/$feed->[1]";
334 my $m = $update_method{$feed->[0]};
336 if (!-d "$localpath" || !$m->{'revision'}) {
339 elsif( $m->{'controldir'} && -d "$localpath/$m->{'controldir'}" ) {
340 $revision = `cd '$localpath'; $m->{'revision'}`;
346 printf "%s%s%s%s%s%s%s\n", $feed->[1], $opts{d}, $feed->[0], $opts{d}, $revision, $opts{d}, join(", ", @{$feed->[2]});
349 my $uri = join(", ", @{$feed->[2]});
350 if ($revision ne "local" && $revision ne "X") {
352 $uri .= "^" . $revision;
354 printf "%s %s %s\n", $feed->[0], $feed->[1], $uri;
357 printf "\%-10s \%-8s \%-8s \%s\n", $feed->[1], $feed->[0], $revision, join(", ", @{$feed->[2]});
362 foreach my $feed (@feeds) {
363 list_feed($feed->[1], @ARGV) if (!defined($opts{r}) or $opts{r} eq $feed->[1]);
368 sub do_install_package($$) {
371 my $path = $pkg->{makefile};
374 $path =~ s/\/Makefile$//;
376 -d "./package/feeds" or mkdir "./package/feeds";
377 -d "./package/feeds/$feed->[1]" or mkdir "./package/feeds/$feed->[1]";
378 system("ln -sf ../../../$path ./package/feeds/$feed->[1]/");
380 warn "Package is not valid\n";
387 sub do_install_target($) {
389 my $path = $target->{makefile};
392 $path =~ s/\/Makefile$//;
395 my $dest = "./target/linux/$name";
398 warn "Path $dest already exists";
402 system("ln -sf ../../$path ./target/linux/");
404 warn "Target is not valid\n";
411 sub lookup_package($$) {
415 foreach my $feed ($feed, @feeds) {
416 next unless $feed->[1];
417 next unless $feed_cache{$feed->[1]};
418 $feed_cache{$feed->[1]}->[0]->{$package} and return $feed;
423 sub lookup_target($$) {
427 foreach my $feed ($feed, @feeds) {
428 next unless $feed->[1];
429 next unless $feed_cache{$feed->[1]};
430 $feed_cache{$feed->[1]}->[2]->{$target} and return $feed;
435 sub is_core_package($) {
437 foreach my $file ("tmp/info/.packageinfo-$package", glob("tmp/info/.packageinfo-*_$package")) {
438 next unless index($file, "tmp/info/.packageinfo-feeds_");
439 return 1 if -s $file;
448 $feed = $feed_cache{$feed->[1]}->[2];
451 my $target = $feed->{$name};
454 warn "Installing target '$name'\n";
455 return do_install_target($target);
458 sub install_package {
464 my $this_feed_target = lookup_target($feed, $name);
465 $this_feed_target and do {
466 $installed_targets{$name} and return 0;
467 install_target($this_feed_target, $name);
471 $feed = lookup_package($feed, $name);
473 $installed{$name} and return 0;
474 # TODO: check if it's already installed within ./package directory
475 $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";
479 # switch to the metadata for the selected feed
480 my $cur = get_feed($feed->[1]);
482 my $pkg = $cur->{$name} or return 1;
484 $installed{$name} and return 0;
485 # TODO: check if this is an alias package, maybe it's known by another name
486 warn "WARNING: Package '$name' is not available in feed $feed->[1].\n";
489 my $src = $pkg->{src};
490 my $type = $feed->[0];
491 $src or $src = $name;
493 # If it's a core package and we don't want to override, just return
494 !$force and is_core_package($src) and return 0;
496 # previously installed packages set the runtime package
497 # newly installed packages set the source package to 1
498 $installed{$src} and $installed{$src} == 1 and return 0;
500 # we'll trigger the override only with the 3 conditions below:
501 # - override is allowed by command line (-f)
502 # - a package with the same src exists in the core packages list
503 # - the package previously installed is not from a feed
504 my $override = 1 if ($force and is_core_package($src) and !$installed{$name}->{feed});
506 # check previously installed packages
507 $installed{$name} and !$override and return 0;
508 $installed{$src} = 1;
510 defined($override) and $override == 1
511 and warn "Overriding core package '$src' with version from $feed->[1]\n"
512 or warn "Installing package '$src' from $feed->[1]\n";
514 do_install_package($feed, $pkg) == 0 or do {
519 # install all dependencies referenced from the source package
520 foreach my $vpkg (@{$feed_src->{$src}}) {
521 foreach my $dep (@{$vpkg->{depends}}, @{$vpkg->{builddepends}}, @{$vpkg->{"builddepends/host"}}) {
527 install_package($feed, $dep, 0) == 0 or $ret = 1;
537 # Don't create .config if it doesn't already exist so that making a
538 # config only occurs when the user intends it do (however we do
539 # want to refresh an existing config).
540 return if not (-e '.config');
542 # workaround for timestamp check
543 system("rm -f tmp/.packageinfo");
547 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
549 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
559 getopts('ap:d:fh', \%opts);
568 foreach my $f (@feeds) {
572 # look up the preferred feed
573 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
577 foreach my $f (@feeds) {
578 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
579 printf "Installing all packages from feed %s.\n", $f->[1];
581 foreach my $name (sort { lc($a) cmp lc($b) } keys %$feed_package) {
582 my $p = $feed_package->{$name};
583 next if $p->{vdepends};
585 install_package($feed, $p->{name}, exists($opts{f})) == 0 or $ret = 1;
592 while ($name = shift @ARGV) {
593 install_package($feed, $name, exists($opts{f})) == 0 or $ret = 1;
597 # workaround for timestamp check
600 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
601 refresh_config($opts{d});
607 sub uninstall_target($) {
612 my $dest = readlink $dir;
613 return unless $dest =~ /..\/..\/feeds/;
614 warn "Uninstalling target '$name'\n";
623 getopts('ah', \%opts);
631 system("rm -rvf ./package/feeds");
632 foreach my $dir (glob "target/linux/*") {
634 uninstall_target($dir);
639 warn "WARNING: no package to uninstall\n";
643 while ($name = shift @ARGV) {
644 my $target = "target/linux/$name";
645 -l "$target" and do {
646 uninstall_target($target);
651 my $pkg = $installed{$name};
653 warn "WARNING: $name not installed\n";
656 $pkg->{src} and $name = $pkg->{src};
657 warn "Uninstalling package '$name'\n";
658 system("rm -f ./package/feeds/*/$name");
662 $uninstall and refresh_config();
666 sub update_feed($$$$)
671 my $perform_update=shift;
672 my $force_relocate=update_location( $name, "@$src" );
674 if( $force_relocate ) {
675 warn "Source of feed $name has changed, replacing copy\n";
677 $update_method{$type} or do {
678 warn "Unknown type '$type' in feed $name\n";
681 $perform_update and do {
683 foreach my $feedsrc (@$src) {
684 warn "Updating feed '$name' from '$feedsrc' ...\n";
685 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
694 warn "Create index file './feeds/$name.index' \n";
695 update_index($name) == 0 or do {
705 my $perform_update=1;
708 $ENV{SCAN_COOKIE} = $$;
709 $ENV{OPENWRT_VERBOSE} = 's';
711 getopts('ahi', \%opts);
719 # don't update from (remote) repository
720 # only re-create index information
725 mkdir "feeds" or die "Unable to create the feeds directory";
728 if ( ($#ARGV == -1) or $opts{a}) {
729 foreach my $feed (@feeds) {
730 my ($type, $name, $src) = @$feed;
731 update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
734 while ($feed_name = shift @ARGV) {
735 foreach my $feed (@feeds) {
736 my ($type, $name, $src) = @$feed;
737 if($feed_name ne $name) {
740 update_feed($type, $name, $src, $perform_update) == 0 or $failed=1;
751 foreach my $feed (@feeds) {
752 my $installed = (-f "feeds/$feed->[1].index");
754 printf "\tconfig FEED_%s\n", $feed->[1];
755 printf "\t\tbool \"Enable feed %s\"\n", $feed->[1];
756 printf "\t\tdepends on PER_FEED_REPO\n";
757 printf "\t\tdefault y\n" if $installed;
759 printf "\t\t Enable the \\\"%s\\\" feed at %s.\n", $feed->[1], $feed->[2][0];
768 Usage: $0 <command> [options]
771 list [options]: List feeds, their content and revisions (if installed)
773 -n : List of feed names.
774 -s : List of feed names and their URL.
775 -r <feedname>: List packages of specified feed.
776 -d <delimiter>: Use specified delimiter to distinguish rows (default: spaces)
777 -f : List feeds in feeds.conf compatible format (when using -s).
779 install [options] <package>: Install a package
781 -a : Install all packages from all feeds or from the specified feed using the -p option.
782 -p <feedname>: Prefer this feed when installing packages.
783 -d <y|m|n>: Set default for newly installed packages.
784 -f : Install will be forced even if the package exists in core OpenWrt (override)
786 search [options] <substring>: Search for a package
788 -r <feedname>: Only search in this feed
790 uninstall -a|<package>: Uninstall a package
792 -a : Uninstalls all packages.
794 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
796 -a : Update all feeds listed within feeds.conf. Otherwise the specified feeds will be updated.
797 -i : Recreate the index only. No feed update from repository is performed.
799 clean: Remove downloaded/generated files.
807 'update' => \&update,
808 'install' => \&install,
809 'search' => \&search,
810 'uninstall' => \&uninstall,
811 'feed_config' => \&feed_config,
813 system("rm -rf ./feeds ./package/feeds");
817 my $arg = shift @ARGV;
820 foreach my $cmd (keys %commands) {
821 $arg eq $cmd and do {
822 exit(&{$commands{$cmd}}());