From 4d23a1b8630288d3f18cddd282316af22d446b4c Mon Sep 17 00:00:00 2001
From: Yusei Tahara <yusei@nexedi.com>
Date: Thu, 12 Oct 2017 15:17:51 +0900
Subject: [PATCH] component/pytorch: Fix RPATH to use slapos libraries.

---
 component/pytorch/buildout.cfg           |  7 ++++++-
 component/pytorch/pytorch.4af66c4.patch  | 26 ++++++++++++++++++++++++
 component/pytorch/setup.py.4af66c4.patch | 13 ------------
 3 files changed, 32 insertions(+), 14 deletions(-)
 create mode 100644 component/pytorch/pytorch.4af66c4.patch
 delete mode 100644 component/pytorch/setup.py.4af66c4.patch

diff --git a/component/pytorch/buildout.cfg b/component/pytorch/buildout.cfg
index 126223795e..ef1c5b8a1e 100644
--- a/component/pytorch/buildout.cfg
+++ b/component/pytorch/buildout.cfg
@@ -23,7 +23,7 @@ commit = 4af66c43045a317b477918c503d105f565b4a66b
 git-binary = ${git:location}/bin/git
 patch-binary = ${patch:location}/bin/patch
 location = ${buildout:parts-directory}/${:_buildout_section_name_}
-command = export HOME=${:location}; (${:git-binary} clone --recursive --quiet -b ${:tag} ${:repository} ${:location}; cd ${:location}; ${:git-binary} checkout ${:commit}; ${:patch-binary} -p1 -d . < ${:_profile_base_location_}/setup.py.4af66c4.patch ) || (rm -fr ${:location}; exit 1)
+command = export HOME=${:location}; (${:git-binary} clone --recursive --quiet -b ${:tag} ${:repository} ${:location}; cd ${:location}; ${:git-binary} checkout ${:commit}; ${:patch-binary} -p1 -d . < ${:_profile_base_location_}/pytorch.4af66c4.patch ) || (rm -fr ${:location}; exit 1)
 
 [pytorch-build-interpreter]
 recipe = zc.recipe.egg
@@ -78,6 +78,11 @@ script =
          'CC':gcc_location+'/bin/gcc',
          'CXX':gcc_location+'/bin/g++',
          'PYTORCH_PYTHON':python_bin,
+         'SLAPOS_COMPILE_ARGS':' '.join(['-Wl,-rpath,'+gcc_location+'/lib',
+                                         '-Wl,-rpath,'+gcc_location+'/lib64',
+                                         '-Wl,-rpath,'+binutils_location+'/lib',
+                                         '-Wl,-rpath,'+openblas_location+'/lib',
+                                         ])
          }
   if self.options.get('no-cuda') == '1':
     env['NO_CUDA'] = '1'
diff --git a/component/pytorch/pytorch.4af66c4.patch b/component/pytorch/pytorch.4af66c4.patch
new file mode 100644
index 0000000000..6b4aca1ef3
--- /dev/null
+++ b/component/pytorch/pytorch.4af66c4.patch
@@ -0,0 +1,26 @@
+diff --git a/setup.py b/setup.py
+index 1d9a765..a50e9cb 100644
+--- a/setup.py
++++ b/setup.py
+@@ -90,7 +90,7 @@ def build_libs(libs):
+         assert lib in dep_libs, 'invalid lib: {}'.format(lib)
+     build_libs_cmd = ['bash', 'torch/lib/build_libs.sh']
+     my_env = os.environ.copy()
+-    my_env["PYTORCH_PYTHON"] = sys.executable
++    #my_env["PYTORCH_PYTHON"] = sys.executable
+     if WITH_SYSTEM_NCCL:
+         my_env["NCCL_ROOT_DIR"] = NCCL_ROOT_DIR
+     if WITH_CUDA:
+diff --git a/torch/lib/build_libs.sh b/torch/lib/build_libs.sh
+index af7020e..a91bd05 100755
+--- a/torch/lib/build_libs.sh
++++ b/torch/lib/build_libs.sh
+@@ -24,7 +24,7 @@ C_FLAGS=" -DTH_INDEX_BASE=0 -I$INSTALL_DIR/include \
+   -I$INSTALL_DIR/include/THS -I$INSTALL_DIR/include/THCS \
+   -I$INSTALL_DIR/include/THPP -I$INSTALL_DIR/include/THNN \
+   -I$INSTALL_DIR/include/THCUNN"
+-LDFLAGS="-L$INSTALL_DIR/lib "
++LDFLAGS="-L$INSTALL_DIR/lib $SLAPOS_COMPILE_ARGS"
+ LD_POSTFIX=".so.1"
+ LD_POSTFIX_UNVERSIONED=".so"
+ if [[ $(uname) == 'Darwin' ]]; then
diff --git a/component/pytorch/setup.py.4af66c4.patch b/component/pytorch/setup.py.4af66c4.patch
deleted file mode 100644
index 041ef0b79d..0000000000
--- a/component/pytorch/setup.py.4af66c4.patch
+++ /dev/null
@@ -1,13 +0,0 @@
-diff --git a/setup.py b/setup.py
-index 1d9a765..a50e9cb 100644
---- a/setup.py
-+++ b/setup.py
-@@ -90,7 +90,7 @@ def build_libs(libs):
-         assert lib in dep_libs, 'invalid lib: {}'.format(lib)
-     build_libs_cmd = ['bash', 'torch/lib/build_libs.sh']
-     my_env = os.environ.copy()
--    my_env["PYTORCH_PYTHON"] = sys.executable
-+    #my_env["PYTORCH_PYTHON"] = sys.executable
-     if WITH_SYSTEM_NCCL:
-         my_env["NCCL_ROOT_DIR"] = NCCL_ROOT_DIR
-     if WITH_CUDA:
-- 
2.30.9