metadata.pm 9.5 KB

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