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