target-metadata.pl 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. #!/usr/bin/env perl
  2. use FindBin;
  3. use lib "$FindBin::Bin";
  4. use strict;
  5. use metadata;
  6. use Getopt::Long;
  7. sub target_config_features(@) {
  8. my $ret;
  9. while ($_ = shift @_) {
  10. /arm_v(\w+)/ and $ret .= "\tselect arm_v$1\n";
  11. /broken/ and $ret .= "\tdepends on BROKEN\n";
  12. /audio/ and $ret .= "\tselect AUDIO_SUPPORT\n";
  13. /display/ and $ret .= "\tselect DISPLAY_SUPPORT\n";
  14. /dt/ and $ret .= "\tselect USES_DEVICETREE\n";
  15. /gpio/ and $ret .= "\tselect GPIO_SUPPORT\n";
  16. /pci/ and $ret .= "\tselect PCI_SUPPORT\n";
  17. /pcie/ and $ret .= "\tselect PCIE_SUPPORT\n";
  18. /usb/ and $ret .= "\tselect USB_SUPPORT\n";
  19. /usbgadget/ and $ret .= "\tselect USB_GADGET_SUPPORT\n";
  20. /pcmcia/ and $ret .= "\tselect PCMCIA_SUPPORT\n";
  21. /rtc/ and $ret .= "\tselect RTC_SUPPORT\n";
  22. /squashfs/ and $ret .= "\tselect USES_SQUASHFS\n";
  23. /jffs2$/ and $ret .= "\tselect USES_JFFS2\n";
  24. /jffs2_nand/ and $ret .= "\tselect USES_JFFS2_NAND\n";
  25. /ext4/ and $ret .= "\tselect USES_EXT4\n";
  26. /targz/ and $ret .= "\tselect USES_TARGZ\n";
  27. /cpiogz/ and $ret .= "\tselect USES_CPIOGZ\n";
  28. /minor/ and $ret .= "\tselect USES_MINOR\n";
  29. /ubifs/ and $ret .= "\tselect USES_UBIFS\n";
  30. /fpu/ and $ret .= "\tselect HAS_FPU\n";
  31. /spe_fpu/ and $ret .= "\tselect HAS_SPE_FPU\n";
  32. /ramdisk/ and $ret .= "\tselect USES_INITRAMFS\n";
  33. /powerpc64/ and $ret .= "\tselect powerpc64\n";
  34. /nommu/ and $ret .= "\tselect NOMMU\n";
  35. /mips16/ and $ret .= "\tselect HAS_MIPS16\n";
  36. /rfkill/ and $ret .= "\tselect RFKILL_SUPPORT\n";
  37. /low_mem/ and $ret .= "\tselect LOW_MEMORY_FOOTPRINT\n";
  38. /small_flash/ and $ret .= "\tselect SMALL_FLASH\n";
  39. /nand/ and $ret .= "\tselect NAND_SUPPORT\n";
  40. }
  41. return $ret;
  42. }
  43. sub target_name($) {
  44. my $target = shift;
  45. my $parent = $target->{parent};
  46. if ($parent) {
  47. return $target->{parent}->{name}." - ".$target->{name};
  48. } else {
  49. return $target->{name};
  50. }
  51. }
  52. sub kver($) {
  53. my $v = shift;
  54. $v =~ tr/\./_/;
  55. if (substr($v,0,2) eq "2_") {
  56. $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
  57. } else {
  58. $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
  59. }
  60. return $v;
  61. }
  62. sub print_target($) {
  63. my $target = shift;
  64. my $features = target_config_features(@{$target->{features}});
  65. my $help = $target->{desc};
  66. my $confstr;
  67. chomp $features;
  68. $features .= "\n";
  69. if ($help =~ /\w+/) {
  70. $help =~ s/^\s*/\t /mg;
  71. $help = "\thelp\n$help";
  72. } else {
  73. undef $help;
  74. }
  75. my $v = kver($target->{version});
  76. if (@{$target->{subtargets}} == 0) {
  77. $confstr = <<EOF;
  78. config TARGET_$target->{conf}
  79. bool "$target->{name}"
  80. select LINUX_$v
  81. EOF
  82. }
  83. else {
  84. $confstr = <<EOF;
  85. config TARGET_$target->{conf}
  86. bool "$target->{name}"
  87. EOF
  88. }
  89. if ($target->{subtarget}) {
  90. $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
  91. }
  92. if (@{$target->{subtargets}} > 0) {
  93. $confstr .= "\tselect HAS_SUBTARGETS\n";
  94. grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
  95. } else {
  96. $confstr .= $features;
  97. if ($target->{arch} =~ /\w/) {
  98. $confstr .= "\tselect $target->{arch}\n";
  99. }
  100. if ($target->{has_devices}) {
  101. $confstr .= "\tselect HAS_DEVICES\n";
  102. }
  103. }
  104. foreach my $dep (@{$target->{depends}}) {
  105. my $mode = "depends on";
  106. my $flags;
  107. my $name;
  108. $dep =~ /^([@\+\-]+)(.+)$/;
  109. $flags = $1;
  110. $name = $2;
  111. next if $name =~ /:/;
  112. $flags =~ /-/ and $mode = "deselect";
  113. $flags =~ /\+/ and $mode = "select";
  114. $flags =~ /@/ and $confstr .= "\t$mode $name\n";
  115. }
  116. $confstr .= "$help\n\n";
  117. print $confstr;
  118. }
  119. sub merge_package_lists($$) {
  120. my $list1 = shift;
  121. my $list2 = shift;
  122. my @l = ();
  123. my %pkgs;
  124. foreach my $pkg (@$list1, @$list2) {
  125. $pkgs{$pkg} = 1;
  126. }
  127. foreach my $pkg (keys %pkgs) {
  128. push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
  129. }
  130. return sort(@l);
  131. }
  132. sub gen_target_config() {
  133. my $file = shift @ARGV;
  134. my @target = parse_target_metadata($file);
  135. my %defaults;
  136. my @target_sort = sort {
  137. target_name($a) cmp target_name($b);
  138. } @target;
  139. print <<EOF;
  140. choice
  141. prompt "Target System"
  142. default TARGET_ar71xx
  143. reset if !DEVEL
  144. EOF
  145. foreach my $target (@target_sort) {
  146. next if $target->{subtarget};
  147. print_target($target);
  148. }
  149. print <<EOF;
  150. endchoice
  151. choice
  152. prompt "Subtarget" if HAS_SUBTARGETS
  153. EOF
  154. foreach my $target (@target) {
  155. next unless $target->{def_subtarget};
  156. print <<EOF;
  157. default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
  158. EOF
  159. }
  160. print <<EOF;
  161. EOF
  162. foreach my $target (@target) {
  163. next unless $target->{subtarget};
  164. print_target($target);
  165. }
  166. print <<EOF;
  167. endchoice
  168. choice
  169. prompt "Target Profile"
  170. EOF
  171. foreach my $target (@target) {
  172. my $profile = $target->{profiles}->[0];
  173. $profile or next;
  174. print <<EOF;
  175. default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf}
  176. EOF
  177. }
  178. print <<EOF;
  179. config TARGET_MULTI_PROFILE
  180. bool "Multiple devices"
  181. depends on HAS_DEVICES
  182. EOF
  183. foreach my $target (@target) {
  184. my $profiles = $target->{profiles};
  185. foreach my $profile (@{$target->{profiles}}) {
  186. print <<EOF;
  187. config TARGET_$target->{conf}_$profile->{id}
  188. bool "$profile->{name}"
  189. depends on TARGET_$target->{conf}
  190. EOF
  191. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  192. foreach my $pkg (@pkglist) {
  193. print "\tselect DEFAULT_$pkg\n";
  194. $defaults{$pkg} = 1;
  195. }
  196. my $help = $profile->{desc};
  197. if ($help =~ /\w+/) {
  198. $help =~ s/^\s*/\t /mg;
  199. $help = "\thelp\n$help";
  200. } else {
  201. undef $help;
  202. }
  203. print "$help\n";
  204. }
  205. }
  206. print <<EOF;
  207. endchoice
  208. menu "Target Devices"
  209. depends on TARGET_MULTI_PROFILE
  210. EOF
  211. foreach my $target (@target) {
  212. my $profiles = $target->{profiles};
  213. foreach my $profile (@{$target->{profiles}}) {
  214. next unless $profile->{id} =~ /^DEVICE_/;
  215. print <<EOF;
  216. config TARGET_DEVICE_$target->{conf}_$profile->{id}
  217. bool "$profile->{name}"
  218. depends on TARGET_$target->{conf}
  219. EOF
  220. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  221. foreach my $pkg (@pkglist) {
  222. print "\tselect DEFAULT_$pkg\n";
  223. $defaults{$pkg} = 1;
  224. }
  225. }
  226. }
  227. print <<EOF;
  228. endmenu
  229. config HAS_SUBTARGETS
  230. bool
  231. config HAS_DEVICES
  232. bool
  233. config TARGET_BOARD
  234. string
  235. EOF
  236. foreach my $target (@target) {
  237. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  238. }
  239. print <<EOF;
  240. config TARGET_SUBTARGET
  241. string
  242. default "generic" if !HAS_SUBTARGETS
  243. EOF
  244. foreach my $target (@target) {
  245. foreach my $subtarget (@{$target->{subtargets}}) {
  246. print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
  247. }
  248. }
  249. print <<EOF;
  250. config TARGET_PROFILE
  251. string
  252. EOF
  253. foreach my $target (@target) {
  254. my $profiles = $target->{profiles};
  255. foreach my $profile (@$profiles) {
  256. print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
  257. }
  258. }
  259. print <<EOF;
  260. config TARGET_ARCH_PACKAGES
  261. string
  262. EOF
  263. foreach my $target (@target) {
  264. next if @{$target->{subtargets}} > 0;
  265. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  266. }
  267. print <<EOF;
  268. config DEFAULT_TARGET_OPTIMIZATION
  269. string
  270. EOF
  271. foreach my $target (@target) {
  272. next if @{$target->{subtargets}} > 0;
  273. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  274. }
  275. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  276. print <<EOF;
  277. config CPU_TYPE
  278. string
  279. EOF
  280. foreach my $target (@target) {
  281. next if @{$target->{subtargets}} > 0;
  282. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  283. }
  284. print "\tdefault \"\"\n";
  285. my %kver;
  286. foreach my $target (@target) {
  287. my $v = kver($target->{version});
  288. next if $kver{$v};
  289. $kver{$v} = 1;
  290. print <<EOF;
  291. config LINUX_$v
  292. bool
  293. EOF
  294. }
  295. foreach my $def (sort keys %defaults) {
  296. print "\tconfig DEFAULT_".$def."\n";
  297. print "\t\tbool\n\n";
  298. }
  299. }
  300. sub gen_profile_mk() {
  301. my $file = shift @ARGV;
  302. my $target = shift @ARGV;
  303. my @targets = parse_target_metadata($file);
  304. foreach my $cur (@targets) {
  305. next unless $cur->{id} eq $target;
  306. print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
  307. foreach my $profile (@{$cur->{profiles}}) {
  308. print $profile->{id}.'_NAME:='.$profile->{name}."\n";
  309. print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
  310. }
  311. }
  312. }
  313. sub parse_command() {
  314. GetOptions("ignore=s", \@ignore);
  315. my $cmd = shift @ARGV;
  316. for ($cmd) {
  317. /^config$/ and return gen_target_config();
  318. /^profile_mk$/ and return gen_profile_mk();
  319. }
  320. die <<EOF
  321. Available Commands:
  322. $0 config [file] Target metadata in Kconfig format
  323. $0 profile_mk [file] [target] Profile metadata in makefile format
  324. EOF
  325. }
  326. parse_command();