target-metadata.pl 12 KB

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