Merge pull request #1263 from abidrahmank:pyCLAHE_24
[profile/ivi/opencv.git] / platforms / ios / build_framework.py
1 #!/usr/bin/env python
2 """
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.
5
6 Usage:
7     ./build_framework.py <outputdir>
8
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.
11
12 Script will create <outputdir>, if it's missing, and a few its subdirectories:
13
14     <outputdir>
15         build/
16             iPhoneOS-*/
17                [cmake-generated build tree for an iOS device target]
18             iPhoneSimulator/
19                [cmake-generated build tree for iOS simulator]
20         opencv2.framework/
21             [the framework content]
22
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.
26 """
27
28 import glob, re, os, os.path, shutil, string, sys
29
30 def build_opencv(srcroot, buildroot, target, arch):
31     "builds OpenCV for device or simulator"
32
33     builddir = os.path.join(buildroot, target + '-' + arch)
34     if not os.path.isdir(builddir):
35         os.makedirs(builddir)
36     currdir = os.getcwd()
37     os.chdir(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,))
47     else:
48         os.system("cmake %s %s" % (cmakeargs, srcroot))
49
50     for wlib in [builddir + "/modules/world/UninstalledProducts/libopencv_world.a",
51                  builddir + "/lib/Release/libopencv_world.a"]:
52         if os.path.isfile(wlib):
53             os.remove(wlib)
54
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()))
57     os.chdir(currdir)
58
59 def put_framework_together(srcroot, dstroot):
60     "constructs the framework directory after all the targets are built"
61
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]
65
66     # set the current dir to the dst root
67     currdir = os.getcwd()
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)
73
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(".")]
80             break
81     cfg.close()
82
83     # form the directory tree
84     dstdir = "Versions/A"
85     os.makedirs(dstdir + "/Resources")
86
87     # copy headers
88     shutil.copytree(tdir0 + "/install/include/opencv2", dstdir + "/Headers")
89
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")
93
94     # form Info.plist
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))
99     srcfile.close()
100     dstfile.close()
101
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")
107
108
109 def build_framework(srcroot, dstroot):
110     "main function to do all the work"
111
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])
116
117     put_framework_together(srcroot, dstroot)
118
119
120 if __name__ == "__main__":
121     if len(sys.argv) != 2:
122         print "Usage:\n\t./build_framework.py <outputdir>\n\n"
123         sys.exit(0)
124
125     build_framework(os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../..")), os.path.abspath(sys.argv[1]))