mirror of https://github.com/opencv/opencv.git
Open Source Computer Vision Library
https://opencv.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
125 lines
4.7 KiB
125 lines
4.7 KiB
13 years ago
|
#!/usr/bin/env python
|
||
|
"""
|
||
|
The script builds OpenCV.framework for iOS.
|
||
|
The built framework is universal, it can be used to build app and run it on either iOS simulator or real device.
|
||
|
|
||
|
Usage:
|
||
|
./build_framework.py <outputdir>
|
||
13 years ago
|
|
||
|
By cmake conventions (and especially if you work with OpenCV repository),
|
||
13 years ago
|
the output dir should not be a subdirectory of OpenCV source tree.
|
||
13 years ago
|
|
||
13 years ago
|
Script will create <outputdir>, if it's missing, and a few its subdirectories:
|
||
13 years ago
|
|
||
13 years ago
|
<outputdir>
|
||
|
build/
|
||
12 years ago
|
iPhoneOS-*/
|
||
13 years ago
|
[cmake-generated build tree for an iOS device target]
|
||
|
iPhoneSimulator/
|
||
|
[cmake-generated build tree for iOS simulator]
|
||
12 years ago
|
opencv2.framework/
|
||
13 years ago
|
[the framework content]
|
||
|
|
||
|
The script should handle minor OpenCV updates efficiently
|
||
|
- it does not recompile the library from scratch each time.
|
||
12 years ago
|
However, opencv2.framework directory is erased and recreated on each run.
|
||
13 years ago
|
"""
|
||
|
|
||
|
import glob, re, os, os.path, shutil, string, sys
|
||
|
|
||
12 years ago
|
def build_opencv(srcroot, buildroot, target, arch):
|
||
13 years ago
|
"builds OpenCV for device or simulator"
|
||
13 years ago
|
|
||
12 years ago
|
builddir = os.path.join(buildroot, target + '-' + arch)
|
||
13 years ago
|
if not os.path.isdir(builddir):
|
||
|
os.makedirs(builddir)
|
||
|
currdir = os.getcwd()
|
||
|
os.chdir(builddir)
|
||
|
# for some reason, if you do not specify CMAKE_BUILD_TYPE, it puts libs to "RELEASE" rather than "Release"
|
||
|
cmakeargs = ("-GXcode " +
|
||
|
"-DCMAKE_BUILD_TYPE=Release " +
|
||
12 years ago
|
"-DCMAKE_TOOLCHAIN_FILE=%s/platforms/ios/cmake/Toolchains/Toolchain-%s_Xcode.cmake " +
|
||
13 years ago
|
"-DBUILD_opencv_world=ON " +
|
||
|
"-DCMAKE_INSTALL_PREFIX=install") % (srcroot, target)
|
||
|
# if cmake cache exists, just rerun cmake to update OpenCV.xproj if necessary
|
||
|
if os.path.isfile(os.path.join(builddir, "CMakeCache.txt")):
|
||
|
os.system("cmake %s ." % (cmakeargs,))
|
||
|
else:
|
||
|
os.system("cmake %s %s" % (cmakeargs, srcroot))
|
||
13 years ago
|
|
||
13 years ago
|
for wlib in [builddir + "/modules/world/UninstalledProducts/libopencv_world.a",
|
||
|
builddir + "/lib/Release/libopencv_world.a"]:
|
||
|
if os.path.isfile(wlib):
|
||
|
os.remove(wlib)
|
||
13 years ago
|
|
||
12 years ago
|
os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
|
||
|
os.system("xcodebuild ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
|
||
13 years ago
|
os.chdir(currdir)
|
||
13 years ago
|
|
||
13 years ago
|
def put_framework_together(srcroot, dstroot):
|
||
|
"constructs the framework directory after all the targets are built"
|
||
13 years ago
|
|
||
13 years ago
|
# find the list of targets (basically, ["iPhoneOS", "iPhoneSimulator"])
|
||
|
targetlist = glob.glob(os.path.join(dstroot, "build", "*"))
|
||
|
targetlist = [os.path.basename(t) for t in targetlist]
|
||
13 years ago
|
|
||
13 years ago
|
# set the current dir to the dst root
|
||
|
currdir = os.getcwd()
|
||
|
framework_dir = dstroot + "/opencv2.framework"
|
||
|
if os.path.isdir(framework_dir):
|
||
|
shutil.rmtree(framework_dir)
|
||
|
os.makedirs(framework_dir)
|
||
|
os.chdir(framework_dir)
|
||
13 years ago
|
|
||
13 years ago
|
# determine OpenCV version (without subminor part)
|
||
|
tdir0 = "../build/" + targetlist[0]
|
||
|
cfg = open(tdir0 + "/cvconfig.h", "rt")
|
||
|
for l in cfg.readlines():
|
||
|
if l.startswith("#define VERSION"):
|
||
|
opencv_version = l[l.find("\"")+1:l.rfind(".")]
|
||
|
break
|
||
|
cfg.close()
|
||
13 years ago
|
|
||
13 years ago
|
# form the directory tree
|
||
|
dstdir = "Versions/A"
|
||
|
os.makedirs(dstdir + "/Resources")
|
||
|
|
||
|
# copy headers
|
||
|
shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers")
|
||
13 years ago
|
|
||
13 years ago
|
# make universal static lib
|
||
|
wlist = " ".join(["../build/" + t + "/lib/Release/libopencv_world.a" for t in targetlist])
|
||
|
os.system("lipo -create " + wlist + " -o " + dstdir + "/opencv2")
|
||
13 years ago
|
|
||
13 years ago
|
# form Info.plist
|
||
12 years ago
|
srcfile = open(srcroot + "/platforms/ios/Info.plist.in", "rt")
|
||
13 years ago
|
dstfile = open(dstdir + "/Resources/Info.plist", "wt")
|
||
|
for l in srcfile.readlines():
|
||
|
dstfile.write(l.replace("${VERSION}", opencv_version))
|
||
|
srcfile.close()
|
||
|
dstfile.close()
|
||
13 years ago
|
|
||
13 years ago
|
# make symbolic links
|
||
|
os.symlink("A", "Versions/Current")
|
||
12 years ago
|
os.symlink("Versions/Current/Headers", "Headers")
|
||
|
os.symlink("Versions/Current/Resources", "Resources")
|
||
|
os.symlink("Versions/Current/opencv2", "opencv2")
|
||
13 years ago
|
|
||
|
|
||
13 years ago
|
def build_framework(srcroot, dstroot):
|
||
|
"main function to do all the work"
|
||
13 years ago
|
|
||
12 years ago
|
targets = ["iPhoneOS", "iPhoneOS", "iPhoneSimulator"]
|
||
|
archs = ["armv7", "armv7s", "i386"]
|
||
|
for i in range(len(targets)):
|
||
|
build_opencv(srcroot, os.path.join(dstroot, "build"), targets[i], archs[i])
|
||
13 years ago
|
|
||
13 years ago
|
put_framework_together(srcroot, dstroot)
|
||
13 years ago
|
|
||
13 years ago
|
|
||
|
if __name__ == "__main__":
|
||
|
if len(sys.argv) != 2:
|
||
|
print "Usage:\n\t./build_framework.py <outputdir>\n\n"
|
||
|
sys.exit(0)
|
||
13 years ago
|
|
||
12 years ago
|
build_framework(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")), os.path.abspath(sys.argv[1]))
|