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 ]
2020-06-09 02:32:53 +08:00
{ framework_name } . framework /
2012-06-28 23:13:57 +08:00
[ the framework content ]
2020-08-09 15:39:24 +08:00
samples /
[ sample projects ]
docs /
[ documentation ]
2012-06-28 23:13:57 +08:00
The script should handle minor OpenCV updates efficiently
- it does not recompile the library from scratch each time .
2020-06-09 02:32:53 +08:00
However , { framework_name } . framework directory is erased and recreated on each run .
2017-01-21 00:16:01 +08:00
2020-06-09 02:32:53 +08:00
Adding - - dynamic parameter will build { framework_name } . framework as App Store dynamic framework . Only iOS 8 + versions are supported .
2012-06-28 23:13:57 +08:00
"""
2015-11-11 21:12:35 +08:00
from __future__ import print_function
2017-05-02 23:48:54 +08:00
import glob , re , os , os . path , shutil , string , sys , argparse , traceback , multiprocessing
2015-11-11 21:12:35 +08:00
from subprocess import check_call , check_output , CalledProcessError
2020-06-09 02:32:53 +08:00
from distutils . dir_util import copy_tree
2015-11-11 21:12:35 +08:00
Fix modules/ typos
Found using `codespell -q 3 -S ./3rdparty -L activ,amin,ang,atleast,childs,dof,endwhile,halfs,hist,iff,nd,od,uint`
2019-08-16 06:02:09 +08:00
IPHONEOS_DEPLOYMENT_TARGET = ' 8.0 ' # default, can be changed via command line options or environment variable
2018-10-24 12:37:31 +08:00
2015-11-11 21:12:35 +08:00
def execute ( cmd , cwd = None ) :
print ( " Executing: %s in %s " % ( cmd , cwd ) , file = sys . stderr )
2018-10-24 12:37:31 +08:00
print ( ' Executing: ' + ' ' . join ( cmd ) )
2015-11-11 21:12:35 +08:00
retcode = check_call ( cmd , cwd = cwd )
if retcode != 0 :
raise Exception ( " Child returned: " , retcode )
def getXCodeMajor ( ) :
ret = check_output ( [ " xcodebuild " , " -version " ] )
2018-09-27 05:20:37 +08:00
m = re . match ( r ' Xcode \ s+( \ d+) \ ..* ' , ret , flags = re . IGNORECASE )
2015-11-11 21:12:35 +08:00
if m :
return int ( m . group ( 1 ) )
2018-09-27 05:20:37 +08:00
else :
raise Exception ( " Failed to parse Xcode version " )
2015-11-11 21:12:35 +08:00
2020-06-25 20:27:31 +08:00
def getXCodeSetting ( var , projectdir ) :
ret = check_output ( [ " xcodebuild " , " -showBuildSettings " ] , cwd = projectdir )
m = re . search ( " \ s " + var + " = (.*) " , ret )
if m :
return m . group ( 1 )
else :
raise Exception ( " Failed to parse Xcode settings " )
2015-11-11 21:12:35 +08:00
class Builder :
2020-08-09 15:39:24 +08:00
def __init__ ( self , opencv , contrib , dynamic , bitcodedisabled , exclude , disable , enablenonfree , targets , debug , debug_info , framework_name , run_tests , build_docs ) :
2015-11-11 21:12:35 +08:00
self . opencv = os . path . abspath ( opencv )
self . contrib = None
if contrib :
modpath = os . path . join ( contrib , " modules " )
if os . path . isdir ( modpath ) :
self . contrib = os . path . abspath ( modpath )
else :
print ( " Note: contrib repository is bad - modules subfolder not found " , file = sys . stderr )
2017-01-21 00:16:01 +08:00
self . dynamic = dynamic
self . bitcodedisabled = bitcodedisabled
2016-09-29 05:32:23 +08:00
self . exclude = exclude
2020-06-09 02:32:53 +08:00
self . build_objc_wrapper = not " objc " in self . exclude
2019-09-24 23:42:35 +08:00
self . disable = disable
2018-12-12 22:32:19 +08:00
self . enablenonfree = enablenonfree
2015-12-16 22:28:03 +08:00
self . targets = targets
2019-08-23 23:22:29 +08:00
self . debug = debug
self . debug_info = debug_info
2020-06-09 02:32:53 +08:00
self . framework_name = framework_name
2020-08-09 15:39:24 +08:00
self . run_tests = run_tests
self . build_docs = build_docs
2015-11-11 21:12:35 +08:00
2020-08-30 06:41:54 +08:00
def getBuildDir ( self , parent , target ) :
2017-01-21 00:16:01 +08:00
2020-08-30 06:41:54 +08:00
res = os . path . join ( parent , ' build- %s - %s ' % ( target [ 0 ] . lower ( ) , target [ 1 ] . lower ( ) ) )
2017-01-21 00:16:01 +08:00
2015-11-11 21:12:35 +08:00
if not os . path . isdir ( res ) :
os . makedirs ( res )
return os . path . abspath ( res )
2015-12-16 22:28:03 +08:00
def _build ( self , outdir ) :
2015-11-11 21:12:35 +08:00
outdir = os . path . abspath ( outdir )
if not os . path . isdir ( outdir ) :
os . makedirs ( outdir )
2020-08-30 06:41:54 +08:00
main_working_dir = os . path . join ( outdir , " build " )
2015-11-11 21:12:35 +08:00
dirs = [ ]
xcode_ver = getXCodeMajor ( )
2020-08-30 06:41:54 +08:00
# build each architecture separately
alltargets = [ ]
2017-01-21 00:16:01 +08:00
2020-08-30 06:41:54 +08:00
for target_group in self . targets :
for arch in target_group [ 0 ] :
current = ( arch , target_group [ 1 ] )
alltargets . append ( current )
2017-01-21 00:16:01 +08:00
2020-08-30 06:41:54 +08:00
for target in alltargets :
main_build_dir = self . getBuildDir ( main_working_dir , target )
dirs . append ( main_build_dir )
2017-01-21 00:16:01 +08:00
2015-11-11 21:12:35 +08:00
cmake_flags = [ ]
if self . contrib :
cmake_flags . append ( " -DOPENCV_EXTRA_MODULES_PATH= %s " % self . contrib )
2020-08-30 06:41:54 +08:00
if xcode_ver > = 7 and target [ 1 ] == ' iPhoneOS ' and self . bitcodedisabled == False :
2015-11-11 21:12:35 +08:00
cmake_flags . append ( " -DCMAKE_C_FLAGS=-fembed-bitcode " )
cmake_flags . append ( " -DCMAKE_CXX_FLAGS=-fembed-bitcode " )
2020-08-30 06:41:54 +08:00
self . buildOne ( target [ 0 ] , target [ 1 ] , main_build_dir , cmake_flags )
2017-01-21 00:16:01 +08:00
2020-06-25 20:27:31 +08:00
if not self . dynamic :
2020-08-30 06:41:54 +08:00
self . mergeLibs ( main_build_dir )
else :
self . makeDynamicLib ( main_build_dir )
2015-11-11 21:12:35 +08:00
self . makeFramework ( outdir , dirs )
2020-06-09 02:32:53 +08:00
if self . build_objc_wrapper :
2020-08-09 15:39:24 +08:00
if self . run_tests :
check_call ( [ sys . argv [ 0 ] . replace ( " build_framework " , " run_tests " ) , " --framework_dir= " + outdir , " --framework_name= " + self . framework_name , dirs [ 0 ] + " /modules/objc/test " ] )
else :
print ( " To run tests call: " )
print ( sys . argv [ 0 ] . replace ( " build_framework " , " run_tests " ) + " --framework_dir= " + outdir + " --framework_name= " + self . framework_name + " " + dirs [ 0 ] + " /modules/objc/test " )
if self . build_docs :
check_call ( [ sys . argv [ 0 ] . replace ( " build_framework " , " build_docs " ) , dirs [ 0 ] + " /modules/objc/framework_build " ] )
doc_path = os . path . join ( dirs [ 0 ] , " modules " , " objc " , " doc_build " , " docs " )
if os . path . exists ( doc_path ) :
shutil . copytree ( doc_path , os . path . join ( outdir , " docs " ) )
shutil . copyfile ( os . path . join ( self . opencv , " doc " , " opencv.ico " ) , os . path . join ( outdir , " docs " , " favicon.ico " ) )
else :
print ( " To build docs call: " )
print ( sys . argv [ 0 ] . replace ( " build_framework " , " build_docs " ) + " " + dirs [ 0 ] + " /modules/objc/framework_build " )
2020-06-09 02:32:53 +08:00
self . copy_samples ( outdir )
2015-11-11 21:12:35 +08:00
2015-12-16 22:28:03 +08:00
def build ( self , outdir ) :
try :
self . _build ( outdir )
except Exception as e :
print ( " = " * 60 , file = sys . stderr )
print ( " ERROR: %s " % e , file = sys . stderr )
print ( " = " * 60 , file = sys . stderr )
traceback . print_exc ( file = sys . stderr )
sys . exit ( 1 )
def getToolchain ( self , arch , target ) :
2016-10-11 19:37:53 +08:00
return None
2015-12-16 22:28:03 +08:00
2019-08-23 23:22:29 +08:00
def getConfiguration ( self ) :
return " Debug " if self . debug else " Release "
2015-12-16 22:28:03 +08:00
def getCMakeArgs ( self , arch , target ) :
2017-01-21 00:16:01 +08:00
2017-02-21 17:50:09 +08:00
args = [
" cmake " ,
" -GXcode " ,
" -DAPPLE_FRAMEWORK=ON " ,
" -DCMAKE_INSTALL_PREFIX=install " ,
2019-08-23 23:22:29 +08:00
" -DCMAKE_BUILD_TYPE= %s " % self . getConfiguration ( ) ,
2018-09-04 06:40:16 +08:00
" -DOPENCV_INCLUDE_INSTALL_PATH=include " ,
2020-06-09 02:32:53 +08:00
" -DOPENCV_3P_LIB_INSTALL_PATH=lib/3rdparty " ,
" -DFRAMEWORK_NAME= %s " % self . framework_name ,
2017-02-21 17:50:09 +08:00
] + ( [
" -DBUILD_SHARED_LIBS=ON " ,
" -DCMAKE_MACOSX_BUNDLE=ON " ,
" -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=NO " ,
2020-06-25 20:27:31 +08:00
] if self . dynamic and not self . build_objc_wrapper else [ ] ) + ( [
" -DDYNAMIC_PLIST=ON "
2018-12-12 22:32:19 +08:00
] if self . dynamic else [ ] ) + ( [
" -DOPENCV_ENABLE_NONFREE=ON "
2019-08-23 23:22:29 +08:00
] if self . enablenonfree else [ ] ) + ( [
" -DBUILD_WITH_DEBUG_INFO=ON "
] if self . debug_info else [ ] )
2016-09-29 05:32:23 +08:00
if len ( self . exclude ) > 0 :
2020-06-25 20:27:31 +08:00
args + = [ " -DBUILD_opencv_world=OFF " ] if not ( self . dynamic and not self . build_objc_wrapper ) else [ ]
2017-02-21 17:50:09 +08:00
args + = [ " -DBUILD_opencv_ %s =OFF " % m for m in self . exclude ]
2016-09-29 05:32:23 +08:00
2019-09-24 23:42:35 +08:00
if len ( self . disable ) > 0 :
args + = [ " -DWITH_ %s =OFF " % f for f in self . disable ]
2015-12-16 22:28:03 +08:00
return args
2020-08-30 06:41:54 +08:00
def getBuildCommand ( self , arch , target ) :
2017-01-21 00:16:01 +08:00
2017-02-21 17:50:09 +08:00
buildcmd = [
" xcodebuild " ,
]
2020-06-25 20:27:31 +08:00
if ( self . dynamic or self . build_objc_wrapper ) and not self . bitcodedisabled and target == " iPhoneOS " :
buildcmd . append ( " BITCODE_GENERATION_MODE=bitcode " )
2020-08-30 06:41:54 +08:00
buildcmd + = [
" IPHONEOS_DEPLOYMENT_TARGET= " + os . environ [ ' IPHONEOS_DEPLOYMENT_TARGET ' ] ,
" ARCHS= %s " % arch ,
]
2017-02-21 17:50:09 +08:00
buildcmd + = [
2017-01-21 00:16:01 +08:00
" -sdk " , target . lower ( ) ,
2019-08-23 23:22:29 +08:00
" -configuration " , self . getConfiguration ( ) ,
2017-01-21 00:16:01 +08:00
" -parallelizeTargets " ,
2017-05-02 23:59:37 +08:00
" -jobs " , str ( multiprocessing . cpu_count ( ) ) ,
2020-08-30 06:41:54 +08:00
]
2017-01-21 00:16:01 +08:00
2015-12-16 22:28:03 +08:00
return buildcmd
def getInfoPlist ( self , builddirs ) :
return os . path . join ( builddirs [ 0 ] , " ios " , " Info.plist " )
2020-06-09 02:32:53 +08:00
def makeCMakeCmd ( self , arch , target , dir , cmakeargs = [ ] ) :
2015-12-16 22:28:03 +08:00
toolchain = self . getToolchain ( arch , target )
cmakecmd = self . getCMakeArgs ( arch , target ) + \
( [ " -DCMAKE_TOOLCHAIN_FILE= %s " % toolchain ] if toolchain is not None else [ ] )
2017-01-21 00:16:01 +08:00
if target . lower ( ) . startswith ( " iphoneos " ) :
2018-10-24 12:37:31 +08:00
cmakecmd . append ( " -DCPU_BASELINE=DETECT " )
2020-08-30 06:41:54 +08:00
if target . lower ( ) == " macosx " :
build_arch = check_output ( [ " uname " , " -m " ] ) . rstrip ( )
if build_arch != arch :
cmakecmd . append ( " -DCMAKE_SYSTEM_PROCESSOR= " + arch )
cmakecmd . append ( " -DCMAKE_OSX_ARCHITECTURES= " + arch )
cmakecmd . append ( " -DCPU_BASELINE=DETECT " )
cmakecmd . append ( " -DCMAKE_CROSSCOMPILING=ON " )
cmakecmd . append ( " -DOPENCV_WORKAROUND_CMAKE_20989=ON " )
2020-06-09 02:32:53 +08:00
cmakecmd . append ( dir )
2015-12-16 22:28:03 +08:00
cmakecmd . extend ( cmakeargs )
2020-06-09 02:32:53 +08:00
return cmakecmd
def buildOne ( self , arch , target , builddir , cmakeargs = [ ] ) :
# Run cmake
#toolchain = self.getToolchain(arch, target)
#cmakecmd = self.getCMakeArgs(arch, target) + \
# (["-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain] if toolchain is not None else [])
#if target.lower().startswith("iphoneos"):
# cmakecmd.append("-DCPU_BASELINE=DETECT")
#cmakecmd.append(self.opencv)
#cmakecmd.extend(cmakeargs)
cmakecmd = self . makeCMakeCmd ( arch , target , self . opencv , cmakeargs )
2015-12-16 22:28:03 +08:00
execute ( cmakecmd , cwd = builddir )
2017-01-21 00:16:01 +08:00
2015-12-16 22:28:03 +08:00
# Clean and build
clean_dir = os . path . join ( builddir , " install " )
if os . path . isdir ( clean_dir ) :
shutil . rmtree ( clean_dir )
buildcmd = self . getBuildCommand ( arch , target )
2015-11-11 21:12:35 +08:00
execute ( buildcmd + [ " -target " , " ALL_BUILD " , " build " ] , cwd = builddir )
2019-08-23 23:22:29 +08:00
execute ( [ " cmake " , " -DBUILD_TYPE= %s " % self . getConfiguration ( ) , " -P " , " cmake_install.cmake " ] , cwd = builddir )
2020-06-09 02:32:53 +08:00
if self . build_objc_wrapper :
cmakecmd = self . makeCMakeCmd ( arch , target , builddir + " /modules/objc/gen " , cmakeargs )
cmakecmd . append ( " -DBUILD_ROOT= %s " % builddir )
cmakecmd . append ( " -DCMAKE_INSTALL_NAME_TOOL=install_name_tool " )
cmakecmd . append ( " --no-warn-unused-cli " )
execute ( cmakecmd , cwd = builddir + " /modules/objc/framework_build " )
execute ( buildcmd + [ " -target " , " ALL_BUILD " , " build " ] , cwd = builddir + " /modules/objc/framework_build " )
execute ( [ " cmake " , " -DBUILD_TYPE= %s " % self . getConfiguration ( ) , " -DCMAKE_INSTALL_PREFIX= %s " % ( builddir + " /install " ) , " -P " , " cmake_install.cmake " ] , cwd = builddir + " /modules/objc/framework_build " )
2015-11-11 21:12:35 +08:00
def mergeLibs ( self , builddir ) :
2019-08-23 23:22:29 +08:00
res = os . path . join ( builddir , " lib " , self . getConfiguration ( ) , " libopencv_merged.a " )
2015-12-16 22:28:03 +08:00
libs = glob . glob ( os . path . join ( builddir , " install " , " lib " , " *.a " ) )
2020-06-09 02:32:53 +08:00
module = [ os . path . join ( builddir , " install " , " lib " , self . framework_name + " .framework " , self . framework_name ) ] if self . build_objc_wrapper else [ ]
2018-09-04 06:40:16 +08:00
libs3 = glob . glob ( os . path . join ( builddir , " install " , " lib " , " 3rdparty " , " *.a " ) )
2020-06-09 02:32:53 +08:00
print ( " Merging libraries: \n \t %s " % " \n \t " . join ( libs + libs3 + module ) , file = sys . stderr )
execute ( [ " libtool " , " -static " , " -o " , res ] + libs + libs3 + module )
2015-11-11 21:12:35 +08:00
2020-06-25 20:27:31 +08:00
def makeDynamicLib ( self , builddir ) :
target = builddir [ ( builddir . rfind ( " build- " ) + 6 ) : ]
target_platform = target [ ( target . rfind ( " - " ) + 1 ) : ]
is_device = target_platform == " iphoneos "
res = os . path . join ( builddir , " install " , " lib " , self . framework_name + " .framework " , self . framework_name )
libs = glob . glob ( os . path . join ( builddir , " install " , " lib " , " *.a " ) )
module = [ os . path . join ( builddir , " lib " , self . getConfiguration ( ) , self . framework_name + " .framework " , self . framework_name ) ]
libs3 = glob . glob ( os . path . join ( builddir , " install " , " lib " , " 3rdparty " , " *.a " ) )
link_target = target [ : target . find ( " - " ) ] + " -apple-ios " + os . environ [ ' IPHONEOS_DEPLOYMENT_TARGET ' ] + ( " -simulator " if target . endswith ( " simulator " ) else " " )
bitcode_flags = [ " -fembed-bitcode " , " -Xlinker " , " -bitcode_verify " ] if is_device and not self . bitcodedisabled else [ ]
toolchain_dir = getXCodeSetting ( " TOOLCHAIN_DIR " , builddir )
swift_link_dirs = [ " -L " + toolchain_dir + " /usr/lib/swift/ " + target_platform , " -L/usr/lib/swift " ]
sdk_dir = getXCodeSetting ( " SDK_DIR " , builddir )
execute ( [
" clang++ " ,
" -Xlinker " , " -rpath " ,
" -Xlinker " , " /usr/lib/swift " ,
" -target " , link_target ,
" -isysroot " , sdk_dir ,
" -install_name " , ( " @executable_path/Frameworks/ " + self . framework_name + " .framework/ " + self . framework_name ) if is_device else res ,
" -dynamiclib " , " -dead_strip " , " -fobjc-link-runtime " , " -all_load " ,
" -o " , res
] + swift_link_dirs + bitcode_flags + module + libs + libs3 )
2015-11-11 21:12:35 +08:00
def makeFramework ( self , outdir , builddirs ) :
2020-06-09 02:32:53 +08:00
name = self . framework_name
2015-11-11 21:12:35 +08:00
# set the current dir to the dst root
framework_dir = os . path . join ( outdir , " %s .framework " % name )
if os . path . isdir ( framework_dir ) :
shutil . rmtree ( framework_dir )
os . makedirs ( framework_dir )
2017-01-21 00:16:01 +08:00
if self . dynamic :
dstdir = framework_dir
else :
dstdir = os . path . join ( framework_dir , " Versions " , " A " )
2015-11-11 21:12:35 +08:00
# copy headers from one of build folders
shutil . copytree ( os . path . join ( builddirs [ 0 ] , " install " , " include " , " opencv2 " ) , os . path . join ( dstdir , " Headers " ) )
2020-06-09 02:32:53 +08:00
if name != " opencv2 " :
for dirname , dirs , files in os . walk ( os . path . join ( dstdir , " Headers " ) ) :
for filename in files :
filepath = os . path . join ( dirname , filename )
with open ( filepath ) as file :
body = file . read ( )
body = body . replace ( " include \" opencv2/ " , " include \" " + name + " / " )
body = body . replace ( " include <opencv2/ " , " include < " + name + " / " )
with open ( filepath , " w " ) as file :
file . write ( body )
if self . build_objc_wrapper :
copy_tree ( os . path . join ( builddirs [ 0 ] , " install " , " lib " , name + " .framework " , " Headers " ) , os . path . join ( dstdir , " Headers " ) )
platform_name_map = {
" arm " : " armv7-apple-ios " ,
" arm64 " : " arm64-apple-ios " ,
" i386 " : " i386-apple-ios-simulator " ,
" x86_64 " : " x86_64-apple-ios-simulator " ,
} if builddirs [ 0 ] . find ( " iphone " ) != - 1 else {
" x86_64 " : " x86_64-apple-macos " ,
2020-08-30 06:41:54 +08:00
" arm64 " : " arm64-apple-macos " ,
2020-06-09 02:32:53 +08:00
}
for d in builddirs :
copy_tree ( os . path . join ( d , " install " , " lib " , name + " .framework " , " Modules " ) , os . path . join ( dstdir , " Modules " ) )
for dirname , dirs , files in os . walk ( os . path . join ( dstdir , " Modules " ) ) :
for filename in files :
filestem = os . path . splitext ( filename ) [ 0 ]
fileext = os . path . splitext ( filename ) [ 1 ]
if filestem in platform_name_map :
os . rename ( os . path . join ( dirname , filename ) , os . path . join ( dirname , platform_name_map [ filestem ] + fileext ) )
2015-11-11 21:12:35 +08:00
# make universal static lib
2020-06-25 20:27:31 +08:00
if self . dynamic :
libs = [ os . path . join ( d , " install " , " lib " , name + " .framework " , name ) for d in builddirs ]
else :
libs = [ os . path . join ( d , " lib " , self . getConfiguration ( ) , " libopencv_merged.a " ) for d in builddirs ]
2015-11-11 21:12:35 +08:00
lipocmd = [ " lipo " , " -create " ]
lipocmd . extend ( libs )
lipocmd . extend ( [ " -o " , os . path . join ( dstdir , name ) ] )
print ( " Creating universal library from: \n \t %s " % " \n \t " . join ( libs ) , file = sys . stderr )
execute ( lipocmd )
2017-01-21 00:16:01 +08:00
# dynamic framework has different structure, just copy the Plist directly
if self . dynamic :
resdir = dstdir
shutil . copyfile ( self . getInfoPlist ( builddirs ) , os . path . join ( resdir , " Info.plist " ) )
else :
# copy Info.plist
resdir = os . path . join ( dstdir , " Resources " )
os . makedirs ( resdir )
shutil . copyfile ( self . getInfoPlist ( builddirs ) , os . path . join ( resdir , " Info.plist " ) )
# make symbolic links
links = [
( [ " A " ] , [ " Versions " , " Current " ] ) ,
( [ " Versions " , " Current " , " Headers " ] , [ " Headers " ] ) ,
( [ " Versions " , " Current " , " Resources " ] , [ " Resources " ] ) ,
2020-06-09 02:32:53 +08:00
( [ " Versions " , " Current " , " Modules " ] , [ " Modules " ] ) ,
2017-01-21 00:16:01 +08:00
( [ " Versions " , " Current " , name ] , [ name ] )
]
for l in links :
s = os . path . join ( * l [ 0 ] )
d = os . path . join ( framework_dir , * l [ 1 ] )
os . symlink ( s , d )
2012-06-28 23:13:57 +08:00
2020-06-09 02:32:53 +08:00
def copy_samples ( self , outdir ) :
return
2016-10-11 19:37:53 +08:00
class iOSBuilder ( Builder ) :
def getToolchain ( self , arch , target ) :
toolchain = os . path . join ( self . opencv , " platforms " , " ios " , " cmake " , " Toolchains " , " Toolchain- %s _Xcode.cmake " % target )
return toolchain
def getCMakeArgs ( self , arch , target ) :
args = Builder . getCMakeArgs ( self , arch , target )
args = args + [
' -DIOS_ARCH= %s ' % arch
]
return args
2020-06-09 02:32:53 +08:00
def copy_samples ( self , outdir ) :
print ( ' Copying samples to: ' + outdir )
samples_dir = os . path . join ( outdir , " samples " )
shutil . copytree ( os . path . join ( self . opencv , " samples " , " swift " , " ios " ) , samples_dir )
if self . framework_name != " OpenCV " :
for dirname , dirs , files in os . walk ( samples_dir ) :
for filename in files :
if not filename . endswith ( ( " .h " , " .swift " , " .pbxproj " ) ) :
continue
filepath = os . path . join ( dirname , filename )
with open ( filepath ) as file :
body = file . read ( )
body = body . replace ( " import OpenCV " , " import " + self . framework_name )
body = body . replace ( " #import <OpenCV/OpenCV.h> " , " #import < " + self . framework_name + " / " + self . framework_name + " .h> " )
body = body . replace ( " OpenCV.framework " , self . framework_name + " .framework " )
body = body . replace ( " ../../OpenCV/** " , " ../../ " + self . framework_name + " /** " )
with open ( filepath , " w " ) as file :
file . write ( body )
2016-10-11 19:37:53 +08:00
2012-06-28 23:13:57 +08:00
if __name__ == " __main__ " :
2015-11-11 21:12:35 +08:00
folder = os . path . abspath ( os . path . join ( os . path . dirname ( sys . argv [ 0 ] ) , " ../.. " ) )
2014-12-05 22:48:28 +08:00
parser = argparse . ArgumentParser ( description = ' The script builds OpenCV.framework for iOS. ' )
2015-11-11 21:12:35 +08:00
parser . add_argument ( ' out ' , metavar = ' OUTDIR ' , help = ' folder to put built framework ' )
parser . add_argument ( ' --opencv ' , metavar = ' DIR ' , default = folder , help = ' folder with opencv repository (default is " ../.. " relative to script location) ' )
parser . add_argument ( ' --contrib ' , metavar = ' DIR ' , default = None , help = ' folder with opencv_contrib repository (default is " None " - build only main framework) ' )
2016-09-29 05:32:23 +08:00
parser . add_argument ( ' --without ' , metavar = ' MODULE ' , default = [ ] , action = ' append ' , help = ' OpenCV modules to exclude from the framework ' )
2019-09-24 23:42:35 +08:00
parser . add_argument ( ' --disable ' , metavar = ' FEATURE ' , default = [ ] , action = ' append ' , help = ' OpenCV features to disable (add WITH_*=OFF) ' )
2017-01-21 00:16:01 +08:00
parser . add_argument ( ' --dynamic ' , default = False , action = ' store_true ' , help = ' build dynamic framework (default is " False " - builds static framework) ' )
parser . add_argument ( ' --disable-bitcode ' , default = False , dest = ' bitcodedisabled ' , action = ' store_true ' , help = ' disable bitcode (enabled by default) ' )
2018-10-24 12:37:31 +08:00
parser . add_argument ( ' --iphoneos_deployment_target ' , default = os . environ . get ( ' IPHONEOS_DEPLOYMENT_TARGET ' , IPHONEOS_DEPLOYMENT_TARGET ) , help = ' specify IPHONEOS_DEPLOYMENT_TARGET ' )
parser . add_argument ( ' --iphoneos_archs ' , default = ' armv7,armv7s,arm64 ' , help = ' select iPhoneOS target ARCHS ' )
2018-11-27 02:28:59 +08:00
parser . add_argument ( ' --iphonesimulator_archs ' , default = ' i386,x86_64 ' , help = ' select iPhoneSimulator target ARCHS ' )
2018-12-12 22:32:19 +08:00
parser . add_argument ( ' --enable_nonfree ' , default = False , dest = ' enablenonfree ' , action = ' store_true ' , help = ' enable non-free modules (disabled by default) ' )
2019-08-23 23:22:29 +08:00
parser . add_argument ( ' --debug ' , default = False , dest = ' debug ' , action = ' store_true ' , help = ' Build " Debug " binaries (disabled by default) ' )
parser . add_argument ( ' --debug_info ' , default = False , dest = ' debug_info ' , action = ' store_true ' , help = ' Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON) ' )
2020-06-15 22:15:05 +08:00
parser . add_argument ( ' --framework_name ' , default = ' opencv2 ' , dest = ' framework_name ' , help = ' Name of OpenCV framework (default: opencv2, will change to OpenCV in future version) ' )
2020-06-09 02:32:53 +08:00
parser . add_argument ( ' --legacy_build ' , default = False , dest = ' legacy_build ' , action = ' store_true ' , help = ' Build legacy opencv2 framework (default: False, equivalent to " --framework_name=opencv2 --without=objc " ) ' )
2020-08-09 15:39:24 +08:00
parser . add_argument ( ' --run_tests ' , default = False , dest = ' run_tests ' , action = ' store_true ' , help = ' Run tests ' )
parser . add_argument ( ' --build_docs ' , default = False , dest = ' build_docs ' , action = ' store_true ' , help = ' Build docs ' )
2014-12-05 22:48:28 +08:00
args = parser . parse_args ( )
2018-10-24 12:37:31 +08:00
os . environ [ ' IPHONEOS_DEPLOYMENT_TARGET ' ] = args . iphoneos_deployment_target
print ( ' Using IPHONEOS_DEPLOYMENT_TARGET= ' + os . environ [ ' IPHONEOS_DEPLOYMENT_TARGET ' ] )
iphoneos_archs = args . iphoneos_archs . split ( ' , ' )
print ( ' Using iPhoneOS ARCHS= ' + str ( iphoneos_archs ) )
2018-11-27 02:28:59 +08:00
iphonesimulator_archs = args . iphonesimulator_archs . split ( ' , ' )
print ( ' Using iPhoneSimulator ARCHS= ' + str ( iphonesimulator_archs ) )
2020-06-09 02:32:53 +08:00
if args . legacy_build :
args . framework_name = " opencv2 "
if not " objc " in args . without :
args . without . append ( " objc " )
2018-10-24 12:37:31 +08:00
2019-09-24 23:42:35 +08:00
b = iOSBuilder ( args . opencv , args . contrib , args . dynamic , args . bitcodedisabled , args . without , args . disable , args . enablenonfree ,
2016-08-11 18:47:04 +08:00
[
2018-10-24 12:37:31 +08:00
( iphoneos_archs , " iPhoneOS " ) ,
2016-08-11 18:47:04 +08:00
] if os . environ . get ( ' BUILD_PRECOMMIT ' , None ) else
2015-12-16 22:28:03 +08:00
[
2018-10-24 12:37:31 +08:00
( iphoneos_archs , " iPhoneOS " ) ,
2018-11-27 02:28:59 +08:00
( iphonesimulator_archs , " iPhoneSimulator " ) ,
2020-08-09 15:39:24 +08:00
] , args . debug , args . debug_info , args . framework_name , args . run_tests , args . build_docs )
2020-06-25 20:27:31 +08:00
2015-12-16 22:28:03 +08:00
b . build ( args . out )