2014-08-27 07:57:37 +02:00
|
|
|
#!/usr/bin/env python
|
2018-06-12 11:43:13 +02:00
|
|
|
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
from distutils.core import setup, Extension
|
2016-07-28 01:19:24 +02:00
|
|
|
from distutils.sysconfig import get_config_vars
|
2014-08-27 07:57:37 +02:00
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
import sys
|
|
|
|
import shutil
|
|
|
|
import multiprocessing
|
2017-01-20 16:07:09 +01:00
|
|
|
|
2014-08-27 07:57:37 +02:00
|
|
|
|
2016-02-16 00:16:09 +01:00
|
|
|
class flags_parser:
|
2017-01-20 16:07:09 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.include_dirs = []
|
|
|
|
self.library_dirs = []
|
|
|
|
self.libraries = []
|
|
|
|
|
|
|
|
def parse(self, args):
|
|
|
|
"""Parse out the -I -L -l directives
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
list: All other arguments
|
|
|
|
"""
|
|
|
|
ret = []
|
|
|
|
for token in args.split():
|
|
|
|
prefix = token[:2]
|
|
|
|
if prefix == '-I':
|
|
|
|
self.include_dirs.append(token[2:])
|
|
|
|
elif prefix == '-L':
|
|
|
|
self.library_dirs.append(token[2:])
|
|
|
|
elif prefix == '-l':
|
|
|
|
self.libraries.append(token[2:])
|
|
|
|
else:
|
|
|
|
ret.append(token)
|
|
|
|
return ret
|
|
|
|
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
def arch():
|
2017-01-20 16:07:09 +01:00
|
|
|
if platform.system() == 'Darwin':
|
|
|
|
__, __, machine = platform.mac_ver()
|
|
|
|
if machine.startswith('ppc'):
|
|
|
|
return ['-arch', machine]
|
|
|
|
return []
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
|
2017-01-20 16:07:09 +01:00
|
|
|
def target_specific():
|
|
|
|
if platform.system() == 'Darwin':
|
|
|
|
# On mavericks, clang will fail when unknown arguments are passed in.
|
|
|
|
# python distutils will pass in arguments it doesn't know about.
|
|
|
|
return ['-Wno-error=unused-command-line-argument-hard-error-in-future']
|
|
|
|
return []
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
2017-01-20 16:07:09 +01:00
|
|
|
with open('compile_flags') as _file:
|
|
|
|
extra_cmd = _file.read()
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2017-01-20 16:07:09 +01:00
|
|
|
extra_cmd = None
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
try:
|
2017-01-20 16:07:09 +01:00
|
|
|
with open('link_flags') as _file:
|
|
|
|
ldflags = _file.read()
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2017-01-20 16:07:09 +01:00
|
|
|
ldflags = None
|
2014-08-27 07:57:37 +02:00
|
|
|
|
2017-07-30 18:36:27 +02:00
|
|
|
# this is to pull out compiler arguments from the CXX flags set up by the
|
|
|
|
# configure script. Specifically, the -std=c++11 flag is added to CXX and here
|
|
|
|
# we pull out everything starting from the first flag (i.e. something starting
|
|
|
|
# with a '-'). The actual command to call the compiler may be more than one
|
|
|
|
# word, for instance "ccache g++".
|
|
|
|
try:
|
|
|
|
with open('compile_cmd') as _file:
|
|
|
|
cmd = _file.read().split(' ')
|
|
|
|
while len(cmd) > 0 and not cmd[0].startswith('-'):
|
|
|
|
cmd = cmd[1:]
|
|
|
|
extra_cmd += ' '.join(cmd)
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2017-07-30 18:36:27 +02:00
|
|
|
pass
|
|
|
|
|
2014-08-27 07:57:37 +02:00
|
|
|
ext = None
|
|
|
|
packages = None
|
|
|
|
|
2015-08-27 12:52:13 +02:00
|
|
|
if '--bjam' in sys.argv:
|
2017-01-20 16:07:09 +01:00
|
|
|
del sys.argv[sys.argv.index('--bjam')]
|
|
|
|
|
2018-04-02 00:03:43 +02:00
|
|
|
if '--help' not in sys.argv \
|
2018-06-12 11:43:13 +02:00
|
|
|
and '--help-commands' not in sys.argv:
|
2018-04-02 00:03:43 +02:00
|
|
|
|
|
|
|
toolset = ''
|
|
|
|
file_ext = '.so'
|
|
|
|
|
2017-01-20 16:07:09 +01:00
|
|
|
if platform.system() == 'Windows':
|
|
|
|
file_ext = '.pyd'
|
2018-04-02 00:03:43 +02:00
|
|
|
# See https://wiki.python.org/moin/WindowsCompilers for a table of msvc versions
|
|
|
|
# used for each python version
|
|
|
|
# Specify the full version number for 9.0 and 10.0 because apparently
|
|
|
|
# older versions of boost don't support only specifying the major number and
|
|
|
|
# there was only one version of msvc with those majors.
|
|
|
|
# Only specify the major for msvc-14 so that 14.1, 14.11, etc can be used.
|
|
|
|
# Hopefully people building with msvc-14 are using a new enough version of boost
|
|
|
|
# for this to work.
|
|
|
|
if sys.version_info[0:2] in ((2, 6), (2, 7), (3, 0), (3, 1), (3, 2)):
|
|
|
|
toolset = ' toolset=msvc-9.0'
|
|
|
|
elif sys.version_info[0:2] in ((3, 3), (3, 4)):
|
|
|
|
toolset = ' toolset=msvc-10.0'
|
|
|
|
elif sys.version_info[0:2] in ((3, 5), (3, 6)):
|
|
|
|
toolset = ' toolset=msvc-14'
|
|
|
|
else:
|
|
|
|
# unknown python version, lets hope the user has the right version of msvc configured
|
|
|
|
toolset = ' toolset=msvc'
|
2017-01-20 16:07:09 +01:00
|
|
|
|
|
|
|
parallel_builds = ' -j%d' % multiprocessing.cpu_count()
|
2018-04-02 00:03:43 +02:00
|
|
|
if sys.maxsize > 2**32:
|
|
|
|
address_model = ' address-model=64'
|
|
|
|
else:
|
|
|
|
address_model = ' address-model=32'
|
|
|
|
|
|
|
|
# add extra quoting around the path to prevent bjam from parsing it as a list
|
|
|
|
# if the path has spaces
|
|
|
|
os.environ['LIBTORRENT_PYTHON_INTERPRETER'] = '"' + sys.executable + '"'
|
2017-01-20 16:07:09 +01:00
|
|
|
|
|
|
|
# build libtorrent using bjam and build the installer with distutils
|
|
|
|
cmdline = ('b2 libtorrent-link=static boost-link=static release '
|
|
|
|
'optimization=space stage_module --abbreviate-paths' +
|
2018-04-02 00:03:43 +02:00
|
|
|
address_model + toolset + parallel_builds)
|
2017-01-20 16:07:09 +01:00
|
|
|
print(cmdline)
|
|
|
|
if os.system(cmdline) != 0:
|
|
|
|
print('build failed')
|
|
|
|
sys.exit(1)
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
try:
|
|
|
|
os.mkdir('build')
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
shutil.rmtree('build/lib')
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.mkdir('build/lib')
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
pass
|
|
|
|
try:
|
|
|
|
os.mkdir('libtorrent')
|
2018-10-19 14:35:19 +02:00
|
|
|
except Exception:
|
2018-06-12 11:43:13 +02:00
|
|
|
pass
|
2017-01-20 16:07:09 +01:00
|
|
|
shutil.copyfile('libtorrent' + file_ext,
|
|
|
|
'build/lib/libtorrent' + file_ext)
|
|
|
|
|
|
|
|
packages = ['libtorrent']
|
2014-08-27 07:57:37 +02:00
|
|
|
|
|
|
|
else:
|
2017-01-20 16:07:09 +01:00
|
|
|
# Remove '-Wstrict-prototypes' compiler option, which isn't valid for C++.
|
|
|
|
cfg_vars = get_config_vars()
|
2018-06-12 11:43:13 +02:00
|
|
|
for key, value in list(cfg_vars.items()):
|
2017-01-20 16:07:09 +01:00
|
|
|
if isinstance(value, str):
|
|
|
|
cfg_vars[key] = value.replace('-Wstrict-prototypes', '')
|
|
|
|
|
2018-06-12 11:43:13 +02:00
|
|
|
src_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "src"))
|
|
|
|
source_list = [os.path.join(src_dir, s) for s in os.listdir(src_dir) if s.endswith(".cpp")]
|
2017-01-20 16:07:09 +01:00
|
|
|
|
2020-01-07 19:24:06 +01:00
|
|
|
flags = flags_parser()
|
|
|
|
ext_extra = {}
|
|
|
|
|
|
|
|
if ldflags:
|
2017-01-20 16:07:09 +01:00
|
|
|
# ldflags parsed first to ensure the correct library search path order
|
2020-01-07 19:24:06 +01:00
|
|
|
ext_extra["extra_link_args"] = flags.parse(ldflags) + arch()
|
|
|
|
|
|
|
|
if extra_cmd:
|
|
|
|
ext_extra["extra_compile_args"] = flags.parse(extra_cmd) + arch() + target_specific()
|
|
|
|
|
|
|
|
ext = [Extension(
|
|
|
|
'libtorrent',
|
|
|
|
sources=sorted(source_list),
|
|
|
|
language='c++',
|
|
|
|
include_dirs=flags.include_dirs,
|
|
|
|
library_dirs=flags.library_dirs,
|
|
|
|
libraries=['torrent-rasterbar'] + flags.libraries,
|
|
|
|
**ext_extra)
|
|
|
|
]
|
2017-01-20 16:07:09 +01:00
|
|
|
|
|
|
|
setup(
|
|
|
|
name='python-libtorrent',
|
2020-03-08 01:20:12 +01:00
|
|
|
version='1.2.5',
|
2017-01-20 16:07:09 +01:00
|
|
|
author='Arvid Norberg',
|
|
|
|
author_email='arvid@libtorrent.org',
|
|
|
|
description='Python bindings for libtorrent-rasterbar',
|
|
|
|
long_description='Python bindings for libtorrent-rasterbar',
|
|
|
|
url='http://libtorrent.org',
|
|
|
|
platforms=[platform.system() + '-' + platform.machine()],
|
|
|
|
license='BSD',
|
|
|
|
packages=packages,
|
|
|
|
ext_modules=ext
|
2014-08-27 07:57:37 +02:00
|
|
|
)
|