metadata.pm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package metadata;
  2. use base 'Exporter';
  3. use strict;
  4. use warnings;
  5. our @EXPORT = qw(%package %srcpackage %category %preconfig %features %overrides clear_packages parse_package_metadata parse_target_metadata get_multiline @ignore %usernames %groupnames);
  6. our %package;
  7. our %preconfig;
  8. our %srcpackage;
  9. our %category;
  10. our %features;
  11. our %overrides;
  12. our @ignore;
  13. our %usernames;
  14. our %groupnames;
  15. our %userids;
  16. our %groupids;
  17. sub get_multiline {
  18. my $fh = shift;
  19. my $prefix = shift;
  20. my $str;
  21. while (<$fh>) {
  22. last if /^@@/;
  23. $str .= (($_ and $prefix) ? $prefix . $_ : $_);
  24. }
  25. return $str ? $str : "";
  26. }
  27. sub confstr($) {
  28. my $conf = shift;
  29. $conf =~ tr#/\.\-/#___#;
  30. return $conf;
  31. }
  32. sub parse_package_metadata_usergroup($$$$$) {
  33. my $makefile = shift;
  34. my $typename = shift;
  35. my $names = shift;
  36. my $ids = shift;
  37. my $spec = shift;
  38. my $name;
  39. my $id;
  40. # the regex for name is taken from is_valid_name() of package shadow
  41. if ($spec =~ /^([a-z_][a-z0-9_-]*\$?)$/) {
  42. $name = $spec;
  43. $id = -1;
  44. } elsif ($spec =~ /^([a-z_][a-z0-9_-]*\$?)=(\d+)$/) {
  45. $name = $1;
  46. $id = $2;
  47. } else {
  48. warn "$makefile: invalid $typename spec $spec\n";
  49. return 0;
  50. }
  51. if ($id =~ /^[1-9]\d*$/) {
  52. if ($id >= 65536) {
  53. warn "$makefile: $typename $name id $id >= 65536";
  54. return 0;
  55. }
  56. if (not exists $ids->{$id}) {
  57. $ids->{$id} = {
  58. name => $name,
  59. makefile => $makefile,
  60. };
  61. } elsif ($ids->{$id}{name} ne $name) {
  62. warn "$makefile: $typename $name id $id is already taken by $ids->{$id}{makefile}\n";
  63. return 0;
  64. }
  65. } elsif ($id != -1) {
  66. warn "$makefile: $typename $name has invalid id $id\n";
  67. return 0;
  68. }
  69. if (not exists $names->{$name}) {
  70. $names->{$name} = {
  71. id => $id,
  72. makefile => $makefile,
  73. };
  74. } elsif ($names->{$name}{id} != $id) {
  75. warn "$makefile: id of $typename $name collides with that defined defined in $names->{$name}{makefile}\n";
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. sub parse_target_metadata($) {
  81. my $file = shift;
  82. my ($target, @target, $profile);
  83. my %target;
  84. my $makefile;
  85. open FILE, "<$file" or do {
  86. warn "Can't open file '$file': $!\n";
  87. return;
  88. };
  89. while (<FILE>) {
  90. chomp;
  91. /^Source-Makefile: \s*((.+\/)([^\/]+)\/Makefile)\s*$/ and $makefile = $1;
  92. /^Target:\s*(.+)\s*$/ and do {
  93. my $name = $1;
  94. $target = {
  95. id => $name,
  96. board => $name,
  97. makefile => $makefile,
  98. boardconf => confstr($name),
  99. conf => confstr($name),
  100. profiles => [],
  101. features => [],
  102. depends => [],
  103. subtargets => []
  104. };
  105. push @target, $target;
  106. $target{$name} = $target;
  107. if ($name =~ /([^\/]+)\/([^\/]+)/) {
  108. push @{$target{$1}->{subtargets}}, $2;
  109. $target->{board} = $1;
  110. $target->{boardconf} = confstr($1);
  111. $target->{subtarget} = 1;
  112. $target->{parent} = $target{$1};
  113. }
  114. };
  115. /^Target-Name:\s*(.+)\s*$/ and $target->{name} = $1;
  116. /^Target-Arch:\s*(.+)\s*$/ and $target->{arch} = $1;
  117. /^Target-Arch-Packages:\s*(.+)\s*$/ and $target->{arch_packages} = $1;
  118. /^Target-Features:\s*(.+)\s*$/ and $target->{features} = [ split(/\s+/, $1) ];
  119. /^Target-Depends:\s*(.+)\s*$/ and $target->{depends} = [ split(/\s+/, $1) ];
  120. /^Target-Description:/ and $target->{desc} = get_multiline(*FILE);
  121. /^Target-Optimization:\s*(.+)\s*$/ and $target->{cflags} = $1;
  122. /^CPU-Type:\s*(.+)\s*$/ and $target->{cputype} = $1;
  123. /^Linux-Version:\s*(.+)\s*$/ and $target->{version} = $1;
  124. /^Linux-Release:\s*(.+)\s*$/ and $target->{release} = $1;
  125. /^Linux-Kernel-Arch:\s*(.+)\s*$/ and $target->{karch} = $1;
  126. /^Default-Subtarget:\s*(.+)\s*$/ and $target->{def_subtarget} = $1;
  127. /^Default-Packages:\s*(.+)\s*$/ and $target->{packages} = [ split(/\s+/, $1) ];
  128. /^Target-Profile:\s*(.+)\s*$/ and do {
  129. $profile = {
  130. id => $1,
  131. name => $1,
  132. priority => 999,
  133. packages => []
  134. };
  135. $1 =~ /^DEVICE_/ and $target->{has_devices} = 1;
  136. push @{$target->{profiles}}, $profile;
  137. };
  138. /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
  139. /^Target-Profile-Priority:\s*(\d+)\s*$/ and do {
  140. $profile->{priority} = $1;
  141. $target->{sort} = 1;
  142. };
  143. /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
  144. /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
  145. }
  146. close FILE;
  147. foreach my $target (@target) {
  148. if (@{$target->{subtargets}} > 0) {
  149. $target->{profiles} = [];
  150. next;
  151. }
  152. @{$target->{profiles}} > 0 or $target->{profiles} = [
  153. {
  154. id => 'Default',
  155. name => 'Default',
  156. packages => []
  157. }
  158. ];
  159. $target->{sort} and @{$target->{profiles}} = sort {
  160. $a->{priority} <=> $b->{priority} or
  161. $a->{name} cmp $b->{name};
  162. } @{$target->{profiles}};
  163. }
  164. return @target;
  165. }
  166. sub clear_packages() {
  167. %preconfig = ();
  168. %package = ();
  169. %srcpackage = ();
  170. %category = ();
  171. %features = ();
  172. %overrides = ();
  173. %usernames = ();
  174. %groupnames = ();
  175. }
  176. sub parse_package_metadata($) {
  177. my $file = shift;
  178. my $pkg;
  179. my $feature;
  180. my $preconfig;
  181. my $src;
  182. my $override;
  183. my %ignore = map { $_ => 1 } @ignore;
  184. open FILE, "<$file" or do {
  185. warn "Cannot open '$file': $!\n";
  186. return undef;
  187. };
  188. while (<FILE>) {
  189. chomp;
  190. /^Source-Makefile: \s*((?:package\/)?((?:.+\/)?([^\/]+))\/Makefile)\s*$/ and do {
  191. $src = {
  192. makefile => $1,
  193. path => $2,
  194. name => $3,
  195. ignore => $ignore{$3},
  196. packages => [],
  197. buildtypes => [],
  198. builddepends => [],
  199. };
  200. $srcpackage{$3} = $src;
  201. $override = "";
  202. undef $pkg;
  203. };
  204. /^Override: \s*(.+?)\s*$/ and do {
  205. $override = $1;
  206. $overrides{$src->{name}} = 1;
  207. };
  208. next unless $src;
  209. /^Package:\s*(.+?)\s*$/ and do {
  210. undef $feature;
  211. $pkg = {};
  212. $pkg->{ignore} = $src->{ignore};
  213. $pkg->{src} = $src;
  214. $pkg->{makefile} = $src->{makefile};
  215. $pkg->{name} = $1;
  216. $pkg->{title} = "";
  217. $pkg->{depends} = [];
  218. $pkg->{mdepends} = [];
  219. $pkg->{tristate} = 1;
  220. $pkg->{override} = $override;
  221. $package{$1} = $pkg;
  222. push @{$src->{packages}}, $pkg;
  223. };
  224. /^Feature:\s*(.+?)\s*$/ and do {
  225. undef $pkg;
  226. $feature = {};
  227. $feature->{name} = $1;
  228. $feature->{priority} = 0;
  229. };
  230. $feature and do {
  231. /^Target-Name:\s*(.+?)\s*$/ and do {
  232. $features{$1} or $features{$1} = [];
  233. push @{$features{$1}}, $feature unless $src->{ignore};
  234. };
  235. /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
  236. /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
  237. /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
  238. /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
  239. next;
  240. };
  241. /^Build-Depends: \s*(.+)\s*$/ and $src->{builddepends} = [ split /\s+/, $1 ];
  242. /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $src->{"builddepends/$1"} = [ split /\s+/, $2 ];
  243. /^Build-Types:\s*(.+)\s*$/ and $src->{buildtypes} = [ split /\s+/, $1 ];
  244. next unless $pkg;
  245. /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
  246. /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
  247. /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
  248. /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
  249. /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
  250. /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
  251. /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
  252. /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
  253. /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
  254. /^Provides: \s*(.+)\s*$/ and do {
  255. my @vpkg = split /\s+/, $1;
  256. foreach my $vpkg (@vpkg) {
  257. $package{$vpkg} or $package{$vpkg} = {
  258. name => $vpkg,
  259. vdepends => [],
  260. src => $src,
  261. makefile => $src->{makefile},
  262. };
  263. push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
  264. }
  265. };
  266. /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
  267. /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
  268. /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
  269. /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
  270. /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
  271. /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
  272. /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
  273. /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
  274. /^Category: \s*(.+)\s*$/ and do {
  275. $pkg->{category} = $1;
  276. defined $category{$1} or $category{$1} = {};
  277. defined $category{$1}{$src->{name}} or $category{$1}{$src->{name}} = [];
  278. push @{$category{$1}{$src->{name}}}, $pkg;
  279. };
  280. /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
  281. /^Type: \s*(.+)\s*$/ and do {
  282. $pkg->{type} = [ split /\s+/, $1 ];
  283. undef $pkg->{tristate};
  284. foreach my $type (@{$pkg->{type}}) {
  285. $type =~ /ipkg/ and $pkg->{tristate} = 1;
  286. }
  287. };
  288. /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
  289. /^Prereq-Check:/ and $pkg->{prereq} = 1;
  290. /^Preconfig:\s*(.+)\s*$/ and do {
  291. my $pkgname = $pkg->{name};
  292. $preconfig{$pkgname} or $preconfig{$pkgname} = {};
  293. if (exists $preconfig{$pkgname}->{$1}) {
  294. $preconfig = $preconfig{$pkgname}->{$1};
  295. } else {
  296. $preconfig = {
  297. id => $1
  298. };
  299. $preconfig{$pkgname}{$1} = $preconfig unless $src->{ignore};
  300. }
  301. };
  302. /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
  303. /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
  304. /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
  305. /^Require-User:\s*(.*?)\s*$/ and do {
  306. my @ugspecs = split /\s+/, $1;
  307. for my $ugspec (@ugspecs) {
  308. my @ugspec = split /:/, $ugspec, 2;
  309. parse_package_metadata_usergroup($src->{makefile}, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
  310. if (@ugspec > 1) {
  311. parse_package_metadata_usergroup($src->{makefile}, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
  312. }
  313. }
  314. };
  315. }
  316. close FILE;
  317. return 1;
  318. }
  319. 1;