generated from nosoop/NinjaBuild-SMPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.py
145 lines (115 loc) · 4.73 KB
/
configure.py
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
#!/usr/bin/python
# plugin names, relative to `scripting/`
plugins = [
"tf2_sentryfirebullet.sp",
]
# these are class "definitions" that are converted to methodmaps
# see the "Build methodmap classes from templates" comment in the build.ninja configuration
generated_classes = [
"classdefs/firebulletsinfo_t.toml",
]
# files to copy to builddir, relative to root
# plugin names from previous list will be copied automatically
copy_files = [
"gamedata/FireBullets.txt",
"scripting/include/tf2_sentryfirebullet.inc",
]
# additional directories for sourcepawn include lookup
include_dirs = [
'${root}/third_party/submodules',
'${root}/third_party/vendored',
'${builddir}/generated',
]
# required version of spcomp (presumably pinned to SM version)
spcomp_min_version = (1, 11)
########################
# build.ninja script generation below.
import contextlib
import misc.ninja_syntax as ninja_syntax
import misc.spcomp_util
import os
import sys
import argparse
import platform
import shutil
parser = argparse.ArgumentParser('Configures the project.')
parser.add_argument('--spcomp-dir',
help = 'Directory with the SourcePawn compiler. Will check PATH if not specified.')
args = parser.parse_args()
print("""Checking for SourcePawn compiler...""")
spcomp = shutil.which('spcomp', path = args.spcomp_dir)
if 'x86_64' in platform.machine():
# Use 64-bit spcomp if architecture supports it
spcomp = shutil.which('spcomp64', path = args.spcomp_dir) or spcomp
if not spcomp:
raise FileNotFoundError('Could not find SourcePawn compiler.')
available_version = misc.spcomp_util.extract_version(spcomp)
version_string = '.'.join(map(str, available_version))
print('Found SourcePawn compiler version', version_string, 'at', os.path.abspath(spcomp))
if spcomp_min_version > available_version:
raise ValueError("Failed to meet required compiler version "
+ '.'.join(map(str, spcomp_min_version)))
with contextlib.closing(ninja_syntax.Writer(open('build.ninja', 'wt'))) as build:
build.comment('This file is used to build SourceMod plugins with ninja.')
build.comment('The file is automatically generated by configure.py')
build.newline()
vars = {
'configure_args': sys.argv[1:],
'root': '.',
'builddir': 'build',
'spcomp': spcomp,
'spcflags': [ '-h', '-v0' ]
}
vars['spcflags'] += ('-i{}'.format(d) for d in include_dirs)
for key, value in vars.items():
build.variable(key, value)
build.newline()
build.comment("""Regenerate build files if build script changes.""")
build.rule('configure',
command = sys.executable + ' ${root}/configure.py ${configure_args}',
description = 'Reconfiguring build', generator = 1)
build.build('build.ninja', 'configure',
implicit = [ '${root}/configure.py', '${root}/misc/ninja_syntax.py' ])
build.newline()
build.rule('spcomp', deps = 'msvc',
command = '${spcomp} ${in} ${spcflags} -o ${out}',
description = 'Compiling ${out}')
build.newline()
build.rule('genclass',
command = sys.executable + ' ${root}/misc/generate_classes.py ${template} ${in} ${out}',
description = 'Generating ${out}')
build.newline()
# Platform-specific copy instructions
if platform.system() == "Windows":
build.rule('copy', command = 'cmd /c copy ${in} ${out} > NUL',
description = 'Copying ${out}')
elif platform.system() == "Linux":
build.rule('copy', command = 'cp ${in} ${out}', description = 'Copying ${out}')
build.newline()
build.comment("""Build methodmap classes from templates""")
class_sources = []
for classdef in generated_classes:
template_file = "${root}/scripting/templates/class.ms.sp"
sp_name = os.path.splitext(classdef)[0] + '.sp'
sp_file = os.path.normpath(os.path.join('$builddir', 'generated', sp_name))
class_sources.extend(build.build(sp_file, 'genclass', classdef,
implicit = [ template_file, '${root}/misc/generate_classes.py' ],
variables = { 'template': template_file }))
build.newline()
build.comment("""Compile plugins specified in `plugins` list""")
for plugin in plugins:
smx_plugin = os.path.splitext(plugin)[0] + '.smx'
sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))
smx_file = os.path.normpath(os.path.join('$builddir', 'plugins', smx_plugin))
build.build(smx_file, 'spcomp', sp_file, order_only = class_sources)
build.newline()
build.comment("""Copy plugin sources to build output""")
for plugin in plugins:
sp_file = os.path.normpath(os.path.join('$root', 'scripting', plugin))
dist_sp = os.path.normpath(os.path.join('$builddir', 'scripting', plugin))
build.build(dist_sp, 'copy', sp_file)
build.newline()
build.comment("""Copy other files from source tree""")
for filepath in copy_files:
build.build(os.path.normpath(os.path.join('$builddir', filepath)), 'copy',
os.path.normpath(os.path.join('$root', filepath)))