summaryrefslogtreecommitdiff
path: root/modules/mono/build_scripts/mono_configure.py
blob: c549640d6145dba9056de3e906453fde3b1fcdb2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import imp
import os
import os.path
import sys
import subprocess

from distutils.version import LooseVersion
from SCons.Script import Dir, Environment

if os.name == 'nt':
    from . import mono_reg_utils as monoreg


android_arch_dirs = {
    'armv7': 'armeabi-v7a',
    'arm64v8': 'arm64-v8a',
    'x86': 'x86',
    'x86_64': 'x86_64'
}


def get_android_out_dir(env):
    return os.path.join(Dir('#platform/android/java/libs').abspath,
                        'release' if env['target'] == 'release' else 'debug',
                        android_arch_dirs[env['android_arch']])


def find_file_in_dir(directory, files, prefix='', extension=''):
    if not extension.startswith('.'):
        extension = '.' + extension
    for curfile in files:
        if os.path.isfile(os.path.join(directory, prefix + curfile + extension)):
            return curfile
    return ''


def copy_file(src_dir, dst_dir, name):
    from shutil import copy

    src_path = os.path.join(Dir(src_dir).abspath, name)
    dst_dir = Dir(dst_dir).abspath

    if not os.path.isdir(dst_dir):
        os.mkdir(dst_dir)

    copy(src_path, dst_dir)


def configure(env, env_mono):
    bits = env['bits']
    is_android = env['platform'] == 'android'

    tools_enabled = env['tools']
    mono_static = env['mono_static']
    copy_mono_root = env['copy_mono_root']

    mono_prefix = env['mono_prefix']

    mono_lib_names = ['mono-2.0-sgen', 'monosgen-2.0']

    if is_android and not env['android_arch'] in android_arch_dirs:
        raise RuntimeError('This module does not support for the specified \'android_arch\': ' + env['android_arch'])

    if is_android and tools_enabled:
        # TODO: Implement this. We have to add the data directory to the apk, concretely the Api and Tools folders.
        raise RuntimeError('This module does not currently support building for android with tools enabled')

    if (os.getenv('MONO32_PREFIX') or os.getenv('MONO64_PREFIX')) and not mono_prefix:
        print("WARNING: The environment variables 'MONO32_PREFIX' and 'MONO64_PREFIX' are deprecated; use the 'mono_prefix' SCons parameter instead")

    if env['platform'] == 'windows':
        mono_root = mono_prefix

        if not mono_root and os.name == 'nt':
            mono_root = monoreg.find_mono_root_dir(bits)

        if not mono_root:
            raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")

        print('Found Mono root directory: ' + mono_root)

        mono_version = mono_root_try_find_mono_version(mono_root)
        configure_for_mono_version(env_mono, mono_version)

        mono_lib_path = os.path.join(mono_root, 'lib')

        env.Append(LIBPATH=mono_lib_path)
        env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))

        if mono_static:
            lib_suffix = Environment()['LIBSUFFIX']

            if env.msvc:
                mono_static_lib_name = 'libmono-static-sgen'
            else:
                mono_static_lib_name = 'libmonosgen-2.0'

            if not os.path.isfile(os.path.join(mono_lib_path, mono_static_lib_name + lib_suffix)):
                raise RuntimeError('Could not find static mono library in: ' + mono_lib_path)

            if env.msvc:
                env.Append(LINKFLAGS=mono_static_lib_name + lib_suffix)

                env.Append(LINKFLAGS='Mincore' + lib_suffix)
                env.Append(LINKFLAGS='msvcrt' + lib_suffix)
                env.Append(LINKFLAGS='LIBCMT' + lib_suffix)
                env.Append(LINKFLAGS='Psapi' + lib_suffix)
            else:
                env.Append(LINKFLAGS=os.path.join(mono_lib_path, mono_static_lib_name + lib_suffix))

                env.Append(LIBS='psapi')
                env.Append(LIBS='version')
        else:
            mono_lib_name = find_file_in_dir(mono_lib_path, mono_lib_names, extension='.lib')

            if not mono_lib_name:
                raise RuntimeError('Could not find mono library in: ' + mono_lib_path)

            if env.msvc:
                env.Append(LINKFLAGS=mono_lib_name + Environment()['LIBSUFFIX'])
            else:
                env.Append(LIBS=mono_lib_name)

            mono_bin_path = os.path.join(mono_root, 'bin')

            mono_dll_name = find_file_in_dir(mono_bin_path, mono_lib_names, extension='.dll')

            if not mono_dll_name:
                raise RuntimeError('Could not find mono shared library in: ' + mono_bin_path)

            copy_file(mono_bin_path, '#bin', mono_dll_name + '.dll')
    else:
        is_apple = (sys.platform == 'darwin' or "osxcross" in env)

        sharedlib_ext = '.dylib' if is_apple else '.so'

        mono_root = mono_prefix
        mono_lib_path = ''
        mono_so_name = ''

        if not mono_root and is_android:
            raise RuntimeError("Mono installation directory not found; specify one manually with the 'mono_prefix' SCons parameter")

        if not mono_root and is_apple:
            # Try with some known directories under OSX
            hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current', '/usr/local/var/homebrew/linked/mono']
            for hint_dir in hint_dirs:
                if os.path.isdir(hint_dir):
                    mono_root = hint_dir
                    break

        # We can't use pkg-config to link mono statically,
        # but we can still use it to find the mono root directory
        if not mono_root and mono_static:
            mono_root = pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext)
            if not mono_root:
                raise RuntimeError("Building with mono_static=yes, but failed to find the mono prefix with pkg-config; " + \
                    "specify one manually with the 'mono_prefix' SCons parameter")

        if mono_root:
            print('Found Mono root directory: ' + mono_root)

            mono_version = mono_root_try_find_mono_version(mono_root)
            configure_for_mono_version(env_mono, mono_version)

            mono_lib_path = os.path.join(mono_root, 'lib')

            env.Append(LIBPATH=mono_lib_path)
            env_mono.Prepend(CPPPATH=os.path.join(mono_root, 'include', 'mono-2.0'))

            mono_lib = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension='.a')

            if not mono_lib:
                raise RuntimeError('Could not find mono library in: ' + mono_lib_path)

            env_mono.Append(CPPFLAGS=['-D_REENTRANT'])

            if mono_static:
                mono_lib_file = os.path.join(mono_lib_path, 'lib' + mono_lib + '.a')

                if is_apple:
                    env.Append(LINKFLAGS=['-Wl,-force_load,' + mono_lib_file])
                else:
                    env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_lib_file, '-Wl,-no-whole-archive'])
            else:
                env.Append(LIBS=[mono_lib])

            if is_apple:
                env.Append(LIBS=['iconv', 'pthread'])
            elif is_android:
                env.Append(LIBS=['m', 'dl'])
            else:
                env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])

            if not mono_static:
                mono_so_name = find_file_in_dir(mono_lib_path, mono_lib_names, prefix='lib', extension=sharedlib_ext)

                if not mono_so_name:
                    raise RuntimeError('Could not find mono shared library in: ' + mono_lib_path)

                copy_file(mono_lib_path, '#bin', 'lib' + mono_so_name + sharedlib_ext)
        else:
            assert not mono_static

            # TODO: Add option to force using pkg-config
            print('Mono root directory not found. Using pkg-config instead')

            mono_version = pkgconfig_try_find_mono_version()
            configure_for_mono_version(env_mono, mono_version)

            env.ParseConfig('pkg-config monosgen-2 --libs')
            env_mono.ParseConfig('pkg-config monosgen-2 --cflags')

            tmpenv = Environment()
            tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
            tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')

            for hint_dir in tmpenv['LIBPATH']:
                name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext)
                if name_found:
                    mono_lib_path = hint_dir
                    mono_so_name = name_found
                    break

            if not mono_so_name:
                raise RuntimeError('Could not find mono shared library in: ' + str(tmpenv['LIBPATH']))

        if not mono_static:
            libs_output_dir = get_android_out_dir(env) if is_android else '#bin'
            copy_file(mono_lib_path, libs_output_dir, 'lib' + mono_so_name + sharedlib_ext)

        env.Append(LINKFLAGS='-rdynamic')

    if not tools_enabled and not is_android:
        if not mono_root:
            mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()

        make_template_dir(env, mono_root)

    if copy_mono_root:
        if not mono_root:
            mono_root = subprocess.check_output(['pkg-config', 'mono-2', '--variable=prefix']).decode('utf8').strip()

        if tools_enabled:
           copy_mono_root_files(env, mono_root)
        else:
            print("Ignoring option: 'copy_mono_root'. Only available for builds with 'tools' enabled.")


def make_template_dir(env, mono_root):
    from shutil import rmtree

    platform = env['platform']
    target = env['target']

    template_dir_name = ''

    if platform in ['windows', 'osx', 'x11', 'android']:
        template_dir_name = 'data.mono.%s.%s.%s' % (platform, env['bits'], target)
    else:
        assert False

    output_dir = Dir('#bin').abspath
    template_dir = os.path.join(output_dir, template_dir_name)

    template_mono_root_dir = os.path.join(template_dir, 'Mono')

    if os.path.isdir(template_mono_root_dir):
        rmtree(template_mono_root_dir) # Clean first

    # Copy etc/mono/

    if platform != 'android':
        template_mono_config_dir = os.path.join(template_mono_root_dir, 'etc', 'mono')
        copy_mono_etc_dir(mono_root, template_mono_config_dir, env['platform'])

    # Copy the required shared libraries

    copy_mono_shared_libs(env, mono_root, template_mono_root_dir)


def copy_mono_root_files(env, mono_root):
    from glob import glob
    from shutil import copy
    from shutil import rmtree

    if not mono_root:
        raise RuntimeError('Mono installation directory not found')

    output_dir = Dir('#bin').abspath
    editor_mono_root_dir = os.path.join(output_dir, 'GodotSharp', 'Mono')

    if os.path.isdir(editor_mono_root_dir):
        rmtree(editor_mono_root_dir) # Clean first

    # Copy etc/mono/

    editor_mono_config_dir = os.path.join(editor_mono_root_dir, 'etc', 'mono')
    copy_mono_etc_dir(mono_root, editor_mono_config_dir, env['platform'])

    # Copy the required shared libraries

    copy_mono_shared_libs(env, mono_root, editor_mono_root_dir)

    # Copy framework assemblies

    mono_framework_dir = os.path.join(mono_root, 'lib', 'mono', '4.5')
    mono_framework_facades_dir = os.path.join(mono_framework_dir, 'Facades')

    editor_mono_framework_dir = os.path.join(editor_mono_root_dir, 'lib', 'mono', '4.5')
    editor_mono_framework_facades_dir = os.path.join(editor_mono_framework_dir, 'Facades')

    if not os.path.isdir(editor_mono_framework_dir):
        os.makedirs(editor_mono_framework_dir)
    if not os.path.isdir(editor_mono_framework_facades_dir):
        os.makedirs(editor_mono_framework_facades_dir)

    for assembly in glob(os.path.join(mono_framework_dir, '*.dll')):
        copy(assembly, editor_mono_framework_dir)
    for assembly in glob(os.path.join(mono_framework_facades_dir, '*.dll')):
        copy(assembly, editor_mono_framework_facades_dir)


def copy_mono_etc_dir(mono_root, target_mono_config_dir, platform):
    from distutils.dir_util import copy_tree
    from glob import glob
    from shutil import copy

    if not os.path.isdir(target_mono_config_dir):
        os.makedirs(target_mono_config_dir)

    mono_etc_dir = os.path.join(mono_root, 'etc', 'mono')
    if not os.path.isdir(mono_etc_dir):
        mono_etc_dir = ''
        etc_hint_dirs = []
        if platform != 'windows':
            etc_hint_dirs += ['/etc/mono', '/usr/local/etc/mono']
        if 'MONO_CFG_DIR' in os.environ:
            etc_hint_dirs += [os.path.join(os.environ['MONO_CFG_DIR'], 'mono')]
        for etc_hint_dir in etc_hint_dirs:
            if os.path.isdir(etc_hint_dir):
                mono_etc_dir = etc_hint_dir
                break
        if not mono_etc_dir:
            raise RuntimeError('Mono installation etc directory not found')

    copy_tree(os.path.join(mono_etc_dir, '2.0'), os.path.join(target_mono_config_dir, '2.0'))
    copy_tree(os.path.join(mono_etc_dir, '4.0'), os.path.join(target_mono_config_dir, '4.0'))
    copy_tree(os.path.join(mono_etc_dir, '4.5'), os.path.join(target_mono_config_dir, '4.5'))
    if os.path.isdir(os.path.join(mono_etc_dir, 'mconfig')):
        copy_tree(os.path.join(mono_etc_dir, 'mconfig'), os.path.join(target_mono_config_dir, 'mconfig'))

    for file in glob(os.path.join(mono_etc_dir, '*')):
        if os.path.isfile(file):
            copy(file, target_mono_config_dir)


def copy_mono_shared_libs(env, mono_root, target_mono_root_dir):
    from shutil import copy

    def copy_if_exists(src, dst):
        if os.path.isfile(src):
            copy(src, dst)

    platform = env['platform']

    if platform == 'windows':
        target_mono_bin_dir = os.path.join(target_mono_root_dir, 'bin')

        if not os.path.isdir(target_mono_bin_dir):
            os.makedirs(target_mono_bin_dir)

        copy(os.path.join(mono_root, 'bin', 'MonoPosixHelper.dll'), target_mono_bin_dir)
    else:
        target_mono_lib_dir = get_android_out_dir(env) if platform == 'android' else os.path.join(target_mono_root_dir, 'lib')

        if not os.path.isdir(target_mono_lib_dir):
            os.makedirs(target_mono_lib_dir)

        if platform == 'osx':
            # TODO: Make sure nothing is missing
            copy(os.path.join(mono_root, 'lib', 'libMonoPosixHelper.dylib'), target_mono_lib_dir)
        elif platform == 'x11' or platform == 'android':
            lib_file_names = [lib_name + '.so' for lib_name in [
                'libmono-btls-shared', 'libmono-ee-interp', 'libmono-native', 'libMonoPosixHelper',
                'libmono-profiler-aot', 'libmono-profiler-coverage', 'libmono-profiler-log', 'libMonoSupportW'
            ]]

            for lib_file_name in lib_file_names:
                copy_if_exists(os.path.join(mono_root, 'lib', lib_file_name), target_mono_lib_dir)


def configure_for_mono_version(env, mono_version):
    if mono_version is None:
        if os.getenv('MONO_VERSION'):
            mono_version = os.getenv('MONO_VERSION')
        else:
            raise RuntimeError("Mono JIT compiler version not found; specify one manually with the 'MONO_VERSION' environment variable")
    print('Found Mono JIT compiler version: ' + str(mono_version))
    if mono_version >= LooseVersion('5.12.0'):
        env.Append(CPPFLAGS=['-DHAS_PENDING_EXCEPTIONS'])


def pkgconfig_try_find_mono_root(mono_lib_names, sharedlib_ext):
    tmpenv = Environment()
    tmpenv.AppendENVPath('PKG_CONFIG_PATH', os.getenv('PKG_CONFIG_PATH'))
    tmpenv.ParseConfig('pkg-config monosgen-2 --libs-only-L')
    for hint_dir in tmpenv['LIBPATH']:
        name_found = find_file_in_dir(hint_dir, mono_lib_names, prefix='lib', extension=sharedlib_ext)
        if name_found and os.path.isdir(os.path.join(hint_dir, '..', 'include', 'mono-2.0')):
            return os.path.join(hint_dir, '..')
    return ''


def pkgconfig_try_find_mono_version():
    from compat import decode_utf8

    lines = subprocess.check_output(['pkg-config', 'monosgen-2', '--modversion']).splitlines()
    greater_version = None
    for line in lines:
        try:
            version = LooseVersion(decode_utf8(line))
            if greater_version is None or version > greater_version:
                greater_version = version
        except ValueError:
            pass
    return greater_version


def mono_root_try_find_mono_version(mono_root):
    from compat import decode_utf8

    mono_bin = os.path.join(mono_root, 'bin')
    if os.path.isfile(os.path.join(mono_bin, 'mono')):
        mono_binary = os.path.join(mono_bin, 'mono')
    elif os.path.isfile(os.path.join(mono_bin, 'mono.exe')):
        mono_binary = os.path.join(mono_bin, 'mono.exe')
    else:
        return None
    output = subprocess.check_output([mono_binary, '--version'])
    first_line = decode_utf8(output.splitlines()[0])
    try:
        return LooseVersion(first_line.split()[len('Mono JIT compiler version'.split())])
    except (ValueError, IndexError):
        return None