metadata.pl 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. #!/usr/bin/perl
  2. use strict;
  3. my %preconfig;
  4. my %package;
  5. my %srcpackage;
  6. my %category;
  7. sub get_multiline {
  8. my $prefix = shift;
  9. my $str;
  10. while (<>) {
  11. last if /^@@/;
  12. s/^\s*//g;
  13. $str .= (($_ and $prefix) ? $prefix . $_ : $_);
  14. }
  15. return $str;
  16. }
  17. sub parse_target_metadata() {
  18. my ($target, @target, $profile);
  19. while (<>) {
  20. chomp;
  21. /^Target:\s*((.+)-(\d+\.\d+))\s*$/ and do {
  22. my $conf = uc $3.'_'.$2;
  23. $conf =~ tr/\.-/__/;
  24. $target = {
  25. id => $1,
  26. conf => $conf,
  27. board => $2,
  28. kernel => $3,
  29. profiles => []
  30. };
  31. push @target, $target;
  32. };
  33. /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
  34. /^Target-Path:\s*(.+)\s*$/ and $target->{path} = $1;
  35. /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
  36. /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
  37. /^Target-Description:/ and $target->{desc} = get_multiline();
  38. /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
  39. /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
  40. /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
  41. /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
  42. /^Target-Profile:\s*(.+)\s*$/ and do {
  43. $profile = {
  44. id => $1,
  45. name => $1,
  46. packages => []
  47. };
  48. push @{$target->{profiles}}, $profile;
  49. };
  50. /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
  51. /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
  52. /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline();
  53. /^Target-Profile-Config:/ and $profile->{config} = get_multiline("\t");
  54. /^Target-Profile-Kconfig:/ and $profile->{kconfig} = 1;
  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 $preconfig;
  71. my $src;
  72. while (<>) {
  73. chomp;
  74. /^Source-Makefile: \s*(.+\/([^\/]+)\/Makefile)\s*$/ and do {
  75. $makefile = $1;
  76. $src = $2;
  77. $srcpackage{$src} = [];
  78. undef $pkg;
  79. };
  80. /^Package:\s*(.+?)\s*$/ and do {
  81. $pkg = {};
  82. $pkg->{src} = $src;
  83. $pkg->{makefile} = $makefile;
  84. $pkg->{name} = $1;
  85. $pkg->{default} = "m if ALL";
  86. $pkg->{depends} = [];
  87. $pkg->{builddepends} = [];
  88. $package{$1} = $pkg;
  89. push @{$srcpackage{$src}}, $pkg;
  90. };
  91. /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
  92. /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
  93. /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
  94. /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
  95. /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
  96. /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
  97. /^Provides: \s*(.+)\s*$/ and do {
  98. my @vpkg = split /\s+/, $1;
  99. foreach my $vpkg (@vpkg) {
  100. $package{$vpkg} or $package{$vpkg} = { vdepends => [] };
  101. push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
  102. }
  103. };
  104. /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
  105. /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
  106. /^Category: \s*(.+)\s*$/ and do {
  107. $pkg->{category} = $1;
  108. defined $category{$1} or $category{$1} = {};
  109. defined $category{$1}->{$src} or $category{$1}->{$src} = [];
  110. push @{$category{$1}->{$src}}, $pkg;
  111. };
  112. /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline("\t\t ");
  113. /^Config: \s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline();
  114. /^Prereq-Check:/ and $pkg->{prereq} = 1;
  115. /^Preconfig:\s*(.+)\s*$/ and do {
  116. my $pkgname = $pkg->{name};
  117. $preconfig{$pkgname} or $preconfig{$pkgname} = [];
  118. $preconfig = {
  119. id => $1
  120. };
  121. push @{$preconfig{$pkgname}}, $preconfig;
  122. };
  123. /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
  124. /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
  125. /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
  126. }
  127. return %category;
  128. }
  129. sub merge_package_lists($$) {
  130. my $list1 = shift;
  131. my $list2 = shift;
  132. my @l = ();
  133. my %pkgs;
  134. foreach my $pkg (@$list1, @$list2) {
  135. $pkgs{$pkg} = 1;
  136. }
  137. foreach my $pkg (keys %pkgs) {
  138. push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
  139. }
  140. return sort(@l);
  141. }
  142. sub gen_target_mk() {
  143. my @target = parse_target_metadata();
  144. @target = sort {
  145. $a->{id} cmp $b->{id}
  146. } @target;
  147. foreach my $target (@target) {
  148. my ($profiles_def, $profiles_eval);
  149. my $conf = uc $target->{kernel}.'_'.$target->{board};
  150. $conf =~ tr/\.-/__/;
  151. foreach my $profile (@{$target->{profiles}}) {
  152. $profiles_def .= "
  153. define Profile/$conf\_$profile->{id}
  154. ID:=$profile->{id}
  155. NAME:=$profile->{name}
  156. PACKAGES:=".join(" ", merge_package_lists($target->{packages}, $profile->{packages}))."\n";
  157. $profile->{kconfig} and $profiles_def .= " KCONFIG:=1\n";
  158. $profiles_def .= " endef";
  159. $profiles_eval .= "
  160. \$(eval \$(call AddProfile,$conf\_$profile->{id}))"
  161. }
  162. print "
  163. ifeq (\$(CONFIG_LINUX_$conf),y)
  164. define Target
  165. KERNEL:=$target->{kernel}
  166. BOARD:=$target->{board}
  167. BOARDNAME:=$target->{name}
  168. LINUX_VERSION:=$target->{version}
  169. LINUX_RELEASE:=$target->{release}
  170. LINUX_KARCH:=$target->{karch}
  171. DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
  172. endef$profiles_def
  173. endif$profiles_eval
  174. "
  175. }
  176. print "\$(eval \$(call Target))\n";
  177. }
  178. sub target_config_features(@) {
  179. my $ret;
  180. while ($_ = shift @_) {
  181. /broken/ and $ret .= "\tdepends BROKEN\n";
  182. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  183. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  184. /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
  185. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  186. /video/ and $ret .= "\tselect VIDEO_SUPPORT\n";
  187. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  188. /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
  189. /ext2/ and $ret .= "\tselect USES_EXT2\n";
  190. }
  191. return $ret;
  192. }
  193. sub gen_target_config() {
  194. my @target = parse_target_metadata();
  195. @target = sort {
  196. $a->{name} cmp $b->{name}
  197. } @target;
  198. print <<EOF;
  199. choice
  200. prompt "Target System"
  201. default LINUX_2_4_BRCM
  202. reset if !DEVEL
  203. EOF
  204. foreach my $target (@target) {
  205. my $features = target_config_features(@{$target->{features}});
  206. my $help = $target->{desc};
  207. my $kernel = $target->{kernel};
  208. $kernel =~ tr/./_/;
  209. chomp $features;
  210. $features .= "\n";
  211. if ($help =~ /\w+/) {
  212. $help =~ s/^\s*/\t /mg;
  213. $help = "\thelp\n$help";
  214. } else {
  215. undef $help;
  216. }
  217. print <<EOF
  218. config LINUX_$target->{conf}
  219. bool "$target->{name}"
  220. select $target->{arch}
  221. select LINUX_$kernel
  222. $features$help
  223. EOF
  224. }
  225. print <<EOF;
  226. if DEVEL
  227. config LINUX_2_6_ARM
  228. bool "UNSUPPORTED little-endian arm platform"
  229. depends BROKEN
  230. select LINUX_2_6
  231. select arm
  232. config LINUX_2_6_CRIS
  233. bool "UNSUPPORTED cris platform"
  234. depends BROKEN
  235. select LINUX_2_6
  236. select cris
  237. config LINUX_2_6_M68K
  238. bool "UNSUPPORTED m68k platform"
  239. depends BROKEN
  240. select LINUX_2_6
  241. select m68k
  242. config LINUX_2_6_SH3
  243. bool "UNSUPPORTED little-endian sh3 platform"
  244. depends BROKEN
  245. select LINUX_2_6
  246. select sh3
  247. config LINUX_2_6_SH3EB
  248. bool "UNSUPPORTED big-endian sh3 platform"
  249. depends BROKEN
  250. select LINUX_2_6
  251. select sh3eb
  252. config LINUX_2_6_SH4
  253. bool "UNSUPPORTED little-endian sh4 platform"
  254. depends BROKEN
  255. select LINUX_2_6
  256. select sh4
  257. config LINUX_2_6_SH4EB
  258. bool "UNSUPPORTED big-endian sh4 platform"
  259. depends BROKEN
  260. select LINUX_2_6
  261. select sh4eb
  262. config LINUX_2_6_SPARC
  263. bool "UNSUPPORTED sparc platform"
  264. depends BROKEN
  265. select LINUX_2_6
  266. select sparc
  267. endif
  268. endchoice
  269. choice
  270. prompt "Target Profile"
  271. EOF
  272. foreach my $target (@target) {
  273. my $profiles = $target->{profiles};
  274. foreach my $profile (@$profiles) {
  275. print <<EOF;
  276. config LINUX_$target->{conf}_$profile->{id}
  277. bool "$profile->{name}"
  278. depends LINUX_$target->{conf}
  279. $profile->{config}
  280. EOF
  281. $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
  282. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  283. foreach my $pkg (@pkglist) {
  284. print "\tselect DEFAULT_$pkg\n";
  285. }
  286. print "\n";
  287. }
  288. }
  289. print "endchoice\n";
  290. }
  291. sub find_package_dep($$) {
  292. my $pkg = shift;
  293. my $name = shift;
  294. my $deps = ($pkg->{vdepends} or $pkg->{depends});
  295. return 0 unless defined $deps;
  296. foreach my $dep (@{$deps}) {
  297. return 1 if $dep eq $name;
  298. return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
  299. }
  300. return 0;
  301. }
  302. sub package_depends($$) {
  303. my $a = shift;
  304. my $b = shift;
  305. my $ret;
  306. return 0 if ($a->{submenu} ne $b->{submenu});
  307. if (find_package_dep($a, $b->{name}) == 1) {
  308. $ret = 1;
  309. } elsif (find_package_dep($b, $a->{name}) == 1) {
  310. $ret = -1;
  311. } else {
  312. return 0;
  313. }
  314. return $ret;
  315. }
  316. sub mconf_depends($$) {
  317. my $depends = shift;
  318. my $only_dep = shift;
  319. my $res;
  320. $depends or return;
  321. my @depends = @$depends;
  322. foreach my $depend (@depends) {
  323. my $m = "depends";
  324. $depend =~ s/^([@\+]+)//;
  325. my $flags = $1;
  326. my $vdep;
  327. if ($vdep = $package{$depend}->{vdepends}) {
  328. $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
  329. } else {
  330. $flags =~ /\+/ and do {
  331. next if $only_dep;
  332. $m = "select";
  333. # Menuconfig will not treat 'select FOO' as a real dependency
  334. # thus if FOO depends on other config options, these dependencies
  335. # will not be checked. To fix this, we simply emit all of FOO's
  336. # depends here as well.
  337. $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
  338. };
  339. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  340. }
  341. $res .= "\t\t$m $depend\n";
  342. }
  343. return $res;
  344. }
  345. sub print_package_config_category($) {
  346. my $cat = shift;
  347. my %menus;
  348. my %menu_dep;
  349. return unless $category{$cat};
  350. print "menu \"$cat\"\n\n";
  351. my %spkg = %{$category{$cat}};
  352. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  353. foreach my $pkg (@{$spkg{$spkg}}) {
  354. my $menu = $pkg->{submenu};
  355. if ($menu) {
  356. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  357. } else {
  358. $menu = 'undef';
  359. }
  360. $menus{$menu} or $menus{$menu} = [];
  361. push @{$menus{$menu}}, $pkg;
  362. print "\tconfig DEFAULT_".$pkg->{name}."\n";
  363. print "\t\tbool\n\n";
  364. }
  365. }
  366. my @menus = sort {
  367. ($a eq 'undef' ? 1 : 0) or
  368. ($b eq 'undef' ? -1 : 0) or
  369. ($a cmp $b)
  370. } keys %menus;
  371. foreach my $menu (@menus) {
  372. my @pkgs = sort {
  373. package_depends($a, $b) or
  374. ($a->{name} cmp $b->{name})
  375. } @{$menus{$menu}};
  376. if ($menu ne 'undef') {
  377. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  378. print "menu \"$menu\"\n";
  379. }
  380. foreach my $pkg (@pkgs) {
  381. my $title = $pkg->{name};
  382. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  383. if ($c > 0) {
  384. $title .= ("." x $c). " ". $pkg->{title};
  385. }
  386. print "\t";
  387. $pkg->{menu} and print "menu";
  388. print "config PACKAGE_".$pkg->{name}."\n";
  389. print "\t\ttristate \"$title\"\n";
  390. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  391. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  392. print "\t\tdefault $default\n";
  393. }
  394. print mconf_depends($pkg->{depends}, 0);
  395. print "\t\thelp\n";
  396. print $pkg->{description};
  397. print "\n";
  398. $pkg->{config} and print $pkg->{config}."\n";
  399. }
  400. if ($menu ne 'undef') {
  401. print "endmenu\n";
  402. $menu_dep{$menu} and print "endif\n";
  403. }
  404. }
  405. print "endmenu\n\n";
  406. undef $category{$cat};
  407. }
  408. sub gen_package_config() {
  409. parse_package_metadata();
  410. print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
  411. foreach my $preconfig (keys %preconfig) {
  412. foreach my $cfg (@{$preconfig{$preconfig}}) {
  413. my $conf = $cfg->{id};
  414. $conf =~ tr/\.-/__/;
  415. print <<EOF
  416. config UCI_PRECONFIG_$conf
  417. string "$cfg->{label}" if UCI_PRECONFIG
  418. depends PACKAGE_$preconfig
  419. default "$cfg->{default}"
  420. EOF
  421. }
  422. }
  423. print_package_config_category 'Base system';
  424. foreach my $cat (keys %category) {
  425. print_package_config_category $cat;
  426. }
  427. }
  428. sub gen_package_mk() {
  429. my %conf;
  430. my %dep;
  431. my $line;
  432. parse_package_metadata();
  433. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  434. my $config;
  435. my $pkg = $package{$name};
  436. next if defined $pkg->{vdepends};
  437. if ($ENV{SDK}) {
  438. $conf{$pkg->{src}} or do {
  439. $config = 'm';
  440. $conf{$pkg->{src}} = 1;
  441. };
  442. } else {
  443. $config = "\$(CONFIG_PACKAGE_$name)"
  444. }
  445. if ($config) {
  446. print "package-$config += $pkg->{src}\n";
  447. $pkg->{prereq} and print "prereq-$config += $pkg->{src}\n";
  448. }
  449. my $hasdeps = 0;
  450. my $depline = "";
  451. foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
  452. next if $dep =~ /@/;
  453. $dep =~ s/\+//;
  454. my $idx;
  455. my $pkg_dep = $package{$dep};
  456. $pkg_dep or $pkg_dep = $srcpackage{$dep}->[0];
  457. next unless defined $pkg_dep;
  458. next if defined $pkg_dep->{vdepends};
  459. if (defined $pkg_dep->{src}) {
  460. ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{src};
  461. } elsif (defined($pkg_dep) && !defined($ENV{SDK})) {
  462. $idx = $dep;
  463. }
  464. undef $idx if $idx =~ /^(kernel)|(base-files)$/;
  465. if ($idx) {
  466. next if $dep{$pkg->{src}."->".$idx};
  467. $depline .= " $idx\-compile";
  468. $dep{$pkg->{src}."->".$idx} = 1;
  469. }
  470. }
  471. if ($depline) {
  472. $line .= "$pkg->{src}-compile: $depline\n";
  473. }
  474. }
  475. if ($line ne "") {
  476. print "\n$line";
  477. }
  478. foreach my $preconfig (keys %preconfig) {
  479. my $cmds;
  480. foreach my $cfg (@{$preconfig{$preconfig}}) {
  481. my $conf = $cfg->{id};
  482. $conf =~ tr/\.-/__/;
  483. $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
  484. }
  485. next unless $cmds;
  486. print <<EOF
  487. \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
  488. ( \\
  489. $cmds \\
  490. ) > \$@
  491. ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
  492. preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
  493. endif
  494. EOF
  495. }
  496. }
  497. sub parse_command() {
  498. my $cmd = shift @ARGV;
  499. for ($cmd) {
  500. /^target_mk$/ and return gen_target_mk();
  501. /^target_config$/ and return gen_target_config();
  502. /^package_mk$/ and return gen_package_mk();
  503. /^package_config$/ and return gen_package_config();
  504. }
  505. print <<EOF
  506. Available Commands:
  507. $0 target_mk [file] Target metadata in makefile format
  508. $0 target_config [file] Target metadata in Kconfig format
  509. $0 package_mk [file] Package metadata in makefile format
  510. $0 package_config [file] Package metadata in Kconfig format
  511. EOF
  512. }
  513. parse_command();