check for correct make version ( thanks to Thomas )
[openwrt/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}}) {
331 next if $dep =~ /@/;
332 $dep =~ s/^\+//;
333 $dep =~ s/^.+://;
334 next unless $dep;
335 install_package($feed, $dep) == 0 or $ret = 1;
336 }
337 }
338
339 return $ret;
340 }
341
342 sub refresh_config {
343 my $default = shift;
344
345 # workaround for timestamp check
346 system("rm -f tmp/.packageinfo");
347
348 # refresh the config
349 if ($default) {
350 system("$mk oldconfig CONFDEFAULT=\"$default\" Config.in >/dev/null 2>/dev/null");
351 } else {
352 system("$mk defconfig Config.in >/dev/null 2>/dev/null");
353 }
354 }
355
356 sub install {
357 my $name;
358 my %opts;
359 my $feed;
360 my $ret = 0;
361
362 getopts('ap:d:h', \%opts);
363
364 if ($opts{h}) {
365 usage();
366 return 0;
367 }
368
369 get_installed();
370
371 foreach my $f (@feeds) {
372 # index all feeds
373 $feed{$f->[1]} = get_feed($f->[1]);
374
375 # look up the preferred feed
376 $opts{p} and $f->[1] eq $opts{p} and $feed = $f;
377 }
378
379 if($opts{a}) {
380 foreach my $f (@feeds) {
381 if (!defined($opts{p}) or $opts{p} eq $f->[1]) {
382 printf "Installing all packages from feed %s.\n", $f->[1];
383 get_feed($f->[1]);
384 foreach my $name (sort { lc($a) cmp lc($b) } keys %package) {
385 my $p = $package{$name};
386 if( $p->{name} ) {
387 install_package($feed, $p->{name}) == 0 or $ret = 1;
388 }
389 }
390 }
391 }
392 } else {
393 while ($name = shift @ARGV) {
394 install_package($feed, $name) == 0 or $ret = 1;
395 }
396 }
397
398 # workaround for timestamp check
399
400 # set the defaults
401 if ($opts{d} and $opts{d} =~ /^[ymn]$/) {
402 refresh_config($opts{d});
403 }
404
405 return $ret;
406 }
407
408 sub uninstall {
409 my %opts;
410 my $name;
411 my $uninstall;
412
413 getopts('ah', \%opts);
414
415 if ($opts{h}) {
416 usage();
417 return 0;
418 }
419
420 if ($opts{a}) {
421 system("rm -rvf ./package/feeds");
422 $uninstall = 1;
423 } else {
424 if($#ARGV == -1) {
425 warn "WARNING: no package to uninstall\n";
426 return 0;
427 }
428 get_installed();
429 while ($name = shift @ARGV) {
430 my $pkg = $installed{$name};
431 $pkg or do {
432 warn "WARNING: $name not installed\n";
433 next;
434 };
435 $pkg->{src} and $name = $pkg->{src};
436 warn "Uninstalling package '$name'\n";
437 system("rm -f ./package/feeds/*/$name");
438 $uninstall = 1;
439 }
440 }
441 $uninstall and refresh_config();
442 return 0;
443 }
444
445 sub update_feed($$$$)
446 {
447 my $type=shift;
448 my $name=shift;
449 my $src=shift;
450 my $perform_update=shift;
451 my $force_relocate=update_location( $name, "@$src" );
452
453 if( $force_relocate ) {
454 warn "Source of feed $name has changed, replacing copy\n";
455 }
456 $update_method{$type} or do {
457 warn "Unknown type '$type' in feed $name\n";
458 return 1;
459 };
460 $perform_update and do {
461 my $failed = 1;
462 foreach my $feedsrc (@$src) {
463 warn "Updating feed '$name' from '$feedsrc' ...\n";
464 next unless update_feed_via($type, $name, $feedsrc, $force_relocate) == 0;
465 $failed = 0;
466 last;
467 }
468 $failed and do {
469 warn "failed.\n";
470 return 1;
471 };
472 };
473 warn "Create index file './feeds/$name.index' \n";
474 update_index($name) == 0 or do {
475 warn "failed.\n";
476 return 1;
477 };
478 return 0;
479 }
480
481 sub update {
482 my %opts;
483 my $feed_name;
484 my $perform_update=1;
485
486 $ENV{SCAN_COOKIE} = $$;
487 $ENV{KBUILD_VERBOSE} = 99;
488
489 getopts('ahi', \%opts);
490
491 if ($opts{h}) {
492 usage();
493 return 0;
494 }
495
496 if ($opts{i}) {
497 # don't update from (remote) repository
498 # only re-create index information
499 $perform_update=0;
500 }
501
502 -d "feeds" or do {
503 mkdir "feeds" or die "Unable to create the feeds directory";
504 };
505
506 if ( ($#ARGV == -1) or $opts{a}) {
507 foreach my $feed (@feeds) {
508 my ($type, $name, $src) = @$feed;
509 update_feed($type, $name, $src, $perform_update);
510 }
511 } else {
512 while ($feed_name = shift @ARGV) {
513 foreach my $feed (@feeds) {
514 my ($type, $name, $src) = @$feed;
515 if($feed_name ne $name) {
516 next;
517 }
518 update_feed($type, $name, $src, $perform_update);
519 }
520 }
521 }
522
523 refresh_config();
524
525 return 0;
526 }
527
528 sub usage() {
529 print <<EOF;
530 Usage: $0 <command> [options]
531
532 Commands:
533 list [options]: List feeds and their content
534 Options:
535 -s : List of feed names and their URL.
536 -r <feedname>: List packages of specified feed.
537
538 install [options] <package>: Install a package
539 Options:
540 -a : Install all packages from all feeds or from the specified feed using the -p option.
541 -p <feedname>: Prefer this feed when installing packages.
542 -d <y|m|n>: Set default for newly installed packages.
543
544 search [options] <substring>: Search for a package
545 Options:
546 -r <feedname>: Only search in this feed
547
548 uninstall -a|<package>: Uninstall a package
549 Options:
550 -a : Uninstalls all packages.
551
552 update -a|<feedname(s)>: Update packages and lists of feeds in feeds.conf .
553 Options:
554 -a : Update all feeds listed within feeds.conf. Otherwise the spezified feeds will be updated.
555 -i : Recreate the index only. No feed update from repository is performed.
556
557 clean: Remove downloaded/generated files.
558
559 EOF
560 exit(1);
561 }
562
563 my %commands = (
564 'list' => \&list,
565 'update' => \&update,
566 'install' => \&install,
567 'search' => \&search,
568 'uninstall' => \&uninstall,
569 'clean' => sub {
570 system("rm -rf feeds");
571 }
572 );
573
574 my $arg = shift @ARGV;
575 $arg or usage();
576 parse_config;
577 foreach my $cmd (keys %commands) {
578 $arg eq $cmd and do {
579 exit(&{$commands{$cmd}}());
580 };
581 }
582 usage();