godotsharptools_build.py 7.7 KB

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