target-metadata.pl 12 KB

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