0

I would like to cross-compile OpenSSL 1.0.1h for iOS on OS X without gcc installed by following instructions in the WWDC 2014 session 305 video and from the OpenSSL web site.

I unzip/untar and the switch from gcc to clang (almost) as suggested here.

OLD_LANG=$LANG
unset LANG

sed -i "" 's|\"iphoneos-cross\"\,\"llvm-gcc\:-O3|\"iphoneos-cross\"\,\"clang\:-Os|g' Configure
sed -i "" 's/CC= cc/CC= clang/g' Makefile.org
sed -i "" 's/CFLAG= -O/CFLAG= -Os/g' Makefile.org

export LANG=$OLD_LANG

Notice that I had to stick with the makedepend tool for otherwise already the make depend step will fail (as was also mentioned elsewhere). Installing that tool with brew install makedepend is not an issue for me (but installing gcc next to Clang would be). Now

./Configure iphoneos-cross
make depend
make

leads to warnings (during make depend) and errors (during make) about missing include files (such as stdlib.h). The typical include path passed to clang is apparently -I. -I.. -I../include.

I assume this is to related to clang not receiving specific C options by default, but if so, what is the most sensible way for making the compilation work e.g. by fixing the sed edits or by setting environment variables. (Currently I don't have any CC* environment variables set. There is e.g. /usr/include/stdlib.h on my system.)

I rarely use OS X C compilers on the command line and the solution is therefore not obvious to me.

UPDATE Here is the question in a more specific for whose valid answer will be awarded with a bounty: What specific edits (similar to the first code snippet above) have to be performed on the files in latest version of OpenSSL source code (e.g. openssl-1.0.1h.tar.gz) so that one can cross-compiles it by issuing commands similar to the second code snippet above for iOS 7 (soon iOS 8) architectures on OS X 10.9 with Xcode 5.1.1 (soon Xcode 6) installed, without gcc installed and with makedepend optionally installed?

5
  • Related
    – Drux
    Commented Jul 15, 2014 at 5:29
  • What output do you see from this on the command line? "xcrun gcc"
    – quellish
    Commented Jul 20, 2014 at 23:23
  • @quellish I get clang: error: no input files. So gcc apparently exists in the shape of Clang. So what I mean by it not being installed is that I did not try to (re)install it with MacPorts, Homebrew, ...
    – Drux
    Commented Jul 21, 2014 at 18:33
  • 2
    You should not have to install anything if you have Xcode installed. 'gcc' is aliased to clang, which will accept gcc's options. It's a drop in replacement. Invoke it by using 'xcrun gcc' if just 'gcc' from the command line does not work for you. On Mavericks it should. Other than that, you should only have to change CFLAGS and LDFLAGS to do the cross compiling you want.
    – quellish
    Commented Jul 21, 2014 at 18:41
  • +1 @quellish Can you please propose specific values that do make the cross-compilation work (and the bounty is yours ...)? Here is the OpenSSL download link. E.g. both export CFLAGS=-I/usr/include; make and make CGLAGS=-I/usr/include lead to (albeit different) errors. There also seems to be a (perhaps subsequent) problem about a directory /SDKs/, which is missing on my Mac. (I do have Xcode 5.1.1 -- even Xcode 6.0 -- installed on Mavericks, though.)
    – Drux
    Commented Jul 21, 2014 at 19:29

2 Answers 2

5
+50

First, you must make sure that the current Xcode developer directory is set:

xcode-select -p

This should output something like this: /Applications/Xcode.app/Contents/Developer If you want to change this, for example to switch between Xcode 6 and Xcode 5 (or if that command outputted a blank line), you would use xcode-select as follows:

xcode-select -s /Applications/OldXcode.app/Contents/Developer

With that done, next you will need to choose an SDK to use. Since you're trying to build for iPhone, it would be some version of the iPhoneOS SDK. You can find which version it will try to use by default using this command:

xcrun --sdk iphoneos --show-sdk-version

Which will output something like 7.1 . If you need a different SDK version, you can provide it by using the specific SDK name with the version, i.e. iphoneos7.1. This of course must already be installed in the version of Xcode that you selected with xcode-select.

So assuming you have all those ducks in a row, using that information to cross compile is pretty easy. For example:

make CC="$(xcrun --sdk iphoneos --find clang) -isysroot $(xcrun --sdk iphoneos --show-sdk-path) -arch armv7 -arch armv7s -arch arm64"

This will pass the CC commands through xcrun, with the arguments to use the current iphoneos SDK of the Xcode set by xcode-select, building using the SDK root as the sys root and building for architectures armv7, armv7s, and arm64. Building for the iOS Simulator would be similar to the above, but passing the iphonesimulator in place of iphoneos as the SDK, and setting the architectures to i386 and x86_64.

With that, things should pretty much just work and you may not need to do anything further -- unfortunately I'm not in a position to try this myself and I'm referring to notes from a previous project. You may run into subsequent issues that are specific to OpenSSL or it's build scripts.

Note that all of the above can be used to compile for MacOS X as well, obviously with different SDK and architecture values.

4
  • +1 Thx, this is very helpful, although the OpenSSL build scripts indeed still cause some subsequent issues.
    – Drux
    Commented Jul 21, 2014 at 21:47
  • What issues are the OpenSSL build scripts giving you at this point? There might be easy solutions. Unfortunately I won't be in a position to try a build of this myself until late tonight or tomorrow.
    – quellish
    Commented Jul 21, 2014 at 22:59
  • Alright I got a step further now. Cross-compilation works, linking does not yet. What I apparently need to do now is employ lipo as $(AR) in calls to $(AR) $(LIB) $(LIBOBJ) (see e.g. OpenSSL's crypto/bio/Makefile) in such a way that it replaces in a single step several *.o files (those listed in $(LIBOBJ)) for several architectures (armv7s, armv7, arm64) in an *.a file (i.e. $(LIB)) that may or may not yet exist. Maybe it comes down to writing a short bash script (which I have not done in a long time :) ar says it cannot handle the fat *.a file.
    – Drux
    Commented Jul 22, 2014 at 8:57
  • 1
    This? stackoverflow.com/questions/22538061/…
    – quellish
    Commented Jul 22, 2014 at 18:33
1

I don't know if it's exactly what you want... but I compile OpenSSL for iOS following this script: https://github.com/x2on/OpenSSL-for-iPhone/blob/master/build-libssl.sh

and it's very straightforward... anyways, this answer here can help somebody else looking for how to compile OpenSSL for iOS.

2
  • 1
    Thx & yes, I've also found this script by now, but since I've invested quite some time already, and this concerns a security topic, I'll invest some more now in trying to understand what's exactly going on rather than just running somebody's convenient script. I'm almost there now ...
    – Drux
    Commented Jul 23, 2014 at 6:39
  • I see... I noticed... when you finish your solution please post it here... I am curious about it... Commented Jul 23, 2014 at 11:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.