9 import.pl [options] /path/to/edk2/edk2
13 -h,--help Display brief help message
14 -v,--verbose Increase verbosity
15 -q,--quiet Decrease verbosity
19 use File::Spec::Functions qw ( :ALL );
36 # Skip everything except headers
37 return unless $filename =~ /\.h$/;
39 # Skip files that are iPXE native headers
40 my $outfile = catfile ( $ipxedir, $filename );
42 open my $outfh, "<$outfile" or die "Could not open $outfile: $!\n";
46 return if $line =~ /^\#ifndef\s+_IPXE_\S+_H$/;
49 # Search for importable header
50 foreach my $edkdir ( @$edkdirs ) {
51 my $infile = catfile ( $edktop, $edkdir, $filename );
53 # We have found a matching source file - import it
54 print "$filename <- ".catfile ( $edkdir, $filename )."\n"
56 open my $infh, "<$infile" or die "Could not open $infile: $!\n";
57 ( undef, my $outdir, undef ) = splitpath ( $outfile );
59 open my $outfh, ">$outfile" or die "Could not open $outfile: $!\n";
60 my @dependencies = ();
65 # Strip CR and trailing whitespace
69 # Update include lines, and record included files
70 if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include <ipxe\/efi\/$1>/ ) {
71 push @dependencies, $1;
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;
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;
88 if ( /^#ifndef\s+_?_(\S+)_?_/ ) {
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 );
107 die "$filename has no equivalent in $edktop\n";
110 # Parse command-line options
111 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
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;
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 );
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" );
133 if ( $verbosity >= 1 ) {
134 print "Importing EFI headers into $ipxedir\nfrom ";
135 print join ( "\n and ", map { catdir ( $edktop, $_ ) } @$edkdirs )."\n";
139 find ( { wanted => sub {
140 try_import_file ( $ipxedir, $edktop, $edkdirs, abs2rel ( $_, $ipxedir ) );
141 }, no_chdir => 1 }, $ipxedir );