Build cocos2d-x fat static library

from: http://www.nicnocquee.com/2016/01/20/build-cocos2d-x-fat-static-library.html

  1. Create a new empty project with targeting cocos2d-x version.
  2. Open project with Xcode to set build settings
  3. Go to <cocos2d-x_directory>/build
  4. Create a shell script file named buildstaticlib.sh as following:
    #!/bin/bash
    
    # This script is based on https://gist.github.com/rehos/7856163
    
    if [ -z "$1" ]; then
      echo
      echo "usage: $0 <project.xcodeproj> [<scheme>]"
      echo
      exit
    fi
    
    if [ ! -d "$1" ]; then
      echo
      echo "Error: $1 does not exist"
      echo
      exit
    fi
    
    MY_PROJECT="$1"
    MY_PROJECT_NAME=$(basename "$1")
    
    if [ -z "$2" ]; then
    	MY_SCHEME=${MY_PROJECT_NAME%.*}
    else
    	MY_SCHEME="$2"
    fi
    
    MY_STATIC_LIB="${MY_SCHEME}.a"
    MY_BUILD_PATH=`mktemp -d -t "build"`
    MY_SDK_VERSION=`xcrun --sdk iphoneos --show-sdk-version`
    
    # armv7, armv7s
    MY_ARM_BUILD_PATH="${MY_BUILD_PATH}/build-arm"
    MY_CURRENT_BUILD_PATH="${MY_ARM_BUILD_PATH}"
    xcodebuild -project "${MY_PROJECT}" -scheme "${MY_SCHEME}" -configuration 'Release' -sdk "iphoneos${MY_SDK_VERSION}" clean build ARCHS='armv7 armv7s' VALID_ARCHS='armv7 armv7s' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR="${MY_CURRENT_BUILD_PATH}" BUILT_PRODUCTS_DIR="${MY_CURRENT_BUILD_PATH}" OBJROOT="${MY_CURRENT_BUILD_PATH}" SYMROOT="${MY_CURRENT_BUILD_PATH}"
    
    # arm64
    MY_ARM64_BUILD_PATH="${MY_BUILD_PATH}/build-arm64"
    MY_CURRENT_BUILD_PATH="${MY_ARM64_BUILD_PATH}"
    xcodebuild -project "${MY_PROJECT}" -scheme "${MY_SCHEME}" -configuration 'Release' -sdk "iphoneos${MY_SDK_VERSION}" clean build ARCHS='arm64' VALID_ARCHS='arm64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR="${MY_CURRENT_BUILD_PATH}" BUILT_PRODUCTS_DIR="${MY_CURRENT_BUILD_PATH}" OBJROOT="${MY_CURRENT_BUILD_PATH}" SYMROOT="${MY_CURRENT_BUILD_PATH}"
    
    # i386
    MY_I386_BUILD_PATH="${MY_BUILD_PATH}/build-i386"
    MY_CURRENT_BUILD_PATH="${MY_I386_BUILD_PATH}"
    xcodebuild -project "${MY_PROJECT}" -scheme "${MY_SCHEME}" -configuration 'Release' -sdk "iphonesimulator${MY_SDK_VERSION}" clean build ARCHS='i386' VALID_ARCHS='i386' IPHONEOS_DEPLOYMENT_TARGET='5.0' TARGET_BUILD_DIR="${MY_CURRENT_BUILD_PATH}" BUILT_PRODUCTS_DIR="${MY_CURRENT_BUILD_PATH}" OBJROOT="${MY_CURRENT_BUILD_PATH}" SYMROOT="${MY_CURRENT_BUILD_PATH}"
    
    # x86_64
    MY_X86_64_BUILD_PATH="${MY_BUILD_PATH}/build-x86_64"
    MY_CURRENT_BUILD_PATH="${MY_X86_64_BUILD_PATH}"
    xcodebuild -project "${MY_PROJECT}" -scheme "${MY_SCHEME}" -configuration 'Release' -sdk "iphonesimulator${MY_SDK_VERSION}" clean build ARCHS='x86_64' VALID_ARCHS='x86_64' IPHONEOS_DEPLOYMENT_TARGET='7.0' TARGET_BUILD_DIR="${MY_CURRENT_BUILD_PATH}" BUILT_PRODUCTS_DIR="${MY_CURRENT_BUILD_PATH}" OBJROOT="${MY_CURRENT_BUILD_PATH}" SYMROOT="${MY_CURRENT_BUILD_PATH}"
    
    lipo -create "${MY_ARM_BUILD_PATH}/${MY_STATIC_LIB}" "${MY_ARM64_BUILD_PATH}/${MY_STATIC_LIB}" "${MY_I386_BUILD_PATH}/${MY_STATIC_LIB}" "${MY_X86_64_BUILD_PATH}/${MY_STATIC_LIB}" -output "${MY_STATIC_LIB}"
    
    rm -rf "${MY_BUILD_PATH}"
  5. Run the script: ./buildstaticlib.sh cocos2d_libs.xcodeproj “libcocos2d iOS”

Now the fat static library is prepared. From now on, when creating a new project, follow below instructions:

  1. Remove “cocos2d” directory under the project and make a symbolic link to the “cocos2d” directory created for the fat static library.
  2. Remove dependency of “libcocos2d iOS” from “Build Phases” > “Target Dependencies”
  3. Remove “libcocos2d iOS.a” in “Build Phases” > “Link Binary With Libraries” and add newly built “libcocos2d iOS.a” using “Add Other…” button.
  4. Add “libcocos2d iOS.a” containing directory to “Build Settings” > “Library Search Path” to locate static library.
  5. Within Scheme of the main project, uncheck “Find Implicit Dependencies” of the Build tab.
  6. clean library targets such as “libcocos2d iOS” to make sure the fat static library is used in build phase.

Following document also can be helpful: http://cocos2d-objc.org/migrating-to-x/creating-a-fat-static-library/