如何使用Visual Studio 2017在Windows上构建OpenSSL?

时间:2017-08-03 21:15:13

标签: c++ visual-studio build openssl

我正在尝试使用OpenSSL,但我仍然坚持编译的步骤。 OpenSSL项目有非常不友好(糟糕)的文档。

有没有实际帮助如何使用Visual Studio 2017在Windows上构建最新的OpenSSL版本?

我在官方OpenSSL网站上找不到任何有用的信息。是的,互联网上有很多关于OpenSSL编译的帖子,但都是过时的。

4 个答案:

答案 0 :(得分:24)

我没有使用过VS2017但是之前的版本。我想它大致相同。简而言之,步骤是:

  1. 安装Perl(ActiveState或Strawberry)

  2. 安装NASM

  3. 确保Perl和NASM都在您的%PATH%

  4. 使用管理权限启动Visual Studio Developer命令提示符(如果要构建32位OpenSSL,请确保使用32位,如果要构建64位OpenSSL,请确保使用64位版本) )

  5. 从OpenSSL源目录的根目录输入perl Configure VC-WIN32,如果需要32位OpenSSL,请输入perl Configure VC-WIN64A如果需要64位OpenSSL

  6. 输入nmake

  7. 输入nmake test

  8. 输入nmake install

  9. 如果在任何阶段出现任何问题,请检查INSTALL文件和NOTES.WIN文件。

答案 1 :(得分:1)

对于OpenSSL 1.0.2,我编写了一个Python脚本,为我做了构建。我有制作这些剧本的习惯,因为我不想在每次需要制作东西时重新发明轮子。

该脚本是为OpenSSL 1.0.2制作的。可能OpenSSL 1.1.0的变化很小。

这是脚本:

import os
from subprocess import call
import sys
import re

vs_version = "140"
compile_flags = "-no-asm -no-shared"


openssl_32_flag = "VC-WIN32"
openssl_64_flag = "VC-WIN64A"

src_32_suffix = "_" + "vs" + vs_version + "_32"
src_64_suffix = "_" + "vs" + vs_version + "_64"

vs_tools_env_var = "VS" + vs_version + "COMNTOOLS"

if len(sys.argv) < 2:
    print("First argument must be the tar.gz file of OpenSSL source")
    exit(1)

if len(sys.argv) < 3:
    print("Second argument must be 32 or 64")
    exit(1)

filename = sys.argv[1]
dirname  = filename.replace(".tar.gz","")
working_dir = os.getcwd()
arch = sys.argv[2]

if arch != "32" and arch != "64":
    print("Second argument must be 32 or 64")
    exit(1)


if not bool(re.match("(openssl-){1}(\d)+(.)(\d)+(.)(\d)+(\w)+(.tar.gz)",filename)):
    print("The file given doesn't seem to be an openssl source file. It must be in the form: openssl-x.y.zw.tar.gz")
    exit(1)


call("7z x " + filename) #extract the .gz file

dirname_src_32 = dirname + src_32_suffix
dirname_src_64 = dirname + src_64_suffix
dirname_bin_32 = dirname + src_32_suffix + "_build"
dirname_bin_64 = dirname + src_64_suffix + "_build"

openssl_tar_file = filename[0:-3]

if arch == "32":
#extract tar file for 32
    call("7z x " + openssl_tar_file)
    os.rename(dirname, dirname_src_32)

#Compile 32
    os.chdir(dirname_src_32)

    call("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags,shell=True)
    call(r"ms\do_ms.bat",shell=True)
    call(r"nmake -f ms\nt.mak",shell=True)
    call(r"nmake -f ms\nt.mak instalL",shell=True)

    print("32-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

if arch == "64":
#extract for 64
    call("7z x " + openssl_tar_file)
    os.rename(dirname, dirname_src_64)

#Compile 64
    os.chdir(dirname_src_64)

    call("perl Configure " + openssl_64_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_64) + " " + compile_flags,shell=True)
    call(r"ms\do_ms.bat",shell=True)
    call(r"nmake -f ms\nt.mak",shell=True)
    call(r"nmake -f ms\nt.mak instalL",shell=True)

    print("64-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

os.remove(openssl_tar_file)

选项1 :将脚本保存到CompileOpenSSL.py,然后下载预期名称格式为openssl-1.X.Y.tar.gz的OpenSSL源文件。现在假设您可以在命令提示符下从全局范围访问7zip和perl,并且您已加载了正确的MSVC变量(例如vsvars32.bat或启动正确的终端),请运行以下命令:

python CompileOpenSSL.py openssl-1.X.Y.tar.gz 32

如果您正在使用32位MSVC或

python CompileOpenSSL.py openssl-1.X.Y.tar.gz 64

用于MSVC 64位。

选项2 :执行脚本手动执行的操作。该脚本只是提取存档,配置源并运行do_ms.bat然后nmake。关注来源,它将起作用。

祝你好运!

答案 2 :(得分:0)

  1. 使用Visual Studio cmd进入ssl目录,并将perl和nasm添加到系统路径,然后键入:

  2. perl Configure --openssldir=D:OpenSSLdirectory VC-WIN32

  3. ms\do_ms.bat

  4. nmake -f ms\ntdll.mak

  5. nmake -f ms\ntdll.mak install

  6. (欣赏。)

答案 3 :(得分:0)

量子物理学家python脚本的修改版本

它可以编译OpenSSL 1.0.x或OpenSSL 1.1.x

它可以与包括的Visual Studio 2017/2019的多个版本一起编译。

1)创建文件:CompileOpenSSL.py

import os
import os.path
from subprocess import call
import shutil
import sys
import re
import argparse

# args
parser = argparse.ArgumentParser()
parser.add_argument("-f", "--filename", help="First argument must be the tar.gz file of OpenSSL source", required=True)
parser.add_argument("-a", "--arch", help="Second argument must be x86 or amd64", required=True)
parser.add_argument("-v", "--vs_version", help="Visual Studio version (eg:90, 140, 150)", required=True)
parser.set_defaults(writeVersionInfos=False)
args = parser.parse_args()

compile_flags = "-no-asm"
#compile_flags = "-no-asm -no-shared"

openssl_32_flag = "VC-WIN32"
openssl_64_flag = "VC-WIN64A"

working_dir = os.getcwd()

dirname  = args.filename.replace(".tar.gz","")

src_32_suffix = "_" + "vs" + args.vs_version + "_32"
src_64_suffix = "_" + "vs" + args.vs_version + "_64"

vs_tools_env_var = "VS" + args.vs_version + "COMNTOOLS"


if args.arch != "x86" and args.arch != "amd64":
    print("Second argument must be x86 or amd64")
    exit(1)


if not bool(re.match("(openssl-){1}(\d)+(.)(\d)+(.)(\d)+(\w)+(.tar.gz)",args.filename)):
    print("The file given doesn't seem to be an openssl source file. It must be in the form: openssl-x.y.zw.tar.gz")
    exit(1)


call("7z x -y " + args.filename) #extract the .gz file

dirname_src_32 = dirname + src_32_suffix
dirname_src_64 = dirname + src_64_suffix
dirname_bin_32 = dirname + src_32_suffix + "_build"
dirname_bin_64 = dirname + src_64_suffix + "_build"

openssl_tar_file = args.filename[0:-3]

if args.arch == "x86":

#delete previous directories
    shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)
    shutil.rmtree(os.getcwd()+'/'+dirname_src_32, ignore_errors=True)

#extract tar file for 32

    call("7z x -y " + openssl_tar_file)
    os.rename(dirname, dirname_src_32)

#Compile 32
    os.chdir(dirname_src_32)

    print("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags)
    call("perl Configure " + openssl_32_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_32) + " " + compile_flags,shell=True)

    if( os.path.exists("ms/do_ms.bat") ):
        call("ms\do_ms.bat",shell=True)
        print(os.getcwd())
        call("nmake -f ms/ntdll.mak",shell=True)
        call("nmake -f ms/ntdll.mak install",shell=True)
    else:
        call("nmake",shell=True)
        call("nmake test",shell=True)
        call("nmake install",shell=True)

    print("32-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

if args.arch == "amd64":

#delete previous directories
    shutil.rmtree(os.getcwd()+'/'+dirname, ignore_errors=True)
    shutil.rmtree(os.getcwd()+'/'+dirname_src_64, ignore_errors=True)


#extract for 64
    call("7z x -y " + openssl_tar_file)
    os.rename(dirname, dirname_src_64)

#Compile 64
    os.chdir(dirname_src_64)

    call("perl Configure " + openssl_64_flag + " --prefix=" + os.path.join(working_dir,dirname_bin_64) + " " + compile_flags,shell=True)
    if( os.path.exists("ms\do_ms.bat") ):
        call("ms\do_win64a.bat",shell=True)
        call("nmake -f ms/ntdll.mak",shell=True)
        call("nmake -f ms/ntdll.mak install",shell=True)
    else:
        call("nmake",shell=True)
        call("nmake test",shell=True)
        call("nmake install",shell=True)

    print("64-bit compilation complete.")

#Go back to base dir
os.chdir(working_dir)
################

os.remove(openssl_tar_file)

2)创建文件:CompileOpenSSL_vs.cmd

ECHO  --------------------------------------
ECHO Require Python, 7Zip, PERL and NASM in PATH
ECHO  --------------------------------------

Rem ------------------------------------------------------
Rem TO CONFIGURE -----------------------------------------
Rem ------------------------------------------------------

Rem SET YOUR LOCAL PATHS-----------------------------------------
SET PATH=C:\Program Files (x86)\7-Zip;C:\Perl64\bin;M:\Backup\Coders\_tools\7-Zip\;%PATH% 

Rem SET YOUR OPENSSL ARCHIVE-----------------------------------------
REM SET FILENAME=openssl-1.0.2r.tar.gz 
SET FILENAME=openssl-1.1.1b.tar.gz

Rem SET THE VERSION OF YOUR VISUAL STUDIO-----------------------------------------
SET VSVERSION=%1


Rem ------------------------------------------------------
Rem COMPILATION LAUNCH -----------------------------------
Rem ------------------------------------------------------

Rem UTILS PATH-----
SET VSCOMNTOOLSNAME=VS%VSVERSION%COMNTOOLS

Rem Pick the good path for Visual Studio-----------------------------------------
IF %VSVERSION% GEQ 150 (
    Echo DO NOT FORGET TO ADD A SYSTEM VARIABLE %VSCOMNTOOLSNAME% - like: "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\Tools\"
    SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\Auxiliary\Build\vcvarsall.bat"
) ELSE (
    SET VCVARPATH="%%%VSCOMNTOOLSNAME%%%..\..\VC\vcvarsall.bat"
)

Rem Set env -----------------------------------------
@pushd "%~dp0"
call %VCVARPATH% %2
@popd

Rem ------------------------------------------------------
Rem TEST APP EXIST -----------------------------------
Rem ------------------------------------------------------

where /q 7z.exe
IF ERRORLEVEL 1 (
    ECHO The application "7z.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q perl.exe
IF ERRORLEVEL 1 (
    ECHO The application "perl.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q nmake.exe
IF ERRORLEVEL 1 (
    ECHO The application "nmake.exe" is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

where /q py.exe
IF ERRORLEVEL 1 (
    ECHO The application "py.exe" [shortcut of python] is missing. Ensure to add/install it to the PATH in beginning of this script, check SET PATH
    PAUSE
    EXIT /B
)

Rem Launch compilation -----------------------------------------

py CompileOpenSSL.py -f %FILENAME% -a %2 -v %VSVERSION%


PAUSE

3)从命令行启动编译(在Visual Studio外部) 例如:

CompileOpenSSL_vs.cmd 150 x86
CompileOpenSSL_vs.cmd 150 amd64

CompileOpenSSL_vs.cmd 90 x86