solution_builder.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import os
  2. verbose = False
  3. def find_nuget_unix():
  4. import os
  5. if 'NUGET_PATH' in os.environ:
  6. hint_path = os.environ['NUGET_PATH']
  7. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  8. return hint_path
  9. hint_path = os.path.join(hint_path, 'nuget')
  10. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  11. return hint_path
  12. import os.path
  13. import sys
  14. hint_dirs = ['/opt/novell/mono/bin']
  15. if sys.platform == 'darwin':
  16. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs
  17. for hint_dir in hint_dirs:
  18. hint_path = os.path.join(hint_dir, 'nuget')
  19. if os.path.isfile(hint_path):
  20. return hint_path
  21. elif os.path.isfile(hint_path + '.exe'):
  22. return hint_path + '.exe'
  23. for hint_dir in os.environ['PATH'].split(os.pathsep):
  24. hint_dir = hint_dir.strip('"')
  25. hint_path = os.path.join(hint_dir, 'nuget')
  26. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  27. return hint_path
  28. if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
  29. return hint_path + '.exe'
  30. return None
  31. def find_nuget_windows(env):
  32. import os
  33. if 'NUGET_PATH' in os.environ:
  34. hint_path = os.environ['NUGET_PATH']
  35. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  36. return hint_path
  37. hint_path = os.path.join(hint_path, 'nuget.exe')
  38. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  39. return hint_path
  40. from . mono_reg_utils import find_mono_root_dir
  41. mono_root = env['mono_prefix'] or find_mono_root_dir(env['bits'])
  42. if mono_root:
  43. mono_bin_dir = os.path.join(mono_root, 'bin')
  44. nuget_mono = os.path.join(mono_bin_dir, 'nuget.bat')
  45. if os.path.isfile(nuget_mono):
  46. return nuget_mono
  47. # Standalone NuGet
  48. for hint_dir in os.environ['PATH'].split(os.pathsep):
  49. hint_dir = hint_dir.strip('"')
  50. hint_path = os.path.join(hint_dir, 'nuget.exe')
  51. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  52. return hint_path
  53. return None
  54. def find_msbuild_unix(filename):
  55. import os.path
  56. import sys
  57. hint_dirs = ['/opt/novell/mono/bin']
  58. if sys.platform == 'darwin':
  59. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs
  60. for hint_dir in hint_dirs:
  61. hint_path = os.path.join(hint_dir, filename)
  62. if os.path.isfile(hint_path):
  63. return hint_path
  64. elif os.path.isfile(hint_path + '.exe'):
  65. return hint_path + '.exe'
  66. for hint_dir in os.environ['PATH'].split(os.pathsep):
  67. hint_dir = hint_dir.strip('"')
  68. hint_path = os.path.join(hint_dir, filename)
  69. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  70. return hint_path
  71. if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
  72. return hint_path + '.exe'
  73. return None
  74. def find_msbuild_windows(env):
  75. from . mono_reg_utils import find_mono_root_dir, find_msbuild_tools_path_reg
  76. mono_root = env['mono_prefix'] or find_mono_root_dir(env['bits'])
  77. if not mono_root:
  78. raise RuntimeError('Cannot find mono root directory')
  79. framework_path = os.path.join(mono_root, 'lib', 'mono', '4.5')
  80. mono_bin_dir = os.path.join(mono_root, 'bin')
  81. msbuild_mono = os.path.join(mono_bin_dir, 'msbuild.bat')
  82. if os.path.isfile(msbuild_mono):
  83. # The (Csc/Vbc/Fsc)ToolExe environment variables are required when
  84. # building with Mono's MSBuild. They must point to the batch files
  85. # in Mono's bin directory to make sure they are executed with Mono.
  86. mono_msbuild_env = {
  87. 'CscToolExe': os.path.join(mono_bin_dir, 'csc.bat'),
  88. 'VbcToolExe': os.path.join(mono_bin_dir, 'vbc.bat'),
  89. 'FscToolExe': os.path.join(mono_bin_dir, 'fsharpc.bat')
  90. }
  91. return (msbuild_mono, framework_path, mono_msbuild_env)
  92. msbuild_tools_path = find_msbuild_tools_path_reg()
  93. if msbuild_tools_path:
  94. return (os.path.join(msbuild_tools_path, 'MSBuild.exe'), framework_path, {})
  95. return None
  96. def run_command(command, args, env_override=None, name=None):
  97. def cmd_args_to_str(cmd_args):
  98. return ' '.join([arg if not ' ' in arg else '"%s"' % arg for arg in cmd_args])
  99. args = [command] + args
  100. if name is None:
  101. name = os.path.basename(command)
  102. if verbose:
  103. print("Running '%s': %s" % (name, cmd_args_to_str(args)))
  104. import subprocess
  105. try:
  106. if env_override is None:
  107. subprocess.check_call(args)
  108. else:
  109. subprocess.check_call(args, env=env_override)
  110. except subprocess.CalledProcessError as e:
  111. raise RuntimeError("'%s' exited with error code: %s" % (name, e.returncode))
  112. def nuget_restore(env, *args):
  113. global verbose
  114. verbose = env['verbose']
  115. # Find NuGet
  116. nuget_path = find_nuget_windows(env) if os.name == 'nt' else find_nuget_unix()
  117. if nuget_path is None:
  118. raise RuntimeError('Cannot find NuGet executable')
  119. print('NuGet path: ' + nuget_path)
  120. # Do NuGet restore
  121. run_command(nuget_path, ['restore'] + list(args), name='nuget restore')
  122. def build_solution(env, solution_path, build_config, extra_msbuild_args=[]):
  123. global verbose
  124. verbose = env['verbose']
  125. framework_path = ''
  126. msbuild_env = os.environ.copy()
  127. # Needed when running from Developer Command Prompt for VS
  128. if 'PLATFORM' in msbuild_env:
  129. del msbuild_env['PLATFORM']
  130. # Find MSBuild
  131. if os.name == 'nt':
  132. msbuild_info = find_msbuild_windows(env)
  133. if msbuild_info is None:
  134. raise RuntimeError('Cannot find MSBuild executable')
  135. msbuild_path = msbuild_info[0]
  136. framework_path = msbuild_info[1]
  137. msbuild_env.update(msbuild_info[2])
  138. else:
  139. msbuild_path = find_msbuild_unix('msbuild')
  140. if msbuild_path is None:
  141. xbuild_fallback = env['xbuild_fallback']
  142. if xbuild_fallback and os.name == 'nt':
  143. print('Option \'xbuild_fallback\' not supported on Windows')
  144. xbuild_fallback = False
  145. if xbuild_fallback:
  146. print('Cannot find MSBuild executable, trying with xbuild')
  147. print('Warning: xbuild is deprecated')
  148. msbuild_path = find_msbuild_unix('xbuild')
  149. if msbuild_path is None:
  150. raise RuntimeError('Cannot find xbuild executable')
  151. else:
  152. raise RuntimeError('Cannot find MSBuild executable')
  153. print('MSBuild path: ' + msbuild_path)
  154. # Build solution
  155. msbuild_args = [solution_path, '/p:Configuration=' + build_config]
  156. msbuild_args += ['/p:FrameworkPathOverride=' + framework_path] if framework_path else []
  157. msbuild_args += extra_msbuild_args
  158. run_command(msbuild_path, msbuild_args, env_override=msbuild_env, name='msbuild')