Skip to content

Commit

Permalink
Added some aid-compile option in autoconf
Browse files Browse the repository at this point in the history
* Trying to setup pycv into config

* forgot to commit configure before pushig

* redesigning the compilation with the new ac

* changed behaviour of not finding cython

* now if no python package is found also python_bin is not set

* now configure understands if python is embeddable

* moved the variables and added some comments for clarity

* rerun autoconf

* set up  a cleaner instalation

* forgot to push the compileconfiguration common sourced file

* added some aid for maintainers

---------

Co-authored-by: Daniele Rapetti <[email protected]>
  • Loading branch information
Iximiel and Iximiel authored Dec 19, 2023
1 parent 2ac1f50 commit 534003b
Show file tree
Hide file tree
Showing 10 changed files with 250 additions and 79 deletions.
5 changes: 5 additions & 0 deletions Makefile.conf.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ datadir=@datadir@
docdir=@docdir@
htmldir=@htmldir@
python_bin=@PYTHON_BIN@
plumed_found_cython=@CYTHON_FOUND@
canPyCV=@PLUMED_CAN_PYCV@
pybind11_cflags=@PYBIND11_CFLAGS@
python_cf_embedded=@PYTHON_CFLAGS@
python_ld_embedded=@PYTHON_LDFLAGS@
mpiexec=@MPIEXEC@
make_static_archive=@make_static_archive@
use_absolute_soname=@use_absolute_soname@
Expand Down
103 changes: 90 additions & 13 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,11 @@ make_doc
readelf
LD_MAIN_RUNPATH
use_debug_glibcxx
PYTHON_CFLAGS
PYTHON_LDFLAGS
PYBIND11_CFLAGS
PLUMED_CAN_PYCV
CYTHON_FOUND
OPENMP_CXXFLAGS
EGREP
GREP
Expand Down Expand Up @@ -742,6 +747,7 @@ enable_boost_graph
enable_boost_serialization
enable_fftw
enable_python
enable_pycv
enable_af_ocl
enable_af_cuda
enable_af_cpu
Expand Down Expand Up @@ -1443,6 +1449,7 @@ Optional Features:
enable search for boost serialization, default: no
--enable-fftw enable search for fftw, default: yes
--enable-python enable search for python, default: yes
--enable-pycv enable set up the pycv plugin, default: yes
--enable-af_ocl enable search for arrayfire_ocl, default: no
--enable-af_cuda enable search for arrayfire_cuda, default: no
--enable-af_cpu enable search for arrayfire_cpu, default: no
Expand Down Expand Up @@ -3143,6 +3150,24 @@ fi



pycv=
# Check whether --enable-pycv was given.
if test "${enable_pycv+set}" = set; then :
enableval=$enable_pycv; case "${enableval}" in
(yes) pycv=true ;;
(no) pycv=false ;;
(*) as_fn_error $? "wrong argument to --enable-pycv" "$LINENO" 5 ;;
esac
else
case "yes" in
(yes) pycv=true ;;
(no) pycv=false ;;
esac

fi



af_ocl=
# Check whether --enable-af_ocl was given.
if test "${enable_af_ocl+set}" = set; then :
Expand Down Expand Up @@ -3222,7 +3247,6 @@ fi




# by default use -O flag
# we override the autoconf default (-g) because in release build we do not want to
# include symbol information (obj files are huge)
Expand Down Expand Up @@ -8945,11 +8969,21 @@ $as_echo "$as_me: WARNING: cannot enable __PLUMED_HAS_FFTW" >&2;}

fi

if test $python = true ; then
# python executable: if any of CYTHON_FOUND or PLUMED_CAN_PYCV is on will store
# the python executable

# set if PYTHON_BIN has Cython in its environment

# definitions and variables related to pycv and in general embedding python





# if PYTHON_BIN is defined, it is expected to be the full path to python
# Otherwise, search from a list of names:
if test -z "$PYTHON_BIN" ; then
for ac_prog in python
if test -z "$PYTHON_BIN" ; then
for ac_prog in python python3
do
# Extract the first word of "$ac_prog", so it can be a program name with args.
set dummy $ac_prog; ac_word=$2
Expand Down Expand Up @@ -8991,14 +9025,54 @@ fi
test -n "$PYTHON_BIN" && break
done

fi
if test -n "$PYTHON_BIN"
then
fi
if test -n "$PYTHON_BIN" ; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: Python executable is $PYTHON_BIN" >&5
$as_echo "$as_me: Python executable is $PYTHON_BIN" >&6;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking support for required python modules (python3, setuptools, cython)" >&5
$as_echo_n "checking support for required python modules (python3, setuptools, cython)... " >&6; }
testimport="
if test $pycv = true ; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking pycv: support for required python modules (python3, pybind11, numpy)" >&5
$as_echo_n "checking pycv: support for required python modules (python3, pybind11, numpy)... " >&6; }
testimport="
import numpy
import pybind11
#no need to check for python3, if you have python2, you don't have pybind11 (and vice-versa)
"
if echo "$testimport" | "$PYTHON_BIN" 1>/dev/null 2>/dev/null; then

pyver=$($PYTHON_BIN -c "import sysconfig;print(sysconfig.get_config_var('VERSION'))")
python_config=python${pyver}-config

if ${python_config} --ldflags --embed 1>/dev/null 2>/dev/null ;then
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: yes" >&5
$as_echo "yes" >&6; }
PYBIND11_CFLAGS="$($PYTHON_BIN -m pybind11 --includes) -fvisibility=hidden"
PYTHON_CFLAGS=$(${python_config} --cflags --embed)
PYTHON_LDFLAGS=$(${python_config} --ldflags --embed)
#TODO: conda_fixup
PLUMED_CAN_PYCV=yes
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cannot enable pycv" >&5
$as_echo "$as_me: WARNING: cannot enable pycv" >&2;}
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"PyCV needs python to be embeddable, please rebuild python\"" >&5
$as_echo "$as_me: WARNING: \"PyCV needs python to be embeddable, please rebuild python\"" >&2;}
fi
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cannot enable pycv" >&5
$as_echo "$as_me: WARNING: cannot enable pycv" >&2;}
fi
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cannot enable pycv" >&5
$as_echo "$as_me: WARNING: cannot enable pycv" >&2;}
fi

if test $python = true ; then
{ $as_echo "$as_me:${as_lineno-$LINENO}: checking python wrapper: support for required python modules (python3, setuptools, cython)" >&5
$as_echo_n "checking python wrapper: support for required python modules (python3, setuptools, cython)... " >&6; }
testimport="
from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
Expand All @@ -9011,19 +9085,24 @@ if sys.version_info < (3,):
$as_echo "yes" >&6; }
$as_echo "#define __PLUMED_HAS_PYTHON 1" >>confdefs.h

CYTHON_FOUND=yes
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: result: no" >&5
$as_echo "no" >&6; }
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cannot enable python interface" >&5
$as_echo "$as_me: WARNING: cannot enable python interface" >&2;}
PYTHON_BIN=
fi
else
{ $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: cannot enable python interface" >&5
$as_echo "$as_me: WARNING: cannot enable python interface" >&2;}
fi
fi

if test "${PLUMED_CAN_PYCV}" != yes && test "${CYTHON_FOUND}" != yes; then
#this deactivate python bin if no package was found
PYTHON_BIN=
fi

if test "$af_ocl" = true ; then

found=ko
Expand Down Expand Up @@ -10062,8 +10141,6 @@ $as_echo "#define _REENTRANT 1" >>confdefs.h

# these are libraries that should be linked also to MD engines

# python executable




Expand Down
71 changes: 58 additions & 13 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,7 @@ PLUMED_CONFIG_ENABLE([boost_graph],[search for boost graph],[no])
PLUMED_CONFIG_ENABLE([boost_serialization],[search for boost serialization],[no])
PLUMED_CONFIG_ENABLE([fftw],[search for fftw],[yes])
PLUMED_CONFIG_ENABLE([python],[search for python],[yes])
PLUMED_CONFIG_ENABLE([pycv],[set up the pycv plugin],[yes])
PLUMED_CONFIG_ENABLE([af_ocl],[search for arrayfire_ocl],[no])
PLUMED_CONFIG_ENABLE([af_cuda],[search for arrayfire_cuda],[no])
PLUMED_CONFIG_ENABLE([af_cpu],[search for arrayfire_cpu],[no])
Expand All @@ -331,7 +332,6 @@ AC_ARG_VAR(PYTHON_BIN,[python executable (e.g. python2.7 or /opt/local/bin/pytho
AC_ARG_VAR(BASH_COMPLETION_DIR,[path where bash completion will be installed - default: search with pkg-config])
AC_ARG_VAR(MPIEXEC,[command to run mpi programs in tests - default not specified, which means use PLUMED_MPIRUN env var at runtime for backward compatibility])


# by default use -O flag
# we override the autoconf default (-g) because in release build we do not want to
# include symbol information (obj files are huge)
Expand Down Expand Up @@ -838,17 +838,59 @@ if test $fftw = true ; then
PLUMED_CHECK_PACKAGE([fftw3.h],[fftw_execute],[__PLUMED_HAS_FFTW],[fftw3])
fi

if test $python = true ; then
# python executable: if any of CYTHON_FOUND or PLUMED_CAN_PYCV is on will store
# the python executable
AC_SUBST(PYTHON_BIN)
# set if PYTHON_BIN has Cython in its environment
AC_SUBST(CYTHON_FOUND)
# definitions and variables related to pycv and in general embedding python
AC_SUBST(PLUMED_CAN_PYCV)
AC_SUBST(PYBIND11_CFLAGS)
AC_SUBST(PYTHON_LDFLAGS)
AC_SUBST(PYTHON_CFLAGS)

# if PYTHON_BIN is defined, it is expected to be the full path to python
# Otherwise, search from a list of names:
if test -z "$PYTHON_BIN" ; then
AC_CHECK_PROGS([PYTHON_BIN],[python])
fi
if test -n "$PYTHON_BIN"
then
# Otherwise, search from a list of names:
if test -z "$PYTHON_BIN" ; then
AC_CHECK_PROGS([PYTHON_BIN],[python python3])
fi
if test -n "$PYTHON_BIN" ; then
AC_MSG_NOTICE([Python executable is $PYTHON_BIN])
AC_MSG_CHECKING([support for required python modules (python3, setuptools, cython)])
testimport="
if test $pycv = true ; then
AC_MSG_CHECKING([pycv: support for required python modules (python3, pybind11, numpy)])
testimport="
import numpy
import pybind11
#no need to check for python3, if you have python2, you don't have pybind11 (and vice-versa)
"
if echo "$testimport" | "$PYTHON_BIN" 1>/dev/null 2>/dev/null; then

pyver=$($PYTHON_BIN -c "import sysconfig;print(sysconfig.get_config_var('VERSION'))")
python_config=python${pyver}-config

if ${python_config} --ldflags --embed 1>/dev/null 2>/dev/null ;then
AC_MSG_RESULT([yes])
PYBIND11_CFLAGS="$($PYTHON_BIN -m pybind11 --includes) -fvisibility=hidden"
PYTHON_CFLAGS=$(${python_config} --cflags --embed)
PYTHON_LDFLAGS=$(${python_config} --ldflags --embed)
#TODO: conda_fixup
PLUMED_CAN_PYCV=yes
else
AC_MSG_RESULT([no])
AC_MSG_WARN([cannot enable pycv])
AC_MSG_WARN(["PyCV needs python to be embeddable, please rebuild python"])
fi
else
AC_MSG_RESULT([no])
AC_MSG_WARN([cannot enable pycv])
fi
else
AC_MSG_WARN([cannot enable pycv])
fi

if test $python = true ; then
AC_MSG_CHECKING([python wrapper: support for required python modules (python3, setuptools, cython)])
testimport="
from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize
Expand All @@ -859,16 +901,21 @@ if sys.version_info < (3,):
if echo "$testimport" | "$PYTHON_BIN" 1>/dev/null 2>/dev/null; then
AC_MSG_RESULT([yes])
AC_DEFINE([__PLUMED_HAS_PYTHON])
CYTHON_FOUND=yes
else
AC_MSG_RESULT([no])
AC_MSG_WARN([cannot enable python interface])
PYTHON_BIN=
fi
else
AC_MSG_WARN([cannot enable python interface])
fi
fi

if test "${PLUMED_CAN_PYCV}" != yes && test "${CYTHON_FOUND}" != yes; then
#this deactivate python bin if no package was found
PYTHON_BIN=
fi

if test "$af_ocl" = true ; then
PLUMED_CHECK_PACKAGE([arrayfire.h],[af_is_double],[__PLUMED_HAS_ARRAYFIRE],[afopencl])
PLUMED_CHECK_PACKAGE([arrayfire.h],[af_is_double],[__PLUMED_HAS_ARRAYFIRE_OCL],[afopencl])
Expand Down Expand Up @@ -944,8 +991,6 @@ AC_SUBST(LDSHARED)
AC_SUBST(LD_MAIN_RUNPATH)
# these are libraries that should be linked also to MD engines
AC_SUBST(STATIC_LIBS)
# python executable
AC_SUBST(PYTHON_BIN)

AC_SUBST(MPIEXEC)

Expand Down
2 changes: 1 addition & 1 deletion plugins/pycv/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Make.inc
/Makefile.conf
*.o
*.so
env
14 changes: 11 additions & 3 deletions plugins/pycv/Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
include Make.inc
# include the machine dependent configuration
ifneq ($(MAKECMDGOALS),clean)
-include ../../Makefile.conf
ifndef canPyCV
include ./Makefile.conf
endif
endif

.PHONY: clean check all
#Dependency tracking based on https://make.mad-scientist.net/papers/advanced-auto-dependency-generation/#tldr
#this assumes gcc
DEPDIR := .deps
DEPFLAGS = -MT $@ -MMD -MP -MF $(DEPDIR)/$*.d
PYBIND11FLAG =

ADDCPPFLAGS:=$(python_cf_embedded) $(pybind11_cflags) $(ADDCPPFLAGS)
ADDCLDFLAGS:=$(ADDCLDFLAGS) $(python_ld_embedded)
OBJS = ActionWithPython.o PythonCVInterface.o PythonFunction.o PlumedPythonEmbeddedModule.o

ifeq ($(SOEXT),dylib)
Expand All @@ -18,6 +25,7 @@ endif
all: PythonCVInterface.$(SOEXT)

#-fvisibility=hidden is needed for pybind11 (to not conflict with different pybind11 versions)
#I think I enforced this nearly everywhere I set up a flag for the compiler
ActionWithPython.o PythonCVInterface.o PythonFunction.o PlumedPythonEmbeddedModule.o: PYBIND11FLAG= -fvisibility=hidden

%.o: %.cpp $(DEPDIR)/%.d | $(DEPDIR)
Expand All @@ -35,7 +43,7 @@ include $(wildcard $(DEPFILES))
#TODO: seems that $PLUMED_KERNEL is not needed, check
PythonCVInterface.$(SOEXT): $(OBJS)
@echo Linking $@
$(LDSHARED) $(SONAME_OPTION),"$(notdir $@)" $(DYNAMIC_LIBS) $(PLUMED_KERNEL) $(ADDCLDFLAGS) $^ -o $@
$(LDSHARED) $(SONAME_OPTION),"$(notdir $@)" $(DYNAMIC_LIBS) $(PLUMED_KERNEL) $(ADDCLDFLAGS) $^ -o $@

clean:
rm -f $(OBJS) PythonCVInterface.$(SOEXT)
Expand Down
11 changes: 7 additions & 4 deletions plugins/pycv/astyle.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
#! /usr/bin/env bash

#formatted with shfmt https://github.com/mvdan/sh/releases
#checked with shellcheck

for file in *.c *.cpp *.h *.inc.in; do

test -f "$file" || continue

echo -n "astyle $file"

../../astyle/astyle --options=../../.astyle.options <$file >$file.tmp && {
if cmp -s $file $file.tmp; then
../../astyle/astyle --options=../../.astyle.options <"$file" >"${file}.tmp" && {
if cmp -s "$file" "${file}.tmp"; then
echo
else
cp $file.tmp $file
cp "${file}.tmp" "$file"
echo " +++ PATCHED"
fi
}

rm $file.tmp
rm "${file}.tmp"

done
Loading

0 comments on commit 534003b

Please sign in to comment.