target-metadata.pl 12 KB

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