metadata.pl 11 KB

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