target-metadata.pl 11 KB

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