target-metadata.pl 11 KB

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