1313def main ():
1414os .chdir (os .path .dirname (os .path .abspath (__file__ )))
1515
16- # These are needed for source fetching
16+ CI_BUILD = os .environ .get ("CI_BUILD" ,"False" )
17+ is_CI_build = True if CI_BUILD == "True" else False
1718cmake_source_dir = "opencv"
1819minimum_supported_numpy = "1.13.1"
1920build_contrib = get_build_env_var_by_name ("contrib" )
20- # headless flag to skip GUI deps if needed
2121build_headless = get_build_env_var_by_name ("headless" )
2222
2323if sys .version_info [:2 ]>= (3 ,6 ):
@@ -30,11 +30,16 @@ def main():
3030numpy_version = "numpy>=%s" % minimum_supported_numpy
3131
3232python_version = cmaker .CMaker .get_python_version ()
33- python_lib_path = cmaker .CMaker .get_python_library (python_version ).replace ('\\ ' ,'/' )
34- python_include_dir = cmaker .CMaker .get_python_include_dir (python_version ).replace ('\\ ' ,'/' )
33+ python_lib_path = cmaker .CMaker .get_python_library (python_version ).replace (
34+ "\\ " ,"/"
35+ )
36+ python_include_dir = cmaker .CMaker .get_python_include_dir (python_version ).replace (
37+ "\\ " ,"/"
38+ )
3539
3640if os .path .exists (".git" ):
3741import pip ._internal .vcs .git as git
42+
3843g = git .Git ()# NOTE: pip API's are internal, this has to be refactored
3944
4045g .run_command (["submodule" ,"sync" ])
@@ -47,7 +52,9 @@ def main():
4752 ["submodule" ,"update" ,"--init" ,"--recursive" ,"opencv_contrib" ]
4853 )
4954
50- package_version ,build_contrib ,build_headless = get_opencv_version (build_contrib ,build_headless )
55+ package_version ,build_contrib ,build_headless = get_opencv_version (
56+ build_contrib ,build_headless
57+ )
5158
5259# https://stackoverflow.com/questions/1405913/python-32bit-or-64bit-mode
5360x64 = sys .maxsize > 2 ** 32
@@ -86,7 +93,10 @@ def main():
8693# In Windows, in python/X.Y/<arch>/; in Linux, in just python/X.Y/.
8794# Naming conventions vary so widely between versions and OSes
8895# had to give up on checking them.
89- ["python/cv2[^/]*%(ext)s" % {"ext" :re .escape (sysconfig .get_config_var ("EXT_SUFFIX" ))}],
96+ [
97+ "python/cv2[^/]*%(ext)s"
98+ % {"ext" :re .escape (sysconfig .get_config_var ("EXT_SUFFIX" ))}
99+ ],
90100"cv2.data" : [# OPENCV_OTHER_INSTALL_PATH
91101 ("etc" if os .name == "nt" else "share/opencv4" )+ r"/haarcascades/.*\.xml"
92102 ],
@@ -96,12 +106,14 @@ def main():
96106# Raw paths relative to sourcetree root.
97107files_outside_package_dir = {"cv2" : ["LICENSE.txt" ,"LICENSE-3RD-PARTY.txt" ]}
98108
109+ ci_cmake_generator = (
110+ ["-G" ,"Visual Studio 14" + (" Win64" if x64 else "" )]
111+ if os .name == "nt"
112+ else ["-G" ,"Unix Makefiles" ]
113+ )
114+
99115cmake_args = (
100- (
101- ["-G" ,"Visual Studio 14" + (" Win64" if x64 else "" )]
102- if os .name == "nt"
103- else ["-G" ,"Unix Makefiles" ]# don't make CMake try (and fail) Ninja first
104- )
116+ (ci_cmake_generator if is_CI_build else [])
105117+ [
106118# skbuild inserts PYTHON_* vars. That doesn't satisfy opencv build scripts in case of Py3
107119"-DPYTHON3_EXECUTABLE=%s" % sys .executable ,
@@ -131,33 +143,40 @@ def main():
131143 )
132144 )
133145
134- # OS-specific components
135- if sys .platform .startswith ('linux' )and not build_headless :
136- cmake_args .append ("-DWITH_QT=4" )
137-
138- if sys .platform == 'darwin' and not build_headless :
139- cmake_args .append ("-DWITH_QT=5" )
140- rearrange_cmake_output_data ["cv2.qt.plugins.platforms" ]= [
141- (r"lib/qt/plugins/platforms/libqcocoa\.dylib" )
142- ]
143-
144146if build_headless :
145147# it seems that cocoa cannot be disabled so on macOS the package is not truly headless
146148cmake_args .append ("-DWITH_WIN32UI=OFF" )
147149cmake_args .append ("-DWITH_QT=OFF" )
148- cmake_args .append (
149- "-DWITH_MSMF=OFF"
150- )# see: https://github.com/skvark/opencv-python/issues/263
150+ cmake_args .append ("-DWITH_GTK=OFF" )
151+ if is_CI_build :
152+ cmake_args .append (
153+ "-DWITH_MSMF=OFF"
154+ )# see: https://github.com/skvark/opencv-python/issues/263
155+
156+ # OS-specific components during CI builds
157+ if is_CI_build :
158+ if sys .platform .startswith ("linux" )and not build_headless :
159+ cmake_args .append ("-DWITH_QT=4" )
151160
152- if sys .platform .startswith ("linux" ):
153- cmake_args .append ("-DWITH_V4L=ON" )
154- cmake_args .append ("-DWITH_LAPACK=ON" )
155- cmake_args .append ("-DENABLE_PRECOMPILED_HEADERS=OFF" )
161+ if sys .platform == "darwin" and not build_headless :
162+ cmake_args .append ("-DWITH_QT=5" )
163+
164+ if sys .platform .startswith ("linux" ):
165+ cmake_args .append ("-DWITH_V4L=ON" )
166+ cmake_args .append ("-DWITH_LAPACK=ON" )
167+ cmake_args .append ("-DENABLE_PRECOMPILED_HEADERS=OFF" )
156168
157169if sys .platform .startswith ("linux" )and not x64 and "bdist_wheel" in sys .argv :
158170subprocess .check_call ("patch -p0 < patches/patchOpenEXR" ,shell = True )
159171
160- if sys .platform == "darwin" and "bdist_wheel" in sys .argv :
172+ if (
173+ sys .platform == "darwin"
174+ and "bdist_wheel" in sys .argv
175+ and ("WITH_QT=5" in sys .argv or "WITH_QT=5" in cmake_args )
176+ ):
177+ rearrange_cmake_output_data ["cv2.qt.plugins.platforms" ]= [
178+ (r"lib/qt/plugins/platforms/libqcocoa\.dylib" )
179+ ]
161180subprocess .check_call ("patch -p1 < patches/patchQtPlugins" ,shell = True )
162181
163182# works via side effect
@@ -357,22 +376,25 @@ def _classify_installed_files_override(
357376cmake_install_dir = cmake_install_reldir ,
358377 )
359378
379+
360380def get_opencv_version (contrib ,headless ):
361381# cv2/version.py should be generated by running find_version.py
362382version = {}
363383here = os .path .abspath (os .path .dirname (__file__ ))
364384version_file = os .path .join (here ,"cv2" ,"version.py" )
365385
366- if not os .path .exists (version_file ):
367- old_args = sys .argv .copy ()
368- sys .argv = ['' ,str (contrib ),str (headless )]
369- runpy .run_path ("find_version.py" ,run_name = "__main__" )
370- sys .argv = old_args
386+ # generate a fresh version.py always when Git repository exists
387+ if os .path .exists (".git" ):
388+ old_args = sys .argv .copy ()
389+ sys .argv = ["" ,str (contrib ),str (headless )]
390+ runpy .run_path ("find_version.py" ,run_name = "__main__" )
391+ sys .argv = old_args
371392
372393with open (version_file )as fp :
373394exec (fp .read (),version )
374395
375- return version ['opencv_version' ],version ['contrib' ],version ['headless' ]
396+ return version ["opencv_version" ],version ["contrib" ],version ["headless" ]
397+
376398
377399def get_build_env_var_by_name (flag_name ):
378400flag_set = False
@@ -390,6 +412,7 @@ def get_build_env_var_by_name(flag_name):
390412
391413return flag_set
392414
415+
393416# This creates a list which is empty but returns a length of 1.
394417# Should make the wheel a binary distribution and platlib compliant.
395418class EmptyListWithLength (list ):