Skip to content

Instantly share code, notes, and snippets.

@jay
Last active June 4, 2020 07:33
Show Gist options
  • Save jay/a1861b50ecce2b32931237180f856e28 to your computer and use it in GitHub Desktop.
Save jay/a1861b50ecce2b32931237180f856e28 to your computer and use it in GitHub Desktop.
Add crypt32.lib to AdditionalDependencies in curl project templates
#!/usr/bin/env perl
=begin comment
README
./add-crypt32.pl
This script was written to fix Windows project file templates in the curl repo.
It adds crypt32.lib to any additional dependency list that contains
ssleay32.lib (OpenSSL), if it is not there already. crypt32 is now usually
required as a dependency for OpenSSL builds on Windows.
Refer to https://github.com/curl/curl/pull/5516
Copyright (C) 2020 Jay Satiro <[email protected]>
http://curl.haxx.se/docs/copyright.html
https://gist.github.com/jay/a1861b50ecce2b32931237180f856e28
=end comment
=cut
use warnings;
use strict;
my $lib = "ssleay32.lib"; # lib to find in dependency list
my $add = "crypt32.lib"; # lib to add to list if $lib is found
sub modify(@) {
local (*IN, *OUT);
my ($version, $infile, $outfile) = @_;
open(IN, "<:raw", $infile) or return "Can't open $infile : $!";
open(OUT, ">:raw", $outfile) or return "Can't open $outfile : $!";
while(!eof(IN)) {
defined(my $line = readline(IN)) or return "Can't read $infile : $!";
if(index($line, $add) == -1) {
if($version == 6) {
$line =~ s/(^# ADD (?:BASE )?LINK32 +)(.*\Q$lib\E)/$1$add $2/s;
}
elsif($version >= 7 && $version <= 9) {
$line =~ s/(^\s*AdditionalDependencies=\"\s*)(.*\Q$lib\E)/$1$add $2/s;
}
elsif($version >= 10) {
$line =~ s/(^\s*<AdditionalDependencies>\s*)(.*\Q$lib\E)/$1$add;$2/s;
}
}
print OUT $line or return "Can't write $outfile : $!";
}
close(IN) or return "Can't close $infile : $!";
close(OUT) or return "Can't close $outfile : $!";
return undef;
}
# This is the max times to retry modifying each file.
# Modification fails if Microsoft dev git detects file changes and opens
# the file at the same time as this script is attempting to modify.
my $max_retries = 10;
for my $version (6, 7, 7.1, 8, 9, 10, 11, 12, 14, 15) {
for my $template ("src/curl.tmpl", "lib/libcurl.tmpl") {
my $infile = "curl/projects/Windows/VC$version/$template";
my $outfile = "$infile.tmp";
for(my $i = 0; $i < $max_retries; sleep($i++)) {
my $errmsg = modify($version, $infile, $outfile);
last if !$errmsg;
die "FATAL: $errmsg" if $i+1 == $max_retries;
print STDERR "[Retrying in ${i}s] $errmsg\n";
}
for(my $i = 0; $i < $max_retries; sleep($i++)) {
my $result = rename($outfile, $infile);
last if $result;
my $errmsg = "Can't rename $outfile => $infile: $!";
die "FATAL: $errmsg" if $i+1 == $max_retries;
print STDERR "[Retrying in ${i}s] $errmsg\n";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment