Skip to content

Commit

Permalink
Merge branch 'meson'
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarter committed Jan 11, 2023
2 parents 3e78d87 + 1b2986a commit 52997cc
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 15 deletions.
18 changes: 13 additions & 5 deletions ccblade/meson.build
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
# NumPy include directory - needed in all submodules
incdir_numpy = run_command(py3,
['-c', 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'],
check : true
).stdout().strip()
incdir_numpy = get_option('incdir_numpy')
if incdir_numpy == ''
incdir_numpy = run_command(py3,
[
'-c',
'import os; os.chdir(".."); import numpy; print(numpy.get_include())'
],
check: true
).stdout().strip()
endif
# this creates a raw string which is useful for Windows use of '\' for paths
incdir_numpy = '''@0@'''.format(incdir_numpy)

#incdir_f2py = run_command(py3,
# ['-c', 'import os; os.chdir(".."); import numpy.f2py; print(numpy.f2py.get_include())'],
Expand All @@ -19,7 +27,7 @@ inc_np = include_directories(incdir_numpy, incdir_f2py)
#subdir('src')
bem_source = custom_target('bemmodule.c',
input : ['src/bem.f90'],
output : ['_bemmodule.c', '_bem-f2pywrappers.f'],
output : ['_bemmodule.c'],
command: [py3, '-m', 'numpy.f2py',
'@INPUT@', '-m', '_bem', '--lower', '--build-dir', 'ccblade']
)
Expand Down
14 changes: 8 additions & 6 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ project(
],
)

add_languages('fortran')

cc = meson.get_compiler('c')
add_languages('fortran', native: false)
fc = meson.get_compiler('fortran')
is_windows = host_machine.system() == 'windows'

# We need -lm for all C code (assuming it uses math functions, which is safe to
# assume for SciPy). For C++ it isn't needed, because libstdc++/libc++ is
Expand All @@ -23,14 +24,15 @@ if m_dep.found()
add_project_link_arguments('-lm', language : 'c')
endif

# Adding at project level causes many spurious -lgfortran flags.
add_languages('fortran', native: false)

# https://mesonbuild.com/Python-module.html
# Here we differentiate from the python used by meson, py3_command, and that python target, py3_target. This is useful
# when cross compiling like on conda-forge
py_mod = import('python')
py3 = py_mod.find_installation('python')
if get_option('python_target') != ''
py3 = py_mod.find_installation(get_option('python_target'))
else
py3 = py_mod.find_installation('python')
endif
py3_dep = py3.dependency()

message(py3.path())
Expand Down
9 changes: 9 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
option('incdir_numpy', type: 'string', value: '',
description: 'Include directory for numpy. If left empty Meson will try to find it on its own.')

option('python_target', type: 'string', value: '',
description: '''Target python path. This is used in the case that the Python installation that PyOptSparse is intended
to be built for is different than the Python installation that is used to run Meson. For example, Meson may be installed
on the user's system which is run using the system Python installation, but the user may want build PyOptSparse for
a Python installation in a virtual environment. Leave as an empty string to build for Python installation running
Meson.''')
20 changes: 16 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
# only if building in place: ``python setup.py build_ext --inplace``
import os
import re
import platform
import shutil
import setuptools
import subprocess


def run_meson_build():
def run_meson_build(staging_dir):
prefix = os.path.join(os.getcwd(), staging_dir)
purelibdir = "."

Expand All @@ -19,6 +20,12 @@ def run_meson_build():
if "MESON_ARGS" in os.environ:
meson_args = os.environ["MESON_ARGS"]

if platform.system() == "Windows":
if not "FC" in os.environ:
os.environ["FC"] = "gfortran"
if not "CC" in os.environ:
os.environ["CC"] = "gcc"

# configure
meson_path = shutil.which("meson")
meson_call = (
Expand All @@ -27,7 +34,9 @@ def run_meson_build():
)
sysargs = meson_call.split(" ")
sysargs = [arg for arg in sysargs if arg != ""]
print(sysargs)
p1 = subprocess.run(sysargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
os.makedirs(staging_dir, exist_ok=True)
setup_log = os.path.join(staging_dir, "setup.log")
with open(setup_log, "wb") as f:
f.write(p1.stdout)
Expand All @@ -39,6 +48,8 @@ def run_meson_build():
# build
meson_call = f"{meson_path} compile -vC {staging_dir}"
sysargs = meson_call.split(" ")
sysargs = [arg for arg in sysargs if arg != ""]
print(sysargs)
p2 = subprocess.run(sysargs, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
compile_log = os.path.join(staging_dir, "compile.log")
with open(compile_log, "wb") as f:
Expand Down Expand Up @@ -73,9 +84,9 @@ def copy_shared_libraries():
staging_dir = "meson_build"

# this keeps the meson build system from running more than once
if "dist" not in str(os.path.abspath(__file__)) and not os.path.isdir(staging_dir):
if "dist" not in str(os.path.abspath(__file__)):
cwd = os.getcwd()
run_meson_build()
run_meson_build(staging_dir)
os.chdir(cwd)
copy_shared_libraries()

Expand Down Expand Up @@ -106,8 +117,9 @@ def copy_shared_libraries():
extras_require={
"testing": ["pytest"],
},
python_requires=">=3.7",
python_requires=">=3.8",
packages=["ccblade"],
package_data={"": ["*.yaml", "*.so", "*.lib", "*.pyd", "*.pdb", "*.dylib", "*.dll"]},
license='Apache License, Version 2.0',
zip_safe=False,
)
Expand Down

0 comments on commit 52997cc

Please sign in to comment.