metadata.pl 10 KB

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