|
| 1 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 | +# you may not use this file except in compliance with the License. |
| 3 | +# You may obtain a copy of the License at |
| 4 | +# |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# |
| 7 | +# Unless required by applicable law or agreed to in writing, software |
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +# See the License for the specific language governing permissions and |
| 11 | +# limitations under the License. |
| 12 | + |
| 13 | +$ErrorActionPreference = "Stop" |
| 14 | +function SetVCVars($version="12.0", $platform="x86_amd64") |
| 15 | +{ |
| 16 | + if($version -eq "15.0") { |
| 17 | + pushd "$ENV:ProgramFiles (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\" |
| 18 | + } else { |
| 19 | + pushd "$ENV:ProgramFiles (x86)\Microsoft Visual Studio $version\VC\" |
| 20 | + } |
| 21 | + |
| 22 | + try |
| 23 | + { |
| 24 | + cmd /c "vcvarsall.bat $platform & set" | |
| 25 | + foreach { |
| 26 | + if ($_ -match "=") { |
| 27 | + $v = $_.split("="); set-item -force -path "ENV:\$($v[0])" -value "$($v[1])" |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + finally |
| 32 | + { |
| 33 | + popd |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +function CheckFileHash($path, $hash, $algorithm="SHA1") { |
| 38 | + $h = Get-Filehash -Algorithm $algorithm $path |
| 39 | + if ($h.Hash.ToUpper() -ne $hash.ToUpper()) { |
| 40 | + throw "Hash comparison failed for file: $path" |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +function Expand7z($archive, $outputDir = ".") |
| 45 | +{ |
| 46 | + pushd . |
| 47 | + try |
| 48 | + { |
| 49 | + cd $outputDir |
| 50 | + &7z.exe x -y $archive |
| 51 | + if ($LastExitCode) { throw "7z.exe failed on archive: $archive"} |
| 52 | + } |
| 53 | + finally |
| 54 | + { |
| 55 | + popd |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +function CheckDir($path) |
| 60 | +{ |
| 61 | + if (!(Test-Path -path $path)) |
| 62 | + { |
| 63 | + mkdir $path |
| 64 | + } |
| 65 | +} |
| 66 | +function BuildOpenSSL($buildDir, $outputPath, $opensslVersion, $platform, $cmakeGenerator, $platformToolset, |
| 67 | + $dllBuild=$true, $runTests=$true, $hash=$null) |
| 68 | +{ |
| 69 | + $opensslBase = "openssl-$opensslVersion" |
| 70 | + $opensslPath = "$ENV:Temp\$opensslBase.tar.gz" |
| 71 | + $opensslUrl = "https://www.openssl.org/source/$opensslBase.tar.gz" |
| 72 | + |
| 73 | + pushd . |
| 74 | + try |
| 75 | + { |
| 76 | + cd $buildDir |
| 77 | + |
| 78 | + # Needed by the OpenSSL server |
| 79 | + [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 |
| 80 | + (new-object System.Net.WebClient).DownloadFile($opensslUrl, $opensslPath) |
| 81 | + |
| 82 | + if($hash) { CheckFileHash $opensslPath $hash } |
| 83 | + |
| 84 | + Expand7z $opensslPath |
| 85 | + del $opensslPath |
| 86 | + Expand7z "$opensslBase.tar" |
| 87 | + del "$opensslBase.tar" |
| 88 | + |
| 89 | + cd $opensslBase |
| 90 | + &cmake . -G $cmakeGenerator -T $platformToolset |
| 91 | + |
| 92 | + $platformMap = @{"x86"="VC-WIN32"; "amd64"="VC-WIN64A"; "x86_amd64"="VC-WIN64A"} |
| 93 | + &perl Configure $platformMap[$platform] --prefix="$ENV:OPENSSL_ROOT_DIR" |
| 94 | + if ($LastExitCode) { throw "perl failed" } |
| 95 | + |
| 96 | + if($platform -eq "amd64" -or $platform -eq "x86_amd64") |
| 97 | + { |
| 98 | + &.\ms\do_win64a |
| 99 | + if ($LastExitCode) { throw "do_win64 failed" } |
| 100 | + } |
| 101 | + elseif($platform -eq "x86") |
| 102 | + { |
| 103 | + &.\ms\do_nasm |
| 104 | + if ($LastExitCode) { throw "do_nasm failed" } |
| 105 | + } |
| 106 | + else |
| 107 | + { |
| 108 | + throw "Invalid platform: $platform" |
| 109 | + } |
| 110 | + |
| 111 | + if($dllBuild) |
| 112 | + { |
| 113 | + $makFile = "ms\ntdll.mak" |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + $makFile = "ms\nt.mak" |
| 118 | + } |
| 119 | + |
| 120 | + &nmake -f $makFile |
| 121 | + if ($LastExitCode) { throw "nmake failed" } |
| 122 | + |
| 123 | + if($runTests) |
| 124 | + { |
| 125 | + &nmake -f $makFile test |
| 126 | + if ($LastExitCode) { throw "nmake test failed" } |
| 127 | + } |
| 128 | + |
| 129 | + &nmake -f $makFile install |
| 130 | + if ($LastExitCode) { throw "nmake install failed" } |
| 131 | + |
| 132 | + copy "$ENV:OPENSSL_ROOT_DIR\bin\*.dll" $outputPath |
| 133 | + copy "$ENV:OPENSSL_ROOT_DIR\bin\*.exe" $outputPath |
| 134 | + } |
| 135 | + finally |
| 136 | + { |
| 137 | + popd |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +# Build tools |
| 142 | + |
| 143 | +# Visual Studio 2017 Community edition or above (tested also with 2013 and 2105, change the following variables accordingly in case) |
| 144 | +# CMake: https://cmake.org/files/v3.10/cmake-3.10.0-rc4-win64-x64.msi |
| 145 | +# ActivePerl: http://downloads.activestate.com/ActivePerl/releases/5.24.2.2403/ActivePerl-5.24.2.2403-MSWin32-x64-403863.exe |
| 146 | +# NASM: http://www.nasm.us/pub/nasm/releasebuilds/2.13/win64/nasm-2.13-installer-x64.exe |
| 147 | +# 7-zip: http://www.7-zip.org/a/7z1604-x64.exe |
| 148 | + |
| 149 | +# Make sure ActivePerl comes before MSYS Perl, otherwise |
| 150 | +# the OpenSSL build will fail |
| 151 | +$ENV:PATH = "C:\Perl64\bin;$ENV:PATH" |
| 152 | +$ENV:PATH += ";$ENV:ProgramFiles\7-Zip" |
| 153 | +$ENV:PATH += ";$ENV:ProgramFiles\CMake\bin" |
| 154 | +$ENV:PATH += ";$ENV:ProgramFiles\nasm" |
| 155 | + |
| 156 | +# Visual Studio 2017, amd64 build, change as needed |
| 157 | +$vsVersion = "15.0" |
| 158 | +$vsPlatform = "amd64" |
| 159 | +$cmakeGenerator = "Visual Studio 15 2017 Win64" |
| 160 | +$platformToolset = "v150" |
| 161 | + |
| 162 | +# Change OpenSSL version and hash based on your requirements, e.g.: 1.1.0g |
| 163 | +$opensslVersion = "1.0.2m" |
| 164 | +$opensslSha1 = "27fb00641260f97eaa587eb2b80fab3647f6013b" |
| 165 | + |
| 166 | +# Set to false for a static build |
| 167 | +$dllBuild = $true |
| 168 | +# Set to false to skip the tests |
| 169 | +$runTests = $true |
| 170 | + |
| 171 | +# Change the build and output path as needed |
| 172 | +$buildDir = "C:\Build\" |
| 173 | +$outputPath = "$buildDir\bin" |
| 174 | + |
| 175 | +CheckDir $buildDir |
| 176 | +CheckDir $outputPath |
| 177 | +$ENV:OPENSSL_ROOT_DIR="$outputPath\OpenSSL" |
| 178 | + |
| 179 | +SetVCVars $vsVersion $vsPlatform |
| 180 | +BuildOpenSSL $buildDir $outputPath $opensslVersion $vsPlatform $cmakeGenerator $platformToolset $dllBuild $runTests $opensslSha1 |
| 181 | + |
| 182 | +Write-Output "Done! The generated OpenSSL binaries are available in: $outputPath" |
| 183 | + |
0 commit comments