1+ from datetime import date
12import glob
23import io
34import os
45import os .path
56import sys
6- import runpy
77import subprocess
88import re
99import sysconfig
@@ -22,6 +22,7 @@ def main():
2222 build_java = "ON" if get_build_env_var_by_name ("java" ) else "OFF"
2323 build_rolling = get_build_env_var_by_name ("rolling" )
2424
25+ # TODO(@Breakthrough): What architectures should we ship?
2526 # fermi = "2.0"
2627 # kepler = "3.0;3.5;3.7"
2728 # maxwell = "5.0;5.2"
@@ -32,7 +33,7 @@ def main():
3233 # lovelace = "8.9"
3334 # hopper = "9.0"
3435 # blackwell = "10.0;12.0"
35- cuda_arch_bin = "6.0;6.1;7.0;7.5,8.0"
36+ cuda_arch_bin = "5.0;5.2; 6.0;6.1;7.0;7.5,8.0"
3637 cuda_arch_ptx = "8.0"
3738
3839 # NOTE: since 2.3.0 numpy upgraded from manylinux2014 to manylinux_2_28
@@ -81,8 +82,8 @@ def main():
8182 ["submodule" , "update" , "--init" , "--recursive" , "opencv_contrib" ]
8283 )
8384
84- package_version , build_contrib , build_headless , build_rolling = get_and_set_info (
85- build_contrib , build_headless , build_rolling , is_CI_build , cuda_arch_bin , cuda_arch_ptx
85+ package_version , build_contrib , build_headless , build_rolling = generate_version_file (
86+ build_contrib , build_headless , build_rolling , cuda_arch_bin , cuda_arch_ptx
8687 )
8788
8889 # https://stackoverflow.com/questions/1405913/python-32bit-or-64bit-mode
@@ -500,24 +501,83 @@ def _classify_installed_files_override(
500501 )
501502
502503
503- def get_and_set_info (contrib , headless , rolling , ci_build , cuda_arch_bin , cuda_arch_ptx ):
504- # cv2/version.py should be generated by running find_version.py
504+ def generate_version_file (contrib , headless , rolling , cuda_arch_bin , cuda_arch_ptx ):
505505 version = {}
506- here = os .path .abspath (os .path .dirname (__file__ ))
507- version_file = os .path .join (here , "cv2" , "version.py" )
508506
509- # generate a fresh version.py always when Git repository exists
510- # (in sdists the version.py file already exists)
511- if os .path .exists (".git" ):
512- old_args = sys .argv .copy ()
513- sys .argv = ["" , str (contrib ), str (headless ), str (rolling ), str (ci_build ), str (cuda_arch_bin ), str (cuda_arch_ptx )]
514- runpy .run_path ("find_version.py" , run_name = "__main__" )
515- sys .argv = old_args
507+ # generate version.py
508+ opencv_version = ""
509+ # dig out the version from OpenCV sources
510+ version_file_path = "opencv/modules/core/include/opencv2/core/version.hpp"
516511
517- with open (version_file ) as fp :
518- exec (fp .read (), version )
512+ with open (version_file_path , "r" ) as f :
513+ for line in f :
514+ words = line .split ()
519515
520- return version ["opencv_version" ], version ["contrib" ], version ["headless" ], version ["rolling" ]
516+ if "CV_VERSION_MAJOR" in words :
517+ opencv_version += words [2 ]
518+ opencv_version += "."
519+
520+ if "CV_VERSION_MINOR" in words :
521+ opencv_version += words [2 ]
522+ opencv_version += "."
523+
524+ if "CV_VERSION_REVISION" in words :
525+ opencv_version += words [2 ]
526+ break
527+
528+ # used in local dev releases
529+ git_hash = (
530+ subprocess .check_output (["git" , "rev-parse" , "--short" , "HEAD" ])
531+ .splitlines ()[0 ]
532+ .decode ()
533+ )
534+ # this outputs the annotated tag if we are exactly on a tag, otherwise <tag>-<n>-g<shortened sha-1>
535+ try :
536+ tag = (
537+ subprocess .check_output (
538+ ["git" , "describe" , "--tags" ], stderr = subprocess .STDOUT
539+ )
540+ .splitlines ()[0 ]
541+ .decode ()
542+ .split ("-" )
543+ )
544+ except subprocess .CalledProcessError as e :
545+ # no tags reachable (e.g. on a topic branch in a fork), see
546+ # https://stackoverflow.com/questions/4916492/git-describe-fails-with-fatal-no-names-found-cannot-describe-anything
547+ if e .output .rstrip () == b"fatal: No names found, cannot describe anything." :
548+ tag = []
549+ else :
550+ print (e .output )
551+ raise
552+
553+
554+ # TODO(@Breakthrough): Update this so that we just use the tag name directly as the version
555+ # identifier. We might need to strip off a "v" suffix if we include that in the repo, but it
556+ # will greatly simplify things. This can also get out of sync since we cache artifacts now.
557+
558+ if len (tag ) == 1 :
559+ # tag identifies the build and should be a sequential revision number
560+ version = tag [0 ]
561+ opencv_version += ".{}" .format (version )
562+ # rolling has converted into string using generate_version_file() function in setup.py
563+ elif rolling == "True" :
564+ # rolling version identifier, will be published in a dedicated rolling PyPI repository
565+ version = date .today ().strftime ('%Y%m%d' )
566+ opencv_version += ".{}" .format (version )
567+ else :
568+ # local version identifier, not to be published on PyPI
569+ version = git_hash
570+ opencv_version += "+{}" .format (version )
571+
572+ with open ("cv2/version.py" , "w" ) as f :
573+ f .write ('opencv_version = "{}"\n ' .format (opencv_version ))
574+ f .write ("contrib = {}\n " .format (contrib ))
575+ f .write ("headless = {}\n " .format (headless ))
576+ f .write ("rolling = {}\n " .format (rolling ))
577+ f .write ("cuda_arch_bin = {}" .format (cuda_arch_bin ))
578+ f .write ("cuda_arch_ptx = {}" .format (cuda_arch_ptx ))
579+
580+ return opencv_version , contrib , headless , rolling
521581
522582
523583def get_build_env_var_by_name (flag_name ):
0 commit comments