1
0

target-metadata.pl 11 KB

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