target-metadata.pl 11 KB

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