metadata.pl 14 KB

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