metadata.pl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 gen_kconfig_overrides() {
  136. my %config;
  137. my %kconfig;
  138. my $package;
  139. my $pkginfo = shift @ARGV;
  140. my $cfgfile = shift @ARGV;
  141. # parameter 2: build system config
  142. open FILE, "<$cfgfile" or return;
  143. while (<FILE>) {
  144. /^(CONFIG_.+?)=(.+)$/ and $config{$1} = 1;
  145. }
  146. close FILE;
  147. # parameter 1: package metadata
  148. open FILE, "<$pkginfo" or return;
  149. while (<FILE>) {
  150. /^Package:\s*(.+?)\s*$/ and $package = $1;
  151. /^Kernel-Config:\s*(.+?)\s*$/ and do {
  152. my @config = split /\s+/, $1;
  153. foreach my $config (@config) {
  154. my $val = 'm';
  155. my $override;
  156. if ($config =~ /^(.+?)=(.+)$/) {
  157. $config = $1;
  158. $override = 1;
  159. $val = $2;
  160. }
  161. if ($config{"CONFIG_PACKAGE_$package"} and ($config ne 'n')) {
  162. $kconfig{$config} = $val;
  163. } elsif (!$override) {
  164. $kconfig{$config} or $kconfig{$config} = 'n';
  165. }
  166. }
  167. };
  168. };
  169. close FILE;
  170. foreach my $kconfig (sort keys %kconfig) {
  171. if ($kconfig{$kconfig} eq 'n') {
  172. print "# $kconfig is not set\n";
  173. } else {
  174. print "$kconfig=$kconfig{$kconfig}\n";
  175. }
  176. }
  177. }
  178. sub merge_package_lists($$) {
  179. my $list1 = shift;
  180. my $list2 = shift;
  181. my @l = ();
  182. my %pkgs;
  183. foreach my $pkg (@$list1, @$list2) {
  184. $pkgs{$pkg} = 1;
  185. }
  186. foreach my $pkg (keys %pkgs) {
  187. push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
  188. }
  189. return sort(@l);
  190. }
  191. sub gen_target_mk() {
  192. my @target = parse_target_metadata();
  193. @target = sort {
  194. $a->{id} cmp $b->{id}
  195. } @target;
  196. foreach my $target (@target) {
  197. my ($profiles_def, $profiles_eval);
  198. my $conf = uc $target->{kernel}.'_'.$target->{board};
  199. $conf =~ tr/\.-/__/;
  200. foreach my $profile (@{$target->{profiles}}) {
  201. $profiles_def .= "
  202. define Profile/$conf\_$profile->{id}
  203. ID:=$profile->{id}
  204. NAME:=$profile->{name}
  205. PACKAGES:=".join(" ", merge_package_lists($target->{packages}, $profile->{packages}))."\n";
  206. $profile->{kconfig} and $profiles_def .= " KCONFIG:=1\n";
  207. $profiles_def .= " endef";
  208. $profiles_eval .= "
  209. \$(eval \$(call AddProfile,$conf\_$profile->{id}))"
  210. }
  211. print "
  212. ifeq (\$(CONFIG_LINUX_$conf),y)
  213. define Target
  214. KERNEL:=$target->{kernel}
  215. BOARD:=$target->{board}
  216. BOARDNAME:=$target->{name}
  217. LINUX_VERSION:=$target->{version}
  218. LINUX_RELEASE:=$target->{release}
  219. LINUX_KARCH:=$target->{karch}
  220. DEFAULT_PACKAGES:=".join(" ", @{$target->{packages}})."
  221. endef$profiles_def
  222. endif$profiles_eval
  223. "
  224. }
  225. print "\$(eval \$(call Target))\n";
  226. }
  227. sub target_config_features(@) {
  228. my $ret;
  229. while ($_ = shift @_) {
  230. /broken/ and $ret .= "\tdepends BROKEN\n";
  231. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  232. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  233. /atm/ and $ret .= "\tselect ATM_SUPPORT\n";
  234. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  235. /video/ and $ret .= "\tselect VIDEO_SUPPORT\n";
  236. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  237. /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
  238. /ext2/ and $ret .= "\tselect USES_EXT2\n";
  239. /tgz/ and $ret .= "\tselect USES_TGZ\n";
  240. }
  241. return $ret;
  242. }
  243. sub gen_target_config() {
  244. my @target = parse_target_metadata();
  245. @target = sort {
  246. $a->{name} cmp $b->{name}
  247. } @target;
  248. print <<EOF;
  249. choice
  250. prompt "Target System"
  251. default LINUX_2_4_BRCM
  252. reset if !DEVEL
  253. EOF
  254. foreach my $target (@target) {
  255. my $features = target_config_features(@{$target->{features}});
  256. my $help = $target->{desc};
  257. my $kernel = $target->{kernel};
  258. $kernel =~ tr/./_/;
  259. chomp $features;
  260. $features .= "\n";
  261. if ($help =~ /\w+/) {
  262. $help =~ s/^\s*/\t /mg;
  263. $help = "\thelp\n$help";
  264. } else {
  265. undef $help;
  266. }
  267. print <<EOF
  268. config LINUX_$target->{conf}
  269. bool "$target->{name}"
  270. select $target->{arch}
  271. select LINUX_$kernel
  272. $features$help
  273. EOF
  274. }
  275. print <<EOF;
  276. if DEVEL
  277. config LINUX_2_6_ARM
  278. bool "UNSUPPORTED little-endian arm platform"
  279. depends BROKEN
  280. select LINUX_2_6
  281. select arm
  282. config LINUX_2_6_CRIS
  283. bool "UNSUPPORTED cris platform"
  284. depends BROKEN
  285. select LINUX_2_6
  286. select cris
  287. config LINUX_2_6_M68K
  288. bool "UNSUPPORTED m68k platform"
  289. depends BROKEN
  290. select LINUX_2_6
  291. select m68k
  292. config LINUX_2_6_SH3
  293. bool "UNSUPPORTED little-endian sh3 platform"
  294. depends BROKEN
  295. select LINUX_2_6
  296. select sh3
  297. config LINUX_2_6_SH3EB
  298. bool "UNSUPPORTED big-endian sh3 platform"
  299. depends BROKEN
  300. select LINUX_2_6
  301. select sh3eb
  302. config LINUX_2_6_SH4
  303. bool "UNSUPPORTED little-endian sh4 platform"
  304. depends BROKEN
  305. select LINUX_2_6
  306. select sh4
  307. config LINUX_2_6_SH4EB
  308. bool "UNSUPPORTED big-endian sh4 platform"
  309. depends BROKEN
  310. select LINUX_2_6
  311. select sh4eb
  312. config LINUX_2_6_SPARC
  313. bool "UNSUPPORTED sparc platform"
  314. depends BROKEN
  315. select LINUX_2_6
  316. select sparc
  317. endif
  318. endchoice
  319. choice
  320. prompt "Target Profile"
  321. EOF
  322. foreach my $target (@target) {
  323. my $profiles = $target->{profiles};
  324. foreach my $profile (@$profiles) {
  325. print <<EOF;
  326. config LINUX_$target->{conf}_$profile->{id}
  327. bool "$profile->{name}"
  328. depends LINUX_$target->{conf}
  329. $profile->{config}
  330. EOF
  331. $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
  332. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  333. foreach my $pkg (@pkglist) {
  334. print "\tselect DEFAULT_$pkg\n";
  335. }
  336. print "\n";
  337. }
  338. }
  339. print "endchoice\n";
  340. }
  341. sub find_package_dep($$) {
  342. my $pkg = shift;
  343. my $name = shift;
  344. my $deps = ($pkg->{vdepends} or $pkg->{depends});
  345. return 0 unless defined $deps;
  346. foreach my $dep (@{$deps}) {
  347. return 1 if $dep eq $name;
  348. return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
  349. }
  350. return 0;
  351. }
  352. sub package_depends($$) {
  353. my $a = shift;
  354. my $b = shift;
  355. my $ret;
  356. return 0 if ($a->{submenu} ne $b->{submenu});
  357. if (find_package_dep($a, $b->{name}) == 1) {
  358. $ret = 1;
  359. } elsif (find_package_dep($b, $a->{name}) == 1) {
  360. $ret = -1;
  361. } else {
  362. return 0;
  363. }
  364. return $ret;
  365. }
  366. sub mconf_depends($$) {
  367. my $depends = shift;
  368. my $only_dep = shift;
  369. my $res;
  370. $depends or return;
  371. my @depends = @$depends;
  372. foreach my $depend (@depends) {
  373. my $m = "depends";
  374. $depend =~ s/^([@\+]+)//;
  375. my $flags = $1;
  376. my $vdep;
  377. if ($vdep = $package{$depend}->{vdepends}) {
  378. $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
  379. } else {
  380. $flags =~ /\+/ and do {
  381. next if $only_dep;
  382. $m = "select";
  383. # Menuconfig will not treat 'select FOO' as a real dependency
  384. # thus if FOO depends on other config options, these dependencies
  385. # will not be checked. To fix this, we simply emit all of FOO's
  386. # depends here as well.
  387. $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
  388. };
  389. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  390. }
  391. $res .= "\t\t$m $depend\n";
  392. }
  393. return $res;
  394. }
  395. sub print_package_config_category($) {
  396. my $cat = shift;
  397. my %menus;
  398. my %menu_dep;
  399. return unless $category{$cat};
  400. print "menu \"$cat\"\n\n";
  401. my %spkg = %{$category{$cat}};
  402. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  403. foreach my $pkg (@{$spkg{$spkg}}) {
  404. my $menu = $pkg->{submenu};
  405. if ($menu) {
  406. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  407. } else {
  408. $menu = 'undef';
  409. }
  410. $menus{$menu} or $menus{$menu} = [];
  411. push @{$menus{$menu}}, $pkg;
  412. print "\tconfig DEFAULT_".$pkg->{name}."\n";
  413. print "\t\tbool\n\n";
  414. }
  415. }
  416. my @menus = sort {
  417. ($a eq 'undef' ? 1 : 0) or
  418. ($b eq 'undef' ? -1 : 0) or
  419. ($a cmp $b)
  420. } keys %menus;
  421. foreach my $menu (@menus) {
  422. my @pkgs = sort {
  423. package_depends($a, $b) or
  424. ($a->{name} cmp $b->{name})
  425. } @{$menus{$menu}};
  426. if ($menu ne 'undef') {
  427. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  428. print "menu \"$menu\"\n";
  429. }
  430. foreach my $pkg (@pkgs) {
  431. my $title = $pkg->{name};
  432. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  433. if ($c > 0) {
  434. $title .= ("." x $c). " ". $pkg->{title};
  435. }
  436. print "\t";
  437. $pkg->{menu} and print "menu";
  438. print "config PACKAGE_".$pkg->{name}."\n";
  439. print "\t\ttristate \"$title\"\n";
  440. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  441. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  442. print "\t\tdefault $default\n";
  443. }
  444. print mconf_depends($pkg->{depends}, 0);
  445. print "\t\thelp\n";
  446. print $pkg->{description};
  447. print "\n";
  448. $pkg->{config} and print $pkg->{config}."\n";
  449. }
  450. if ($menu ne 'undef') {
  451. print "endmenu\n";
  452. $menu_dep{$menu} and print "endif\n";
  453. }
  454. }
  455. print "endmenu\n\n";
  456. undef $category{$cat};
  457. }
  458. sub gen_package_config() {
  459. parse_package_metadata();
  460. print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
  461. foreach my $preconfig (keys %preconfig) {
  462. foreach my $cfg (@{$preconfig{$preconfig}}) {
  463. my $conf = $cfg->{id};
  464. $conf =~ tr/\.-/__/;
  465. print <<EOF
  466. config UCI_PRECONFIG_$conf
  467. string "$cfg->{label}" if UCI_PRECONFIG
  468. depends PACKAGE_$preconfig
  469. default "$cfg->{default}"
  470. EOF
  471. }
  472. }
  473. print_package_config_category 'Base system';
  474. foreach my $cat (keys %category) {
  475. print_package_config_category $cat;
  476. }
  477. }
  478. sub gen_package_mk() {
  479. my %conf;
  480. my %dep;
  481. my $line;
  482. parse_package_metadata();
  483. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  484. my $config;
  485. my $pkg = $package{$name};
  486. next if defined $pkg->{vdepends};
  487. if ($ENV{SDK}) {
  488. $conf{$pkg->{src}} or do {
  489. $config = 'm';
  490. $conf{$pkg->{src}} = 1;
  491. };
  492. } else {
  493. $config = "\$(CONFIG_PACKAGE_$name)"
  494. }
  495. if ($config) {
  496. print "package-$config += $pkg->{subdir}$pkg->{src}\n";
  497. $pkg->{prereq} and print "prereq-$config += $pkg->{src}\n";
  498. }
  499. my $hasdeps = 0;
  500. my $depline = "";
  501. foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
  502. next if $dep =~ /@/;
  503. $dep =~ s/\+//;
  504. my $idx;
  505. my $pkg_dep = $package{$dep};
  506. next if defined $pkg_dep->{vdepends};
  507. if (defined $pkg_dep->{src}) {
  508. ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
  509. } elsif (defined($srcpackage{$dep})) {
  510. $idx = $subdir{$dep}.$dep;
  511. }
  512. undef $idx if $idx =~ /^(kernel)|(base-files)$/;
  513. if ($idx) {
  514. next if $dep{$pkg->{src}."->".$idx};
  515. $depline .= " $idx\-compile";
  516. $dep{$pkg->{src}."->".$idx} = 1;
  517. }
  518. }
  519. if ($depline) {
  520. $line .= $pkg->{subdir}."$pkg->{src}-compile: $depline\n";
  521. }
  522. }
  523. if ($line ne "") {
  524. print "\n$line";
  525. }
  526. foreach my $preconfig (keys %preconfig) {
  527. my $cmds;
  528. foreach my $cfg (@{$preconfig{$preconfig}}) {
  529. my $conf = $cfg->{id};
  530. $conf =~ tr/\.-/__/;
  531. $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
  532. }
  533. next unless $cmds;
  534. print <<EOF
  535. \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
  536. ( \\
  537. $cmds \\
  538. ) > \$@
  539. ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
  540. preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
  541. endif
  542. EOF
  543. }
  544. }
  545. sub parse_command() {
  546. my $cmd = shift @ARGV;
  547. for ($cmd) {
  548. /^target_mk$/ and return gen_target_mk();
  549. /^target_config$/ and return gen_target_config();
  550. /^package_mk$/ and return gen_package_mk();
  551. /^package_config$/ and return gen_package_config();
  552. /^kconfig/ and return gen_kconfig_overrides();
  553. }
  554. print <<EOF
  555. Available Commands:
  556. $0 target_mk [file] Target metadata in makefile format
  557. $0 target_config [file] Target metadata in Kconfig format
  558. $0 package_mk [file] Package metadata in makefile format
  559. $0 package_config [file] Package metadata in Kconfig format
  560. $0 kconfig [file] [config] Kernel config overrides
  561. EOF
  562. }
  563. parse_command();