cv_build_utils.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. """
  3. Common utilities. These should be compatible with Python 2 and 3.
  4. """
  5. from __future__ import print_function
  6. import sys, re
  7. from subprocess import check_call, check_output, CalledProcessError
  8. def execute(cmd, cwd = None):
  9. print("Executing: %s in %s" % (cmd, cwd), file=sys.stderr)
  10. print('Executing: ' + ' '.join(cmd))
  11. retcode = check_call(cmd, cwd = cwd)
  12. if retcode != 0:
  13. raise Exception("Child returned:", retcode)
  14. def print_header(text):
  15. print("="*60)
  16. print(text)
  17. print("="*60)
  18. def print_error(text):
  19. print("="*60, file=sys.stderr)
  20. print("ERROR: %s" % text, file=sys.stderr)
  21. print("="*60, file=sys.stderr)
  22. def get_xcode_major():
  23. ret = check_output(["xcodebuild", "-version"]).decode('utf-8')
  24. m = re.match(r'Xcode\s+(\d+)\..*', ret, flags=re.IGNORECASE)
  25. if m:
  26. return int(m.group(1))
  27. else:
  28. raise Exception("Failed to parse Xcode version")
  29. def get_xcode_version():
  30. """
  31. Returns the major and minor version of the current Xcode
  32. command line tools as a tuple of (major, minor)
  33. """
  34. ret = check_output(["xcodebuild", "-version"]).decode('utf-8')
  35. m = re.match(r'Xcode\s+(\d+)\.(\d+)', ret, flags=re.IGNORECASE)
  36. if m:
  37. return (int(m.group(1)), int(m.group(2)))
  38. else:
  39. raise Exception("Failed to parse Xcode version")
  40. def get_xcode_setting(var, projectdir):
  41. ret = check_output(["xcodebuild", "-showBuildSettings"], cwd = projectdir).decode('utf-8')
  42. m = re.search("\s" + var + " = (.*)", ret)
  43. if m:
  44. return m.group(1)
  45. else:
  46. raise Exception("Failed to parse Xcode settings")
  47. def get_cmake_version():
  48. """
  49. Returns the major and minor version of the current CMake
  50. command line tools as a tuple of (major, minor, revision)
  51. """
  52. ret = check_output(["cmake", "--version"]).decode('utf-8')
  53. m = re.match(r'cmake\sversion\s+(\d+)\.(\d+).(\d+)', ret, flags=re.IGNORECASE)
  54. if m:
  55. return (int(m.group(1)), int(m.group(2)), int(m.group(3)))
  56. else:
  57. raise Exception("Failed to parse CMake version")