1
0

target-metadata.pl 12 KB

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