Skip to content

Instantly share code, notes, and snippets.

@atinfinity
Last active November 4, 2015 10:05
Show Gist options
  • Save atinfinity/b7461d4f2dddc018be4d to your computer and use it in GitHub Desktop.
Save atinfinity/b7461d4f2dddc018be4d to your computer and use it in GitHub Desktop.
OpenCV 3.0-beta Mac OS X向けframeworkビルド用スクリプト(Mac OS X/Xcode/x86_64)
#!/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>
By cmake conventions (and especially if you work with OpenCV repository),
the output dir should not be a subdirectory of OpenCV source tree.
Script will create <outputdir>, if it's missing, and a few its subdirectories:
<outputdir>
build/
iPhoneOS-*/
[cmake-generated build tree for an iOS device target]
iPhoneSimulator/
[cmake-generated build tree for iOS simulator]
opencv2.framework/
[the framework content]
The script should handle minor OpenCV updates efficiently
- it does not recompile the library from scratch each time.
However, opencv2.framework directory is erased and recreated on each run.
"""
import glob, re, os, os.path, shutil, string, sys
def build_opencv(srcroot, buildroot, target, arch):
"builds OpenCV for device or simulator"
builddir = os.path.join(buildroot, target + '-' + arch)
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 " +
"-DBUILD_SHARED_LIBS=OFF " +
"-DBUILD_DOCS=OFF " +
"-DBUILD_EXAMPLES=OFF " +
"-DBUILD_TESTS=OFF " +
"-DBUILD_PERF_TESTS=OFF " +
"-DBUILD_opencv_apps=ON " +
"-DBUILD_opencv_calib3d=ON " +
"-DBUILD_opencv_core=ON " +
"-DBUILD_opencv_cuda=OFF " +
"-DBUILD_opencv_cudaarithm=OFF " +
"-DBUILD_opencv_cudabgsegm=OFF " +
"-DBUILD_opencv_cudacodec=OFF " +
"-DBUILD_opencv_cudafeatures2d=OFF " +
"-DBUILD_opencv_cudafilters=OFF " +
"-DBUILD_opencv_cudaimgproc=OFF " +
"-DBUILD_opencv_cudalegacy=OFF " +
"-DBUILD_opencv_cudaoptflow=OFF " +
"-DBUILD_opencv_cudastereo=OFF " +
"-DBUILD_opencv_cudawarping=OFF " +
"-DBUILD_opencv_cudev=OFF " +
"-DBUILD_opencv_features2d=ON " +
"-DBUILD_opencv_flann=ON " +
"-DBUILD_opencv_highgui=ON " +
"-DBUILD_opencv_imgcodecs=ON " +
"-DBUILD_opencv_imgproc=ON " +
"-DBUILD_opencv_java=OFF " +
"-DBUILD_opencv_ml=ON " +
"-DBUILD_opencv_objdetect=ON " +
"-DBUILD_opencv_photo=ON " +
"-DBUILD_opencv_python2=ON " +
"-DBUILD_opencv_python3=ON " +
"-DBUILD_opencv_shape=ON " +
"-DBUILD_opencv_stitching=ON " +
"-DBUILD_opencv_superres=ON " +
"-DBUILD_opencv_ts=ON " +
"-DBUILD_opencv_video=ON " +
"-DBUILD_opencv_videoio=ON " +
"-DBUILD_opencv_videostab=ON " +
"-DBUILD_opencv_viz=OFF " +
"-DBUILD_opencv_world=ON " +
"-DWITH_PNG=ON -DBUILD_PNG=OFF " +
"-DWITH_TIFF=ON -DBUILD_TIFF=OFF " +
"-DWITH_JASPER=ON -DBUILD_JASPER=OFF " +
"-DWITH_JPEG=ON -DBUILD_JPEG=OFF " +
"-DWITH_WEBP=ON -DBUILD_WEBP=OFF " +
"-DWITH_OPENEXR=ON -DBUILD_OPENEXR=OFF " +
"-DWITH_TBB=ON -DBUILD_TBB=OFF " +
"-DWITH_IPP=ON -DWITH_IPP_A=OFF " +
"-DWITH_1394=OFF" +
"-DWITH_CUBLAS=OFF" +
"-DWITH_CUDA=OFF" +
"-DWITH_CUFFT=OFF" +
"-DWITH_EIGEN=ON" +
"-DWITH_FFMPEG=ON" +
"-DWITH_OPENCL=ON" +
"-DWITH_OPENCLAMDBLAS=OFF" +
"-DWITH_OPENCLAMDFFT=OFF" +
"-DWITH_OPENGL=OFF" +
"-DCMAKE_C_FLAGS=\"-Wno-implicit-function-declaration\" " +
"-DCMAKE_INSTALL_PREFIX=install")
# 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))
for wlib in [builddir + "/modules/world/UninstalledProducts/libopencv_world.a",
builddir + "/lib/Release/libopencv_world.a"]:
if os.path.isfile(wlib):
os.remove(wlib)
os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 2 -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()))
os.chdir(currdir)
def put_framework_together(srcroot, dstroot):
"constructs the framework directory after all the targets are built"
# 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]
# 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)
# form the directory tree
dstdir = "Versions/A"
os.makedirs(dstdir + "/Resources")
tdir0 = "../build/" + targetlist[0]
# copy headers
shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers")
# 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")
# copy Info.plist
shutil.copyfile(tdir0 + "/osx/Info.plist", dstdir + "/Resources/Info.plist")
# make symbolic links
os.symlink("A", "Versions/Current")
os.symlink("Versions/Current/Headers", "Headers")
os.symlink("Versions/Current/Resources", "Resources")
os.symlink("Versions/Current/opencv2", "opencv2")
def build_framework(srcroot, dstroot):
"main function to do all the work"
targets = ["MacOSX" ]
archs = ["x86_64"]
for i in range(len(targets)):
build_opencv(srcroot, os.path.join(dstroot, "build"), targets[i], archs[i])
put_framework_together(srcroot, dstroot)
if __name__ == "__main__":
if len(sys.argv) != 2:
print "Usage:\n\t./build_framework.py <outputdir>\n\n"
sys.exit(0)
build_framework(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")), os.path.abspath(sys.argv[1]))
@atinfinity
Copy link
Author

OpenCV 3.0-beta Mac OS X向けframeworkビルド用スクリプト(Mac OS X/Xcode/x86_64)

生成方法

  1. githubから3.0.0-betaのソースを取得する。
  2. このページにあるbuild_framework.pyを以下のディレクトリに格納する。
    opencv-3.0.0-beta/platforms/osx/
  3. 展開してターミナルで以下のコマンドを実行する。ビルドが完了すると「osx」ディレクトリにopencv2.frameworkが生成される。
    cd ~/<my_working_directory>
    python opencv-3.0.0-beta/platforms/osx/build_framework.py osx

使い方

  1. Xcodeでプロジェクトを開き、「Build Phases」画面に移動する。
  2. 「Link Binary With Libraries」で「+」ボタンを押下して、生成した「opencv2.framework」を追加する。
  3. 「Link Binary With Libraries」で「OpenCL.framework」、「AppKit.framework」を追加する。
  4. 「Link Binary With Libraries」で「libtbb.dylib」(TBB)、「libippicv.a」(ippicv)を追加する。

注意点

  • ビルドするためにはCMake(2.8.8以降)、Pythonをインストールする必要があります。
  • このページにあるbuild_framework.pyではビルド時間を短縮するためにアーキテクチャをx86_64に限定しています。
  • build_framework.pyのCMakeオプションを編集することでOpenCVの機能をカスタマイズできます。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment