metadata.pm 8.3 KB

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