mono_configure.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. import os
  2. import os.path
  3. import sys
  4. import subprocess
  5. from SCons.Script import Dir, Environment
  6. if os.name == 'nt':
  7. from . import mono_reg_utils as monoreg
  8. android_arch_dirs = {
  9. 'armv7': 'armeabi-v7a',
  10. 'arm64v8': 'arm64-v8a',
  11. 'x86': 'x86',
  12. 'x86_64': 'x86_64'
  13. }
  14. def get_android_out_dir(env):
  15. return os.path.join(Dir('#platform/android/java/libs').abspath,
  16. 'release' if env['target'] == 'release' else 'debug',
  17. android_arch_dirs[env['android_arch']])
  18. def find_file_in_dir(directory, files, prefix='', extension=''):
  19. if not extension.startswith('.'):
  20. extension = '.' + extension
  21. for curfile in files:
  22. if os.path.isfile(os.path.join(directory, prefix + curfile + extension)):
  23. return curfile
  24. return ''
  25. def copy_file(src_dir, dst_dir, name):
  26. from shutil import copy
  27. src_path = os.path.join(Dir(src_dir).abspath, name)
  28. dst_dir = Dir(dst_dir).abspath
  29. if not os.path.isdir(dst_dir):
  30. os.makedirs(dst_dir)
  31. copy(src_path, dst_dir)
  32. def configure(env, env_mono):
  33. bits = env['bits']
  34. is_android = env['platform'] == 'android'
  35. tools_enabled = env['tools']
  36. mono_static = env['mono_static']
  37. copy_mono_root = env['copy_mono_root']
  38. mono_prefix = env['mono_prefix']
  39. mono_lib_names = ['mono-2.0-sgen', 'monosgen-2.0']
  40. is_travis = os.environ.get('TRAVIS') == 'true'
  41. if is_travis:
  42. # Travis CI may have a Mono version lower than 5.12
  43. env_mono.Append(CPPDEFINES=['NO_PENDING_EXCEPTIONS'])
  44. if is_android and not env['android_arch'] in android_arch_dirs:
  45. raise RuntimeError('This module does not support for the specified \'android_arch\': ' + env['android_arch'])
  46. if is_android and tools_enabled:
  47. # TODO: Implement this. We have to add the data directory to the apk, concretely the Api and Tools folders.
  48. raise RuntimeError('This module does not currently support building for android with tools enabled')
  49. if is_android and mono_static:
  50. # When static linking and doing something that requires libmono-native, we get a dlopen error as libmono-native seems to depend on libmonosgen-2.0
  51. raise RuntimeError('Linking Mono statically is not currently supported on Android')
  52. if (os.getenv('MONO32_PREFIX') or os.getenv('MONO64_PREFIX')) and not mono_prefix:
  53. print("WARNING: The environment variables 'MONO32_PREFIX' and 'MONO64_PREFIX' are deprecated; use the 'mono_prefix' SCons parameter instead")
  54. if env['platform'] == 'windows':
  55. mono_root = mono_prefix
  56. if not mono_root and os.name == 'nt':
  57. mono_root = monoreg.find_mono_root_dir(bits)
  58. if not mono_root:
  59. raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")
  60. print('Found Mono root directory: ' + mono_root)
  61. mono_lib_path = os.path.join(mono_root, 'lib')
  62. env.Append(LIBPATH=mono_lib_path)
  63. env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  64. if mono_static:
  65. lib_suffix = Environment()['LIBSUFFIX']
  66. if env.msvc:
  67. mono_static_lib_name = 'libmono-static-sgen'
  68. else:
  69. mono_static_lib_name = 'libmonosgen-2.0'
  70. if not os.path.isfile(os.path.join(mono_lib_path, mono_static_lib_name + lib_suffix)):
  71. raise RuntimeError('Could not find static mono library in: ' + mono_lib_path)
  72. if env.msvc:
  73. env.Append(LINKFLAGS=mono_static_lib_name + lib_suffix)
  74. env.Append(LINKFLAGS='Mincore' + lib_suffix)
  75. env.Append(LINKFLAGS='msvcrt' + lib_suffix)
  76. env.Append(LINKFLAGS='LIBCMT' + lib_suffix)
  77. env.Append(LINKFLAGS='Psapi' + lib_suffix)
  78. else:
  79. env.Append(LINKFLAGS=os.path.join(mono_lib_path, mono_static_lib_name + lib_suffix))
  80. env.Append(LIBS='psapi')
  81. env.Append(LIBS='version')
  82. else:
  83. mono_lib_name = find_file_in_dir(mono_lib_path, mono_lib_names, extension='.lib')
  84. if not mono_lib_name:
  85. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  86. if env.msvc:
  87. env.Append(LINKFLAGS=mono_lib_name + Environment()['LIBSUFFIX'])
  88. else:
  89. env.Append(LIBS=mono_lib_name)
  90. mono_bin_path = os.path.join(mono_root, 'bin')
  91. mono_dll_name = find_file_in_dir(mono_bin_path, mono_lib_names, extension='.dll')
  92. if not mono_dll_name:
  93. raise RuntimeError('Could not find mono shared library in: ' + mono_bin_path)
  94. copy_file(mono_bin_path, '#bin', mono_dll_name + '.dll')
  95. else:
  96. is_apple = (sys.platform == 'darwin' or "osxcross" in env)
  97. sharedlib_ext = '.dylib' if is_apple else '.so'
  98. mono_root = mono_prefix
  99. mono_lib_path = ''
  100. mono_so_name = ''
  101. if not mono_root and is_android:
  102. raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")
  103. if not mono_root and is_apple:
  104. # Try with some known directories under OSX
  105. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current', '/usr/local/var/homebrew/linked/mono']
  106. for hint_dir in hint_dirs:
  107. if os.path.isdir(hint_dir):
  108. mono_root = hint_dir
  109. break
  110. # We can't use pkg-config to link mono statically,
  111. # but we can still use it to find the mono root directory
  112. if not mono_root and mono_static:
  113. mono_root = pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext)
  114. if not mono_root:
  115. raise RuntimeError("Building with mono_static=yes, but failed to find the mono prefix with pkg-config; " + \
  116. "specify one manually with the 'mono_prefix' SCons parameter")
  117. if mono_root:
  118. print('Found Mono root directory: ' + mono_root)
  119. mono_lib_path = os.path.join(mono_root, 'lib')
  120. env.Append(LIBPATH=mono_lib_path)
  121. env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  122. mono_lib = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension='.a')
  123. if not mono_lib:
  124. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  125. env_mono.Append(CPPDEFINES=['_REENTRANT'])
  126. if mono_static:
  127. mono_lib_file = os.path.join(mono_lib_path, 'lib' + mono_lib + '.a')
  128. if is_apple:
  129. env.Append(LINKFLAGS=['-Wl,-force_load,' + mono_lib_file])
  130. else:
  131. env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_lib_file, '-Wl,-no-whole-archive'])
  132. else:
  133. env.Append(LIBS=[mono_lib])
  134. if is_apple:
  135. env.Append(LIBS=['iconv', 'pthread'])
  136. elif is_android:
  137. pass # Nothing
  138. else:
  139. env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])
  140. if not mono_static:
  141. mono_so_name = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension=sharedlib_ext)
  142. if not mono_so_name:
  143. raise RuntimeError('Could not find mono shared library in: ' + mono_lib_path)
  144. copy_file(mono_lib_path, '#bin', 'lib' + mono_so_name + sharedlib_ext)
  145. else:
  146. assert not mono_static
  147. # TODO: Add option to force using pkg-config
  148. print('Mono root directory not found. Using pkg-config instead')
  149. env.ParseConfig('pkg-config monosgen-2 --libs')
  150. env_mono.ParseConfig('pkg-config monosgen-2 --cflags')
  151. tmpenv = Environment()
  152. tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
  153. tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
  154. for hint_dir in tmpenv['LIBPATH']:
  155. name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext)
  156. if name_found:
  157. mono_lib_path = hint_dir
  158. mono_so_name = name_found
  159. break
  160. if not mono_so_name:
  161. raise RuntimeError('Could not find mono shared library in: ' + str(tmpenv['LIBPATH']))
  162. if not mono_static:
  163. libs_output_dir = get_android_out_dir(env) if is_android else '#bin'
  164. copy_file(mono_lib_path, libs_output_dir, 'lib' + mono_so_name + sharedlib_ext)
  165. env.Append(LINKFLAGS='-rdynamic')
  166. if not tools_enabled and not is_android:
  167. if not mono_root:
  168. mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()
  169. make_template_dir(env, mono_root)
  170. elif not tools_enabled and is_android:
  171. # Compress Android Mono Config
  172. from . import make_android_mono_config
  173. config_file_path = os.path.join(mono_root, 'etc', 'mono', 'config')
  174. make_android_mono_config.generate_compressed_config(config_file_path, 'mono_gd/')
  175. # Copy the required shared libraries
  176. copy_mono_shared_libs(env, mono_root, None)
  177. if copy_mono_root:
  178. if not mono_root:
  179. mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()
  180. if tools_enabled:
  181. copy_mono_root_files(env, mono_root)
  182. else:
  183. print("Ignoring option: 'copy_mono_root'. Only available for builds with 'tools' enabled.")
  184. def make_template_dir(env, mono_root):
  185. from shutil import rmtree
  186. platform = env['platform']
  187. target = env['target']
  188. template_dir_name = ''
  189. if platform in ['windows', 'osx', 'x11', 'android']:
  190. template_dir_name = 'data.mono.%s.%s.%s' % (platform, env['bits'], target)
  191. else:
  192. assert False
  193. output_dir = Dir('#bin').abspath
  194. template_dir = os.path.join(output_dir, template_dir_name)
  195. template_mono_root_dir = os.path.join(template_dir, 'Mono')
  196. if os.path.isdir(template_mono_root_dir):
  197. rmtree(template_mono_root_dir) # Clean first
  198. # Copy etc/mono/
  199. template_mono_config_dir = os.path.join(template_mono_root_dir, 'etc', 'mono')
  200. copy_mono_etc_dir(mono_root, template_mono_config_dir, env['platform'])
  201. # Copy the required shared libraries
  202. copy_mono_shared_libs(env, mono_root, template_mono_root_dir)
  203. def copy_mono_root_files(env, mono_root):
  204. from glob import glob
  205. from shutil import copy
  206. from shutil import rmtree
  207. if not mono_root:
  208. raise RuntimeError('Mono installation directory not found')
  209. output_dir = Dir('#bin').abspath
  210. editor_mono_root_dir = os.path.join(output_dir, 'GodotSharp', 'Mono')
  211. if os.path.isdir(editor_mono_root_dir):
  212. rmtree(editor_mono_root_dir) # Clean first
  213. # Copy etc/mono/
  214. editor_mono_config_dir = os.path.join(editor_mono_root_dir, 'etc', 'mono')
  215. copy_mono_etc_dir(mono_root, editor_mono_config_dir, env['platform'])
  216. # Copy the required shared libraries
  217. copy_mono_shared_libs(env, mono_root, editor_mono_root_dir)
  218. # Copy framework assemblies
  219. mono_framework_dir = os.path.join(mono_root, 'lib', 'mono', '4.5')
  220. mono_framework_facades_dir = os.path.join(mono_framework_dir, 'Facades')
  221. editor_mono_framework_dir = os.path.join(editor_mono_root_dir, 'lib', 'mono', '4.5')
  222. editor_mono_framework_facades_dir = os.path.join(editor_mono_framework_dir, 'Facades')
  223. if not os.path.isdir(editor_mono_framework_dir):
  224. os.makedirs(editor_mono_framework_dir)
  225. if not os.path.isdir(editor_mono_framework_facades_dir):
  226. os.makedirs(editor_mono_framework_facades_dir)
  227. for assembly in glob(os.path.join(mono_framework_dir, '*.dll')):
  228. copy(assembly, editor_mono_framework_dir)
  229. for assembly in glob(os.path.join(mono_framework_facades_dir, '*.dll')):
  230. copy(assembly, editor_mono_framework_facades_dir)
  231. def copy_mono_etc_dir(mono_root, target_mono_config_dir, platform):
  232. from distutils.dir_util import copy_tree
  233. from glob import glob
  234. from shutil import copy
  235. if not os.path.isdir(target_mono_config_dir):
  236. os.makedirs(target_mono_config_dir)
  237. mono_etc_dir = os.path.join(mono_root, 'etc', 'mono')
  238. if not os.path.isdir(mono_etc_dir):
  239. mono_etc_dir = ''
  240. etc_hint_dirs = []
  241. if platform != 'windows':
  242. etc_hint_dirs += ['/etc/mono', '/usr/local/etc/mono']
  243. if 'MONO_CFG_DIR' in os.environ:
  244. etc_hint_dirs += [os.path.join(os.environ['MONO_CFG_DIR'], 'mono')]
  245. for etc_hint_dir in etc_hint_dirs:
  246. if os.path.isdir(etc_hint_dir):
  247. mono_etc_dir = etc_hint_dir
  248. break
  249. if not mono_etc_dir:
  250. raise RuntimeError('Mono installation etc directory not found')
  251. copy_tree(os.path.join(mono_etc_dir, '2.0'), os.path.join(target_mono_config_dir, '2.0'))
  252. copy_tree(os.path.join(mono_etc_dir, '4.0'), os.path.join(target_mono_config_dir, '4.0'))
  253. copy_tree(os.path.join(mono_etc_dir, '4.5'), os.path.join(target_mono_config_dir, '4.5'))
  254. if os.path.isdir(os.path.join(mono_etc_dir, 'mconfig')):
  255. copy_tree(os.path.join(mono_etc_dir, 'mconfig'), os.path.join(target_mono_config_dir, 'mconfig'))
  256. for file in glob(os.path.join(mono_etc_dir, '*')):
  257. if os.path.isfile(file):
  258. copy(file, target_mono_config_dir)
  259. def copy_mono_shared_libs(env, mono_root, target_mono_root_dir):
  260. from shutil import copy
  261. def copy_if_exists(src, dst):
  262. if os.path.isfile(src):
  263. copy(src, dst)
  264. platform = env['platform']
  265. if platform == 'windows':
  266. target_mono_bin_dir = os.path.join(target_mono_root_dir, 'bin')
  267. if not os.path.isdir(target_mono_bin_dir):
  268. os.makedirs(target_mono_bin_dir)
  269. copy(os.path.join(mono_root, 'bin', 'MonoPosixHelper.dll'), target_mono_bin_dir)
  270. else:
  271. target_mono_lib_dir = get_android_out_dir(env) if platform == 'android' else os.path.join(target_mono_root_dir, 'lib')
  272. if not os.path.isdir(target_mono_lib_dir):
  273. os.makedirs(target_mono_lib_dir)
  274. if platform == 'osx':
  275. # TODO: Make sure nothing is missing
  276. copy(os.path.join(mono_root, 'lib', 'libMonoPosixHelper.dylib'), target_mono_lib_dir)
  277. elif platform == 'x11' or platform == 'android':
  278. lib_file_names = [lib_name + '.so' for lib_name in [
  279. 'libmono-btls-shared', 'libmono-ee-interp', 'libmono-native', 'libMonoPosixHelper',
  280. 'libmono-profiler-aot', 'libmono-profiler-coverage', 'libmono-profiler-log', 'libMonoSupportW'
  281. ]]
  282. for lib_file_name in lib_file_names:
  283. copy_if_exists(os.path.join(mono_root, 'lib', lib_file_name), target_mono_lib_dir)
  284. def pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext):
  285. tmpenv = Environment()
  286. tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
  287. tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
  288. for hint_dir in tmpenv['LIBPATH']:
  289. name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext)
  290. if name_found and os.path.isdir(os.path.join(hint_dir, '..', 'include', 'mono-2.0')):
  291. return os.path.join(hint_dir, '..')
  292. return ''