metadata.pl 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. #!/usr/bin/perl
  2. use strict;
  3. my %package;
  4. my %category;
  5. sub parse_target_metadata() {
  6. my ($target, @target, $profile);
  7. while (<>) {
  8. chomp;
  9. /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
  10. my $conf = uc $3.'_'.$2;
  11. $conf =~ tr/\.-/__/;
  12. $target = {
  13. id => $1,
  14. conf => $conf,
  15. board => $2,
  16. kernel => $3,
  17. profiles => []
  18. };
  19. push @target, $target;
  20. };
  21. /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
  22. /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
  23. /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
  24. /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
  25. /^Target-Description:/ and do {
  26. my $desc;
  27. while (<>) {
  28. last if /^@@/;
  29. $desc .= $_;
  30. }
  31. $target->{desc} = $desc;
  32. };
  33. /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
  34. /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
  35. /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
  36. /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
  37. /^Target-Profile:\s*(.+)\s*$/ and do {
  38. $profile = {
  39. id => $1,
  40. name => $1,
  41. packages => []
  42. };
  43. push @{$target->{profiles}}, $profile;
  44. };
  45. /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
  46. /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
  47. }
  48. return @target;
  49. }
  50. sub parse_package_metadata() {
  51. my $pkg;
  52. my $makefile;
  53. my $src;
  54. while (<>) {
  55. chomp;
  56. /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
  57. $makefile = $1;
  58. $src = $2;
  59. undef $pkg;
  60. };
  61. /^Package: \s*(.+)\s*$/ and do {
  62. $pkg = {};
  63. $pkg->{src} = $src;
  64. $pkg->{makefile} = $makefile;
  65. $pkg->{name} = $1;
  66. $pkg->{default} = "m if ALL";
  67. $package{$1} = $pkg;
  68. };
  69. /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
  70. /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
  71. /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
  72. /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
  73. /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
  74. /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
  75. /^Provides: \s*(.+)\s*$/ and do {
  76. my @vpkg = split /\s+/, $1;
  77. foreach my $vpkg (@vpkg) {
  78. $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
  79. push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
  80. }
  81. };
  82. /^Depends: \s*(.+)\s*$/ and do {
  83. my @dep = split /\s+/, $1;
  84. $pkg->{depends} = \@dep;
  85. };
  86. /^Category: \s*(.+)\s*$/ and do {
  87. $pkg->{category} = $1;
  88. defined $category{$1} or $category{$1} = {};
  89. defined $category{$1}->{$src} or $category{$1}->{$src} = [];
  90. push @{$category{$1}->{$src}}, $pkg;
  91. };
  92. /^Description: \s*(.*)\s*$/ and do {
  93. my $desc = "\t\t$1\n\n";
  94. my $line;
  95. while ($line = <>) {
  96. last if $line =~ /^@@/;
  97. $desc .= "\t\t$line";
  98. }
  99. $pkg->{description} = $desc;
  100. };
  101. /^Config: \s*(.*)\s*$/ and do {
  102. my $conf = "$1\n";
  103. my $line;
  104. while ($line = <>) {
  105. last if $line =~ /^@@/;
  106. $conf .= "$line";
  107. }
  108. $pkg->{config} = $conf;
  109. }
  110. }
  111. return %category;
  112. }
  113. sub gen_target_mk() {
  114. my @target = parse_target_metadata();
  115. @target = sort {
  116. $a->{id} cmp $b->{id}
  117. } @target;
  118. foreach my $target (@target) {
  119. my ($profiles_def, $profiles_eval);
  120. my $conf = uc $target->{kernel}.'_'.$target->{board};
  121. $conf =~ tr/\.-/__/;
  122. foreach my $profile (@{$target->{profiles}}) {
  123. $profiles_def .= "
  124. define Profile/$conf\_$profile->{id}
  125. ID:=$profile->{id}
  126. NAME:=$profile->{name}
  127. PACKAGES:=".join(" ", @{$profile->{packages}})."
  128. endef";
  129. $profiles_eval .= "
  130. \$(eval \$(call Profile,$conf\_$profile->{id}))"
  131. }
  132. print "
  133. ifeq (\$(CONFIG_LINUX_$conf),y)
  134. define Target
  135. KERNEL:=$target->{kernel}
  136. BOARD:=$target->{board}
  137. BOARDNAME:=$target->{name}
  138. LINUX_VERSION:=$target->{version}
  139. LINUX_RELEASE:=$target->{release}
  140. LINUX_KARCH:=$target->{karch}
  141. DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
  142. endef$profiles_def
  143. endif$profiles_eval
  144. "
  145. }
  146. print "\$(eval \$(call Target))\n";
  147. }
  148. sub target_config_features(@) {
  149. my $ret;
  150. while ($_ = shift @_) {
  151. /broken/ and $ret .= "\tdepends BROKEN\n";
  152. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  153. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  154. /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
  155. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  156. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  157. /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
  158. /ext2/ and $ret .= "\tselect USES_EXT2\n";
  159. }
  160. return $ret;
  161. }
  162. sub gen_target_config() {
  163. my @target = parse_target_metadata();
  164. @target = sort {
  165. $a->{name} cmp $b->{name}
  166. } @target;
  167. print <<EOF;
  168. choice
  169. prompt "Target System"
  170. default LINUX_2_4_BRCM
  171. EOF
  172. foreach my $target (@target) {
  173. my $features = target_config_features(@{$target->{features}});
  174. my $help = $target->{desc};
  175. my $kernel = $target->{kernel};
  176. $kernel =~ tr/./_/;
  177. chomp $features;
  178. $features .= "\n";
  179. if ($help =~ /\w+/) {
  180. $help =~ s/^\s*/\t /mg;
  181. $help = "\thelp\n$help";
  182. } else {
  183. undef $help;
  184. }
  185. print <<EOF
  186. config LINUX_$target->{conf}
  187. bool "$target->{name}"
  188. select $target->{arch}
  189. select LINUX_$kernel
  190. $features$help
  191. EOF
  192. }
  193. print <<EOF;
  194. if DEVEL
  195. config LINUX_2_6_ARM
  196. bool "UNSUPPORTED little-endian arm platform"
  197. depends BROKEN
  198. select LINUX_2_6
  199. select arm
  200. config LINUX_2_6_CRIS
  201. bool "UNSUPPORTED cris platform"
  202. depends BROKEN
  203. select LINUX_2_6
  204. select cris
  205. config LINUX_2_6_M68K
  206. bool "UNSUPPORTED m68k platform"
  207. depends BROKEN
  208. select LINUX_2_6
  209. select m68k
  210. config LINUX_2_6_SH3
  211. bool "UNSUPPORTED little-endian sh3 platform"
  212. depends BROKEN
  213. select LINUX_2_6
  214. select sh3
  215. config LINUX_2_6_SH3EB
  216. bool "UNSUPPORTED big-endian sh3 platform"
  217. depends BROKEN
  218. select LINUX_2_6
  219. select sh3eb
  220. config LINUX_2_6_SH4
  221. bool "UNSUPPORTED little-endian sh4 platform"
  222. depends BROKEN
  223. select LINUX_2_6
  224. select sh4
  225. config LINUX_2_6_SH4EB
  226. bool "UNSUPPORTED big-endian sh4 platform"
  227. depends BROKEN
  228. select LINUX_2_6
  229. select sh4eb
  230. config LINUX_2_6_SPARC
  231. bool "UNSUPPORTED sparc platform"
  232. depends BROKEN
  233. select LINUX_2_6
  234. select sparc
  235. endif
  236. endchoice
  237. choice
  238. prompt "Target Profile"
  239. EOF
  240. foreach my $target (@target) {
  241. my $profiles = $target->{profiles};
  242. @$profiles > 0 or $profiles = [
  243. {
  244. id => 'Default',
  245. name => 'Default',
  246. packages => []
  247. }
  248. ];
  249. foreach my $profile (@$profiles) {
  250. print <<EOF;
  251. config LINUX_$target->{conf}_$profile->{id}
  252. bool "$profile->{name}"
  253. depends LINUX_$target->{conf}
  254. EOF
  255. foreach my $pkg (@{$target->{packages}}, @{$profile->{packages}}) {
  256. print "\tselect DEFAULT_$pkg\n";
  257. }
  258. print "\n";
  259. }
  260. }
  261. print "endchoice\n";
  262. }
  263. sub find_package_dep($$) {
  264. my $pkg = shift;
  265. my $name = shift;
  266. my $deps = ($pkg->{vdepends} or $pkg->{depends});
  267. return 0 unless defined $deps;
  268. foreach my $dep (@{$deps}) {
  269. return 1 if $dep eq $name;
  270. return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
  271. }
  272. return 0;
  273. }
  274. sub package_depends($$) {
  275. my $a = shift;
  276. my $b = shift;
  277. my $ret;
  278. return 0 if ($a->{submenu} ne $b->{submenu});
  279. if (find_package_dep($a, $b->{name}) == 1) {
  280. $ret = 1;
  281. } elsif (find_package_dep($b, $a->{name}) == 1) {
  282. $ret = -1;
  283. } else {
  284. return 0;
  285. }
  286. return $ret;
  287. }
  288. sub print_package_config_category($) {
  289. my $cat = shift;
  290. my %menus;
  291. my %menu_dep;
  292. return unless $category{$cat};
  293. print "menu \"$cat\"\n\n";
  294. my %spkg = %{$category{$cat}};
  295. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  296. foreach my $pkg (@{$spkg{$spkg}}) {
  297. my $menu = $pkg->{submenu};
  298. if ($menu) {
  299. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  300. } else {
  301. $menu = 'undef';
  302. }
  303. $menus{$menu} or $menus{$menu} = [];
  304. push @{$menus{$menu}}, $pkg;
  305. print "\tconfig DEFAULT_".$pkg->{name}."\n";
  306. print "\t\tbool\n\n";
  307. }
  308. }
  309. my @menus = sort {
  310. ($a eq 'undef' ? 1 : 0) or
  311. ($b eq 'undef' ? -1 : 0) or
  312. ($a cmp $b)
  313. } keys %menus;
  314. foreach my $menu (@menus) {
  315. my @pkgs = sort {
  316. package_depends($a, $b) or
  317. ($a->{name} cmp $b->{name})
  318. } @{$menus{$menu}};
  319. if ($menu ne 'undef') {
  320. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  321. print "menu \"$menu\"\n";
  322. }
  323. foreach my $pkg (@pkgs) {
  324. my $title = $pkg->{name};
  325. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  326. if ($c > 0) {
  327. $title .= ("." x $c). " ". $pkg->{title};
  328. }
  329. print "\t";
  330. $pkg->{menu} and print "menu";
  331. print "config PACKAGE_".$pkg->{name}."\n";
  332. print "\t\ttristate \"$title\"\n";
  333. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  334. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  335. print "\t\tdefault $default\n";
  336. }
  337. foreach my $depend (@{$pkg->{depends}}) {
  338. my $m = "depends";
  339. $depend =~ s/^([@\+]+)//;
  340. my $flags = $1;
  341. my $vdep;
  342. if ($vdep = $package{$depend}->{vdepends}) {
  343. $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
  344. } else {
  345. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  346. $flags =~ /\+/ and $m = "select";
  347. }
  348. print "\t\t$m $depend\n";
  349. }
  350. print "\t\thelp\n";
  351. print $pkg->{description};
  352. print "\n";
  353. $pkg->{config} and print $pkg->{config}."\n";
  354. }
  355. if ($menu ne 'undef') {
  356. print "endmenu\n";
  357. $menu_dep{$menu} and print "endif\n";
  358. }
  359. }
  360. print "endmenu\n\n";
  361. undef $category{$cat};
  362. }
  363. sub gen_package_config() {
  364. parse_package_metadata();
  365. print_package_config_category 'Base system';
  366. foreach my $cat (keys %category) {
  367. print_package_config_category $cat;
  368. }
  369. }
  370. sub parse_command() {
  371. my $cmd = shift @ARGV;
  372. for ($cmd) {
  373. /^target_mk$/ and return gen_target_mk();
  374. /^target_config$/ and return gen_target_config();
  375. /^package_config$/ and return gen_package_config();
  376. }
  377. print <<EOF
  378. Available Commands:
  379. $0 target_mk [file] Target metadata in makefile format
  380. $0 target_config [file] Target metadata in Kconfig format
  381. $0 package_config [file] Package metadata in Kconfig format
  382. EOF
  383. }
  384. parse_command();