]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - wimboot/wimboot-2.7.3/src/efi/import.pl
1.1.07 release
[Ventoy.git] / wimboot / wimboot-2.7.3 / src / 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 $wimbootdir = 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 wimboot native headers
40 my $outfile = catfile ( $wimbootdir, $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+_WIMBOOT_\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 while ( <$infh> ) {
62 # Strip CR and trailing whitespace
63 s/\r//g;
64 s/\s*$//g;
65 chomp;
66 # Update include lines, and record included files
67 if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include "efi\/$1"/ ) {
68 push @dependencies, $1;
69 }
70 # Write out line
71 print $outfh "$_\n";
72 }
73 close $outfh;
74 close $infh;
75 # Recurse to handle any included files that we don't already have
76 foreach my $dependency ( @dependencies ) {
77 if ( ! -e catfile ( $wimbootdir, $dependency ) ) {
78 print "...following dependency on $dependency\n" if $verbosity >= 1;
79 try_import_file ( $wimbootdir, $edktop, $edkdirs, $dependency );
80 }
81 }
82 return;
83 }
84 }
85 die "$filename has no equivalent in $edktop\n";
86 }
87
88 # Parse command-line options
89 Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
90 GetOptions (
91 'verbose|v+' => sub { $verbosity++; },
92 'quiet|q+' => sub { $verbosity--; },
93 'help|h' => sub { pod2usage ( 1 ); },
94 ) or die "Could not parse command-line options\n";
95 pod2usage ( 1 ) unless @ARGV == 1;
96 my $edktop = shift;
97
98 # Identify edk import directories
99 my $edkdirs = [ "MdePkg/Include", "IntelFrameworkPkg/Include",
100 "MdeModulePkg/Include", "EdkCompatibilityPkg/Foundation" ];
101 foreach my $edkdir ( @$edkdirs ) {
102 die "Directory \"$edktop\" does not appear to contain the EFI EDK2 "
103 ."(missing \"$edkdir\")\n" unless -d catdir ( $edktop, $edkdir );
104 }
105
106 # Identify wimboot EFI includes directory
107 my $wimbootdir = $FindBin::Bin;
108 die "Directory \"$wimbootdir\" does not contain the wimboot EFI includes\n"
109 unless -e catfile ( $wimbootdir, "../wimboot.h" );
110
111 if ( $verbosity >= 1 ) {
112 print "Importing EFI headers into $wimbootdir\nfrom ";
113 print join ( "\n and ", map { catdir ( $edktop, $_ ) } @$edkdirs )."\n";
114 }
115
116 # Import headers
117 find ( { wanted => sub {
118 try_import_file ( $wimbootdir, $edktop, $edkdirs,
119 abs2rel ( $_, $wimbootdir ) );
120 }, no_chdir => 1 }, $wimbootdir );