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