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