9 import.pl [options] /path/to/xen
13 -h,--help Display brief help message
14 -v,--verbose Increase verbosity
15 -q,--quiet Decrease verbosity
19 use File::Spec::Functions qw ( :ALL );
35 # Skip everything except headers
36 return unless $filename =~ /\.h$/;
38 # Search for importable header
39 ( undef, my $subdir, undef ) = splitpath ( $filename );
40 my $outfile = catfile ( $ipxedir, $filename );
41 my $infile = catfile ( $xendir, "xen/include/public", $filename );
42 die "$infile does not exist\n" unless -e $infile;
45 print "$filename <- ".catfile ( $xendir, $filename )."\n"
47 open my $infh, "<", $infile or die "Could not open $infile: $!\n";
48 mkpath ( catdir ( $xendir, $subdir ) );
49 open my $outfh, ">", $outfile or die "Could not open $outfile: $!\n";
50 my @dependencies = ();
54 # Strip CR and trailing whitespace
58 # Update include lines, and record included files
59 if ( /^\#include\s+[<\"](\S+)[>\"]/ ) {
60 push @dependencies, catfile ( $subdir, $1 );
64 # Apply FILE_LICENCE() immediately after include guard
65 if ( defined $maybe_guard ) {
66 if ( /^\#define\s+_+${maybe_guard}_H_*$/ ) {
67 die "Duplicate header guard detected in $infile\n" if $guard;
68 $guard = $maybe_guard;
69 print $outfh "\nFILE_LICENCE ( MIT );\n";
73 if ( /^#ifndef\s+_+(\S+)_H_*$/ ) {
79 # Warn if no header guard was detected
80 warn "Cannot detect header guard in $infile\n" unless $guard;
81 # Recurse to handle any included files that we don't already have
82 foreach my $dependency ( @dependencies ) {
83 if ( ! -e catfile ( $ipxedir, $dependency ) ) {
84 print "...following dependency on $dependency\n" if $verbosity >= 1;
85 try_import_file ( $ipxedir, $xendir, $dependency );
91 # Parse command-line options
92 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
94 'verbose|v+' => sub { $verbosity++; },
95 'quiet|q+' => sub { $verbosity--; },
96 'help|h' => sub { pod2usage ( 1 ); },
97 ) or die "Could not parse command-line options\n";
98 pod2usage ( 1 ) unless @ARGV == 1;
101 # Identify Xen import directory
102 die "Directory \"$xendir\" does not appear to contain the Xen source tree\n"
103 unless -e catfile ( $xendir, "xen/include/public/xen.h" );
105 # Identify iPXE Xen includes directory
106 my $ipxedir = $FindBin::Bin;
107 die "Directory \"$ipxedir\" does not appear to contain the iPXE Xen includes\n"
108 unless -e catfile ( $ipxedir, "../../include/ipxe" );
110 print "Importing Xen headers into $ipxedir\nfrom $xendir\n"
114 find ( { wanted => sub {
115 try_import_file ( $ipxedir, $xendir, abs2rel ( $_, $ipxedir ) );
116 }, no_chdir => 1 }, $ipxedir );