build_docs.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. """
  3. This script builds OpenCV docs for iOS.
  4. """
  5. from __future__ import print_function
  6. import os, sys, multiprocessing, argparse, traceback
  7. from subprocess import check_call, check_output, CalledProcessError, Popen
  8. def execute(cmd, cwd = None, output = None):
  9. if not output:
  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. else:
  16. with open(output, "a") as f:
  17. f.flush()
  18. p = Popen(cmd, cwd = cwd, stdout = f)
  19. os.waitpid(p.pid, 0)
  20. class DocBuilder:
  21. def __init__(self, script_dir, framework_dir, output_dir, framework_header, framework_name, arch, target):
  22. self.script_dir = script_dir
  23. self.framework_dir = framework_dir
  24. self.output_dir = output_dir
  25. self.framework_header = framework_header
  26. self.framework_name = framework_name
  27. self.arch = arch
  28. self.target = target
  29. def _build(self):
  30. if not os.path.isdir(self.output_dir):
  31. os.makedirs(self.output_dir)
  32. self.buildDocs()
  33. def build(self):
  34. try:
  35. self._build()
  36. except Exception as e:
  37. print("="*60, file=sys.stderr)
  38. print("ERROR: %s" % e, file=sys.stderr)
  39. print("="*60, file=sys.stderr)
  40. traceback.print_exc(file=sys.stderr)
  41. sys.exit(1)
  42. def getToolchain(self):
  43. return None
  44. def getSourceKitten(self):
  45. ret = check_output(["gem", "which", "jazzy"])
  46. if ret.find('ERROR:') == 0:
  47. raise Exception("Failed to find jazzy")
  48. else:
  49. return os.path.join(ret[0:ret.rfind('/')], '../bin/sourcekitten')
  50. def buildDocs(self):
  51. sourceKitten = self.getSourceKitten()
  52. sourceKittenSwiftDoc = [sourceKitten, "doc", "--module-name", self.framework_name, "--", "-project", self.framework_name + ".xcodeproj", "ARCHS=" + self.arch, "-sdk", self.target, "-configuration", "Release", "-parallelizeTargets", "-jobs", str(multiprocessing.cpu_count()), "-target", "opencv_objc_framework"]
  53. execute(sourceKittenSwiftDoc, cwd = self.framework_dir, output = os.path.join(self.output_dir, "swiftDoc.json"))
  54. sdk_dir = check_output(["xcrun", "--show-sdk-path", "--sdk", self.target]).rstrip()
  55. sourceKittenObjcDoc = [sourceKitten, "doc", "--objc", self.framework_header, "--", "-x", "objective-c", "-isysroot", sdk_dir, "-fmodules"]
  56. print(sourceKittenObjcDoc)
  57. execute(sourceKittenObjcDoc, cwd = self.framework_dir, output = os.path.join(self.output_dir, "objcDoc.json"))
  58. execute(["jazzy", "--author", "OpenCV", "--author_url", "http://opencv.org", "--github_url", "https://github.com/opencv/opencv", "--module", self.framework_name, "--undocumented-text", "\"\"", "--sourcekitten-sourcefile", "swiftDoc.json,objcDoc.json"], cwd = self.output_dir)
  59. class iOSDocBuilder(DocBuilder):
  60. def getToolchain(self):
  61. return None
  62. if __name__ == "__main__":
  63. script_dir = os.path.abspath(os.path.dirname(sys.argv[0]))
  64. parser = argparse.ArgumentParser(description='The script builds OpenCV docs for iOS.')
  65. parser.add_argument('framework_dir', metavar='FRAMEWORK_DIR', help='folder where framework build files are located')
  66. parser.add_argument('--output_dir', default=None, help='folder where docs will be built (default is "../doc_build" relative to framework_dir)')
  67. parser.add_argument('--framework_header', default=None, help='umbrella header for OpenCV framework (default is "../../../lib/Release/{framework_name}.framework/Headers/{framework_name}.h")')
  68. parser.add_argument('--framework_name', default='opencv2', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
  69. args = parser.parse_args()
  70. arch = "x86_64"
  71. target = "iphonesimulator"
  72. b = iOSDocBuilder(script_dir, args.framework_dir, args.output_dir if args.output_dir else os.path.join(args.framework_dir, "../doc_build"), args.framework_header if args.framework_header else os.path.join(args.framework_dir, "../../../lib/Release/" + args.framework_name + ".framework/Headers/" + args.framework_name + ".h"), args.framework_name, arch, target)
  73. b.build()