From e7e5805c56873ddfdcc09c407251185234caafbc Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Sun, 25 Mar 2018 13:31:26 -0700 Subject: [PATCH] support building python bindings for python 3 and 64 bit --- bindings/python/Jamfile | 37 +++++++++++++++++++++++++++++++++++-- bindings/python/setup.py | 29 +++++++++++++++++++++++++---- setup.py | 2 +- 3 files changed, 61 insertions(+), 7 deletions(-) mode change 100755 => 100644 setup.py diff --git a/bindings/python/Jamfile b/bindings/python/Jamfile index 64b6d246b..9539eb5cf 100644 --- a/bindings/python/Jamfile +++ b/bindings/python/Jamfile @@ -8,6 +8,8 @@ import modules ; use-project /torrent : ../.. ; BOOST_ROOT = [ modules.peek : BOOST_ROOT ] ; +# this is used to make bjam use the same version of python which is executing setup.py +LIBTORRENT_PYTHON_INTERPRETER = [ modules.peek : LIBTORRENT_PYTHON_INTERPRETER ] ; feature visibility : default hidden : composite ; feature.compose hidden : -fvisibility=hidden -fvisibility-inlines-hidden ; @@ -16,10 +18,20 @@ feature libtorrent-link : shared static : composite propagated ; feature libtorrent-python-pic : off on : composite propagated link-incompatible ; feature.compose on : -fPIC ; +# this is just to force boost build to pick the desired python target when using LIBTORRENT_PYTHON_INTERPRETER +feature libtorrent-python : on ; + +if $(LIBTORRENT_PYTHON_INTERPRETER) +{ + echo "using python interpreter at: " $(LIBTORRENT_PYTHON_INTERPRETER) ; + using python : : "$(LIBTORRENT_PYTHON_INTERPRETER)" : : : on ; +} + if $(BOOST_ROOT) { use-project /boost : $(BOOST_ROOT) ; alias boost_python : /boost/python//boost_python : : : $(BOOST_ROOT) ; + alias boost_python3 : /boost/python//boost_python3 : : : $(BOOST_ROOT) ; } else { @@ -40,9 +52,13 @@ else # the names are decorated in MacPorts lib boost_python : : darwin boost_python-mt : : $(boost-include-path) ; + lib boost_python3 : : darwin boost_python3-mt + : : $(boost-include-path) ; lib boost_python : : boost_python : : $(boost-include-path) ; + lib boost_python3 : : boost_python3 + : : $(boost-include-path) ; } @@ -79,16 +95,33 @@ rule libtorrent_linking ( properties * ) ECHO "WARNING: you cannot link statically against boost-python on linux, because it links against pthread statically in that case, which is not allowed" ; } + local boost_python_lib ; + + for local prop in $(properties) + { + switch $(prop) + { + case 2.* : boost_python_lib = boost_python ; + case 3.* : boost_python_lib = boost_python3 ; + } + } + + if ! $(boost_python_lib) + { + ECHO "WARNING: unknown python version" ; + boost_python_lib = boost_python ; + } + # linux must link dynamically against boost python because it pulls # in libpthread, which must be linked dynamically since we're building a .so # (the static build of libpthread is not position independent) if shared in $(properties) || linux in $(properties) { - result += boost_python/shared ; + result += $(boost_python_lib)/shared ; } else { - result += boost_python/static ; + result += $(boost_python_lib)/static ; } if shared in $(properties) diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 482776ecf..3ea9c2c2d 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -74,15 +74,36 @@ if '--bjam' in sys.argv: file_ext = '.so' if platform.system() == 'Windows': - # msvc 9.0 (2008) is the official windows compiler for python 2.6 - # http://docs.python.org/whatsnew/2.6.html#build-and-c-api-changes - toolset = ' msvc-9.0' file_ext = '.pyd' + # 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' parallel_builds = ' -j%d' % multiprocessing.cpu_count() + 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 + '"' # 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' + toolset + parallel_builds + cmdline = 'b2 libtorrent-link=static boost-link=static release optimization=space stage_module --abbreviate-paths' + address_model + toolset + parallel_builds print(cmdline) if os.system(cmdline) != 0: print('build failed') diff --git a/setup.py b/setup.py old mode 100755 new mode 100644 index 06f12e01a..28f3076b4 --- a/setup.py +++ b/setup.py @@ -2,4 +2,4 @@ import os os.chdir('bindings/python') -execfile('setup.py') +exec(open('setup.py').read())