metadata.pl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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*(.+)\s*$/ and do {
  23. my $conf = $1;
  24. $conf =~ tr#/\.\-/#___#;
  25. $target = {
  26. id => $1,
  27. conf => $conf,
  28. profiles => []
  29. };
  30. push @target, $target;
  31. };
  32. /^Target-Board:\s*(.+)\s*$/ and $target->{board} = $1;
  33. /^Target-Kernel:\s*(\d+\.\d+)\s*$/ and $target->{kernel} = $1;
  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 target_config_features(@) {
  192. my $ret;
  193. while ($_ = shift @_) {
  194. /broken/ and $ret .= "\tdepends BROKEN\n";
  195. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  196. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  197. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  198. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  199. /jffs2/ and $ret .= "\tselect USES_JFFS2\n";
  200. /ext2/ and $ret .= "\tselect USES_EXT2\n";
  201. /tgz/ and $ret .= "\tselect USES_TGZ\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 TARGET_brcm_2_4
  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 TARGET_$target->{conf}
  231. bool "$target->{name}"
  232. select $target->{arch}
  233. select LINUX_$kernel
  234. $features$help
  235. EOF
  236. }
  237. print <<EOF;
  238. endchoice
  239. config TARGET_BOARD
  240. string
  241. EOF
  242. foreach my $target (@target) {
  243. print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  244. }
  245. print <<EOF;
  246. choice
  247. prompt "Target Profile"
  248. EOF
  249. foreach my $target (@target) {
  250. my $profiles = $target->{profiles};
  251. foreach my $profile (@$profiles) {
  252. print <<EOF;
  253. config TARGET_$target->{conf}_$profile->{id}
  254. bool "$profile->{name}"
  255. depends TARGET_$target->{conf}
  256. $profile->{config}
  257. EOF
  258. $profile->{kconfig} and print "\tselect PROFILE_KCONFIG\n";
  259. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  260. foreach my $pkg (@pkglist) {
  261. print "\tselect DEFAULT_$pkg\n";
  262. }
  263. print "\n";
  264. }
  265. }
  266. print "endchoice\n";
  267. }
  268. sub find_package_dep($$) {
  269. my $pkg = shift;
  270. my $name = shift;
  271. my $deps = ($pkg->{vdepends} or $pkg->{depends});
  272. return 0 unless defined $deps;
  273. foreach my $dep (@{$deps}) {
  274. return 1 if $dep eq $name;
  275. return 1 if ($package{$dep} and (find_package_dep($package{$dep},$name) == 1));
  276. }
  277. return 0;
  278. }
  279. sub package_depends($$) {
  280. my $a = shift;
  281. my $b = shift;
  282. my $ret;
  283. return 0 if ($a->{submenu} ne $b->{submenu});
  284. if (find_package_dep($a, $b->{name}) == 1) {
  285. $ret = 1;
  286. } elsif (find_package_dep($b, $a->{name}) == 1) {
  287. $ret = -1;
  288. } else {
  289. return 0;
  290. }
  291. return $ret;
  292. }
  293. sub mconf_depends($$) {
  294. my $depends = shift;
  295. my $only_dep = shift;
  296. my $res;
  297. $depends or return;
  298. my @depends = @$depends;
  299. foreach my $depend (@depends) {
  300. my $m = "depends";
  301. $depend =~ s/^([@\+]+)//;
  302. my $flags = $1;
  303. my $vdep;
  304. if ($vdep = $package{$depend}->{vdepends}) {
  305. $depend = join("||", map { "PACKAGE_".$_ } @$vdep);
  306. } else {
  307. $flags =~ /\+/ and do {
  308. next if $only_dep;
  309. $m = "select";
  310. # Menuconfig will not treat 'select FOO' as a real dependency
  311. # thus if FOO depends on other config options, these dependencies
  312. # will not be checked. To fix this, we simply emit all of FOO's
  313. # depends here as well.
  314. $package{$depend} and $res .= mconf_depends($package{$depend}->{depends}, 1);
  315. };
  316. $flags =~ /@/ or $depend = "PACKAGE_$depend";
  317. }
  318. $res .= "\t\t$m $depend\n";
  319. }
  320. return $res;
  321. }
  322. sub print_package_config_category($) {
  323. my $cat = shift;
  324. my %menus;
  325. my %menu_dep;
  326. return unless $category{$cat};
  327. print "menu \"$cat\"\n\n";
  328. my %spkg = %{$category{$cat}};
  329. foreach my $spkg (sort {uc($a) cmp uc($b)} keys %spkg) {
  330. foreach my $pkg (@{$spkg{$spkg}}) {
  331. my $menu = $pkg->{submenu};
  332. if ($menu) {
  333. $menu_dep{$menu} or $menu_dep{$menu} = $pkg->{submenudep};
  334. } else {
  335. $menu = 'undef';
  336. }
  337. $menus{$menu} or $menus{$menu} = [];
  338. push @{$menus{$menu}}, $pkg;
  339. print "\tconfig DEFAULT_".$pkg->{name}."\n";
  340. print "\t\tbool\n\n";
  341. }
  342. }
  343. my @menus = sort {
  344. ($a eq 'undef' ? 1 : 0) or
  345. ($b eq 'undef' ? -1 : 0) or
  346. ($a cmp $b)
  347. } keys %menus;
  348. foreach my $menu (@menus) {
  349. my @pkgs = sort {
  350. package_depends($a, $b) or
  351. ($a->{name} cmp $b->{name})
  352. } @{$menus{$menu}};
  353. if ($menu ne 'undef') {
  354. $menu_dep{$menu} and print "if $menu_dep{$menu}\n";
  355. print "menu \"$menu\"\n";
  356. }
  357. foreach my $pkg (@pkgs) {
  358. my $title = $pkg->{name};
  359. my $c = (72 - length($pkg->{name}) - length($pkg->{title}));
  360. if ($c > 0) {
  361. $title .= ("." x $c). " ". $pkg->{title};
  362. }
  363. print "\t";
  364. $pkg->{menu} and print "menu";
  365. print "config PACKAGE_".$pkg->{name}."\n";
  366. print "\t\ttristate \"$title\"\n";
  367. print "\t\tdefault y if DEFAULT_".$pkg->{name}."\n";
  368. foreach my $default (split /\s*,\s*/, $pkg->{default}) {
  369. print "\t\tdefault $default\n";
  370. }
  371. print mconf_depends($pkg->{depends}, 0);
  372. print "\t\thelp\n";
  373. print $pkg->{description};
  374. print "\n";
  375. $pkg->{config} and print $pkg->{config}."\n";
  376. }
  377. if ($menu ne 'undef') {
  378. print "endmenu\n";
  379. $menu_dep{$menu} and print "endif\n";
  380. }
  381. }
  382. print "endmenu\n\n";
  383. undef $category{$cat};
  384. }
  385. sub gen_package_config() {
  386. parse_package_metadata();
  387. print "menuconfig UCI_PRECONFIG\n\tbool \"Image configuration\"\n";
  388. foreach my $preconfig (keys %preconfig) {
  389. foreach my $cfg (@{$preconfig{$preconfig}}) {
  390. my $conf = $cfg->{id};
  391. $conf =~ tr/\.-/__/;
  392. print <<EOF
  393. config UCI_PRECONFIG_$conf
  394. string "$cfg->{label}" if UCI_PRECONFIG
  395. depends PACKAGE_$preconfig
  396. default "$cfg->{default}"
  397. EOF
  398. }
  399. }
  400. print_package_config_category 'Base system';
  401. foreach my $cat (keys %category) {
  402. print_package_config_category $cat;
  403. }
  404. }
  405. sub gen_package_mk() {
  406. my %conf;
  407. my %dep;
  408. my $line;
  409. parse_package_metadata();
  410. foreach my $name (sort {uc($a) cmp uc($b)} keys %package) {
  411. my $config;
  412. my $pkg = $package{$name};
  413. next if defined $pkg->{vdepends};
  414. if ($ENV{SDK}) {
  415. $conf{$pkg->{src}} or do {
  416. $config = 'm';
  417. $conf{$pkg->{src}} = 1;
  418. };
  419. } else {
  420. $config = "\$(CONFIG_PACKAGE_$name)"
  421. }
  422. if ($config) {
  423. print "package-$config += $pkg->{subdir}$pkg->{src}\n";
  424. $pkg->{prereq} and print "prereq-$config += $pkg->{subdir}$pkg->{src}\n";
  425. }
  426. my $hasdeps = 0;
  427. my $depline = "";
  428. foreach my $dep (@{$pkg->{depends}}, @{$pkg->{builddepends}}) {
  429. next if $dep =~ /@/;
  430. $dep =~ s/\+//;
  431. my $idx;
  432. my $pkg_dep = $package{$dep};
  433. next if defined $pkg_dep->{vdepends};
  434. if (defined $pkg_dep->{src}) {
  435. ($pkg->{src} ne $pkg_dep->{src}) and $idx = $pkg_dep->{subdir}.$pkg_dep->{src};
  436. } elsif (defined($srcpackage{$dep})) {
  437. $idx = $subdir{$dep}.$dep;
  438. }
  439. undef $idx if $idx =~ /^(kernel)|(base-files)$/;
  440. if ($idx) {
  441. next if $dep{$pkg->{src}."->".$idx};
  442. $depline .= " \$(curdir)/$idx/compile";
  443. $dep{$pkg->{src}."->".$idx} = 1;
  444. }
  445. }
  446. if ($depline) {
  447. $line .= "\$(curdir)/".$pkg->{subdir}."$pkg->{src}/compile += $depline\n";
  448. }
  449. }
  450. if ($line ne "") {
  451. print "\n$line";
  452. }
  453. foreach my $preconfig (keys %preconfig) {
  454. my $cmds;
  455. foreach my $cfg (@{$preconfig{$preconfig}}) {
  456. my $conf = $cfg->{id};
  457. $conf =~ tr/\.-/__/;
  458. $cmds .= "\techo \"uci set '$cfg->{id}=\$(subst \",,\$(CONFIG_UCI_PRECONFIG_$conf))'\"; \\\n";
  459. }
  460. next unless $cmds;
  461. print <<EOF
  462. \$(TARGET_DIR)/etc/uci-defaults/$preconfig: FORCE
  463. ( \\
  464. $cmds \\
  465. ) > \$@
  466. ifneq (\$(UCI_PRECONFIG)\$(CONFIG_UCI_PRECONFIG),)
  467. package/preconfig: \$(TARGET_DIR)/etc/uci-defaults/$preconfig
  468. endif
  469. EOF
  470. }
  471. }
  472. sub parse_command() {
  473. my $cmd = shift @ARGV;
  474. for ($cmd) {
  475. /^target_config$/ and return gen_target_config();
  476. /^package_mk$/ and return gen_package_mk();
  477. /^package_config$/ and return gen_package_config();
  478. /^kconfig/ and return gen_kconfig_overrides();
  479. }
  480. print <<EOF
  481. Available Commands:
  482. $0 target_config [file] Target metadata in Kconfig format
  483. $0 package_mk [file] Package metadata in makefile format
  484. $0 package_config [file] Package metadata in Kconfig format
  485. $0 kconfig [file] [config] Kernel config overrides
  486. EOF
  487. }
  488. parse_command();