2015-08-10 23:02:41 +08:00
#!/usr/bin/env python
2019-06-07 03:55:17 +08:00
import os , sys
import argparse
import glob
import re
import shutil
import subprocess
import time
2015-08-10 23:02:41 +08:00
import logging as log
import xml . etree . ElementTree as ET
2018-01-05 10:40:37 +08:00
SCRIPT_DIR = os . path . dirname ( os . path . abspath ( __file__ ) )
2015-08-10 23:02:41 +08:00
class Fail ( Exception ) :
def __init__ ( self , text = None ) :
self . t = text
def __str__ ( self ) :
return " ERROR " if self . t is None else self . t
2015-09-01 05:59:08 +08:00
def execute ( cmd , shell = False ) :
2015-08-10 23:02:41 +08:00
try :
2018-01-05 10:40:37 +08:00
log . debug ( " Executing: %s " % cmd )
log . info ( ' Executing: ' + ' ' . join ( cmd ) )
2015-09-01 05:59:08 +08:00
retcode = subprocess . call ( cmd , shell = shell )
2015-08-10 23:02:41 +08:00
if retcode < 0 :
2018-05-03 13:47:34 +08:00
raise Fail ( " Child was terminated by signal: %s " % - retcode )
2015-08-10 23:02:41 +08:00
elif retcode > 0 :
raise Fail ( " Child returned: %s " % retcode )
except OSError as e :
2015-09-01 17:14:40 +08:00
raise Fail ( " Execution failed: %d / %s " % ( e . errno , e . strerror ) )
2015-08-10 23:02:41 +08:00
def rm_one ( d ) :
d = os . path . abspath ( d )
if os . path . exists ( d ) :
if os . path . isdir ( d ) :
log . info ( " Removing dir: %s " , d )
shutil . rmtree ( d )
elif os . path . isfile ( d ) :
log . info ( " Removing file: %s " , d )
os . remove ( d )
def check_dir ( d , create = False , clean = False ) :
d = os . path . abspath ( d )
log . info ( " Check dir %s (create: %s , clean: %s ) " , d , create , clean )
if os . path . exists ( d ) :
if not os . path . isdir ( d ) :
raise Fail ( " Not a directory: %s " % d )
if clean :
for x in glob . glob ( os . path . join ( d , " * " ) ) :
rm_one ( x )
else :
if create :
os . makedirs ( d )
return d
2019-03-04 20:19:29 +08:00
def check_executable ( cmd ) :
try :
2019-03-04 20:23:14 +08:00
log . debug ( " Executing: %s " % cmd )
result = subprocess . check_output ( cmd , stderr = subprocess . STDOUT )
2020-06-18 15:40:43 +08:00
if not isinstance ( result , str ) :
result = result . decode ( " utf-8 " )
2019-03-04 20:23:14 +08:00
log . debug ( " Result: %s " % ( result + ' \n ' ) . split ( ' \n ' ) [ 0 ] )
2019-03-04 20:19:29 +08:00
return True
2019-03-04 20:23:14 +08:00
except Exception as e :
log . debug ( ' Failed: %s ' % e )
2019-03-04 20:19:29 +08:00
return False
2015-08-10 23:02:41 +08:00
def determine_opencv_version ( version_hpp_path ) :
# version in 2.4 - CV_VERSION_EPOCH.CV_VERSION_MAJOR.CV_VERSION_MINOR.CV_VERSION_REVISION
# version in master - CV_VERSION_MAJOR.CV_VERSION_MINOR.CV_VERSION_REVISION-CV_VERSION_STATUS
with open ( version_hpp_path , " rt " ) as f :
data = f . read ( )
major = re . search ( r ' ^#define \ W+CV_VERSION_MAJOR \ W+( \ d+)$ ' , data , re . MULTILINE ) . group ( 1 )
minor = re . search ( r ' ^#define \ W+CV_VERSION_MINOR \ W+( \ d+)$ ' , data , re . MULTILINE ) . group ( 1 )
revision = re . search ( r ' ^#define \ W+CV_VERSION_REVISION \ W+( \ d+)$ ' , data , re . MULTILINE ) . group ( 1 )
2015-12-18 22:38:04 +08:00
version_status = re . search ( r ' ^#define \ W+CV_VERSION_STATUS \ W+ " ([^ " ]*) " $ ' , data , re . MULTILINE ) . group ( 1 )
2015-08-10 23:02:41 +08:00
return " %(major)s . %(minor)s . %(revision)s %(version_status)s " % locals ( )
2018-01-04 03:07:55 +08:00
# shutil.move fails if dst exists
def move_smart ( src , dst ) :
def move_recurse ( subdir ) :
s = os . path . join ( src , subdir )
d = os . path . join ( dst , subdir )
if os . path . exists ( d ) :
if os . path . isdir ( d ) :
for item in os . listdir ( s ) :
move_recurse ( os . path . join ( subdir , item ) )
elif os . path . isfile ( s ) :
shutil . move ( s , d )
else :
shutil . move ( s , d )
move_recurse ( ' ' )
# shutil.copytree fails if dst exists
def copytree_smart ( src , dst ) :
def copy_recurse ( subdir ) :
s = os . path . join ( src , subdir )
d = os . path . join ( dst , subdir )
if os . path . exists ( d ) :
if os . path . isdir ( d ) :
for item in os . listdir ( s ) :
copy_recurse ( os . path . join ( subdir , item ) )
elif os . path . isfile ( s ) :
shutil . copy2 ( s , d )
else :
if os . path . isdir ( s ) :
shutil . copytree ( s , d )
elif os . path . isfile ( s ) :
shutil . copy2 ( s , d )
copy_recurse ( ' ' )
2020-06-04 20:07:30 +08:00
def get_highest_version ( subdirs ) :
return max ( subdirs , key = lambda dir : [ int ( comp ) for comp in os . path . split ( dir ) [ - 1 ] . split ( ' . ' ) ] )
2015-08-10 23:02:41 +08:00
#===================================================================================================
class ABI :
2018-01-05 10:40:37 +08:00
def __init__ ( self , platform_id , name , toolchain , ndk_api_level = None , cmake_vars = dict ( ) ) :
2015-08-10 23:02:41 +08:00
self . platform_id = platform_id # platform code to add to apk version (for cmake)
self . name = name # general name (official Android ABI identifier)
self . toolchain = toolchain # toolchain identifier (for cmake)
2018-01-05 10:40:37 +08:00
self . cmake_vars = dict (
ANDROID_STL = " gnustl_static " ,
ANDROID_ABI = self . name ,
ANDROID_PLATFORM_ID = platform_id ,
)
2018-08-28 05:06:42 +08:00
if toolchain is not None :
self . cmake_vars [ ' ANDROID_TOOLCHAIN_NAME ' ] = toolchain
else :
self . cmake_vars [ ' ANDROID_TOOLCHAIN ' ] = ' clang '
2019-07-12 04:04:05 +08:00
self . cmake_vars [ ' ANDROID_STL ' ] = ' c++_shared '
2018-01-05 10:40:37 +08:00
if ndk_api_level :
self . cmake_vars [ ' ANDROID_NATIVE_API_LEVEL ' ] = ndk_api_level
self . cmake_vars . update ( cmake_vars )
2015-08-10 23:02:41 +08:00
def __str__ ( self ) :
return " %s ( %s ) " % ( self . name , self . toolchain )
def haveIPP ( self ) :
2016-01-18 19:11:02 +08:00
return self . name == " x86 " or self . name == " x86_64 "
2015-08-10 23:02:41 +08:00
#===================================================================================================
class Builder :
2018-01-04 03:07:55 +08:00
def __init__ ( self , workdir , opencvdir , config ) :
2015-08-10 23:02:41 +08:00
self . workdir = check_dir ( workdir , create = True )
self . opencvdir = check_dir ( opencvdir )
2018-01-04 03:07:55 +08:00
self . config = config
2015-08-10 23:02:41 +08:00
self . libdest = check_dir ( os . path . join ( self . workdir , " o4a " ) , create = True , clean = True )
2018-01-04 03:07:55 +08:00
self . resultdest = check_dir ( os . path . join ( self . workdir , ' OpenCV-android-sdk ' ) , create = True , clean = True )
self . docdest = check_dir ( os . path . join ( self . workdir , ' OpenCV-android-sdk ' , ' sdk ' , ' java ' , ' javadoc ' ) , create = True , clean = True )
2015-08-10 23:02:41 +08:00
self . extra_packs = [ ]
self . opencv_version = determine_opencv_version ( os . path . join ( self . opencvdir , " modules " , " core " , " include " , " opencv2 " , " core " , " version.hpp " ) )
2018-01-05 10:40:37 +08:00
self . use_ccache = False if config . no_ccache else True
2019-03-04 20:23:14 +08:00
self . cmake_path = self . get_cmake ( )
self . ninja_path = self . get_ninja ( )
2019-03-11 22:16:42 +08:00
self . debug = True if config . debug else False
2019-05-19 01:13:39 +08:00
self . debug_info = True if config . debug_info else False
2019-07-18 20:31:02 +08:00
self . no_samples_build = True if config . no_samples_build else False
2020-09-29 20:55:31 +08:00
self . opencl = True if config . opencl else False
2021-06-05 19:46:09 +08:00
self . no_kotlin = True if config . no_kotlin else False
2022-07-10 12:45:58 +08:00
self . shared = True if config . shared else False
2024-03-27 20:10:40 +08:00
self . disable = args . disable
2015-08-10 23:02:41 +08:00
2019-03-04 20:19:29 +08:00
def get_cmake ( self ) :
2019-03-04 20:23:14 +08:00
if not self . config . use_android_buildtools and check_executable ( [ ' cmake ' , ' --version ' ] ) :
2019-03-04 20:19:29 +08:00
log . info ( " Using cmake from PATH " )
return ' cmake '
# look to see if Android SDK's cmake is installed
android_cmake = os . path . join ( os . environ [ ' ANDROID_SDK ' ] , ' cmake ' )
if os . path . exists ( android_cmake ) :
cmake_subdirs = [ f for f in os . listdir ( android_cmake ) if check_executable ( [ os . path . join ( android_cmake , f , ' bin ' , ' cmake ' ) , ' --version ' ] ) ]
if len ( cmake_subdirs ) > 0 :
2020-06-04 20:07:30 +08:00
# there could be more than one - get the most recent
cmake_from_sdk = os . path . join ( android_cmake , get_highest_version ( cmake_subdirs ) , ' bin ' , ' cmake ' )
2019-03-04 20:19:29 +08:00
log . info ( " Using cmake from Android SDK: %s " , cmake_from_sdk )
return cmake_from_sdk
raise Fail ( " Can ' t find cmake " )
def get_ninja ( self ) :
2019-03-04 20:23:14 +08:00
if not self . config . use_android_buildtools and check_executable ( [ ' ninja ' , ' --version ' ] ) :
2019-03-04 20:19:29 +08:00
log . info ( " Using ninja from PATH " )
return ' ninja '
# Android SDK's cmake includes a copy of ninja - look to see if its there
android_cmake = os . path . join ( os . environ [ ' ANDROID_SDK ' ] , ' cmake ' )
if os . path . exists ( android_cmake ) :
cmake_subdirs = [ f for f in os . listdir ( android_cmake ) if check_executable ( [ os . path . join ( android_cmake , f , ' bin ' , ' ninja ' ) , ' --version ' ] ) ]
if len ( cmake_subdirs ) > 0 :
# there could be more than one - just take the first one
ninja_from_sdk = os . path . join ( android_cmake , cmake_subdirs [ 0 ] , ' bin ' , ' ninja ' )
log . info ( " Using ninja from Android SDK: %s " , ninja_from_sdk )
return ninja_from_sdk
raise Fail ( " Can ' t find ninja " )
2015-08-10 23:02:41 +08:00
def get_toolchain_file ( self ) :
2018-01-05 10:40:37 +08:00
if not self . config . force_opencv_toolchain :
toolchain = os . path . join ( os . environ [ ' ANDROID_NDK ' ] , ' build ' , ' cmake ' , ' android.toolchain.cmake ' )
if os . path . exists ( toolchain ) :
return toolchain
toolchain = os . path . join ( SCRIPT_DIR , " android.toolchain.cmake " )
if os . path . exists ( toolchain ) :
return toolchain
else :
raise Fail ( " Can ' t find toolchain " )
2015-08-10 23:02:41 +08:00
def get_engine_apk_dest ( self , engdest ) :
return os . path . join ( engdest , " platforms " , " android " , " service " , " engine " , " .build " )
def add_extra_pack ( self , ver , path ) :
if path is None :
return
self . extra_packs . append ( ( ver , check_dir ( path ) ) )
def clean_library_build_dir ( self ) :
for d in [ " CMakeCache.txt " , " CMakeFiles/ " , " bin/ " , " libs/ " , " lib/ " , " package/ " , " install/samples/ " ] :
rm_one ( d )
2023-12-13 20:54:31 +08:00
def build_library ( self , abi , do_install , no_media_ndk ) :
2019-03-04 20:23:14 +08:00
cmd = [ self . cmake_path , " -GNinja " ]
2018-01-05 10:40:37 +08:00
cmake_vars = dict (
CMAKE_TOOLCHAIN_FILE = self . get_toolchain_file ( ) ,
2018-11-06 01:39:09 +08:00
INSTALL_CREATE_DISTRIB = " ON " ,
2018-01-05 10:40:37 +08:00
WITH_OPENCL = " OFF " ,
2021-06-05 19:46:09 +08:00
BUILD_KOTLIN_EXTENSIONS = " ON " ,
2018-01-05 10:40:37 +08:00
WITH_IPP = ( " ON " if abi . haveIPP ( ) else " OFF " ) ,
WITH_TBB = " ON " ,
BUILD_EXAMPLES = " OFF " ,
BUILD_TESTS = " OFF " ,
BUILD_PERF_TESTS = " OFF " ,
BUILD_DOCS = " OFF " ,
2019-07-18 20:31:02 +08:00
BUILD_ANDROID_EXAMPLES = ( " OFF " if self . no_samples_build else " ON " ) ,
2020-06-16 12:11:26 +08:00
INSTALL_ANDROID_EXAMPLES = ( " OFF " if self . no_samples_build else " ON " ) ,
2018-01-05 10:40:37 +08:00
)
2019-03-04 20:23:14 +08:00
if self . ninja_path != ' ninja ' :
cmake_vars [ ' CMAKE_MAKE_PROGRAM ' ] = self . ninja_path
2018-01-05 10:40:37 +08:00
2019-03-11 22:16:42 +08:00
if self . debug :
cmake_vars [ ' CMAKE_BUILD_TYPE ' ] = " Debug "
2019-05-19 01:13:39 +08:00
if self . debug_info : # Release with debug info
2019-03-11 22:16:42 +08:00
cmake_vars [ ' BUILD_WITH_DEBUG_INFO ' ] = " ON "
2020-09-29 20:55:31 +08:00
if self . opencl :
cmake_vars [ ' WITH_OPENCL ' ] = " ON "
2021-06-05 19:46:09 +08:00
if self . no_kotlin :
cmake_vars [ ' BUILD_KOTLIN_EXTENSIONS ' ] = " OFF "
2022-07-10 12:45:58 +08:00
if self . shared :
cmake_vars [ ' BUILD_SHARED_LIBS ' ] = " ON "
2020-01-22 22:48:33 +08:00
if self . config . modules_list is not None :
2023-11-20 21:54:05 +08:00
cmake_vars [ ' BUILD_LIST ' ] = ' %s ' % self . config . modules_list
2020-01-22 22:48:33 +08:00
2018-01-05 10:40:37 +08:00
if self . config . extra_modules_path is not None :
2023-11-20 21:54:05 +08:00
cmake_vars [ ' OPENCV_EXTRA_MODULES_PATH ' ] = ' %s ' % self . config . extra_modules_path
2016-04-23 06:09:07 +08:00
2015-08-10 23:02:41 +08:00
if self . use_ccache == True :
2023-11-20 21:54:05 +08:00
cmake_vars [ ' NDK_CCACHE ' ] = ' ccache '
2015-08-10 23:02:41 +08:00
if do_install :
2023-11-20 21:54:05 +08:00
cmake_vars [ ' BUILD_TESTS ' ] = " ON "
cmake_vars [ ' INSTALL_TESTS ' ] = " ON "
2018-01-05 10:40:37 +08:00
2023-12-13 20:54:31 +08:00
if no_media_ndk :
cmake_vars [ ' WITH_ANDROID_MEDIANDK ' ] = " OFF "
2018-01-05 10:40:37 +08:00
cmake_vars . update ( abi . cmake_vars )
2024-03-27 20:10:40 +08:00
if len ( self . disable ) > 0 :
cmake_vars . update ( { ' WITH_ %s ' % f : " OFF " for f in self . disable } )
2018-01-05 10:40:37 +08:00
cmd + = [ " -D %s = ' %s ' " % ( k , v ) for ( k , v ) in cmake_vars . items ( ) if v is not None ]
cmd . append ( self . opencvdir )
2015-08-10 23:02:41 +08:00
execute ( cmd )
2019-03-19 21:50:08 +08:00
# full parallelism for C++ compilation tasks
execute ( [ self . ninja_path , " opencv_modules " ] )
2019-07-18 20:31:02 +08:00
# limit parallelism for building samples (avoid huge memory consumption)
if self . no_samples_build :
execute ( [ self . ninja_path , " install " if ( self . debug_info or self . debug ) else " install/strip " ] )
else :
2023-11-28 14:42:39 +08:00
execute ( [ self . ninja_path , " -j1 " , " install " if ( self . debug_info or self . debug ) else " install/strip " ] )
2015-08-10 23:02:41 +08:00
def build_javadoc ( self ) :
2018-01-04 03:07:55 +08:00
classpaths = [ ]
2015-08-10 23:02:41 +08:00
for dir , _ , files in os . walk ( os . environ [ " ANDROID_SDK " ] ) :
for f in files :
if f == " android.jar " or f == " annotations.jar " :
classpaths . append ( os . path . join ( dir , f ) )
2019-06-07 03:55:17 +08:00
srcdir = os . path . join ( self . resultdest , ' sdk ' , ' java ' , ' src ' )
dstdir = self . docdest
# synchronize with modules/java/jar/build.xml.in
shutil . copy2 ( os . path . join ( SCRIPT_DIR , ' ../../doc/mymath.js ' ) , dstdir )
2015-08-10 23:02:41 +08:00
cmd = [
" javadoc " ,
2019-06-07 03:55:17 +08:00
' -windowtitle ' , ' OpenCV %s Java documentation ' % self . opencv_version ,
' -doctitle ' , ' OpenCV Java documentation ( %s ) ' % self . opencv_version ,
2015-08-10 23:02:41 +08:00
" -nodeprecated " ,
" -public " ,
2019-06-07 03:55:17 +08:00
' -sourcepath ' , srcdir ,
' -encoding ' , ' UTF-8 ' ,
' -charset ' , ' UTF-8 ' ,
' -docencoding ' , ' UTF-8 ' ,
' --allow-script-in-comments ' ,
' -header ' ,
'''
< script >
var url = window . location . href ;
var pos = url . lastIndexOf ( ' /javadoc/ ' ) ;
url = pos > = 0 ? ( url . substring ( 0 , pos ) + ' /javadoc/mymath.js ' ) : ( window . location . origin + ' /mymath.js ' ) ;
var script = document . createElement ( ' script ' ) ;
script . src = ' %s /MathJax.js?config=TeX-AMS-MML_HTMLorMML, ' + url ;
document . getElementsByTagName ( ' head ' ) [ 0 ] . appendChild ( script ) ;
< / script >
''' % ' https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0 ' ,
' -bottom ' , ' Generated on %s / OpenCV %s ' % ( time . strftime ( " % Y- % m- %d % H: % M: % S " ) , self . opencv_version ) ,
" -d " , dstdir ,
2018-01-04 03:07:55 +08:00
" -classpath " , " : " . join ( classpaths ) ,
' -subpackages ' , ' org.opencv ' ,
2015-08-10 23:02:41 +08:00
]
execute ( cmd )
2018-11-06 01:39:09 +08:00
def gather_results ( self ) :
2015-08-10 23:02:41 +08:00
# Copy all files
root = os . path . join ( self . libdest , " install " )
for item in os . listdir ( root ) :
2018-01-04 03:07:55 +08:00
src = os . path . join ( root , item )
dst = os . path . join ( self . resultdest , item )
if os . path . isdir ( src ) :
2015-08-10 23:02:41 +08:00
log . info ( " Copy dir: %s " , item )
2018-01-04 03:07:55 +08:00
if self . config . force_copy :
copytree_smart ( src , dst )
else :
move_smart ( src , dst )
elif os . path . isfile ( src ) :
2015-08-10 23:02:41 +08:00
log . info ( " Copy file: %s " , item )
2018-01-04 03:07:55 +08:00
if self . config . force_copy :
shutil . copy2 ( src , dst )
else :
shutil . move ( src , dst )
2015-08-10 23:02:41 +08:00
2020-06-04 20:07:30 +08:00
def get_ndk_dir ( ) :
# look to see if Android NDK is installed
android_sdk_ndk = os . path . join ( os . environ [ " ANDROID_SDK " ] , ' ndk ' )
android_sdk_ndk_bundle = os . path . join ( os . environ [ " ANDROID_SDK " ] , ' ndk-bundle ' )
if os . path . exists ( android_sdk_ndk ) :
ndk_subdirs = [ f for f in os . listdir ( android_sdk_ndk ) if os . path . exists ( os . path . join ( android_sdk_ndk , f , ' package.xml ' ) ) ]
if len ( ndk_subdirs ) > 0 :
# there could be more than one - get the most recent
ndk_from_sdk = os . path . join ( android_sdk_ndk , get_highest_version ( ndk_subdirs ) )
log . info ( " Using NDK (side-by-side) from Android SDK: %s " , ndk_from_sdk )
return ndk_from_sdk
if os . path . exists ( os . path . join ( android_sdk_ndk_bundle , ' package.xml ' ) ) :
log . info ( " Using NDK bundle from Android SDK: %s " , android_sdk_ndk_bundle )
return android_sdk_ndk_bundle
return None
2015-08-10 23:02:41 +08:00
#===================================================================================================
if __name__ == " __main__ " :
parser = argparse . ArgumentParser ( description = ' Build OpenCV for Android SDK ' )
2018-01-05 10:40:37 +08:00
parser . add_argument ( " work_dir " , nargs = ' ? ' , default = ' . ' , help = " Working directory (and output) " )
parser . add_argument ( " opencv_dir " , nargs = ' ? ' , default = os . path . join ( SCRIPT_DIR , ' ../.. ' ) , help = " Path to OpenCV source dir " )
2019-03-21 00:07:59 +08:00
parser . add_argument ( ' --config ' , default = ' ndk-18-api-level-21.config.py ' , type = str , help = " Package build configuration " , )
2015-08-10 23:02:41 +08:00
parser . add_argument ( ' --ndk_path ' , help = " Path to Android NDK to use for build " )
parser . add_argument ( ' --sdk_path ' , help = " Path to Android SDK to use for build " )
2019-03-04 20:23:14 +08:00
parser . add_argument ( ' --use_android_buildtools ' , action = " store_true " , help = ' Use cmake/ninja build tools from Android SDK ' )
2020-01-22 22:48:33 +08:00
parser . add_argument ( " --modules_list " , help = " List of modules to include for build " )
2016-04-23 06:09:07 +08:00
parser . add_argument ( " --extra_modules_path " , help = " Path to extra modules to use for build " )
2018-02-09 02:04:25 +08:00
parser . add_argument ( ' --sign_with ' , help = " Certificate to sign the Manager apk " )
2015-08-10 23:02:41 +08:00
parser . add_argument ( ' --build_doc ' , action = " store_true " , help = " Build javadoc " )
parser . add_argument ( ' --no_ccache ' , action = " store_true " , help = " Do not use ccache during library build " )
2018-01-04 03:07:55 +08:00
parser . add_argument ( ' --force_copy ' , action = " store_true " , help = " Do not use file move during library build (useful for debug) " )
2018-01-05 10:40:37 +08:00
parser . add_argument ( ' --force_opencv_toolchain ' , action = " store_true " , help = " Do not use toolchain from Android NDK " )
2019-05-19 01:13:39 +08:00
parser . add_argument ( ' --debug ' , action = " store_true " , help = " Build ' Debug ' binaries (CMAKE_BUILD_TYPE=Debug) " )
parser . add_argument ( ' --debug_info ' , action = " store_true " , help = " Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON) " )
2019-07-18 20:31:02 +08:00
parser . add_argument ( ' --no_samples_build ' , action = " store_true " , help = " Do not build samples (speeds up build) " )
2020-09-29 20:55:31 +08:00
parser . add_argument ( ' --opencl ' , action = " store_true " , help = " Enable OpenCL support " )
2021-06-05 19:46:09 +08:00
parser . add_argument ( ' --no_kotlin ' , action = " store_true " , help = " Disable Kotlin extensions " )
2022-07-10 12:45:58 +08:00
parser . add_argument ( ' --shared ' , action = " store_true " , help = " Build shared libraries " )
2023-12-13 20:54:31 +08:00
parser . add_argument ( ' --no_media_ndk ' , action = " store_true " , help = " Do not link Media NDK (required for video I/O support) " )
2024-03-27 20:10:40 +08:00
parser . add_argument ( ' --disable ' , metavar = ' FEATURE ' , default = [ ] , action = ' append ' , help = ' OpenCV features to disable (add WITH_*=OFF). To disable multiple, specify this flag again, e.g. " --disable TBB --disable OPENMP " ' )
2015-08-10 23:02:41 +08:00
args = parser . parse_args ( )
log . basicConfig ( format = ' %(message)s ' , level = log . DEBUG )
log . debug ( " Args: %s " , args )
if args . ndk_path is not None :
os . environ [ " ANDROID_NDK " ] = args . ndk_path
if args . sdk_path is not None :
os . environ [ " ANDROID_SDK " ] = args . sdk_path
2018-11-06 01:39:09 +08:00
if not ' ANDROID_HOME ' in os . environ and ' ANDROID_SDK ' in os . environ :
os . environ [ ' ANDROID_HOME ' ] = os . environ [ " ANDROID_SDK " ]
2019-03-04 20:19:29 +08:00
if not ' ANDROID_SDK ' in os . environ :
raise Fail ( " SDK location not set. Either pass --sdk_path or set ANDROID_SDK environment variable " )
# look for an NDK installed with the Android SDK
2020-06-04 20:07:30 +08:00
if not ' ANDROID_NDK ' in os . environ and ' ANDROID_SDK ' in os . environ :
sdk_ndk_dir = get_ndk_dir ( )
if sdk_ndk_dir :
os . environ [ ' ANDROID_NDK ' ] = sdk_ndk_dir
2019-03-04 20:19:29 +08:00
if not ' ANDROID_NDK ' in os . environ :
raise Fail ( " NDK location not set. Either pass --ndk_path or set ANDROID_NDK environment variable " )
2020-06-04 20:07:30 +08:00
show_samples_build_warning = False
#also set ANDROID_NDK_HOME (needed by the gradle build)
if not ' ANDROID_NDK_HOME ' in os . environ and ' ANDROID_NDK ' in os . environ :
os . environ [ ' ANDROID_NDK_HOME ' ] = os . environ [ " ANDROID_NDK " ]
show_samples_build_warning = True
2019-03-04 20:19:29 +08:00
if not check_executable ( [ ' ccache ' , ' --version ' ] ) :
2019-03-04 20:23:14 +08:00
log . info ( " ccache not found - disabling ccache support " )
2019-03-04 20:19:29 +08:00
args . no_ccache = True
2018-01-05 10:40:37 +08:00
if os . path . realpath ( args . work_dir ) == os . path . realpath ( SCRIPT_DIR ) :
raise Fail ( " Specify workdir (building from script directory is not supported) " )
if os . path . realpath ( args . work_dir ) == os . path . realpath ( args . opencv_dir ) :
raise Fail ( " Specify workdir (building from OpenCV source directory is not supported) " )
2018-04-25 18:20:03 +08:00
# Relative paths become invalid in sub-directories
if args . opencv_dir is not None and not os . path . isabs ( args . opencv_dir ) :
args . opencv_dir = os . path . abspath ( args . opencv_dir )
if args . extra_modules_path is not None and not os . path . isabs ( args . extra_modules_path ) :
args . extra_modules_path = os . path . abspath ( args . extra_modules_path )
2018-01-05 10:40:37 +08:00
cpath = args . config
if not os . path . exists ( cpath ) :
cpath = os . path . join ( SCRIPT_DIR , cpath )
if not os . path . exists ( cpath ) :
raise Fail ( ' Config " %s " is missing ' % args . config )
with open ( cpath , ' r ' ) as f :
cfg = f . read ( )
print ( " Package configuration: " )
print ( ' = ' * 80 )
print ( cfg . strip ( ) )
print ( ' = ' * 80 )
2018-05-11 18:29:28 +08:00
ABIs = None # make flake8 happy
2018-01-05 10:40:37 +08:00
exec ( compile ( cfg , cpath , ' exec ' ) )
2015-08-10 23:02:41 +08:00
log . info ( " Android NDK path: %s " , os . environ [ " ANDROID_NDK " ] )
log . info ( " Android SDK path: %s " , os . environ [ " ANDROID_SDK " ] )
2018-01-04 03:07:55 +08:00
builder = Builder ( args . work_dir , args . opencv_dir , args )
2015-08-10 23:02:41 +08:00
log . info ( " Detected OpenCV version: %s " , builder . opencv_version )
for i , abi in enumerate ( ABIs ) :
do_install = ( i == 0 )
log . info ( " ===== " )
log . info ( " ===== Building library for %s " , abi )
log . info ( " ===== " )
os . chdir ( builder . libdest )
builder . clean_library_build_dir ( )
2023-12-13 20:54:31 +08:00
builder . build_library ( abi , do_install , args . no_media_ndk )
2015-08-10 23:02:41 +08:00
2018-11-06 01:39:09 +08:00
builder . gather_results ( )
2018-01-04 03:07:55 +08:00
2015-08-10 23:02:41 +08:00
if args . build_doc :
builder . build_javadoc ( )
log . info ( " ===== " )
log . info ( " ===== Build finished " )
log . info ( " ===== " )
2020-06-04 20:07:30 +08:00
if show_samples_build_warning :
#give a hint how to solve "Gradle sync failed: NDK not configured."
log . info ( " ANDROID_NDK_HOME environment variable required by the samples project is not set " )
2015-08-10 23:02:41 +08:00
log . info ( " SDK location: %s " , builder . resultdest )
log . info ( " Documentation location: %s " , builder . docdest )