Last active
December 14, 2015 13:48
-
-
Save jonforums/5095788 to your computer and use it in GitHub Desktop.
Automate building zlib on Windows with mingw/mingw-w64
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -version 2.0 | |
# Author: Jon Maken | |
# License: 3-clause BSD | |
# Revision: 2013-03-11 01:39:59 -0600 | |
# | |
# TODO: | |
# - extract generics into a downloadable utils helper module | |
# - add proper try-catch-finally error handling | |
# - support x86 and x64 builds | |
param( | |
[parameter(Mandatory=$true, | |
Position=0, | |
HelpMessage='zlib version to build (eg - 1.2.7)')] | |
[alias('v')] | |
[validateset('1.2.7')] | |
[string] $version, | |
[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' | |
) | |
$msg_color = 'Yellow' | |
$root = Split-Path -parent $script:MyInvocation.MyCommand.Path | |
$libname = 'zlib' | |
$source = "${libname}-${version}.tar.gz" | |
$source_dir = "${libname}-${version}" | |
$repo_root = 'http://zlib.net/' | |
$archive = "${repo_root}${source}" | |
$hash_uri = "https://raw.github.com/jonforums/poshlab/master/hashery/${libname}.md5" | |
# 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 downloaded source archive | |
Write-Host "[INFO] validating $source" -foregroundcolor $msg_color | |
$client = New-Object System.Net.WebClient | |
$hash = ConvertFrom-StringData $client.DownloadString($hash_uri) | |
try { | |
$hasher = New-Object System.Security.Cryptography.MD5Cng | |
$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[$version].ToLower()) { | |
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('-')))*.tar" | |
(& "$7ZA" "x" $source) -and (& "$7ZA" "x" $tar_file) -and (rm $tar_file) | Out-Null | |
# patch, configure, build, archive | |
Push-Location "${source_dir}" | |
# activate toolchain | |
Write-Host "[INFO] activating toolchain" -foregroundcolor $msg_color | |
. "$DEVKIT/devkitvars.ps1" | Out-Null | |
# configure | |
Write-Host "[INFO] configuring ${source_dir}" -foregroundcolor $msg_color | |
$install_dir = "$($PWD.ToString().Replace('\','/'))/my_install" | |
# build | |
Write-Host "[INFO] building ${source_dir}" -foregroundcolor $msg_color | |
sh -c "make -f win32/Makefile.gcc" | Out-Null | |
# install | |
$install_opts = @("BINARY_PATH=${install_dir}/bin", | |
"INCLUDE_PATH=${install_dir}/include", | |
"LIBRARY_PATH=${install_dir}/lib", | |
"SHARED_MODE=1") | |
sh -c "make install -f win32/Makefile.gcc $(${install_opts} -join ' ')" | 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