3 use lib
"$FindBin::Bin";
11 $conf =~ tr
#/\.\-/#___#;
15 sub parse_target_metadata
() {
16 my $file = shift @ARGV;
17 my ($target, @target, $profile);
20 open FILE
, "<$file" or do {
21 warn "Can't open file '$file': $!\n";
26 /^Target:\s*(.+)\s*$/ and do {
31 boardconf
=> confstr
($name),
32 conf
=> confstr
($name),
38 push @target, $target;
39 $target{$name} = $target;
40 if ($name =~ /([^\/]+)\
/([^\/]+)/) {
41 push @
{$target{$1}->{subtargets
}}, $2;
42 $target->{board
} = $1;
43 $target->{boardconf
} = confstr
($1);
44 $target->{subtarget
} = 1;
45 $target->{parent
} = $target{$1};
48 /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel
} = $1;
49 /^Target-Name:\s*(.+)\s*$/ and $target->{name
} = $1;
50 /^Target-Path:\s*(.+)\s*$/ and $target->{path
} = $1;
51 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch
} = $1;
52 /^Target-Features:\s*(.+)\s*$/ and $target->{features
} = [ split(/\s+/, $1) ];
53 /^Target-Depends:\s*(.+)\s*$/ and $target->{depends
} = [ split(/\s+/, $1) ];
54 /^Target-Description:/ and $target->{desc
} = get_multiline
(*FILE
);
55 /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags
} = $1;
56 /^Linux-Version:\s*(.+)\s*$/ and $target->{version
} = $1;
57 /^Linux-Release:\s*(.+)\s*$/ and $target->{release
} = $1;
58 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch
} = $1;
59 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages
} = [ split(/\s+/, $1) ];
60 /^Target-Profile:\s*(.+)\s*$/ and do {
66 push @
{$target->{profiles
}}, $profile;
68 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name
} = $1;
69 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages
} = [ split(/\s+/, $1) ];
70 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc
} = get_multiline
(*FILE
);
71 /^Target-Profile-Config:/ and $profile->{config
} = get_multiline
(*FILE
, "\t");
72 /^Target-Profile-Kconfig:/ and $profile->{kconfig
} = 1;
75 foreach my $target (@target) {
76 next if @
{$target->{subtargets
}} > 0;
77 @
{$target->{profiles
}} > 0 or $target->{profiles
} = [
88 sub gen_kconfig_overrides
() {
92 my $pkginfo = shift @ARGV;
93 my $cfgfile = shift @ARGV;
95 # parameter 2: build system config
96 open FILE
, "<$cfgfile" or return;
98 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
102 # parameter 1: package metadata
103 open FILE
, "<$pkginfo" or return;
105 /^Package:\s*(.+?)\s*$/ and $package = $1;
106 /^Kernel-Config:\s*(.+?)\s*$/ and do {
107 my @config = split /\s+/, $1;
108 foreach my $config (@config) {
111 if ($config =~ /^(.+?)=(.+)$/) {
116 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
117 $kconfig{$config} = $val;
118 } elsif (!$override) {
119 $kconfig{$config} or $kconfig{$config} = 'n';
126 foreach my $kconfig (sort keys %kconfig) {
127 if ($kconfig{$kconfig} eq 'n') {
128 print "# $kconfig is not set\n";
130 print "$kconfig=$kconfig{$kconfig}\n";
135 sub merge_package_lists
($$) {
141 foreach my $pkg (@
$list1, @
$list2) {
144 foreach my $pkg (keys %pkgs) {
145 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
150 sub target_config_features
(@
) {
153 while ($_ = shift @_) {
154 /broken/ and $ret .= "\tdepends BROKEN\n";
155 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
156 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
157 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
158 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
159 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
160 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
161 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
162 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
163 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
164 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
165 /ext2/ and $ret .= "\tselect USES_EXT2\n";
166 /targz/ and $ret .= "\tselect USES_TARGZ\n";
167 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
168 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
169 /fpu/ and $ret .= "\tselect HAS_FPU\n";
170 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
171 /powerpc64/ and $ret .= "\tselect powerpc64\n";
172 /nommu/ and $ret .= "\tselect NOMMU\n";
179 my $parent = $target->{parent
};
181 return $target->{parent
}->{name
}." - ".$target->{name
};
183 return $target->{name
};
190 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
194 sub print_target
($) {
196 my $features = target_config_features
(@
{$target->{features
}});
197 my $help = $target->{desc
};
198 my $kernel = $target->{kernel
};
204 if ($help =~ /\w+/) {
205 $help =~ s/^\s*/\t /mg;
206 $help = "\thelp\n$help";
211 my $v = kver
($target->{version
});
212 if (@
{$target->{subtargets
}} == 0) {
214 config TARGET_$target->{conf}
215 bool "$target->{name}"
222 config TARGET_$target->{conf}
223 bool "$target->{name}"
226 if ($target->{subtarget
}) {
227 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
229 if (@
{$target->{subtargets
}} > 0) {
230 $confstr .= "\tselect HAS_SUBTARGETS\n";
233 if ($target->{arch
} =~ /\w/) {
234 $confstr .= "\tselect $target->{arch}\n";
236 foreach my $dep (@
{$target->{depends
}}) {
237 my $mode = "depends";
241 $dep =~ /^([@\+\-]+)(.+)$/;
245 next if $name =~ /:/;
246 $flags =~ /-/ and $mode = "deselect";
247 $flags =~ /\+/ and $mode = "select";
248 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
250 $confstr .= $features;
251 $confstr .= "$help\n\n";
255 sub gen_target_config
() {
256 my @target = parse_target_metadata
();
259 my @target_sort = sort {
260 target_name
($a) cmp target_name
($b);
266 prompt "Target System"
267 default TARGET_brcm47xx
272 foreach my $target (@target_sort) {
273 next if $target->{subtarget
};
274 print_target
($target);
281 prompt "Subtarget" if HAS_SUBTARGETS
284 foreach my $target (@target) {
285 next unless $target->{subtarget
};
286 print_target
($target);
293 prompt "Target Profile"
297 foreach my $target (@target) {
298 my $profiles = $target->{profiles
};
300 foreach my $profile (@
$profiles) {
302 config TARGET_$target->{conf}_$profile->{id}
303 bool "$profile->{name}"
304 depends TARGET_$target->{conf}
307 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
308 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
309 foreach my $pkg (@pkglist) {
310 print "\tselect DEFAULT_$pkg\n";
313 my $help = $profile->{desc
};
314 if ($help =~ /\w+/) {
315 $help =~ s/^\s*/\t /mg;
316 $help = "\thelp\n$help";
327 config HAS_SUBTARGETS
334 foreach my $target (@target) {
335 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
339 config DEFAULT_TARGET_OPTIMIZATION
342 foreach my $target (@target) {
343 next if @
{$target->{subtargets
}} > 0;
344 print "\tdefault \"".$target->{cflags
}."\" if TARGET_".$target->{conf
}."\n";
346 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
349 foreach my $target (@target) {
350 my $v = kver
($target->{version
});
360 foreach my $def (sort keys %defaults) {
361 print "\tconfig DEFAULT_".$def."\n";
362 print "\t\tbool\n\n";
367 sub __find_package_dep
($$) {
370 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
372 return 0 unless defined $deps;
373 foreach my $dep (@
{$deps}) {
374 next if $dep_check{$dep};
375 $dep_check{$dep} = 1;
376 return 1 if $dep eq $name;
377 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
382 # wrapper to avoid infinite recursion
383 sub find_package_dep
($$) {
388 return __find_package_dep
($pkg, $name);
391 sub package_depends
($$) {
396 return 0 if ($a->{submenu
} ne $b->{submenu
});
397 if (find_package_dep
($a, $b->{name
}) == 1) {
399 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
410 my $only_dep = shift;
414 my $parent_condition = shift;
419 my @depends = @
$depends;
420 foreach my $depend (@depends) {
423 $depend =~ s/^([@\+]+)// and $flags = $1;
425 my $condition = $parent_condition;
427 next if $condition eq $depend;
428 next if $seen->{"$parent_condition:$depend"};
429 $seen->{"$parent_condition:$depend"} = 1;
430 if ($depend =~ /^(.+):(.+)$/) {
431 if ($1 ne "PACKAGE_$pkgname") {
433 $condition = "$condition && $1";
440 next if $package{$depend} and $package{$depend}->{buildonly
};
441 if ($vdep = $package{$depend}->{vdepends
}) {
442 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
444 $flags =~ /\+/ and do {
445 # Menuconfig will not treat 'select FOO' as a real dependency
446 # thus if FOO depends on other config options, these dependencies
447 # will not be checked. To fix this, we simply emit all of FOO's
448 # depends here as well.
449 $package{$depend} and mconf_depends
($pkgname, $package{$depend}->{depends
}, 1, $dep, $seen, $condition);
454 $flags =~ /@/ or $depend = "PACKAGE_$depend";
456 if ($m =~ /select/) {
457 $depend = "$depend if $condition";
459 $depend = "!($condition) || $depend";
463 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
465 foreach my $depend (keys %$dep) {
466 my $m = $dep->{$depend};
467 $res .= "\t\t$m $depend\n";
472 sub print_package_config_category
($) {
477 return unless $category{$cat};
479 print "menu \"$cat\"\n\n";
480 my %spkg = %{$category{$cat}};
482 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
483 foreach my $pkg (@
{$spkg{$spkg}}) {
484 next if $pkg->{buildonly
};
485 my $menu = $pkg->{submenu
};
487 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
491 $menus{$menu} or $menus{$menu} = [];
492 push @
{$menus{$menu}}, $pkg;
496 ($a eq 'undef' ?
1 : 0) or
497 ($b eq 'undef' ?
-1 : 0) or
501 foreach my $menu (@menus) {
503 package_depends
($a, $b) or
504 ($a->{name
} cmp $b->{name
})
506 if ($menu ne 'undef') {
507 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
508 print "menu \"$menu\"\n";
510 foreach my $pkg (@pkgs) {
511 my $title = $pkg->{name
};
512 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
514 $title .= ("." x
$c). " ". $pkg->{title
};
517 $pkg->{menu
} and print "menu";
518 print "config PACKAGE_".$pkg->{name
}."\n";
519 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." \"$title\"\n";
520 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
521 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
522 print "\t\tdefault $default\n";
524 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
526 print $pkg->{description
};
529 $pkg->{config
} and print $pkg->{config
}."\n";
531 if ($menu ne 'undef') {
533 $menu_dep{$menu} and print "endif\n";
538 undef $category{$cat};
541 sub print_package_features
() {
542 keys %features > 0 or return;
543 print "menu \"Package features\"\n";
544 foreach my $n (keys %features) {
545 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
548 prompt "$features[0]->{target_title}"
549 default FEATURE_$features[0]->{name}
552 foreach my $feature (@features) {
554 config FEATURE_$feature->{name}
555 bool "$feature->{title}"
557 $feature->{description
} =~ /\w/ and do {
558 print "\t\thelp\n".$feature->{description
}."\n";
566 sub gen_package_config
() {
567 parse_package_metadata
($ARGV[0]) or exit 1;
568 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
569 foreach my $preconfig (keys %preconfig) {
570 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
571 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
574 config UCI_PRECONFIG_$conf
575 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
576 depends PACKAGE_$preconfig
577 default "$preconfig{$preconfig}->{$cfg}->{default}"
582 print "source \"package/*/image-config.in\"\n";
583 if (scalar glob "package/feeds/*/*/image-config.in") {
584 print "source \"package/feeds/*/*/image-config.in\"\n";
586 print_package_features
();
587 print_package_config_category
'Base system';
588 foreach my $cat (keys %category) {
589 print_package_config_category
$cat;
593 sub get_conditional_dep
($$) {
594 my $condition = shift;
597 if ($condition =~ /^!(.+)/) {
598 return "\$(if \$(CONFIG_$1),,$depstr)";
600 return "\$(if \$(CONFIG_$condition),$depstr)";
607 sub gen_package_mk
() {
613 parse_package_metadata
($ARGV[0]) or exit 1;
614 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
616 my $pkg = $package{$name};
619 next if defined $pkg->{vdepends
};
622 $conf{$pkg->{src
}} or do {
624 $conf{$pkg->{src
}} = 1;
627 $config = "\$(CONFIG_PACKAGE_$name)"
630 $pkg->{buildonly
} and $config = "";
631 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
632 if ($pkg->{variant
}) {
633 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
635 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
638 next if $done{$pkg->{src
}};
639 $done{$pkg->{src
}} = 1;
641 if (@
{$pkg->{buildtypes
}} > 0) {
642 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
645 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
646 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
653 foreach my $type (@
{$pkg->{buildtypes
}}) {
657 next unless $pkg->{"builddepends/$type"};
658 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
662 if ($dep =~ /^(.+):(.+)/) {
666 if ($dep =~ /^(.+)(\/.+)/) {
672 my $pkg_dep = $package{$dep};
673 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
674 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
675 } elsif (defined($srcpackage{$dep})) {
676 $idx = $subdir{$dep}.$dep;
680 my $depstr = "\$(curdir)/$idx$suffix/compile";
681 my $depline = get_conditional_dep
($condition, $depstr);
683 $deplines{$dep} = $depline;
686 my $depline = join(" ", values %deplines);
688 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
694 foreach my $deps (@srcdeps) {
700 if ($deps =~ /^(.+):(.+)/) {
704 if ($deps =~ /^(.+)(\/.+)/) {
709 my $pkg_dep = $package{$deps};
712 if ($pkg_dep->{vdepends
}) {
713 @deps = @
{$pkg_dep->{vdepends
}};
718 foreach my $dep (@deps) {
719 $pkg_dep = $package{$deps};
720 if (defined $pkg_dep->{src
}) {
721 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
722 } elsif (defined($srcpackage{$dep})) {
723 $idx = $subdir{$dep}.$dep;
726 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
729 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
730 next if $dep{$pkg->{src
}."->".$idx};
731 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
734 if ($pkg_dep->{vdepends
}) {
735 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
736 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
738 $depstr = "\$(curdir)/$idx/compile";
739 $dep{$pkg->{src
}."->".$idx} = 1;
741 $depline = get_conditional_dep
($condition, $depstr);
743 $deplines{$idx.$dep} = $depline;
748 my $depline = join(" ", values %deplines);
750 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
757 foreach my $preconfig (keys %preconfig) {
759 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
760 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
762 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
767 ifndef DUMP_TARGET_DB
768 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
773 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
774 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
782 sub gen_package_source
() {
783 parse_package_metadata
($ARGV[0]) or exit 1;
784 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
785 my $pkg = $package{$name};
786 if ($pkg->{name
} && $pkg->{source
}) {
787 print "$pkg->{name}: ";
788 print "$pkg->{source}\n";
793 sub parse_command
() {
794 my $cmd = shift @ARGV;
796 /^target_config$/ and return gen_target_config
();
797 /^package_mk$/ and return gen_package_mk
();
798 /^package_config$/ and return gen_package_config
();
799 /^kconfig/ and return gen_kconfig_overrides
();
800 /^package_source$/ and return gen_package_source
();
804 $0 target_config [file] Target metadata in Kconfig format
805 $0 package_mk [file] Package metadata in makefile format
806 $0 package_config [file] Package metadata in Kconfig format
807 $0 kconfig [file] [config] Kernel config overrides
808 $0 package_source [file] Package source file information