|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import os, sys, subprocess, argparse, shutil, glob, re |
| 4 | +import logging as log |
| 5 | +import xml.etree.ElementTree as ET |
| 6 | + |
| 7 | +class Fail(Exception): |
| 8 | + def __init__(self, text=None): |
| 9 | + self.t = text |
| 10 | + def __str__(self): |
| 11 | + return "ERROR" if self.t is None else self.t |
| 12 | + |
| 13 | +def execute(cmd, shell=False): |
| 14 | + try: |
| 15 | + log.info("Executing: %s" % cmd) |
| 16 | + retcode = subprocess.call(cmd, shell=shell) |
| 17 | + if retcode < 0: |
| 18 | + raise Fail("Child was terminated by signal:" %s -retcode) |
| 19 | + elif retcode > 0: |
| 20 | + raise Fail("Child returned: %s" % retcode) |
| 21 | + except OSError as e: |
| 22 | + raise Fail("Execution failed: %d / %s" % (e.errno, e.strerror)) |
| 23 | + |
| 24 | +def rm_one(d): |
| 25 | + d = os.path.abspath(d) |
| 26 | + if os.path.exists(d): |
| 27 | + if os.path.isdir(d): |
| 28 | + log.info("Removing dir: %s", d) |
| 29 | + shutil.rmtree(d) |
| 30 | + elif os.path.isfile(d): |
| 31 | + log.info("Removing file: %s", d) |
| 32 | + os.remove(d) |
| 33 | + |
| 34 | +def check_dir(d, create=False, clean=False): |
| 35 | + d = os.path.abspath(d) |
| 36 | + log.info("Check dir %s (create: %s, clean: %s)", d, create, clean) |
| 37 | + if os.path.exists(d): |
| 38 | + if not os.path.isdir(d): |
| 39 | + raise Fail("Not a directory: %s" % d) |
| 40 | + if clean: |
| 41 | + for x in glob.glob(os.path.join(d, "*")): |
| 42 | + rm_one(x) |
| 43 | + else: |
| 44 | + if create: |
| 45 | + os.makedirs(d) |
| 46 | + return d |
| 47 | + |
| 48 | +def determine_engine_version(manifest_path): |
| 49 | + with open(manifest_path, "rt") as f: |
| 50 | + return re.search(r'android:versionName="(\d+\.\d+)"', f.read(), re.MULTILINE).group(1) |
| 51 | + |
| 52 | +def determine_opencv_version(version_hpp_path): |
| 53 | + # version in 2.4 - CV_VERSION_EPOCH.CV_VERSION_MAJOR.CV_VERSION_MINOR.CV_VERSION_REVISION |
| 54 | + # version in master - CV_VERSION_MAJOR.CV_VERSION_MINOR.CV_VERSION_REVISION-CV_VERSION_STATUS |
| 55 | + with open(version_hpp_path, "rt") as f: |
| 56 | + data = f.read() |
| 57 | + major = re.search(r'^#define\W+CV_VERSION_MAJOR\W+(\d+)$', data, re.MULTILINE).group(1) |
| 58 | + minor = re.search(r'^#define\W+CV_VERSION_MINOR\W+(\d+)$', data, re.MULTILINE).group(1) |
| 59 | + revision = re.search(r'^#define\W+CV_VERSION_REVISION\W+(\d+)$', data, re.MULTILINE).group(1) |
| 60 | + version_status = re.search(r'^#define\W+CV_VERSION_STATUS\W+"([^"]*)"$', data, re.MULTILINE).group(1) |
| 61 | + return "%(major)s.%(minor)s.%(revision)s%(version_status)s" % locals() |
| 62 | + |
| 63 | +#=================================================================================================== |
| 64 | + |
| 65 | +class ABI: |
| 66 | + def __init__(self, platform_id, name, toolchain, cmake_name=None): |
| 67 | + self.platform_id = platform_id # platform code to add to apk version (for cmake) |
| 68 | + self.name = name # general name (official Android ABI identifier) |
| 69 | + self.toolchain = toolchain # toolchain identifier (for cmake) |
| 70 | + self.cmake_name = cmake_name # name of android toolchain (for cmake) |
| 71 | + if self.cmake_name is None: |
| 72 | + self.cmake_name = self.name |
| 73 | + def __str__(self): |
| 74 | + return "%s (%s)" % (self.name, self.toolchain) |
| 75 | + def haveIPP(self): |
| 76 | + return self.name == "x86" or self.name == "x86_64" |
| 77 | + |
| 78 | +ABIs = [ |
| 79 | + ABI("2", "armeabi-v7a", "arm-linux-androideabi-4.8", cmake_name="armeabi-v7a with NEON"), |
| 80 | + ABI("1", "armeabi", "arm-linux-androideabi-4.8"), |
| 81 | + ABI("3", "arm64-v8a", "aarch64-linux-android-4.9"), |
| 82 | + ABI("5", "x86_64", "x86_64-4.9"), |
| 83 | + ABI("4", "x86", "x86-4.8"), |
| 84 | + ABI("7", "mips64", "mips64el-linux-android-4.9"), |
| 85 | + ABI("6", "mips", "mipsel-linux-android-4.8") |
| 86 | +] |
| 87 | + |
| 88 | +#=================================================================================================== |
| 89 | + |
| 90 | +class Builder: |
| 91 | + def __init__(self, workdir, opencvdir): |
| 92 | + self.workdir = check_dir(workdir, create=True) |
| 93 | + self.opencvdir = check_dir(opencvdir) |
| 94 | + self.extra_modules_path = None |
| 95 | + self.libdest = check_dir(os.path.join(self.workdir, "o4a"), create=True, clean=True) |
| 96 | + self.docdest = check_dir(os.path.join(self.workdir, "javadoc"), create=True, clean=True) |
| 97 | + self.resultdest = check_dir(os.path.join(self.workdir, "OpenCV-android-sdk"), create=True, clean=True) |
| 98 | + self.extra_packs = [] |
| 99 | + self.opencv_version = determine_opencv_version(os.path.join(self.opencvdir, "modules", "core", "include", "opencv2", "core", "version.hpp")) |
| 100 | + self.engine_version = determine_engine_version(os.path.join(self.opencvdir, "platforms", "android", "service", "engine", "AndroidManifest.xml")) |
| 101 | + self.use_ccache = True |
| 102 | + |
| 103 | + def get_toolchain_file(self): |
| 104 | + return os.path.join(self.opencvdir, "platforms", "android", "android.toolchain.cmake") |
| 105 | + |
| 106 | + def get_engine_apk_dest(self, engdest): |
| 107 | + return os.path.join(engdest, "platforms", "android", "service", "engine", ".build") |
| 108 | + |
| 109 | + def add_extra_pack(self, ver, path): |
| 110 | + if path is None: |
| 111 | + return |
| 112 | + self.extra_packs.append((ver, check_dir(path))) |
| 113 | + |
| 114 | + def clean_library_build_dir(self): |
| 115 | + for d in ["CMakeCache.txt", "CMakeFiles/", "bin/", "libs/", "lib/", "package/", "install/samples/"]: |
| 116 | + rm_one(d) |
| 117 | + |
| 118 | + def build_library(self, abi, do_install): |
| 119 | + cmd = [ |
| 120 | + "cmake", |
| 121 | + "-GNinja", |
| 122 | + "-DCMAKE_TOOLCHAIN_FILE='%s'" % self.get_toolchain_file(), |
| 123 | + "-DWITH_OPENCL=OFF", |
| 124 | + "-DWITH_CUDA=OFF", |
| 125 | + "-DWITH_IPP=%s" % ("ON" if abi.haveIPP() else "OFF"), |
| 126 | + "-DBUILD_EXAMPLES=OFF", |
| 127 | + "-DBUILD_TESTS=OFF", |
| 128 | + "-DBUILD_PERF_TESTS=OFF", |
| 129 | + "-DBUILD_DOCS=OFF", |
| 130 | + "-DBUILD_ANDROID_EXAMPLES=ON", |
| 131 | + "-DINSTALL_ANDROID_EXAMPLES=ON", |
| 132 | + "-DANDROID_STL=gnustl_static", |
| 133 | + "-DANDROID_NATIVE_API_LEVEL=9", |
| 134 | + "-DANDROID_ABI='%s'" % abi.cmake_name, |
| 135 | + "-DWITH_TBB=ON", |
| 136 | + "-DANDROID_TOOLCHAIN_NAME=%s" % abi.toolchain |
| 137 | + ] |
| 138 | + |
| 139 | + if self.extra_modules_path is not None: |
| 140 | + cmd.append("-DOPENCV_EXTRA_MODULES_PATH='%s'" % self.extra_modules_path) |
| 141 | + |
| 142 | + cmd.append(self.opencvdir) |
| 143 | + |
| 144 | + if self.use_ccache == True: |
| 145 | + cmd.append("-DNDK_CCACHE=ccache") |
| 146 | + if do_install: |
| 147 | + cmd.extend(["-DBUILD_TESTS=ON", "-DINSTALL_TESTS=ON"]) |
| 148 | + execute(cmd) |
| 149 | + if do_install: |
| 150 | + execute(["ninja"]) |
| 151 | + for c in ["libs", "dev", "java", "samples"]: |
| 152 | + execute(["cmake", "-DCOMPONENT=%s" % c, "-P", "cmake_install.cmake"]) |
| 153 | + else: |
| 154 | + execute(["ninja", "install/strip"]) |
| 155 | + |
| 156 | + def build_engine(self, abi, engdest): |
| 157 | + cmd = [ |
| 158 | + "cmake", |
| 159 | + "-GNinja", |
| 160 | + "-DCMAKE_TOOLCHAIN_FILE='%s'" % self.get_toolchain_file(), |
| 161 | + "-DANDROID_ABI='%s'" % abi.cmake_name, |
| 162 | + "-DBUILD_ANDROID_SERVICE=ON", |
| 163 | + "-DANDROID_PLATFORM_ID=%s" % abi.platform_id, |
| 164 | + "-DWITH_CUDA=OFF", |
| 165 | + "-DWITH_OPENCL=OFF", |
| 166 | + "-DWITH_IPP=OFF", |
| 167 | + self.opencvdir |
| 168 | + ] |
| 169 | + execute(cmd) |
| 170 | + apkdest = self.get_engine_apk_dest(engdest) |
| 171 | + # Add extra data |
| 172 | + apkxmldest = check_dir(os.path.join(apkdest, "res", "xml"), create=True) |
| 173 | + apklibdest = check_dir(os.path.join(apkdest, "libs", abi.name), create=True) |
| 174 | + for ver, d in self.extra_packs + [("3.1.0", os.path.join(self.libdest, "lib"))]: |
| 175 | + r = ET.Element("library", attrib={"version": ver}) |
| 176 | + log.info("Adding libraries from %s", d) |
| 177 | + |
| 178 | + for f in glob.glob(os.path.join(d, abi.name, "*.so")): |
| 179 | + log.info("Copy file: %s", f) |
| 180 | + shutil.copy2(f, apklibdest) |
| 181 | + if "libnative_camera" in f: |
| 182 | + continue |
| 183 | + log.info("Register file: %s", os.path.basename(f)) |
| 184 | + n = ET.SubElement(r, "file", attrib={"name": os.path.basename(f)}) |
| 185 | + |
| 186 | + if len(list(r)) > 0: |
| 187 | + xmlname = os.path.join(apkxmldest, "config%s.xml" % ver.replace(".", "")) |
| 188 | + log.info("Generating XML config: %s", xmlname) |
| 189 | + ET.ElementTree(r).write(xmlname, encoding="utf-8") |
| 190 | + |
| 191 | + execute(["ninja", "opencv_engine"]) |
| 192 | + execute(["ant", "-f", os.path.join(apkdest, "build.xml"), "debug"], |
| 193 | + shell=(sys.platform == 'win32')) |
| 194 | + # TODO: Sign apk |
| 195 | + |
| 196 | + def build_javadoc(self): |
| 197 | + classpaths = [os.path.join(self.libdest, "bin", "classes")] |
| 198 | + for dir, _, files in os.walk(os.environ["ANDROID_SDK"]): |
| 199 | + for f in files: |
| 200 | + if f == "android.jar" or f == "annotations.jar": |
| 201 | + classpaths.append(os.path.join(dir, f)) |
| 202 | + cmd = [ |
| 203 | + "javadoc", |
| 204 | + "-header", "OpenCV %s" % self.opencv_version, |
| 205 | + "-nodeprecated", |
| 206 | + "-footer", '<a href="http://docs.opencv.org">OpenCV %s Documentation</a>' % self.opencv_version, |
| 207 | + "-public", |
| 208 | + "-sourcepath", os.path.join(self.libdest, "src"), |
| 209 | + "-d", self.docdest, |
| 210 | + "-classpath", ":".join(classpaths) |
| 211 | + ] |
| 212 | + for _, dirs, _ in os.walk(os.path.join(self.libdest, "src", "org", "opencv")): |
| 213 | + cmd.extend(["org.opencv." + d for d in dirs]) |
| 214 | + execute(cmd) |
| 215 | + |
| 216 | + def gather_results(self, engines): |
| 217 | + # Copy all files |
| 218 | + root = os.path.join(self.libdest, "install") |
| 219 | + for item in os.listdir(root): |
| 220 | + name = item |
| 221 | + item = os.path.join(root, item) |
| 222 | + if os.path.isdir(item): |
| 223 | + log.info("Copy dir: %s", item) |
| 224 | + shutil.copytree(item, os.path.join(self.resultdest, name)) |
| 225 | + elif os.path.isfile(item): |
| 226 | + log.info("Copy file: %s", item) |
| 227 | + shutil.copy2(item, os.path.join(self.resultdest, name)) |
| 228 | + |
| 229 | + # Copy engines for all platforms |
| 230 | + for abi, engdest in engines: |
| 231 | + log.info("Copy engine: %s (%s)", abi, engdest) |
| 232 | + f = os.path.join(self.get_engine_apk_dest(engdest), "bin", "opencv_engine-debug.apk") |
| 233 | + resname = "OpenCV_%s_Manager_%s_%s.apk" % (self.opencv_version, self.engine_version, abi) |
| 234 | + shutil.copy2(f, os.path.join(self.resultdest, "apk", resname)) |
| 235 | + |
| 236 | + # Copy javadoc |
| 237 | + log.info("Copy docs: %s", self.docdest) |
| 238 | + shutil.copytree(self.docdest, os.path.join(self.resultdest, "sdk", "java", "javadoc")) |
| 239 | + |
| 240 | + # Clean samples |
| 241 | + path = os.path.join(self.resultdest, "samples") |
| 242 | + for item in os.listdir(path): |
| 243 | + item = os.path.join(path, item) |
| 244 | + if os.path.isdir(item): |
| 245 | + for name in ["build.xml", "local.properties", "proguard-project.txt"]: |
| 246 | + rm_one(os.path.join(item, name)) |
| 247 | + |
| 248 | + |
| 249 | +#=================================================================================================== |
| 250 | + |
| 251 | +if __name__ == "__main__": |
| 252 | + parser = argparse.ArgumentParser(description='Build OpenCV for Android SDK') |
| 253 | + parser.add_argument("work_dir", help="Working directory (and output)") |
| 254 | + parser.add_argument("opencv_dir", help="Path to OpenCV source dir") |
| 255 | + parser.add_argument('--ndk_path', help="Path to Android NDK to use for build") |
| 256 | + parser.add_argument('--sdk_path', help="Path to Android SDK to use for build") |
| 257 | + parser.add_argument("--extra_modules_path", help="Path to extra modules to use for build") |
| 258 | + parser.add_argument('--sign_with', help="Sertificate to sign the Manager apk") |
| 259 | + parser.add_argument('--build_doc', action="store_true", help="Build javadoc") |
| 260 | + parser.add_argument('--no_ccache', action="store_true", help="Do not use ccache during library build") |
| 261 | + parser.add_argument('--extra_pack', action='append', help="provide extra OpenCV libraries for Manager apk in form <version>:<path-to-native-libs>, for example '2.4.11:unpacked/sdk/native/libs'") |
| 262 | + args = parser.parse_args() |
| 263 | + |
| 264 | + log.basicConfig(format='%(message)s', level=log.DEBUG) |
| 265 | + log.debug("Args: %s", args) |
| 266 | + |
| 267 | + if args.ndk_path is not None: |
| 268 | + os.environ["ANDROID_NDK"] = args.ndk_path |
| 269 | + if args.sdk_path is not None: |
| 270 | + os.environ["ANDROID_SDK"] = args.sdk_path |
| 271 | + |
| 272 | + log.info("Android NDK path: %s", os.environ["ANDROID_NDK"]) |
| 273 | + log.info("Android SDK path: %s", os.environ["ANDROID_SDK"]) |
| 274 | + |
| 275 | + builder = Builder(args.work_dir, args.opencv_dir) |
| 276 | + |
| 277 | + if args.extra_modules_path is not None: |
| 278 | + builder.extra_modules_path = os.path.abspath(args.extra_modules_path) |
| 279 | + |
| 280 | + if args.no_ccache: |
| 281 | + builder.use_ccache = False |
| 282 | + |
| 283 | + log.info("Detected OpenCV version: %s", builder.opencv_version) |
| 284 | + log.info("Detected Engine version: %s", builder.engine_version) |
| 285 | + |
| 286 | + if args.extra_pack: |
| 287 | + for one in args.extra_pack: |
| 288 | + i = one.find(":") |
| 289 | + if i > 0 and i < len(one) - 1: |
| 290 | + builder.add_extra_pack(one[:i], one[i+1:]) |
| 291 | + else: |
| 292 | + raise Fail("Bad extra pack provided: %s, should be in form '<version>:<path-to-native-libs>'" % one) |
| 293 | + |
| 294 | + engines = [] |
| 295 | + for i, abi in enumerate(ABIs): |
| 296 | + do_install = (i == 0) |
| 297 | + engdest = check_dir(os.path.join(builder.workdir, "build_service_%s" % abi.name), create=True, clean=True) |
| 298 | + |
| 299 | + log.info("=====") |
| 300 | + log.info("===== Building library for %s", abi) |
| 301 | + log.info("=====") |
| 302 | + |
| 303 | + os.chdir(builder.libdest) |
| 304 | + builder.clean_library_build_dir() |
| 305 | + builder.build_library(abi, do_install) |
| 306 | + |
| 307 | + log.info("=====") |
| 308 | + log.info("===== Building engine for %s", abi) |
| 309 | + log.info("=====") |
| 310 | + |
| 311 | + os.chdir(engdest) |
| 312 | + builder.build_engine(abi, engdest) |
| 313 | + engines.append((abi.name, engdest)) |
| 314 | + |
| 315 | + if args.build_doc: |
| 316 | + builder.build_javadoc() |
| 317 | + |
| 318 | + builder.gather_results(engines) |
| 319 | + |
| 320 | + log.info("=====") |
| 321 | + log.info("===== Build finished") |
| 322 | + log.info("=====") |
| 323 | + log.info("SDK location: %s", builder.resultdest) |
| 324 | + log.info("Documentation location: %s", builder.docdest) |
0 commit comments