Skip to content

Commit

Permalink
MAINT: updated meson for numpy 2
Browse files Browse the repository at this point in the history
Added a detection and switch for numpy 2.
  • Loading branch information
aburrell committed Dec 23, 2024
1 parent 1ba4b6c commit 448c62d
Showing 1 changed file with 31 additions and 12 deletions.
43 changes: 31 additions & 12 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,23 @@ add_languages('fortran', native: false)
fc = meson.get_compiler('fortran')
cc = meson.get_compiler('c')

# https://mesonbuild.com/Python-module.html
py_mod = import('python')
py3 = py_mod.find_installation()
py3_dep = py3.dependency()
message(py3.full_path())
message(py3.get_install_dir())

# Determine whether this is being compiled with numpy 1.X or 2.X
numpy_ver = run_command(py3,
['-c', 'import numpy; print(numpy.__version__.split(".")[0])'],
check : true
).stdout().strip()
is_numpytwo = numpy_ver.to_int() >= 2

message('The Numpy major version is: ', numpy_ver)


# Don't use the deprecated NumPy C API. Define this to a fixed version instead
# of NPY_API_VERSION in order not to break compilation for released versions
# when NumPy introduces a new deprecation. Use in a meson.build file::
Expand All @@ -28,7 +45,12 @@ cc = meson.get_compiler('c')
# 'source_fname',
# numpy_nodepr_api)
#
numpy_nodepr_api = '-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION'
if is_numpytwo
numpy_nodepr_api = '-DNPY_NO_DEPRECATED_API=NPY_2_0_API_VERSION'
else
numpy_nodepr_api = '-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION'
endif


# This argument is called -Wno-unused-but-set-variable by GCC, however Clang
# doesn't recognize that.
Expand All @@ -45,13 +67,6 @@ endif
# Add more link arguments
add_project_link_arguments('-lquadmath', language: ['c', 'fortran'])

# https://mesonbuild.com/Python-module.html
py_mod = import('python')
py3 = py_mod.find_installation()
py3_dep = py3.dependency()
message(py3.full_path())
message(py3.get_install_dir())

incdir_numpy = run_command(py3,
['-c', 'import os; os.chdir(".."); import numpy; print(numpy.get_include())'],
check : true
Expand All @@ -64,15 +79,19 @@ incdir_f2py = run_command(py3,

inc_dirs = include_directories(incdir_numpy, incdir_f2py)

# Don't use the deprecated NumPy C API. Define this to a fixed version instead of
# NPY_API_VERSION in order not to break compilation for released SciPy versions
# when NumPy introduces a new deprecation. Use in a meson.build file::
# Don't use the deprecated NumPy C API. Define this to a fixed version instead
# of NPY_API_VERSION in order not to break compilation for released SciPy
# versions when NumPy introduces a new deprecation. Use in a meson.build file::
#
# py3.extension_module('_name',
# 'source_fname',
# numpy_nodepr_api)
#
c_flags = ['-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION']
if is_numpytwo
c_flags = ['-DNPY_NO_DEPRECATED_API=NPY_2_0_API_VERSION']
else
c_flags = ['-DNPY_NO_DEPRECATED_API=NPY_1_9_API_VERSION']
endif

# Platform detection to set more flags for Windows systems
is_windows = host_machine.system() == 'windows'
Expand Down

0 comments on commit 448c62d

Please sign in to comment.