SCsub 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. #!/usr/bin/env python
  2. Import('env')
  3. Import('env_modules')
  4. env_mono = env_modules.Clone()
  5. # TODO move functions to their own modules
  6. def make_cs_files_header(src, dst):
  7. from compat import byte_to_str
  8. with open(dst, 'w') as header:
  9. header.write('/* This is an automatically generated file; DO NOT EDIT! OK THX */\n')
  10. header.write('#ifndef _CS_FILES_DATA_H\n')
  11. header.write('#define _CS_FILES_DATA_H\n\n')
  12. header.write('#include "map.h"\n')
  13. header.write('#include "ustring.h"\n')
  14. inserted_files = ''
  15. import os
  16. for file in os.listdir(src):
  17. if file.endswith('.cs'):
  18. with open(os.path.join(src, file), 'rb') as f:
  19. buf = f.read()
  20. decomp_size = len(buf)
  21. import zlib
  22. buf = zlib.compress(buf)
  23. name = os.path.splitext(file)[0]
  24. header.write('\nstatic const int _cs_' + name + '_compressed_size = ' + str(len(buf)) + ';\n')
  25. header.write('static const int _cs_' + name + '_uncompressed_size = ' + str(decomp_size) + ';\n')
  26. header.write('static const unsigned char _cs_' + name + '_compressed[] = { ')
  27. for i, buf_idx in enumerate(range(len(buf))):
  28. if i > 0:
  29. header.write(', ')
  30. header.write(byte_to_str(buf[buf_idx]))
  31. inserted_files += '\tr_files.insert("' + file + '", ' \
  32. 'CompressedFile(_cs_' + name + '_compressed_size, ' \
  33. '_cs_' + name + '_uncompressed_size, ' \
  34. '_cs_' + name + '_compressed));\n'
  35. header.write(' };\n')
  36. version_file = os.path.join(src, 'VERSION.txt')
  37. with open(version_file, 'r') as content_file:
  38. try:
  39. glue_version = int(content_file.read()) # make sure the format is valid
  40. header.write('\n#define CS_GLUE_VERSION UINT32_C(' + str(glue_version) + ')\n')
  41. except ValueError:
  42. raise ValueError('Invalid C# glue version in: ' + version_file)
  43. header.write('\nstruct CompressedFile\n' '{\n'
  44. '\tint compressed_size;\n' '\tint uncompressed_size;\n' '\tconst unsigned char* data;\n'
  45. '\n\tCompressedFile(int p_comp_size, int p_uncomp_size, const unsigned char* p_data)\n'
  46. '\t{\n' '\t\tcompressed_size = p_comp_size;\n' '\t\tuncompressed_size = p_uncomp_size;\n'
  47. '\t\tdata = p_data;\n' '\t}\n' '\n\tCompressedFile() {}\n' '};\n'
  48. '\nvoid get_compressed_files(Map<String, CompressedFile>& r_files)\n' '{\n' + inserted_files + '}\n'
  49. )
  50. header.write('#endif // _CS_FILES_DATA_H')
  51. env_mono.add_source_files(env.modules_sources, '*.cpp')
  52. env_mono.add_source_files(env.modules_sources, 'mono_gd/*.cpp')
  53. env_mono.add_source_files(env.modules_sources, 'utils/*.cpp')
  54. if env['tools']:
  55. env_mono.add_source_files(env.modules_sources, 'editor/*.cpp')
  56. make_cs_files_header('glue/cs_files', 'glue/cs_compressed.gen.h')
  57. vars = Variables()
  58. vars.Add(BoolVariable('mono_glue', 'Build with the mono glue sources', True))
  59. vars.Add(BoolVariable('xbuild_fallback', 'If MSBuild is not found, fallback to xbuild', False))
  60. vars.Update(env_mono)
  61. # Glue sources
  62. if env_mono['mono_glue']:
  63. env_mono.add_source_files(env.modules_sources, 'glue/*.cpp')
  64. else:
  65. env_mono.Append(CPPDEFINES=['MONO_GLUE_DISABLED'])
  66. if ARGUMENTS.get('yolo_copy', False):
  67. env_mono.Append(CPPDEFINES=['YOLO_COPY'])
  68. # Configure TLS checks
  69. import tls_configure
  70. conf = Configure(env_mono)
  71. tls_configure.configure(conf)
  72. env_mono = conf.Finish()
  73. # Build GodotSharpTools solution
  74. import os
  75. def find_msbuild_unix(filename):
  76. import os.path
  77. import sys
  78. hint_dirs = ['/opt/novell/mono/bin']
  79. if sys.platform == 'darwin':
  80. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs
  81. for hint_dir in hint_dirs:
  82. hint_path = os.path.join(hint_dir, filename)
  83. if os.path.isfile(hint_path):
  84. return hint_path
  85. elif os.path.isfile(hint_path + '.exe'):
  86. return hint_path + '.exe'
  87. for hint_dir in os.environ['PATH'].split(os.pathsep):
  88. hint_dir = hint_dir.strip('"')
  89. hint_path = os.path.join(hint_dir, filename)
  90. if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
  91. return hint_path
  92. if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
  93. return hint_path + '.exe'
  94. return None
  95. def find_msbuild_windows():
  96. import mono_reg_utils as monoreg
  97. bits = env['bits']
  98. if bits == '32':
  99. if os.getenv('MONO32_PREFIX'):
  100. mono_root = os.getenv('MONO32_PREFIX')
  101. else:
  102. mono_root = monoreg.find_mono_root_dir(bits)
  103. else:
  104. if os.getenv('MONO64_PREFIX'):
  105. mono_root = os.getenv('MONO64_PREFIX')
  106. else:
  107. mono_root = monoreg.find_mono_root_dir(bits)
  108. if not mono_root:
  109. raise RuntimeError('Cannot find mono root directory')
  110. framework_path = os.path.join(mono_root, 'lib', 'mono', '4.5')
  111. mono_bin_dir = os.path.join(mono_root, 'bin')
  112. msbuild_mono = os.path.join(mono_bin_dir, 'msbuild.bat')
  113. if os.path.isfile(msbuild_mono):
  114. # The (Csc/Vbc/Fsc)ToolExe environment variables are required when
  115. # building with Mono's MSBuild. They must point to the batch files
  116. # in Mono's bin directory to make sure they are executed with Mono.
  117. mono_msbuild_env = {
  118. 'CscToolExe': os.path.join(mono_bin_dir, 'csc.bat'),
  119. 'VbcToolExe': os.path.join(mono_bin_dir, 'vbc.bat'),
  120. 'FscToolExe': os.path.join(mono_bin_dir, 'fsharpc.bat')
  121. }
  122. return (msbuild_mono, framework_path, mono_msbuild_env)
  123. msbuild_tools_path = monoreg.find_msbuild_tools_path_reg()
  124. if msbuild_tools_path:
  125. return (os.path.join(msbuild_tools_path, 'MSBuild.exe'), os.path.join(mono_root, 'lib', 'mono', '4.5'))
  126. else:
  127. msbuild_mono = os.path.join(mono_root, 'bin', 'msbuild.bat')
  128. if os.path.isfile(msbuild_mono):
  129. return (msbuild_mono, '')
  130. return None
  131. def mono_build_solution(source, target, env):
  132. import subprocess
  133. import mono_reg_utils as monoreg
  134. from shutil import copyfile
  135. framework_path_override = ''
  136. if os.name == 'nt':
  137. msbuild_info = find_msbuild_windows()
  138. if msbuild_info is None:
  139. raise RuntimeError('Cannot find MSBuild executable')
  140. msbuild_path = msbuild_info[0]
  141. framework_path_override = msbuild_info[1]
  142. else:
  143. msbuild_path = find_msbuild_unix('msbuild')
  144. if msbuild_path is None:
  145. xbuild_fallback = env['xbuild_fallback']
  146. if xbuild_fallback and os.name == 'nt':
  147. print('Option \'xbuild_fallback\' not supported on Windows')
  148. xbuild_fallback = False
  149. if xbuild_fallback:
  150. print('Cannot find MSBuild executable, trying with xbuild')
  151. print('Warning: xbuild is deprecated')
  152. msbuild_path = find_msbuild_unix('xbuild')
  153. if msbuild_path is None:
  154. raise RuntimeError('Cannot find xbuild executable')
  155. else:
  156. raise RuntimeError('Cannot find MSBuild executable')
  157. print('MSBuild path: ' + msbuild_path)
  158. build_config = 'Release'
  159. msbuild_args = [
  160. msbuild_path,
  161. os.path.abspath(str(source[0])),
  162. '/p:Configuration=' + build_config,
  163. ]
  164. if framework_path_override:
  165. msbuild_args += ['/p:FrameworkPathOverride=' + framework_path_override]
  166. msbuild_env = os.environ.copy()
  167. # Needed when running from Developer Command Prompt for VS
  168. if 'PLATFORM' in msbuild_env:
  169. del msbuild_env['PLATFORM']
  170. try:
  171. subprocess.check_call(msbuild_args, env=msbuild_env)
  172. except subprocess.CalledProcessError:
  173. raise RuntimeError('GodotSharpTools build failed')
  174. src_dir = os.path.abspath(os.path.join(str(source[0]), os.pardir, 'bin', build_config))
  175. dst_dir = os.path.abspath(os.path.join(str(target[0]), os.pardir))
  176. if not os.path.isdir(dst_dir):
  177. if os.path.exists(dst_dir):
  178. raise RuntimeError('Target directory is a file')
  179. os.makedirs(dst_dir)
  180. asm_file = 'GodotSharpTools.dll'
  181. copyfile(os.path.join(src_dir, asm_file), os.path.join(dst_dir, asm_file))
  182. output_dir = Dir('#bin').abspath
  183. assemblies_output_dir = Dir(env['mono_assemblies_output_dir']).abspath
  184. mono_sln_builder = Builder(action=mono_build_solution)
  185. env_mono.Append(BUILDERS={'MonoBuildSolution': mono_sln_builder})
  186. env_mono.MonoBuildSolution(
  187. os.path.join(assemblies_output_dir, 'GodotSharpTools.dll'),
  188. 'editor/GodotSharpTools/GodotSharpTools.sln'
  189. )
  190. if os.path.normpath(output_dir) != os.path.normpath(assemblies_output_dir):
  191. rel_assemblies_output_dir = os.path.relpath(assemblies_output_dir, output_dir)
  192. env_mono.Append(CPPDEFINES={'GD_MONO_EDITOR_ASSEMBLIES_DIR': rel_assemblies_output_dir})