run_tests.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #!/usr/bin/env python
  2. """
  3. This script runs OpenCV.framework tests for iOS.
  4. """
  5. from __future__ import print_function
  6. import glob, re, os, os.path, shutil, string, sys, argparse, traceback, multiprocessing
  7. from subprocess import check_call, check_output, CalledProcessError
  8. IPHONEOS_DEPLOYMENT_TARGET='9.0' # default, can be changed via command line options or environment variable
  9. def execute(cmd, cwd = None):
  10. print("Executing: %s in %s" % (cmd, cwd), file=sys.stderr)
  11. print('Executing: ' + ' '.join(cmd))
  12. retcode = check_call(cmd, cwd = cwd)
  13. if retcode != 0:
  14. raise Exception("Child returned:", retcode)
  15. class TestRunner:
  16. def __init__(self, script_dir, tests_dir, build_dir, framework_dir, framework_name, arch, target, platform):
  17. self.script_dir = script_dir
  18. self.tests_dir = tests_dir
  19. self.build_dir = build_dir
  20. self.framework_dir = framework_dir
  21. self.framework_name = framework_name
  22. self.arch = arch
  23. self.target = target
  24. self.platform = platform
  25. def _run(self):
  26. if not os.path.isdir(self.build_dir):
  27. os.makedirs(self.build_dir)
  28. self.runTest()
  29. def run(self):
  30. try:
  31. self._run()
  32. except Exception as e:
  33. print("="*60, file=sys.stderr)
  34. print("ERROR: %s" % e, file=sys.stderr)
  35. print("="*60, file=sys.stderr)
  36. traceback.print_exc(file=sys.stderr)
  37. sys.exit(1)
  38. def getToolchain(self):
  39. return None
  40. def getCMakeArgs(self):
  41. args = [
  42. "cmake",
  43. "-GXcode",
  44. "-DFRAMEWORK_DIR=%s" % self.framework_dir,
  45. "-DFRAMEWORK_NAME=%s" % self.framework_name,
  46. ]
  47. return args
  48. def makeCMakeCmd(self):
  49. toolchain = self.getToolchain()
  50. cmakecmd = self.getCMakeArgs() + \
  51. (["-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain] if toolchain is not None else []) + \
  52. ["-DCMAKE_INSTALL_NAME_TOOL=install_name_tool"]
  53. cmakecmd.append(self.tests_dir)
  54. return cmakecmd
  55. def runTest(self):
  56. cmakecmd = self.makeCMakeCmd()
  57. execute(cmakecmd, cwd = self.build_dir)
  58. buildcmd = self.getTestCommand()
  59. execute(buildcmd, cwd = self.build_dir)
  60. def getTestCommand(self):
  61. testcmd = [
  62. "xcodebuild",
  63. "test",
  64. "-project", "OpenCVTest.xcodeproj",
  65. "-scheme", "OpenCVTestTests",
  66. "-destination", "platform=%s" % self.platform
  67. ]
  68. return testcmd
  69. class iOSTestRunner(TestRunner):
  70. def getToolchain(self):
  71. toolchain = os.path.join(self.script_dir, "cmake", "Toolchains", "Toolchain-%s_Xcode.cmake" % self.target)
  72. return toolchain
  73. def getCMakeArgs(self):
  74. args = TestRunner.getCMakeArgs(self)
  75. args = args + [
  76. "-DIOS_ARCH=%s" % self.arch,
  77. "-DIPHONEOS_DEPLOYMENT_TARGET=%s" % os.environ['IPHONEOS_DEPLOYMENT_TARGET'],
  78. ]
  79. return args
  80. if __name__ == "__main__":
  81. script_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
  82. parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for iOS.')
  83. parser.add_argument('tests_dir', metavar='TEST_DIR', help='folder where test files are located')
  84. parser.add_argument('--build_dir', default=None, help='folder where test will be built (default is "../test_build" relative to tests_dir)')
  85. parser.add_argument('--framework_dir', default=None, help='folder where OpenCV framework is located')
  86. parser.add_argument('--framework_name', default='opencv2', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
  87. parser.add_argument('--iphoneos_deployment_target', default=os.environ.get('IPHONEOS_DEPLOYMENT_TARGET', IPHONEOS_DEPLOYMENT_TARGET), help='specify IPHONEOS_DEPLOYMENT_TARGET')
  88. parser.add_argument('--platform', default='iOS Simulator,name=iPhone 11', help='xcodebuild platform parameter (default is iOS 11 simulator)')
  89. args = parser.parse_args()
  90. os.environ['IPHONEOS_DEPLOYMENT_TARGET'] = args.iphoneos_deployment_target
  91. print('Using IPHONEOS_DEPLOYMENT_TARGET=' + os.environ['IPHONEOS_DEPLOYMENT_TARGET'])
  92. arch = "x86_64"
  93. target = "iPhoneSimulator"
  94. print('Using iPhoneSimulator ARCH=' + arch)
  95. r = iOSTestRunner(script_dir, args.tests_dir, args.build_dir if args.build_dir else os.path.join(args.tests_dir, "../test_build"), args.framework_dir, args.framework_name, arch, target, args.platform)
  96. r.run()