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-Name:\s*(.+)\s*$/ and $target->{name
} = $1;
49 /^Target-Path:\s*(.+)\s*$/ and $target->{path
} = $1;
50 /^Target-Arch:\s*(.+)\s*$/ and $target->{arch
} = $1;
51 /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages
} = $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-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget
} = $1;
60 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages
} = [ split(/\s+/, $1) ];
61 /^Target-Profile:\s*(.+)\s*$/ and do {
67 push @
{$target->{profiles
}}, $profile;
69 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name
} = $1;
70 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages
} = [ split(/\s+/, $1) ];
71 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc
} = get_multiline
(*FILE
);
72 /^Target-Profile-Config:/ and $profile->{config
} = get_multiline
(*FILE
, "\t");
73 /^Target-Profile-Kconfig:/ and $profile->{kconfig
} = 1;
76 foreach my $target (@target) {
77 next if @
{$target->{subtargets
}} > 0;
78 @
{$target->{profiles
}} > 0 or $target->{profiles
} = [
89 sub gen_kconfig_overrides
() {
93 my $pkginfo = shift @ARGV;
94 my $cfgfile = shift @ARGV;
96 # parameter 2: build system config
97 open FILE
, "<$cfgfile" or return;
99 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
103 # parameter 1: package metadata
104 open FILE
, "<$pkginfo" or return;
106 /^Package:\s*(.+?)\s*$/ and $package = $1;
107 /^Kernel-Config:\s*(.+?)\s*$/ and do {
108 my @config = split /\s+/, $1;
109 foreach my $config (@config) {
112 if ($config =~ /^(.+?)=(.+)$/) {
117 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
118 $kconfig{$config} = $val;
119 } elsif (!$override) {
120 $kconfig{$config} or $kconfig{$config} = 'n';
127 foreach my $kconfig (sort keys %kconfig) {
128 if ($kconfig{$kconfig} eq 'n') {
129 print "# $kconfig is not set\n";
131 print "$kconfig=$kconfig{$kconfig}\n";
136 sub merge_package_lists
($$) {
142 foreach my $pkg (@
$list1, @
$list2) {
145 foreach my $pkg (keys %pkgs) {
146 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
151 sub target_config_features
(@
) {
154 while ($_ = shift @_) {
155 /broken/ and $ret .= "\tdepends BROKEN\n";
156 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
157 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
158 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
159 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
160 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
161 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
162 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
163 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
164 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
165 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
166 /ext4/ and $ret .= "\tselect USES_EXT4\n";
167 /targz/ and $ret .= "\tselect USES_TARGZ\n";
168 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
169 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
170 /fpu/ and $ret .= "\tselect HAS_FPU\n";
171 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
172 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
173 /powerpc64/ and $ret .= "\tselect powerpc64\n";
174 /nommu/ and $ret .= "\tselect NOMMU\n";
181 my $parent = $target->{parent
};
183 return $target->{parent
}->{name
}." - ".$target->{name
};
185 return $target->{name
};
192 if (substr($v,0,2) eq "2_") {
193 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
195 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
200 sub print_target
($) {
202 my $features = target_config_features
(@
{$target->{features
}});
203 my $help = $target->{desc
};
208 if ($help =~ /\w+/) {
209 $help =~ s/^\s*/\t /mg;
210 $help = "\thelp\n$help";
215 my $v = kver
($target->{version
});
216 if (@
{$target->{subtargets
}} == 0) {
218 config TARGET_$target->{conf}
219 bool "$target->{name}"
225 config TARGET_$target->{conf}
226 bool "$target->{name}"
229 if ($target->{subtarget
}) {
230 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
232 if (@
{$target->{subtargets
}} > 0) {
233 $confstr .= "\tselect HAS_SUBTARGETS\n";
235 $confstr .= $features;
238 if ($target->{arch
} =~ /\w/) {
239 $confstr .= "\tselect $target->{arch}\n";
241 foreach my $dep (@
{$target->{depends
}}) {
242 my $mode = "depends";
246 $dep =~ /^([@\+\-]+)(.+)$/;
250 next if $name =~ /:/;
251 $flags =~ /-/ and $mode = "deselect";
252 $flags =~ /\+/ and $mode = "select";
253 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
255 $confstr .= "$help\n\n";
259 sub gen_target_config
() {
260 my @target = parse_target_metadata
();
263 my @target_sort = sort {
264 target_name
($a) cmp target_name
($b);
270 prompt "Target System"
271 default TARGET_brcm47xx
276 foreach my $target (@target_sort) {
277 next if $target->{subtarget
};
278 print_target
($target);
285 prompt "Subtarget" if HAS_SUBTARGETS
287 foreach my $target (@target) {
288 next unless $target->{def_subtarget
};
290 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
296 foreach my $target (@target) {
297 next unless $target->{subtarget
};
298 print_target
($target);
305 prompt "Target Profile"
309 foreach my $target (@target) {
310 my $profiles = $target->{profiles
};
312 foreach my $profile (@
$profiles) {
314 config TARGET_$target->{conf}_$profile->{id}
315 bool "$profile->{name}"
316 depends TARGET_$target->{conf}
319 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
320 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
321 foreach my $pkg (@pkglist) {
322 print "\tselect DEFAULT_$pkg\n";
325 my $help = $profile->{desc
};
326 if ($help =~ /\w+/) {
327 $help =~ s/^\s*/\t /mg;
328 $help = "\thelp\n$help";
339 config HAS_SUBTARGETS
346 foreach my $target (@target) {
347 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
350 config TARGET_ARCH_PACKAGES
354 foreach my $target (@target) {
355 next if @
{$target->{subtargets
}} > 0;
356 print "\t\tdefault \"".($target->{arch_packages
} || $target->{board
})."\" if TARGET_".$target->{conf
}."\n";
360 config DEFAULT_TARGET_OPTIMIZATION
363 foreach my $target (@target) {
364 next if @
{$target->{subtargets
}} > 0;
365 print "\tdefault \"".$target->{cflags
}."\" if TARGET_".$target->{conf
}."\n";
367 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
370 foreach my $target (@target) {
371 my $v = kver
($target->{version
});
381 foreach my $def (sort keys %defaults) {
382 print "\tconfig DEFAULT_".$def."\n";
383 print "\t\tbool\n\n";
388 sub __find_package_dep
($$) {
391 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
393 return 0 unless defined $deps;
394 foreach my $dep (@
{$deps}) {
395 next if $dep_check{$dep};
396 $dep_check{$dep} = 1;
397 return 1 if $dep eq $name;
398 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
403 # wrapper to avoid infinite recursion
404 sub find_package_dep
($$) {
409 return __find_package_dep
($pkg, $name);
412 sub package_depends
($$) {
417 return 0 if ($a->{submenu
} ne $b->{submenu
});
418 if (find_package_dep
($a, $b->{name
}) == 1) {
420 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
431 my $only_dep = shift;
435 my $parent_condition = shift;
440 my @depends = @
$depends;
441 foreach my $depend (@depends) {
444 $depend =~ s/^([@\+]+)// and $flags = $1;
446 my $condition = $parent_condition;
448 next if $condition eq $depend;
449 next if $seen->{"$parent_condition:$depend"};
450 $seen->{"$parent_condition:$depend"} = 1;
451 if ($depend =~ /^(.+):(.+)$/) {
452 if ($1 ne "PACKAGE_$pkgname") {
454 $condition = "$condition && $1";
461 next if $package{$depend} and $package{$depend}->{buildonly
};
462 if ($vdep = $package{$depend}->{vdepends
}) {
463 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
465 $flags =~ /\+/ and do {
466 # Menuconfig will not treat 'select FOO' as a real dependency
467 # thus if FOO depends on other config options, these dependencies
468 # will not be checked. To fix this, we simply emit all of FOO's
469 # depends here as well.
470 $package{$depend} and mconf_depends
($pkgname, $package{$depend}->{depends
}, 1, $dep, $seen, $condition);
475 $flags =~ /@/ or $depend = "PACKAGE_$depend";
477 if ($m =~ /select/) {
478 $depend = "$depend if $condition";
480 $depend = "!($condition) || $depend";
484 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
486 foreach my $depend (keys %$dep) {
487 my $m = $dep->{$depend};
488 $res .= "\t\t$m $depend\n";
493 sub print_package_config_category
($) {
498 return unless $category{$cat};
500 print "menu \"$cat\"\n\n";
501 my %spkg = %{$category{$cat}};
503 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
504 foreach my $pkg (@
{$spkg{$spkg}}) {
505 next if $pkg->{buildonly
};
506 my $menu = $pkg->{submenu
};
508 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
512 $menus{$menu} or $menus{$menu} = [];
513 push @
{$menus{$menu}}, $pkg;
517 ($a eq 'undef' ?
1 : 0) or
518 ($b eq 'undef' ?
-1 : 0) or
522 foreach my $menu (@menus) {
524 package_depends
($a, $b) or
525 ($a->{name
} cmp $b->{name
})
527 if ($menu ne 'undef') {
528 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
529 print "menu \"$menu\"\n";
531 foreach my $pkg (@pkgs) {
532 my $title = $pkg->{name
};
533 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
535 $title .= ("." x
$c). " ". $pkg->{title
};
537 $title = "\"$title\"";
539 $pkg->{menu
} and print "menu";
540 print "config PACKAGE_".$pkg->{name
}."\n";
541 $pkg->{hidden
} and $title = "";
542 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." $title\n";
543 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
544 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
545 print "\t\tdefault $default\n";
547 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
548 print mconf_depends
($pkg->{name
}, $pkg->{mdepends
}, 0);
550 print $pkg->{description
};
553 $pkg->{config
} and print $pkg->{config
}."\n";
555 if ($menu ne 'undef') {
557 $menu_dep{$menu} and print "endif\n";
562 undef $category{$cat};
565 sub print_package_features
() {
566 keys %features > 0 or return;
567 print "menu \"Package features\"\n";
568 foreach my $n (keys %features) {
569 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
572 prompt "$features[0]->{target_title}"
573 default FEATURE_$features[0]->{name}
576 foreach my $feature (@features) {
578 config FEATURE_$feature->{name}
579 bool "$feature->{title}"
581 $feature->{description
} =~ /\w/ and do {
582 print "\t\thelp\n".$feature->{description
}."\n";
590 sub gen_package_config
() {
591 parse_package_metadata
($ARGV[0]) or exit 1;
592 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
593 foreach my $preconfig (keys %preconfig) {
594 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
595 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
598 config UCI_PRECONFIG_$conf
599 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
600 depends PACKAGE_$preconfig
601 default "$preconfig{$preconfig}->{$cfg}->{default}"
606 print "source \"package/*/image-config.in\"\n";
607 if (scalar glob "package/feeds/*/*/image-config.in") {
608 print "source \"package/feeds/*/*/image-config.in\"\n";
610 print_package_features
();
611 print_package_config_category
'Base system';
612 foreach my $cat (keys %category) {
613 print_package_config_category
$cat;
617 sub get_conditional_dep
($$) {
618 my $condition = shift;
621 if ($condition =~ /^!(.+)/) {
622 return "\$(if \$(CONFIG_$1),,$depstr)";
624 return "\$(if \$(CONFIG_$condition),$depstr)";
631 sub gen_package_mk
() {
637 parse_package_metadata
($ARGV[0]) or exit 1;
638 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
640 my $pkg = $package{$name};
643 next if defined $pkg->{vdepends
};
646 $conf{$pkg->{src
}} or do {
648 $conf{$pkg->{src
}} = 1;
651 $config = "\$(CONFIG_PACKAGE_$name)"
654 $pkg->{buildonly
} and $config = "";
655 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
656 if ($pkg->{variant
}) {
657 if (!defined($done{$pkg->{src
}})) {
658 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
660 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
662 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
665 next if $done{$pkg->{src
}};
666 $done{$pkg->{src
}} = 1;
668 if (@
{$pkg->{buildtypes
}} > 0) {
669 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
672 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
673 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
680 foreach my $type (@
{$pkg->{buildtypes
}}) {
684 next unless $pkg->{"builddepends/$type"};
685 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
689 if ($dep =~ /^(.+):(.+)/) {
693 if ($dep =~ /^(.+)(\/.+)/) {
699 my $pkg_dep = $package{$dep};
700 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
701 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
702 } elsif (defined($srcpackage{$dep})) {
703 $idx = $subdir{$dep}.$dep;
707 my $depstr = "\$(curdir)/$idx$suffix/compile";
708 my $depline = get_conditional_dep
($condition, $depstr);
710 $deplines{$depline}++;
713 my $depline = join(" ", sort keys %deplines);
715 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
721 foreach my $deps (@srcdeps) {
727 if ($deps =~ /^(.+):(.+)/) {
731 if ($deps =~ /^(.+)(\/.+)/) {
736 my $pkg_dep = $package{$deps};
739 if ($pkg_dep->{vdepends
}) {
740 @deps = @
{$pkg_dep->{vdepends
}};
745 foreach my $dep (@deps) {
746 $pkg_dep = $package{$deps};
747 if (defined $pkg_dep->{src
}) {
748 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
749 } elsif (defined($srcpackage{$dep})) {
750 $idx = $subdir{$dep}.$dep;
753 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
756 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
757 next if $dep{$condition.":".$pkg->{src
}."->".$idx};
758 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
761 if ($pkg_dep->{vdepends
}) {
762 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
763 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
765 $depstr = "\$(curdir)/$idx/compile";
766 $dep{$pkg->{src
}."->".$idx} = 1;
768 $depline = get_conditional_dep
($condition, $depstr);
770 $deplines{$depline}++;
775 my $depline = join(" ", sort keys %deplines);
777 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
784 foreach my $preconfig (keys %preconfig) {
786 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
787 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
789 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
794 ifndef DUMP_TARGET_DB
795 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
800 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
801 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
809 sub gen_package_source
() {
810 parse_package_metadata
($ARGV[0]) or exit 1;
811 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
812 my $pkg = $package{$name};
813 if ($pkg->{name
} && $pkg->{source
}) {
814 print "$pkg->{name}: ";
815 print "$pkg->{source}\n";
820 sub parse_command
() {
821 my $cmd = shift @ARGV;
823 /^target_config$/ and return gen_target_config
();
824 /^package_mk$/ and return gen_package_mk
();
825 /^package_config$/ and return gen_package_config
();
826 /^kconfig/ and return gen_kconfig_overrides
();
827 /^package_source$/ and return gen_package_source
();
831 $0 target_config [file] Target metadata in Kconfig format
832 $0 package_mk [file] Package metadata in makefile format
833 $0 package_config [file] Package metadata in Kconfig format
834 $0 kconfig [file] [config] Kernel config overrides
835 $0 package_source [file] Package source file information