metadata.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 gen_target_mk() {
  130. my @target = parse_target_metadata();
  131. @target = sort {
  132. $a->{id} cmp $b->{id}
  133. } @target;
  134. foreach my $target (@target) {
  135. my ($profiles_def, $profiles_eval);
  136. my $conf = uc $target->{kernel}.'_'.$target->{board};
  137. $conf =~ tr/\.-/__/;
  138. foreach my $profile (@{$target->{profiles}}) {
  139. $profiles_def .= "
  140. define Profile/$conf\_$profile->{id}
  141. ID:=$profile->{id}
  142. NAME:=$profile->{name}
  143. PACKAGES:=".join(" ", @{$profile->{packages}})."\n";
  144. $profile->{kconfig} and $profiles_def .= " KCONFIG:=1\n";
  145. $profiles_def .= " endef";
  146. $profiles_eval .= "
  147. \$(eval \$(call AddProfile,$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. reset if !DEVEL
  190. EOF
  191. foreach my $target (@target) {
  192. my $features = target_config_features(@{$target->{features}});
  193. my $help = $target->{desc};
  194. my $kernel = $target->{kernel};
  195. $kernel =~ tr/./_/;
  196. chomp $features;
  197. $features .= "\n";
  198. if ($help =~ /\w+/) {
  199. $help =~ s/^\s*/\t /mg;
  200. $help = "\thelp\n$help";
  201. } else {
  202. undef $help;
  203. }
  204. print <<EOF
  205. config LINUX_$target->{conf}
  206. bool "$target->{name}"
  207. select $target->{arch}
  208. select LINUX_$kernel
  209. $features$help
  210. EOF
  211. }
  212. print <<EOF;
  213. if DEVEL
  214. config LINUX_2_6_ARM
  215. bool "UNSUPPORTED little-endian arm platform"
  216. depends BROKEN
  217. select LINUX_2_6
  218. select arm
  219. config LINUX_2_6_CRIS
  220. bool "UNSUPPORTED cris platform"
  221. depends BROKEN
  222. select LINUX_2_6
  223. select cris
  224. config LINUX_2_6_M68K
  225. bool "UNSUPPORTED m68k platform"
  226. depends BROKEN
  227. select LINUX_2_6
  228. select m68k
  229. config LINUX_2_6_SH3
  230. bool "UNSUPPORTED little-endian sh3 platform"
  231. depends BROKEN
  232. select LINUX_2_6
  233. select sh3
  234. config LINUX_2_6_SH3EB
  235. bool "UNSUPPORTED big-endian sh3 platform"
  236. depends BROKEN
  237. select LINUX_2_6
  238. select sh3eb
  239. config LINUX_2_6_SH4
  240. bool "UNSUPPORTED little-endian sh4 platform"
  241. depends BROKEN
  242. select LINUX_2_6
  243. select sh4
  244. config LINUX_2_6_SH4EB
  245. bool "UNSUPPORTED big-endian sh4 platform"
  246. depends BROKEN
  247. select LINUX_2_6
  248. select sh4eb
  249. config LINUX_2_6_SPARC
  250. bool "UNSUPPORTED sparc platform"
  251. depends BROKEN
  252. select LINUX_2_6
  253. select sparc
  254. endif
  255. endchoice
  256. choice
  257. prompt "Target Profile"
  258. EOF
  259. foreach my $target (@target) {
  260. my $profiles = $target->{profiles};
  261. foreach my $profile (@$profiles) {
  262. print <<EOF;
  263. config LINUX_$target->{conf}_$profile->{id}
  264. bool "$profile->{name}"
  265. depends LINUX_$target->{conf}
  266. $profile->{config}
  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 "menu \"Image configuration\"\n";
  401. foreach my $preconfig (keys %preconfig) {
  402. print "\tcomment \"$preconfig\"\n";
  403. foreach my $cfg (@{$preconfig{$preconfig}}) {
  404. my $conf = $cfg->{id};
  405. $conf =~ tr/\.-/__/;
  406. print <<EOF
  407. config UCI_PRECONFIG_$conf
  408. string "$cfg->{label}"
  409. depends PACKAGE_$preconfig
  410. default "$cfg->{default}"
  411. EOF
  412. }
  413. }
  414. print "endmenu\n\n";
  415. print_package_config_category 'Base system';
  416. foreach my $cat (keys %category) {
  417. print_package_config_category $cat;
  418. }
  419. }
  420. sub gen_package_mk() {
  421. my %conf;
  422. my %dep;
  423. my $line;
  424. parse_package_metadata();
  425. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  426. my $config;
  427. my $pkg = $package{$name};
  428. next if defined $pkg->{vdepends};
  429. if ($ENV{SDK}) {
  430. $conf{$pkg->{src}} or do {
  431. $config = 'm';
  432. $conf{$pkg->{src}} = 1;
  433. };
  434. } else {
  435. $config = "\$(CONFIG_PACKAGE_$name)"
  436. }
  437. if ($config) {
  438. print "package-$config += $pkg->{src}\n";
  439. $pkg->{prereq} and print "prereq-$config += $pkg->{src}\n";
  440. }
  441. my $hasdeps = 0;
  442. my $depline = "";
  443. foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
  444. next if $dep =~ /@/;
  445. $dep =~ s/\+//;
  446. my $idx;
  447. my $pkg_dep = $package{$dep};
  448. $pkg_dep or $pkg_dep = $srcpackage{$dep}->[0];
  449. next unless defined $pkg_dep;
  450. next if defined $pkg_dep->{vdepends};
  451. if (defined $pkg_dep->{src}) {
  452. ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{src};
  453. } elsif (defined($pkg_dep) && !defined($ENV{SDK})) {
  454. $idx = $dep;
  455. }
  456. undef $idx if $idx =~ /^(kernel)|(base-files)$/;
  457. if ($idx) {
  458. next if $dep{$pkg->{src}."->".$idx};
  459. $depline .= " $idx\-compile";
  460. $dep{$pkg->{src}."->".$idx} = 1;
  461. }
  462. }
  463. if ($depline) {
  464. $line .= "$pkg->{src}-compile: $depline\n";
  465. }
  466. }
  467. if ($line ne "") {
  468. print "\n$line";
  469. }
  470. foreach my $preconfig (keys %preconfig) {
  471. my $cmds;
  472. foreach my $cfg (@{$preconfig{$preconfig}}) {
  473. my $conf = $cfg->{id};
  474. $conf =~ tr/\.-/__/;
  475. $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
  476. }
  477. next unless $cmds;
  478. print <<EOF
  479. \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
  480. ( \\
  481. $cmds \\
  482. ) > \$@
  483. preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
  484. EOF
  485. }
  486. }
  487. sub parse_command() {
  488. my $cmd = shift @ARGV;
  489. for ($cmd) {
  490. /^target_mk$/ and return gen_target_mk();
  491. /^target_config$/ and return gen_target_config();
  492. /^package_mk$/ and return gen_package_mk();
  493. /^package_config$/ and return gen_package_config();
  494. }
  495. print <<EOF
  496. Available Commands:
  497. $0 target_mk [file] Target metadata in makefile format
  498. $0 target_config [file] Target metadata in Kconfig format
  499. $0 package_mk [file] Package metadata in makefile format
  500. $0 package_config [file] Package metadata in Kconfig format
  501. EOF
  502. }
  503. parse_command();