Skip to content

Instantly share code, notes, and snippets.

@jonforums
Last active July 7, 2016 12:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonforums/4771531 to your computer and use it in GitHub Desktop.
Save jonforums/4771531 to your computer and use it in GitHub Desktop.
Automate building OpenSSL on Windows with mingw/mingw-w64
#requires -version 2.0
# Author: Jon Maken
# License: 3-clause BSD
# Revision: 2013-03-11 01:24:20 -0600
#
# TODO:
# - extract generics into a downloadable utils helper module
# - add try-catch-finally error handling
# - add checkpoint support
# - support x86 and x64 builds
param(
[parameter(Mandatory=$true,
Position=0,
HelpMessage='OpenSSL version to build (eg - 1.0.1e).')]
[alias('v')]
[validateset('1.0.0k','1.0.1e')]
[string] $version,
[parameter(HelpMessage='mingw toolchain flavor to use (eg - mingw, mingw64)')]
[validateset('mingw','mingw64')]
[string] $toolchain = 'mingw',
[parameter(HelpMessage='Path to 7-Zip command line tool')]
[string] $7ZA = 'C:/tools/7za.exe',
[parameter(HelpMessage='Path to DevKit root directory')]
[string] $DEVKIT = 'C:/Devkit',
[parameter(HelpMessage='Path to zlib dev libraries root directory')]
[alias('with-zlib-dir')]
[string] $ZLIBDIR = 'C:/devlibs/zlib-1.2.7'
)
$msg_color = 'Yellow'
$root = Split-Path -parent $script:MyInvocation.MyCommand.Path
$libname = 'openssl'
$source = "${libname}-${version}.tar.gz"
$source_dir = "${libname}-${version}"
$repo_root = 'http://www.openssl.org/source/'
$archive = "${repo_root}${source}"
$archive_hash = "${repo_root}${source}.sha1"
# download and verify
# TODO implement progress bar when extracted to util helper module
if(-not (Test-Path $source)) {
Import-Module BitsTransfer
Write-Host "[INFO] downloading $archive" -foregroundcolor $msg_color
Start-BitsTransfer $archive "$PWD\$source"
}
# download hash data and validate source archive
Write-Host "[INFO] validating $source" -foregroundcolor $msg_color
$client = New-Object System.Net.WebClient
$hash = $client.DownloadString($archive_hash).Trim(" ","`r","`n").ToLower()
try {
$hasher = New-Object System.Security.Cryptography.SHA1Cng
$fs = New-Object System.IO.FileStream "$PWD\$source", 'Open', 'Read'
$test_hash = [BitConverter]::ToString($hasher.ComputeHash($fs)).Replace('-','').ToLower()
} finally {
$fs.Close()
}
if ($test_hash -ne $hash) {
Write-Host "[ERROR] $source validation failed, exiting" -foregroundcolor red
break
}
# extract
Write-Host "[INFO] extracting $source" -foregroundcolor $msg_color
$tar_file = $source.Substring(0, $source.LastIndexOf('.'))
(& "$7ZA" "x" $source) -and (& "$7ZA" "x" $tar_file) -and (rm $tar_file) | Out-Null
# patch, configure, build, archive
Push-Location "${source_dir}"
# patch
Write-Host "[INFO] patching ${source_dir}" -foregroundcolor $msg_color
Push-Location test
rm md2test.c,rc5test.c,jpaketest.c
Pop-Location
# activate toolchain
Write-Host "[INFO] activating toolchain" -foregroundcolor $msg_color
. "$DEVKIT/devkitvars.ps1" | Out-Null
$env:CPATH = "$ZLIBDIR/include"
# configure
Write-Host "[INFO] configuring ${source_dir}" -foregroundcolor $msg_color
$install_dir = "$($PWD.ToString().Replace('\','/'))/my_install"
perl Configure $toolchain zlib-dynamic shared --prefix="$install_dir" | Out-Null
# build
Write-Host "[INFO] building ${source_dir}" -foregroundcolor $msg_color
sh -c "make" | Out-Null
# install
sh -c "make install_sw" | Out-Null
# archive
Push-Location "$install_dir"
Write-Host "[INFO] creating binary archive for ${source_dir}" -foregroundcolor $msg_color
$bin_archive = "${source_dir}-x86-windows-bin.7z"
& "$7ZA" "a" "-mx=9" "-r" $bin_archive "*" | Out-Null
Pop-Location
Pop-Location
# hoist binary archive and cleanup
Write-Host "[INFO] cleaning up" -foregroundcolor $msg_color
mv "$install_dir/$bin_archive" "$PWD"
rm -recurse -force "${source_dir}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment