使用Makefile自动编译iPhone程序

Xcode 也支持以命令行形式来编译 iPhone 程序。另外还可以手动的编写 Makefile 文件,实现编译→安装的自动化批处理过程。如果你习惯了命令行的操作方式(linux,unix),那么这样的操作还是很方便的。

首先看看 Xcode 的命令行格式:

  1. xcodebuild -target Project_Name
  2. xcodebuild install -target Project_Name

下面我们来实现程序的编译,并通过 ldid 转换编码格式,最后用 ssh 将编译好的程序安装到 iPhone 上的 /Applications/目录下。

首先安装 ssh 的公开密匙到 iPhone 上

1). 在Mac的终端上产生密匙

  1. ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/home/xxxx/.ssh/id_rsa):
  4. Created directory '/home/xxxx/.ssh'.
  5. Enter passphrase (empty for no passphrase): xxx
  6. Enter same passphrase again: xxx
  7. Your identification has been saved in /home/xxxx/.ssh/id_rsa.
  8. Your public key has been saved in /home/xxxx/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. e4:e8:b7:05:06:b3:f0:ff:af:13:fc:50:6a:5b:d1:b5 [email protected]

 

过程中会提问你通行证(passphrase),输入你常用的秘密。

2). 在 iPhone 上创建.ssh目录(iPhone的IP地址是10.0.2.2)

 

  1. ssh [email protected] 'mkdir -p .ssh'

如果问道你iPhone root password,输入 alpine。

3). 拷贝刚才生成的公开密匙到 iPhone

 

  1. cat ~/.ssh/id_rsa.pub | ssh [email protected] 'cat >> .ssh/authorized_keys'

如果问道你iPhone root password,输入 alpine。

4). 在 iPhone 上编辑 /etc/ssh/sshd_config 文件

  1. #将
  2.  
  3. #StrictModes yes
  4. #PubkeyAuthentication yes
  5. #AuthorizedKeysFile .ssh/authorized_keys
  6.  
  7. #替换为
  8.  
  9. StrictModes no
  10. PubkeyAuthentication yes
  11. AuthorizedKeysFile .ssh/authorized_keys

5). 重新启动iPhone

接下来,编译生成ldid工具

  1. wget http://svn.telesphoreo.org/trunk/data/ldid/ldid-1.0.610.tgz
  2.  
  3. tar -zxf ldid-1.0.610.tgz
  4.  
  5. # 如果是 PowerPC 下载下面的补丁
  6. # wget -qO- http://fink.cvs.sourceforge.net/viewvcfink/dists/10.4/unstable/crypto/finkinfo/ldid.patch?revision=1.1 | patch -p0
  7.  
  8. cd ldid-1.0.610
  9.  
  10. g++ -I . -o util/ldid{,.cpp} -x c util/{lookup2,sha1}.c
  11.  
  12. sudo cp -a util/ldid /usr/bin

 

最后,让我们看看Makefile中都有什么

项目中的文件如下所示:

Classes : source code (.m .c .cpp etc)Resources : png file and other support filesProject folder : *.xib Info.plist
   

  1. LDFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks" LDFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFra
  2. LDFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks"
  3. LDFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFrameworks"
  4.  
  5. ## 编译开关
  6. CFLAGS  += $(INCPATH) \
  7.         -std=c99 \
  8.         -W -Wall \
  9.         -funroll-loops \
  10.         -Diphoneos_version_min=2.0 \
  11.         -Wno-unused-parameter \
  12.         -Wno-sign-compare
  13.  
  14. ifeq ($(DEBUG), y)
  15. CFLAGS  += -O0 -g -DDEBUG_MUTEX
  16. else
  17. CFLAGS  += -O3 -DNDEBUG
  18. ifeq ($(DEVEL), y)
  19. CFLAGS  += -g
  20. endif
  21. endif
  22. CFLAGS += -F"$(IPHONESDK)/System/Library/Frameworks"
  23. CFLAGS += -F"$(IPHONESDK)/System/Library/PrivateFrameworks"
  24.  
  25. ####################################################################################
  26.  
  27. BUILDDIR =./build/3.0
  28. SRCDIR   =./Classes
  29. RESDIR   =./Resources
  30.  
  31. ###/////////////////////////////////////////////////////////////
  32. ###                        Source files
  33. ###/////////////////////////////////////////////////////////////
  34.  
  35. OBJS     = $(patsubst %.m,%.o,$(wildcard $(SRCDIR)/*.m))
  36. OBJS    += $(patsubst %.m,%.o,$(wildcard ./*.m))
  37. OBJS    += $(patsubst %.c,%.o,$(wildcard $(SRCDIR)/*.c))
  38. OBJS    += $(patsubst %.cpp,%.o,$(wildcard $(SRCDIR)/*.cpp))
  39.  
  40. NIBS     = $(patsubst %.xib,%.nib,$(wildcard *.xib))
  41.  
  42. RESOURCES= $(wildcard $(RESDIR)/*)
  43.  
  44. APPFOLDER= $(TARGET).app
  45.  
  46. .PHONY: all
  47. all: $(TARGET) bundle
  48.  
  49. $(TARGET): $(OBJS)
  50.     $(LD) $(LDFLAGS) -o $@ $^
  51.  
  52. %.o:    %.m
  53.     $(CC) -c $(CFLAGS) [        DISCUZ_CODE_157        ]lt; -o $@
  54.  
  55. %.o:    %.c
  56.     $(CC) -c $(CFLAGS) [        DISCUZ_CODE_157        ]lt; -o $@
  57.  
  58. %.o:    %.cpp
  59.     $(CXX) -x objective-c++ $(CFLAGS) [        DISCUZ_CODE_157        ]lt; -o $@
  60.  
  61. %.nib:  %.xib
  62.     ibtool [        DISCUZ_CODE_157        ]lt; --compile $@
  63.  
  64. bundle: $(TARGET)
  65.     @rm -rf $(BUILDDIR)
  66.     @mkdir -p $(BUILDDIR)/$(APPFOLDER)
  67.     @cp -r $(RESDIR)/* $(BUILDDIR)/$(APPFOLDER)
  68.     @cp Info.plist $(BUILDDIR)/$(APPFOLDER)/Info.plist
  69.     @echo "APPL" > $(BUILDDIR)/$(APPFOLDER)/PkgInfo
  70.     mv $(NIBS) $(BUILDDIR)/$(APPFOLDER)
  71. #    export CODESIGN_ALLOCATE=/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/codesign_
  72. allocate
  73.  
  74. @ldid -S $(TARGET)
  75.     @mv $(TARGET) $(BUILDDIR)/$(APPFOLDER)/$(TARGET)_
  76.  
  77.  
  78. install: bundle
  79.     @ssh root@$(IP) "cd /Applications/$(APPFOLDER) && rm -R * || echo 'not found' "
  80.     @scp -rp $(BUILDDIR)/$(APPFOLDER) root@$(IP):/Applications
  81.     @ssh root@$(IP) "cd /Applications/$(APPFOLDER) ; ldid -S $(TARGET)_; killall SpringBoard"
  82.     @echo "Application $(APPFOLDER) installed"
  83.  
  84. uninstall:
  85.     ssh root@$(IPHONE_IP) 'rm -fr /Applications/$(APPFOLDER); respring'
  86.     @echo "Application $(APPFOLDER) uninstalled, please respring iPhone"
  87.  
  88. install_respring:
  89.     scp respring_arm root@$(IPHONE_IP):/usr/bin/respring
  90.  
  91. .PHONY: clean
  92. clean:
  93.     @rm -f $(OBJS) $(TARGET)
  94.     @rm -rf $(BUILDDIR)

 

然后执行下面的make命令,我们就可以直接在 iPhone 上测试我们的程序了。

 

  1. make install_respring
  2. make
  3. make install

你可能感兴趣的:(makefile)