-
Notifications
You must be signed in to change notification settings - Fork 29
/
setup.py
executable file
·205 lines (170 loc) · 6.63 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
202
203
204
205
#!/usr/bin/env python
from __future__ import print_function
import sys
import os
import stat
from contextlib import contextmanager
from subprocess import check_call
import zipfile
import multiprocessing
try:
# python 3
from urllib.request import urlretrieve
except ImportError:
from urllib import urlretrieve
from setuptools import setup, find_packages, Extension, Command
from distutils.command.build_ext import build_ext as distutils_build_ext
MODE = 'native'
os.chdir(os.path.abspath(os.path.dirname(__file__)))
sources = list(map(lambda path: os.path.join('v8py', path),
filter(lambda path: path.endswith('.cpp'),
os.listdir('v8py'))))
v8_libraries = ['v8_libplatform', 'v8_base', 'v8_snapshot', 'v8_libbase', 'v8_libsampler']
libraries = list(v8_libraries)
if os.name == 'nt':
library_dirs = ['v8/out.gn/x64.release/obj', 'v8/out.gn/x64.release/obj/src/inspector']
include_dirs = ['v8py', 'v8/include']
extra_compile_args = ['/MT']
extra_link_args = []
libraries.extend(['Dbghelp', 'Shlwapi', 'Winmm', 'inspector'])
else:
library_dirs = ['v8/out/{}'.format(MODE),
'v8/out/{}/obj.target/src'.format(MODE)]
include_dirs = ['v8py', 'v8/include']
extra_compile_args = ['-std=c++11']
extra_link_args = []
if sys.platform.startswith('linux'):
libraries.append('rt')
if sys.platform.startswith('darwin'):
extra_compile_args.append('-stdlib=libc++')
extension = Extension('_v8py',
sources=sources,
include_dirs=include_dirs,
library_dirs=library_dirs,
libraries=libraries,
extra_compile_args=extra_compile_args,
extra_link_args=extra_link_args,
language='c++')
@contextmanager
def cd(path):
old_cwd = os.getcwd()
try:
yield os.chdir(path)
finally:
os.chdir(old_cwd)
DEPOT_TOOLS_PATH = os.path.join(os.getcwd(), 'depot_tools')
COMMAND_ENV = os.environ.copy()
COMMAND_ENV['PATH'] = DEPOT_TOOLS_PATH + os.path.pathsep + os.environ['PATH']
if os.name == 'nt':
COMMAND_ENV['GYP_MSVS_VERSION'] = '2015'
COMMAND_ENV['GYP_MSVS_OVERRIDE_PATH'] = 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0'
COMMAND_ENV['GYP_CHROMIUM_NO_ACTION'] = '0'
COMMAND_ENV['VS140COMNTOOLS'] = 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\Common7\\Tools\\'
COMMAND_ENV['DEPOT_TOOLS_WIN_TOOLCHAIN'] = '0'
COMMAND_ENV.pop('CC', None)
COMMAND_ENV.pop('CXX', None)
def run(command):
print(command)
check_call(command, shell=True, env=COMMAND_ENV)
def v8_exists():
def library_exists(library):
if library == 'rt':
return True
lib_filename = 'lib{}.a'.format(library)
for lib_dir in library_dirs:
lib_path = os.path.join(lib_dir, lib_filename)
if os.path.isfile(lib_path):
return True
print(lib_filename, 'not found')
return False
return all(library_exists(lib) for lib in libraries)
def get_ninja():
if os.path.isdir("ninja"):
return
print("Downloading ninja")
urlretrieve("https://github.com/ninja-build/ninja/releases/download/v1.8.2/ninja-win.zip", "ninja-win.zip")
print("Extranting ninja")
zip_ref = zipfile.ZipFile("ninja-win.zip", 'r')
zip_ref.extractall("ninja")
zip_ref.close()
def get_v8():
if not os.path.isdir('depot_tools'):
print('installing depot tools')
run('git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git')
with cd('depot_tools'):
with open('python', 'w') as python:
print('#!/bin/sh', file=python)
print('python2 "$@"', file=python)
# Octal literals don't have the same syntax on python 2 and 3, so I use a decimal literal
os.chmod('python', os.stat('python').st_mode | 73)
else:
print('updating depot tools')
with cd('depot_tools'):
run('./update_depot_tools')
if not os.path.isdir('v8/.git'):
print('downloading v8')
run('fetch --force v8')
else:
print('updating v8')
with cd('v8'):
run('gclient fetch')
with cd('v8'):
run('git checkout -f {}'.format('branch-heads/5.9'))
run('gclient sync')
class BuildV8Command(Command):
# currently no options
def initialize_options(self):
pass
def finalize_options(self):
pass
def run(self):
if not v8_exists():
get_v8()
with cd('v8'):
if os.name == 'nt':
get_ninja()
run('python tools/dev/v8gen.py x64.release -- v8_static_library=true is_debug=false '
'v8_use_external_startup_data=false '
'is_component_build=false v8_enable_i18n_support=false '
'v8_static_library=true')
# apparently building d8 is the only way to get v8_base.lib
run('ninja\\ninja -C out.gn/x64.release d8')
else:
gypflags = '-Dv8_use_external_startup_data=0 -Dv8_enable_i18n_support=0 -Dv8_enable_inspector=1 -Dwerror=\'\' '
run('make GYPFLAGS="{}" CFLAGS=-fPIC CXXFLAGS=-fPIC {} -j{}'.format(gypflags, MODE,
multiprocessing.cpu_count()))
class build_ext(distutils_build_ext):
def build_extension(self, ext):
self.run_command('build_v8')
distutils_build_ext.build_extension(self, ext)
with open('README.rst', 'r') as f:
long_description = f.read()
setup(
name='v8py',
version='0.9.15',
author='Theodore Dubois',
author_email='tblodt@icloud.com',
url='https://github.com/tbodt/v8py',
description='Write Python APIs, then call them from JavaScript using the V8 engine.',
long_description=long_description,
license='LGPLv3',
classifiers=[
'Development Status :: 3 - Alpha',
'License :: OSI Approved :: GNU Lesser General Public License v3 or later (LGPLv3+)',
'Topic :: Software Development :: Interpreters',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.5',
],
keywords=['v8', 'javascript'],
packages=find_packages(exclude=['tests']),
ext_modules=[extension],
extras_require={
'devtools': ['gevent', 'greenstack-greenlet', 'karellen-geventws'],
},
setup_requires=['pytest-runner'],
tests_require=['pytest'],
cmdclass={
'build_ext': build_ext,
'build_v8': BuildV8Command,
},
)