godotsharptools_build.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. # Build GodotSharpTools solution
  2. import os
  3. from SCons.Script import Builder, Dir
  4. def find_nuget_unix():
  5. import os
  6. if 'NUGET_PATH' in os.environ:
  7. hint_path = os.environ['NUGET_PATH']
  8. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  9. return hint_path
  10. hint_path = os.path.join(hint_path, 'nuget')
  11. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  12. return hint_path
  13. import os.path
  14. import sys
  15. hint_dirs = ['/opt/novell/mono/bin']
  16. if sys.platform == 'darwin':
  17. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs
  18. for hint_dir in hint_dirs:
  19. hint_path = os.path.join(hint_dir, 'nuget')
  20. if os.path.isfile(hint_path):
  21. return hint_path
  22. elif os.path.isfile(hint_path + '.exe'):
  23. return hint_path + '.exe'
  24. for hint_dir in os.environ['PATH'].split(os.pathsep):
  25. hint_dir = hint_dir.strip('"')
  26. hint_path = os.path.join(hint_dir, 'nuget')
  27. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  28. return hint_path
  29. if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
  30. return hint_path + '.exe'
  31. return None
  32. def find_nuget_windows(env):
  33. import os
  34. if 'NUGET_PATH' in os.environ:
  35. hint_path = os.environ['NUGET_PATH']
  36. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  37. return hint_path
  38. hint_path = os.path.join(hint_path, 'nuget.exe')
  39. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  40. return hint_path
  41. from . import mono_reg_utils as monoreg
  42. mono_root = ''
  43. bits = env['bits']
  44. if bits == '32':
  45. if os.getenv('MONO32_PREFIX'):
  46. mono_root = os.getenv('MONO32_PREFIX')
  47. else:
  48. mono_root = monoreg.find_mono_root_dir(bits)
  49. else:
  50. if os.getenv('MONO64_PREFIX'):
  51. mono_root = os.getenv('MONO64_PREFIX')
  52. else:
  53. mono_root = monoreg.find_mono_root_dir(bits)
  54. if mono_root:
  55. mono_bin_dir = os.path.join(mono_root, 'bin')
  56. nuget_mono = os.path.join(mono_bin_dir, 'nuget.bat')
  57. if os.path.isfile(nuget_mono):
  58. return nuget_mono
  59. # Standalone NuGet
  60. for hint_dir in os.environ['PATH'].split(os.pathsep):
  61. hint_dir = hint_dir.strip('"')
  62. hint_path = os.path.join(hint_dir, 'nuget.exe')
  63. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  64. return hint_path
  65. return None
  66. def find_msbuild_unix(filename):
  67. import os.path
  68. import sys
  69. hint_dirs = ['/opt/novell/mono/bin']
  70. if sys.platform == 'darwin':
  71. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs
  72. for hint_dir in hint_dirs:
  73. hint_path = os.path.join(hint_dir, filename)
  74. if os.path.isfile(hint_path):
  75. return hint_path
  76. elif os.path.isfile(hint_path + '.exe'):
  77. return hint_path + '.exe'
  78. for hint_dir in os.environ['PATH'].split(os.pathsep):
  79. hint_dir = hint_dir.strip('"')
  80. hint_path = os.path.join(hint_dir, filename)
  81. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  82. return hint_path
  83. if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
  84. return hint_path + '.exe'
  85. return None
  86. def find_msbuild_windows(env):
  87. from . import mono_reg_utils as monoreg
  88. mono_root = ''
  89. bits = env['bits']
  90. if bits == '32':
  91. if os.getenv('MONO32_PREFIX'):
  92. mono_root = os.getenv('MONO32_PREFIX')
  93. else:
  94. mono_root = monoreg.find_mono_root_dir(bits)
  95. else:
  96. if os.getenv('MONO64_PREFIX'):
  97. mono_root = os.getenv('MONO64_PREFIX')
  98. else:
  99. mono_root = monoreg.find_mono_root_dir(bits)
  100. if not mono_root:
  101. raise RuntimeError('Cannot find mono root directory')
  102. framework_path = os.path.join(mono_root, 'lib', 'mono', '4.5')
  103. mono_bin_dir = os.path.join(mono_root, 'bin')
  104. msbuild_mono = os.path.join(mono_bin_dir, 'msbuild.bat')
  105. if os.path.isfile(msbuild_mono):
  106. # The (Csc/Vbc/Fsc)ToolExe environment variables are required when
  107. # building with Mono's MSBuild. They must point to the batch files
  108. # in Mono's bin directory to make sure they are executed with Mono.
  109. mono_msbuild_env = {
  110. 'CscToolExe': os.path.join(mono_bin_dir, 'csc.bat'),
  111. 'VbcToolExe': os.path.join(mono_bin_dir, 'vbc.bat'),
  112. 'FscToolExe': os.path.join(mono_bin_dir, 'fsharpc.bat')
  113. }
  114. return (msbuild_mono, framework_path, mono_msbuild_env)
  115. msbuild_tools_path = monoreg.find_msbuild_tools_path_reg()
  116. if msbuild_tools_path:
  117. return (os.path.join(msbuild_tools_path, 'MSBuild.exe'), framework_path, {})
  118. return None
  119. def mono_build_solution(source, target, env):
  120. import subprocess
  121. from shutil import copyfile
  122. sln_path = os.path.abspath(str(source[0]))
  123. target_path = os.path.abspath(str(target[0]))
  124. framework_path = ''
  125. msbuild_env = os.environ.copy()
  126. # Needed when running from Developer Command Prompt for VS
  127. if 'PLATFORM' in msbuild_env:
  128. del msbuild_env['PLATFORM']
  129. # Find MSBuild
  130. if os.name == 'nt':
  131. msbuild_info = find_msbuild_windows(env)
  132. if msbuild_info is None:
  133. raise RuntimeError('Cannot find MSBuild executable')
  134. msbuild_path = msbuild_info[0]
  135. framework_path = msbuild_info[1]
  136. msbuild_env.update(msbuild_info[2])
  137. else:
  138. msbuild_path = find_msbuild_unix('msbuild')
  139. if msbuild_path is None:
  140. xbuild_fallback = env['xbuild_fallback']
  141. if xbuild_fallback and os.name == 'nt':
  142. print('Option \'xbuild_fallback\' not supported on Windows')
  143. xbuild_fallback = False
  144. if xbuild_fallback:
  145. print('Cannot find MSBuild executable, trying with xbuild')
  146. print('Warning: xbuild is deprecated')
  147. msbuild_path = find_msbuild_unix('xbuild')
  148. if msbuild_path is None:
  149. raise RuntimeError('Cannot find xbuild executable')
  150. else:
  151. raise RuntimeError('Cannot find MSBuild executable')
  152. print('MSBuild path: ' + msbuild_path)
  153. # Find NuGet
  154. nuget_path = find_nuget_windows(env) if os.name == 'nt' else find_nuget_unix()
  155. if nuget_path is None:
  156. raise RuntimeError('Cannot find NuGet executable')
  157. print('NuGet path: ' + nuget_path)
  158. # Do NuGet restore
  159. try:
  160. subprocess.check_call([nuget_path, 'restore', sln_path])
  161. except subprocess.CalledProcessError:
  162. raise RuntimeError('GodotSharpTools: NuGet restore failed')
  163. # Build solution
  164. build_config = 'Release'
  165. msbuild_args = [
  166. msbuild_path,
  167. sln_path,
  168. '/p:Configuration=' + build_config,
  169. ]
  170. if framework_path:
  171. msbuild_args += ['/p:FrameworkPathOverride=' + framework_path]
  172. try:
  173. subprocess.check_call(msbuild_args, env=msbuild_env)
  174. except subprocess.CalledProcessError:
  175. raise RuntimeError('GodotSharpTools: Build failed')
  176. # Copy files
  177. src_dir = os.path.abspath(os.path.join(sln_path, os.pardir, 'bin', build_config))
  178. dst_dir = os.path.abspath(os.path.join(target_path, os.pardir))
  179. asm_file = 'GodotSharpTools.dll'
  180. if not os.path.isdir(dst_dir):
  181. if os.path.exists(dst_dir):
  182. raise RuntimeError('Target directory is a file')
  183. os.makedirs(dst_dir)
  184. copyfile(os.path.join(src_dir, asm_file), os.path.join(dst_dir, asm_file))
  185. # Dependencies
  186. copyfile(os.path.join(src_dir, "DotNet.Glob.dll"), os.path.join(dst_dir, "DotNet.Glob.dll"))
  187. def build(env_mono):
  188. if not env_mono['tools']:
  189. return
  190. output_dir = Dir('#bin').abspath
  191. editor_tools_dir = os.path.join(output_dir, 'GodotSharp', 'Tools')
  192. mono_sln_builder = Builder(action=mono_build_solution)
  193. env_mono.Append(BUILDERS={'MonoBuildSolution': mono_sln_builder})
  194. env_mono.MonoBuildSolution(
  195. os.path.join(editor_tools_dir, 'GodotSharpTools.dll'),
  196. 'editor/GodotSharpTools/GodotSharpTools.sln'
  197. )