3 The script builds OpenCV.framework for iOS.
4 The built framework is universal, it can be used to build app and run it on either iOS simulator or real device.
7 ./build_framework.py <outputdir>
9 By cmake conventions (and especially if you work with OpenCV repository),
10 the output dir should not be a subdirectory of OpenCV source tree.
12 Script will create <outputdir>, if it's missing, and a few its subdirectories:
17 [cmake-generated build tree for an iOS device target]
19 [cmake-generated build tree for iOS simulator]
21 [the framework content]
23 The script should handle minor OpenCV updates efficiently
24 - it does not recompile the library from scratch each time.
25 However, opencv2.framework directory is erased and recreated on each run.
28 import glob, re, os, os.path, shutil, string, sys
30 def build_opencv(srcroot, buildroot, target, arch):
31 "builds OpenCV for device or simulator"
33 builddir = os.path.join(buildroot, target + '-' + arch)
34 if not os.path.isdir(builddir):
38 # for some reason, if you do not specify CMAKE_BUILD_TYPE, it puts libs to "RELEASE" rather than "Release"
39 cmakeargs = ("-GXcode " +
40 "-DCMAKE_BUILD_TYPE=Release " +
41 "-DCMAKE_TOOLCHAIN_FILE=%s/platforms/ios/cmake/Toolchains/Toolchain-%s_Xcode.cmake " +
42 "-DBUILD_opencv_world=ON " +
43 "-DCMAKE_INSTALL_PREFIX=install") % (srcroot, target)
44 # if cmake cache exists, just rerun cmake to update OpenCV.xproj if necessary
45 if os.path.isfile(os.path.join(builddir, "CMakeCache.txt")):
46 os.system("cmake %s ." % (cmakeargs,))
48 os.system("cmake %s %s" % (cmakeargs, srcroot))
50 for wlib in [builddir + "/modules/world/UninstalledProducts/libopencv_world.a",
51 builddir + "/lib/Release/libopencv_world.a"]:
52 if os.path.isfile(wlib):
55 os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
56 os.system("xcodebuild ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
59 def put_framework_together(srcroot, dstroot):
60 "constructs the framework directory after all the targets are built"
62 # find the list of targets (basically, ["iPhoneOS", "iPhoneSimulator"])
63 targetlist = glob.glob(os.path.join(dstroot, "build", "*"))
64 targetlist = [os.path.basename(t) for t in targetlist]
66 # set the current dir to the dst root
68 framework_dir = dstroot + "/opencv2.framework"
69 if os.path.isdir(framework_dir):
70 shutil.rmtree(framework_dir)
71 os.makedirs(framework_dir)
72 os.chdir(framework_dir)
74 # determine OpenCV version (without subminor part)
75 tdir0 = "../build/" + targetlist[0]
76 cfg = open(tdir0 + "/cvconfig.h", "rt")
77 for l in cfg.readlines():
78 if l.startswith("#define VERSION"):
79 opencv_version = l[l.find("\"")+1:l.rfind(".")]
83 # form the directory tree
85 os.makedirs(dstdir + "/Resources")
88 shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers")
90 # make universal static lib
91 wlist = " ".join(["../build/" + t + "/lib/Release/libopencv_world.a" for t in targetlist])
92 os.system("lipo -create " + wlist + " -o " + dstdir + "/opencv2")
95 srcfile = open(srcroot + "/platforms/ios/Info.plist.in", "rt")
96 dstfile = open(dstdir + "/Resources/Info.plist", "wt")
97 for l in srcfile.readlines():
98 dstfile.write(l.replace("${VERSION}", opencv_version))
102 # make symbolic links
103 os.symlink("A", "Versions/Current")
104 os.symlink("Versions/Current/Headers", "Headers")
105 os.symlink("Versions/Current/Resources", "Resources")
106 os.symlink("Versions/Current/opencv2", "opencv2")
109 def build_framework(srcroot, dstroot):
110 "main function to do all the work"
112 targets = ["iPhoneOS", "iPhoneOS", "iPhoneSimulator"]
113 archs = ["armv7", "armv7s", "i386"]
114 for i in range(len(targets)):
115 build_opencv(srcroot, os.path.join(dstroot, "build"), targets[i], archs[i])
117 put_framework_together(srcroot, dstroot)
120 if __name__ == "__main__":
121 if len(sys.argv) != 2:
122 print "Usage:\n\t./build_framework.py <outputdir>\n\n"
125 build_framework(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")), os.path.abspath(sys.argv[1]))