target-metadata.pl 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. default TARGET_MULTI_PROFILE if BUILDBOT
  184. EOF
  185. foreach my $target (@target) {
  186. my $profile = $target->{profiles}->[0];
  187. $profile or next;
  188. print <<EOF;
  189. default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf} && !BUILDBOT
  190. EOF
  191. }
  192. print <<EOF;
  193. config TARGET_MULTI_PROFILE
  194. bool "Multiple devices"
  195. depends on HAS_DEVICES
  196. help
  197. Instead of only building a single image, or all images, this allows you
  198. to select images to be built for multiple devices in one build.
  199. EOF
  200. foreach my $target (@target) {
  201. my $profiles = $target->{profiles};
  202. foreach my $profile (@{$target->{profiles}}) {
  203. print <<EOF;
  204. config TARGET_$target->{conf}_$profile->{id}
  205. bool "$profile->{name}"
  206. depends on TARGET_$target->{conf}
  207. EOF
  208. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  209. foreach my $pkg (@pkglist) {
  210. print "\tselect DEFAULT_$pkg\n";
  211. $defaults{$pkg} = 1;
  212. }
  213. my $help = $profile->{desc};
  214. if ($help =~ /\w+/) {
  215. $help =~ s/^\s*/\t /mg;
  216. $help = "\thelp\n$help";
  217. } else {
  218. undef $help;
  219. }
  220. print "$help\n";
  221. }
  222. }
  223. print <<EOF;
  224. endchoice
  225. menu "Target Devices"
  226. depends on TARGET_MULTI_PROFILE
  227. config TARGET_ALL_PROFILES
  228. bool "Enable all profiles by default"
  229. default BUILDBOT
  230. config TARGET_PER_DEVICE_ROOTFS
  231. bool "Use a per-device root filesystem that adds profile packages"
  232. default BUILDBOT
  233. help
  234. When disabled, all device packages from all selected devices
  235. will be included in all images by default. (Marked as <*>) You will
  236. still be able to manually deselect any/all packages.
  237. When enabled, each device builds it's own image, including only the
  238. profile packages for that device. (Marked as {M}) You will be able
  239. to change a package to included in all images by marking as {*}, but
  240. will not be able to disable a profile package completely.
  241. To get the most use of this setting, you must set in a .config stub
  242. before calling "make defconfig". Selecting TARGET_MULTI_PROFILE and
  243. then manually selecting (via menuconfig for instance) this option
  244. will have pre-defaulted all profile packages to included, making this
  245. option appear to have had no effect.
  246. EOF
  247. foreach my $target (@target) {
  248. my @profiles = sort {
  249. my $x = $a->{name};
  250. my $y = $b->{name};
  251. "\L$x" cmp "\L$y";
  252. } @{$target->{profiles}};
  253. foreach my $profile (@profiles) {
  254. next unless $profile->{id} =~ /^DEVICE_/;
  255. print <<EOF;
  256. menuconfig TARGET_DEVICE_$target->{conf}_$profile->{id}
  257. bool "$profile->{name}"
  258. depends on TARGET_$target->{conf}
  259. default y if TARGET_ALL_PROFILES
  260. EOF
  261. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  262. foreach my $pkg (@pkglist) {
  263. print "\tselect DEFAULT_$pkg if !TARGET_PER_DEVICE_ROOTFS\n";
  264. print "\tselect MODULE_DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  265. $defaults{$pkg} = 1;
  266. }
  267. print <<EOF;
  268. config TARGET_DEVICE_PACKAGES_$target->{conf}_$profile->{id}
  269. string "$profile->{name} additional packages"
  270. default ""
  271. depends on TARGET_PER_DEVICE_ROOTFS
  272. depends on TARGET_DEVICE_$target->{conf}_$profile->{id}
  273. EOF
  274. }
  275. }
  276. print <<EOF;
  277. endmenu
  278. config HAS_SUBTARGETS
  279. bool
  280. config HAS_DEVICES
  281. bool
  282. config TARGET_BOARD
  283. string
  284. EOF
  285. foreach my $target (@target) {
  286. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  287. }
  288. print <<EOF;
  289. config TARGET_SUBTARGET
  290. string
  291. default "generic" if !HAS_SUBTARGETS
  292. EOF
  293. foreach my $target (@target) {
  294. foreach my $subtarget (@{$target->{subtargets}}) {
  295. print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
  296. }
  297. }
  298. print <<EOF;
  299. config TARGET_PROFILE
  300. string
  301. EOF
  302. foreach my $target (@target) {
  303. my $profiles = $target->{profiles};
  304. foreach my $profile (@$profiles) {
  305. print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
  306. }
  307. }
  308. print <<EOF;
  309. config TARGET_ARCH_PACKAGES
  310. string
  311. EOF
  312. foreach my $target (@target) {
  313. next if @{$target->{subtargets}} > 0;
  314. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  315. }
  316. print <<EOF;
  317. config DEFAULT_TARGET_OPTIMIZATION
  318. string
  319. EOF
  320. foreach my $target (@target) {
  321. next if @{$target->{subtargets}} > 0;
  322. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  323. }
  324. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  325. print <<EOF;
  326. config CPU_TYPE
  327. string
  328. EOF
  329. foreach my $target (@target) {
  330. next if @{$target->{subtargets}} > 0;
  331. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  332. }
  333. print "\tdefault \"\"\n";
  334. my %kver;
  335. foreach my $target (@target) {
  336. my $v = kver($target->{version});
  337. next if $kver{$v};
  338. $kver{$v} = 1;
  339. print <<EOF;
  340. config LINUX_$v
  341. bool
  342. EOF
  343. }
  344. foreach my $def (sort keys %defaults) {
  345. print <<EOF;
  346. config DEFAULT_$def
  347. bool
  348. config MODULE_DEFAULT_$def
  349. tristate
  350. depends on TARGET_PER_DEVICE_ROOTFS
  351. depends on m
  352. default m if DEFAULT_$def
  353. select PACKAGE_$def
  354. EOF
  355. }
  356. }
  357. sub gen_profile_mk() {
  358. my $file = shift @ARGV;
  359. my $target = shift @ARGV;
  360. my @targets = parse_target_metadata($file);
  361. foreach my $cur (@targets) {
  362. next unless $cur->{id} eq $target;
  363. print "PROFILE_NAMES = ".join(" ", map { $_->{id} } @{$cur->{profiles}})."\n";
  364. foreach my $profile (@{$cur->{profiles}}) {
  365. print $profile->{id}.'_NAME:='.$profile->{name}."\n";
  366. print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
  367. }
  368. }
  369. }
  370. sub parse_command() {
  371. GetOptions("ignore=s", \@ignore);
  372. my $cmd = shift @ARGV;
  373. for ($cmd) {
  374. /^config$/ and return gen_target_config();
  375. /^profile_mk$/ and return gen_profile_mk();
  376. }
  377. die <<EOF
  378. Available Commands:
  379. $0 config [file] Target metadata in Kconfig format
  380. $0 profile_mk [file] [target] Profile metadata in makefile format
  381. EOF
  382. }
  383. parse_command();