Build Multiarch OpenSSL on OS X

I need to build OpenSSL for OS X for 32-bit and 64-bit architectures. What options do I need to provide ./Configure so that I build them for both architectures in the same .a file?

+5
source share
1 answer

./Configure so that I build it for both architectures in a single .a file?

You must be careful with the OpenSSL and multiarch libraries because the library is not multi-archived. This is because each configuration has its own <openssl/opensslconf.h> file, and each BIGNUM platform BIGNUM different.

Delivering -arch x86_64 -arch i386 will result in build failure due to OpenSSL build commands forming commands. Also see Getting libcrypto ar error while compiling OpenSSL for Mac .

The same procedure described below also applies to iOS. The only thing that changes is -arch . For iOS, you'll probably use armv7 , armv7s , arm64 and i386 (for 32-bit debugging of the simulator) and x86_64 (for 64-bit debugging simulator).

There you need another less obvious trick. OpenSSL hardcodes some default paths based on --prefix and --openssldir , so you need to create a 32-bit directory to install, install, and then move it; then create a 64-bit installation directory, install, and then move it; and then create the bold library in the installation directory. Also see How to determine the default location for openssl.cnf?

Finally, do not replace OS X with the supplied OpenSSL. OpenSSL 1.0.x and 1.1.x are not compatible with the OpenSSL version for Apple 0.9.8. Due to incompatibilities, the procedures below use $HOME/ssl . You can use /usr/local/ssl or any other place that suits your taste.


Before you start, the OpenSSL wiki database has a page on Compilation and Installation . There are many options for delivering 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 instructions for creating a library. You will configure, create, install, and relocate for each architecture that you support in your assembly with multiple rights.

32 bit

 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 bit

 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 

Headings

You need to copy one set of headers (no matter which one), 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 multi-archive 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 

New <opensslconf.h>

If you have not already done so, create $HOME/ssl/include/openssl/opensslconf.h . Make sure you use the new header protector ( 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 */ 

New <bn.h>

Create $HOME/ssl/include/openssl/bn.h Make sure you use the new header protector ( 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 */ 

Libraries

At this point, you have the x86 library assembly located in $HOME/ssl-x86 , and the x64 library assembly located in $HOME/ssl-x64 . You combine them with lipo in $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 

Share Libraries

If you configured using shared , you need to do:

 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 programmatic links:

 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, check things out. Make sure the libraries are much more archival:

 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 

And then the test program:

 #include <openssl/opensslconf.h> #include <openssl/ssl.h> int main(int argc, char* argv[]) { SSL_library_init(); return 0; } 

and

 $ 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 used if you created dynamic libraries in OS X.


If you wish, you can delete settings other than several lots:

 rm -rf $HOME/ssl-x86 rm -rf $HOME/ssl-x64 
+16
source

Source: https://habr.com/ru/post/1201240/


All Articles