2012-06-28 23:13:57 +08:00
|
|
|
#!/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>
|
2012-08-07 17:29:43 +08:00
|
|
|
|
|
|
|
By cmake conventions (and especially if you work with OpenCV repository),
|
2012-06-28 23:13:57 +08:00
|
|
|
the output dir should not be a subdirectory of OpenCV source tree.
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
Script will create <outputdir>, if it's missing, and a few its subdirectories:
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
<outputdir>
|
|
|
|
build/
|
2012-11-01 22:15:48 +08:00
|
|
|
iPhoneOS-*/
|
2012-06-28 23:13:57 +08:00
|
|
|
[cmake-generated build tree for an iOS device target]
|
2014-10-16 04:23:39 +08:00
|
|
|
iPhoneSimulator-*/
|
2012-06-28 23:13:57 +08:00
|
|
|
[cmake-generated build tree for iOS simulator]
|
2012-11-01 22:15:48 +08:00
|
|
|
opencv2.framework/
|
2012-06-28 23:13:57 +08:00
|
|
|
[the framework content]
|
|
|
|
|
|
|
|
The script should handle minor OpenCV updates efficiently
|
|
|
|
- it does not recompile the library from scratch each time.
|
2012-11-01 22:15:48 +08:00
|
|
|
However, opencv2.framework directory is erased and recreated on each run.
|
2012-06-28 23:13:57 +08:00
|
|
|
"""
|
|
|
|
|
2014-12-05 22:48:28 +08:00
|
|
|
import glob, re, os, os.path, shutil, string, sys, exceptions, subprocess, argparse
|
|
|
|
|
|
|
|
opencv_contrib_path = None
|
2014-10-01 15:44:28 +08:00
|
|
|
|
|
|
|
def execute(cmd):
|
|
|
|
try:
|
|
|
|
print >>sys.stderr, "Executing:", cmd
|
|
|
|
retcode = subprocess.call(cmd, shell=True)
|
|
|
|
if retcode < 0:
|
|
|
|
raise Exception("Child was terminated by signal:", -retcode)
|
|
|
|
elif retcode > 0:
|
|
|
|
raise Exception("Child returned:", retcode)
|
|
|
|
except OSError as e:
|
|
|
|
raise Exception("Execution failed:", e)
|
2012-06-28 23:13:57 +08:00
|
|
|
|
2012-10-19 22:56:12 +08:00
|
|
|
def build_opencv(srcroot, buildroot, target, arch):
|
2012-06-28 23:13:57 +08:00
|
|
|
"builds OpenCV for device or simulator"
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-10-19 22:56:12 +08:00
|
|
|
builddir = os.path.join(buildroot, target + '-' + arch)
|
2012-06-28 23:13:57 +08:00
|
|
|
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 " +
|
2013-06-19 04:20:21 +08:00
|
|
|
"-DCMAKE_TOOLCHAIN_FILE=%s/platforms/ios/cmake/Toolchains/Toolchain-%s_Xcode.cmake " +
|
2014-04-18 19:42:47 +08:00
|
|
|
"-DCMAKE_C_FLAGS=\"-Wno-implicit-function-declaration\" " +
|
2012-06-28 23:13:57 +08:00
|
|
|
"-DCMAKE_INSTALL_PREFIX=install") % (srcroot, target)
|
2014-10-14 02:34:15 +08:00
|
|
|
|
|
|
|
if arch.startswith("armv"):
|
|
|
|
cmakeargs += " -DENABLE_NEON=ON"
|
|
|
|
|
2014-12-05 22:48:28 +08:00
|
|
|
if opencv_contrib_path is not None:
|
2015-01-19 23:03:29 +08:00
|
|
|
cmakeargs += " -DCMAKE_SKIP_INSTALL_ALL_DEPENDENCY=ON -DOPENCV_EXTRA_MODULES_PATH=%s -DBUILD_opencv_contrib_world=ON" % opencv_contrib_path
|
|
|
|
build_target = "opencv_contrib_world"
|
|
|
|
libname = "libopencv_contrib_world.a"
|
|
|
|
else:
|
|
|
|
cmakeargs += " -DBUILD_opencv_world=ON"
|
|
|
|
build_target = "ALL_BUILD"
|
|
|
|
libname = "libopencv_world.a"
|
2014-12-05 22:48:28 +08:00
|
|
|
|
2014-10-16 04:23:39 +08:00
|
|
|
# if cmake cache exists, just rerun cmake to update OpenCV.xcodeproj if necessary
|
2012-06-28 23:13:57 +08:00
|
|
|
if os.path.isfile(os.path.join(builddir, "CMakeCache.txt")):
|
2014-10-01 15:44:28 +08:00
|
|
|
execute("cmake %s ." % (cmakeargs,))
|
2012-06-28 23:13:57 +08:00
|
|
|
else:
|
2014-10-01 15:44:28 +08:00
|
|
|
execute("cmake %s %s" % (cmakeargs, srcroot))
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2015-01-19 23:03:29 +08:00
|
|
|
for wlib in [builddir + "/modules/world/UninstalledProducts/" + libname,
|
|
|
|
builddir + "/lib/Release/" + libname]:
|
2012-06-28 23:13:57 +08:00
|
|
|
if os.path.isfile(wlib):
|
|
|
|
os.remove(wlib)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2015-01-19 23:03:29 +08:00
|
|
|
execute("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target %s" % (arch, target.lower(), build_target))
|
2014-10-01 15:44:28 +08:00
|
|
|
execute("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
|
2012-06-28 23:13:57 +08:00
|
|
|
os.chdir(currdir)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
def put_framework_together(srcroot, dstroot):
|
|
|
|
"constructs the framework directory after all the targets are built"
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2014-12-05 22:48:28 +08:00
|
|
|
name = "opencv2" if opencv_contrib_path is None else "opencv2_contrib"
|
2015-01-19 23:03:29 +08:00
|
|
|
libname = "libopencv_world.a" if opencv_contrib_path is None else "libopencv_contrib_world.a"
|
2014-12-05 22:48:28 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
# 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]
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
# set the current dir to the dst root
|
|
|
|
currdir = os.getcwd()
|
2014-12-05 22:48:28 +08:00
|
|
|
framework_dir = dstroot + "/%s.framework" % name
|
2012-06-28 23:13:57 +08:00
|
|
|
if os.path.isdir(framework_dir):
|
|
|
|
shutil.rmtree(framework_dir)
|
|
|
|
os.makedirs(framework_dir)
|
|
|
|
os.chdir(framework_dir)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
# form the directory tree
|
|
|
|
dstdir = "Versions/A"
|
|
|
|
os.makedirs(dstdir + "/Resources")
|
|
|
|
|
2013-08-23 16:51:50 +08:00
|
|
|
tdir0 = "../build/" + targetlist[0]
|
2012-06-28 23:13:57 +08:00
|
|
|
# copy headers
|
|
|
|
shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers")
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
# make universal static lib
|
2015-01-19 23:03:29 +08:00
|
|
|
wlist = " ".join(["../build/" + t + "/lib/Release/" + libname for t in targetlist])
|
2014-12-05 22:48:28 +08:00
|
|
|
execute("lipo -create " + wlist + " -o " + dstdir + "/%s" % name)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2013-08-06 22:56:36 +08:00
|
|
|
# copy Info.plist
|
2013-08-23 16:51:50 +08:00
|
|
|
shutil.copyfile(tdir0 + "/ios/Info.plist", dstdir + "/Resources/Info.plist")
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
# make symbolic links
|
|
|
|
os.symlink("A", "Versions/Current")
|
2013-03-25 00:07:55 +08:00
|
|
|
os.symlink("Versions/Current/Headers", "Headers")
|
|
|
|
os.symlink("Versions/Current/Resources", "Resources")
|
2014-12-05 22:48:28 +08:00
|
|
|
os.symlink("Versions/Current/%s" % name, name)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
def build_framework(srcroot, dstroot):
|
|
|
|
"main function to do all the work"
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2014-10-01 15:44:28 +08:00
|
|
|
targets = [("armv7", "iPhoneOS"),
|
|
|
|
("armv7s", "iPhoneOS"),
|
|
|
|
("arm64", "iPhoneOS"),
|
|
|
|
("i386", "iPhoneSimulator"),
|
|
|
|
("x86_64", "iPhoneSimulator")]
|
|
|
|
for t in targets:
|
|
|
|
build_opencv(srcroot, os.path.join(dstroot, "build"), t[1], t[0])
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
put_framework_together(srcroot, dstroot)
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2012-06-28 23:13:57 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2014-12-05 22:48:28 +08:00
|
|
|
parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for iOS.')
|
|
|
|
parser.add_argument('outputdir', nargs=1, help='folder to put built framework')
|
|
|
|
parser.add_argument('--contrib', help="folder with opencv_contrib repository")
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# path to OpenCV main repository - hardcoded ../..
|
|
|
|
opencv_path = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../.."))
|
|
|
|
print "OpenCV:", opencv_path
|
|
|
|
|
|
|
|
# path to OpenCV_contrib repository, can be empty - global variable
|
|
|
|
if hasattr(args, "contrib") and args.contrib is not None:
|
|
|
|
if os.path.isdir(args.contrib + "/modules"):
|
|
|
|
opencv_contrib_path = os.path.abspath(args.contrib + "/modules")
|
|
|
|
print "Contrib:", opencv_contrib_path
|
|
|
|
else:
|
|
|
|
print "Note: contrib repository is bad: modules subfolder not found"
|
|
|
|
|
|
|
|
# result path - folder where framework will be located
|
|
|
|
output_path = os.path.abspath(args.outputdir[0])
|
|
|
|
print "Output:", output_path
|
2012-08-07 17:29:43 +08:00
|
|
|
|
2014-10-01 15:44:28 +08:00
|
|
|
try:
|
2014-12-05 22:48:28 +08:00
|
|
|
build_framework(opencv_path, output_path)
|
2014-10-01 15:44:28 +08:00
|
|
|
except Exception as e:
|
|
|
|
print >>sys.stderr, e
|
|
|
|
sys.exit(1)
|