You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

import.pl 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/perl -w
  2. =head1 NAME
  3. import.pl
  4. =head1 SYNOPSIS
  5. import.pl [options] /path/to/edk2/edk2
  6. Options:
  7. -h,--help Display brief help message
  8. -v,--verbose Increase verbosity
  9. -q,--quiet Decrease verbosity
  10. =cut
  11. use File::Spec::Functions qw ( :ALL );
  12. use File::Find;
  13. use File::Path;
  14. use Getopt::Long;
  15. use Pod::Usage;
  16. use FindBin;
  17. use strict;
  18. use warnings;
  19. my $verbosity = 0;
  20. sub try_import_file {
  21. my $ipxedir = shift;
  22. my $edktop = shift;
  23. my $edkdirs = shift;
  24. my $filename = shift;
  25. # Skip everything except headers
  26. return unless $filename =~ /\.h$/;
  27. # Skip files that are iPXE native headers
  28. my $outfile = catfile ( $ipxedir, $filename );
  29. if ( -s $outfile ) {
  30. open my $outfh, "<$outfile" or die "Could not open $outfile: $!\n";
  31. my $line = <$outfh>;
  32. close $outfh;
  33. chomp $line;
  34. return if $line =~ /^\#ifndef\s+_IPXE_\S+_H$/;
  35. }
  36. # Search for importable header
  37. foreach my $edkdir ( @$edkdirs ) {
  38. my $infile = catfile ( $edktop, $edkdir, $filename );
  39. if ( -e $infile ) {
  40. # We have found a matching source file - import it
  41. print "$filename <- ".catfile ( $edkdir, $filename )."\n"
  42. if $verbosity >= 1;
  43. open my $infh, "<$infile" or die "Could not open $infile: $!\n";
  44. ( undef, my $outdir, undef ) = splitpath ( $outfile );
  45. mkpath ( $outdir );
  46. open my $outfh, ">$outfile" or die "Could not open $outfile: $!\n";
  47. my @dependencies = ();
  48. my $licence;
  49. my $maybe_guard;
  50. my $guard;
  51. while ( <$infh> ) {
  52. # Strip CR and trailing whitespace
  53. s/\r//g;
  54. s/\s*$//g;
  55. chomp;
  56. # Update include lines, and record included files
  57. if ( s/^\#include\s+[<\"](\S+)[>\"]/\#include <ipxe\/efi\/$1>/ ) {
  58. push @dependencies, $1;
  59. }
  60. # Check for BSD licence statement
  61. if ( /^\s*THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE/ ) {
  62. die "Licence detected after header guard\n" if $guard;
  63. $licence = "BSD3";
  64. }
  65. # Write out line
  66. print $outfh "$_\n";
  67. # Apply FILE_LICENCE() immediately after include guard
  68. if ( defined $maybe_guard && ! defined $guard ) {
  69. if ( /^\#define\s+_?_${maybe_guard}_?_$/ ) {
  70. $guard = $maybe_guard;
  71. print $outfh "\nFILE_LICENCE ( $licence );\n" if $licence;
  72. }
  73. undef $maybe_guard;
  74. }
  75. if ( /^#ifndef\s+_?_(\S+)_?_/ ) {
  76. $maybe_guard = $1;
  77. }
  78. }
  79. close $outfh;
  80. close $infh;
  81. # Warn if no licence was detected
  82. warn "Cannot detect licence in $infile\n" unless $licence;
  83. warn "Cannot detect header guard in $infile\n" unless $guard;
  84. # Recurse to handle any included files that we don't already have
  85. foreach my $dependency ( @dependencies ) {
  86. if ( ! -e catfile ( $ipxedir, $dependency ) ) {
  87. print "...following dependency on $dependency\n" if $verbosity >= 1;
  88. try_import_file ( $ipxedir, $edktop, $edkdirs, $dependency );
  89. }
  90. }
  91. return;
  92. }
  93. }
  94. die "$filename has no equivalent in $edktop\n";
  95. }
  96. # Parse command-line options
  97. Getopt::Long::Configure ( 'bundling', 'auto_abbrev' );
  98. GetOptions (
  99. 'verbose|v+' => sub { $verbosity++; },
  100. 'quiet|q+' => sub { $verbosity--; },
  101. 'help|h' => sub { pod2usage ( 1 ); },
  102. ) or die "Could not parse command-line options\n";
  103. pod2usage ( 1 ) unless @ARGV == 1;
  104. my $edktop = shift;
  105. # Identify edk import directories
  106. my $edkdirs = [ "MdePkg/Include", "IntelFrameworkPkg/Include",
  107. "MdeModulePkg/Include", "EdkCompatibilityPkg/Foundation" ];
  108. foreach my $edkdir ( @$edkdirs ) {
  109. die "Directory \"$edktop\" does not appear to contain the EFI EDK2 "
  110. ."(missing \"$edkdir\")\n" unless -d catdir ( $edktop, $edkdir );
  111. }
  112. # Identify iPXE EFI includes directory
  113. my $ipxedir = $FindBin::Bin;
  114. die "Directory \"$ipxedir\" does not appear to contain the iPXE EFI includes\n"
  115. unless -e catfile ( $ipxedir, "../../../include/ipxe/efi" );
  116. if ( $verbosity >= 1 ) {
  117. print "Importing EFI headers into $ipxedir\nfrom ";
  118. print join ( "\n and ", map { catdir ( $edktop, $_ ) } @$edkdirs )."\n";
  119. }
  120. # Import headers
  121. find ( { wanted => sub {
  122. try_import_file ( $ipxedir, $edktop, $edkdirs, abs2rel ( $_, $ipxedir ) );
  123. }, no_chdir => 1 }, $ipxedir );