[process] Include process name in debug messages
[ipxe.git] / src / include / xen / import.pl
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 import.pl
6
7 =head1 SYNOPSIS
8
9 import.pl [options] /path/to/xen
10
11 Options:
12
13 -h,--help Display brief help message
14 -v,--verbose Increase verbosity
15 -q,--quiet Decrease verbosity
16
17 =cut
18
19 use File::Spec::Functions qw ( :ALL );
20 use File::Find;
21 use File::Path;
22 use Getopt::Long;
23 use Pod::Usage;
24 use FindBin;
25 use strict;
26 use warnings;
27
28 my $verbosity = 0;
29
30 sub try_import_file {
31 my $ipxedir = shift;
32 my $xendir = shift;
33 my $filename = shift;
34
35 # Skip everything except headers
36 return unless $filename =~ /\.h$/;
37
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;
43
44 # Import header file
45 print "$filename <- ".catfile ( $xendir, $filename )."\n"
46 if $verbosity >= 1;
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 = ();
51 my $maybe_guard;
52 my $guard;
53 while ( <$infh> ) {
54 # Strip CR and trailing whitespace
55 s/\r//g;
56 s/\s*$//g;
57 chomp;
58 # Update include lines, and record included files
59 if ( /^\#include\s+[<\"](\S+)[>\"]/ ) {
60 push @dependencies, catfile ( $subdir, $1 );
61 }
62 # Write out line
63 print $outfh "$_\n";
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";
70 }
71 undef $maybe_guard;
72 }
73 if ( /^#ifndef\s+_+(\S+)_H_*$/ ) {
74 $maybe_guard = $1;
75 }
76 }
77 close $outfh;
78 close $infh;
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 );
86 }
87 }
88 return;
89 }
90
91 # Parse command-line options
92 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
93 GetOptions (
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;
99 my $xendir = shift;
100
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" );
104
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" );
109
110 print "Importing Xen headers into $ipxedir\nfrom $xendir\n"
111 if $verbosity >= 1;
112
113 # Import headers
114 find ( { wanted => sub {
115 try_import_file ( $ipxedir, $xendir, abs2rel ( $_, $ipxedir ) );
116 }, no_chdir => 1 }, $ipxedir );