• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/perl -w
2
3use File::Spec::Functions qw ( :ALL );
4use File::Find;
5use File::Path;
6use FindBin;
7use strict;
8use warnings;
9
10sub try_import_file {
11  my $gpxedir = shift;
12  my $edkdirs = shift;
13  my $filename = shift;
14
15  # Skip everything except headers
16  return unless $filename =~ /\.h$/;
17  print "$filename...";
18
19  my $outfile = catfile ( $gpxedir, $filename );
20  foreach my $edkdir ( @$edkdirs ) {
21    my $infile = catfile ( $edkdir, $filename );
22    if ( -e $infile ) {
23      # We have found a matching source file - import it
24      print "$infile\n";
25      open my $infh, "<$infile" or die "Could not open $infile: $!\n";
26      ( undef, my $outdir, undef ) = splitpath ( $outfile );
27      mkpath ( $outdir );
28      open my $outfh, ">$outfile" or die "Could not open $outfile: $!\n";
29      my @dependencies = ();
30      while ( <$infh> ) {
31	# Strip CR and trailing whitespace
32	s/\r//g;
33	s/\s*$//g;
34	chomp;
35	# Update include lines, and record included files
36	if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include <gpxe\/efi\/$1>/ ) {
37	  push @dependencies, $1;
38	}
39	print $outfh "$_\n";
40      }
41      close $outfh;
42      close $infh;
43      # Recurse to handle any included files that we don't already have
44      foreach my $dependency ( @dependencies ) {
45	if ( ! -e catfile ( $gpxedir, $dependency ) ) {
46	  print "...following dependency on $dependency\n";
47	  try_import_file ( $gpxedir, $edkdirs, $dependency );
48	}
49      }
50      return;
51    }
52  }
53  print "no equivalent found\n";
54}
55
56# Identify edk import directories
57die "Syntax $0 /path/to/edk2/edk2\n" unless @ARGV == 1;
58my $edktop = shift;
59die "Directory \"$edktop\" does not appear to contain the EFI EDK2\n"
60    unless -e catfile ( $edktop, "MdePkg" );
61my $edkdirs = [ catfile ( $edktop, "MdePkg/Include" ),
62		catfile ( $edktop, "IntelFrameworkPkg/Include" ) ];
63
64# Identify gPXE EFI includes directory
65my $gpxedir = $FindBin::Bin;
66die "Directory \"$gpxedir\" does not appear to contain the gPXE EFI includes\n"
67    unless -e catfile ( $gpxedir, "../../../include/gpxe/efi" );
68
69print "Importing EFI headers into $gpxedir\nfrom ";
70print join ( "\n and ", @$edkdirs )."\n";
71
72# Import headers
73find ( { wanted => sub {
74  try_import_file ( $gpxedir, $edkdirs, abs2rel ( $_, $gpxedir ) );
75}, no_chdir => 1 }, $gpxedir );
76