target-metadata.pl 12 KB

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