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