mono_configure.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. import os
  2. import os.path
  3. import subprocess
  4. from SCons.Script import Dir, Environment
  5. if os.name == "nt":
  6. from . import mono_reg_utils as monoreg
  7. android_arch_dirs = {
  8. "armv7": "armeabi-v7a",
  9. "arm64v8": "arm64-v8a",
  10. "x86": "x86",
  11. "x86_64": "x86_64",
  12. }
  13. def get_android_out_dir(env):
  14. return os.path.join(
  15. Dir("#platform/android/java/lib/libs").abspath,
  16. "release" if env["target"] == "release" else "debug",
  17. android_arch_dirs[env["android_arch"]],
  18. )
  19. def find_name_in_dir_files(directory, names, prefixes=[""], extensions=[""]):
  20. for extension in extensions:
  21. if extension and not extension.startswith("."):
  22. extension = "." + extension
  23. for prefix in prefixes:
  24. for curname in names:
  25. if os.path.isfile(os.path.join(directory, prefix + curname + extension)):
  26. return curname
  27. return ""
  28. def find_file_in_dir(directory, names, prefixes=[""], extensions=[""]):
  29. for extension in extensions:
  30. if extension and not extension.startswith("."):
  31. extension = "." + extension
  32. for prefix in prefixes:
  33. for curname in names:
  34. filename = prefix + curname + extension
  35. if os.path.isfile(os.path.join(directory, filename)):
  36. return filename
  37. return ""
  38. def copy_file(src_dir, dst_dir, src_name, dst_name=""):
  39. from shutil import copy
  40. src_path = os.path.join(Dir(src_dir).abspath, src_name)
  41. dst_dir = Dir(dst_dir).abspath
  42. if not os.path.isdir(dst_dir):
  43. os.makedirs(dst_dir)
  44. if dst_name:
  45. copy(src_path, os.path.join(dst_dir, dst_name))
  46. else:
  47. copy(src_path, dst_dir)
  48. def is_desktop(platform):
  49. return platform in ["windows", "osx", "linuxbsd", "server", "uwp", "haiku"]
  50. def is_unix_like(platform):
  51. return platform in ["osx", "linuxbsd", "server", "android", "haiku", "iphone"]
  52. def module_supports_tools_on(platform):
  53. return platform not in ["android", "javascript", "iphone"]
  54. def find_wasm_src_dir(mono_root):
  55. hint_dirs = [
  56. os.path.join(mono_root, "src"),
  57. os.path.join(mono_root, "../src"),
  58. ]
  59. for hint_dir in hint_dirs:
  60. if os.path.isfile(os.path.join(hint_dir, "driver.c")):
  61. return hint_dir
  62. return ""
  63. def configure(env, env_mono):
  64. bits = env["bits"]
  65. is_android = env["platform"] == "android"
  66. is_javascript = env["platform"] == "javascript"
  67. is_ios = env["platform"] == "iphone"
  68. is_ios_sim = is_ios and env["arch"] in ["x86", "x86_64"]
  69. tools_enabled = env["tools"]
  70. mono_static = env["mono_static"]
  71. copy_mono_root = env["copy_mono_root"]
  72. mono_prefix = env["mono_prefix"]
  73. mono_bcl = env["mono_bcl"]
  74. mono_lib_names = ["mono-2.0-sgen", "monosgen-2.0"]
  75. if is_android and not env["android_arch"] in android_arch_dirs:
  76. raise RuntimeError("This module does not support the specified 'android_arch': " + env["android_arch"])
  77. if tools_enabled and not module_supports_tools_on(env["platform"]):
  78. # TODO:
  79. # Android: We have to add the data directory to the apk, concretely the Api and Tools folders.
  80. raise RuntimeError("This module does not currently support building for this platform with tools enabled")
  81. if is_android and mono_static:
  82. # FIXME: When static linking and doing something that requires libmono-native, we get a dlopen error as 'libmono-native'
  83. # seems to depend on 'libmonosgen-2.0'. Could be fixed by re-directing to '__Internal' with a dllmap or in the dlopen hook.
  84. raise RuntimeError("Statically linking Mono is not currently supported for this platform")
  85. if not mono_static and (is_javascript or is_ios):
  86. raise RuntimeError("Dynamically linking Mono is not currently supported for this platform")
  87. if not mono_prefix and (os.getenv("MONO32_PREFIX") or os.getenv("MONO64_PREFIX")):
  88. print(
  89. "WARNING: The environment variables 'MONO32_PREFIX' and 'MONO64_PREFIX' are deprecated; use the"
  90. " 'mono_prefix' SCons parameter instead"
  91. )
  92. # Although we don't support building with tools for any platform where we currently use static AOT,
  93. # if these are supported in the future, we won't be using static AOT for them as that would be
  94. # too restrictive for the editor. These builds would probably be made to only use the interpreter.
  95. mono_aot_static = (is_ios and not is_ios_sim) and not env["tools"]
  96. # Static AOT is only supported on the root domain
  97. mono_single_appdomain = mono_aot_static
  98. if mono_single_appdomain:
  99. env_mono.Append(CPPDEFINES=["GD_MONO_SINGLE_APPDOMAIN"])
  100. if (env["tools"] or env["target"] != "release") and not mono_single_appdomain:
  101. env_mono.Append(CPPDEFINES=["GD_MONO_HOT_RELOAD"])
  102. if env["platform"] == "windows":
  103. mono_root = mono_prefix
  104. if not mono_root and os.name == "nt":
  105. mono_root = monoreg.find_mono_root_dir(bits)
  106. if not mono_root:
  107. raise RuntimeError(
  108. "Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter"
  109. )
  110. print("Found Mono root directory: " + mono_root)
  111. mono_lib_path = os.path.join(mono_root, "lib")
  112. env.Append(LIBPATH=mono_lib_path)
  113. env_mono.Prepend(CPPPATH=os.path.join(mono_root, "include", "mono-2.0"))
  114. lib_suffixes = [".lib"]
  115. if not env.msvc:
  116. # MingW supports both '.a' and '.lib'
  117. lib_suffixes.insert(0, ".a")
  118. if mono_static:
  119. if env.msvc:
  120. mono_static_lib_name = "libmono-static-sgen"
  121. else:
  122. mono_static_lib_name = "libmonosgen-2.0"
  123. mono_static_lib_file = find_file_in_dir(mono_lib_path, [mono_static_lib_name], extensions=lib_suffixes)
  124. if not mono_static_lib_file:
  125. raise RuntimeError("Could not find static mono library in: " + mono_lib_path)
  126. if env.msvc:
  127. env.Append(LINKFLAGS=mono_static_lib_file)
  128. env.Append(LINKFLAGS="Mincore.lib")
  129. env.Append(LINKFLAGS="msvcrt.lib")
  130. env.Append(LINKFLAGS="LIBCMT.lib")
  131. env.Append(LINKFLAGS="Psapi.lib")
  132. else:
  133. mono_static_lib_file_path = os.path.join(mono_lib_path, mono_static_lib_file)
  134. env.Append(LINKFLAGS=["-Wl,-whole-archive", mono_static_lib_file_path, "-Wl,-no-whole-archive"])
  135. env.Append(LIBS=["psapi"])
  136. env.Append(LIBS=["version"])
  137. else:
  138. mono_lib_file = find_file_in_dir(mono_lib_path, mono_lib_names, extensions=lib_suffixes)
  139. if not mono_lib_file:
  140. raise RuntimeError("Could not find mono library in: " + mono_lib_path)
  141. if env.msvc:
  142. env.Append(LINKFLAGS=mono_lib_file)
  143. else:
  144. mono_lib_file_path = os.path.join(mono_lib_path, mono_lib_file)
  145. env.Append(LINKFLAGS=mono_lib_file_path)
  146. mono_bin_path = os.path.join(mono_root, "bin")
  147. mono_dll_file = find_file_in_dir(mono_bin_path, mono_lib_names, prefixes=["", "lib"], extensions=[".dll"])
  148. if not mono_dll_file:
  149. raise RuntimeError("Could not find mono shared library in: " + mono_bin_path)
  150. copy_file(mono_bin_path, "#bin", mono_dll_file)
  151. else:
  152. is_apple = env["platform"] in ["osx", "iphone"]
  153. is_macos = is_apple and not is_ios
  154. sharedlib_ext = ".dylib" if is_apple else ".so"
  155. mono_root = mono_prefix
  156. mono_lib_path = ""
  157. mono_so_file = ""
  158. if not mono_root and (is_android or is_javascript or is_ios):
  159. raise RuntimeError(
  160. "Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter"
  161. )
  162. if not mono_root and is_macos:
  163. # Try with some known directories under OSX
  164. hint_dirs = ["/Library/Frameworks/Mono.framework/Versions/Current", "/usr/local/var/homebrew/linked/mono"]
  165. for hint_dir in hint_dirs:
  166. if os.path.isdir(hint_dir):
  167. mono_root = hint_dir
  168. break
  169. # We can't use pkg-config to link mono statically,
  170. # but we can still use it to find the mono root directory
  171. if not mono_root and mono_static:
  172. mono_root = pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext)
  173. if not mono_root:
  174. raise RuntimeError(
  175. "Building with mono_static=yes, but failed to find the mono prefix with pkg-config; "
  176. + "specify one manually with the 'mono_prefix' SCons parameter"
  177. )
  178. if is_ios and not is_ios_sim:
  179. env_mono.Append(CPPDEFINES=["IOS_DEVICE"])
  180. if mono_root:
  181. print("Found Mono root directory: " + mono_root)
  182. mono_lib_path = os.path.join(mono_root, "lib")
  183. env.Append(LIBPATH=[mono_lib_path])
  184. env_mono.Prepend(CPPPATH=os.path.join(mono_root, "include", "mono-2.0"))
  185. mono_lib = find_name_in_dir_files(mono_lib_path, mono_lib_names, prefixes=["lib"], extensions=[".a"])
  186. if not mono_lib:
  187. raise RuntimeError("Could not find mono library in: " + mono_lib_path)
  188. env_mono.Append(CPPDEFINES=["_REENTRANT"])
  189. if mono_static:
  190. if not is_javascript:
  191. env.Append(LINKFLAGS=["-rdynamic"])
  192. mono_lib_file = os.path.join(mono_lib_path, "lib" + mono_lib + ".a")
  193. if is_apple:
  194. if is_macos:
  195. env.Append(LINKFLAGS=["-Wl,-force_load," + mono_lib_file])
  196. else:
  197. arch = env["arch"]
  198. def copy_mono_lib(libname_wo_ext):
  199. copy_file(
  200. mono_lib_path, "#bin", libname_wo_ext + ".a", "%s.iphone.%s.a" % (libname_wo_ext, arch)
  201. )
  202. # Copy Mono libraries to the output folder. These are meant to be bundled with
  203. # the export templates and added to the Xcode project when exporting a game.
  204. copy_mono_lib("lib" + mono_lib)
  205. copy_mono_lib("libmono-native")
  206. copy_mono_lib("libmono-profiler-log")
  207. if not is_ios_sim:
  208. copy_mono_lib("libmono-ee-interp")
  209. copy_mono_lib("libmono-icall-table")
  210. copy_mono_lib("libmono-ilgen")
  211. else:
  212. assert is_desktop(env["platform"]) or is_android or is_javascript
  213. env.Append(LINKFLAGS=["-Wl,-whole-archive", mono_lib_file, "-Wl,-no-whole-archive"])
  214. if is_javascript:
  215. env.Append(LIBS=["mono-icall-table", "mono-native", "mono-ilgen", "mono-ee-interp"])
  216. wasm_src_dir = os.path.join(mono_root, "src")
  217. if not os.path.isdir(wasm_src_dir):
  218. raise RuntimeError("Could not find mono wasm src directory")
  219. # Ideally this should be defined only for 'driver.c', but I can't fight scons for another 2 hours
  220. env_mono.Append(CPPDEFINES=["CORE_BINDINGS"])
  221. env_mono.add_source_files(
  222. env.modules_sources,
  223. [
  224. os.path.join(wasm_src_dir, "driver.c"),
  225. os.path.join(wasm_src_dir, "zlib-helper.c"),
  226. os.path.join(wasm_src_dir, "corebindings.c"),
  227. ],
  228. )
  229. env.Append(
  230. LINKFLAGS=[
  231. "--js-library",
  232. os.path.join(wasm_src_dir, "library_mono.js"),
  233. "--js-library",
  234. os.path.join(wasm_src_dir, "binding_support.js"),
  235. "--js-library",
  236. os.path.join(wasm_src_dir, "dotnet_support.js"),
  237. ]
  238. )
  239. else:
  240. env.Append(LIBS=[mono_lib])
  241. if is_macos:
  242. env.Append(LIBS=["iconv", "pthread"])
  243. elif is_android:
  244. pass # Nothing
  245. elif is_ios:
  246. pass # Nothing, linking is delegated to the exported Xcode project
  247. elif is_javascript:
  248. env.Append(LIBS=["m", "rt", "dl", "pthread"])
  249. else:
  250. env.Append(LIBS=["m", "rt", "dl", "pthread"])
  251. if not mono_static:
  252. mono_so_file = find_file_in_dir(
  253. mono_lib_path, mono_lib_names, prefixes=["lib"], extensions=[sharedlib_ext]
  254. )
  255. if not mono_so_file:
  256. raise RuntimeError("Could not find mono shared library in: " + mono_lib_path)
  257. else:
  258. assert not mono_static
  259. # TODO: Add option to force using pkg-config
  260. print("Mono root directory not found. Using pkg-config instead")
  261. env.ParseConfig("pkg-config monosgen-2 --libs")
  262. env_mono.ParseConfig("pkg-config monosgen-2 --cflags")
  263. tmpenv = Environment()
  264. tmpenv.AppendENVPath("PKG_CONFIG_PATH", os.getenv("PKG_CONFIG_PATH"))
  265. tmpenv.ParseConfig("pkg-config monosgen-2 --libs-only-L")
  266. for hint_dir in tmpenv["LIBPATH"]:
  267. file_found = find_file_in_dir(hint_dir, mono_lib_names, prefixes=["lib"], extensions=[sharedlib_ext])
  268. if file_found:
  269. mono_lib_path = hint_dir
  270. mono_so_file = file_found
  271. break
  272. if not mono_so_file:
  273. raise RuntimeError("Could not find mono shared library in: " + str(tmpenv["LIBPATH"]))
  274. if not mono_static:
  275. libs_output_dir = get_android_out_dir(env) if is_android else "#bin"
  276. copy_file(mono_lib_path, libs_output_dir, mono_so_file)
  277. if not tools_enabled:
  278. if is_desktop(env["platform"]):
  279. if not mono_root:
  280. mono_root = (
  281. subprocess.check_output(["pkg-config", "mono-2", "--variable=prefix"]).decode("utf8").strip()
  282. )
  283. make_template_dir(env, mono_root)
  284. elif is_android:
  285. # Compress Android Mono Config
  286. from . import make_android_mono_config
  287. module_dir = os.getcwd()
  288. config_file_path = os.path.join(module_dir, "build_scripts", "mono_android_config.xml")
  289. make_android_mono_config.generate_compressed_config(config_file_path, "mono_gd/")
  290. # Copy the required shared libraries
  291. copy_mono_shared_libs(env, mono_root, None)
  292. elif is_javascript:
  293. pass # No data directory for this platform
  294. elif is_ios:
  295. pass # No data directory for this platform
  296. if copy_mono_root:
  297. if not mono_root:
  298. mono_root = subprocess.check_output(["pkg-config", "mono-2", "--variable=prefix"]).decode("utf8").strip()
  299. if tools_enabled:
  300. # Only supported for editor builds.
  301. copy_mono_root_files(env, mono_root, mono_bcl)
  302. def make_template_dir(env, mono_root):
  303. from shutil import rmtree
  304. platform = env["platform"]
  305. target = env["target"]
  306. template_dir_name = ""
  307. assert is_desktop(platform)
  308. template_dir_name = "data.mono.%s.%s.%s" % (platform, env["bits"], target)
  309. output_dir = Dir("#bin").abspath
  310. template_dir = os.path.join(output_dir, template_dir_name)
  311. template_mono_root_dir = os.path.join(template_dir, "Mono")
  312. if os.path.isdir(template_mono_root_dir):
  313. rmtree(template_mono_root_dir) # Clean first
  314. # Copy etc/mono/
  315. template_mono_config_dir = os.path.join(template_mono_root_dir, "etc", "mono")
  316. copy_mono_etc_dir(mono_root, template_mono_config_dir, platform)
  317. # Copy the required shared libraries
  318. copy_mono_shared_libs(env, mono_root, template_mono_root_dir)
  319. def copy_mono_root_files(env, mono_root, mono_bcl):
  320. from glob import glob
  321. from shutil import copy
  322. from shutil import rmtree
  323. if not mono_root:
  324. raise RuntimeError("Mono installation directory not found")
  325. output_dir = Dir("#bin").abspath
  326. editor_mono_root_dir = os.path.join(output_dir, "GodotSharp", "Mono")
  327. if os.path.isdir(editor_mono_root_dir):
  328. rmtree(editor_mono_root_dir) # Clean first
  329. # Copy etc/mono/
  330. editor_mono_config_dir = os.path.join(editor_mono_root_dir, "etc", "mono")
  331. copy_mono_etc_dir(mono_root, editor_mono_config_dir, env["platform"])
  332. # Copy the required shared libraries
  333. copy_mono_shared_libs(env, mono_root, editor_mono_root_dir)
  334. # Copy framework assemblies
  335. mono_framework_dir = mono_bcl or os.path.join(mono_root, "lib", "mono", "4.5")
  336. mono_framework_facades_dir = os.path.join(mono_framework_dir, "Facades")
  337. editor_mono_framework_dir = os.path.join(editor_mono_root_dir, "lib", "mono", "4.5")
  338. editor_mono_framework_facades_dir = os.path.join(editor_mono_framework_dir, "Facades")
  339. if not os.path.isdir(editor_mono_framework_dir):
  340. os.makedirs(editor_mono_framework_dir)
  341. if not os.path.isdir(editor_mono_framework_facades_dir):
  342. os.makedirs(editor_mono_framework_facades_dir)
  343. for assembly in glob(os.path.join(mono_framework_dir, "*.dll")):
  344. copy(assembly, editor_mono_framework_dir)
  345. for assembly in glob(os.path.join(mono_framework_facades_dir, "*.dll")):
  346. copy(assembly, editor_mono_framework_facades_dir)
  347. def copy_mono_etc_dir(mono_root, target_mono_config_dir, platform):
  348. from distutils.dir_util import copy_tree
  349. from glob import glob
  350. from shutil import copy
  351. if not os.path.isdir(target_mono_config_dir):
  352. os.makedirs(target_mono_config_dir)
  353. mono_etc_dir = os.path.join(mono_root, "etc", "mono")
  354. if not os.path.isdir(mono_etc_dir):
  355. mono_etc_dir = ""
  356. etc_hint_dirs = []
  357. if platform != "windows":
  358. etc_hint_dirs += ["/etc/mono", "/usr/local/etc/mono"]
  359. if "MONO_CFG_DIR" in os.environ:
  360. etc_hint_dirs += [os.path.join(os.environ["MONO_CFG_DIR"], "mono")]
  361. for etc_hint_dir in etc_hint_dirs:
  362. if os.path.isdir(etc_hint_dir):
  363. mono_etc_dir = etc_hint_dir
  364. break
  365. if not mono_etc_dir:
  366. raise RuntimeError("Mono installation etc directory not found")
  367. copy_tree(os.path.join(mono_etc_dir, "2.0"), os.path.join(target_mono_config_dir, "2.0"))
  368. copy_tree(os.path.join(mono_etc_dir, "4.0"), os.path.join(target_mono_config_dir, "4.0"))
  369. copy_tree(os.path.join(mono_etc_dir, "4.5"), os.path.join(target_mono_config_dir, "4.5"))
  370. if os.path.isdir(os.path.join(mono_etc_dir, "mconfig")):
  371. copy_tree(os.path.join(mono_etc_dir, "mconfig"), os.path.join(target_mono_config_dir, "mconfig"))
  372. for file in glob(os.path.join(mono_etc_dir, "*")):
  373. if os.path.isfile(file):
  374. copy(file, target_mono_config_dir)
  375. def copy_mono_shared_libs(env, mono_root, target_mono_root_dir):
  376. from shutil import copy
  377. def copy_if_exists(src, dst):
  378. if os.path.isfile(src):
  379. copy(src, dst)
  380. platform = env["platform"]
  381. if platform == "windows":
  382. src_mono_bin_dir = os.path.join(mono_root, "bin")
  383. target_mono_bin_dir = os.path.join(target_mono_root_dir, "bin")
  384. if not os.path.isdir(target_mono_bin_dir):
  385. os.makedirs(target_mono_bin_dir)
  386. mono_posix_helper_file = find_file_in_dir(
  387. src_mono_bin_dir, ["MonoPosixHelper"], prefixes=["", "lib"], extensions=[".dll"]
  388. )
  389. copy(
  390. os.path.join(src_mono_bin_dir, mono_posix_helper_file),
  391. os.path.join(target_mono_bin_dir, "MonoPosixHelper.dll"),
  392. )
  393. # For newer versions
  394. btls_dll_path = os.path.join(src_mono_bin_dir, "libmono-btls-shared.dll")
  395. if os.path.isfile(btls_dll_path):
  396. copy(btls_dll_path, target_mono_bin_dir)
  397. else:
  398. target_mono_lib_dir = (
  399. get_android_out_dir(env) if platform == "android" else os.path.join(target_mono_root_dir, "lib")
  400. )
  401. if not os.path.isdir(target_mono_lib_dir):
  402. os.makedirs(target_mono_lib_dir)
  403. lib_file_names = []
  404. if platform == "osx":
  405. lib_file_names = [
  406. lib_name + ".dylib"
  407. for lib_name in ["libmono-btls-shared", "libmono-native-compat", "libMonoPosixHelper"]
  408. ]
  409. elif is_unix_like(platform):
  410. lib_file_names = [
  411. lib_name + ".so"
  412. for lib_name in [
  413. "libmono-btls-shared",
  414. "libmono-ee-interp",
  415. "libmono-native",
  416. "libMonoPosixHelper",
  417. "libmono-profiler-aot",
  418. "libmono-profiler-coverage",
  419. "libmono-profiler-log",
  420. "libMonoSupportW",
  421. ]
  422. ]
  423. for lib_file_name in lib_file_names:
  424. copy_if_exists(os.path.join(mono_root, "lib", lib_file_name), target_mono_lib_dir)
  425. def pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext):
  426. tmpenv = Environment()
  427. tmpenv.AppendENVPath("PKG_CONFIG_PATH", os.getenv("PKG_CONFIG_PATH"))
  428. tmpenv.ParseConfig("pkg-config monosgen-2 --libs-only-L")
  429. for hint_dir in tmpenv["LIBPATH"]:
  430. name_found = find_name_in_dir_files(hint_dir, mono_lib_names, prefixes=["lib"], extensions=[sharedlib_ext])
  431. if name_found and os.path.isdir(os.path.join(hint_dir, "..", "include", "mono-2.0")):
  432. return os.path.join(hint_dir, "..")
  433. return ""