Skip to content

Commit

Permalink
Merge pull request #61 from PaulSt/pip_mac2
Browse files Browse the repository at this point in the history
pip for mac
  • Loading branch information
PaulSt authored Jun 25, 2023
2 parents 66dbafb + ddc7a5e commit 1b01c06
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 105 deletions.
64 changes: 42 additions & 22 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env:
BUILD_TYPE: Debug

jobs:

maketest:
runs-on: ubuntu-latest
container:
Expand Down Expand Up @@ -65,33 +66,52 @@ jobs:
packages_dir: NGSTrefftz/wheelhouse/
#repository_url: https://test.pypi.org/legacy/

pypi-src:

pypi-mac:
name: Build wheels for macOS
needs: [maketest]
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'created'
strategy:
matrix:
py: ['3.8', '3.9', '3.10', '3.11']
runs-on: macos-11
env:
MACOSX_DEPLOYMENT_TARGET: '10.15'
steps:
- uses: actions/checkout@v3
- name: Checkout
uses: actions/checkout@v3
with:
submodules: 'True'
fetch-depth: 0
- name: install pkgs
run: |
sudo apt-get update && DEBIAN_FRONTEND="noninteractive" sudo apt-get -y install vim python3 python3-pip python3-distutils python3-tk libpython3-dev libxmu-dev tk-dev tcl-dev cmake git g++ libglu1-mesa-dev libblas-dev liblapack-dev
- name: Update pip and Install setuptools
run: |
python3 -m pip install --upgrade pip
python3 -m pip install -U pytest-check numpy wheel scikit-build mkl==2022.* mkl-devel==2022.* setuptools
python3 -m pip install ngsolve --pre
- name: Package ngstrefftz sources for PyPI
run: python3 setup.py sdist
- name: pip install ngstrefftz
run: pip install dist/*
- name: Publish source distribution to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
if: github.event_name == 'release' && github.event.action == 'created' #startsWith(github.ref, 'refs/tags/v') # && github.event_name == 'push'
submodules: recursive
path: 'NGSTrefftz'
- name: Get Python ${{ matrix.py }}
uses: actions/setup-python@v4
with:
skip_existing: true
user: __token__
password: ${{ secrets.PYPI_API_TOKEN }}
python-version: ${{ matrix.py }}
- name: Run build script
run: cd NGSTrefftz/.github/workflows && bash build_pip_mac.sh ${{ matrix.py }}
- name: Store the binary wheel
uses: actions/upload-artifact@v2
with:
name: python-package-distributions
path: NGSTrefftz/dist
publish-pypi-mac:
name: Publish to PyPI
needs: pypi-mac
runs-on: ubuntu-latest
steps:
- name: Download all the dists
uses: actions/download-artifact@v2
with:
name: python-package-distributions
path: NGSTrefftz/dist/
- name: Publish binary distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
skip_existing: true
password: ${{ secrets.PYPI_API_TOKEN }}
packages_dir: NGSTrefftz/dist/
#repository_url: https://test.pypi.org/legacy/

docker:
needs: [maketest,pypi]
Expand Down
27 changes: 27 additions & 0 deletions .github/workflows/build_pip_mac.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
set -e
cd ../..
rm -rf _skbuild dist venv_ngs

export PYDIR=$Python3_ROOT_DIR/bin
#export PYDIR=/Library/Frameworks/Python.framework/Versions/$1/bin

$PYDIR/python3 --version
#$PYDIR/python3 -m venv venv_ngs
#source venv_ngs/bin/activate

export PATH=/Applications/CMake.app/Contents/bin:$PATH
export NETGEN_Dir=$PYDIR/../lib/python$1/site-packages/netgen/cmake
export NGSolve_Dir=$PYDIR/../lib/python$1/site-packages/ngsolve/cmake
export CMAKE_PREFIX_PATH=$CMAKE_PREFIX_PATH:$NGSolve_Dir:$NETGEN_Dir
#export PYDIR=/Library/Frameworks/Python.framework/Versions/$1/bin
#$PYDIR/python3 -m venv ../venv_ngs
#source ../venv_ngs/bin/activate
$PYDIR/pip3 install scikit-build wheel
#$PYDIR/pip3 install -U pytest-check numpy wheel scikit-build mkl==2022.* mkl-devel==2022.* setuptools

export CMAKE_OSX_ARCHITECTURES='arm64;x86_64'
#export CMAKE_OSX_ARCHITECTURES='x86_64'
$PYDIR/pip3 install ngsolve

#$PYDIR/pip3 wheel .
$PYDIR/python3 setup.py bdist_wheel --plat-name macosx-10.15-universal2 -d dist
145 changes: 62 additions & 83 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import glob
import os
import re
import sys
import platform
import subprocess
import warnings
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from distutils.version import LooseVersion
# import netgen.version
import site

from skbuild import setup
import skbuild.cmaker
from subprocess import check_output
from distutils.sysconfig import get_python_lib
import pkg_resources

from os.path import dirname, isdir, join
import os
Expand Down Expand Up @@ -66,91 +68,68 @@ def get_version():
return version


class CMakeExtension(Extension):
def __init__(self, name, sourcedir=''):
Extension.__init__(self, name, sources=[])
self.sourcedir = os.path.abspath(sourcedir)
class CMakeBuild(build_ext):
def run(self):
try:
out = subprocess.check_output(['cmake', '--version'])
except OSError:
raise RuntimeError("CMake must be installed to build the following extensions: " +
", ".join(e.name for e in self.extensions))
if "NETGENDIR" not in os.environ:
warnings.warn("Could not find NETGENDIR")
try:
import ngsolve
except:
raise RuntimeError("Could not run NGSolve, is it installed? Did you set pythonpath correctly?")

for ext in self.extensions:
self.build_extension(ext)
def build_extension(self, ext):
extdir = os.path.abspath(os.path.dirname(self.get_ext_fullpath(ext.name)))
# required for auto-detection of auxiliary "native" libs
if not extdir.endswith(os.path.sep):
extdir += os.path.sep
cmake_args = ['-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=' + extdir,
'-DPYTHON_EXECUTABLE=' + sys.executable,
'-DCMAKE_CXX_COMPILER=ngscxx']
if 'PYDIR' in os.environ:
cmake_args += [f'-DCMAKE_PREFIX_PATH={os.environ["PYDIR"]}/..']
cmake_args += [f'-DPYTHON_EXECUTABLE={os.environ["PYDIR"]}/python3']
cmake_args += [f'-DPYTHON_LIBRARY={os.environ["PYDIR"]}/../lib']
cmake_args += [f'-DPYTHON_INCLUDE_DIR={os.environ["PYDIR"]}/../include']
cfg = 'Debug' if self.debug else 'Release'
build_args = ['--config', cfg]
cmake_args += ['-DCMAKE_BUILD_TYPE=' + cfg]
build_args += ['--', '-j2']
env = os.environ.copy()
env['CXXFLAGS'] = '{} -DVERSION_INFO=\\"{}\\"'.format(env.get('CXXFLAGS', ''),
self.distribution.get_version())
if not os.path.exists(self.build_temp):
os.makedirs(self.build_temp)

subprocess.check_call(['cmake', ext.sourcedir] + cmake_args, cwd=self.build_temp, env=env)
subprocess.check_call(['cmake', '--build', '.'] + build_args, cwd=self.build_temp)

# subprocess.check_call(['mv', '_trefftz.so', 'ngstrefftz'], cwd=self.build_lib)
# subprocess.check_call(['mkdir', 'ngstents'], cwd=self.build_lib)
# subprocess.check_call(['mv', '_pytents.so', 'ngstents'], cwd=self.build_lib)

import netgen.version
import ngsolve
import pkg_resources
netgen_name = netgen.config.NETGEN_PYTHON_PACKAGE_NAME
# avx2 = netgen_name.replace('netgen-mesher', '') # keep -avx2 suffix
avx2 = ''
name = 'ngstrefftz' + avx2
# ngsolve_version = pkg_resources.get_distribution("ngsolve").version
# import netgen.version
# import ngsolve
# import pkg_resources
# netgen_name = netgen.config.NETGEN_PYTHON_PACKAGE_NAME
# # avx2 = netgen_name.replace('netgen-mesher', '') # keep -avx2 suffix
# avx2 = ''
# name = 'ngstrefftz' + avx2
# # ngsolve_version = pkg_resources.get_distribution("ngsolve").version
ngsolve_version = '6.2.2301'
install_requires = [ 'ngsolve'+avx2+'>='+ngsolve_version ]

if sys.argv[1] == "sdist":
package_data = {"ngstrefftz": ["*"
,"../test/*"
,"../external_dependencies/ngstents/*"\
,"../external_dependencies/ngstents/src/*"\
,"../external_dependencies/ngstents/py/*"\
]}
name += "-src"
else:
package_data = {}
install_requires = [ 'ngsolve >= '+ngsolve_version ]


# def install_filter(cmake_manifest):
# return cmake_manifest

# def _patched_parse_manifests(self):
# paths = \
# glob.glob(os.path.join(skbuild.cmaker.CMAKE_BUILD_DIR(), "ngstrefftz", "install_manifest*.txt"))
# try:
# return [self._parse_manifest(path) for path in paths][0]
# except IndexError:
# return []

# # we are using the ngsolve superbuild (to download and build some dependencies)
# # patch the parse_manifests function to point to the actual ngsolve cmake project within the superbuild
# skbuild.cmaker.CMaker._parse_manifests = _patched_parse_manifests

py_install_dir = get_python_lib(1,0,'').replace('\\','/')

# root_dir = os.path.abspath(os.path.join(netgen.__file__, '../'*(len(py_install_dir.split('/'))+2)))

_cmake_args = ['-DCMAKE_CXX_COMPILER=ngscxx']

packages=["ngstrefftz"]

if 'darwin' in sys.platform:
pass
elif 'linux' in sys.platform:
install_requires.append('mkl')
# packages = []
elif 'win' in sys.platform:
install_requires.append('mkl')

if 'PYDIR' in os.environ:
_cmake_args += [f'-DCMAKE_PREFIX_PATH={os.environ["PYDIR"]}']
_cmake_args += [f'-DPYTHON_EXECUTABLE={os.environ["PYDIR"]}/python3']
_cmake_args += [f'-DPYTHON_LIBRARY={os.environ["PYDIR"]}/../lib']
_cmake_args += [f'-DPYTHON_INCLUDE_DIR={os.environ["PYDIR"]}/../include']

setup(
name=name,
name='ngstrefftz',
version=str(get_version()),
author='Paul Stocker',
author_email='[email protected]',
description='NGSTrefftz is an add-on to NGSolve for Trefftz methods.',
long_description='NGSTrefftz provides a framework to implement Trefftz finite element spaces for NGSolve, with several Trefftz spaces already implemented. Additionally, Trefftz-DG on tent-pitched meshes for the acoustic wave equation is implemented using meshes provided by ngstents. Furthermore, the package includes an implementation of the embedded Trefftz method.',
url="https://github.com/PaulSt/ngstrefftz",
license="LGPL2.1",
install_requires=install_requires,
ext_modules=[CMakeExtension('ngstrefftz_py','src')],
cmdclass=dict(build_ext=CMakeBuild),
packages=["ngstrefftz"],
packages=packages,
package_dir={"ngstrefftz": "src"},
python_requires='>=3.8',
package_data=package_data,
# cmake_process_manifest_hook=install_filter,
cmake_args=_cmake_args,
cmake_source_dir='src',
)

0 comments on commit 1b01c06

Please sign in to comment.