From bebec55cb09da7aa8cdd577431b4135edb7b539e Mon Sep 17 00:00:00 2001
From: Giovanni Bussi <giovanni.bussi@gmail.com>
Date: Tue, 20 Nov 2018 13:39:31 +0100
Subject: [PATCH] Improvements to allow for pip install

- renamed script to setup.py
- Use "make pip" to copy required files in this dir for pip
- Use try/except when importing numpy and cython so as to notify the user
- renamed env vars to plumed_xxx so that there is no risk of clashes

Plumed can now also be installed using

cd python
make pip
pip install --user .

make pip is just needed to bring version information and Plumed.h
in this directory. Might be solved differently in MacPorts / Pypi.
---
 python/Makefile                              | 17 ++++++++---
 python/{buildPythonInterface.py => setup.py} | 32 ++++++++++++++++----
 src/lib/Makefile                             |  9 +++---
 3 files changed, 43 insertions(+), 15 deletions(-)
 rename python/{buildPythonInterface.py => setup.py} (73%)

diff --git a/python/Makefile b/python/Makefile
index 072c148ad..488b2e924 100644
--- a/python/Makefile
+++ b/python/Makefile
@@ -3,6 +3,8 @@ ifneq ($(MAKECMDGOALS),clean)
   -include ../Makefile.conf
 endif
 
+VERSION = $(shell if test -f ../VERSION ; then grep -v \\\# ../VERSION ; else echo "Unknown" ; fi )
+
 .PHONY: all clean install
 
 plumed_compiled := $(wildcard ../src/lib/plumed)
@@ -19,10 +21,11 @@ ifdef python_bin
 all:
 	@echo Building python interface for PLUMED 
 	unset CXX && unset CC && unset CFLAGS && unset CXXFLAGS && unset LDSHARED && \
-          program_name=plumed \
-          include_dir=../src/wrapper \
-          default_kernel="$$PWD/../src/lib/libplumedKernel.$(SOEXT)" \
-          $(python_bin) buildPythonInterface.py build_ext -i
+          plumed_program_name=plumed \
+          plumed_version=$(VERSION) \
+          plumed_include_dir=../src/wrapper \
+          plumed_default_kernel="$$PWD/../src/lib/libplumedKernel.$(SOEXT)" \
+          $(python_bin) setup.py build_ext -i
 
 else
 
@@ -32,5 +35,9 @@ endif
 
 endif
 
+pip:
+	cp ../src/wrapper/Plumed.h .
+	cp ../VERSION .
+
 clean:
-	rm -fr *.so plumed.cpp build
+	rm -fr *.so plumed.cpp build Plumed.h VERSION
diff --git a/python/buildPythonInterface.py b/python/setup.py
similarity index 73%
rename from python/buildPythonInterface.py
rename to python/setup.py
index ff7fd60bd..e9f416266 100644
--- a/python/buildPythonInterface.py
+++ b/python/setup.py
@@ -24,27 +24,47 @@
 #
 from distutils.core import setup
 from distutils.extension import Extension
-from Cython.Build import cythonize
-import numpy
 import subprocess
 import os
+import sys
 
-plumedname = os.getenv("program_name")
+plumedname = os.getenv("plumed_program_name")
 if plumedname is None:
     plumedname = "plumed"
 
-plumedversion = subprocess.check_output(['grep','-v','#','../VERSION']).decode("utf-8")
+plumedversion = os.getenv("plumed_version")
+if plumedversion is None:
+    plumedversion = subprocess.check_output(['grep','-v','#','./VERSION']).decode("utf-8")
 
 print( "Module name " + plumedname )
 print( "Version number " + plumedversion )
 
 extra_compile_args=['-D__PLUMED_HAS_DLOPEN','-D__PLUMED_WRAPPER_LINK_RUNTIME=1','-D__PLUMED_WRAPPER_CXX=1','-D__PLUMED_WRAPPER_IMPLEMENTATION=1','-D__PLUMED_WRAPPER_EXTERN=0','-D__PLUMED_WRAPPER_CXX_DEFAULT_INVALID=1'] 
 
-defaultkernel=os.getenv("default_kernel")
+defaultkernel=os.getenv("plumed_default_kernel")
 if defaultkernel is not None:
     extra_compile_args.append("-D__PLUMED_DEFAULT_KERNEL=" + os.path.abspath(defaultkernel))
     print( "Hardcoded PLUMED_KERNEL " + os.path.abspath(defaultkernel))
 
+try:
+    import numpy
+except:
+    print('Error: building ' + plumedname + ' requires numpy. Please install it first with pip install numpy')
+    sys.exit(-1)
+
+try:
+    from Cython.Build import cythonize
+except:
+    print('Error: building ' + plumedname + ' requires cython. Please install it first with pip install cython')
+    sys.exit(-1)
+
+include_dirs=[numpy.get_include()]
+
+try:
+    include_dirs.append(os.environ["plumed_include_dir"])
+except:
+    include_dirs.append(".")
+
 setup(
   name=plumedname,
   version=plumedversion,
@@ -56,7 +76,7 @@ setup(
                   Extension( name=plumedname,
                              sources=["plumed.pyx"],
                              language="c++",
-                             include_dirs=[os.environ["include_dir"], numpy.get_include()],
+                             include_dirs=include_dirs,
                              extra_compile_args=extra_compile_args
                            )
                           ])
diff --git a/src/lib/Makefile b/src/lib/Makefile
index 9cb765941..1dc913886 100644
--- a/src/lib/Makefile
+++ b/src/lib/Makefile
@@ -313,10 +313,11 @@ ifdef python_bin
 	cp -pr ../../python install 
 	cd install/python && rm -fr *.so plumed.cpp build && \
           unset CXX && unset CC && unset CFLAGS && unset CXXFLAGS && unset LDSHARED && \
-          program_name="$(program_name)" \
-          include_dir=../../../wrapper \
-          default_kernel="$(libdir)/lib$(program_name)Kernel.$(SOEXT)" \
-          $(python_bin) buildPythonInterface.py build_ext -i
+          plumed_program_name="$(program_name)" \
+          plumed_version=$(VERSION) \
+          plumed_include_dir=../../../wrapper \
+          plumed_default_kernel="$(libdir)/lib$(program_name)Kernel.$(SOEXT)" \
+          $(python_bin) setup.py build_ext -i
 	cp -pr install/python "$(DESTDIR)$(libdir)/$(program_name)/" 
 endif
 ifdef BASH_COMPLETION_DIR
-- 
GitLab