]>
git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/OpensslLib/process_files.pl
545f2182842b90efe2bfa0e52e8fc3f8a6e1085e
3 # This script runs the OpenSSL Configure script, then processes the
4 # resulting file list into our local OpensslLib[Crypto].inf and also
5 # takes copies of opensslconf.h and dso_conf.h.
7 # This only needs to be done once by a developer when updating to a
8 # new version of OpenSSL (or changing options, etc.). Normal users
9 # do not need to do this, since the results are stored in the EDK2
10 # git repository for them.
12 # Due to the script wrapping required to process the OpenSSL
13 # configuration data, each native architecture must be processed
14 # individually by the maintainer (in addition to the standard version):
16 # ./process_files.pl X64
17 # ./process_files.pl [Arch]
23 use File
::Path
qw(make_path remove_tree);
26 my $comment_character;
29 # OpenSSL perlasm generator script does not transfer the copyright header
31 sub copy_license_header
33 my @args = split / /, shift; #Separate args by spaces
34 my $source = $args[1]; #Source file is second (after "perl")
35 my $target = pop @args; #Target file is always last
36 chop ($target); #Remove newline char
38 my $temp_file_name = "license.tmp";
39 open (my $source_file, "<" . $source) || die $source;
40 open (my $target_file, "<" . $target) || die $target;
41 open (my $temp_file, ">" . $temp_file_name) || die $temp_file_name;
43 #Add "generated file" warning
44 $source =~ s/^..//; #Remove leading "./"
45 print ($temp_file "$comment_character WARNING: do not edit!\r\n");
46 print ($temp_file "$comment_character Generated from $source\r\n");
47 print ($temp_file "$comment_character\r\n");
49 #Copy source file header to temp file
50 while (my $line = <$source_file>) {
51 next if ($line =~ /#!/); #Ignore shebang line
52 $line =~ s/#/$comment_character/; #Fix comment character for assembly
53 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings
54 print ($temp_file $line);
55 last if ($line =~ /http/); #Last line of copyright header contains a web link
57 print ($temp_file "\r\n");
58 #Retrieve generated assembly contents
59 while (my $line = <$target_file>) {
60 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings
61 print ($temp_file expand
($line)); #expand() replaces tabs with spaces
68 move
($temp_file_name, $target) ||
69 die "Cannot replace \"" . $target . "\"!";
73 # Find the openssl directory name for use lib. We have to do this
74 # inside of BEGIN. The variables we create here, however, don't seem
75 # to be available to the main script, so we have to repeat the
85 # Use PCD to conditionally enable certain openssl features.
86 # $conditional_feature contains pcd_name:fetures_names pairs
87 # of conditional features.
88 # @conditional_feature_dir contains relative_path:pcd_name pairs
89 # of conditional features in openssl, MUST correspond to the content
90 # in $conditional_feature.
92 # Configure list [openssl_configuration : new_define_list : new_file_list : pcd]
93 # 1. no-ec : {NO_EC, NO_ECDH, NO_ECDSA, NO_TLS1_3, NO_SM2} : {/ec/, /sm2/} : PcdOpensslEcEnabled
95 my %conditional_feature = ("PcdOpensslEcEnabled"=>["EC", "ECDH", "ECDSA", "TLS1_3", "SM2"]);
96 my %conditional_feature_dir = ("/ec/"=>"PcdOpensslEcEnabled", "/sm2/"=>"PcdOpensslEcEnabled");
99 $inf_file = "OpensslLib.inf";
100 $uefi_config = "UEFI";
104 if (uc ($arch) eq "X64") {
106 $inf_file = "OpensslLibX64.inf";
107 $uefi_config = "UEFI-x86_64";
109 $comment_character = ";";
110 } elsif (uc ($arch) eq "X64GCC") {
112 $inf_file = "OpensslLibX64Gcc.inf";
113 $uefi_config = "UEFI-x86_64-GCC";
115 $comment_character = "#";
117 die "Unsupported architecture \"" . $arch . "\"!";
119 if ($extension eq "nasm") {
120 if (`nasm -v 2>&1`) {
121 #Presence of nasm executable will trigger inclusion of AVX instructions
122 die "\nCannot run assembly generators with NASM in path!\n\n";
126 # Prepare assembly folder
128 opendir my $dir, $arch ||
129 die "Cannot open assembly folder \"" . $arch . "\"!";
130 while (defined (my $file = readdir $dir)) {
131 if (-d
"$arch/$file") {
132 next if $file eq ".";
133 next if $file eq "..";
134 remove_tree
("$arch/$file", {safe
=> 1}) ||
135 die "Cannot clean assembly folder \"" . "$arch/$file" . "\"!";
141 die "Cannot create assembly folder \"" . $arch . "\"!";
145 # Read the contents of the inf file
146 open( FD
, "<" . $inf_file ) ||
147 die "Cannot open \"" . $inf_file . "\"!";
150 die "Cannot close \"" . $inf_file . "\"!";
153 if (/DEFINE\s+OPENSSL_PATH\s*=\s*([a-z]+)/) {
155 # We need to run Configure before we can include its result...
158 my $basedir = getcwd
();
160 chdir($OPENSSL_PATH) ||
161 die "Cannot change to OpenSSL directory \"" . $OPENSSL_PATH . "\"";
166 "--config=../UefiAsm.conf",
171 "no-autoload-config",
212 # OpenSSL1_1_1b doesn't support default rand-seed-os for UEFI
213 # UEFI only support --with-rand-seed=none
214 "--with-rand-seed=none"
216 die "OpenSSL Configure failed!\n";
218 # Generate opensslconf.h per config data
220 "perl -I. -Mconfigdata util/dofile.pl " .
221 "include/openssl/opensslconf.h.in " .
222 "> include/openssl/opensslconf.h"
224 die "Failed to generate opensslconf.h!\n";
226 # Generate dso_conf.h per config data
228 "perl -I. -Mconfigdata util/dofile.pl " .
229 "include/crypto/dso_conf.h.in " .
230 "> include/crypto/dso_conf.h"
232 die "Failed to generate dso_conf.h!\n";
235 die "Cannot change to base directory \"" . $basedir . "\"";
244 # Retrieve file lists from OpenSSL configdata
246 use configdata qw
/%unified_info/;
247 use configdata qw
/%config/;
248 use configdata qw
/%target/;
251 # Collect build flags from configdata
254 foreach my $f (@{$config{lib_defines
}}) {
258 my @cryptofilelist = ();
259 my @sslfilelist = ();
260 my @asmfilelist = ();
262 foreach my $product ((@{$unified_info{libraries
}},
263 @{$unified_info{engines
}})) {
264 foreach my $o (@{$unified_info{sources
}->{$product}}) {
265 foreach my $s (@{$unified_info{sources
}->{$o}}) {
266 # No need to add unused files in UEFI.
267 # So it can reduce porting time, compile time, library size.
268 next if $s =~ "crypto/bio/b_print.c";
269 next if $s =~ "crypto/rand/randfile.c";
270 next if $s =~ "crypto/store/";
271 next if $s =~ "crypto/err/err_all.c";
272 next if $s =~ "crypto/aes/aes_ecb.c";
274 if ($unified_info{generate
}->{$s}) {
276 my $buildstring = "perl";
277 foreach my $arg (@{$unified_info{generate
}->{$s}}) {
279 $buildstring .= " ./openssl/$arg";
280 } elsif ($arg =~ "PERLASM_SCHEME") {
281 $buildstring .= " $target{perlasm_scheme}";
282 } elsif ($arg =~ "LIB_CFLAGS") {
283 $buildstring .= "$flags";
286 ($s, my $path, undef) = fileparse
($s, qr/\.[^.]*/);
287 $buildstring .= " ./$arch/$path$s.$extension";
288 make_path
("./$arch/$path");
289 push @asmbuild, "$buildstring\n";
290 push @asmfilelist, " $arch/$path$s.$extension\r\n";
294 if ($product =~ "libssl") {
295 push @sslfilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";
298 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $s;
299 foreach (keys(%conditional_feature_dir)) {
301 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$_};
304 push @cryptofilelist, "\r\n";
311 # Update the perl script to generate the missing header files
314 for (sort keys %{$unified_info{dirinfo
}}){
323 @files = glob($_."/*.h");
324 push @headers, @files;
330 push @sslfilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";
333 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $_;
334 foreach my $conditional_key (keys(%conditional_feature_dir)) {
335 if ($_ =~ $conditional_key) {
336 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$conditional_key};
339 push @cryptofilelist, "\r\n";
344 # Generate assembly files
347 print "\n--> Generating assembly files ... ";
348 foreach my $buildstring (@asmbuild) {
349 system ("$buildstring");
350 copy_license_header
($buildstring);
356 # Update OpensslLib.inf with autogenerated file list
360 print "\n--> Updating $inf_file ... ";
362 if ($_ =~ "DEFINE OPENSSL_FLAGS_CONFIG") {
363 push @new_inf, " DEFINE OPENSSL_FLAGS_CONFIG =" . $flags . "\r\n";
366 if ( $_ =~ "# Autogenerated files list starts here" ) {
367 push @new_inf, $_, @asmfilelist, @cryptofilelist, @sslfilelist;
371 if ( $_ =~ "# Autogenerated files list ends here" ) {
381 my $new_inf_file = $inf_file . ".new";
382 open( FD
, ">" . $new_inf_file ) ||
384 print( FD
@new_inf ) ||
388 rename( $new_inf_file, $inf_file ) ||
389 die "rename $inf_file";
392 if (!defined $arch) {
394 # Update OpensslLibCrypto.inf with auto-generated file list (no libssl)
396 $inf_file = "OpensslLibCrypto.inf";
398 # Read the contents of the inf file
401 open( FD
, "<" . $inf_file ) ||
402 die "Cannot open \"" . $inf_file . "\"!";
405 die "Cannot close \"" . $inf_file . "\"!";
408 print "\n--> Updating OpensslLibCrypto.inf ... ";
410 if ( $_ =~ "# Autogenerated files list starts here" ) {
411 push @new_inf, $_, @cryptofilelist;
415 if ( $_ =~ "# Autogenerated files list ends here" ) {
425 $new_inf_file = $inf_file . ".new";
426 open( FD
, ">" . $new_inf_file ) ||
428 print( FD
@new_inf ) ||
432 rename( $new_inf_file, $inf_file ) ||
433 die "rename $inf_file";
438 # Copy opensslconf.h and dso_conf.h generated from OpenSSL Configuration
440 print "\n--> Duplicating opensslconf.h into Include/openssl ... ";
442 "perl -pe 's/\\n/\\r\\n/' " .
443 "< " . $OPENSSL_PATH . "/include/openssl/opensslconf.h " .
444 "> " . $OPENSSL_PATH . "/../../Include/openssl/opensslconf_generated.h"
446 die "Cannot copy opensslconf.h!";
449 print "\n--> Duplicating dso_conf.h into Include/crypto ... ";
451 "perl -pe 's/\\n/\\r\\n/' " .
452 "< " . $OPENSSL_PATH . "/include/crypto/dso_conf.h" .
453 "> " . $OPENSSL_PATH . "/../../Include/crypto/dso_conf.h"
455 die "Cannot copy dso_conf.h!";
459 # Add conditional feature to opensslconf.h
461 my $conf_file = "../Include/openssl/opensslconf.h";
463 my @conditional_define = ();
464 print "\n--> Updating conditional feature in $conf_file ... ";
466 foreach my $pcd_name (keys(%conditional_feature)) {
467 push @conditional_define, "#if !FixedPcdGetBool ($pcd_name)\r\n";
468 foreach (@{$conditional_feature{$pcd_name}}) {
469 push @conditional_define, "# ifndef OPENSSL_NO_$_\r\n";
470 push @conditional_define, "# define OPENSSL_NO_$_\r\n";
471 push @conditional_define, "# endif\r\n";
473 push @conditional_define, "#endif\r\n";
476 open( FD
, "<" . $conf_file ) ||
479 # Insert conditional define to the begin of opensslconf.h
480 if ($_ =~ "Autogenerated conditional openssl feature list starts here") {
481 push @conf_raw, $_, @conditional_define;
485 if ($_ =~ "Autogenerated conditional openssl feature list ends here") {
496 open( FD
, ">" . $conf_file ) ||
498 print( FD
@conf_raw ) ||
504 print "\nProcessing Files Done!\n";