How to build OpenSSL on Windows with Visual Studio 2017?

前端 未结 4 1419
渐次进展
渐次进展 2021-01-30 14:33

I am trying to use OpenSSL but I am stuck on the step of compiling. The OpenSSL project has very unfriendly (bad) documentation.

Is there any actual help how to build th

4条回答
  •  梦谈多话
    2021-01-30 15:05

    For OpenSSL 1.0.2, I wrote a Python script that does the building for me. I have this habit of making these scripts, as I don't like to reinvent the wheel everytime I need to build something.

    The script is made for OpenSSL 1.0.2. Probably the changes are minimal for OpenSSL 1.1.0.

    Here's the script:

    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)
    

    Option 1: Save the script to CompileOpenSSL.py, and download the OpenSSL source file that is expected to have the name format openssl-1.X.Y.tar.gz. Now assuming that 7zip and perl are accessible from the global scope on your command prompt and you have the correct MSVC variables loaded (with e.g. vsvars32.bat, or starting the right terminal), run the following:

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

    If you're using MSVC 32-bit, or

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

    for MSVC 64-bit.

    Option 2: Do what the script does manually. The script simply extracts the archive, configures the sources and runs do_ms.bat then nmake. Follow the source and it'll work.

    Good luck!

提交回复
热议问题