Skip to content

Instantly share code, notes, and snippets.

@csmoe
Last active August 23, 2022 03:48
Show Gist options
  • Save csmoe/f6ba72a15fbc90f50381477975521522 to your computer and use it in GitHub Desktop.
Save csmoe/f6ba72a15fbc90f50381477975521522 to your computer and use it in GitHub Desktop.
Build sqlcipher on Windows(>=10) with PowerShell
# make sure you have visual studio build tools installed, cc https://visualstudio.microsoft.com/visual-cpp-build-tools/
# and prebuilt openssl for windows, cc https://wiki.openssl.org/index.php/Binaries
#
# Build instruction:
# $OPENSSL_PATH = <your_openssl_path>; powershell sqlcipher.ps1 windows x86 v4.5.2
param (
[Parameter(Mandatory)] [ValidateSet('windows')] $OS,
[Parameter(Mandatory)] [ValidateSet('x86', 'x64', 'x64_arm64')] $ARCH,
[Parameter(Mandatory)] $TAG
)
Write-Host "Build sqlcipher for $OS-$ARCH" -ForegroundColor Cyan
Set-ExecutionPolicy Bypass -Scope Process -Force
$ErrorActionPreference = "Stop"
$SQLCIPHER_PATH=$env:TEMP\sqlcipher
function Get-Sourcecode {
Write-Host "Clone sqlcipher" -ForegroundColor Cyan
try {
if (Test-Path $SQLCIPHER_PATH\.git\config -PathType Leaf) {
git -C $SQLCIPHER_PATH fetch origin $TAG
git -C $SQLCIPHER_PATH reset --hard $TAG
git -C $SQLCIPHER_PATH clean -fdx
} else {
Remove-Item -Force -Recurse $SQLCIPHER_PATH
mkdir -p $SQLCIPHER_PATH
git clone https://github.com/sqlcipher/sqlcipher -b $TAG --depth 1 $SQLCIPHER_PATH
}
} catch {
throw "Cannot fetch sqlcipher sourcecode"
}
}
function Build-Pre {
$env:Path += ";$env:TEMP\IronTcl\bin\"
pushd $env:TEMP
try {
if (-Not (Get-Command tclsh -errorAction SilentlyContinue))
{
Write-Host "Installing IronTcl..."
$tcl = "irontcl-amd64-8.6.7.zip"
Invoke-WebRequest https://www.irontcl.com/downloads/$tcl -OutFile $env:TEMP\$tcl
Expand-Archive $tcl -Force
New-Item -Path $env:TEMP\IronTcl\bin\tclsh.exe -ItemType SymbolicLink -Value $env:TEMP\IronTcl\bin\tclsh86t.exe
if (-Not (Get-Command tclsh -errorAction SilentlyContinue)) {
throw "Cannot install tcl"
}
}
} finally {
popd
}
Write-Host "Tcl installed" -ForegroundColor Green
try {
if (-Not (Get-Command nmake -errorAction SilentlyContinue))
{
Write-Host "Inject Visual Studio Command Prompt variables" -ForegroundColor Cyan
install-module vssetup -scope currentuser -force
$VSPATH=(Get-VSSetupInstance -all).InstallationPath
pushd "$VSPATH\VC\Auxiliary\Build"
cmd /c "vcvarsall.bat $ARCH & set" |
foreach {
if ($_ -match "=") {
$v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])"
}
}
popd
Write-Host "Visual Studio Command Prompt variables set." -ForegroundColor Yellow
}
} catch {
Write-Host $_
} finally {
popd
}
}
function Build {
Write-Host "Build sqlcipher" -ForegroundColor Cyan
pushd $SQLCIPHER_PATH
SET PLATFORM=$ARCH
try {
$OPTS = "-DSQLCIPHER_CRYPTO_OPENSSL -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2"
$OPTS = $OPTS + " -I`"$OPENSSL_PATH\include`""
$RCCOPTS=$OPTS
$LTLIBPATHS="$LTLIBPATH /LIBPATH:$OPENSSL_PATH\lib"
$LTLIBS="$LTLIBS crypto.lib advapi32.lib gdi32.lib user32.lib crypt32.lib ws2_32.lib"
nmake /f Makefile.msc `
OPTS="$OPTS" `
RCCOPTS="$RCCOPTS" `
LTLIBPATHS="$LTLIBPATHS" `
LTLIBS="$LTLIBS" `
SQLITE3DLL="sqlcipher.dll" `
SQLITE3LIB="sqlcipher.lib" `
SQLITE3EXE="sqlcipher.exe" `
SQLITE3EXEPDB="/pdb:sqlcipher.pdb"
if ($?) {
Write-Host "Build sqlcipher successfully" -ForegroundColor Green
} else {
Write-Error $_
}
} finally {
popd
}
}
Get-Sourcecode
Build-Pre
Build
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment