tizen 2.3.1 release
[external/qemu.git] / 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 $guard;
63       while ( <$infh> ) {
64         # Strip CR and trailing whitespace
65         s/\r//g;
66         s/\s*$//g;
67         chomp;
68         # Update include lines, and record included files
69         if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include <ipxe\/efi\/$1>/ ) {
70           push @dependencies, $1;
71         }
72         # Check for BSD licence statement
73         if ( /^\s*THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE/ ) {
74           die "Licence detected after header guard\n" if $guard;
75           $licence = "BSD3";
76         }
77         # Write out line
78         print $outfh "$_\n";
79         # Apply FILE_LICENCE() immediately after include guard
80         if ( /^\#define\s+_?_\S+_H_?_$/ ) {
81           die "Duplicate header guard detected in $infile\n" if $guard;
82           $guard = 1;
83           print $outfh "\nFILE_LICENCE ( $licence );\n" if $licence;
84         }
85       }
86       close $outfh;
87       close $infh;
88       # Warn if no licence was detected
89       warn "Cannot detect licence in $infile\n" unless $licence;
90       warn "Cannot detect header guard in $infile\n" unless $guard;
91       # Recurse to handle any included files that we don't already have
92       foreach my $dependency ( @dependencies ) {
93         if ( ! -e catfile ( $ipxedir, $dependency ) ) {
94           print "...following dependency on $dependency\n" if $verbosity >= 1;
95           try_import_file ( $ipxedir, $edktop, $edkdirs, $dependency );
96         }
97       }
98       return;
99     }
100   }
101   die "$filename has no equivalent in $edktop\n";
102 }
103
104 # Parse command-line options
105 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
106 GetOptions (
107   'verbose|v+' => sub { $verbosity++; },
108   'quiet|q+' => sub { $verbosity--; },
109   'help|h' => sub { pod2usage ( 1 ); },
110 ) or die "Could not parse command-line options\n";
111 pod2usage ( 1 ) unless @ARGV == 1;
112 my $edktop = shift;
113
114 # Identify edk import directories
115 my $edkdirs = [ "MdePkg/Include", "IntelFrameworkPkg/Include",
116                 "MdeModulePkg/Include" ];
117 foreach my $edkdir ( @$edkdirs ) {
118   die "Directory \"$edktop\" does not appear to contain the EFI EDK2 "
119       ."(missing \"$edkdir\")\n" unless -d catdir ( $edktop, $edkdir );
120 }
121
122 # Identify iPXE EFI includes directory
123 my $ipxedir = $FindBin::Bin;
124 die "Directory \"$ipxedir\" does not appear to contain the iPXE EFI includes\n"
125     unless -e catfile ( $ipxedir, "../../../include/ipxe/efi" );
126
127 if ( $verbosity >= 1 ) {
128   print "Importing EFI headers into $ipxedir\nfrom ";
129   print join ( "\n and ", map { catdir ( $edktop, $_ ) } @$edkdirs )."\n";
130 }
131
132 # Import headers
133 find ( { wanted => sub {
134   try_import_file ( $ipxedir, $edktop, $edkdirs, abs2rel ( $_, $ipxedir ) );
135 }, no_chdir => 1 }, $ipxedir );