Makefile for XCode 4.6 and iOS SDK 6.1

2013-02-20
2013-07-02
  • Hello,

    had to change makefile to compile FreeImage for iOS using SDK 6.1. Relates to this post.

    Changes:
    1. Compiler from gcc to llvm-gcc
    2. Compiler from g++ to llvm-g++
    3. Developer directory is under /Applications/XCode.app/Contents/Developer instead of /Developer
    4. ARCH_PHONE is used when using libtool too
    5. Difference between deployment architecture (minimum OS version) and SDK. Created variables for SDK (IPHONEOS_SDK and MACOSX_SDK).
    6. Changed deployment target to iOS 5.1, and changed accordingly D__IPHONE_OS_VERSION_MIN_REQUIRED from 20000 to 50000

    # Configuration for iPhone OS, making static libs
    # this will generate both iPhone (arm) and iPhoneSimulator (i686) libs
    
    include Makefile.srcs
    
    CFLAGS =  -g -O2 -Wall -Wmissing-prototypes -std=c99 -ffast-math -fno-strict-aliasing
    CXXFLAGS =  -g -O2 -Wall -fno-strict-aliasing
    
    GCC_VERSION = 4.2
    IPHONEOS_SDK = 6.1
    IPHONEOS_DEPLOYMENT_TARGET = 5.1
    MACOSX_SDK = 10.8
    MACOSX_DEPLOYMENT_TARGET = 10.6
    
    PLATFORM_SIM = iPhoneSimulator
    PLATFORM_PHONE = iPhoneOS
    
    ARCH_SIM = i686
    ARCH_PHONE = armv7
    
    DEVELOPER=/Applications/Xcode.app/Contents/Developer
    PLATFORM_SIM_DEVELOPER_BIN_DIR = $(DEVELOPER)/Platforms/$(PLATFORM_SIM).platform/Developer/usr/bin
    PLATFORM_PHONE_DEVELOPER_BIN_DIR = $(DEVELOPER)/Platforms/$(PLATFORM_PHONE).platform/Developer/usr/bin
    
    SDKROOT_SIM = $(DEVELOPER)/Platforms/$(PLATFORM_SIM).platform/Developer/SDKs/$(PLATFORM_SIM)$(IPHONEOS_SDK).sdk
    SDKROOT_PHONE = $(DEVELOPER)/Platforms/$(PLATFORM_PHONE).platform/Developer/SDKs/$(PLATFORM_PHONE)$(IPHONEOS_SDK).sdk
    
    EXTRA_CFLAGS_SIM += -arch $(ARCH_SIM) -pipe -mdynamic-no-pic -fvisibility=hidden $(INCLUDE) -isysroot $(SDKROOT_SIM)
    EXTRA_LDFLAGS_SIM += -arch $(ARCH_SIM) -isysroot $(SDKROOT_SIM) -Wl,-dead_strip
    EXTRA_CFLAGS_SIM += -D__IPHONE_OS_VERSION_MIN_REQUIRED=50000 -mmacosx-version-min=$(MACOSX_DEPLOYMENT_TARGET)
    EXTRA_LDFLAGS_SIM += -mmacosx-version-min=$(MACOSX_DEPLOYMENT_TARGET)
    
    EXTRA_CFLAGS_PHONE += -arch $(ARCH_PHONE) -pipe -mdynamic-no-pic -fvisibility=hidden $(INCLUDE) -isysroot $(SDKROOT_PHONE)
    EXTRA_LDFLAGS_PHONE += -arch $(ARCH_PHONE) -isysroot $(SDKROOT_PHONE) -Wl,-dead_strip
    EXTRA_CFLAGS_PHONE += -miphoneos-version-min=$(IPHONEOS_DEPLOYMENT_TARGET)
    EXTRA_LDFLAGS_PHONE += -miphoneos-version-min=$(IPHONEOS_DEPLOYMENT_TARGET)
    
    AR_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/ar
    AR_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/ar
    
    CC_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/llvm-gcc-$(GCC_VERSION)
    CC_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/llvm-gcc-$(GCC_VERSION)
    
    CFLAGS_SIM = $(CFLAGS) $(EXTRA_CFLAGS_SIM)
    LDFLAGS_SIM = $(EXTRA_LDFLAGS_SIM)
    CXX_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/llvm-g++-$(GCC_VERSION)
    CXXFLAGS_SIM += $(CXXFLAGS) $(EXTRA_CFLAGS_SIM) -fvisibility-inlines-hidden
    LIBTOOL_SIM = $(DEVELOPER)/Platforms/$(PLATFORM_SIM).platform/Developer/usr/bin/libtool
    
    CFLAGS_PHONE = $(CFLAGS) $(EXTRA_CFLAGS_PHONE)
    LDFLAGS_PHONE += $(EXTRA_LDFLAGS_PHONE)
    CXX_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/llvm-g++-$(GCC_VERSION)
    CXXFLAGS_PHONE += $(CXXFLAGS) $(EXTRA_CFLAGS_PHONE) -fvisibility-inlines-hidden
    LIBTOOL_PHONE = $(DEVELOPER)/Platforms/$(PLATFORM_PHONE).platform/Developer/usr/bin/libtool
    
    TARGET = freeimage
    STATICLIB_SIM = lib$(TARGET)-iphonesimulator.a
    STATICLIB_PHONE = lib$(TARGET)-iphone.a
    HEADER = Source/FreeImage.h
    
    .SUFFIXES: .o-i686 .o-arm
    MODULES_ARM = $(SRCS:.c=.o-arm)
    MODULES_ARM := $(MODULES_ARM:.cpp=.o-arm)
    MODULES_i686 = $(SRCS:.c=.o-i686)
    MODULES_i686 := $(MODULES_i686:.cpp=.o-i686)
    
    default: all
    
    all: dist
    
    dist: FreeImage
        cp *.a Dist
        cp Source/FreeImage.h Dist
    
    FreeImage: $(STATICLIB_SIM) $(STATICLIB_PHONE)
    
    $(STATICLIB_SIM): $(MODULES_i686)
        $(LIBTOOL_SIM) -arch_only i686 -o $@ $(MODULES_i686)
    
    .c.o-i686:
        $(CC_SIM) $(CFLAGS_SIM) -c $< -o $@
    
    .cpp.o-i686:
        $(CXX_SIM) $(CXXFLAGS_SIM) -c $< -o $@
    
    $(STATICLIB_PHONE): $(MODULES_ARM)
        $(LIBTOOL_PHONE) -arch_only $(ARCH_PHONE) -o $@ $(MODULES_ARM)
    
    .c.o-arm:
        $(CC_PHONE) $(CFLAGS_PHONE) -c $< -o $@
    
    .cpp.o-arm:
        $(CXX_PHONE) $(CXXFLAGS_PHONE) -c $< -o $@
    
    clean:
        rm -f core Dist/*.* u2dtmp* $(MODULES_i686) $(MODULES_ARM) $(STATICLIB_SIM) $(STATICLIB_PHONE)
    
     
    Last edit: Miguel Angel Quinones 2013-02-20
    • danomatika
      danomatika
      2013-07-02

      Hah, I found this after basically doing the same thing.

      https://gist.github.com/danomatika/5911188

      I incorporated some of these changes and added the following:

      1.use xcode-select to get the Xcode developer root, so the Makefile will still work with older versions of XCode
      2. use suffix based on $ARCH_PHONE, this allows easy building of multiple versions of the lib for different ARM architectures: armv6, armv7, armv7s, etc
      3. added comment documenting how to build for multiple archs and link them into a fat lib