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 /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype
} = $1;
57 /^Linux-Version:\s*(.+)\s*$/ and $target->{version
} = $1;
58 /^Linux-Release:\s*(.+)\s*$/ and $target->{release
} = $1;
59 /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch
} = $1;
60 /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget
} = $1;
61 /^Default-Packages:\s*(.+)\s*$/ and $target->{packages
} = [ split(/\s+/, $1) ];
62 /^Target-Profile:\s*(.+)\s*$/ and do {
68 push @
{$target->{profiles
}}, $profile;
70 /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name
} = $1;
71 /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages
} = [ split(/\s+/, $1) ];
72 /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc
} = get_multiline
(*FILE
);
73 /^Target-Profile-Config:/ and $profile->{config
} = get_multiline
(*FILE
, "\t");
74 /^Target-Profile-Kconfig:/ and $profile->{kconfig
} = 1;
77 foreach my $target (@target) {
78 if (@
{$target->{subtargets
}} > 0) {
79 $target->{profiles
} = [];
82 @
{$target->{profiles
}} > 0 or $target->{profiles
} = [
93 sub gen_kconfig_overrides
() {
97 my $pkginfo = shift @ARGV;
98 my $cfgfile = shift @ARGV;
100 # parameter 2: build system config
101 open FILE
, "<$cfgfile" or return;
103 /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
107 # parameter 1: package metadata
108 open FILE
, "<$pkginfo" or return;
110 /^Package:\s*(.+?)\s*$/ and $package = $1;
111 /^Kernel-Config:\s*(.+?)\s*$/ and do {
112 my @config = split /\s+/, $1;
113 foreach my $config (@config) {
116 if ($config =~ /^(.+?)=(.+)$/) {
121 if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
122 next if $kconfig{$config} eq 'y';
123 $kconfig{$config} = $val;
124 } elsif (!$override) {
125 $kconfig{$config} or $kconfig{$config} = 'n';
132 foreach my $kconfig (sort keys %kconfig) {
133 if ($kconfig{$kconfig} eq 'n') {
134 print "# $kconfig is not set\n";
136 print "$kconfig=$kconfig{$kconfig}\n";
141 sub merge_package_lists
($$) {
147 foreach my $pkg (@
$list1, @
$list2) {
150 foreach my $pkg (keys %pkgs) {
151 push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
156 sub target_config_features
(@
) {
159 while ($_ = shift @_) {
160 /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
161 /broken/ and $ret .= "\tdepends on BROKEN\n";
162 /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
163 /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
164 /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
165 /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
166 /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
167 /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
168 /usb/ and $ret .= "\tselect USB_SUPPORT\n";
169 /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
170 /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
171 /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
172 /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
173 /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
174 /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
175 /ext4/ and $ret .= "\tselect USES_EXT4\n";
176 /targz/ and $ret .= "\tselect USES_TARGZ\n";
177 /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
178 /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
179 /fpu/ and $ret .= "\tselect HAS_FPU\n";
180 /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
181 /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
182 /powerpc64/ and $ret .= "\tselect powerpc64\n";
183 /nommu/ and $ret .= "\tselect NOMMU\n";
184 /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
185 /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
186 /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
187 /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
194 my $parent = $target->{parent
};
196 return $target->{parent
}->{name
}." - ".$target->{name
};
198 return $target->{name
};
205 if (substr($v,0,2) eq "2_") {
206 $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
208 $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
213 sub print_target
($) {
215 my $features = target_config_features
(@
{$target->{features
}});
216 my $help = $target->{desc
};
221 if ($help =~ /\w+/) {
222 $help =~ s/^\s*/\t /mg;
223 $help = "\thelp\n$help";
228 my $v = kver
($target->{version
});
229 if (@
{$target->{subtargets
}} == 0) {
231 config TARGET_$target->{conf}
232 bool "$target->{name}"
238 config TARGET_$target->{conf}
239 bool "$target->{name}"
242 if ($target->{subtarget
}) {
243 $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
245 if (@
{$target->{subtargets
}} > 0) {
246 $confstr .= "\tselect HAS_SUBTARGETS\n";
247 grep { /broken/ } @
{$target->{features
}} and $confstr .= "\tdepends on BROKEN\n";
249 $confstr .= $features;
252 if ($target->{arch
} =~ /\w/) {
253 $confstr .= "\tselect $target->{arch}\n";
255 foreach my $dep (@
{$target->{depends
}}) {
256 my $mode = "depends on";
260 $dep =~ /^([@\+\-]+)(.+)$/;
264 next if $name =~ /:/;
265 $flags =~ /-/ and $mode = "deselect";
266 $flags =~ /\+/ and $mode = "select";
267 $flags =~ /@/ and $confstr .= "\t$mode $name\n";
269 $confstr .= "$help\n\n";
273 sub gen_target_config
() {
274 my @target = parse_target_metadata
();
277 my @target_sort = sort {
278 target_name
($a) cmp target_name
($b);
284 prompt "Target System"
285 default TARGET_ar71xx
290 foreach my $target (@target_sort) {
291 next if $target->{subtarget
};
292 print_target
($target);
299 prompt "Subtarget" if HAS_SUBTARGETS
301 foreach my $target (@target) {
302 next unless $target->{def_subtarget
};
304 default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
310 foreach my $target (@target) {
311 next unless $target->{subtarget
};
312 print_target
($target);
319 prompt "Target Profile"
323 foreach my $target (@target) {
324 my $profiles = $target->{profiles
};
326 foreach my $profile (@
$profiles) {
328 config TARGET_$target->{conf}_$profile->{id}
329 bool "$profile->{name}"
330 depends on TARGET_$target->{conf}
333 $profile->{kconfig
} and print "\tselect PROFILE_KCONFIG\n";
334 my @pkglist = merge_package_lists
($target->{packages
}, $profile->{packages
});
335 foreach my $pkg (@pkglist) {
336 print "\tselect DEFAULT_$pkg\n";
339 my $help = $profile->{desc
};
340 if ($help =~ /\w+/) {
341 $help =~ s/^\s*/\t /mg;
342 $help = "\thelp\n$help";
353 config HAS_SUBTARGETS
360 foreach my $target (@target) {
361 $target->{subtarget
} or print "\t\tdefault \"".$target->{board
}."\" if TARGET_".$target->{conf
}."\n";
364 config TARGET_ARCH_PACKAGES
368 foreach my $target (@target) {
369 next if @
{$target->{subtargets
}} > 0;
370 print "\t\tdefault \"".($target->{arch_packages
} || $target->{board
})."\" if TARGET_".$target->{conf
}."\n";
374 config DEFAULT_TARGET_OPTIMIZATION
377 foreach my $target (@target) {
378 next if @
{$target->{subtargets
}} > 0;
379 print "\tdefault \"".$target->{cflags
}."\" if TARGET_".$target->{conf
}."\n";
381 print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
387 foreach my $target (@target) {
388 next if @
{$target->{subtargets
}} > 0;
389 print "\tdefault \"".$target->{cputype
}."\" if TARGET_".$target->{conf
}."\n";
391 print "\tdefault \"\"\n";
394 foreach my $target (@target) {
395 my $v = kver
($target->{version
});
405 foreach my $def (sort keys %defaults) {
406 print "\tconfig DEFAULT_".$def."\n";
407 print "\t\tbool\n\n";
412 sub __find_package_dep
($$) {
415 my $deps = ($pkg->{vdepends
} or $pkg->{depends
});
417 return 0 unless defined $deps;
418 foreach my $dep (@
{$deps}) {
419 next if $dep_check{$dep};
420 $dep_check{$dep} = 1;
421 return 1 if $dep eq $name;
422 return 1 if ($package{$dep} and (__find_package_dep
($package{$dep},$name) == 1));
427 # wrapper to avoid infinite recursion
428 sub find_package_dep
($$) {
433 return __find_package_dep
($pkg, $name);
436 sub package_depends
($$) {
441 return 0 if ($a->{submenu
} ne $b->{submenu
});
442 if (find_package_dep
($a, $b->{name
}) == 1) {
444 } elsif (find_package_dep
($b, $a->{name
}) == 1) {
455 my $only_dep = shift;
459 my $parent_condition = shift;
465 my @depends = @
$depends;
466 foreach my $depend (@depends) {
467 my $m = "depends on";
469 $depend =~ s/^([@\+]+)// and $flags = $1;
471 my $condition = $parent_condition;
473 next if $condition eq $depend;
474 next if $seen->{"$parent_condition:$depend"};
475 next if $seen->{":$depend"};
476 $seen->{"$parent_condition:$depend"} = 1;
477 if ($depend =~ /^(.+):(.+)$/) {
478 if ($1 ne "PACKAGE_$pkgname") {
480 $condition = "$condition && $1";
487 next if $package{$depend} and $package{$depend}->{buildonly
};
488 if ($vdep = $package{$depend}->{vdepends
}) {
489 $depend = join("||", map { "PACKAGE_".$_ } @
$vdep);
491 $flags =~ /\+/ and do {
492 # Menuconfig will not treat 'select FOO' as a real dependency
493 # thus if FOO depends on other config options, these dependencies
494 # will not be checked. To fix this, we simply emit all of FOO's
495 # depends here as well.
496 $package{$depend} and push @t_depends, [ $package{$depend}->{depends
}, $condition ];
501 $flags =~ /@/ or $depend = "PACKAGE_$depend";
503 if ($m =~ /select/) {
504 next if $depend eq $condition;
505 $depend = "$depend if $condition";
507 $depend = "!($condition) || $depend" unless $dep->{$condition} eq 'select';
511 $dep->{$depend} =~ /select/ or $dep->{$depend} = $m;
514 foreach my $tdep (@t_depends) {
515 mconf_depends
($pkgname, $tdep->[0], 1, $dep, $seen, $tdep->[1]);
518 foreach my $depend (keys %$dep) {
519 my $m = $dep->{$depend};
520 $res .= "\t\t$m $depend\n";
525 sub print_package_config_category
($) {
530 return unless $category{$cat};
532 print "menu \"$cat\"\n\n";
533 my %spkg = %{$category{$cat}};
535 foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
536 foreach my $pkg (@
{$spkg{$spkg}}) {
537 next if $pkg->{buildonly
};
538 my $menu = $pkg->{submenu
};
540 $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep
};
544 $menus{$menu} or $menus{$menu} = [];
545 push @
{$menus{$menu}}, $pkg;
549 ($a eq 'undef' ?
1 : 0) or
550 ($b eq 'undef' ?
-1 : 0) or
554 foreach my $menu (@menus) {
556 package_depends
($a, $b) or
557 ($a->{name
} cmp $b->{name
})
559 if ($menu ne 'undef') {
560 $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
561 print "menu \"$menu\"\n";
563 foreach my $pkg (@pkgs) {
564 my $title = $pkg->{name
};
565 my $c = (72 - length($pkg->{name
}) - length($pkg->{title
}));
567 $title .= ("." x
$c). " ". $pkg->{title
};
569 $title = "\"$title\"";
571 $pkg->{menu
} and print "menu";
572 print "config PACKAGE_".$pkg->{name
}."\n";
573 $pkg->{hidden
} and $title = "";
574 print "\t\t".($pkg->{tristate
} ?
'tristate' : 'bool')." $title\n";
575 print "\t\tdefault y if DEFAULT_".$pkg->{name
}."\n";
576 unless ($pkg->{hidden
}) {
577 $pkg->{default} ||= "m if ALL";
579 if ($pkg->{default}) {
580 foreach my $default (split /\s*,\s*/, $pkg->{default}) {
581 print "\t\tdefault $default\n";
584 print mconf_depends
($pkg->{name
}, $pkg->{depends
}, 0);
585 print mconf_depends
($pkg->{name
}, $pkg->{mdepends
}, 0);
587 print $pkg->{description
};
590 $pkg->{config
} and print $pkg->{config
}."\n";
592 if ($menu ne 'undef') {
594 $menu_dep{$menu} and print "endif\n";
599 undef $category{$cat};
602 sub print_package_features
() {
603 keys %features > 0 or return;
604 print "menu \"Package features\"\n";
605 foreach my $n (keys %features) {
606 my @features = sort { $b->{priority
} <=> $a->{priority
} or $a->{title
} cmp $b->{title
} } @
{$features{$n}};
609 prompt "$features[0]->{target_title}"
610 default FEATURE_$features[0]->{name}
613 foreach my $feature (@features) {
615 config FEATURE_$feature->{name}
616 bool "$feature->{title}"
618 $feature->{description
} =~ /\w/ and do {
619 print "\t\thelp\n".$feature->{description
}."\n";
627 sub gen_package_config
() {
628 parse_package_metadata
($ARGV[0]) or exit 1;
629 print "menuconfig IMAGEOPT\n\tbool \"Image configuration\"\n\tdefault n\n";
630 foreach my $preconfig (keys %preconfig) {
631 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
632 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
635 config UCI_PRECONFIG_$conf
636 string "$preconfig{$preconfig}->{$cfg}->{label}" if IMAGEOPT
637 depends on PACKAGE_$preconfig
638 default "$preconfig{$preconfig}->{$cfg}->{default}"
643 print "source \"package/*/image-config.in\"\n";
644 if (scalar glob "package/feeds/*/*/image-config.in") {
645 print "source \"package/feeds/*/*/image-config.in\"\n";
647 print_package_features
();
648 print_package_config_category
'Base system';
649 foreach my $cat (sort {uc($a) cmp uc($b)} keys %category) {
650 print_package_config_category
$cat;
654 sub get_conditional_dep
($$) {
655 my $condition = shift;
658 if ($condition =~ /^!(.+)/) {
659 return "\$(if \$(CONFIG_$1),,$depstr)";
661 return "\$(if \$(CONFIG_$condition),$depstr)";
668 sub gen_package_mk
() {
674 parse_package_metadata
($ARGV[0]) or exit 1;
675 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
677 my $pkg = $package{$name};
680 next if defined $pkg->{vdepends
};
682 $config = "\$(CONFIG_PACKAGE_$name)";
684 $pkg->{buildonly
} and $config = "";
685 print "package-$config += $pkg->{subdir}$pkg->{src}\n";
686 if ($pkg->{variant
}) {
687 if (!defined($done{$pkg->{src
}})) {
688 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/default-variant := $pkg->{variant}\n";
690 print "\$(curdir)/$pkg->{subdir}$pkg->{src}/variants += \$(if $config,$pkg->{variant})\n"
692 $pkg->{prereq
} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
695 next if $done{$pkg->{src
}};
696 $done{$pkg->{src
}} = 1;
698 if (@
{$pkg->{buildtypes
}} > 0) {
699 print "buildtypes-$pkg->{subdir}$pkg->{src} = ".join(' ', @
{$pkg->{buildtypes
}})."\n";
702 foreach my $spkg (@
{$srcpackage{$pkg->{src
}}}) {
703 foreach my $dep (@
{$spkg->{depends
}}, @
{$spkg->{builddepends
}}) {
710 foreach my $type (@
{$pkg->{buildtypes
}}) {
714 next unless $pkg->{"builddepends/$type"};
715 foreach my $dep (@
{$pkg->{"builddepends/$type"}}) {
719 if ($dep =~ /^(.+):(.+)/) {
723 if ($dep =~ /^(.+)(\/.+)/) {
729 my $pkg_dep = $package{$dep};
730 if (defined($pkg_dep) && defined($pkg_dep->{src
})) {
731 $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
732 } elsif (defined($srcpackage{$dep})) {
733 $idx = $subdir{$dep}.$dep;
737 my $depstr = "\$(curdir)/$idx$suffix/compile";
738 my $depline = get_conditional_dep
($condition, $depstr);
740 $deplines{$depline}++;
743 my $depline = join(" ", sort keys %deplines);
745 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/$type/compile += $depline\n";
751 foreach my $deps (@srcdeps) {
757 if ($deps =~ /^(.+):(.+)/) {
761 if ($deps =~ /^(.+)(\/.+)/) {
766 my $pkg_dep = $package{$deps};
769 if ($pkg_dep->{vdepends
}) {
770 @deps = @
{$pkg_dep->{vdepends
}};
775 foreach my $dep (@deps) {
776 $pkg_dep = $package{$deps};
777 if (defined $pkg_dep->{src
}) {
778 ($pkg->{src
} ne $pkg_dep->{src
}.$suffix) and $idx = $pkg_dep->{subdir
}.$pkg_dep->{src
};
779 } elsif (defined($srcpackage{$dep})) {
780 $idx = $subdir{$dep}.$dep;
782 undef $idx if $idx eq 'base-files';
787 next if $pkg->{src
} eq $pkg_dep->{src
}.$suffix;
788 next if $dep{$condition.":".$pkg->{src
}."->".$idx};
789 next if $dep{$pkg->{src
}."->($dep)".$idx} and $pkg_dep->{vdepends
};
792 if ($pkg_dep->{vdepends
}) {
793 $depstr = "\$(if \$(CONFIG_PACKAGE_$dep),\$(curdir)/$idx/compile)";
794 $dep{$pkg->{src
}."->($dep)".$idx} = 1;
796 $depstr = "\$(curdir)/$idx/compile";
797 $dep{$pkg->{src
}."->".$idx} = 1;
799 $depline = get_conditional_dep
($condition, $depstr);
801 $deplines{$depline}++;
806 my $depline = join(" ", sort keys %deplines);
808 $line .= "\$(curdir)/".$pkg->{subdir
}."$pkg->{src}/compile += $depline\n";
815 foreach my $preconfig (keys %preconfig) {
817 foreach my $cfg (keys %{$preconfig{$preconfig}}) {
818 my $conf = $preconfig{$preconfig}->{$cfg}->{id
};
820 $cmds .= "\techo \"uci set '$preconfig{$preconfig}->{$cfg}->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
825 ifndef DUMP_TARGET_DB
826 \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
831 ifneq (\$(IMAGEOPT)\$(CONFIG_IMAGEOPT),)
832 package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
840 sub gen_package_source
() {
841 parse_package_metadata
($ARGV[0]) or exit 1;
842 foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
843 my $pkg = $package{$name};
844 if ($pkg->{name
} && $pkg->{source
}) {
845 print "$pkg->{name}: ";
846 print "$pkg->{source}\n";
851 sub parse_command
() {
852 my $cmd = shift @ARGV;
854 /^target_config$/ and return gen_target_config
();
855 /^package_mk$/ and return gen_package_mk
();
856 /^package_config$/ and return gen_package_config
();
857 /^kconfig/ and return gen_kconfig_overrides
();
858 /^package_source$/ and return gen_package_source
();
862 $0 target_config [file] Target metadata in Kconfig format
863 $0 package_mk [file] Package metadata in makefile format
864 $0 package_config [file] Package metadata in Kconfig format
865 $0 kconfig [file] [config] Kernel config overrides
866 $0 package_source [file] Package source file information