target-metadata.pl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 =~ s/^~//r} = 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_mediatek
  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. foreach my $p (@{$target->{profiles}}) {
  199. last unless $target->{default_profile};
  200. my $name = $p->{id};
  201. $name =~ s/^DEVICE_//;
  202. next unless $name eq $target->{default_profile};
  203. $profile = $p;
  204. last;
  205. }
  206. $profile or next;
  207. print <<EOF;
  208. default TARGET_$target->{conf}_$profile->{id} if TARGET_$target->{conf} && !BUILDBOT
  209. EOF
  210. }
  211. print <<EOF;
  212. config TARGET_MULTI_PROFILE
  213. bool "Multiple devices"
  214. depends on HAS_DEVICES
  215. help
  216. Instead of only building a single image, or all images, this allows you
  217. to select images to be built for multiple devices in one build.
  218. EOF
  219. foreach my $target (@target) {
  220. my $profiles = $target->{profiles};
  221. foreach my $profile (@{$target->{profiles}}) {
  222. print <<EOF;
  223. config TARGET_$target->{conf}_$profile->{id}
  224. bool "$profile->{name}"
  225. depends on TARGET_$target->{conf}
  226. EOF
  227. $profile->{broken} and print "\tdepends on BROKEN\n";
  228. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  229. foreach my $pkg (@pkglist) {
  230. print "\tselect DEFAULT_$pkg\n";
  231. $defaults{$pkg} = 1;
  232. }
  233. my $help = $profile->{desc};
  234. if ($help =~ /\w+/) {
  235. $help =~ s/^\s*/\t /mg;
  236. $help = "\thelp\n$help";
  237. } else {
  238. undef $help;
  239. }
  240. print "$help\n";
  241. }
  242. }
  243. print <<EOF;
  244. endchoice
  245. menu "Target Devices"
  246. depends on TARGET_MULTI_PROFILE
  247. config TARGET_ALL_PROFILES
  248. bool "Enable all profiles by default"
  249. default BUILDBOT
  250. config TARGET_PER_DEVICE_ROOTFS
  251. bool "Use a per-device root filesystem that adds profile packages"
  252. default BUILDBOT
  253. help
  254. When disabled, all device packages from all selected devices
  255. will be included in all images by default. (Marked as <*>) You will
  256. still be able to manually deselect any/all packages.
  257. When enabled, each device builds it's own image, including only the
  258. profile packages for that device. (Marked as {M}) You will be able
  259. to change a package to included in all images by marking as {*}, but
  260. will not be able to disable a profile package completely.
  261. To get the most use of this setting, you must set in a .config stub
  262. before calling "make defconfig". Selecting TARGET_MULTI_PROFILE and
  263. then manually selecting (via menuconfig for instance) this option
  264. will have pre-defaulted all profile packages to included, making this
  265. option appear to have had no effect.
  266. EOF
  267. foreach my $target (@target) {
  268. my @profiles = sort {
  269. my $x = $a->{name};
  270. my $y = $b->{name};
  271. "\L$x" cmp "\L$y";
  272. } @{$target->{profiles}};
  273. foreach my $profile (@profiles) {
  274. next unless $profile->{id} =~ /^DEVICE_/;
  275. print <<EOF;
  276. menuconfig TARGET_DEVICE_$target->{conf}_$profile->{id}
  277. bool "$profile->{name}"
  278. depends on TARGET_$target->{conf}
  279. default $profile->{default}
  280. EOF
  281. $profile->{broken} and print "\tdepends on BROKEN\n";
  282. my @pkglist = merge_package_lists($target->{packages}, $profile->{packages});
  283. foreach my $pkg (@pkglist) {
  284. print "\tselect DEFAULT_$pkg if !TARGET_PER_DEVICE_ROOTFS\n";
  285. print "\tselect MODULE_DEFAULT_$pkg if TARGET_PER_DEVICE_ROOTFS\n";
  286. $defaults{$pkg} = 1;
  287. }
  288. print <<EOF;
  289. config TARGET_DEVICE_PACKAGES_$target->{conf}_$profile->{id}
  290. string "$profile->{name} additional packages"
  291. default ""
  292. depends on TARGET_PER_DEVICE_ROOTFS
  293. depends on TARGET_DEVICE_$target->{conf}_$profile->{id}
  294. EOF
  295. }
  296. }
  297. print <<EOF;
  298. endmenu
  299. config HAS_SUBTARGETS
  300. bool
  301. config HAS_DEVICES
  302. bool
  303. config TARGET_BOARD
  304. string
  305. EOF
  306. foreach my $target (@target) {
  307. $target->{subtarget} or print "\t\tdefault \"".$target->{board}."\" if TARGET_".$target->{conf}."\n";
  308. }
  309. print <<EOF;
  310. config TARGET_SUBTARGET
  311. string
  312. default "generic" if !HAS_SUBTARGETS
  313. EOF
  314. foreach my $target (@target) {
  315. foreach my $subtarget (@{$target->{subtargets}}) {
  316. print "\t\tdefault \"$subtarget\" if TARGET_".$target->{conf}."_$subtarget\n";
  317. }
  318. }
  319. print <<EOF;
  320. config TARGET_PROFILE
  321. string
  322. EOF
  323. foreach my $target (@target) {
  324. my $profiles = $target->{profiles};
  325. foreach my $profile (@$profiles) {
  326. print "\tdefault \"$profile->{id}\" if TARGET_$target->{conf}_$profile->{id}\n";
  327. }
  328. }
  329. print <<EOF;
  330. config TARGET_ARCH_PACKAGES
  331. string
  332. EOF
  333. foreach my $target (@target) {
  334. next if @{$target->{subtargets}} > 0;
  335. print "\t\tdefault \"".($target->{arch_packages} || $target->{board})."\" if TARGET_".$target->{conf}."\n";
  336. }
  337. print <<EOF;
  338. config DEFAULT_TARGET_OPTIMIZATION
  339. string
  340. EOF
  341. foreach my $target (@target) {
  342. next if @{$target->{subtargets}} > 0;
  343. print "\tdefault \"".$target->{cflags}."\" if TARGET_".$target->{conf}."\n";
  344. }
  345. print "\tdefault \"-Os -pipe -funit-at-a-time\"\n";
  346. print <<EOF;
  347. config CPU_TYPE
  348. string
  349. EOF
  350. foreach my $target (@target) {
  351. next if @{$target->{subtargets}} > 0;
  352. print "\tdefault \"".$target->{cputype}."\" if TARGET_".$target->{conf}."\n";
  353. }
  354. print "\tdefault \"\"\n";
  355. my %kver;
  356. foreach my $target (@target) {
  357. foreach my $tv ($target->{version}, $target->{testing_version}) {
  358. next unless $tv;
  359. my $v = kver($tv);
  360. next if $kver{$v};
  361. $kver{$v} = 1;
  362. print <<EOF;
  363. config LINUX_$v
  364. bool
  365. EOF
  366. }
  367. }
  368. foreach my $def (sort keys %defaults) {
  369. print <<EOF;
  370. config DEFAULT_$def
  371. bool
  372. config MODULE_DEFAULT_$def
  373. tristate
  374. depends on TARGET_PER_DEVICE_ROOTFS
  375. depends on m
  376. default m if DEFAULT_$def
  377. select PACKAGE_$def
  378. EOF
  379. }
  380. }
  381. sub gen_profile_mk() {
  382. my $file = shift @ARGV;
  383. my $target = shift @ARGV;
  384. my @targets = parse_target_metadata($file);
  385. foreach my $cur (@targets) {
  386. next unless $cur->{id} eq $target;
  387. my @profile_ids_unique = do { my %seen; grep { !$seen{$_}++} map { $_->{id} } @{$cur->{profiles}}};
  388. print "PROFILE_NAMES = ".join(" ", @profile_ids_unique)."\n";
  389. foreach my $profile (@{$cur->{profiles}}) {
  390. print $profile->{id}.'_NAME:='.$profile->{name}."\n";
  391. print $profile->{id}.'_HAS_IMAGE_METADATA:='.$profile->{has_image_metadata}."\n";
  392. if (defined($profile->{supported_devices}) and @{$profile->{supported_devices}} > 0) {
  393. print $profile->{id}.'_SUPPORTED_DEVICES:='.join(' ', @{$profile->{supported_devices}})."\n";
  394. }
  395. print $profile->{id}.'_PACKAGES:='.join(' ', @{$profile->{packages}})."\n";
  396. }
  397. }
  398. }
  399. sub parse_command() {
  400. GetOptions("ignore=s", \@ignore);
  401. my $cmd = shift @ARGV;
  402. for ($cmd) {
  403. /^config$/ and return gen_target_config();
  404. /^profile_mk$/ and return gen_profile_mk();
  405. }
  406. die <<EOF
  407. Available Commands:
  408. $0 config [file] Target metadata in Kconfig format
  409. $0 profile_mk [file] [target] Profile metadata in makefile format
  410. EOF
  411. }
  412. parse_command();