metadata.pl 14 KB

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