forked from HybridDog/onomatopoeia
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsetup.py
executable file
·202 lines (167 loc) · 6.91 KB
/
setup.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
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
#!/usr/bin/env python3
# Taken from Minecraft Overviewer
import sys
import traceback
# quick version check
if sys.version_info[0] == 2 or (sys.version_info[0] == 3 and sys.version_info[1] < 4):
print("Sorry, the Overviewer requires at least Python 3.4 to run.")
sys.exit(1)
from distutils.core import setup
from distutils.extension import Extension
from distutils.command.build import build
from distutils.command.clean import clean
from distutils.command.build_ext import build_ext
from distutils.command.sdist import sdist
from distutils.cmd import Command
from distutils.dir_util import remove_tree
from distutils.sysconfig import get_python_inc
from distutils import log
import os, os.path
import glob
import platform
import time
import numpy
# now, setup the keyword arguments for setup
# (because we don't know until runtime if py2exe/py2app is available)
setup_kwargs = {}
setup_kwargs['ext_modules'] = []
setup_kwargs['cmdclass'] = {}
setup_kwargs['options'] = {}
#
# metadata
#
setup_kwargs['name'] = 'Onomatopoeia'
setup_kwargs['version'] = '1'
setup_kwargs['description'] = 'Minetest mapper'
setup_kwargs['url'] = ''
setup_kwargs['author'] = ''
setup_kwargs['author_email'] = ''
setup_kwargs['license'] = ''
setup_kwargs['long_description'] = ''
#
# c_overviewer extension
#
# Third-party modules - we depend on numpy for everything
# Obtain the numpy include directory. This logic works across numpy versions.
try:
numpy_include = numpy.get_include()
except AttributeError:
numpy_include = numpy.get_numpy_include()
try:
pil_include = os.environ['PIL_INCLUDE_DIR'].split(os.pathsep)
except Exception:
pil_include = [ os.path.join(get_python_inc(plat_specific=1), 'Imaging') ]
if not os.path.exists(pil_include[0]):
pil_include = [ ]
# used to figure out what files to compile
# auto-created from files in primitives/, but we need the raw names so
# we can use them later.
primitives = []
for name in glob.glob("src/primitives/*.c"):
name = os.path.split(name)[-1]
name = os.path.splitext(name)[0]
primitives.append(name)
c_overviewer_files = ['main.c', 'composite.c', 'iterate.c', 'endian.c', 'rendermodes.c', 'block_class.c']
c_overviewer_files += ['primitives/%s.c' % (mode) for mode in primitives]
c_overviewer_files += ['Draw.c']
c_overviewer_includes = ['overviewer.h', 'rendermodes.h']
c_overviewer_files = ['src/' + s for s in c_overviewer_files]
c_overviewer_includes = ['src/' + s for s in c_overviewer_includes]
setup_kwargs['ext_modules'].append(Extension('onomatopoeia.c_overviewer', c_overviewer_files, include_dirs=['.', numpy_include] + pil_include, depends=c_overviewer_includes, extra_link_args=[]))
# tell build_ext to build the extension in-place
# (NOT in build/)
setup_kwargs['options']['build_ext'] = {'inplace' : 1}
# custom clean command to remove in-place extension
# and the version file, primitives header
class CustomClean(clean):
def run(self):
# do the normal cleanup
clean.run(self)
# try to remove '_composite.{so,pyd,...}' extension,
# regardless of the current system's extension name convention
build_ext = self.get_finalized_command('build_ext')
ext_fname = build_ext.get_ext_filename('onomatopoeia.c_overviewer')
versionpath = os.path.join("onomatopoeia", "overviewer_version.py")
primspath = os.path.join("onomatopoeia", "src", "primitives.h")
for fname in [ext_fname, primspath]:
if os.path.exists(fname):
try:
log.info("removing '%s'", fname)
if not self.dry_run:
os.remove(fname)
except OSError:
log.warning("'%s' could not be cleaned -- permission denied",
fname)
else:
log.debug("'%s' does not exist -- can't clean it",
fname)
# now try to purge all *.pyc files
for root, dirs, files in os.walk(os.path.join(os.path.dirname(__file__), ".")):
for f in files:
if f.endswith(".pyc"):
if self.dry_run:
log.warning("Would remove %s", os.path.join(root,f))
else:
os.remove(os.path.join(root, f))
def generate_primitives_h():
global primitives
prims = [p.lower().replace('-', '_') for p in primitives]
outstr = "/* this file is auto-generated by setup.py */\n"
for p in prims:
outstr += "extern RenderPrimitiveInterface primitive_{0};\n".format(p)
outstr += "static RenderPrimitiveInterface *render_primitives[] = {\n"
for p in prims:
outstr += " &primitive_{0},\n".format(p)
outstr += " NULL\n"
outstr += "};\n"
with open("src/primitives.h", "w") as f:
f.write(outstr)
class CustomSDist(sdist):
def run(self):
# generate the version file
generate_primitives_h()
sdist.run(self)
class CustomBuild(build):
def run(self):
# generate the version file
try:
generate_primitives_h()
build.run(self)
print("\nBuild Complete")
except Exception:
traceback.print_exc(limit=1)
print("\nFailed to build Overviewer!")
print("Please review the errors printed above and the build instructions")
print("at <http://docs.overviewer.org/en/latest/building/>. If you are")
print("still having build problems, file an incident on the github tracker")
print("or find us in IRC.")
sys.exit(1)
class CustomBuildExt(build_ext):
def build_extensions(self):
c = self.compiler.compiler_type
if c == "msvc":
# customize the build options for this compilier
for e in self.extensions:
e.extra_link_args.append("/MANIFEST")
e.extra_link_args.append("/DWINVER=0x060")
e.extra_link_args.append("/D_WIN32_WINNT=0x060")
if c == "unix":
# customize the build options for this compilier
for e in self.extensions:
e.extra_compile_args.append("-Wno-unused-variable") # quell some annoying warnings
e.extra_compile_args.append("-Wno-unused-function") # quell some annoying warnings
e.extra_compile_args.append("-Wdeclaration-after-statement")
e.extra_compile_args.append("-Werror=declaration-after-statement")
e.extra_compile_args.append("-O3")
e.extra_compile_args.append("-std=gnu99")
# build in place, and in the build/ tree
self.inplace = False
build_ext.build_extensions(self)
self.inplace = True
build_ext.build_extensions(self)
setup_kwargs['cmdclass']['clean'] = CustomClean
setup_kwargs['cmdclass']['sdist'] = CustomSDist
setup_kwargs['cmdclass']['build'] = CustomBuild
setup_kwargs['cmdclass']['build_ext'] = CustomBuildExt
###
setup(**setup_kwargs)