Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gubatron/aead9158cc9abab87171 to your computer and use it in GitHub Desktop.
Save gubatron/aead9158cc9abab87171 to your computer and use it in GitHub Desktop.
OpenBazaar: Building libtorrent and python-libtorrent on MacOSX.

OpenBazaar: Building libtorrent and python-libtorrent on MacOSX.

Nov. 15th 2014 - By Angel Leon. Tips are welcome.

Express route

  1. Install a binary boost distribution (it must contain the Boost.Python module) and make sure to setup the BOOST_ROOT environment variable.
  2. Download the libtorrent.tar.gz, and uncompress it.
  3. If you have the 1.0.2 version, you will have to go to bindings\python and remove the compile_flags and link_flags files which got packaged by mistake.
  4. Move the bindings\python\setup.py script to the root of the libtorrent sources folder.
  5. Apply the following patch:
+++ setup.py	2014-11-26 07:36:52.000000000 -0500
@@ -9,6 +9,8 @@
 import multiprocessing
 import subprocess
 
+os.chdir(os.path.join('bindings','python'))
+
 def parse_cmd(cmdline, prefix, keep_prefix = False):
 	ret = []
 	for token in cmdline.split():
@@ -53,7 +55,8 @@
 
 if '--bjam' in sys.argv or ldflags == None or extra_cmd == None:
 
-	del sys.argv[sys.argv.index('--bjam')]
+        if '--bjam' in sys.argv:
+                del sys.argv[sys.argv.index('--bjam')]
 
 	if not '--help' in sys.argv \
 		and not '--help-commands' in sys.argv:

Now execute: python setup.py bdist

That's it, if you go to your python terminal you can now import libtorrent.

These fixes are being sent upstream and hopefully the process will then be as simple as pip install <url-to-libtorrent-tar-gz>

Long way, Building Boost and libtorrent step by step (with OpenBazaar's ./env/bin/python)

If you already had an old boost library that you want to get rid of and you built it manually, go to its source folder and type:

./b2 uninstall
  • Download the boost libraries source code. (I built this with 1.57.0)

  • Decompress and enter the boost sources directory

  • Set and export the BOOST_ROOT variable on your ~/.bash_profile

export BOOST_ROOT=/Users/gubatron/src/boost_1_57_0
  • Bootstrap Boost and let it know it's going to build the Boost.Python library using your OpenBazaar's env/bin/python executable path, in my computer I did it like this:
./bootstrap.sh --with-python=/Users/gubatron/workspace.frostwire/OpenBazaar/env/bin/python

expected output

  • Build Boost
./b2

expected output

You should see this at the end:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

    /Users/gubatron/src/boost_1_57_0

The following directory should be added to linker library paths:

    /Users/gubatron/src/boost_1_57_0/stage/lib
  • export the BOOST_LIB_PATH convenience environment variable
export BOOST_LIB_PATH=$BOOST_ROOT/stage/lib
  • update your LDFLAGS environment variable to include your BOOST_LIB_PATH
LDFLAGS="$LDFLAGS -L$BOOST_LIB_PATH"
export LDFLAGS
  • update your CPPFLAGS environment variable to include your BOOST_ROOT path
CPPFLAGS="$CPPFLAGS -I$BOOST_ROOT"
export CPPFLAGS
  • Install Boost

(it will install by default on /usr/local/, you can pass --prefix= to specify a custom installation location, you can also specify a --build-dir= location)

./b2 install

expected output

Building libtorrent

  • Set and export the BOOST_BUILD_PATH and CXXFLAGS environment variables on your ~/.bash_profile (make sure it's active before the next steps)
export BOOST_BUILD_PATH=$BOOST_ROOT/tools/build
export CXXFLAGS="-stdlib=libc++ -std=c++11 -O3 -I$BOOST_ROOT"
$BOOST_ROOT/bjam toolset=darwin boost=source boost-link=shared link=shared variant=release deprecated-functions=off dht=on

expected output

Other parameters to consider depending if you're building for development or releasing:

debug-symbols=on|off
i2p=on|off
logging=none|default|verbose|errors
asserts=auto|on|off|productions  (auto sets them on if debug mode is on)

If you get a error: feature already defined: error message executing $BOOST_ROOT/bjam:

/Users/gubatron/src/boost_1_57_0/tools/build/src/build/feature.jam:139: in feature.feature from module feature
error: feature already defined:
error: in feature declaration:
error: feature "valgrind" : "on" : "optional" "propagated" "composite"

edit $BOOST_ROOT/libs/coroutine/build/Jamfile.v2 around line 14,15, and comment out:

#feature.feature valgrind : on : optional propagated composite ;
#feature.compose <valgrind>on : <define>BOOST_USE_VALGRIND ;

Build python-libtorrent bindings

  • Make sure no older versions of libtorrent.dylib and libtorrent.so already exist as they will cause a lot of issues. Places to look are /usr/lib/, /usr/local/lib, /usr/local/lib/python2.7/site-packages/libtorrent.so as python might try to load those instead of the one you're about to build.

  • Check that your LDFLAGS,LD_LIBRARY_PATH,DYLD_LIBRARY_PATH don't have more than what you need.

  • Inside your libtorrent sources folder go to bindings/python, replace the setup.py script with this one as the original (as of libtorrent 1.0.2 does not work on MacOSX)

  • Since we'll be building these bindings for the OpenBazaar OpenBazaar/env/bin/python, we'll use that path to build the python-libtorrent bindings, otherwise you can just use the system's python. On my development environment it looked like this:

$/Users/gubatron/workspace.frostwire/OpenBazaar/env/bin/python setup.py bdist

expected output

  • Test it
gubatrons-macbook-pro:OpenBazaar gubatron$ ./env/bin/python
Python 2.7.8 (default, Aug 24 2014, 21:26:19) 
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import libtorrent
>>> 

@hoffmabc
Copy link

This is what I'm getting on the build for setup.py

2 warnings generated.
c++ -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.10-intel-2.7/src/alert.o build/temp.macosx-10.10-intel-2.7/src/big_number.o build/temp.macosx-10.10-intel-2.7/src/converters.o build/temp.macosx-10.10-intel-2.7/src/create_torrent.o build/temp.macosx-10.10-intel-2.7/src/datetime.o build/temp.macosx-10.10-intel-2.7/src/entry.o build/temp.macosx-10.10-intel-2.7/src/error_code.o build/temp.macosx-10.10-intel-2.7/src/fingerprint.o build/temp.macosx-10.10-intel-2.7/src/ip_filter.o build/temp.macosx-10.10-intel-2.7/src/magnet_uri.o build/temp.macosx-10.10-intel-2.7/src/module.o build/temp.macosx-10.10-intel-2.7/src/peer_info.o build/temp.macosx-10.10-intel-2.7/src/session.o build/temp.macosx-10.10-intel-2.7/src/session_settings.o build/temp.macosx-10.10-intel-2.7/src/string.o build/temp.macosx-10.10-intel-2.7/src/torrent_handle.o build/temp.macosx-10.10-intel-2.7/src/torrent_info.o build/temp.macosx-10.10-intel-2.7/src/torrent_status.o build/temp.macosx-10.10-intel-2.7/src/utility.o build/temp.macosx-10.10-intel-2.7/src/version.o -ltorrent-rasterbar -lboost_system -lboost_python -lssl -lcrypto -o build/lib.macosx-10.10-intel-2.7/libtorrent.so -v -L/Users/brianhoffman/Downloads/boost_1_57_0/stage/lib -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi/ed25519/src/ -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi/src/ -arch x86_64
Apple LLVM version 6.0 (clang-600.0.54) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -dynamic -arch x86_64 -bundle -macosx_version_min 10.10.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk -undefined dynamic_lookup -undefined dynamic_lookup -o build/lib.macosx-10.10-intel-2.7/libtorrent.so -L/Users/brianhoffman/Downloads/boost_1_57_0/stage/lib -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi/ed25519/src/ -L../../bin/darwin-4.2.1/release/boost-link-shared/boost-source/deprecated-functions-off/threading-multi/src/ -F. build/temp.macosx-10.10-intel-2.7/src/alert.o build/temp.macosx-10.10-intel-2.7/src/big_number.o build/temp.macosx-10.10-intel-2.7/src/converters.o build/temp.macosx-10.10-intel-2.7/src/create_torrent.o build/temp.macosx-10.10-intel-2.7/src/datetime.o build/temp.macosx-10.10-intel-2.7/src/entry.o build/temp.macosx-10.10-intel-2.7/src/error_code.o build/temp.macosx-10.10-intel-2.7/src/fingerprint.o build/temp.macosx-10.10-intel-2.7/src/ip_filter.o build/temp.macosx-10.10-intel-2.7/src/magnet_uri.o build/temp.macosx-10.10-intel-2.7/src/module.o build/temp.macosx-10.10-intel-2.7/src/peer_info.o build/temp.macosx-10.10-intel-2.7/src/session.o build/temp.macosx-10.10-intel-2.7/src/session_settings.o build/temp.macosx-10.10-intel-2.7/src/string.o build/temp.macosx-10.10-intel-2.7/src/torrent_handle.o build/temp.macosx-10.10-intel-2.7/src/torrent_info.o build/temp.macosx-10.10-intel-2.7/src/torrent_status.o build/temp.macosx-10.10-intel-2.7/src/utility.o build/temp.macosx-10.10-intel-2.7/src/version.o -ltorrent-rasterbar -lboost_system -lboost_python -lssl -lcrypto -lc++ -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib/darwin/libclang_rt.osx.a
ld: library not found for -ltorrent-rasterbar
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'c++' failed with exit status 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment