1
0

metadata.pm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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-Testing-Version:\s*(.+)\s*$/ and $target->{testing_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. has_image_metadata => 0,
  133. supported_devices => [],
  134. priority => 999,
  135. packages => [],
  136. default => "y if TARGET_ALL_PROFILES"
  137. };
  138. $1 =~ /^DEVICE_/ and $target->{has_devices} = 1;
  139. push @{$target->{profiles}}, $profile;
  140. };
  141. /^Target-Profile-Name:\s*(.+)\s*$/ and $profile->{name} = $1;
  142. /^Target-Profile-hasImageMetadata:\s*(\d+)\s*$/ and $profile->{has_image_metadata} = $1;
  143. /^Target-Profile-SupportedDevices:\s*(.+)\s*$/ and $profile->{supported_devices} = [ split(/\s+/, $1) ];
  144. /^Target-Profile-Priority:\s*(\d+)\s*$/ and do {
  145. $profile->{priority} = $1;
  146. $target->{sort} = 1;
  147. };
  148. /^Target-Profile-Packages:\s*(.*)\s*$/ and $profile->{packages} = [ split(/\s+/, $1) ];
  149. /^Target-Profile-Description:\s*(.*)\s*/ and $profile->{desc} = get_multiline(*FILE);
  150. /^Target-Profile-Default:\s*(.+)\s*$/ and $profile->{default} = $1;
  151. }
  152. close FILE;
  153. foreach my $target (@target) {
  154. if (@{$target->{subtargets}} > 0) {
  155. $target->{profiles} = [];
  156. next;
  157. }
  158. @{$target->{profiles}} > 0 or $target->{profiles} = [
  159. {
  160. id => 'Default',
  161. name => 'Default',
  162. packages => []
  163. }
  164. ];
  165. $target->{sort} and @{$target->{profiles}} = sort {
  166. $a->{priority} <=> $b->{priority} or
  167. $a->{name} cmp $b->{name};
  168. } @{$target->{profiles}};
  169. }
  170. return @target;
  171. }
  172. sub clear_packages() {
  173. %package = ();
  174. %vpackage = ();
  175. %srcpackage = ();
  176. %category = ();
  177. %overrides = ();
  178. %usernames = ();
  179. %groupnames = ();
  180. }
  181. sub parse_package_metadata($) {
  182. my $file = shift;
  183. my $pkg;
  184. my $src;
  185. my $override;
  186. my %ignore = map { $_ => 1 } @ignore;
  187. open FILE, "<$file" or do {
  188. warn "Cannot open '$file': $!\n";
  189. return undef;
  190. };
  191. while (<FILE>) {
  192. chomp;
  193. /^Source-Makefile: \s*((?:package\/)?((?:.+\/)?([^\/]+))\/Makefile)\s*$/ and do {
  194. $src = {
  195. makefile => $1,
  196. path => $2,
  197. name => $3,
  198. ignore => $ignore{$3},
  199. packages => [],
  200. buildtypes => [],
  201. builddepends => [],
  202. };
  203. $srcpackage{$3} = $src;
  204. $override = "";
  205. undef $pkg;
  206. };
  207. /^Override: \s*(.+?)\s*$/ and do {
  208. $override = $1;
  209. $overrides{$src->{name}} = 1;
  210. };
  211. next unless $src;
  212. /^Package:\s*(.+?)\s*$/ and do {
  213. $pkg = {};
  214. $pkg->{src} = $src;
  215. $pkg->{name} = $1;
  216. $pkg->{title} = "";
  217. $pkg->{depends} = [];
  218. $pkg->{mdepends} = [];
  219. $pkg->{provides} = [$1];
  220. $pkg->{tristate} = 1;
  221. $pkg->{override} = $override;
  222. $package{$1} = $pkg;
  223. push @{$src->{packages}}, $pkg;
  224. $vpackage{$1} or $vpackage{$1} = [];
  225. unshift @{$vpackage{$1}}, $pkg;
  226. };
  227. /^Build-Depends: \s*(.+)\s*$/ and $src->{builddepends} = [ split /\s+/, $1 ];
  228. /^Build-Depends\/(\w+): \s*(.+)\s*$/ and $src->{"builddepends/$1"} = [ split /\s+/, $2 ];
  229. /^Build-Types:\s*(.+)\s*$/ and $src->{buildtypes} = [ split /\s+/, $1 ];
  230. next unless $pkg;
  231. /^Version: \s*(.+)\s*$/ and $pkg->{version} = $1;
  232. /^ABIVersion: \s*(.+)\s*$/ and $pkg->{abiversion} = $1;
  233. /^Title: \s*(.+)\s*$/ and $pkg->{title} = $1;
  234. /^Menu: \s*(.+)\s*$/ and $pkg->{menu} = $1;
  235. /^Submenu: \s*(.+)\s*$/ and $pkg->{submenu} = $1;
  236. /^Submenu-Depends: \s*(.+)\s*$/ and $pkg->{submenudep} = $1;
  237. /^Source: \s*(.+)\s*$/ and $pkg->{source} = $1;
  238. /^License: \s*(.+)\s*$/ and $pkg->{license} = $1;
  239. /^LicenseFiles: \s*(.+)\s*$/ and $pkg->{licensefiles} = $1;
  240. /^Default: \s*(.+)\s*$/ and $pkg->{default} = $1;
  241. /^Provides: \s*(.+)\s*$/ and do {
  242. my @vpkg = split /\s+/, $1;
  243. @{$pkg->{provides}} = ($pkg->{name}, @vpkg);
  244. foreach my $vpkg (@vpkg) {
  245. next if ($vpkg eq $pkg->{name});
  246. $vpackage{$vpkg} or $vpackage{$vpkg} = [];
  247. push @{$vpackage{$vpkg}}, $pkg;
  248. }
  249. };
  250. /^Menu-Depends: \s*(.+)\s*$/ and $pkg->{mdepends} = [ split /\s+/, $1 ];
  251. /^Depends: \s*(.+)\s*$/ and $pkg->{depends} = [ split /\s+/, $1 ];
  252. /^Conflicts: \s*(.+)\s*$/ and $pkg->{conflicts} = [ split /\s+/, $1 ];
  253. /^Hidden: \s*(.+)\s*$/ and $pkg->{hidden} = 1;
  254. /^Build-Variant: \s*([\w\-]+)\s*/ and $pkg->{variant} = $1;
  255. /^Default-Variant: .*/ and $pkg->{variant_default} = 1;
  256. /^Build-Only: \s*(.+)\s*$/ and $pkg->{buildonly} = 1;
  257. /^Repository:\s*(.+?)\s*$/ and $pkg->{repository} = $1;
  258. /^Category: \s*(.+)\s*$/ and do {
  259. $pkg->{category} = $1;
  260. defined $category{$1} or $category{$1} = {};
  261. defined $category{$1}{$src->{name}} or $category{$1}{$src->{name}} = [];
  262. push @{$category{$1}{$src->{name}}}, $pkg;
  263. };
  264. /^Description: \s*(.*)\s*$/ and $pkg->{description} = "\t\t $1\n". get_multiline(*FILE, "\t\t ");
  265. /^Type: \s*(.+)\s*$/ and do {
  266. $pkg->{type} = [ split /\s+/, $1 ];
  267. undef $pkg->{tristate};
  268. foreach my $type (@{$pkg->{type}}) {
  269. $type =~ /ipkg/ and $pkg->{tristate} = 1;
  270. }
  271. };
  272. /^Config:\s*(.*)\s*$/ and $pkg->{config} = "$1\n".get_multiline(*FILE, "\t");
  273. /^Prereq-Check:/ and $pkg->{prereq} = 1;
  274. /^Require-User:\s*(.*?)\s*$/ and do {
  275. my @ugspecs = split /\s+/, $1;
  276. for my $ugspec (@ugspecs) {
  277. my @ugspec = split /:/, $ugspec, 2;
  278. if ($ugspec[0]) {
  279. parse_package_metadata_usergroup($src->{makefile}, "user", \%usernames, \%userids, $ugspec[0]) or return 0;
  280. }
  281. if ($ugspec[1]) {
  282. parse_package_metadata_usergroup($src->{makefile}, "group", \%groupnames, \%groupids, $ugspec[1]) or return 0;
  283. }
  284. }
  285. };
  286. }
  287. close FILE;
  288. return 1;
  289. }
  290. 1;