本文介绍了在 OS X 上构建 Multiarch OpenSSL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 OS X 上为 32 位和 64 位架构构建 OpenSSL.我需要为 ./Configure 提供哪些选项,以便我将它为两种架构构建到同一个 .a 文件中?

I need to build OpenSSL on OS X for 32 and 64 bit architectures. What are the options I need to give to ./Configure so that I get it built for both architectures into same .a file?

推荐答案

您必须小心使用 OpenSSL 和多架构库,因为该库不是多架构安全的.那是因为每个配置都有自己的文件,每个平台的BIGNUM都不一样.

You have to be careful with OpenSSL and multiarch libraries because the library is not multiarch safe. That's because each configuration has its own <openssl/opensslconf.h> file, and each platform's BIGNUM is different.

提供 -arch x86_64 -arch i386 将导致构建失败,因为 OpenSSL 的构建系统形成命令的方式.另请参阅在为 Mac 编译 OpenSSL 时出现 libcrypto ar 错误.

Supplying -arch x86_64 -arch i386 will result in a build failure because of the way OpenSSL's build system forms commands. Also see Getting libcrypto ar error while compiling OpenSSL for Mac.

下面详述的相同过程也适用于 iOS.唯一改变的是-arch.对于 iOS,您可能会使用 armv7armv7sarm64i386(用于 32 位模拟器调试)和 x86_64(用于 64 位模拟器调试).

The same procedure detailed below applies to iOS, too. The only thing that changes is the -arch. For iOS, you will probably use armv7, armv7s, arm64 and i386 (for 32-bit simulator debug) and x86_64 (for 64-bit simulator debug).

还需要另一个不太明显的技巧.OpenSSL基于--prefix--openssldir硬编码了一些默认路径,所以你必须为安装目录构建32位,安装,然后移动它;然后为安装目录构建64位,安装,然后移动;然后在安装目录下创建fat库.另见如何确定openssl.cnf的默认位置?

There's another not-so-apparent trick required. OpenSSL hard codes some default paths based on --prefix and --openssldir, so you have to build 32-bit for the installation directory, install, and then move it; then build 64-bit for the installation directory, install, and then move it; and then create the fat library in the install directory. Also see How to determine the default location for openssl.cnf?

最后,不要替换 OS X 提供的 OpenSSL.OpenSSL 1.0.x 和 1.1.x 与 Apple 的 0.9.8 版 OpenSSL 二进制兼容.由于不兼容,以下过程使用 $HOME/ssl.您可以使用 /usr/local/ssl 或任何其他适合您口味的位置.

Finally, do not replace OS X's supplied OpenSSL. OpenSSL 1.0.x and 1.1.x are not binary compatible with the Apple's 0.9.8 version of OpenSSL. Because of incompatibilities, the procedures below uses $HOME/ssl. You can use /usr/local/ssl or any other location that suits your taste.

在您开始之前,OpenSSL wiki 有一个关于编译和安装的页面.有很多选项可以提供给 config.选择适合您口味的.我总是使用no-ssl2,通常使用no-ssl3no-comp.在移动设备上,我使用 no-srpno-pskno-hwno-dso 和 无引擎.

Before you begin, OpenSSL wiki has a page on Compilation and Installation. There's lots of options to supply to config. Choose the ones that suit your taste. I always use no-ssl2, and usually use no-ssl3, no-comp. On mobile devices I use no-srp, no-psk, no-hw, no-dso and no-engines.

这是构建库的说明.您将为您在多架构构建中支持的每个架构配置、构建、安装然后移动.

Here are the instructions for building the library. You will configure, build, install and then move for each architecture you are supporting in your multiarch build.

32 位

make clean && make dclean

KERNEL_BITS=32 ./config no-ssl2 no-ssl3 --prefix=$HOME/ssl
make depend
make
make install_sw

mv $HOME/ssl/include/openssl/opensslconf.h $HOME/ssl/include/openssl/opensslconf-x86.h
mv $HOME/ssl/include/openssl/bn.h $HOME/ssl/include/openssl/bn-x86.h
mv $HOME/ssl/ $HOME/ssl-x86

64 位

make clean && make dclean

KERNEL_BITS=64 ./config no-ssl2 no-ssl3 --prefix=$HOME/ssl
make depend
make
make install_sw

mv $HOME/ssl/include/openssl/opensslconf.h $HOME/ssl/include/openssl/opensslconf-x64.h
mv $HOME/ssl/include/openssl/bn.h $HOME/ssl/include/openssl/bn-x64.h
mv $HOME/ssl/ $HOME/ssl-x64

标题

需要复制一组headers(哪个都没有关系),复制opensslconf-x86.hopensslconf-x64.h bn-x86.hbn-x64.h,新建,新建,最后创建多架构库.

You need to copy one set of headers (it does not matter which), copy opensslconf-x86.h, opensslconf-x64.h bn-x86.h and bn-x64.h, create a new <openssl/opensslconf.h>, create a new <openssl/bn.h>, and finally create the multiarch libraries.

rm -rf $HOME/ssl

mkdir -p $HOME/ssl/bin
mkdir -p $HOME/ssl/include/openssl
mkdir -p $HOME/ssl/lib

cp $HOME/ssl-x86/openssl.cnf $HOME/ssl/openssl.cnf
cp $HOME/ssl-x86/include/openssl/* $HOME/ssl/include/openssl
cp $HOME/ssl-x86/include/openssl/opensslconf-x86.h $HOME/ssl/include/openssl/opensslconf-x86.h
cp $HOME/ssl-x64/include/openssl/opensslconf-x64.h $HOME/ssl/include/openssl/opensslconf-x64.h
cp $HOME/ssl-x86/include/openssl/bn-x86.h $HOME/ssl/include/openssl/bn-x86.h
cp $HOME/ssl-x64/include/openssl/bn-x64.h $HOME/ssl/include/openssl/bn-x64.h

新的

如果您还没有这样做,请创建 $HOME/ssl/include/openssl/opensslconf.h.确保您使用新的标题保护 (OPENSSL_MULTIARCH_CONF_HEADER):

If you have not done so, create $HOME/ssl/include/openssl/opensslconf.h. Be sure you use a new header guard (OPENSSL_MULTIARCH_CONF_HEADER):

cat $HOME/ssl/include/openssl/opensslconf.h
#ifndef OPENSSL_MULTIARCH_CONF_HEADER
#define OPENSSL_MULTIARCH_CONF_HEADER

#if __i386 || __i386__
# include "opensslconf-x86.h"
#elif __x86_64 || __x86_64__ || __amd64 || __amd64__
# include "opensslconf-x64.h"
#else
# error Unknown architecture
#endif

#endif /* OPENSSL_MULTIARCH_CONF_HEADER */

新的

创建$HOME/ssl/include/openssl/bn.h.确保您使用新的标题保护 (OPENSSL_MULTIARCH_BN_HEADER):

Create $HOME/ssl/include/openssl/bn.h. Be sure you use a new header guard (OPENSSL_MULTIARCH_BN_HEADER):

cat $HOME/ssl/include/openssl/bn.h
#ifndef OPENSSL_MULTIARCH_BN_HEADER
#define OPENSSL_MULTIARCH_BN_HEADER

#if __i386 || __i386__
# include "bn-x86.h"
#elif __x86_64 || __x86_64__ || __amd64 || __amd64__
# include "bn-x64.h"
#else
# error Unknown architecture
#endif

#endif /* OPENSSL_MULTIARCH_BN_HEADER */

图书馆

此时,您有位于 $HOME/ssl-x86 的库的 x86 构建和位于 $HOME/ssl-x64.您可以将它们与 $HOME/ssl 中的 lipo 结合起来.

At this point, you have a x86 build of the library located at $HOME/ssl-x86 and a x64 build of the library located at $HOME/ssl-x64. You combine them with lipo at $HOME/ssl.

lipo -create $HOME/ssl-x86/lib/libcrypto.a
             $HOME/ssl-x64/lib/libcrypto.a
             -output $HOME/ssl/lib/libcrypto.a

lipo -create $HOME/ssl-x86/lib/libssl.a
             $HOME/ssl-x64/lib/libssl.a
             -output $HOME/ssl/lib/libssl.a

lipo -create $HOME/ssl-x86/bin/openssl
             $HOME/ssl-x64/bin/openssl
             -output $HOME/ssl/bin/openssl

共享库

如果你配置了shared,那么你需要执行:

If you configured with shared, then you need to perform:

lipo -create $HOME/ssl-x86/lib/libcrypto.1.0.0.dylib
             $HOME/ssl-x64/lib/libcrypto.1.0.0.dylib
             -output $HOME/ssl/lib/libcrypto.1.0.0.dylib

lipo -create $HOME/ssl-x86/lib/libssl.1.0.0.dylib
             $HOME/ssl-x64/lib/libssl.1.0.0.dylib
             -output $HOME/ssl/lib/libssl.1.0.0.dylib

然后,您需要重新创建软链接:

Then, you need to recreate the softlinks:

ln -s $HOME/ssl/lib/libcrypto.dylib $HOME/ssl/lib/libcrypto.1.0.0.dylib
ln -s $HOME/ssl/lib/libssl.dylib $HOME/ssl/lib/libssl.1.0.0.dylib

最后,测试一下.验证库是多架构的:


Finally, test things. Verify the libraries are multiarch:

ls $HOME/ssl/lib/
libcrypto.a libssl.a

lipo -info $HOME/ssl/lib/libcrypto.a
Architectures in the fat file: $HOME/ssl/lib/libcrypto.a are: i386 x86_64
lipo -info $HOME/ssl/lib/libssl.a
Architectures in the fat file: $HOME/ssl/lib/libssl.a are: i386 x86_64

然后是一个测试程序:

#include <openssl/opensslconf.h>
#include <openssl/ssl.h>

int main(int argc, char* argv[])
{
  SSL_library_init();
  return 0;
}

还有:

$ clang -arch i386 -arch x86_64 -I $HOME/ssl/include test.c -o test.exe -L $HOME/ssl/lib -lssl -lcrypto
$ DYLD_LIBRARY_PATH=$HOME/ssl/lib; ./test.exe
$

DYLD_LIBRARY_PATH 用于在 OS X 上构建动态库的情况.

DYLD_LIBRARY_PATH is used in case you built the dynamic libraries on OS X.

如果需要,您可以删除非多架构安装:

If desired, you can delete the non-multiarch installations:

rm -rf $HOME/ssl-x86
rm -rf $HOME/ssl-x64

这篇关于在 OS X 上构建 Multiarch OpenSSL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 08:12