metadata.pl 14 KB

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