]> git.proxmox.com Git - mirror_edk2.git/blob - CryptoPkg/Library/OpensslLib/process_files.pl
2ebfbbbca0de142a4021841285e8e2af5f7f270d
[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-ec2m",
173 "no-engine",
174 "no-err",
175 "no-filenames",
176 "no-gost",
177 "no-hw",
178 "no-idea",
179 "no-md4",
180 "no-mdc2",
181 "no-pic",
182 "no-ocb",
183 "no-poly1305",
184 "no-posix-io",
185 "no-rc2",
186 "no-rc4",
187 "no-rfc3779",
188 "no-rmd160",
189 "no-scrypt",
190 "no-seed",
191 "no-sock",
192 "no-srp",
193 "no-ssl",
194 "no-stdio",
195 "no-threads",
196 "no-ts",
197 "no-ui",
198 "no-whirlpool",
199 # OpenSSL1_1_1b doesn't support default rand-seed-os for UEFI
200 # UEFI only support --with-rand-seed=none
201 "--with-rand-seed=none"
202 ) == 0 ||
203 die "OpenSSL Configure failed!\n";
204
205 # Generate opensslconf.h per config data
206 system(
207 "perl -I. -Mconfigdata util/dofile.pl " .
208 "include/openssl/opensslconf.h.in " .
209 "> include/openssl/opensslconf.h"
210 ) == 0 ||
211 die "Failed to generate opensslconf.h!\n";
212
213 # Generate dso_conf.h per config data
214 system(
215 "perl -I. -Mconfigdata util/dofile.pl " .
216 "include/crypto/dso_conf.h.in " .
217 "> include/crypto/dso_conf.h"
218 ) == 0 ||
219 die "Failed to generate dso_conf.h!\n";
220
221 chdir($basedir) ||
222 die "Cannot change to base directory \"" . $basedir . "\"";
223
224 push @INC, $1;
225 last;
226 }
227 }
228 }
229
230 #
231 # Retrieve file lists from OpenSSL configdata
232 #
233 use configdata qw/%unified_info/;
234 use configdata qw/%config/;
235 use configdata qw/%target/;
236
237 #
238 # Collect build flags from configdata
239 #
240 my $flags = "";
241 foreach my $f (@{$config{lib_defines}}) {
242 $flags .= " -D$f";
243 }
244
245 my @cryptofilelist = ();
246 my @sslfilelist = ();
247 my @asmfilelist = ();
248 my @asmbuild = ();
249 foreach my $product ((@{$unified_info{libraries}},
250 @{$unified_info{engines}})) {
251 foreach my $o (@{$unified_info{sources}->{$product}}) {
252 foreach my $s (@{$unified_info{sources}->{$o}}) {
253 # No need to add unused files in UEFI.
254 # So it can reduce porting time, compile time, library size.
255 next if $s =~ "crypto/bio/b_print.c";
256 next if $s =~ "crypto/rand/randfile.c";
257 next if $s =~ "crypto/store/";
258 next if $s =~ "crypto/err/err_all.c";
259 next if $s =~ "crypto/aes/aes_ecb.c";
260
261 if ($unified_info{generate}->{$s}) {
262 if (defined $arch) {
263 my $buildstring = "perl";
264 foreach my $arg (@{$unified_info{generate}->{$s}}) {
265 if ($arg =~ ".pl") {
266 $buildstring .= " ./openssl/$arg";
267 } elsif ($arg =~ "PERLASM_SCHEME") {
268 $buildstring .= " $target{perlasm_scheme}";
269 } elsif ($arg =~ "LIB_CFLAGS") {
270 $buildstring .= "$flags";
271 }
272 }
273 ($s, my $path, undef) = fileparse($s, qr/\.[^.]*/);
274 $buildstring .= " ./$arch/$path$s.$extension";
275 make_path ("./$arch/$path");
276 push @asmbuild, "$buildstring\n";
277 push @asmfilelist, " $arch/$path$s.$extension\r\n";
278 }
279 next;
280 }
281 if ($product =~ "libssl") {
282 push @sslfilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";
283 next;
284 }
285 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $s . "\r\n";
286 }
287 }
288 }
289
290
291 #
292 # Update the perl script to generate the missing header files
293 #
294 my @dir_list = ();
295 for (sort keys %{$unified_info{dirinfo}}){
296 push @dir_list,$_;
297 }
298
299 my $dir = getcwd();
300 my @files = ();
301 my @headers = ();
302 chdir ("openssl");
303 foreach(@dir_list){
304 @files = glob($_."/*.h");
305 push @headers, @files;
306 }
307 chdir ($dir);
308
309 foreach (@headers){
310 if(/ssl/){
311 push @sslfilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";
312 next;
313 }
314 push @cryptofilelist, ' $(OPENSSL_PATH)/' . $_ . "\r\n";
315 }
316
317
318 #
319 # Generate assembly files
320 #
321 if (@asmbuild) {
322 print "\n--> Generating assembly files ... ";
323 foreach my $buildstring (@asmbuild) {
324 system ("$buildstring");
325 copy_license_header ($buildstring);
326 }
327 print "Done!";
328 }
329
330 #
331 # Update OpensslLib.inf with autogenerated file list
332 #
333 my @new_inf = ();
334 my $subbing = 0;
335 print "\n--> Updating $inf_file ... ";
336 foreach (@inf) {
337 if ($_ =~ "DEFINE OPENSSL_FLAGS_CONFIG") {
338 push @new_inf, " DEFINE OPENSSL_FLAGS_CONFIG =" . $flags . "\r\n";
339 next;
340 }
341 if ( $_ =~ "# Autogenerated files list starts here" ) {
342 push @new_inf, $_, @asmfilelist, @cryptofilelist, @sslfilelist;
343 $subbing = 1;
344 next;
345 }
346 if ( $_ =~ "# Autogenerated files list ends here" ) {
347 push @new_inf, $_;
348 $subbing = 0;
349 next;
350 }
351
352 push @new_inf, $_
353 unless ($subbing);
354 }
355
356 my $new_inf_file = $inf_file . ".new";
357 open( FD, ">" . $new_inf_file ) ||
358 die $new_inf_file;
359 print( FD @new_inf ) ||
360 die $new_inf_file;
361 close(FD) ||
362 die $new_inf_file;
363 rename( $new_inf_file, $inf_file ) ||
364 die "rename $inf_file";
365 print "Done!";
366
367 if (!defined $arch) {
368 #
369 # Update OpensslLibCrypto.inf with auto-generated file list (no libssl)
370 #
371 $inf_file = "OpensslLibCrypto.inf";
372
373 # Read the contents of the inf file
374 @inf = ();
375 @new_inf = ();
376 open( FD, "<" . $inf_file ) ||
377 die "Cannot open \"" . $inf_file . "\"!";
378 @inf = (<FD>);
379 close(FD) ||
380 die "Cannot close \"" . $inf_file . "\"!";
381
382 $subbing = 0;
383 print "\n--> Updating OpensslLibCrypto.inf ... ";
384 foreach (@inf) {
385 if ( $_ =~ "# Autogenerated files list starts here" ) {
386 push @new_inf, $_, @cryptofilelist;
387 $subbing = 1;
388 next;
389 }
390 if ( $_ =~ "# Autogenerated files list ends here" ) {
391 push @new_inf, $_;
392 $subbing = 0;
393 next;
394 }
395
396 push @new_inf, $_
397 unless ($subbing);
398 }
399
400 $new_inf_file = $inf_file . ".new";
401 open( FD, ">" . $new_inf_file ) ||
402 die $new_inf_file;
403 print( FD @new_inf ) ||
404 die $new_inf_file;
405 close(FD) ||
406 die $new_inf_file;
407 rename( $new_inf_file, $inf_file ) ||
408 die "rename $inf_file";
409 print "Done!";
410 }
411
412 #
413 # Copy opensslconf.h and dso_conf.h generated from OpenSSL Configuration
414 #
415 print "\n--> Duplicating opensslconf.h into Include/openssl ... ";
416 system(
417 "perl -pe 's/\\n/\\r\\n/' " .
418 "< " . $OPENSSL_PATH . "/include/openssl/opensslconf.h " .
419 "> " . $OPENSSL_PATH . "/../../Include/openssl/opensslconf.h"
420 ) == 0 ||
421 die "Cannot copy opensslconf.h!";
422 print "Done!";
423
424 print "\n--> Duplicating dso_conf.h into Include/crypto ... ";
425 system(
426 "perl -pe 's/\\n/\\r\\n/' " .
427 "< " . $OPENSSL_PATH . "/include/crypto/dso_conf.h" .
428 "> " . $OPENSSL_PATH . "/../../Include/crypto/dso_conf.h"
429 ) == 0 ||
430 die "Cannot copy dso_conf.h!";
431 print "Done!\n";
432
433 print "\nProcessing Files Done!\n";
434
435 exit(0);
436