]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/OpensslLib/process_files.pl
CryptoPkg: Update process_files.pl to automatically add PCD config option
[mirror_edk2.git] / CryptoPkg / Library / OpensslLib / process_files.pl
CommitLineData
264702a0
HW
1#!/usr/bin/perl -w\r
2#\r
3# This script runs the OpenSSL Configure script, then processes the\r
4# resulting file list into our local OpensslLib[Crypto].inf and also\r
1bcc65b9 5# takes copies of opensslconf.h and dso_conf.h.\r
264702a0
HW
6#\r
7# This only needs to be done once by a developer when updating to a\r
8# new version of OpenSSL (or changing options, etc.). Normal users\r
9# do not need to do this, since the results are stored in the EDK2\r
10# git repository for them.\r
11#\r
878a92a8
CZ
12# Due to the script wrapping required to process the OpenSSL\r
13# configuration data, each native architecture must be processed\r
14# individually by the maintainer (in addition to the standard version):\r
15# ./process_files.pl\r
16# ./process_files.pl X64\r
17# ./process_files.pl [Arch]\r
18\r
264702a0
HW
19use strict;\r
20use Cwd;\r
21use File::Copy;\r
878a92a8
CZ
22use File::Basename;\r
23use File::Path qw(make_path remove_tree);\r
24use Text::Tabs;\r
25\r
26my $comment_character;\r
27\r
28#\r
29# OpenSSL perlasm generator script does not transfer the copyright header\r
30#\r
31sub copy_license_header\r
32{\r
33 my @args = split / /, shift; #Separate args by spaces\r
34 my $source = $args[1]; #Source file is second (after "perl")\r
35 my $target = pop @args; #Target file is always last\r
36 chop ($target); #Remove newline char\r
37\r
38 my $temp_file_name = "license.tmp";\r
39 open (my $source_file, "<" . $source) || die $source;\r
40 open (my $target_file, "<" . $target) || die $target;\r
41 open (my $temp_file, ">" . $temp_file_name) || die $temp_file_name;\r
42\r
43 #Add "generated file" warning\r
44 $source =~ s/^..//; #Remove leading "./"\r
45 print ($temp_file "$comment_character WARNING: do not edit!\r\n");\r
46 print ($temp_file "$comment_character Generated from $source\r\n");\r
47 print ($temp_file "$comment_character\r\n");\r
48\r
49 #Copy source file header to temp file\r
50 while (my $line = <$source_file>) {\r
51 next if ($line =~ /#!/); #Ignore shebang line\r
52 $line =~ s/#/$comment_character/; #Fix comment character for assembly\r
53 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings\r
54 print ($temp_file $line);\r
55 last if ($line =~ /http/); #Last line of copyright header contains a web link\r
56 }\r
57 print ($temp_file "\r\n");\r
58 #Retrieve generated assembly contents\r
59 while (my $line = <$target_file>) {\r
60 $line =~ s/\s+$/\r\n/; #Trim trailing whitepsace, fixup line endings\r
61 print ($temp_file expand ($line)); #expand() replaces tabs with spaces\r
62 }\r
63\r
64 close ($source_file);\r
65 close ($target_file);\r
66 close ($temp_file);\r
67\r
68 move ($temp_file_name, $target) ||\r
69 die "Cannot replace \"" . $target . "\"!";\r
70}\r
264702a0
HW
71\r
72#\r
73# Find the openssl directory name for use lib. We have to do this\r
74# inside of BEGIN. The variables we create here, however, don't seem\r
75# to be available to the main script, so we have to repeat the\r
76# exercise.\r
77#\r
78my $inf_file;\r
79my $OPENSSL_PATH;\r
878a92a8
CZ
80my $uefi_config;\r
81my $extension;\r
82my $arch;\r
264702a0 83my @inf;\r
499b0d5f
YL
84#\r
85# Use PCD to conditionally enable certain openssl features.\r
86# $conditional_feature contains pcd_name:fetures_names pairs\r
87# of conditional features.\r
88# @conditional_feature_dir contains relative_path:pcd_name pairs\r
89# of conditional features in openssl, MUST correspond to the content\r
90# in $conditional_feature.\r
91#\r
92# Configure list [openssl_configuration : new_define_list : new_file_list : pcd]\r
93# 1. no-ec : {NO_EC, NO_ECDH, NO_ECDSA, NO_TLS1_3, NO_SM2} : {/ec/, /sm2/} : PcdOpensslEcEnabled\r
94#\r
95my %conditional_feature = ("PcdOpensslEcEnabled"=>["EC", "ECDH", "ECDSA", "TLS1_3", "SM2"]);\r
96my %conditional_feature_dir = ("/ec/"=>"PcdOpensslEcEnabled", "/sm2/"=>"PcdOpensslEcEnabled");\r
264702a0
HW
97\r
98BEGIN {\r
99 $inf_file = "OpensslLib.inf";\r
878a92a8
CZ
100 $uefi_config = "UEFI";\r
101 $arch = shift;\r
102\r
103 if (defined $arch) {\r
104 if (uc ($arch) eq "X64") {\r
105 $arch = "X64";\r
106 $inf_file = "OpensslLibX64.inf";\r
107 $uefi_config = "UEFI-x86_64";\r
108 $extension = "nasm";\r
109 $comment_character = ";";\r
110 } elsif (uc ($arch) eq "X64GCC") {\r
111 $arch = "X64Gcc";\r
112 $inf_file = "OpensslLibX64Gcc.inf";\r
113 $uefi_config = "UEFI-x86_64-GCC";\r
114 $extension = "S";\r
115 $comment_character = "#";\r
116 } else {\r
117 die "Unsupported architecture \"" . $arch . "\"!";\r
118 }\r
119 if ($extension eq "nasm") {\r
120 if (`nasm -v 2>&1`) {\r
121 #Presence of nasm executable will trigger inclusion of AVX instructions\r
122 die "\nCannot run assembly generators with NASM in path!\n\n";\r
123 }\r
124 }\r
125\r
126 # Prepare assembly folder\r
127 if (-d $arch) {\r
128 opendir my $dir, $arch ||\r
129 die "Cannot open assembly folder \"" . $arch . "\"!";\r
130 while (defined (my $file = readdir $dir)) {\r
131 if (-d "$arch/$file") {\r
132 next if $file eq ".";\r
133 next if $file eq "..";\r
134 remove_tree ("$arch/$file", {safe => 1}) ||\r
135 die "Cannot clean assembly folder \"" . "$arch/$file" . "\"!";\r
136 }\r
137 }\r
138\r
139 } else {\r
140 mkdir $arch ||\r
141 die "Cannot create assembly folder \"" . $arch . "\"!";\r
142 }\r
143 }\r
264702a0
HW
144\r
145 # Read the contents of the inf file\r
146 open( FD, "<" . $inf_file ) ||\r
147 die "Cannot open \"" . $inf_file . "\"!";\r
148 @inf = (<FD>);\r
149 close(FD) ||\r
150 die "Cannot close \"" . $inf_file . "\"!";\r
151\r
152 foreach (@inf) {\r
153 if (/DEFINE\s+OPENSSL_PATH\s*=\s*([a-z]+)/) {\r
154\r
155 # We need to run Configure before we can include its result...\r
156 $OPENSSL_PATH = $1;\r
157\r
158 my $basedir = getcwd();\r
159\r
160 chdir($OPENSSL_PATH) ||\r
161 die "Cannot change to OpenSSL directory \"" . $OPENSSL_PATH . "\"";\r
162\r
163 # Configure UEFI\r
164 system(\r
165 "./Configure",\r
878a92a8
CZ
166 "--config=../UefiAsm.conf",\r
167 "$uefi_config",\r
264702a0 168 "no-afalgeng",\r
264702a0 169 "no-async",\r
264702a0 170 "no-autoerrinit",\r
c72ca466 171 "no-autoload-config",\r
264702a0
HW
172 "no-bf",\r
173 "no-blake2",\r
174 "no-camellia",\r
175 "no-capieng",\r
176 "no-cast",\r
177 "no-chacha",\r
178 "no-cms",\r
179 "no-ct",\r
180 "no-deprecated",\r
394d5896 181 "no-des",\r
264702a0
HW
182 "no-dgram",\r
183 "no-dsa",\r
184 "no-dynamic-engine",\r
264702a0
HW
185 "no-ec2m",\r
186 "no-engine",\r
187 "no-err",\r
188 "no-filenames",\r
189 "no-gost",\r
190 "no-hw",\r
191 "no-idea",\r
9b2a082e 192 "no-md4",\r
264702a0
HW
193 "no-mdc2",\r
194 "no-pic",\r
195 "no-ocb",\r
196 "no-poly1305",\r
197 "no-posix-io",\r
198 "no-rc2",\r
f4c15d38 199 "no-rc4",\r
264702a0
HW
200 "no-rfc3779",\r
201 "no-rmd160",\r
202 "no-scrypt",\r
203 "no-seed",\r
204 "no-sock",\r
205 "no-srp",\r
206 "no-ssl",\r
207 "no-stdio",\r
208 "no-threads",\r
209 "no-ts",\r
210 "no-ui",\r
6fcc3d68
XL
211 "no-whirlpool",\r
212 # OpenSSL1_1_1b doesn't support default rand-seed-os for UEFI\r
213 # UEFI only support --with-rand-seed=none\r
214 "--with-rand-seed=none"\r
264702a0
HW
215 ) == 0 ||\r
216 die "OpenSSL Configure failed!\n";\r
217\r
218 # Generate opensslconf.h per config data\r
219 system(\r
220 "perl -I. -Mconfigdata util/dofile.pl " .\r
221 "include/openssl/opensslconf.h.in " .\r
222 "> include/openssl/opensslconf.h"\r
223 ) == 0 ||\r
224 die "Failed to generate opensslconf.h!\n";\r
225\r
1bcc65b9
SZ
226 # Generate dso_conf.h per config data\r
227 system(\r
228 "perl -I. -Mconfigdata util/dofile.pl " .\r
8c30327d
GJ
229 "include/crypto/dso_conf.h.in " .\r
230 "> include/crypto/dso_conf.h"\r
1bcc65b9
SZ
231 ) == 0 ||\r
232 die "Failed to generate dso_conf.h!\n";\r
233\r
264702a0
HW
234 chdir($basedir) ||\r
235 die "Cannot change to base directory \"" . $basedir . "\"";\r
236\r
237 push @INC, $1;\r
238 last;\r
239 }\r
240 }\r
241}\r
242\r
243#\r
244# Retrieve file lists from OpenSSL configdata\r
245#\r
246use configdata qw/%unified_info/;\r
878a92a8
CZ
247use configdata qw/%config/;\r
248use configdata qw/%target/;\r
249\r
250#\r
251# Collect build flags from configdata\r
252#\r
253my $flags = "";\r
254foreach my $f (@{$config{lib_defines}}) {\r
255 $flags .= " -D$f";\r
256}\r
264702a0
HW
257\r
258my @cryptofilelist = ();\r
259my @sslfilelist = ();\r
878a92a8
CZ
260my @asmfilelist = ();\r
261my @asmbuild = ();\r
264702a0
HW
262foreach my $product ((@{$unified_info{libraries}},\r
263 @{$unified_info{engines}})) {\r
264 foreach my $o (@{$unified_info{sources}->{$product}}) {\r
265 foreach my $s (@{$unified_info{sources}->{$o}}) {\r
7eee0488
XL
266 # No need to add unused files in UEFI.\r
267 # So it can reduce porting time, compile time, library size.\r
878a92a8 268 next if $s =~ "crypto/bio/b_print.c";\r
7eee0488
XL
269 next if $s =~ "crypto/rand/randfile.c";\r
270 next if $s =~ "crypto/store/";\r
51f7a3e6 271 next if $s =~ "crypto/err/err_all.c";\r
89db28b9 272 next if $s =~ "crypto/aes/aes_ecb.c";\r
7eee0488 273\r
878a92a8
CZ
274 if ($unified_info{generate}->{$s}) {\r
275 if (defined $arch) {\r
276 my $buildstring = "perl";\r
277 foreach my $arg (@{$unified_info{generate}->{$s}}) {\r
278 if ($arg =~ ".pl") {\r
279 $buildstring .= " ./openssl/$arg";\r
280 } elsif ($arg =~ "PERLASM_SCHEME") {\r
281 $buildstring .= " $target{perlasm_scheme}";\r
282 } elsif ($arg =~ "LIB_CFLAGS") {\r
283 $buildstring .= "$flags";\r
284 }\r
285 }\r
286 ($s, my $path, undef) = fileparse($s, qr/\.[^.]*/);\r
287 $buildstring .= " ./$arch/$path$s.$extension";\r
288 make_path ("./$arch/$path");\r
289 push @asmbuild, "$buildstring\n";\r
290 push @asmfilelist, " $arch/$path$s.$extension\r\n";\r
291 }\r
292 next;\r
293 }\r
264702a0
HW
294 if ($product =~ "libssl") {\r
295 push @sslfilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";\r
296 next;\r
297 }\r
499b0d5f
YL
298 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $s;\r
299 foreach (keys(%conditional_feature_dir)) {\r
300 if ($s =~ $_) {\r
301 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$_};\r
302 }\r
303 }\r
304 push @cryptofilelist, "\r\n";\r
264702a0
HW
305 }\r
306 }\r
307}\r
308\r
9f4fbd56
SZ
309\r
310#\r
311# Update the perl script to generate the missing header files\r
312#\r
313my @dir_list = ();\r
c72ca466 314for (sort keys %{$unified_info{dirinfo}}){\r
9f4fbd56
SZ
315 push @dir_list,$_;\r
316}\r
317\r
318my $dir = getcwd();\r
319my @files = ();\r
320my @headers = ();\r
321chdir ("openssl");\r
322foreach(@dir_list){\r
323 @files = glob($_."/*.h");\r
324 push @headers, @files;\r
325}\r
326chdir ($dir);\r
327\r
328foreach (@headers){\r
329 if(/ssl/){\r
330 push @sslfilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";\r
331 next;\r
332 }\r
499b0d5f
YL
333 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $_;\r
334 foreach my $conditional_key (keys(%conditional_feature_dir)) {\r
335 if ($_ =~ $conditional_key) {\r
336 push @cryptofilelist, ' |*|*|*|gEfiCryptoPkgTokenSpaceGuid.' . $conditional_feature_dir{$conditional_key};\r
337 }\r
338 }\r
339 push @cryptofilelist, "\r\n";\r
9f4fbd56
SZ
340}\r
341\r
342\r
878a92a8
CZ
343#\r
344# Generate assembly files\r
345#\r
346if (@asmbuild) {\r
347 print "\n--> Generating assembly files ... ";\r
348 foreach my $buildstring (@asmbuild) {\r
349 system ("$buildstring");\r
350 copy_license_header ($buildstring);\r
351 }\r
352 print "Done!";\r
353}\r
354\r
264702a0
HW
355#\r
356# Update OpensslLib.inf with autogenerated file list\r
357#\r
358my @new_inf = ();\r
359my $subbing = 0;\r
878a92a8 360print "\n--> Updating $inf_file ... ";\r
264702a0 361foreach (@inf) {\r
878a92a8
CZ
362 if ($_ =~ "DEFINE OPENSSL_FLAGS_CONFIG") {\r
363 push @new_inf, " DEFINE OPENSSL_FLAGS_CONFIG =" . $flags . "\r\n";\r
364 next;\r
365 }\r
264702a0 366 if ( $_ =~ "# Autogenerated files list starts here" ) {\r
878a92a8 367 push @new_inf, $_, @asmfilelist, @cryptofilelist, @sslfilelist;\r
264702a0
HW
368 $subbing = 1;\r
369 next;\r
370 }\r
371 if ( $_ =~ "# Autogenerated files list ends here" ) {\r
372 push @new_inf, $_;\r
373 $subbing = 0;\r
374 next;\r
375 }\r
376\r
377 push @new_inf, $_\r
378 unless ($subbing);\r
379}\r
380\r
381my $new_inf_file = $inf_file . ".new";\r
382open( FD, ">" . $new_inf_file ) ||\r
383 die $new_inf_file;\r
384print( FD @new_inf ) ||\r
385 die $new_inf_file;\r
386close(FD) ||\r
387 die $new_inf_file;\r
388rename( $new_inf_file, $inf_file ) ||\r
389 die "rename $inf_file";\r
390print "Done!";\r
391\r
878a92a8
CZ
392if (!defined $arch) {\r
393 #\r
394 # Update OpensslLibCrypto.inf with auto-generated file list (no libssl)\r
395 #\r
396 $inf_file = "OpensslLibCrypto.inf";\r
264702a0 397\r
878a92a8
CZ
398 # Read the contents of the inf file\r
399 @inf = ();\r
400 @new_inf = ();\r
401 open( FD, "<" . $inf_file ) ||\r
402 die "Cannot open \"" . $inf_file . "\"!";\r
403 @inf = (<FD>);\r
404 close(FD) ||\r
405 die "Cannot close \"" . $inf_file . "\"!";\r
406\r
407 $subbing = 0;\r
408 print "\n--> Updating OpensslLibCrypto.inf ... ";\r
409 foreach (@inf) {\r
410 if ( $_ =~ "# Autogenerated files list starts here" ) {\r
411 push @new_inf, $_, @cryptofilelist;\r
412 $subbing = 1;\r
413 next;\r
414 }\r
415 if ( $_ =~ "# Autogenerated files list ends here" ) {\r
416 push @new_inf, $_;\r
417 $subbing = 0;\r
418 next;\r
419 }\r
420\r
421 push @new_inf, $_\r
422 unless ($subbing);\r
264702a0
HW
423 }\r
424\r
878a92a8
CZ
425 $new_inf_file = $inf_file . ".new";\r
426 open( FD, ">" . $new_inf_file ) ||\r
427 die $new_inf_file;\r
428 print( FD @new_inf ) ||\r
429 die $new_inf_file;\r
430 close(FD) ||\r
431 die $new_inf_file;\r
432 rename( $new_inf_file, $inf_file ) ||\r
433 die "rename $inf_file";\r
434 print "Done!";\r
264702a0
HW
435}\r
436\r
264702a0 437#\r
1bcc65b9 438# Copy opensslconf.h and dso_conf.h generated from OpenSSL Configuration\r
264702a0
HW
439#\r
440print "\n--> Duplicating opensslconf.h into Include/openssl ... ";\r
8c30327d
GJ
441system(\r
442 "perl -pe 's/\\n/\\r\\n/' " .\r
443 "< " . $OPENSSL_PATH . "/include/openssl/opensslconf.h " .\r
499b0d5f 444 "> " . $OPENSSL_PATH . "/../../Include/openssl/opensslconf_generated.h"\r
8c30327d
GJ
445 ) == 0 ||\r
446 die "Cannot copy opensslconf.h!";\r
1bcc65b9 447print "Done!";\r
8c30327d
GJ
448\r
449print "\n--> Duplicating dso_conf.h into Include/crypto ... ";\r
450system(\r
451 "perl -pe 's/\\n/\\r\\n/' " .\r
452 "< " . $OPENSSL_PATH . "/include/crypto/dso_conf.h" .\r
453 "> " . $OPENSSL_PATH . "/../../Include/crypto/dso_conf.h"\r
454 ) == 0 ||\r
455 die "Cannot copy dso_conf.h!";\r
499b0d5f
YL
456print "Done!";\r
457\r
458#\r
459# Add conditional feature to opensslconf.h\r
460#\r
461my $conf_file = "../Include/openssl/opensslconf.h";\r
462my @conf_raw = ();\r
463my @conditional_define = ();\r
464print "\n--> Updating conditional feature in $conf_file ... ";\r
465\r
466foreach my $pcd_name (keys(%conditional_feature)) {\r
467 push @conditional_define, "#if !FixedPcdGetBool ($pcd_name)\r\n";\r
468 foreach (@{$conditional_feature{$pcd_name}}) {\r
469 push @conditional_define, "# ifndef OPENSSL_NO_$_\r\n";\r
470 push @conditional_define, "# define OPENSSL_NO_$_\r\n";\r
471 push @conditional_define, "# endif\r\n";\r
472 }\r
473 push @conditional_define, "#endif\r\n";\r
474}\r
475\r
476open( FD, "<" . $conf_file ) ||\r
477 die $conf_file;\r
478foreach (<FD>) {\r
479 # Insert conditional define to the begin of opensslconf.h\r
480 if ($_ =~ "Autogenerated conditional openssl feature list starts here") {\r
481 push @conf_raw, $_, @conditional_define;\r
482 $subbing = 1;\r
483 next;\r
484 }\r
485 if ($_ =~ "Autogenerated conditional openssl feature list ends here") {\r
486 push @conf_raw, $_;\r
487 $subbing = 0;\r
488 next;\r
489 }\r
490 push @conf_raw, $_\r
491 unless ($subbing);\r
492}\r
493close(FD) ||\r
494 die $conf_file;\r
495\r
496open( FD, ">" . $conf_file ) ||\r
497 die $conf_file;\r
498print( FD @conf_raw ) ||\r
499 die $conf_file;\r
500close(FD) ||\r
501 die $conf_file;\r
264702a0
HW
502print "Done!\n";\r
503\r
504print "\nProcessing Files Done!\n";\r
505\r
506exit(0);\r
507\r