diff --git a/platforms/android/build_sdk.py b/platforms/android/build_sdk.py index b38fd7dc84..34b93824e2 100755 --- a/platforms/android/build_sdk.py +++ b/platforms/android/build_sdk.py @@ -48,6 +48,16 @@ def check_dir(d, create=False, clean=False): os.makedirs(d) return d +def check_executable(cmd): + try: + FNULL = open(os.devnull, 'w') + retcode = subprocess.call(cmd, stdout=FNULL, stderr=subprocess.STDOUT) + if retcode < 0: + return False + return True + except: + return False + 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 @@ -131,6 +141,36 @@ class Builder: self.opencv_version = determine_opencv_version(os.path.join(self.opencvdir, "modules", "core", "include", "opencv2", "core", "version.hpp")) self.use_ccache = False if config.no_ccache else True + def get_cmake(self): + if check_executable(['cmake', '--version']): + 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: + # there could be more than one - just take the first one + cmake_from_sdk = os.path.join(android_cmake, cmake_subdirs[0], 'bin', 'cmake') + 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): + if check_executable(['ninja', '--version']): + 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") + def get_toolchain_file(self): if not self.config.force_opencv_toolchain: toolchain = os.path.join(os.environ['ANDROID_NDK'], 'build', 'cmake', 'android.toolchain.cmake') @@ -155,7 +195,7 @@ class Builder: rm_one(d) def build_library(self, abi, do_install): - cmd = ["cmake", "-GNinja"] + cmd = [self.get_cmake(), "-GNinja"] cmake_vars = dict( CMAKE_TOOLCHAIN_FILE=self.get_toolchain_file(), INSTALL_CREATE_DISTRIB="ON", @@ -168,6 +208,7 @@ class Builder: BUILD_DOCS="OFF", BUILD_ANDROID_EXAMPLES="ON", INSTALL_ANDROID_EXAMPLES="ON", + CMAKE_MAKE_PROGRAM=self.get_ninja(), ) if self.config.extra_modules_path is not None: @@ -182,7 +223,7 @@ class Builder: cmd += [ "-D%s='%s'" % (k, v) for (k, v) in cmake_vars.items() if v is not None] cmd.append(self.opencvdir) execute(cmd) - execute(["ninja", "install/strip"]) + execute([self.get_ninja(), "install/strip"]) def build_javadoc(self): classpaths = [] @@ -251,6 +292,20 @@ if __name__ == "__main__": if not 'ANDROID_HOME' in os.environ and 'ANDROID_SDK' in os.environ: os.environ['ANDROID_HOME'] = os.environ["ANDROID_SDK"] + 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 + if not 'ANDROID_NDK' in os.environ and 'ANDROID_SDK' in os.environ and os.path.exists(os.path.join(os.environ["ANDROID_SDK"], 'ndk-bundle')): + os.environ['ANDROID_NDK'] = os.path.join(os.environ["ANDROID_SDK"], 'ndk-bundle') + + if not 'ANDROID_NDK' in os.environ: + raise Fail("NDK location not set. Either pass --ndk_path or set ANDROID_NDK environment variable") + + if not check_executable(['ccache', '--version']): + log.info("ccache not found - disabling ccache supprt") + args.no_ccache = True + 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):