Skip to content

Commit

Permalink
Merge pull request #11739 from neikeq/tengo-el-mono
Browse files Browse the repository at this point in the history
Moved mono module here
  • Loading branch information
Andreas Haas authored Oct 2, 2017
2 parents 5cd68ab + d5caf71 commit b0194a3
Show file tree
Hide file tree
Showing 93 changed files with 19,628 additions and 7 deletions.
13 changes: 6 additions & 7 deletions SConstruct
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,12 @@ for x in module_list:
module_enabled = True
tmppath = "./modules/" + x
sys.path.append(tmppath)
try:
import config
if (not config.is_enabled()):
module_enabled = False
except:
pass
import config
enabled_attr = getattr(config, "is_enabled", None)
if (callable(enabled_attr) and not config.is_enabled()):
module_enabled = False
sys.path.remove(tmppath)
sys.modules.pop('config')
opts.Add(BoolVariable('module_' + x + '_enabled', "Enable module '%s'" % (x, ), module_enabled))

opts.Update(env_base) # update environment
Expand Down Expand Up @@ -246,7 +245,7 @@ if selected_platform in platform_list:
env = detect.create(env_base)
else:
env = env_base.Clone()

if env['dev']:
env["warnings"] = "all"
env['verbose'] = True
Expand Down
120 changes: 120 additions & 0 deletions modules/mono/SCsub
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
#!/usr/bin/env python

Import('env')


def make_cs_files_header(src, dst):
with open(dst, 'wb') as header:
header.write('/* This is an automatically generated file; DO NOT EDIT! OK THX */\n')
header.write('#ifndef _CS_FILES_DATA_H\n')
header.write('#define _CS_FILES_DATA_H\n\n')
header.write('#include "map.h"\n')
header.write('#include "ustring.h"\n')
inserted_files = ''
import os
for file in os.listdir(src):
if file.endswith('.cs'):
with open(os.path.join(src, file), 'rb') as f:
buf = f.read()
decomp_size = len(buf)
import zlib
buf = zlib.compress(buf)
name = os.path.splitext(file)[0]
header.write('\nstatic const int _cs_' + name + '_compressed_size = ' + str(len(buf)) + ';\n')
header.write('static const int _cs_' + name + '_uncompressed_size = ' + str(decomp_size) + ';\n')
header.write('static const unsigned char _cs_' + name + '_compressed[] = { ')
for i, buf_idx in enumerate(range(len(buf))):
if i > 0:
header.write(', ')
header.write(str(ord(buf[buf_idx])))
inserted_files += '\tr_files.insert(\"' + file + '\", ' \
'CompressedFile(_cs_' + name + '_compressed_size, ' \
'_cs_' + name + '_uncompressed_size, ' \
'_cs_' + name + '_compressed));\n'
header.write(' };\n')
header.write('\nstruct CompressedFile\n' '{\n'
'\tint compressed_size;\n' '\tint uncompressed_size;\n' '\tconst unsigned char* data;\n'
'\n\tCompressedFile(int p_comp_size, int p_uncomp_size, const unsigned char* p_data)\n'
'\t{\n' '\t\tcompressed_size = p_comp_size;\n' '\t\tuncompressed_size = p_uncomp_size;\n'
'\t\tdata = p_data;\n' '\t}\n' '\n\tCompressedFile() {}\n' '};\n'
'\nvoid get_compressed_files(Map<String, CompressedFile>& r_files)\n' '{\n' + inserted_files + '}\n'
)
header.write('#endif // _CS_FILES_DATA_H')


env.add_source_files(env.modules_sources, '*.cpp')
env.add_source_files(env.modules_sources, 'mono_gd/*.cpp')
env.add_source_files(env.modules_sources, 'utils/*.cpp')

if env['tools']:
env.add_source_files(env.modules_sources, 'editor/*.cpp')
make_cs_files_header('glue/cs_files', 'glue/cs_compressed.gen.h')

vars = Variables()
vars.Add(BoolVariable('mono_glue', 'Build with the mono glue sources', True))
vars.Update(env)

# Glue sources
if env['mono_glue']:
env.add_source_files(env.modules_sources, 'glue/*.cpp')
else:
env.Append(CPPDEFINES = [ 'MONO_GLUE_DISABLED' ])

if ARGUMENTS.get('yolo_copy', False):
env.Append(CPPDEFINES = [ 'YOLO_COPY' ])

# Build GodotSharpTools solution

import os
import subprocess
import mono_reg_utils as monoreg


def mono_build_solution(source, target, env):
if os.name == 'nt':
msbuild_tools_path = monoreg.find_msbuild_tools_path_reg()
if not msbuild_tools_path:
raise RuntimeError('Cannot find MSBuild Tools Path in the registry')
msbuild_path = os.path.join(msbuild_tools_path, 'MSBuild.exe')
else:
msbuild_path = 'msbuild'

output_path = os.path.abspath(os.path.join(str(target[0]), os.pardir))

msbuild_args = [
msbuild_path,
os.path.abspath(str(source[0])),
'/p:Configuration=Release',
'/p:OutputPath=' + output_path
]

msbuild_env = os.environ.copy()

# Needed when running from Developer Command Prompt for VS
if 'PLATFORM' in msbuild_env:
del msbuild_env['PLATFORM']

msbuild_alt_paths = [ 'xbuild' ]

while True:
try:
subprocess.check_call(msbuild_args, env = msbuild_env)
break
except subprocess.CalledProcessError:
raise RuntimeError('GodotSharpTools build failed')
except OSError:
if os.name != 'nt':
if not msbuild_alt_paths:
raise RuntimeError('Could not find commands msbuild or xbuild')
# Try xbuild
msbuild_args[0] = msbuild_alt_paths.pop(0)
else:
raise RuntimeError('Could not find command MSBuild.exe')


mono_sln_builder = Builder(action = mono_build_solution)
env.Append(BUILDERS = { 'MonoBuildSolution' : mono_sln_builder })
env.MonoBuildSolution(
os.path.join(Dir('#bin').abspath, 'GodotSharpTools.dll'),
'editor/GodotSharpTools/GodotSharpTools.sln'
)
143 changes: 143 additions & 0 deletions modules/mono/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

import imp
import os
import sys
from shutil import copyfile

from SCons.Script import BoolVariable, Environment, Variables


monoreg = imp.load_source('mono_reg_utils', 'modules/mono/mono_reg_utils.py')


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 None


def can_build(platform):
if platform in ["javascript"]:
return False # Not yet supported
return True


def is_enabled():
# The module is disabled by default. Use module_mono_enabled=yes to enable it.
return False


def configure(env):
env.use_ptrcall = True

envvars = Variables()
envvars.Add(BoolVariable('mono_static', 'Statically link mono', False))
envvars.Update(env)

mono_static = env['mono_static']

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

if env['platform'] == 'windows':
if mono_static:
raise RuntimeError('mono-static: Not supported on Windows')

if env['bits'] == '32':
if os.getenv('MONO32_PREFIX'):
mono_root = os.getenv('MONO32_PREFIX')
elif os.name == 'nt':
mono_root = monoreg.find_mono_root_dir()
else:
if os.getenv('MONO64_PREFIX'):
mono_root = os.getenv('MONO64_PREFIX')
elif os.name == 'nt':
mono_root = monoreg.find_mono_root_dir()

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

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

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

mono_lib_name = find_file_in_dir(mono_lib_path, mono_lib_names, extension='.lib')

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

if os.getenv('VCINSTALLDIR'):
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')

mono_dll_src = os.path.join(mono_bin_path, mono_dll_name + '.dll')
mono_dll_dst = os.path.join('bin', mono_dll_name + '.dll')
copy_mono_dll = True

if not os.path.isdir('bin'):
os.mkdir('bin')
elif os.path.exists(mono_dll_dst):
copy_mono_dll = False

if copy_mono_dll:
copyfile(mono_dll_src, mono_dll_dst)
else:
mono_root = None

if env['bits'] == '32':
if os.getenv('MONO32_PREFIX'):
mono_root = os.getenv('MONO32_PREFIX')
else:
if os.getenv('MONO64_PREFIX'):
mono_root = os.getenv('MONO64_PREFIX')

if mono_root is not None:
mono_lib_path = os.path.join(mono_root, 'lib')

env.Append(LIBPATH=mono_lib_path)
env.Append(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 mono_lib is None:
raise RuntimeError('Could not find mono library in: ' + mono_lib_path)

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

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

if sys.platform == "darwin":
env.Append(LINKFLAGS=['-Wl,-force_load,' + mono_lib_file])
elif sys.platform == "linux" or sys.platform == "linux2":
env.Append(LINKFLAGS=['-Wl,-whole-archive', mono_lib_file, '-Wl,-no-whole-archive'])
else:
raise RuntimeError('mono-static: Not supported on this platform')
else:
env.Append(LIBS=[mono_lib])

env.Append(LIBS=['m', 'rt', 'dl', 'pthread'])
else:
if mono_static:
raise RuntimeError('mono-static: Not supported with pkg-config. Specify a mono prefix manually')

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

env.Append(LINKFLAGS='-rdynamic')


def get_doc_classes():
return ["@C#", "CSharpScript", "GodotSharp"]


def get_doc_path():
return "doc_classes"
Loading

0 comments on commit b0194a3

Please sign in to comment.