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 next if $kconfig{$config} eq 'y';
119 $kconfig{$config} = $val;
120 } elsif (!$override) {
121 $kconfig{$config} or $kconfig{$config} = 'n';
128 foreach my $kconfig (sort keys %kconfig) {
129 if ($kconfig{$kconfig} eq 'n') {
130 print "# $kconfig is not set\n";
132 print "$kconfig=$kconfig{$kconfig}\n";
137 sub merge_package_lists
($$) {
143 foreach my $pkg (@
$list1, @
$list2) {
146 foreach my $pkg (keys %pkgs) {
147 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
152 sub target_config_features
(@
) {
155 while ($_ = shift @_) {
156 /broken/ and $ret .= "\tdepends BROKEN\n";
157 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
158 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
159 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
160 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
161 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
162 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
163 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
164 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
165 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
166 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
167 /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
168 /ext4/ and $ret .= "\tselect USES_EXT4\n";
169 /targz/ and $ret .= "\tselect USES_TARGZ\n";
170 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
171 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
172 /fpu/ and $ret .= "\tselect HAS_FPU\n";
173 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
174 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
175 /powerpc64/ and $ret .= "\tselect powerpc64\n";
176 /nommu/ and $ret .= "\tselect NOMMU\n";
183 my $parent = $target->{parent
};
185 return $target->{parent
}->{name
}." - ".$target->{name
};
187 return $target->{name
};
194 if (substr($v,0,2) eq "2_") {
195 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
197 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
202 sub print_target
($) {
204 my $features = target_config_features
(@
{$target->{features
}});
205 my $help = $target->{desc
};
210 if ($help =~ /\w+/) {
211 $help =~ s/^\s*/\t /mg;
212 $help = "\thelp\n$help";
217 my $v = kver
($target->{version
});
218 if (@
{$target->{subtargets
}} == 0) {
220 config TARGET_$target->{conf}
221 bool "$target->{name}"
227 config TARGET_$target->{conf}
228 bool "$target->{name}"
231 if ($target->{subtarget
}) {
232 $confstr .= "\tdepends TARGET_$target->{boardconf}\n";
234 if (@
{$target->{subtargets
}} > 0) {
235 $confstr .= "\tselect HAS_SUBTARGETS\n";
237 $confstr .= $features;
240 if ($target->{arch
} =~ /\w/) {
241 $confstr .= "\tselect $target->{arch}\n";
243 foreach my $dep (@
{$target->{depends
}}) {
244 my $mode = "depends";
248 $dep =~ /^([@\+\-]+)(.+)$/;
252 next if $name =~ /:/;
253 $flags =~ /-/ and $mode = "deselect";
254 $flags =~ /\+/ and $mode = "select";
255 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
257 $confstr .= "$help\n\n";
261 sub gen_target_config
() {
262 my @target = parse_target_metadata
();
265 my @target_sort = sort {
266 target_name
($a) cmp target_name
($b);
272 prompt "Target System"
273 default TARGET_brcm47xx
278 foreach my $target (@target_sort) {
279 next if $target->{subtarget
};
280 print_target
($target);
287 prompt "Subtarget" if HAS_SUBTARGETS
289 foreach my $target (@target) {
290 next unless $target->{def_subtarget
};
292 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
298 foreach my $target (@target) {
299 next unless $target->{subtarget
};
300 print_target
($target);
307 prompt "Target Profile"
311 foreach my $target (@target) {
312 my $profiles = $target->{profiles
};
314 foreach my $profile (@
$profiles) {
316 config TARGET_$target->{conf}_$profile->{id}
317 bool "$profile->{name}"
318 depends TARGET_$target->{conf}
321 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
322 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
323 foreach my $pkg (@pkglist) {
324 print "\tselect DEFAULT_$pkg\n";
327 my $help = $profile->{desc
};
328 if ($help =~ /\w+/) {
329 $help =~ s/^\s*/\t /mg;
330 $help = "\thelp\n$help";
341 config HAS_SUBTARGETS
348 foreach my $target (@target) {
349 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
352 config TARGET_ARCH_PACKAGES
356 foreach my $target (@target) {
357 next if @
{$target->{subtargets
}} > 0;
358 print "\t\tdefault \"".($target->{arch_packages
} || $target->{board
})."\" if TARGET_".$target->{conf
}."\n";
362 config DEFAULT_TARGET_OPTIMIZATION
365 foreach my $target (@target) {
366 next if @
{$target->{subtargets
}} > 0;
367 print "\tdefault \"".$target->{cflags
}."\" if TARGET_".$target->{conf
}."\n";
369 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
372 foreach my $target (@target) {
373 my $v = kver
($target->{version
});
383 foreach my $def (sort keys %defaults) {
384 print "\tconfig DEFAULT_".$def."\n";
385 print "\t\tbool\n\n";
390 sub __find_package_dep
($$) {
393 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
395 return 0 unless defined $deps;
396 foreach my $dep (@
{$deps}) {
397 next if $dep_check{$dep};
398 $dep_check{$dep} = 1;
399 return 1 if $dep eq $name;
400 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
405 # wrapper to avoid infinite recursion
406 sub find_package_dep
($$) {
411 return __find_package_dep
($pkg, $name);
414 sub package_depends
($$) {
419 return 0 if ($a->{submenu
} ne $b->{submenu
});
420 if (find_package_dep
($a, $b->{name
}) == 1) {
422 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
433 my $only_dep = shift;
437 my $parent_condition = shift;
442 my @depends = @
$depends;
443 foreach my $depend (@depends) {
446 $depend =~ s/^([@\+]+)// and $flags = $1;
448 my $condition = $parent_condition;
450 next if $condition eq $depend;
451 next if $seen->{"$parent_condition:$depend"};
452 $seen->{"$parent_condition:$depend"} = 1;
453 if ($depend =~ /^(.+):(.+)$/) {
454 if ($1 ne "PACKAGE_$pkgname") {
456 $condition = "$condition && $1";
463 next if $package{$depend} and $package{$depend}->{buildonly
};
464 if ($vdep = $package{$depend}->{vdepends
}) {
465 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
467 $flags =~ /\+/ and do {
468 # Menuconfig will not treat 'select FOO' as a real dependency
469 # thus if FOO depends on other config options, these dependencies
470 # will not be checked. To fix this, we simply emit all of FOO's
471 # depends here as well.
472 $package{$depend} and mconf_depends
($pkgname, $package{$depend}->{depends
}, 1, $dep, $seen, $condition);
477 $flags =~ /@/ or $depend = "PACKAGE_$depend";
479 if ($m =~ /select/) {
480 next if $depend eq $condition;
481 $depend = "$depend if $condition";
483 $depend = "!($condition) || $depend";
487 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
489 foreach my $depend (keys %$dep) {
490 my $m = $dep->{$depend};
491 $res .= "\t\t$m $depend\n";
496 sub print_package_config_category
($) {
501 return unless $category{$cat};
503 print "menu \"$cat\"\n\n";
504 my %spkg = %{$category{$cat}};
506 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
507 foreach my $pkg (@
{$spkg{$spkg}}) {
508 next if $pkg->{buildonly
};
509 my $menu = $pkg->{submenu
};
511 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
515 $menus{$menu} or $menus{$menu} = [];
516 push @
{$menus{$menu}}, $pkg;
520 ($a eq 'undef' ?
1 : 0) or
521 ($b eq 'undef' ?
-1 : 0) or
525 foreach my $menu (@menus) {
527 package_depends
($a, $b) or
528 ($a->{name
} cmp $b->{name
})
530 if ($menu ne 'undef') {
531 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
532 print "menu \"$menu\"\n";
534 foreach my $pkg (@pkgs) {
535 my $title = $pkg->{name
};
536 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
538 $title .= ("." x
$c). " ". $pkg->{title
};
540 $title = "\"$title\"";
542 $pkg->{menu
} and print "menu";
543 print "config PACKAGE_".$pkg->{name
}."\n";
544 $pkg->{hidden
} and $title = "";
545 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." $title\n";
546 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
547 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
548 print "\t\tdefault $default\n";
550 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
551 print mconf_depends
($pkg->{name
}, $pkg->{mdepends
}, 0);
553 print $pkg->{description
};
556 $pkg->{config
} and print $pkg->{config
}."\n";
558 if ($menu ne 'undef') {
560 $menu_dep{$menu} and print "endif\n";
565 undef $category{$cat};
568 sub print_package_features
() {
569 keys %features > 0 or return;
570 print "menu \"Package features\"\n";
571 foreach my $n (keys %features) {
572 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
575 prompt "$features[0]->{target_title}"
576 default FEATURE_$features[0]->{name}
579 foreach my $feature (@features) {
581 config FEATURE_$feature->{name}
582 bool "$feature->{title}"
584 $feature->{description
} =~ /\w/ and do {
585 print "\t\thelp\n".$feature->{description
}."\n";
593 sub gen_package_config
() {
594 parse_package_metadata
($ARGV[0]) or exit 1;
595 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
596 foreach my $preconfig (keys %preconfig) {
597 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
598 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
601 config UCI_PRECONFIG_$conf
602 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
603 depends PACKAGE_$preconfig
604 default "$preconfig{$preconfig}->{$cfg}->{default}"
609 print "source \"package/*/image-config.in\"\n";
610 if (scalar glob "package/feeds/*/*/image-config.in") {
611 print "source \"package/feeds/*/*/image-config.in\"\n";
613 print_package_features
();
614 print_package_config_category
'Base system';
615 foreach my $cat (keys %category) {
616 print_package_config_category
$cat;
620 sub get_conditional_dep
($$) {
621 my $condition = shift;
624 if ($condition =~ /^!(.+)/) {
625 return "\$(if \$(CONFIG_$1),,$depstr)";
627 return "\$(if \$(CONFIG_$condition),$depstr)";
634 sub gen_package_mk
() {
640 parse_package_metadata
($ARGV[0]) or exit 1;
641 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
643 my $pkg = $package{$name};
646 next if defined $pkg->{vdepends
};
649 $conf{$pkg->{src
}} or do {
651 $conf{$pkg->{src
}} = 1;
654 $config = "\$(CONFIG_PACKAGE_$name)"
657 $pkg->{buildonly
} and $config = "";
658 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
659 if ($pkg->{variant
}) {
660 if (!defined($done{$pkg->{src
}})) {
661 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
663 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
665 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
668 next if $done{$pkg->{src
}};
669 $done{$pkg->{src
}} = 1;
671 if (@
{$pkg->{buildtypes
}} > 0) {
672 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
675 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
676 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
683 foreach my $type (@
{$pkg->{buildtypes
}}) {
687 next unless $pkg->{"builddepends/$type"};
688 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
692 if ($dep =~ /^(.+):(.+)/) {
696 if ($dep =~ /^(.+)(\/.+)/) {
702 my $pkg_dep = $package{$dep};
703 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
704 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
705 } elsif (defined($srcpackage{$dep})) {
706 $idx = $subdir{$dep}.$dep;
710 my $depstr = "\$(curdir)/$idx$suffix/compile";
711 my $depline = get_conditional_dep
($condition, $depstr);
713 $deplines{$depline}++;
716 my $depline = join(" ", sort keys %deplines);
718 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
724 foreach my $deps (@srcdeps) {
730 if ($deps =~ /^(.+):(.+)/) {
734 if ($deps =~ /^(.+)(\/.+)/) {
739 my $pkg_dep = $package{$deps};
742 if ($pkg_dep->{vdepends
}) {
743 @deps = @
{$pkg_dep->{vdepends
}};
748 foreach my $dep (@deps) {
749 $pkg_dep = $package{$deps};
750 if (defined $pkg_dep->{src
}) {
751 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
752 } elsif (defined($srcpackage{$dep})) {
753 $idx = $subdir{$dep}.$dep;
756 undef $idx if $idx =~ /^(kernel)|(base-files)$/;
759 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
760 next if $dep{$condition.":".$pkg->{src
}."->".$idx};
761 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
764 if ($pkg_dep->{vdepends
}) {
765 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
766 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
768 $depstr = "\$(curdir)/$idx/compile";
769 $dep{$pkg->{src
}."->".$idx} = 1;
771 $depline = get_conditional_dep
($condition, $depstr);
773 $deplines{$depline}++;
778 my $depline = join(" ", sort keys %deplines);
780 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
787 foreach my $preconfig (keys %preconfig) {
789 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
790 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
792 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
797 ifndef DUMP_TARGET_DB
798 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
803 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
804 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
812 sub gen_package_source
() {
813 parse_package_metadata
($ARGV[0]) or exit 1;
814 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
815 my $pkg = $package{$name};
816 if ($pkg->{name
} && $pkg->{source
}) {
817 print "$pkg->{name}: ";
818 print "$pkg->{source}\n";
823 sub parse_command
() {
824 my $cmd = shift @ARGV;
826 /^target_config$/ and return gen_target_config
();
827 /^package_mk$/ and return gen_package_mk
();
828 /^package_config$/ and return gen_package_config
();
829 /^kconfig/ and return gen_kconfig_overrides
();
830 /^package_source$/ and return gen_package_source
();
834 $0 target_config [file] Target metadata in Kconfig format
835 $0 package_mk [file] Package metadata in makefile format
836 $0 package_config [file] Package metadata in Kconfig format
837 $0 kconfig [file] [config] Kernel config overrides
838 $0 package_source [file] Package source file information