public
Last active

cmake files for Apple IOS development (has C++ lean, can be adapted however)

  • Download Gist
CMakeLists.txt
CMake
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
# See original post at http://stackoverflow.com/questions/822404/how-to-set-up-cmake-to-build-an-app-for-the-iphone
 
cmake_minimum_required(VERSION 2.8)
 
cmake_policy(SET CMP0015 NEW)
cmake_policy(SET CMP0016 NEW)
 
project(test)
set(NAME test)
 
file(GLOB headers *.h)
file(GLOB sources *.cpp)
 
SET (SDKVER "4.1")
SET (DEVROOT "/Developer/Platforms/iPhoneOS.platform/Developer")
SET (SDKROOT "${DEVROOT}/SDKs/iPhoneOS${SDKVER}.sdk")
SET (CMAKE_OSX_SYSROOT "${SDKROOT}")
SET (CMAKE_OSX_ARCHITECTURES "$(ARCHS_UNIVERSAL_IPHONE_OS)")
 
#Other 'CMAKE_OSX_ARCHITECTURES' iPhone/IOS option examples
#SET (CMAKE_OSX_ARCHITECTURES "armv6" "armv7")
#SET (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
 
 
set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS
"-framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit"
)
link_directories(\${HOME}/\${SDKROOT}/lib)
 
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.mycompany.\${PRODUCT_NAME:identifier}")
set(APP_TYPE MACOSX_BUNDLE)
 
add_executable(${NAME}
${APP_TYPE}
${headers}
${sources}
)
 
target_link_libraries(${NAME}
# other libraries to link
)
 
# code signing
set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer: My Name")

#CMake-based IOS resource copying (see StackOverflow post )

Xcode doesn't seem to properly add resources when you build the project using CMake, so I this can be added to the CMakeLists.txt file:

# copy resource phase

set(APP_NAME \${TARGET_BUILD_DIR}/\${FULL_PRODUCT_NAME})
set(RES_DIR ${test_SOURCE_DIR}/data)
add_custom_command(
    TARGET ${NAME}
    POST_BUILD
    COMMAND /Developer/Library/PrivateFrameworks/DevToolsCore.framework/Resources/pbxcp -exclude .DS_Store -exclude CVS -exclude .svn -resolve-src-symlinks ${RES_DIR} ${APP_NAME}
)

This will copy the entire folder /data into the resources folder (as if we had added a "blue" folder link in Xcode).

Finding IOS Frameworks programmatically within CMake

The following macro snippet [supposedly] can be used in place of the 'CMAKE_EXE_LINKER_FLAGS' property and arguments

SET(TARGETSDK iPhoneOS4.1.sdk)
SET(CMAKE_OSX_SYSROOT /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/${TARGETSDK})
macro(ADD_FRAMEWORK fwname appname)
    find_library(FRAMEWORK_${fwname}
        NAMES ${fwname}
        PATHS ${CMAKE_OSX_SYSROOT}/System/Library
        PATH_SUFFIXES Frameworks
        NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
        MESSAGE(ERROR ": Framework ${fwname} not found")
    else()
        TARGET_LINK_LIBRARIES(${appname} ${FRAMEWORK_${fwname}})
        MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
    endif()
endmacro(ADD_FRAMEWORK)

Once the macro is in place, the framework can be added thus:

#It might be possible to use the ${NAME} variable value (i.e. substituted for the 'MyApp' name)
ADD_FRAMEWORK(AudioToolbox MyApp)
ADD_FRAMEWORK(CoreGraphics MyApp)
ADD_FRAMEWORK(QuartzCore MyApp)
ADD_FRAMEWORK(UIKit MyApp)

Making CMake produce universal Iphone binaries

Just set the architecture variable to:

set(CMAKE_OSX_ARCHITECTURES "$(ARCHS_UNIVERSAL_IPHONE_OS)")

This will generate armv6/armv7 etc binaries. There are some posts out there (stackoverflow etc.) which say that this has caused problems with iPad deployment, however, I am unable to verify that at present.

Load a file's content into a CMake variable (parsing properties is the grail...)

SET(cat_prog cat)
IF(WIN32)
  IF(NOT UNIX)
    SET(cat_prog type)
  ELSE(NOT UNIX)
ENDIF(WIN32)

EXEC_PROGRAM(${cat_prog} ARGS /input/file.txt OUTPUT_VARIABLE variable)

MESSAGE("Content of file is: ${variable}")

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.