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