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