views:

2736

answers:

2

I'm trying to use CMake to generate an Xcode configuration for the iPhone by manually setting certain attributes. (Is this even the right way to go about this?) My CMake file looks like:

project(MYLIB)
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
set(CMAKE_CONFIGURATION_TYPES Debug Release Debug-iPhone)
set(FILES list of my files...)

add_library(mylib FILES)

set(XCODE_ATTRIBUTE_SDKROOT iphoneos2.2.1)
# more attributes later, I'm just trying to get one to work first

First of all, this doesn't seem to work - in the generated Xcode project (I'm running cmake . -G Xcode), SDKROOT is still set to nothing, and so it says "Current Mac OS".

Second, assuming this is the right way to do this, how do I set the attribute only for the configuration Debug-iPhone?

+3  A: 

As far as I can tell, there are two ways. The one most likely, you are nearly there, is to use CMAKE_OSX_SYSROOT to set the XCode SDKROOT. There is also the variable CMAKE_OSX_ARCHITECTURES, which maps to ARCHS in XCode.

The alternative is to use CMake's cross compiling support. I've not used it for the iphone, but I have done so for other ARM processors. You need to set up a toolchain file that would look something like this:

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_SYSTEM_PROCESSOR arm-elf)
set(CMAKE_C_COMPILER /path/to/complier/bin/arm-elf-gcc)
set(CMAKE_FIND_ROOT_PATH /path/to/compiler)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

And then when you run cmake, set the CMAKE_TOOLCHAIN_FILE variable to the name of your toolchain file. Or if you only compile to one architecture, you can hard-code the value in the CMakeLists.txt file. But I imagine you would need to cross compile for the iphone simulator and for the actual iphone itself, right? So you would run one of these, probably having a couple of build variants:

cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/my/iphone-sim.cmake .
cmake -DCMAKE_TOOLCHAIN_FILE=/path/to/my/iphone-real.cmake .

Where the sim/real files define the environment for the simulator or real iphone compiler tools.

Some other links that may help are this bug report and this mailing list conversation.

rq
I'm currently doing your first suggestion; I tried cross compiling and couldn't get it to target the right architecture (even though it used the right compiler - it was bizarre). I'm wondering - how do you find variables like CMAKE_OSX_SYSROOT? It doesn't even seem to be in a list, let alone documented.
Jesse Beder
How do I personally do it? In this case, I am subscribed to the cmake mailing list and I remember seeing some discussion about iphone there. Some of these variables appear in the cache too, so running ccmake or the gui in advanced mode might give you some hints. Failing that, grep -r on the cmake source code...
rq
+2  A: 

I now have a detailed "how-to" guide on the subject: How-To Cross-Compile for the iPhone Using CMake. The How-To guide contains the exact toolchain files you will need to cross-compile the project.

Michael Aaron Safyan