mono_configure.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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/lib/libs').abspath,
  16. 'release' if env['target'] == 'release' else 'debug',
  17. android_arch_dirs[env['android_arch']])
  18. def find_name_in_dir_files(directory, names, prefixes=[''], extensions=['']):
  19. for extension in extensions:
  20. if extension and not extension.startswith('.'):
  21. extension = '.' + extension
  22. for prefix in prefixes:
  23. for curname in names:
  24. if os.path.isfile(os.path.join(directory, prefix + curname + extension)):
  25. return curname
  26. return ''
  27. def find_file_in_dir(directory, names, prefixes=[''], extensions=['']):
  28. for extension in extensions:
  29. if extension and not extension.startswith('.'):
  30. extension = '.' + extension
  31. for prefix in prefixes:
  32. for curname in names:
  33. filename = prefix + curname + extension
  34. if os.path.isfile(os.path.join(directory, filename)):
  35. return filename
  36. return ''
  37. def copy_file(src_dir, dst_dir, name):
  38. from shutil import copy
  39. src_path = os.path.join(Dir(src_dir).abspath, name)
  40. dst_dir = Dir(dst_dir).abspath
  41. if not os.path.isdir(dst_dir):
  42. os.makedirs(dst_dir)
  43. copy(src_path, dst_dir)
  44. def is_desktop(platform):
  45. return platform in ['windows', 'osx', 'x11', 'server', 'uwp', 'haiku']
  46. def is_unix_like(platform):
  47. return platform in ['osx', 'x11', 'server', 'android', 'haiku']
  48. def module_supports_tools_on(platform):
  49. return platform not in ['android', 'javascript']
  50. def find_wasm_src_dir(mono_root):
  51. hint_dirs = [
  52. os.path.join(mono_root, 'src'),
  53. os.path.join(mono_root, '../src'),
  54. ]
  55. for hint_dir in hint_dirs:
  56. if os.path.isfile(os.path.join(hint_dir, 'driver.c')):
  57. return hint_dir
  58. return ''
  59. def configure(env, env_mono):
  60. bits = env['bits']
  61. is_android = env['platform'] == 'android'
  62. is_javascript = env['platform'] == 'javascript'
  63. tools_enabled = env['tools']
  64. mono_static = env['mono_static']
  65. copy_mono_root = env['copy_mono_root']
  66. mono_prefix = env['mono_prefix']
  67. mono_lib_names = ['mono-2.0-sgen', 'monosgen-2.0']
  68. is_travis = os.environ.get('TRAVIS') == 'true'
  69. if is_travis:
  70. # Travis CI may have a Mono version lower than 5.12
  71. env_mono.Append(CPPDEFINES=['NO_PENDING_EXCEPTIONS'])
  72. if is_android and not env['android_arch'] in android_arch_dirs:
  73. raise RuntimeError('This module does not support the specified \'android_arch\': ' + env['android_arch'])
  74. if tools_enabled and not module_supports_tools_on(env['platform']):
  75. # TODO:
  76. # Android: We have to add the data directory to the apk, concretely the Api and Tools folders.
  77. raise RuntimeError('This module does not currently support building for this platform with tools enabled')
  78. if is_android and mono_static:
  79. # Android: 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
  80. raise RuntimeError('Statically linking Mono is not currently supported on this platform')
  81. if is_javascript:
  82. mono_static = True
  83. if not mono_prefix and (os.getenv('MONO32_PREFIX') or os.getenv('MONO64_PREFIX')):
  84. print("WARNING: The environment variables 'MONO32_PREFIX' and 'MONO64_PREFIX' are deprecated; use the 'mono_prefix' SCons parameter instead")
  85. if env['platform'] == 'windows':
  86. mono_root = mono_prefix
  87. if not mono_root and os.name == 'nt':
  88. mono_root = monoreg.find_mono_root_dir(bits)
  89. if not mono_root:
  90. raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")
  91. print('Found Mono root directory: ' + mono_root)
  92. mono_lib_path = os.path.join(mono_root, 'lib')
  93. env.Append(LIBPATH=mono_lib_path)
  94. env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  95. lib_suffixes = ['.lib']
  96. if not env.msvc:
  97. # MingW supports both '.a' and '.lib'
  98. lib_suffixes.insert(0, '.a')
  99. if mono_static:
  100. if env.msvc:
  101. mono_static_lib_name = 'libmono-static-sgen'
  102. else:
  103. mono_static_lib_name = 'libmonosgen-2.0'
  104. mono_static_lib_file = find_file_in_dir(mono_lib_path, [mono_static_lib_name], extensions=lib_suffixes)
  105. if not mono_static_lib_file:
  106. raise RuntimeError('Could not find static mono library in: ' + mono_lib_path)
  107. if env.msvc:
  108. env.Append(LINKFLAGS=mono_static_lib_file)
  109. env.Append(LINKFLAGS='Mincore.lib')
  110. env.Append(LINKFLAGS='msvcrt.lib')
  111. env.Append(LINKFLAGS='LIBCMT.lib')
  112. env.Append(LINKFLAGS='Psapi.lib')
  113. else:
  114. mono_static_lib_file_path = os.path.join(mono_lib_path, mono_static_lib_file)
  115. env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_static_lib_file_path, '-Wl,-no-whole-archive'])
  116. env.Append(LIBS=['psapi'])
  117. env.Append(LIBS=['version'])
  118. else:
  119. mono_lib_name = find_name_in_dir_files(mono_lib_path, mono_lib_names, prefixes=['', 'lib'], extensions=lib_suffixes)
  120. if not mono_lib_name:
  121. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  122. if env.msvc:
  123. env.Append(LINKFLAGS=mono_lib_name + '.lib')
  124. else:
  125. env.Append(LIBS=[mono_lib_name])
  126. mono_bin_path = os.path.join(mono_root, 'bin')
  127. mono_dll_file = find_file_in_dir(mono_bin_path, mono_lib_names, prefixes=['', 'lib'], extensions=['.dll'])
  128. if not mono_dll_file:
  129. raise RuntimeError('Could not find mono shared library in: ' + mono_bin_path)
  130. copy_file(mono_bin_path, '#bin', mono_dll_file)
  131. else:
  132. is_apple = env['platform'] in ['osx', 'iphone']
  133. sharedlib_ext = '.dylib' if is_apple else '.so'
  134. mono_root = mono_prefix
  135. mono_lib_path = ''
  136. mono_so_file = ''
  137. if not mono_root and (is_android or is_javascript):
  138. raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")
  139. if not mono_root and is_apple:
  140. # Try with some known directories under OSX
  141. hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current', '/usr/local/var/homebrew/linked/mono']
  142. for hint_dir in hint_dirs:
  143. if os.path.isdir(hint_dir):
  144. mono_root = hint_dir
  145. break
  146. # We can't use pkg-config to link mono statically,
  147. # but we can still use it to find the mono root directory
  148. if not mono_root and mono_static:
  149. mono_root = pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext)
  150. if not mono_root:
  151. raise RuntimeError("Building with mono_static=yes, but failed to find the mono prefix with pkg-config; " + \
  152. "specify one manually with the 'mono_prefix' SCons parameter")
  153. if mono_root:
  154. print('Found Mono root directory: ' + mono_root)
  155. mono_lib_path = os.path.join(mono_root, 'lib')
  156. env.Append(LIBPATH=[mono_lib_path])
  157. env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  158. mono_lib = find_name_in_dir_files(mono_lib_path, mono_lib_names, prefixes=['lib'], extensions=['.a'])
  159. if not mono_lib:
  160. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  161. env_mono.Append(CPPDEFINES=['_REENTRANT'])
  162. if mono_static:
  163. env.Append(LINKFLAGS=['-rdynamic'])
  164. mono_lib_file = os.path.join(mono_lib_path, 'lib' + mono_lib + '.a')
  165. if is_apple:
  166. env.Append(LINKFLAGS=['-Wl,-force_load,' + mono_lib_file])
  167. else:
  168. assert is_desktop(env['platform']) or is_android or is_javascript
  169. env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_lib_file, '-Wl,-no-whole-archive'])
  170. if is_javascript:
  171. env.Append(LIBS=['mono-icall-table', 'mono-native', 'mono-ilgen', 'mono-ee-interp'])
  172. wasm_src_dir = os.path.join(mono_root, 'src')
  173. if not os.path.isdir(wasm_src_dir):
  174. raise RuntimeError('Could not find mono wasm src directory')
  175. # Ideally this should be defined only for 'driver.c', but I can't fight scons for another 2 hours
  176. env_mono.Append(CPPDEFINES=['CORE_BINDINGS'])
  177. env_mono.add_source_files(env.modules_sources, [
  178. os.path.join(wasm_src_dir, 'driver.c'),
  179. os.path.join(wasm_src_dir, 'zlib-helper.c'),
  180. os.path.join(wasm_src_dir, 'corebindings.c')
  181. ])
  182. env.Append(LINKFLAGS=[
  183. '--js-library', os.path.join(wasm_src_dir, 'library_mono.js'),
  184. '--js-library', os.path.join(wasm_src_dir, 'binding_support.js'),
  185. '--js-library', os.path.join(wasm_src_dir, 'dotnet_support.js')
  186. ])
  187. else:
  188. env.Append(LIBS=[mono_lib])
  189. if is_apple:
  190. env.Append(LIBS=['iconv', 'pthread'])
  191. elif is_android:
  192. pass # Nothing
  193. elif is_javascript:
  194. env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])
  195. else:
  196. env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])
  197. if not mono_static:
  198. mono_so_file = find_file_in_dir(mono_lib_path, mono_lib_names, prefixes=['lib'], extensions=[sharedlib_ext])
  199. if not mono_so_file:
  200. raise RuntimeError('Could not find mono shared library in: ' + mono_lib_path)
  201. else:
  202. assert not mono_static
  203. # TODO: Add option to force using pkg-config
  204. print('Mono root directory not found. Using pkg-config instead')
  205. env.ParseConfig('pkg-config monosgen-2 --libs')
  206. env_mono.ParseConfig('pkg-config monosgen-2 --cflags')
  207. tmpenv = Environment()
  208. tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
  209. tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
  210. for hint_dir in tmpenv['LIBPATH']:
  211. file_found = find_file_in_dir(hint_dir, mono_lib_names, prefixes=['lib'], extensions=[sharedlib_ext])
  212. if file_found:
  213. mono_lib_path = hint_dir
  214. mono_so_file = file_found
  215. break
  216. if not mono_so_file:
  217. raise RuntimeError('Could not find mono shared library in: ' + str(tmpenv['LIBPATH']))
  218. if not mono_static:
  219. libs_output_dir = get_android_out_dir(env) if is_android else '#bin'
  220. copy_file(mono_lib_path, libs_output_dir, mono_so_file)
  221. if not tools_enabled:
  222. if is_desktop(env['platform']):
  223. if not mono_root:
  224. mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()
  225. make_template_dir(env, mono_root)
  226. elif is_android:
  227. # Compress Android Mono Config
  228. from . import make_android_mono_config
  229. module_dir = os.getcwd()
  230. config_file_path = os.path.join(module_dir, 'build_scripts', 'mono_android_config.xml')
  231. make_android_mono_config.generate_compressed_config(config_file_path, 'mono_gd/')
  232. # Copy the required shared libraries
  233. copy_mono_shared_libs(env, mono_root, None)
  234. elif is_javascript:
  235. pass # No data directory for this platform
  236. if copy_mono_root:
  237. if not mono_root:
  238. mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()
  239. if tools_enabled:
  240. copy_mono_root_files(env, mono_root)
  241. else:
  242. print("Ignoring option: 'copy_mono_root'; only available for builds with 'tools' enabled.")
  243. def make_template_dir(env, mono_root):
  244. from shutil import rmtree
  245. platform = env['platform']
  246. target = env['target']
  247. template_dir_name = ''
  248. assert is_desktop(platform)
  249. template_dir_name = 'data.mono.%s.%s.%s' % (platform, env['bits'], target)
  250. output_dir = Dir('#bin').abspath
  251. template_dir = os.path.join(output_dir, template_dir_name)
  252. template_mono_root_dir = os.path.join(template_dir, 'Mono')
  253. if os.path.isdir(template_mono_root_dir):
  254. rmtree(template_mono_root_dir) # Clean first
  255. # Copy etc/mono/
  256. template_mono_config_dir = os.path.join(template_mono_root_dir, 'etc', 'mono')
  257. copy_mono_etc_dir(mono_root, template_mono_config_dir, platform)
  258. # Copy the required shared libraries
  259. copy_mono_shared_libs(env, mono_root, template_mono_root_dir)
  260. def copy_mono_root_files(env, mono_root):
  261. from glob import glob
  262. from shutil import copy
  263. from shutil import rmtree
  264. if not mono_root:
  265. raise RuntimeError('Mono installation directory not found')
  266. output_dir = Dir('#bin').abspath
  267. editor_mono_root_dir = os.path.join(output_dir, 'GodotSharp', 'Mono')
  268. if os.path.isdir(editor_mono_root_dir):
  269. rmtree(editor_mono_root_dir) # Clean first
  270. # Copy etc/mono/
  271. editor_mono_config_dir = os.path.join(editor_mono_root_dir, 'etc', 'mono')
  272. copy_mono_etc_dir(mono_root, editor_mono_config_dir, env['platform'])
  273. # Copy the required shared libraries
  274. copy_mono_shared_libs(env, mono_root, editor_mono_root_dir)
  275. # Copy framework assemblies
  276. mono_framework_dir = os.path.join(mono_root, 'lib', 'mono', '4.5')
  277. mono_framework_facades_dir = os.path.join(mono_framework_dir, 'Facades')
  278. editor_mono_framework_dir = os.path.join(editor_mono_root_dir, 'lib', 'mono', '4.5')
  279. editor_mono_framework_facades_dir = os.path.join(editor_mono_framework_dir, 'Facades')
  280. if not os.path.isdir(editor_mono_framework_dir):
  281. os.makedirs(editor_mono_framework_dir)
  282. if not os.path.isdir(editor_mono_framework_facades_dir):
  283. os.makedirs(editor_mono_framework_facades_dir)
  284. for assembly in glob(os.path.join(mono_framework_dir, '*.dll')):
  285. copy(assembly, editor_mono_framework_dir)
  286. for assembly in glob(os.path.join(mono_framework_facades_dir, '*.dll')):
  287. copy(assembly, editor_mono_framework_facades_dir)
  288. def copy_mono_etc_dir(mono_root, target_mono_config_dir, platform):
  289. from distutils.dir_util import copy_tree
  290. from glob import glob
  291. from shutil import copy
  292. if not os.path.isdir(target_mono_config_dir):
  293. os.makedirs(target_mono_config_dir)
  294. mono_etc_dir = os.path.join(mono_root, 'etc', 'mono')
  295. if not os.path.isdir(mono_etc_dir):
  296. mono_etc_dir = ''
  297. etc_hint_dirs = []
  298. if platform != 'windows':
  299. etc_hint_dirs += ['/etc/mono', '/usr/local/etc/mono']
  300. if 'MONO_CFG_DIR' in os.environ:
  301. etc_hint_dirs += [os.path.join(os.environ['MONO_CFG_DIR'], 'mono')]
  302. for etc_hint_dir in etc_hint_dirs:
  303. if os.path.isdir(etc_hint_dir):
  304. mono_etc_dir = etc_hint_dir
  305. break
  306. if not mono_etc_dir:
  307. raise RuntimeError('Mono installation etc directory not found')
  308. copy_tree(os.path.join(mono_etc_dir, '2.0'), os.path.join(target_mono_config_dir, '2.0'))
  309. copy_tree(os.path.join(mono_etc_dir, '4.0'), os.path.join(target_mono_config_dir, '4.0'))
  310. copy_tree(os.path.join(mono_etc_dir, '4.5'), os.path.join(target_mono_config_dir, '4.5'))
  311. if os.path.isdir(os.path.join(mono_etc_dir, 'mconfig')):
  312. copy_tree(os.path.join(mono_etc_dir, 'mconfig'), os.path.join(target_mono_config_dir, 'mconfig'))
  313. for file in glob(os.path.join(mono_etc_dir, '*')):
  314. if os.path.isfile(file):
  315. copy(file, target_mono_config_dir)
  316. def copy_mono_shared_libs(env, mono_root, target_mono_root_dir):
  317. from shutil import copy
  318. def copy_if_exists(src, dst):
  319. if os.path.isfile(src):
  320. copy(src, dst)
  321. platform = env['platform']
  322. if platform == 'windows':
  323. src_mono_bin_dir = os.path.join(mono_root, 'bin')
  324. target_mono_bin_dir = os.path.join(target_mono_root_dir, 'bin')
  325. if not os.path.isdir(target_mono_bin_dir):
  326. os.makedirs(target_mono_bin_dir)
  327. mono_posix_helper_file = find_file_in_dir(src_mono_bin_dir, ['MonoPosixHelper'], prefixes=['', 'lib'], extensions=['.dll'])
  328. copy(os.path.join(src_mono_bin_dir, mono_posix_helper_file), os.path.join(target_mono_bin_dir, 'MonoPosixHelper.dll'))
  329. # For newer versions
  330. btls_dll_path = os.path.join(src_mono_bin_dir, 'libmono-btls-shared.dll')
  331. if os.path.isfile(btls_dll_path):
  332. copy(btls_dll_path, target_mono_bin_dir)
  333. else:
  334. target_mono_lib_dir = get_android_out_dir(env) if platform == 'android' else os.path.join(target_mono_root_dir, 'lib')
  335. if not os.path.isdir(target_mono_lib_dir):
  336. os.makedirs(target_mono_lib_dir)
  337. lib_file_names = []
  338. if platform == 'osx':
  339. lib_file_names = [lib_name + '.dylib' for lib_name in [
  340. 'libmono-btls-shared', 'libmono-native-compat', 'libMonoPosixHelper'
  341. ]]
  342. elif is_unix_like(platform):
  343. lib_file_names = [lib_name + '.so' for lib_name in [
  344. 'libmono-btls-shared', 'libmono-ee-interp', 'libmono-native', 'libMonoPosixHelper',
  345. 'libmono-profiler-aot', 'libmono-profiler-coverage', 'libmono-profiler-log', 'libMonoSupportW'
  346. ]]
  347. for lib_file_name in lib_file_names:
  348. copy_if_exists(os.path.join(mono_root, 'lib', lib_file_name), target_mono_lib_dir)
  349. def pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext):
  350. tmpenv = Environment()
  351. tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
  352. tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
  353. for hint_dir in tmpenv['LIBPATH']:
  354. name_found = find_name_in_dir_files(hint_dir, mono_lib_names, prefixes=['lib'], extensions=[sharedlib_ext])
  355. if name_found and os.path.isdir(os.path.join(hint_dir, '..', 'include', 'mono-2.0')):
  356. return os.path.join(hint_dir, '..')
  357. return ''