metadata.pl 14 KB

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