build_framework.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. """
  3. The script builds OpenCV.framework for OSX.
  4. """
  5. from __future__ import print_function
  6. import os, os.path, sys, argparse, traceback, multiprocessing
  7. # import common code
  8. sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios'))
  9. from build_framework import Builder
  10. sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../apple'))
  11. from cv_build_utils import print_error, get_cmake_version
  12. MACOSX_DEPLOYMENT_TARGET='10.12' # default, can be changed via command line options or environment variable
  13. class OSXBuilder(Builder):
  14. def checkCMakeVersion(self):
  15. assert get_cmake_version() >= (3, 17), "CMake 3.17 or later is required. Current version is {}".format(get_cmake_version())
  16. def getObjcTarget(self, target):
  17. # Obj-C generation target
  18. if target == "Catalyst":
  19. return 'ios'
  20. else:
  21. return 'osx'
  22. def getToolchain(self, arch, target):
  23. return None
  24. def getBuildCommand(self, arch, target):
  25. buildcmd = [
  26. "xcodebuild",
  27. "MACOSX_DEPLOYMENT_TARGET=" + os.environ['MACOSX_DEPLOYMENT_TARGET'],
  28. "ARCHS=%s" % arch,
  29. "-sdk", "macosx" if target == "Catalyst" else target.lower(),
  30. "-configuration", "Debug" if self.debug else "Release",
  31. "-parallelizeTargets",
  32. "-jobs", str(multiprocessing.cpu_count())
  33. ]
  34. if target == "Catalyst":
  35. buildcmd.append("-destination 'platform=macOS,arch=%s,variant=Mac Catalyst'" % arch)
  36. buildcmd.append("-UseModernBuildSystem=YES")
  37. buildcmd.append("SKIP_INSTALL=NO")
  38. buildcmd.append("BUILD_LIBRARY_FOR_DISTRIBUTION=YES")
  39. buildcmd.append("TARGETED_DEVICE_FAMILY=\"1,2\"")
  40. buildcmd.append("SDKROOT=iphoneos")
  41. buildcmd.append("SUPPORTS_MAC_CATALYST=YES")
  42. return buildcmd
  43. def getInfoPlist(self, builddirs):
  44. return os.path.join(builddirs[0], "osx", "Info.plist")
  45. if __name__ == "__main__":
  46. folder = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../.."))
  47. parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for OSX.')
  48. # TODO: When we can make breaking changes, we should make the out argument explicit and required like in build_xcframework.py.
  49. parser.add_argument('out', metavar='OUTDIR', help='folder to put built framework')
  50. parser.add_argument('--opencv', metavar='DIR', default=folder, help='folder with opencv repository (default is "../.." relative to script location)')
  51. parser.add_argument('--contrib', metavar='DIR', default=None, help='folder with opencv_contrib repository (default is "None" - build only main framework)')
  52. parser.add_argument('--without', metavar='MODULE', default=[], action='append', help='OpenCV modules to exclude from the framework. To exclude multiple, specify this flag again, e.g. "--without video --without objc"')
  53. parser.add_argument('--disable', metavar='FEATURE', default=[], action='append', help='OpenCV features to disable (add WITH_*=OFF). To disable multiple, specify this flag again, e.g. "--disable tbb --disable openmp"')
  54. parser.add_argument('--dynamic', default=False, action='store_true', help='build dynamic framework (default is "False" - builds static framework)')
  55. parser.add_argument('--enable_nonfree', default=False, dest='enablenonfree', action='store_true', help='enable non-free modules (disabled by default)')
  56. parser.add_argument('--macosx_deployment_target', default=os.environ.get('MACOSX_DEPLOYMENT_TARGET', MACOSX_DEPLOYMENT_TARGET), help='specify MACOSX_DEPLOYMENT_TARGET')
  57. parser.add_argument('--build_only_specified_archs', default=False, action='store_true', help='if enabled, only directly specified archs are built and defaults are ignored')
  58. parser.add_argument('--archs', default=None, help='(Deprecated! Prefer --macos_archs instead.) Select target ARCHS (set to "x86_64,arm64" to build Universal Binary for Big Sur and later). Default is "x86_64".')
  59. parser.add_argument('--macos_archs', default=None, help='Select target ARCHS (set to "x86_64,arm64" to build Universal Binary for Big Sur and later). Default is "x86_64"')
  60. parser.add_argument('--catalyst_archs', default=None, help='Select target ARCHS (set to "x86_64,arm64" to build Universal Binary for Big Sur and later). Default is None')
  61. parser.add_argument('--debug', action='store_true', help='Build "Debug" binaries (CMAKE_BUILD_TYPE=Debug)')
  62. parser.add_argument('--debug_info', action='store_true', help='Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON)')
  63. parser.add_argument('--framework_name', default='opencv2', dest='framework_name', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
  64. parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")')
  65. parser.add_argument('--run_tests', default=False, dest='run_tests', action='store_true', help='Run tests')
  66. parser.add_argument('--build_docs', default=False, dest='build_docs', action='store_true', help='Build docs')
  67. parser.add_argument('--disable-swift', default=False, dest='swiftdisabled', action='store_true', help='Disable building of Swift extensions')
  68. args, unknown_args = parser.parse_known_args()
  69. if unknown_args:
  70. print("The following args are not recognized and will not be used: %s" % unknown_args)
  71. os.environ['MACOSX_DEPLOYMENT_TARGET'] = args.macosx_deployment_target
  72. print('Using MACOSX_DEPLOYMENT_TARGET=' + os.environ['MACOSX_DEPLOYMENT_TARGET'])
  73. macos_archs = None
  74. if args.archs:
  75. # The archs flag is replaced by macos_archs. If the user specifies archs,
  76. # treat it as if the user specified the macos_archs flag instead.
  77. args.macos_archs = args.archs
  78. print("--archs is deprecated! Prefer --macos_archs instead.")
  79. if args.macos_archs:
  80. macos_archs = args.macos_archs.split(',')
  81. elif not args.build_only_specified_archs:
  82. # Supply defaults
  83. macos_archs = ["x86_64"]
  84. print('Using MacOS ARCHS=' + str(macos_archs))
  85. catalyst_archs = None
  86. if args.catalyst_archs:
  87. catalyst_archs = args.catalyst_archs.split(',')
  88. # TODO: To avoid breaking existing CI, catalyst_archs has no defaults. When we can make a breaking change, this should specify a default arch.
  89. print('Using Catalyst ARCHS=' + str(catalyst_archs))
  90. # Prevent the build from happening if the same architecture is specified for multiple platforms.
  91. # When `lipo` is run to stitch the frameworks together into a fat framework, it'll fail, so it's
  92. # better to stop here while we're ahead.
  93. if macos_archs and catalyst_archs:
  94. duplicate_archs = set(macos_archs).intersection(catalyst_archs)
  95. if duplicate_archs:
  96. print_error("Cannot have the same architecture for multiple platforms in a fat framework! Consider using build_xcframework.py in the apple platform folder instead. Duplicate archs are %s" % duplicate_archs)
  97. exit(1)
  98. if args.legacy_build:
  99. args.framework_name = "opencv2"
  100. if not "objc" in args.without:
  101. args.without.append("objc")
  102. targets = []
  103. if not macos_archs and not catalyst_archs:
  104. print_error("--macos_archs and --catalyst_archs are undefined; nothing will be built.")
  105. sys.exit(1)
  106. if macos_archs:
  107. targets.append((macos_archs, "MacOSX"))
  108. if catalyst_archs:
  109. targets.append((catalyst_archs, "Catalyst")),
  110. b = OSXBuilder(args.opencv, args.contrib, args.dynamic, True, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs, args.swiftdisabled)
  111. b.build(args.out)