config.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import imp
  2. import os
  3. import sys
  4. import subprocess
  5. from SCons.Script import BoolVariable, Dir, Environment, PathVariable, Variables
  6. monoreg = imp.load_source('mono_reg_utils', 'modules/mono/mono_reg_utils.py')
  7. def find_file_in_dir(directory, files, prefix='', extension=''):
  8. if not extension.startswith('.'):
  9. extension = '.' + extension
  10. for curfile in files:
  11. if os.path.isfile(os.path.join(directory, prefix + curfile + extension)):
  12. return curfile
  13. return ''
  14. def can_build(platform):
  15. if platform in ["javascript"]:
  16. return False # Not yet supported
  17. return True
  18. def is_enabled():
  19. # The module is disabled by default. Use module_mono_enabled=yes to enable it.
  20. return False
  21. def copy_file(src_dir, dst_dir, name):
  22. from shutil import copyfile
  23. src_path = os.path.join(src_dir, name)
  24. dst_path = os.path.join(dst_dir, name)
  25. if not os.path.isdir(dst_dir):
  26. os.mkdir(dst_dir)
  27. copyfile(src_path, dst_path)
  28. def configure(env):
  29. env.use_ptrcall = True
  30. env.add_module_version_string("mono")
  31. envvars = Variables()
  32. envvars.Add(BoolVariable('mono_static', 'Statically link mono', False))
  33. envvars.Add(PathVariable('mono_assemblies_output_dir', 'Path to the assemblies output directory', '#bin', PathVariable.PathIsDirCreate))
  34. envvars.Update(env)
  35. bits = env['bits']
  36. mono_static = env['mono_static']
  37. assemblies_output_dir = Dir(env['mono_assemblies_output_dir']).abspath
  38. mono_lib_names = ['mono-2.0-sgen', 'monosgen-2.0']
  39. if env['platform'] == 'windows':
  40. if mono_static:
  41. raise RuntimeError('mono-static: Not supported on Windows')
  42. if bits == '32':
  43. if os.getenv('MONO32_PREFIX'):
  44. mono_root = os.getenv('MONO32_PREFIX')
  45. elif os.name == 'nt':
  46. mono_root = monoreg.find_mono_root_dir(bits)
  47. else:
  48. if os.getenv('MONO64_PREFIX'):
  49. mono_root = os.getenv('MONO64_PREFIX')
  50. elif os.name == 'nt':
  51. mono_root = monoreg.find_mono_root_dir(bits)
  52. if not mono_root:
  53. raise RuntimeError('Mono installation directory not found')
  54. mono_lib_path = os.path.join(mono_root, 'lib')
  55. env.Append(LIBPATH=mono_lib_path)
  56. env.Append(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  57. mono_lib_name = find_file_in_dir(mono_lib_path, mono_lib_names, extension='.lib')
  58. if not mono_lib_name:
  59. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  60. if os.getenv('VCINSTALLDIR'):
  61. env.Append(LINKFLAGS=mono_lib_name + Environment()['LIBSUFFIX'])
  62. else:
  63. env.Append(LIBS=mono_lib_name)
  64. mono_bin_path = os.path.join(mono_root, 'bin')
  65. mono_dll_name = find_file_in_dir(mono_bin_path, mono_lib_names, extension='.dll')
  66. if not mono_dll_name:
  67. raise RuntimeError('Could not find mono shared library in: ' + mono_bin_path)
  68. copy_file(mono_bin_path, 'bin', mono_dll_name + '.dll')
  69. copy_file(os.path.join(mono_lib_path, 'mono', '4.5'), assemblies_output_dir, 'mscorlib.dll')
  70. else:
  71. sharedlib_ext = '.dylib' if sys.platform == 'darwin' else '.so'
  72. mono_root = ''
  73. mono_lib_path = ''
  74. if bits == '32':
  75. if os.getenv('MONO32_PREFIX'):
  76. mono_root = os.getenv('MONO32_PREFIX')
  77. else:
  78. if os.getenv('MONO64_PREFIX'):
  79. mono_root = os.getenv('MONO64_PREFIX')
  80. if mono_root:
  81. mono_lib_path = os.path.join(mono_root, 'lib')
  82. env.Append(LIBPATH=mono_lib_path)
  83. env.Append(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))
  84. mono_lib = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension='.a')
  85. if not mono_lib:
  86. raise RuntimeError('Could not find mono library in: ' + mono_lib_path)
  87. env.Append(CPPFLAGS=['-D_REENTRANT'])
  88. if mono_static:
  89. mono_lib_file = os.path.join(mono_lib_path, 'lib' + mono_lib + '.a')
  90. if sys.platform == "darwin":
  91. env.Append(LINKFLAGS=['-Wl,-force_load,' + mono_lib_file])
  92. elif sys.platform == "linux" or sys.platform == "linux2":
  93. env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_lib_file, '-Wl,-no-whole-archive'])
  94. else:
  95. raise RuntimeError('mono-static: Not supported on this platform')
  96. else:
  97. env.Append(LIBS=[mono_lib])
  98. if sys.platform == "darwin":
  99. env.Append(LIBS=['iconv', 'pthread'])
  100. elif sys.platform == "linux" or sys.platform == "linux2":
  101. env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])
  102. if not mono_static:
  103. mono_so_name = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension=sharedlib_ext)
  104. if not mono_so_name:
  105. raise RuntimeError('Could not find mono shared library in: ' + mono_lib_path)
  106. copy_file(mono_lib_path, 'bin', 'lib' + mono_so_name + sharedlib_ext)
  107. copy_file(os.path.join(mono_lib_path, 'mono', '4.5'), assemblies_output_dir, 'mscorlib.dll')
  108. else:
  109. if mono_static:
  110. raise RuntimeError('mono-static: Not supported with pkg-config. Specify a mono prefix manually')
  111. env.ParseConfig('pkg-config monosgen-2 --cflags --libs')
  112. mono_lib_path = ''
  113. mono_so_name = ''
  114. mono_prefix = subprocess.check_output(["pkg-config", "mono-2", "--variable=prefix"]).strip()
  115. tmpenv = Environment()
  116. tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
  117. tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
  118. for hint_dir in tmpenv['LIBPATH']:
  119. name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext)
  120. if name_found:
  121. mono_lib_path = hint_dir
  122. mono_so_name = name_found
  123. break
  124. if not mono_so_name:
  125. raise RuntimeError('Could not find mono shared library in: ' + str(tmpenv['LIBPATH']))
  126. copy_file(mono_lib_path, 'bin', 'lib' + mono_so_name + sharedlib_ext)
  127. copy_file(os.path.join(mono_prefix, 'lib', 'mono', '4.5'), assemblies_output_dir, 'mscorlib.dll')
  128. env.Append(LINKFLAGS='-rdynamic')
  129. def get_doc_classes():
  130. return [
  131. "@C#",
  132. "CSharpScript",
  133. "GodotSharp",
  134. ]
  135. def get_doc_path():
  136. return "doc_classes"