target-metadata.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /virtio/ and $ret .= "\tselect VIRTIO_SUPPORT\n";
  41. }
  42. return $ret;
  43. }
  44. sub target_name($) {
  45. my $target = shift;
  46. my $parent = $target->{parent};
  47. if ($parent) {
  48. return $target->{parent}->{name}." - ".$target->{name};
  49. } else {
  50. return $target->{name};
  51. }
  52. }
  53. sub kver($) {
  54. my $v = shift;
  55. $v =~ tr/\./_/;
  56. if (substr($v,0,2) eq "2_") {
  57. $v =~ /(\d+_\d+_\d+)(_\d+)?/ and $v = $1;
  58. } else {
  59. $v =~ /(\d+_\d+)(_\d+)?/ and $v = $1;
  60. }
  61. return $v;
  62. }
  63. sub print_target($) {
  64. my $target = shift;
  65. my $features = target_config_features(@{$target->{features}});
  66. my $help = $target->{desc};
  67. my $confstr;
  68. chomp $features;
  69. $features .= "\n";
  70. if ($help =~ /\w+/) {
  71. $help =~ s/^\s*/\t /mg;
  72. $help = "\thelp\n$help";
  73. } else {
  74. undef $help;
  75. }
  76. my $v = kver($target->{version});
  77. if (@{$target->{subtargets}} == 0) {
  78. $confstr = <<EOF;
  79. config TARGET_$target->{conf}
  80. bool "$target->{name}"
  81. select LINUX_$v
  82. EOF
  83. }
  84. else {
  85. $confstr = <<EOF;
  86. config TARGET_$target->{conf}
  87. bool "$target->{name}"
  88. EOF
  89. }
  90. if ($target->{subtarget}) {
  91. $confstr .= "\tdepends on TARGET_$target->{boardconf}\n";
  92. }
  93. if (@{$target->{subtargets}} > 0) {
  94. $confstr .= "\tselect HAS_SUBTARGETS\n";
  95. grep { /broken/ } @{$target->{features}} and $confstr .= "\tdepends on BROKEN\n";
  96. } else {
  97. $confstr .= $features;
  98. if ($target->{arch} =~ /\w/) {
  99. $confstr .= "\tselect $target->{arch}\n";
  100. }
  101. if ($target->{has_devices}) {
  102. $confstr .= "\tselect HAS_DEVICES\n";
  103. }
  104. }
  105. foreach my $dep (@{$target->{depends}}) {
  106. my $mode = "depends on";
  107. my $flags;
  108. my $name;
  109. $dep =~ /^([@\+\-]+)(.+)$/;
  110. $flags = $1;
  111. $name = $2;
  112. next if $name =~ /:/;
  113. $flags =~ /-/ and $mode = "deselect";
  114. $flags =~ /\+/ and $mode = "select";
  115. $flags =~ /@/ and $confstr .= "\t$mode $name\n";
  116. }
  117. $confstr .= "$help\n\n";
  118. print $confstr;
  119. }
  120. sub merge_package_lists($$) {
  121. my $list1 = shift;
  122. my $list2 = shift;
  123. my @l = ();
  124. my %pkgs;
  125. foreach my $pkg (@$list1, @$list2) {
  126. $pkgs{$pkg} = 1;
  127. }
  128. foreach my $pkg (keys %pkgs) {
  129. push @l, $pkg unless ($pkg =~ /^-/ or $pkgs{"-$pkg"});
  130. }
  131. return sort(@l);
  132. }
  133. sub gen_target_config() {
  134. my $file = shift @ARGV;
  135. my @target = parse_target_metadata($file);
  136. my %defaults;
  137. my @target_sort = sort {
  138. target_name($a) cmp target_name($b);
  139. } @target;
  140. foreach my $target (@target_sort) {
  141. next if @{$target->{subtargets}} > 0;
  142. print <<EOF;
  143. config DEFAULT_TARGET_$target->{conf}
  144. bool
  145. depends on TARGET_PER_DEVICE_ROOTFS
  146. default y if TARGET_$target->{conf}
  147. EOF
  148. foreach my $pkg (@{$target->{packages}}) {
  149. print "\tselect DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  150. }
  151. }
  152. print <<EOF;
  153. choice
  154. prompt "Target System"
  155. default TARGET_ar71xx
  156. reset if !DEVEL
  157. EOF
  158. foreach my $target (@target_sort) {
  159. next if $target->{subtarget};
  160. print_target($target);
  161. }
  162. print <<EOF;
  163. endchoice
  164. choice
  165. prompt "Subtarget" if HAS_SUBTARGETS
  166. EOF
  167. foreach my $target (@target) {
  168. next unless $target->{def_subtarget};
  169. print <<EOF;
  170. default TARGET_$target->{conf}_$target->{def_subtarget} if TARGET_$target->{conf}
  171. EOF
  172. }
  173. print <<EOF;
  174. EOF
  175. foreach my $target (@target) {
  176. next unless $target->{subtarget};
  177. print_target($target);
  178. }
  179. print <<EOF;
  180. endchoice
  181. choice
  182. prompt "Target Profile"
  183. EOF
  184. foreach my $target (@target) {
  185. my $profile = $target->{profiles}->[0];
  186. $profile or next;
  187. print <<EOF;
  188. default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf}
  189. EOF
  190. }
  191. print <<EOF;
  192. config TARGET_MULTI_PROFILE
  193. bool "Multiple devices"
  194. depends on HAS_DEVICES
  195. help
  196. Instead of only building a single image, or all images, this allows you
  197. to select images to be built for multiple devices in one build.
  198. EOF
  199. foreach my $target (@target) {
  200. my $profiles = $target->{profiles};
  201. foreach my $profile (@{$target->{profiles}}) {
  202. print <<EOF;
  203. config TARGET_$target->{conf}_$profile->{id}
  204. bool "$profile->{name}"
  205. depends on TARGET_$target->{conf}
  206. EOF
  207. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  208. foreach my $pkg (@pkglist) {
  209. print "\tselect DEFAULT_$pkg\n";
  210. $defaults{$pkg} = 1;
  211. }
  212. my $help = $profile->{desc};
  213. if ($help =~ /\w+/) {
  214. $help =~ s/^\s*/\t /mg;
  215. $help = "\thelp\n$help";
  216. } else {
  217. undef $help;
  218. }
  219. print "$help\n";
  220. }
  221. }
  222. print <<EOF;
  223. endchoice
  224. menu "Target Devices"
  225. depends on TARGET_MULTI_PROFILE
  226. config TARGET_ALL_PROFILES
  227. bool "Enable all profiles by default"
  228. config TARGET_PER_DEVICE_ROOTFS
  229. bool "Use a per-device root filesystem that adds profile packages"
  230. help
  231. When disabled, all device packages from all selected devices
  232. will be included in all images by default. (Marked as <*>) You will
  233. still be able to manually deselect any/all packages.
  234. When enabled, each device builds it's own image, including only the
  235. profile packages for that device. (Marked as {M}) You will be able
  236. to change a package to included in all images by marking as {*}, but
  237. will not be able to disable a profile package completely.
  238. To get the most use of this setting, you must set in a .config stub
  239. before calling "make defconfig". Selecting TARGET_MULTI_PROFILE and
  240. then manually selecting (via menuconfig for instance) this option
  241. will have pre-defaulted all profile packages to included, making this
  242. option appear to have had no effect.
  243. EOF
  244. foreach my $target (@target) {
  245. my @profiles = sort {
  246. my $x = $a->{name};
  247. my $y = $b->{name};
  248. "\L$x" cmp "\L$y";
  249. } @{$target->{profiles}};
  250. foreach my $profile (@profiles) {
  251. next unless $profile->{id} =~ /^DEVICE_/;
  252. print <<EOF;
  253. menuconfig TARGET_DEVICE_$target->{conf}_$profile->{id}
  254. bool "$profile->{name}"
  255. depends on TARGET_$target->{conf}
  256. default y if TARGET_ALL_PROFILES
  257. EOF
  258. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  259. foreach my $pkg (@pkglist) {
  260. print "\tselect DEFAULT_$pkg if !TARGET_PER_DEVICE_ROOTFS\n";
  261. print "\tselect MODULE_DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  262. $defaults{$pkg} = 1;
  263. }
  264. print <<EOF;
  265. config TARGET_DEVICE_PACKAGES_$target->{conf}_$profile->{id}
  266. string "$profile->{name} additional packages"
  267. default ""
  268. depends on TARGET_PER_DEVICE_ROOTFS
  269. depends on TARGET_DEVICE_$target->{conf}_$profile->{id}
  270. EOF
  271. }
  272. }
  273. print <<EOF;
  274. endmenu
  275. config HAS_SUBTARGETS
  276. bool
  277. config HAS_DEVICES
  278. bool
  279. config TARGET_BOARD
  280. string
  281. EOF
  282. foreach my $target (@target) {
  283. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  284. }
  285. print <<EOF;
  286. config TARGET_SUBTARGET
  287. string
  288. default "generic" if !HAS_SUBTARGETS
  289. EOF
  290. foreach my $target (@target) {
  291. foreach my $subtarget (@{$target->{subtargets}}) {
  292. print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
  293. }
  294. }
  295. print <<EOF;
  296. config TARGET_PROFILE
  297. string
  298. EOF
  299. foreach my $target (@target) {
  300. my $profiles = $target->{profiles};
  301. foreach my $profile (@$profiles) {
  302. print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
  303. }
  304. }
  305. print <<EOF;
  306. config TARGET_ARCH_PACKAGES
  307. string
  308. EOF
  309. foreach my $target (@target) {
  310. next if @{$target->{subtargets}} > 0;
  311. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  312. }
  313. print <<EOF;
  314. config DEFAULT_TARGET_OPTIMIZATION
  315. string
  316. EOF
  317. foreach my $target (@target) {
  318. next if @{$target->{subtargets}} > 0;
  319. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  320. }
  321. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  322. print <<EOF;
  323. config CPU_TYPE
  324. string
  325. EOF
  326. foreach my $target (@target) {
  327. next if @{$target->{subtargets}} > 0;
  328. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  329. }
  330. print "\tdefault \"\"\n";
  331. my %kver;
  332. foreach my $target (@target) {
  333. my $v = kver($target->{version});
  334. next if $kver{$v};
  335. $kver{$v} = 1;
  336. print <<EOF;
  337. config LINUX_$v
  338. bool
  339. EOF
  340. }
  341. foreach my $def (sort keys %defaults) {
  342. print <<EOF;
  343. config DEFAULT_$def
  344. bool
  345. config MODULE_DEFAULT_$def
  346. tristate
  347. depends on TARGET_PER_DEVICE_ROOTFS
  348. depends on m
  349. default m if DEFAULT_$def
  350. select PACKAGE_$def
  351. EOF
  352. }
  353. }
  354. sub gen_profile_mk() {
  355. my $file = shift @ARGV;
  356. my $target = shift @ARGV;
  357. my @targets = parse_target_metadata($file);
  358. foreach my $cur (@targets) {
  359. next unless $cur->{id} eq $target;
  360. print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
  361. foreach my $profile (@{$cur->{profiles}}) {
  362. print $profile->{id}.'_NAME:='.$profile->{name}."\n";
  363. print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
  364. }
  365. }
  366. }
  367. sub parse_command() {
  368. GetOptions("ignore=s", \@ignore);
  369. my $cmd = shift @ARGV;
  370. for ($cmd) {
  371. /^config$/ and return gen_target_config();
  372. /^profile_mk$/ and return gen_profile_mk();
  373. }
  374. die <<EOF
  375. Available Commands:
  376. $0 config [file] Target metadata in Kconfig format
  377. $0 profile_mk [file] [target] Profile metadata in makefile format
  378. EOF
  379. }
  380. parse_command();