config.py 5.9 KB

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