metadata.pl 12 KB

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