]> git.proxmox.com Git - mirror_edk2.git/blame - CryptoPkg/Library/OpensslLib/process_files.pl
Revert "CryptoPkg: Update process_files.pl to auto 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
HW
83my @inf;\r
84\r
85BEGIN {\r
86 $inf_file = "OpensslLib.inf";\r
878a92a8
CZ
87 $uefi_config = "UEFI";\r
88 $arch = shift;\r
89\r
90 if (defined $arch) {\r
91 if (uc ($arch) eq "X64") {\r
92 $arch = "X64";\r
93 $inf_file = "OpensslLibX64.inf";\r
94 $uefi_config = "UEFI-x86_64";\r
95 $extension = "nasm";\r
96 $comment_character = ";";\r
97 } elsif (uc ($arch) eq "X64GCC") {\r
98 $arch = "X64Gcc";\r
99 $inf_file = "OpensslLibX64Gcc.inf";\r
100 $uefi_config = "UEFI-x86_64-GCC";\r
101 $extension = "S";\r
102 $comment_character = "#";\r
03f70809
CZ
103 } elsif (uc ($arch) eq "IA32") {\r
104 $arch = "IA32";\r
105 $inf_file = "OpensslLibIa32.inf";\r
106 $uefi_config = "UEFI-x86";\r
107 $extension = "nasm";\r
108 $comment_character = ";";\r
109 } elsif (uc ($arch) eq "IA32GCC") {\r
110 $arch = "IA32Gcc";\r
111 $inf_file = "OpensslLibIa32Gcc.inf";\r
112 $uefi_config = "UEFI-x86-GCC";\r
113 $extension = "S";\r
114 $comment_character = "#";\r
878a92a8
CZ
115 } else {\r
116 die "Unsupported architecture \"" . $arch . "\"!";\r
117 }\r
118 if ($extension eq "nasm") {\r
119 if (`nasm -v 2>&1`) {\r
120 #Presence of nasm executable will trigger inclusion of AVX instructions\r
121 die "\nCannot run assembly generators with NASM in path!\n\n";\r
122 }\r
123 }\r
124\r
125 # Prepare assembly folder\r
126 if (-d $arch) {\r
127 opendir my $dir, $arch ||\r
128 die "Cannot open assembly folder \"" . $arch . "\"!";\r
129 while (defined (my $file = readdir $dir)) {\r
130 if (-d "$arch/$file") {\r
131 next if $file eq ".";\r
132 next if $file eq "..";\r
133 remove_tree ("$arch/$file", {safe => 1}) ||\r
134 die "Cannot clean assembly folder \"" . "$arch/$file" . "\"!";\r
135 }\r
136 }\r
137\r
138 } else {\r
139 mkdir $arch ||\r
140 die "Cannot create assembly folder \"" . $arch . "\"!";\r
141 }\r
142 }\r
264702a0
HW
143\r
144 # Read the contents of the inf file\r
145 open( FD, "<" . $inf_file ) ||\r
146 die "Cannot open \"" . $inf_file . "\"!";\r
147 @inf = (<FD>);\r
148 close(FD) ||\r
149 die "Cannot close \"" . $inf_file . "\"!";\r
150\r
151 foreach (@inf) {\r
152 if (/DEFINE\s+OPENSSL_PATH\s*=\s*([a-z]+)/) {\r
153\r
154 # We need to run Configure before we can include its result...\r
155 $OPENSSL_PATH = $1;\r
156\r
157 my $basedir = getcwd();\r
158\r
159 chdir($OPENSSL_PATH) ||\r
160 die "Cannot change to OpenSSL directory \"" . $OPENSSL_PATH . "\"";\r
161\r
162 # Configure UEFI\r
163 system(\r
164 "./Configure",\r
878a92a8
CZ
165 "--config=../UefiAsm.conf",\r
166 "$uefi_config",\r
264702a0 167 "no-afalgeng",\r
264702a0 168 "no-async",\r
264702a0 169 "no-autoerrinit",\r
c72ca466 170 "no-autoload-config",\r
264702a0
HW
171 "no-bf",\r
172 "no-blake2",\r
173 "no-camellia",\r
174 "no-capieng",\r
175 "no-cast",\r
176 "no-chacha",\r
177 "no-cms",\r
178 "no-ct",\r
179 "no-deprecated",\r
394d5896 180 "no-des",\r
264702a0
HW
181 "no-dgram",\r
182 "no-dsa",\r
183 "no-dynamic-engine",\r
264702a0
HW
184 "no-ec2m",\r
185 "no-engine",\r
186 "no-err",\r
187 "no-filenames",\r
188 "no-gost",\r
189 "no-hw",\r
190 "no-idea",\r
9b2a082e 191 "no-md4",\r
264702a0
HW
192 "no-mdc2",\r
193 "no-pic",\r
194 "no-ocb",\r
195 "no-poly1305",\r
196 "no-posix-io",\r
197 "no-rc2",\r
f4c15d38 198 "no-rc4",\r
264702a0
HW
199 "no-rfc3779",\r
200 "no-rmd160",\r
201 "no-scrypt",\r
202 "no-seed",\r
203 "no-sock",\r
204 "no-srp",\r
205 "no-ssl",\r
206 "no-stdio",\r
207 "no-threads",\r
208 "no-ts",\r
209 "no-ui",\r
6fcc3d68
XL
210 "no-whirlpool",\r
211 # OpenSSL1_1_1b doesn't support default rand-seed-os for UEFI\r
212 # UEFI only support --with-rand-seed=none\r
213 "--with-rand-seed=none"\r
264702a0
HW
214 ) == 0 ||\r
215 die "OpenSSL Configure failed!\n";\r
216\r
217 # Generate opensslconf.h per config data\r
218 system(\r
219 "perl -I. -Mconfigdata util/dofile.pl " .\r
220 "include/openssl/opensslconf.h.in " .\r
221 "> include/openssl/opensslconf.h"\r
222 ) == 0 ||\r
223 die "Failed to generate opensslconf.h!\n";\r
224\r
1bcc65b9
SZ
225 # Generate dso_conf.h per config data\r
226 system(\r
227 "perl -I. -Mconfigdata util/dofile.pl " .\r
8c30327d
GJ
228 "include/crypto/dso_conf.h.in " .\r
229 "> include/crypto/dso_conf.h"\r
1bcc65b9
SZ
230 ) == 0 ||\r
231 die "Failed to generate dso_conf.h!\n";\r
232\r
264702a0
HW
233 chdir($basedir) ||\r
234 die "Cannot change to base directory \"" . $basedir . "\"";\r
235\r
236 push @INC, $1;\r
237 last;\r
238 }\r
239 }\r
240}\r
241\r
242#\r
243# Retrieve file lists from OpenSSL configdata\r
244#\r
245use configdata qw/%unified_info/;\r
878a92a8
CZ
246use configdata qw/%config/;\r
247use configdata qw/%target/;\r
248\r
249#\r
250# Collect build flags from configdata\r
251#\r
252my $flags = "";\r
253foreach my $f (@{$config{lib_defines}}) {\r
254 $flags .= " -D$f";\r
255}\r
264702a0
HW
256\r
257my @cryptofilelist = ();\r
258my @sslfilelist = ();\r
878a92a8
CZ
259my @asmfilelist = ();\r
260my @asmbuild = ();\r
264702a0
HW
261foreach my $product ((@{$unified_info{libraries}},\r
262 @{$unified_info{engines}})) {\r
263 foreach my $o (@{$unified_info{sources}->{$product}}) {\r
264 foreach my $s (@{$unified_info{sources}->{$o}}) {\r
7eee0488
XL
265 # No need to add unused files in UEFI.\r
266 # So it can reduce porting time, compile time, library size.\r
878a92a8 267 next if $s =~ "crypto/bio/b_print.c";\r
7eee0488
XL
268 next if $s =~ "crypto/rand/randfile.c";\r
269 next if $s =~ "crypto/store/";\r
51f7a3e6 270 next if $s =~ "crypto/err/err_all.c";\r
89db28b9 271 next if $s =~ "crypto/aes/aes_ecb.c";\r
7eee0488 272\r
878a92a8
CZ
273 if ($unified_info{generate}->{$s}) {\r
274 if (defined $arch) {\r
275 my $buildstring = "perl";\r
276 foreach my $arg (@{$unified_info{generate}->{$s}}) {\r
277 if ($arg =~ ".pl") {\r
278 $buildstring .= " ./openssl/$arg";\r
279 } elsif ($arg =~ "PERLASM_SCHEME") {\r
280 $buildstring .= " $target{perlasm_scheme}";\r
281 } elsif ($arg =~ "LIB_CFLAGS") {\r
282 $buildstring .= "$flags";\r
283 }\r
284 }\r
285 ($s, my $path, undef) = fileparse($s, qr/\.[^.]*/);\r
286 $buildstring .= " ./$arch/$path$s.$extension";\r
287 make_path ("./$arch/$path");\r
288 push @asmbuild, "$buildstring\n";\r
289 push @asmfilelist, " $arch/$path$s.$extension\r\n";\r
290 }\r
291 next;\r
292 }\r
264702a0
HW
293 if ($product =~ "libssl") {\r
294 push @sslfilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";\r
295 next;\r
296 }\r
3b46a1e2 297 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";\r
264702a0
HW
298 }\r
299 }\r
300}\r
301\r
9f4fbd56
SZ
302\r
303#\r
304# Update the perl script to generate the missing header files\r
305#\r
306my @dir_list = ();\r
c72ca466 307for (sort keys %{$unified_info{dirinfo}}){\r
9f4fbd56
SZ
308 push @dir_list,$_;\r
309}\r
310\r
311my $dir = getcwd();\r
312my @files = ();\r
313my @headers = ();\r
314chdir ("openssl");\r
315foreach(@dir_list){\r
316 @files = glob($_."/*.h");\r
317 push @headers, @files;\r
318}\r
319chdir ($dir);\r
320\r
321foreach (@headers){\r
322 if(/ssl/){\r
323 push @sslfilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";\r
324 next;\r
325 }\r
3b46a1e2 326 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";\r
9f4fbd56
SZ
327}\r
328\r
329\r
878a92a8
CZ
330#\r
331# Generate assembly files\r
332#\r
333if (@asmbuild) {\r
334 print "\n--> Generating assembly files ... ";\r
335 foreach my $buildstring (@asmbuild) {\r
336 system ("$buildstring");\r
337 copy_license_header ($buildstring);\r
338 }\r
339 print "Done!";\r
340}\r
341\r
264702a0
HW
342#\r
343# Update OpensslLib.inf with autogenerated file list\r
344#\r
345my @new_inf = ();\r
346my $subbing = 0;\r
878a92a8 347print "\n--> Updating $inf_file ... ";\r
264702a0 348foreach (@inf) {\r
878a92a8
CZ
349 if ($_ =~ "DEFINE OPENSSL_FLAGS_CONFIG") {\r
350 push @new_inf, " DEFINE OPENSSL_FLAGS_CONFIG =" . $flags . "\r\n";\r
351 next;\r
352 }\r
264702a0 353 if ( $_ =~ "# Autogenerated files list starts here" ) {\r
878a92a8 354 push @new_inf, $_, @asmfilelist, @cryptofilelist, @sslfilelist;\r
264702a0
HW
355 $subbing = 1;\r
356 next;\r
357 }\r
358 if ( $_ =~ "# Autogenerated files list ends here" ) {\r
359 push @new_inf, $_;\r
360 $subbing = 0;\r
361 next;\r
362 }\r
363\r
364 push @new_inf, $_\r
365 unless ($subbing);\r
366}\r
367\r
368my $new_inf_file = $inf_file . ".new";\r
369open( FD, ">" . $new_inf_file ) ||\r
370 die $new_inf_file;\r
371print( FD @new_inf ) ||\r
372 die $new_inf_file;\r
373close(FD) ||\r
374 die $new_inf_file;\r
375rename( $new_inf_file, $inf_file ) ||\r
376 die "rename $inf_file";\r
377print "Done!";\r
378\r
878a92a8
CZ
379if (!defined $arch) {\r
380 #\r
381 # Update OpensslLibCrypto.inf with auto-generated file list (no libssl)\r
382 #\r
383 $inf_file = "OpensslLibCrypto.inf";\r
264702a0 384\r
878a92a8
CZ
385 # Read the contents of the inf file\r
386 @inf = ();\r
387 @new_inf = ();\r
388 open( FD, "<" . $inf_file ) ||\r
389 die "Cannot open \"" . $inf_file . "\"!";\r
390 @inf = (<FD>);\r
391 close(FD) ||\r
392 die "Cannot close \"" . $inf_file . "\"!";\r
393\r
394 $subbing = 0;\r
395 print "\n--> Updating OpensslLibCrypto.inf ... ";\r
396 foreach (@inf) {\r
397 if ( $_ =~ "# Autogenerated files list starts here" ) {\r
398 push @new_inf, $_, @cryptofilelist;\r
399 $subbing = 1;\r
400 next;\r
401 }\r
402 if ( $_ =~ "# Autogenerated files list ends here" ) {\r
403 push @new_inf, $_;\r
404 $subbing = 0;\r
405 next;\r
406 }\r
407\r
408 push @new_inf, $_\r
409 unless ($subbing);\r
264702a0
HW
410 }\r
411\r
878a92a8
CZ
412 $new_inf_file = $inf_file . ".new";\r
413 open( FD, ">" . $new_inf_file ) ||\r
414 die $new_inf_file;\r
415 print( FD @new_inf ) ||\r
416 die $new_inf_file;\r
417 close(FD) ||\r
418 die $new_inf_file;\r
419 rename( $new_inf_file, $inf_file ) ||\r
420 die "rename $inf_file";\r
421 print "Done!";\r
264702a0
HW
422}\r
423\r
264702a0 424#\r
1bcc65b9 425# Copy opensslconf.h and dso_conf.h generated from OpenSSL Configuration\r
264702a0
HW
426#\r
427print "\n--> Duplicating opensslconf.h into Include/openssl ... ";\r
8c30327d
GJ
428system(\r
429 "perl -pe 's/\\n/\\r\\n/' " .\r
430 "< " . $OPENSSL_PATH . "/include/openssl/opensslconf.h " .\r
3b46a1e2 431 "> " . $OPENSSL_PATH . "/../../Include/openssl/opensslconf.h"\r
8c30327d
GJ
432 ) == 0 ||\r
433 die "Cannot copy opensslconf.h!";\r
1bcc65b9 434print "Done!";\r
8c30327d
GJ
435\r
436print "\n--> Duplicating dso_conf.h into Include/crypto ... ";\r
437system(\r
438 "perl -pe 's/\\n/\\r\\n/' " .\r
439 "< " . $OPENSSL_PATH . "/include/crypto/dso_conf.h" .\r
440 "> " . $OPENSSL_PATH . "/../../Include/crypto/dso_conf.h"\r
441 ) == 0 ||\r
442 die "Cannot copy dso_conf.h!";\r
264702a0
HW
443print "Done!\n";\r
444\r
445print "\nProcessing Files Done!\n";\r
446\r
447exit(0);\r
448\r