Resolving CMake and CocoaPods Integration Issues in Xcode Projects

Introduction

Integrating CocoaPods into CMake-based iOS or macOS projects can be challenging. Developers often encounter issues where CocoaPods dependencies aren’t correctly linked, leading to build failures or incomplete archives. This blog post explores common problems in such integrations and provides solutions to ensure a smooth development experience.

Common Issues Encountered

1. CocoaPods Frameworks Not Found

After running pod install, you might notice that CocoaPods frameworks aren’t correctly linked in your CMake-generated Xcode project. This often results from incorrect FRAMEWORK_SEARCH_PATHS or LIBRARY_SEARCH_PATHS.

2. Empty Archives

When archiving the project in Xcode, the resulting .xcarchive might be empty or missing the .app bundle. This typically stems from misconfigured SKIP_INSTALL and INSTALL_PATH settings.

Steps Taken to Resolve the Issues

1. Adjusting Search Paths

To ensure that CocoaPods frameworks are correctly located, we set the FRAMEWORK_SEARCH_PATHS and LIBRARY_SEARCH_PATHS to point to the appropriate build directory:

set(POD_FRAMEWORK_PATH "$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)")
set_target_properties(${PROJECT_NAME} PROPERTIES
XCODE_ATTRIBUTE_FRAMEWORK_SEARCH_PATHS "${POD_FRAMEWORK_PATH}"
XCODE_ATTRIBUTE_LIBRARY_SEARCH_PATHS "${POD_FRAMEWORK_PATH}")

2. Configuring Build Location

To utilize Xcode’s DerivedData for build outputs, we set the following CMake attributes:

set(CMAKE_XCODE_ATTRIBUTE_SYMROOT "")
set(CMAKE_XCODE_ATTRIBUTE_OBJROOT "")
set(CMAKE_XCODE_ATTRIBUTE_CONFIGURATION_BUILD_DIR "")
set(CMAKE_XCODE_ATTRIBUTE_BUILD_LOCATION_STYLE "0")  # 0 = DerivedData

3. Ensuring Proper Archiving

To include the .app bundle in the archive, we configured the SKIP_INSTALL and INSTALL_PATH settings:

set_target_properties(${PROJECT_NAME} PROPERTIES
XCODE_ATTRIBUTE_SKIP_INSTALL "NO"
XCODE_ATTRIBUTE_INSTALL_PATH "$(LOCAL_APPS_DIR)"

This ensures that the application is correctly packaged during the archiving process.

Final Solution

By implementing the above configurations, we achieved a seamless integration of CocoaPods into our CMake-based Xcode project. The project now builds and archives correctly, with all dependencies properly linked and the .app bundle included in the archive.

Bonus: Recommended Xcode Attributes in CMake

For optimal integration, consider setting the following Xcode attributes in your CMakeLists.txt:

cmake
set_target_properties(${PROJECT_NAME} PROPERTIES
XCODE_ATTRIBUTE_SKIP_INSTALL “NO”
XCODE_ATTRIBUTE_INSTALL_PATH “$(LOCAL_APPS_DIR)”
XCODE_ATTRIBUTE_FRAMEWORK_SEARCH_PATHS “$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)”
XCODE_ATTRIBUTE_LIBRARY_SEARCH_PATHS “$(BUILD_DIR)/$(CONFIGURATION)$(EFFECTIVE_PLATFORM_NAME)”
XCODE_ATTRIBUTE_ASSETCATALOG_COMPILER_APPICON_NAME “AppIcon”
XCODE_ATTRIBUTE_MARKETING_VERSION “1.0”
XCODE_ATTRIBUTE_CURRENT_PROJECT_VERSION “100”
)