metadata.pl 14 KB

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