2
0

target-metadata.pl 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. foreach my $target (@target_sort) {
  140. next if @{$target->{subtargets}} > 0;
  141. print <<EOF;
  142. config DEFAULT_TARGET_$target->{conf}
  143. bool
  144. depends on TARGET_PER_DEVICE_ROOTFS
  145. default y if TARGET_$target->{conf}
  146. EOF
  147. foreach my $pkg (@{$target->{packages}}) {
  148. print "\tselect DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  149. }
  150. }
  151. print <<EOF;
  152. choice
  153. prompt "Target System"
  154. default TARGET_ar71xx
  155. reset if !DEVEL
  156. EOF
  157. foreach my $target (@target_sort) {
  158. next if $target->{subtarget};
  159. print_target($target);
  160. }
  161. print <<EOF;
  162. endchoice
  163. choice
  164. prompt "Subtarget" if HAS_SUBTARGETS
  165. EOF
  166. foreach my $target (@target) {
  167. next unless $target->{def_subtarget};
  168. print <<EOF;
  169. default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
  170. EOF
  171. }
  172. print <<EOF;
  173. EOF
  174. foreach my $target (@target) {
  175. next unless $target->{subtarget};
  176. print_target($target);
  177. }
  178. print <<EOF;
  179. endchoice
  180. choice
  181. prompt "Target Profile"
  182. EOF
  183. foreach my $target (@target) {
  184. my $profile = $target->{profiles}->[0];
  185. $profile or next;
  186. print <<EOF;
  187. default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf}
  188. EOF
  189. }
  190. print <<EOF;
  191. config TARGET_MULTI_PROFILE
  192. bool "Multiple devices"
  193. depends on HAS_DEVICES
  194. EOF
  195. foreach my $target (@target) {
  196. my $profiles = $target->{profiles};
  197. foreach my $profile (@{$target->{profiles}}) {
  198. print <<EOF;
  199. config TARGET_$target->{conf}_$profile->{id}
  200. bool "$profile->{name}"
  201. depends on TARGET_$target->{conf}
  202. EOF
  203. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  204. foreach my $pkg (@pkglist) {
  205. print "\tselect DEFAULT_$pkg\n";
  206. $defaults{$pkg} = 1;
  207. }
  208. my $help = $profile->{desc};
  209. if ($help =~ /\w+/) {
  210. $help =~ s/^\s*/\t /mg;
  211. $help = "\thelp\n$help";
  212. } else {
  213. undef $help;
  214. }
  215. print "$help\n";
  216. }
  217. }
  218. print <<EOF;
  219. endchoice
  220. menu "Target Devices"
  221. depends on TARGET_MULTI_PROFILE
  222. config TARGET_ALL_PROFILES
  223. bool "Enable all profiles by default"
  224. config TARGET_PER_DEVICE_ROOTFS
  225. bool "Use a per-device root filesystem that adds profile packages"
  226. EOF
  227. foreach my $target (@target) {
  228. my $profiles = $target->{profiles};
  229. foreach my $profile (@{$target->{profiles}}) {
  230. next unless $profile->{id} =~ /^DEVICE_/;
  231. print <<EOF;
  232. config TARGET_DEVICE_$target->{conf}_$profile->{id}
  233. bool "$profile->{name}"
  234. depends on TARGET_$target->{conf}
  235. default y if TARGET_ALL_PROFILES
  236. EOF
  237. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  238. foreach my $pkg (@pkglist) {
  239. print "\tselect DEFAULT_$pkg if !TARGET_PER_DEVICE_ROOTFS\n";
  240. print "\tselect MODULE_DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  241. $defaults{$pkg} = 1;
  242. }
  243. }
  244. }
  245. print <<EOF;
  246. endmenu
  247. config HAS_SUBTARGETS
  248. bool
  249. config HAS_DEVICES
  250. bool
  251. config TARGET_BOARD
  252. string
  253. EOF
  254. foreach my $target (@target) {
  255. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  256. }
  257. print <<EOF;
  258. config TARGET_SUBTARGET
  259. string
  260. default "generic" if !HAS_SUBTARGETS
  261. EOF
  262. foreach my $target (@target) {
  263. foreach my $subtarget (@{$target->{subtargets}}) {
  264. print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
  265. }
  266. }
  267. print <<EOF;
  268. config TARGET_PROFILE
  269. string
  270. EOF
  271. foreach my $target (@target) {
  272. my $profiles = $target->{profiles};
  273. foreach my $profile (@$profiles) {
  274. print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
  275. }
  276. }
  277. print <<EOF;
  278. config TARGET_ARCH_PACKAGES
  279. string
  280. EOF
  281. foreach my $target (@target) {
  282. next if @{$target->{subtargets}} > 0;
  283. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  284. }
  285. print <<EOF;
  286. config DEFAULT_TARGET_OPTIMIZATION
  287. string
  288. EOF
  289. foreach my $target (@target) {
  290. next if @{$target->{subtargets}} > 0;
  291. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  292. }
  293. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  294. print <<EOF;
  295. config CPU_TYPE
  296. string
  297. EOF
  298. foreach my $target (@target) {
  299. next if @{$target->{subtargets}} > 0;
  300. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  301. }
  302. print "\tdefault \"\"\n";
  303. my %kver;
  304. foreach my $target (@target) {
  305. my $v = kver($target->{version});
  306. next if $kver{$v};
  307. $kver{$v} = 1;
  308. print <<EOF;
  309. config LINUX_$v
  310. bool
  311. EOF
  312. }
  313. foreach my $def (sort keys %defaults) {
  314. print <<EOF;
  315. config DEFAULT_$def
  316. bool
  317. config MODULE_DEFAULT_$def
  318. tristate
  319. depends on TARGET_PER_DEVICE_ROOTFS
  320. depends on m
  321. default m if DEFAULT_$def
  322. select PACKAGE_$def
  323. EOF
  324. }
  325. }
  326. sub gen_profile_mk() {
  327. my $file = shift @ARGV;
  328. my $target = shift @ARGV;
  329. my @targets = parse_target_metadata($file);
  330. foreach my $cur (@targets) {
  331. next unless $cur->{id} eq $target;
  332. print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
  333. foreach my $profile (@{$cur->{profiles}}) {
  334. print $profile->{id}.'_NAME:='.$profile->{name}."\n";
  335. print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
  336. }
  337. }
  338. }
  339. sub parse_command() {
  340. GetOptions("ignore=s", \@ignore);
  341. my $cmd = shift @ARGV;
  342. for ($cmd) {
  343. /^config$/ and return gen_target_config();
  344. /^profile_mk$/ and return gen_profile_mk();
  345. }
  346. die <<EOF
  347. Available Commands:
  348. $0 config [file] Target metadata in Kconfig format
  349. $0 profile_mk [file] [target] Profile metadata in makefile format
  350. EOF
  351. }
  352. parse_command();