metadata.pm 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. packages => [],
  202. };
  203. $override = "";
  204. undef $pkg;
  205. };
  206. /^Override: \s*(.+?)\s*$/ and do {
  207. $override = $1;
  208. $overrides{$src} = 1;
  209. };
  210. next unless $src;
  211. /^Package:\s*(.+?)\s*$/ and do {
  212. undef $feature;
  213. $pkg = {};
  214. $pkg->{ignore} = $ignore{$src};
  215. $pkg->{src} = $src;
  216. $pkg->{makefile} = $makefile;
  217. $pkg->{name} = $1;
  218. $pkg->{title} = "";
  219. $pkg->{depends} = [];
  220. $pkg->{mdepends} = [];
  221. $pkg->{builddepends} = [];
  222. $pkg->{buildtypes} = [];
  223. $pkg->{subdir} = $subdir;
  224. $pkg->{tristate} = 1;
  225. $pkg->{override} = $override;
  226. $package{$1} = $pkg;
  227. push @{$srcpackage{$src}{packages}}, $pkg;
  228. };
  229. /^Feature:\s*(.+?)\s*$/ and do {
  230. undef $pkg;
  231. $feature = {};
  232. $feature->{name} = $1;
  233. $feature->{priority} = 0;
  234. };
  235. $feature and do {
  236. /^Target-Name:\s*(.+?)\s*$/ and do {
  237. $features{$1} or $features{$1} = [];
  238. push @{$features{$1}}, $feature unless $ignore{$src};
  239. };
  240. /^Target-Title:\s*(.+?)\s*$/ and $feature->{target_title} = $1;
  241. /^Feature-Priority:\s*(\d+)\s*$/ and $feature->{priority} = $1;
  242. /^Feature-Name:\s*(.+?)\s*$/ and $feature->{title} = $1;
  243. /^Feature-Description:/ and $feature->{description} = get_multiline(\*FILE, "\t\t\t");
  244. next;
  245. };
  246. next unless $pkg;
  247. /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
  248. /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
  249. /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
  250. /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
  251. /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
  252. /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
  253. /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
  254. /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
  255. /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
  256. /^Provides: \s*(.+)\s*$/ and do {
  257. my @vpkg = split /\s+/, $1;
  258. foreach my $vpkg (@vpkg) {
  259. $package{$vpkg} or $package{$vpkg} = {
  260. name => $vpkg,
  261. vdepends => [],
  262. src => $src,
  263. subdir => $subdir,
  264. makefile => $makefile
  265. };
  266. push @{$package{$vpkg}->{vdepends}}, $pkg->{name};
  267. }
  268. };
  269. /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
  270. /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
  271. /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
  272. /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
  273. /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
  274. /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
  275. /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
  276. /^Build-Depends: \s*(.+)\s*$/ and $pkg->{builddepends} = [ split /\s+/, $1 ];
  277. /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $pkg->{"builddepends/$1"} = [ split /\s+/, $2 ];
  278. /^Build-Types:\s*(.+)\s*$/ and $pkg->{buildtypes} = [ split /\s+/, $1 ];
  279. /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
  280. /^Category: \s*(.+)\s*$/ and do {
  281. $pkg->{category} = $1;
  282. defined $category{$1} or $category{$1} = {};
  283. defined $category{$1}->{$src} or $category{$1}->{$src} = [];
  284. push @{$category{$1}->{$src}}, $pkg;
  285. };
  286. /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
  287. /^Type: \s*(.+)\s*$/ and do {
  288. $pkg->{type} = [ split /\s+/, $1 ];
  289. undef $pkg->{tristate};
  290. foreach my $type (@{$pkg->{type}}) {
  291. $type =~ /ipkg/ and $pkg->{tristate} = 1;
  292. }
  293. };
  294. /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
  295. /^Prereq-Check:/ and $pkg->{prereq} = 1;
  296. /^Preconfig:\s*(.+)\s*$/ and do {
  297. my $pkgname = $pkg->{name};
  298. $preconfig{$pkgname} or $preconfig{$pkgname} = {};
  299. if (exists $preconfig{$pkgname}->{$1}) {
  300. $preconfig = $preconfig{$pkgname}->{$1};
  301. } else {
  302. $preconfig = {
  303. id => $1
  304. };
  305. $preconfig{$pkgname}->{$1} = $preconfig unless $ignore{$src};
  306. }
  307. };
  308. /^Preconfig-Type:\s*(.*?)\s*$/ and $preconfig->{type} = $1;
  309. /^Preconfig-Label:\s*(.*?)\s*$/ and $preconfig->{label} = $1;
  310. /^Preconfig-Default:\s*(.*?)\s*$/ and $preconfig->{default} = $1;
  311. /^Require-User:\s*(.*?)\s*$/ and do {
  312. my @ugspecs = split /\s+/, $1;
  313. for my $ugspec (@ugspecs) {
  314. my @ugspec = split /:/, $ugspec, 2;
  315. parse_package_metadata_usergroup($makefile, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
  316. if (@ugspec > 1) {
  317. parse_package_metadata_usergroup($makefile, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
  318. }
  319. }
  320. };
  321. }
  322. close FILE;
  323. return 1;
  324. }
  325. 1;