summaryrefslogtreecommitdiff
path: root/modules/mono/build_scripts/godotsharptools_build.py
blob: 17f9a990aff637e11c29195c2c1cca0db3379a3d (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
# Build GodotSharpTools solution

import os

from SCons.Script import Builder, Dir


def find_nuget_unix():
    import os

    if 'NUGET_PATH' in os.environ:
        hint_path = os.environ['NUGET_PATH']
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path
        hint_path = os.path.join(hint_path, 'nuget')
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path

    import os.path
    import sys

    hint_dirs = ['/opt/novell/mono/bin']
    if sys.platform == 'darwin':
        hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs

    for hint_dir in hint_dirs:
        hint_path = os.path.join(hint_dir, 'nuget')
        if os.path.isfile(hint_path):
            return hint_path
        elif os.path.isfile(hint_path + '.exe'):
            return hint_path + '.exe'

    for hint_dir in os.environ['PATH'].split(os.pathsep):
        hint_dir = hint_dir.strip('"')
        hint_path = os.path.join(hint_dir, 'nuget')
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path
        if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
            return hint_path + '.exe'

    return None


def find_nuget_windows(env):
    import os

    if 'NUGET_PATH' in os.environ:
        hint_path = os.environ['NUGET_PATH']
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path
        hint_path = os.path.join(hint_path, 'nuget.exe')
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path

    from . mono_reg_utils import find_mono_root_dir

    mono_root = env['mono_prefix'] or find_mono_root_dir(env['bits'])

    if mono_root:
        mono_bin_dir = os.path.join(mono_root, 'bin')
        nuget_mono = os.path.join(mono_bin_dir, 'nuget.bat')

        if os.path.isfile(nuget_mono):
            return nuget_mono

    # Standalone NuGet

    for hint_dir in os.environ['PATH'].split(os.pathsep):
        hint_dir = hint_dir.strip('"')
        hint_path = os.path.join(hint_dir, 'nuget.exe')
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path

    return None


def find_msbuild_unix(filename):
    import os.path
    import sys

    hint_dirs = ['/opt/novell/mono/bin']
    if sys.platform == 'darwin':
        hint_dirs = ['/Library/Frameworks/Mono.framework/Versions/Current/bin', '/usr/local/var/homebrew/linked/mono/bin'] + hint_dirs

    for hint_dir in hint_dirs:
        hint_path = os.path.join(hint_dir, filename)
        if os.path.isfile(hint_path):
            return hint_path
        elif os.path.isfile(hint_path + '.exe'):
            return hint_path + '.exe'

    for hint_dir in os.environ['PATH'].split(os.pathsep):
        hint_dir = hint_dir.strip('"')
        hint_path = os.path.join(hint_dir, filename)
        if os.path.isfile(hint_path) and os.access(hint_path, os.X_OK):
            return hint_path
        if os.path.isfile(hint_path + '.exe') and os.access(hint_path + '.exe', os.X_OK):
            return hint_path + '.exe'

    return None


def find_msbuild_windows(env):
    from . mono_reg_utils import find_mono_root_dir, find_msbuild_tools_path_reg

    mono_root = env['mono_prefix'] or find_mono_root_dir(env['bits'])

    if not mono_root:
        raise RuntimeError('Cannot find mono root directory')

    framework_path = os.path.join(mono_root, 'lib', 'mono', '4.5')
    mono_bin_dir = os.path.join(mono_root, 'bin')
    msbuild_mono = os.path.join(mono_bin_dir, 'msbuild.bat')

    if os.path.isfile(msbuild_mono):
        # The (Csc/Vbc/Fsc)ToolExe environment variables are required when
        # building with Mono's MSBuild. They must point to the batch files
        # in Mono's bin directory to make sure they are executed with Mono.
        mono_msbuild_env = {
            'CscToolExe': os.path.join(mono_bin_dir, 'csc.bat'),
            'VbcToolExe': os.path.join(mono_bin_dir, 'vbc.bat'),
            'FscToolExe': os.path.join(mono_bin_dir, 'fsharpc.bat')
        }
        return (msbuild_mono, framework_path, mono_msbuild_env)

    msbuild_tools_path = find_msbuild_tools_path_reg()

    if msbuild_tools_path:
        return (os.path.join(msbuild_tools_path, 'MSBuild.exe'), framework_path, {})

    return None


def mono_build_solution(source, target, env):
    import subprocess
    from shutil import copyfile

    sln_path = os.path.abspath(str(source[0]))
    target_path = os.path.abspath(str(target[0]))

    framework_path = ''
    msbuild_env = os.environ.copy()

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

    # Find MSBuild
    if os.name == 'nt':
        msbuild_info = find_msbuild_windows(env)
        if msbuild_info is None:
            raise RuntimeError('Cannot find MSBuild executable')
        msbuild_path = msbuild_info[0]
        framework_path = msbuild_info[1]
        msbuild_env.update(msbuild_info[2])
    else:
        msbuild_path = find_msbuild_unix('msbuild')
        if msbuild_path is None:
            xbuild_fallback = env['xbuild_fallback']

            if xbuild_fallback and os.name == 'nt':
                print('Option \'xbuild_fallback\' not supported on Windows')
                xbuild_fallback = False

            if xbuild_fallback:
                print('Cannot find MSBuild executable, trying with xbuild')
                print('Warning: xbuild is deprecated')

                msbuild_path = find_msbuild_unix('xbuild')

                if msbuild_path is None:
                    raise RuntimeError('Cannot find xbuild executable')
            else:
                raise RuntimeError('Cannot find MSBuild executable')

    print('MSBuild path: ' + msbuild_path)

    # Find NuGet
    nuget_path = find_nuget_windows(env) if os.name == 'nt' else find_nuget_unix()
    if nuget_path is None:
        raise RuntimeError('Cannot find NuGet executable')

    print('NuGet path: ' + nuget_path)

    # Do NuGet restore

    try:
        subprocess.check_call([nuget_path, 'restore', sln_path])
    except subprocess.CalledProcessError:
        raise RuntimeError('GodotSharpTools: NuGet restore failed')

    # Build solution

    build_config = 'Release'

    msbuild_args = [
        msbuild_path,
        sln_path,
        '/p:Configuration=' + build_config,
    ]

    if framework_path:
        msbuild_args += ['/p:FrameworkPathOverride=' + framework_path]

    try:
        subprocess.check_call(msbuild_args, env=msbuild_env)
    except subprocess.CalledProcessError:
        raise RuntimeError('GodotSharpTools: Build failed')

    # Copy files

    src_dir = os.path.abspath(os.path.join(sln_path, os.pardir, 'bin', build_config))
    dst_dir = os.path.abspath(os.path.join(target_path, os.pardir))
    asm_file = 'GodotSharpTools.dll'

    if not os.path.isdir(dst_dir):
        if os.path.exists(dst_dir):
            raise RuntimeError('Target directory is a file')
        os.makedirs(dst_dir)

    copyfile(os.path.join(src_dir, asm_file), os.path.join(dst_dir, asm_file))

    # Dependencies
    copyfile(os.path.join(src_dir, "DotNet.Glob.dll"), os.path.join(dst_dir, "DotNet.Glob.dll"))

def build(env_mono):
    if not env_mono['tools']:
        return

    output_dir = Dir('#bin').abspath
    editor_tools_dir = os.path.join(output_dir, 'GodotSharp', 'Tools')

    mono_sln_builder = Builder(action=mono_build_solution)
    env_mono.Append(BUILDERS={'MonoBuildSolution': mono_sln_builder})
    env_mono.MonoBuildSolution(
        os.path.join(editor_tools_dir, 'GodotSharpTools.dll'),
        'editor/GodotSharpTools/GodotSharpTools.sln'
    )