Skip to content

Commit

Permalink
Android SDK build Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
mshabunin committed Aug 12, 2015
1 parent 09b9b0f commit ef16650
Show file tree
Hide file tree
Showing 6 changed files with 676 additions and 3 deletions.
4 changes: 2 additions & 2 deletions 3rdparty/ippicv/downloader.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function(_icv_downloader)
set(OPENCV_ICV_PLATFORM "macosx")
set(OPENCV_ICV_PACKAGE_SUBDIR "/ippicv_osx")
elseif(UNIX)
if(ANDROID AND (NOT ANDROID_ABI STREQUAL x86))
if(ANDROID AND NOT (ANDROID_ABI STREQUAL x86 OR ANDROID_ABI STREQUAL x86_64))
return()
endif()
set(OPENCV_ICV_PACKAGE_NAME "ippicv_linux_20141027.tgz")
Expand Down Expand Up @@ -75,7 +75,7 @@ function(_icv_downloader)
message(FATAL_ERROR "ICV: Failed to download ICV package: ${OPENCV_ICV_PACKAGE_NAME}. Status=${__status}")
else()
# Don't remove this code, because EXPECTED_MD5 parameter doesn't fail "file(DOWNLOAD)" step
# on wrong hash
# on wrong hash
file(MD5 "${OPENCV_ICV_PACKAGE_ARCHIVE}" archive_md5)
if(NOT archive_md5 STREQUAL OPENCV_ICV_PACKAGE_HASH)
message(FATAL_ERROR "ICV: Downloaded copy of ICV package has invalid MD5 hash: ${archive_md5} (expected: ${OPENCV_ICV_PACKAGE_HASH})")
Expand Down
78 changes: 78 additions & 0 deletions platforms/android/build-tests/test_ant_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env python

import unittest
import os, sys, subprocess, argparse, shutil, re
from os.path import abspath

class TestAntBuild(unittest.TestCase):
pass

def __init__(self, target, workdir, lib_dir, sample_dir, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self.target = target
self.workdir = workdir
self.src_lib_dir = lib_dir
self.src_sample_dir = sample_dir
self.lib_dir = os.path.join(self.workdir, "opencv")
self.sample_dir = os.path.join(self.workdir, "project")

def shortDescription(self):
return "TARGET: %s, SAMPLE: %s" % (self.target, os.path.basename(self.src_sample_dir))

def setUp(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)
os.mkdir(self.workdir)
shutil.copytree(self.src_lib_dir, self.lib_dir)
shutil.copytree(self.src_sample_dir, self.sample_dir)
os.remove(os.path.join(self.sample_dir, "project.properties"))

def tearDown(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)

def runTest(self):
cmd = [os.path.join(os.environ["ANDROID_SDK"], "tools", "android"), "update", "project", "-p", self.lib_dir, "-t", self.target]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "android update opencv project failed")

cmd = ["ant", "-f", os.path.join(self.lib_dir, "build.xml"), "debug"]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "opencv ant build failed")

cmd = [os.path.join(os.environ["ANDROID_SDK"], "tools", "android"), "update", "project", "-p", self.sample_dir, "-t", self.target, "-l", os.path.relpath(self.lib_dir, self.sample_dir)]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "android update sample project failed")

cmd = ["ant", "-f", os.path.join(self.sample_dir, "build.xml"), "debug"]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "sample ant build failed")

def suite(workdir, opencv_lib_path, opencv_samples_path):
suite = unittest.TestSuite()
for target in ["android-14", "android-17"]:
for item in os.listdir(opencv_samples_path):
item = os.path.join(opencv_samples_path, item)
if (os.path.exists(os.path.join(item, "AndroidManifest.xml"))):
suite.addTest(TestAntBuild(target, workdir, opencv_lib_path, item))
return suite

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test OpenCV for Android SDK with ant')
parser.add_argument('--sdk_path', help="Path to Android SDK to use for build")
parser.add_argument("--workdir", default="testspace", help="Working directory (and output)")
parser.add_argument("opencv_lib_path", help="Path to folder with SDK java library (usually <SDK>/sdk/java/)")
parser.add_argument("opencv_samples_path", help="Path to folder with SDK samples (usually <SDK>/samples/)")

args = parser.parse_args()

if args.sdk_path is not None:
os.environ["ANDROID_SDK"] = os.path.abspath(args.sdk_path)

print("Using SDK: %s" % os.environ["ANDROID_SDK"])

s = suite(abspath(args.workdir), abspath(args.opencv_lib_path), abspath(args.opencv_samples_path))
res = unittest.TextTestRunner(verbosity=3).run(s)
if not res.wasSuccessful():
sys.exit(res)

130 changes: 130 additions & 0 deletions platforms/android/build-tests/test_cmake_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#!/usr/bin/env python

import unittest
import os, sys, subprocess, argparse, shutil, re

CMAKE_TEMPLATE='''\
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(PROJECT_NAME hello-android)
PROJECT(${PROJECT_NAME})
FIND_PACKAGE(OpenCV REQUIRED %(libset)s)
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
FILE(GLOB srcs "*.cpp")
ADD_EXECUTABLE(${PROJECT_NAME} ${srcs})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${OpenCV_LIBS} dl z)
'''

CPP_TEMPLATE = '''\
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
using namespace cv;
const char* message = "Hello Android!";
int main(int argc, char* argv[])
{
(void)argc; (void)argv;
printf("%s\\n", message);
Size textsize = getTextSize(message, CV_FONT_HERSHEY_COMPLEX, 3, 5, 0);
Mat img(textsize.height + 20, textsize.width + 20, CV_32FC1, Scalar(230,230,230));
putText(img, message, Point(10, img.rows - 10), CV_FONT_HERSHEY_COMPLEX, 3, Scalar(0, 0, 0), 5);
imwrite("/mnt/sdcard/HelloAndroid.png", img);
return 0;
}
'''

#===================================================================================================

class TestCmakeBuild(unittest.TestCase):
def __init__(self, libset, abi, toolchain, opencv_cmake_path, workdir, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self.libset = libset
self.abi = abi
self.toolchain = toolchain
self.opencv_cmake_path = opencv_cmake_path
self.workdir = workdir
self.srcdir = os.path.join(self.workdir, "src")
self.bindir = os.path.join(self.workdir, "build")

def shortDescription(self):
return "ABI: %s, TOOLCHAIN: %s, LIBSET: %s" % (self.abi, self.toolchain, self.libset)

def gen_cmakelists(self):
return CMAKE_TEMPLATE % {"libset": self.libset}

def gen_code(self):
return CPP_TEMPLATE

def write_src_file(self, fname, content):
with open(os.path.join(self.srcdir, fname), "w") as f:
f.write(content)

def setUp(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)
os.mkdir(self.workdir)
os.mkdir(self.srcdir)
os.mkdir(self.bindir)
self.write_src_file("CMakeLists.txt", self.gen_cmakelists())
self.write_src_file("main.cpp", self.gen_code())
os.chdir(self.bindir)

def tearDown(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)

def runTest(self):
cmd = [
"cmake",
"-GNinja",
"-DOpenCV_DIR=%s" % self.opencv_cmake_path,
"-DANDROID_ABI=%s" % self.abi,
"-DCMAKE_TOOLCHAIN_FILE=%s" % os.path.join(self.opencv_cmake_path, "android.toolchain.cmake"),
"-DANDROID_TOOLCHAIN_NAME=%s" % self.toolchain,
self.srcdir
]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "cmake failed")

cmd = ["ninja"]
retcode = subprocess.call(cmd)
self.assertEqual(retcode, 0, "make failed")

def suite(workdir, opencv_cmake_path):
abis = {
"armeabi":"arm-linux-androideabi-4.8",
"armeabi-v7a":"arm-linux-androideabi-4.8",
"arm64-v8a":"aarch64-linux-android-4.9",
"x86":"x86-4.8",
"x86_64":"x86_64-4.9",
"mips":"mipsel-linux-android-4.8",
"mips64":"mips64el-linux-android-4.9"
}

suite = unittest.TestSuite()
for libset in ["", "opencv_java"]:
for abi, toolchain in abis.items():
suite.addTest(TestCmakeBuild(libset, abi, toolchain, opencv_cmake_path, workdir))
return suite


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test OpenCV for Android SDK with cmake')
parser.add_argument('--sdk_path', help="Path to Android SDK to use for build")
parser.add_argument('--ndk_path', help="Path to Android NDK to use for build")
parser.add_argument("--workdir", default="testspace", help="Working directory (and output)")
parser.add_argument("opencv_cmake_path", help="Path to folder with OpenCVConfig.cmake and android.toolchain.cmake (usually <SDK>/sdk/native/jni/")

args = parser.parse_args()

if args.sdk_path is not None:
os.environ["ANDROID_SDK"] = os.path.abspath(args.sdk_path)
if args.ndk_path is not None:
os.environ["ANDROID_NDK"] = os.path.abspath(args.ndk_path)

print("Using SDK: %s" % os.environ["ANDROID_SDK"])
print("Using NDK: %s" % os.environ["ANDROID_NDK"])

res = unittest.TextTestRunner(verbosity=3).run(suite(os.path.abspath(args.workdir), os.path.abspath(args.opencv_cmake_path)))
if not res.wasSuccessful():
sys.exit(res)
145 changes: 145 additions & 0 deletions platforms/android/build-tests/test_ndk_build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/usr/bin/env python

import unittest
import os, sys, subprocess, argparse, shutil, re

TEMPLATE_ANDROID_MK = '''\
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
{cut}
LOCAL_MODULE := mixed_sample
LOCAL_SRC_FILES := {cpp1}
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
{cut}
LOCAL_MODULE := mixed_sample2
LOCAL_SRC_FILES := {cpp2}
LOCAL_LDLIBS += -llog -ldl
LOCAL_SHARED_LIBS := mixed_sample
include $(BUILD_SHARED_LIBRARY)
'''

TEMPLATE_APPLICATION_MK = '''\
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := {abi}
APP_PLATFORM := android-9
'''

TEMPLATE_JNI = '''\
#include <jni.h>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/features2d.hpp>
#include <vector>
using namespace std;
using namespace cv;
extern "C" {
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4Mixed_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba);
JNIEXPORT void JNICALL Java_org_opencv_samples_tutorial4_Sample4Mixed_FindFeatures(JNIEnv*, jobject, jlong addrGray, jlong addrRgba)
{
Mat& mGr = *(Mat*)addrGray;
Mat& mRgb = *(Mat*)addrRgba;
vector<KeyPoint> v;
Ptr<FastFeatureDetector> detector = FastFeatureDetector::create(50);
detector->detect(mGr, v);
for( unsigned int i = 0; i < v.size(); i++ )
{
const KeyPoint& kp = v[i];
circle(mRgb, Point(kp.pt.x, kp.pt.y), 10, Scalar(255,0,0,255));
}
}
}
'''

#===================================================================================================

class TestNDKBuild(unittest.TestCase):
def __init__(self, abi, libtype, opencv_mk_path, workdir, *args, **kwargs):
unittest.TestCase.__init__(self, *args, **kwargs)
self.abi = abi # official NDK ABI name or 'all'
self.libtype = libtype # 'static', etc
self.opencv_mk_path = opencv_mk_path
self.workdir = workdir
self.jnidir = os.path.join(self.workdir, "jni")
self.cpp1 = "jni_part1.cpp"
self.cpp2 = "jni_part2.cpp"

def shortDescription(self):
return "ABI: %s, LIBTYPE: %s" % (self.abi, self.libtype)

def gen_android_mk(self):
p = []
if self.libtype == "static":
p.append("OPENCV_LIB_TYPE := STATIC")
elif self.libtype == "shared_debug":
p.append("OPENCV_LIB_TYPE := SHARED")
p.append("OPENCV_CAMERA_MODULES:=on")
p.append("OPENCV_INSTALL_MODULES:=on")
elif self.libtype == "shared":
p.append("OPENCV_LIB_TYPE := SHARED")
p.append("include %s" % os.path.join(self.opencv_mk_path, "OpenCV.mk"))
return TEMPLATE_ANDROID_MK.format(cut = "\n".join(p), cpp1 = self.cpp1, cpp2 = self.cpp2)

def gen_jni_code(self):
return TEMPLATE_JNI

def gen_application_mk(self):
return TEMPLATE_APPLICATION_MK.format(abi = self.abi)

def write_jni_file(self, fname, contents):
with open(os.path.join(self.jnidir, fname), "w") as f:
f.write(contents)

def setUp(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)
os.mkdir(self.workdir)
os.mkdir(self.jnidir)
self.write_jni_file("Android.mk", self.gen_android_mk())
self.write_jni_file("Application.mk", self.gen_application_mk())
self.write_jni_file(self.cpp1, self.gen_jni_code())
self.write_jni_file(self.cpp2, self.gen_jni_code())
os.chdir(self.workdir)

def tearDown(self):
if os.path.exists(self.workdir):
shutil.rmtree(self.workdir)

def runTest(self):
ndk_path = os.environ["ANDROID_NDK"]
retcode = subprocess.call([os.path.join(ndk_path, 'ndk-build'), "V=0"])
self.assertEqual(retcode, 0)

def suite(workdir, opencv_mk_path):
abis = ["armeabi", "armeabi-v7a", "x86", "mips"]
ndk_path = os.environ["ANDROID_NDK"]
with open(os.path.join(ndk_path, "RELEASE.TXT"), "r") as f:
s = f.read()
if re.search(r'r10[b-e]', s):
abis.extend(["arm64-v8a", "x86", "x86_64"])
abis.append("all")

suite = unittest.TestSuite()
for libtype in ["static", "shared", "shared_debug"]:
for abi in abis:
suite.addTest(TestNDKBuild(abi, libtype, opencv_mk_path, workdir))
return suite

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Test OpenCV for Android SDK with NDK')
parser.add_argument('--ndk_path', help="Path to Android NDK to use for build")
parser.add_argument("--workdir", default="testspace", help="Working directory (and output)")
parser.add_argument("opencv_mk_path", help="Path to folder with OpenCV.mk file (usually <SDK>/sdk/native/jni/")

args = parser.parse_args()

if args.ndk_path is not None:
os.environ["ANDROID_NDK"] = os.path.abspath(args.ndk_path)

print("Using NDK: %s" % os.environ["ANDROID_NDK"])

res = unittest.TextTestRunner(verbosity=3).run(suite(os.path.abspath(args.workdir), os.path.abspath(args.opencv_mk_path)))
if not res.wasSuccessful():
sys.exit(res)
Loading

0 comments on commit ef16650

Please sign in to comment.