target-metadata.pl 12 KB

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