Skip to content

Commit a6db169

Browse files
authored
Fix for pybind11 and updates in github actions (#478)
* parent 94fe3d6 author Subhasis Ray <[email protected]> 1715590080 +0530 committer Subhasis Ray <[email protected]> 1715619586 +0530 parent 94fe3d6 author Subhasis Ray <[email protected]> 1715590080 +0530 committer Subhasis Ray <[email protected]> 1715618717 +0530 - Updated includes in sourcecode for pybind11 - Fixed cmake file for finding system pybind11 - Updated github workflow to use micromamba setup from github marketplace * Added simple test of importing moose
1 parent 94fe3d6 commit a6db169

File tree

9 files changed

+94
-75
lines changed

9 files changed

+94
-75
lines changed

.github/workflows/pymoose.yml

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,43 @@ jobs:
1717
- os: macos-14
1818
brew: 20
1919
steps:
20-
- uses: actions/checkout@v2
21-
- if: ${{ matrix.apt }}
22-
run: sudo apt-get install libhdf5-dev libgsl0-dev
23-
- if: ${{ matrix.brew }}
24-
run: |
25-
brew install gsl
26-
brew install hdf5
27-
- name: Set up Python ${{ matrix.python-version }}
28-
shell: bash
29-
run: |
30-
"${SHELL}" <(curl -L micro.mamba.pm/install.sh)
31-
micromamba self-update
32-
micromamba create -n moose python=${{ matrix.python-version }} graphviz lxml cmake numpy matplotlib vpython hdf5 pytables doxygen -c conda-forge
33-
eval "$(micromamba shell hook --shell bash)"
34-
micromamba activate moose
35-
pip install python-libsbml
36-
pip install pyneuroml
37-
- name: Build
38-
run: |
39-
pip install .
40-
20+
- name: mamba-setup
21+
uses: mamba-org/setup-micromamba@v1
22+
with:
23+
environment-name: moose
24+
cache-environment: true
25+
cache-downloads: true
26+
create-args: >-
27+
python=${{ matrix.python-version }}
28+
hdf5
29+
graphviz
30+
pytables
31+
numpy
32+
matplotlib
33+
vpython
34+
lxml
35+
doxygen
36+
setuptools
37+
wheel
38+
- name: Display Python version
39+
run: |
40+
python -c "import sys; print(sys.version)"
41+
- if: ${{ matrix.apt }}
42+
run: sudo apt-get install libhdf5-dev libgsl0-dev graphviz-dev python3-lxml cmake doxygen
43+
- if: ${{ matrix.brew }}
44+
run: |
45+
brew install gsl
46+
brew install hdf5
47+
brew install graphviz
48+
brew install cmake
49+
brew install doxygen
50+
- name: install python module dependencies via pip
51+
run: |
52+
pip install pybind11[global]
53+
pip install python-libsbml
54+
- name: checkout
55+
uses: actions/checkout@v4
56+
- name: build and install
57+
run: |
58+
pip install .
59+
python -c "import moose; moose.le()"

CMakeLists.txt

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,9 @@ include(CheckCXXCompiler.cmake)
99
include(CheckIncludeFileCXX)
1010

1111
# We find python executable here. Though mainly used inside pymoose.
12-
# FIXME: When cmake 3.12 is widely available use the following:
13-
# find_package(Python3 COMPONENTS Interpreter Numpy)
14-
# set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
15-
set(Python_ADDITIONAL_VERSIONS 2.7)
16-
find_package(PythonInterp 3.5)
17-
if(NOT PYTHONINTERP_FOUND)
18-
find_package(PythonInterp 2.7)
19-
endif()
12+
find_package(Python3 COMPONENTS Interpreter Numpy)
13+
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE})
14+
find_package(PythonInterp 3.8)
2015

2116
set(CMAKE_MACOSX_RPATH OFF)
2217

@@ -321,14 +316,9 @@ endif( WITH_BOOST )
321316
# It can be easily done with `pip install pybind11`
322317
# See: https://pybind11.readthedocs.io/en/stable/installing.html
323318
# - Subha, Mon Apr 22 14:26:58 IST 2024
324-
325-
# if(NOT WITH_LEGACY_BINDING)
326-
# add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/pybind11)
327-
# add_subdirectory(pybind11)
328-
# else()
329-
# message(STATUS "Building legacy python binding.")
330-
# add_subdirectory(pymoose)
331-
# endif()
319+
execute_process(COMMAND ${CMAKE_COMMAND} -E pybind11-config --cmakedir OUTPUT_VARIABLE pybind11_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)
320+
find_package(pybind11 REQUIRED HINTS "${Python3_SITELIB}")
321+
add_subdirectory(pybind11)
332322

333323

334324
# always override debian default installation directory. It will be installed in

INSTALL.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ followings are installed.
88

99
- gsl-1.16 or higher.
1010
- python-numpy
11+
- pybind11 (if the setup fails to find pybind11, try running `pip install pybind11[global]`)
1112

1213
On Ubuntu-16.04 or higher, these dependencies can be installed with:
1314

1415
```
15-
sudo apt-get install python-pip python-numpy cmake libgsl-dev g++
16+
sudo apt-get install python-pip python-numpy cmake libgsl-dev g++ pybind11
1617
```
1718

1819
Now use `pip` to download and install `pymoose` from the [github repository](https://github.com/BhallaLab/moose-core).
@@ -83,10 +84,26 @@ Now you can import moose in a Python script or interpreter with the statement:
8384
>>> import moose
8485
>>> moose.test() # will take time. Not all tests will pass.
8586

87+
## Uninstall
88+
89+
To uninstall moose, run
90+
91+
$ pip uninstall pymoose
92+
93+
If you are building moose from source, make sure to get out of the source directory, or you may encounter a message like this:
94+
95+
Found existing installation: pymoose {version}
96+
Can't uninstall 'pymoose'. No files were found to uninstall.
97+
98+
99+
100+
86101
# Notes
87102

88-
SBML support is enabled by installing
103+
- SBML support is enabled by installing
89104
[python-libsbml](http://sbml.org/Software/libSBML/docs/python-api/libsbml-installation.html).
90105
Alternatively, it can be installed by using `python-pip`
91106

92107
$ sudo pip install python-libsbml
108+
109+

pybind11/Finfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
//
1010
// =====================================================================================
1111

12-
#include "../external/pybind11/include/pybind11/pybind11.h"
13-
#include "../external/pybind11/include/pybind11/stl.h"
14-
#include "../external/pybind11/include/pybind11/numpy.h"
15-
#include "../external/pybind11/include/pybind11/functional.h"
12+
#include <pybind11/pybind11.h>
13+
#include <pybind11/stl.h>
14+
#include <pybind11/numpy.h>
15+
#include <pybind11/functional.h>
1616

1717
namespace py = pybind11;
1818

pybind11/MooseVec.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313

1414
using namespace std;
1515

16-
#include "../external/pybind11/include/pybind11/pybind11.h"
17-
#include "../external/pybind11/include/pybind11/numpy.h"
18-
#include "../external/pybind11/include/pybind11/stl.h"
16+
#include <pybind11/pybind11.h>
17+
#include <pybind11/numpy.h>
18+
#include <pybind11/stl.h>
1919

2020
namespace py = pybind11;
2121

pybind11/MooseVec.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#ifndef MOOSE_VEC_H
1111
#define MOOSE_VEC_H
1212

13-
#include "../external/pybind11/include/pybind11/pybind11.h"
14-
#include "../external/pybind11/include/pybind11/numpy.h"
13+
#include <pybind11/pybind11.h>
14+
#include <pybind11/numpy.h>
1515
namespace py = pybind11;
1616

1717
class MooseVec

pybind11/helper.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
#include <stdexcept>
1919
#include <csignal>
2020

21-
#include "../external/pybind11/include/pybind11/functional.h"
22-
#include "../external/pybind11/include/pybind11/numpy.h"
23-
#include "../external/pybind11/include/pybind11/pybind11.h"
24-
#include "../external/pybind11/include/pybind11/stl.h"
21+
#include <pybind11/functional.h>
22+
#include <pybind11/numpy.h>
23+
#include <pybind11/pybind11.h>
24+
#include <pybind11/stl.h>
2525

2626
namespace py = pybind11;
2727

2828
// See
2929
// https://pybind11.readthedocs.io/en/stable/advanced/cast/stl.html#binding-stl-containers
30-
// #include "../external/pybind11/include/pybind11/stl_bind.h"
30+
// #include <pybind11/stl_bind.h>
3131

3232
#include "../basecode/header.h"
3333
#include "../basecode/global.h"

pybind11/pymoose.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
#include <functional>
2020
#include <chrono>
2121

22-
#include "../external/pybind11/include/pybind11/pybind11.h"
23-
#include "../external/pybind11/include/pybind11/stl.h"
24-
#include "../external/pybind11/include/pybind11/numpy.h"
22+
#include <pybind11/pybind11.h>
23+
#include <pybind11/stl.h>
24+
#include <pybind11/numpy.h>
2525

2626
namespace py = pybind11;
2727
using namespace std;

setup.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
# See https://docs.python.org/3/library/distutils.html
2929
# setuptools is preferred over distutils. And we are supporting python3 only.
3030
from setuptools import setup, Extension, Command
31-
from setuptools.command.build_ext import build_ext as _build_ext
31+
from setuptools.command.build_ext import build_ext
3232
import subprocess
3333

3434
# Global variables.
@@ -39,6 +39,7 @@
3939

4040
if not os.path.exists(builddir_):
4141
os.makedirs(builddir_)
42+
4243

4344
numCores_ = multiprocessing.cpu_count()
4445

@@ -49,18 +50,10 @@
4950

5051

5152
class CMakeExtension(Extension):
53+
# Reference: https://martinopilia.com/posts/2018/09/15/building-python-extension.html
5254
def __init__(self, name, **kwargs):
5355
# don't invoke the original build_ext for this special extension
54-
import tempfile
55-
56-
# Create a temp file to create a dummy target. This build raises an
57-
# exception because sources are empty. With python3 we can fix it by
58-
# passing `optional=True` to the argument. With python2 there is no
59-
# getaway from it.
60-
f = tempfile.NamedTemporaryFile(suffix='.cpp', delete=False)
61-
f.write(b'int main() { return 1; }')
62-
Extension.__init__(self, name, sources=[f.name], **kwargs)
63-
f.close()
56+
Extension.__init__(self, name, sources=[], **kwargs)
6457

6558

6659
class TestCommand(Command):
@@ -79,14 +72,14 @@ def run(self):
7972
os.chdir(sdir_)
8073

8174

82-
class build_ext(_build_ext):
75+
class cmake_build_ext(build_ext):
8376
user_options = [
8477
('with-boost', None, 'Use Boost Libraries (OFF)'),
8578
('with-gsl', None, 'Use Gnu Scienfific Library (ON)'),
8679
('with-gsl-static', None, 'Use GNU Scientific Library (static library) (OFF)'),
8780
('debug', None, 'Build moose in debugging mode (OFF)'),
8881
('no-build', None, 'DO NOT BUILD. (for debugging/development)'),
89-
] + _build_ext.user_options
82+
] + build_ext.user_options
9083

9184
def initialize_options(self):
9285
# Initialize options.
@@ -97,12 +90,12 @@ def initialize_options(self):
9790
self.no_build = 0
9891
self.cmake_options = {}
9992
# super().initialize_options()
100-
_build_ext.initialize_options(self)
93+
build_ext.initialize_options(self)
10194

10295
def finalize_options(self):
10396
# Finalize options.
10497
# super().finalize_options()
105-
_build_ext.finalize_options(self)
98+
build_ext.finalize_options(self)
10699
self.cmake_options['PYTHON_EXECUTABLE'] = os.path.realpath(sys.executable)
107100
self.cmake_options['VERSION_MOOSE'] = version_
108101
if self.with_boost:
@@ -122,7 +115,7 @@ def run(self):
122115
for ext in self.extensions:
123116
self.build_cmake(ext)
124117
# super().run()
125-
_build_ext.run(self)
118+
build_ext.run(self)
126119

127120
def build_cmake(self, ext):
128121
global numCores_
@@ -175,7 +168,7 @@ def build_cmake(self, ext):
175168
]
176169
},
177170
install_requires=['numpy', 'matplotlib', 'vpython', 'pybind11'],
178-
extra_requires={'dev': ['coverage', 'pytest', 'pytest-cov']},
179-
ext_modules=[CMakeExtension('dummy', optional=True)],
180-
cmdclass={'build_ext': build_ext, 'test': TestCommand},
171+
extra_require={'dev': ['coverage', 'pytest', 'pytest-cov']},
172+
ext_modules=[CMakeExtension('_moose', optional=True)],
173+
cmdclass={'build_ext': cmake_build_ext, 'test': TestCommand},
181174
)

0 commit comments

Comments
 (0)