标签:
其实这个问题在网上能搜到大把的解决方案。大家的统一答案都是
otool -L yourapp.app/Contents/MacOS/yourapp
根据输出信息在运行 install_name_tool
install_name_tool的使用方法在这里
比较复杂的是当依赖了很多第三方库,尤其是向QT这样的库的时候。打包那叫一个麻烦啊。
QT有个官方的文档告诉你如何手动一步步打包程序,还提供了一个macdepotqt这么个程序。帮你进行打包。
可是啊,macdepotqt不支持其他第三方库,也不灵活,按照官方文档的方式手动打包,坑爹啊。
说他坑爹一点都不假,当你遇到了成片的符号需要你敲命令的话,想死的心情油然而生。
那么好了。自己动手吧。这里我给出一个我写的打包的Python脚本半成品,思想跟手动敲命令是一样的,只不过用脚本实现自动化了同时递归检查并将缺少的库考到bundle目录中。脚本中针对QT的库进行打包了,如果希望把其他的依赖库也打包的话在keywordList里边添加相应的关键字就好了。希望对大家能有帮助。
- import os
- import sys
- import commands
- binaryName = "yourapp"
- bundleName = "yourapp.app"
- bundleFrameworkDir = "Contents/Frameworks/"
- bundleBinaryDir = "Contents/MacOS/"
- bundleLibraryList = [];
- systemFrameworkDir = "/Library/Frameworks/"
- keyWordList = ["Qt"]
-
- def hasKeyWord(word):
- for it in keyWordList:
- if word.find(it) != -1:
- return True
- return False
-
-
- def findApp(name):
- return name+bundleBinaryDir+binaryName
-
-
- def getBundleDependsInfo(app):
- dependList = commands.getoutput("otool -L " + app).replace("\t","").split("\n");
- del(dependList[0])
- dependList = [item.split(" ")[0] for item in dependList if hasKeyWord(item)];
- return dependList
-
-
-
- def copyLibrary(base, src, dst):
- systemFullPath = src
- print "library %s depend %s" % (os.path.basename(base), os.path.basename(dst))
- if not os.path.exists(dst):
- bundleFullPath = os.path.dirname(dst)
- os.system("mkdir -p %s" % (bundleFullPath))
- os.system("cp %s %s" % (systemFullPath, bundleFullPath))
- infoList = getBundleDependsInfo(dst)
- copyDependFiles(dst, infoList)
- os.system("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))
- os.system("install_name_tool -change %s @executable_path/../Frameworks/%s %s" % (src, src, base))
-
-
- def getFrameworkName(dirname):
- if dirname.find("framework") == -1:
- return
- while not dirname.endswith("framework"):
- dirname = os.path.dirname(dirname)
- return dirname
-
-
- def copyFrameworkExtDir(src):
- sysPath = systemFrameworkDir + src
- destPath = ""
- if not os.path.exists(sysPath):
- return
- frameworkPath = getFrameworkName(sysPath)
- frameWorkName = getFrameworkName(src)
- bundlePath = bundleName + "/" + bundleFrameworkDir + frameWorkName + "/"
- for it in bundleFrameworkExtDir:
- destPath = bundlePath + it
- srcPath = frameworkPath + "/" + it
- if not os.path.exists(destPath) and os.path.exists(srcPath):
- print "copying %s %s" % (frameWorkName, it)
- os.system("cp -r %s %s" % (srcPath, destPath))
-
-
- def copyFramework(base, src, dst):
- print "framework %s depend %s" % (os.path.basename(base), os.path.basename(dst))
- systemFullPath = systemFrameworkDir+src
- if not os.path.exists(dst):
- bundleFullPath = os.path.dirname(dst)
- os.system("mkdir -p %s" % (bundleFullPath))
- os.system("cp %s %s" % (systemFullPath, bundleFullPath))
- copyFrameworkExtDir(src)
- infoList = getBundleDependsInfo(dst)
- copyDependFiles(dst, infoList)
- ("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))
- os.system("install_name_tool -id @executable_path/../Frameworks/%s %s" % (src, dst))
- os.system("install_name_tool -change %s @executable_path/../Frameworks/%s %s" % (src, src, base))
-
-
-
-
- def copyDependFiles(base, infoList):
- targetDir = ""
- for it in infoList:
- targetDir = bundleName + "/" + bundleFrameworkDir + it
- if it.find("framework") != -1:
- copyFramework(base, it, targetDir)
- else:
- copyLibrary(base, it, targetDir)
-
-
-
-
-
- def makeBundleDirs():
- os.system("mkdir -p " + bundleName + "/" + bundleFrameworkDir)
-
-
- if __name__ == "__main__":
- target = findApp(bundleName + "/")
- makeBundleDirs()
- infoList = getBundleDependsInfo(target)
- copyDependFiles(target, infoList)
http://blog.csdn.net/livemylife/article/details/7294778
mac下的应用程序发布 及 打包(Python写的脚本,可打包第三方库)
标签:
原文地址:http://www.cnblogs.com/findumars/p/5536460.html