vc++编译openssl 1.0.2

windows编译openssl 1.0.2

下载地址:https://www.openssl.org/source/

编译完成后库文件位于源文件目录\outXX目录下

cls
@echo off

::你的openssl源文件目录
set OPENSSL_PATH=F:\OpenLib\openssl-1.0.2q

::你的编译环境目录
::set VS_DEV_CMD_x86="C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\vsvars32.bat"
set VS_DEV_CMD_x86="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars32.bat"
set VS_DEV_CMD_x64="C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Auxiliary\Build\vcvars64.bat"

set cpuType=0
set buildType=0
set buileDebug=0
set VS_DEV_CMD=""

call :selectCPU
call :selectBuildType
call :selectDebug

if %cpuType%==1 set VS_DEV_CMD=%VS_DEV_CMD_x86%
if %cpuType%==2 set VS_DEV_CMD=%VS_DEV_CMD_x64%

set TO_DIR=%OPENSSL_PATH%\Build
if %cpuType%==1 set TO_DIR=%TO_DIR%_x86
if %cpuType%==2 set TO_DIR=%TO_DIR%_x64
if %buildType%==1 set TO_DIR=%TO_DIR%_lib
if %buildType%==2 set TO_DIR=%TO_DIR%_dll
if %buileDebug%==1 set TO_DIR=%TO_DIR%_debug
if %buileDebug%==2 set TO_DIR=%TO_DIR%_release

::VC-WIN32 或者 VC-WIN64A
set perl_cpuType=""
if %cpuType%==1 set perl_cpuType=VC-WIN32
if %cpuType%==2 set perl_cpuType=VC-WIN64A
if %buileDebug%==1 set perl_cpuType=debug-%perl_cpuType%

::echo 删除临时文件...
::del "%OPENSSL_PATH%\tmp32\*.*" /f/s/q/a >> null
::del "%OPENSSL_PATH%\tmp32.dbg\*.*" /f/s/q/a  >> null

call %VS_DEV_CMD%

cd /d "%OPENSSL_PATH%"

perl Configure %perl_cpuType% no-asm --prefix="%TO_DIR%"

cd /d "%OPENSSL_PATH%"

if %cpuType%==1 call ms\do_ms.bat
if %cpuType%==2 call ms\do_win64a.bat

::替换Zi为Z7 解决使用lib时找不到pdb的痛苦警告
::解决dll依赖vc运行库的痛苦
echo 解决使用库期间潜在的痛苦...
if %buildType%==1 (
	call :replaceText "%OPENSSL_PATH%\ms\nt.mak" "/Zi" "/Z7" >> null
	call :replaceText "%OPENSSL_PATH%\ms\nt.mak" "/MD" "/MT" >> null
	call :replaceText "%OPENSSL_PATH%\ms\nt.mak" "/MDd" "/MTd" >> null
	
	if %cpuType%==2 (
		call :replaceText "%OPENSSL_PATH%\ms\nt.mak" "out32" "out64" >> null
		call :replaceText "%OPENSSL_PATH%\ms\nt.mak" "tmp32" "tmp64" >> null
	)
	
) else (
	call :replaceText "%OPENSSL_PATH%\ms\ntdll.mak" "/Zi" "/Z7" >> null
	call :replaceText "%OPENSSL_PATH%\ms\ntdll.mak" "/MD" "/MT" >> null
	call :replaceText "%OPENSSL_PATH%\ms\ntdll.mak" "/MDd" "/MTd" >> null
	
	if %cpuType%==2 (
		call :replaceText "%OPENSSL_PATH%\ms\ntdll.mak" "out32" "out64" >> null
		call :replaceText "%OPENSSL_PATH%\ms\ntdll.mak" "tmp32" "tmp64" >> null
	)
)

echo 开始编译...
if %buildType%==1 nmake -f ms\nt.mak
if %buildType%==2 nmake -f ms\ntdll.mak

pause
@exit /B 0

:selectCPU
echo 1,编译x86
echo 2,编译x64
set /p cpuType="输入数字(1或2):"
if %cpuType%==1 @exit /B 0
if %cpuType%==2 @exit /B 0
cls
@echo off
echo 输入错误:%cpuType%,请输入1或2
goto selectCPU


:selectBuildType
echo 1,编译静态库(lib)
echo 2,编译动态库(dll)
set /p buildType="输入数字(1或2):"
if %buildType%==1 @exit /B 0
if %buildType%==2 @exit /B 0
cls
@echo off
echo 输入错误:%buildType%,请输入1或2
goto selectBuildType

:selectDebug
echo 1,编译debug版本
echo 2,编译release版本
set /p buileDebug="输入数字(1或2):"
if %buileDebug%==1 @exit /B 0
if %buileDebug%==2 @exit /B 0
cls
@echo off
echo 输入错误:%buileDebug%,请输入1或2
goto selectDebug

:replaceText
setlocal enabledelayedexpansion
set file=%~1
set srcTxt=%~2
set toTxt=%~3
set file_tmp=%file%.tmp
set file_bak=%file%.bak
for /f "delims=" %%i in (%file%) do (
    set str=%%i
        set "str=!str:%srcTxt%=%toTxt%!"
        echo !str!>>%file_tmp%
)
copy /Y "%file%" "%file_bak%" >nul 2>nul
move /Y "%file_tmp%" "%file%"
@exit /B 0

发表评论

电子邮件地址不会被公开。