You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/perl -w
  2. =head1 NAME
  3. import.pl
  4. =head1 SYNOPSIS
  5. import.pl [options] /path/to/xen
  6. Options:
  7. -h,--help Display brief help message
  8. -v,--verbose Increase verbosity
  9. -q,--quiet Decrease verbosity
  10. =cut
  11. use File::Spec::Functions qw ( :ALL );
  12. use File::Find;
  13. use File::Path;
  14. use Getopt::Long;
  15. use Pod::Usage;
  16. use FindBin;
  17. use strict;
  18. use warnings;
  19. my $verbosity = 0;
  20. sub try_import_file {
  21. my $ipxedir = shift;
  22. my $xendir = shift;
  23. my $filename = shift;
  24. # Skip everything except headers
  25. return unless $filename =~ /\.h$/;
  26. # Search for importable header
  27. ( undef, my $subdir, undef ) = splitpath ( $filename );
  28. my $outfile = catfile ( $ipxedir, $filename );
  29. my $infile = catfile ( $xendir, "xen/include/public", $filename );
  30. die "$infile does not exist\n" unless -e $infile;
  31. # Import header file
  32. print "$filename <- ".catfile ( $xendir, $filename )."\n"
  33. if $verbosity >= 1;
  34. open my $infh, "<", $infile or die "Could not open $infile: $!\n";
  35. mkpath ( catdir ( $xendir, $subdir ) );
  36. open my $outfh, ">", $outfile or die "Could not open $outfile: $!\n";
  37. my @dependencies = ();
  38. my $maybe_guard;
  39. my $guard;
  40. while ( <$infh> ) {
  41. # Strip CR and trailing whitespace
  42. s/\r//g;
  43. s/\s*$//g;
  44. chomp;
  45. # Update include lines, and record included files
  46. if ( /^\#include\s+[<\"](\S+)[>\"]/ ) {
  47. push @dependencies, catfile ( $subdir, $1 );
  48. }
  49. # Write out line
  50. print $outfh "$_\n";
  51. # Apply FILE_LICENCE() immediately after include guard
  52. if ( defined $maybe_guard ) {
  53. if ( /^\#define\s+_+${maybe_guard}_H_*$/ ) {
  54. die "Duplicate header guard detected in $infile\n" if $guard;
  55. $guard = $maybe_guard;
  56. print $outfh "\nFILE_LICENCE ( MIT );\n";
  57. }
  58. undef $maybe_guard;
  59. }
  60. if ( /^#ifndef\s+_+(\S+)_H_*$/ ) {
  61. $maybe_guard = $1;
  62. }
  63. }
  64. close $outfh;
  65. close $infh;
  66. # Warn if no header guard was detected
  67. warn "Cannot detect header guard in $infile\n" unless $guard;
  68. # Recurse to handle any included files that we don't already have
  69. foreach my $dependency ( @dependencies ) {
  70. if ( ! -e catfile ( $ipxedir, $dependency ) ) {
  71. print "...following dependency on $dependency\n" if $verbosity >= 1;
  72. try_import_file ( $ipxedir, $xendir, $dependency );
  73. }
  74. }
  75. return;
  76. }
  77. # Parse command-line options
  78. Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
  79. GetOptions (
  80. 'verbose|v+' => sub { $verbosity++; },
  81. 'quiet|q+' => sub { $verbosity--; },
  82. 'help|h' => sub { pod2usage ( 1 ); },
  83. ) or die "Could not parse command-line options\n";
  84. pod2usage ( 1 ) unless @ARGV == 1;
  85. my $xendir = shift;
  86. # Identify Xen import directory
  87. die "Directory \"$xendir\" does not appear to contain the Xen source tree\n"
  88. unless -e catfile ( $xendir, "xen/include/public/xen.h" );
  89. # Identify iPXE Xen includes directory
  90. my $ipxedir = $FindBin::Bin;
  91. die "Directory \"$ipxedir\" does not appear to contain the iPXE Xen includes\n"
  92. unless -e catfile ( $ipxedir, "../../include/ipxe" );
  93. print "Importing Xen headers into $ipxedir\nfrom $xendir\n"
  94. if $verbosity >= 1;
  95. # Import headers
  96. find ( { wanted => sub {
  97. try_import_file ( $ipxedir, $xendir, abs2rel ( $_, $ipxedir ) );
  98. }, no_chdir => 1 }, $ipxedir );