Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / include / ipxe / efi / 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/edk2/edk2
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 $edktop = shift;
33   my $edkdirs = shift;
34   my $filename = shift;
35
36   # Skip everything except headers
37   return unless $filename =~ /\.h$/;
38
39   # Skip files that are iPXE native headers
40   my $outfile = catfile ( $ipxedir, $filename );
41   if ( -s $outfile ) {
42     open my $outfh, "<$outfile" or die "Could not open $outfile: $!\n";
43     my $line = <$outfh>;
44     close $outfh;
45     chomp $line;
46     return if $line =~ /^\#ifndef\s+_IPXE_\S+_H$/;
47   }
48
49   # Search for importable header
50   foreach my $edkdir ( @$edkdirs ) {
51     my $infile = catfile ( $edktop, $edkdir, $filename );
52     if ( -e $infile ) {
53       # We have found a matching source file - import it
54       print "$filename <- ".catfile ( $edkdir, $filename )."\n"
55           if $verbosity >= 1;
56       open my $infh, "<$infile" or die "Could not open $infile: $!\n";
57       ( undef, my $outdir, undef ) = splitpath ( $outfile );
58       mkpath ( $outdir );
59       open my $outfh, ">$outfile" or die "Could not open $outfile: $!\n";
60       my @dependencies = ();
61       my $licence;
62       my $maybe_guard;
63       my $guard;
64       while ( <$infh> ) {
65         # Strip CR and trailing whitespace
66         s/\r//g;
67         s/\s*$//g;
68         chomp;
69         # Update include lines, and record included files
70         if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include <ipxe\/efi\/$1>/ ) {
71           push @dependencies, $1;
72         }
73         # Check for BSD licence statement
74         if ( /^\s*THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE/ ) {
75           die "Licence detected after header guard\n" if $guard;
76           $licence = "BSD3";
77         }
78         # Write out line
79         print $outfh "$_\n";
80         # Apply FILE_LICENCE() immediately after include guard
81         if ( defined $maybe_guard && ! defined $guard ) {
82           if ( /^\#define\s+_?_${maybe_guard}_?_$/ ) {
83             $guard = $maybe_guard;
84             print $outfh "\nFILE_LICENCE ( $licence );\n" if $licence;
85           }
86           undef $maybe_guard;
87         }
88         if ( /^#ifndef\s+_?_(\S+)_?_/ ) {
89           $maybe_guard = $1;
90         }
91       }
92       close $outfh;
93       close $infh;
94       # Warn if no licence was detected
95       warn "Cannot detect licence in $infile\n" unless $licence;
96       warn "Cannot detect header guard in $infile\n" unless $guard;
97       # Recurse to handle any included files that we don't already have
98       foreach my $dependency ( @dependencies ) {
99         if ( ! -e catfile ( $ipxedir, $dependency ) ) {
100           print "...following dependency on $dependency\n" if $verbosity >= 1;
101           try_import_file ( $ipxedir, $edktop, $edkdirs, $dependency );
102         }
103       }
104       return;
105     }
106   }
107   die "$filename has no equivalent in $edktop\n";
108 }
109
110 # Parse command-line options
111 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
112 GetOptions (
113   'verbose|v+' => sub { $verbosity++; },
114   'quiet|q+' => sub { $verbosity--; },
115   'help|h' => sub { pod2usage ( 1 ); },
116 ) or die "Could not parse command-line options\n";
117 pod2usage ( 1 ) unless @ARGV == 1;
118 my $edktop = shift;
119
120 # Identify edk import directories
121 my $edkdirs = [ "MdePkg/Include", "IntelFrameworkPkg/Include",
122                 "MdeModulePkg/Include", "EdkCompatibilityPkg/Foundation" ];
123 foreach my $edkdir ( @$edkdirs ) {
124   die "Directory \"$edktop\" does not appear to contain the EFI EDK2 "
125       ."(missing \"$edkdir\")\n" unless -d catdir ( $edktop, $edkdir );
126 }
127
128 # Identify iPXE EFI includes directory
129 my $ipxedir = $FindBin::Bin;
130 die "Directory \"$ipxedir\" does not appear to contain the iPXE EFI includes\n"
131     unless -e catfile ( $ipxedir, "../../../include/ipxe/efi" );
132
133 if ( $verbosity >= 1 ) {
134   print "Importing EFI headers into $ipxedir\nfrom ";
135   print join ( "\n and ", map { catdir ( $edktop, $_ ) } @$edkdirs )."\n";
136 }
137
138 # Import headers
139 find ( { wanted => sub {
140   try_import_file ( $ipxedir, $edktop, $edkdirs, abs2rel ( $_, $ipxedir ) );
141 }, no_chdir => 1 }, $ipxedir );