Skip to content

Commit

Permalink
numpy: build and test the full NumPy package
Browse files Browse the repository at this point in the history
  • Loading branch information
valgur committed Mar 21, 2024
1 parent 35957f7 commit b7af0d6
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 163 deletions.
5 changes: 0 additions & 5 deletions recipes/numpy/all/conandata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,3 @@ sources:
"1.26.4":
url: "https://github.com/numpy/numpy/releases/download/v1.26.4/numpy-1.26.4.tar.gz"
sha256: "2a02aba9ed12e4ac4eb3ea9421c420301a0c6460d9830d74a9df87efa4912010"
patches:
"1.26.4":
- patch_file: "patches/1.26.4-patch-meson.patch"
patch_description: "Build only the core C library"
patch_type: "conan"
48 changes: 33 additions & 15 deletions recipes/numpy/all/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import textwrap

from conan import ConanFile
from conan.tools.apple import fix_apple_shared_install_name
from conan.tools.env import Environment, VirtualBuildEnv
from conan.tools.files import copy, export_conandata_patches, get, apply_conandata_patches, rmdir, move_folder_contents, mkdir, save
from conan.tools.files import copy, export_conandata_patches, get, apply_conandata_patches, save
from conan.tools.gnu import PkgConfigDeps
from conan.tools.layout import basic_layout
from conan.tools.meson import Meson, MesonToolchain
Expand All @@ -18,8 +17,8 @@ class NumpyConan(ConanFile):
description = "NumPy is the fundamental package for scientific computing with Python."
license = "BSD 3-Clause"
url = "https://github.com/conan-io/conan-center-index"
homepage = "https://numpy.org/doc/stable/reference/c-api/coremath.html"
topics = ("ndarray", "array", "linear algebra", "npymath", "npyrandom")
homepage = "https://numpy.org/devdocs/reference/c-api/index.html"
topics = ("ndarray", "array", "linear algebra", "npymath")

package_type = "static-library"
settings = "os", "arch", "compiler", "build_type"
Expand Down Expand Up @@ -60,14 +59,20 @@ def source(self):
def _meson_root(self):
return self.source_path.joinpath("vendored-meson", "meson")

@property
def _build_site_packages(self):
return os.path.join(self.build_folder, "site-packages")

def generate(self):
venv = VirtualBuildEnv(self)
venv.generate()

# NumPy can only be built with its vendored Meson
env = Environment()
# NumPy can only be built with its vendored Meson
env.prepend_path("PATH", str(self._meson_root))
env.vars(self).save_script("conanbuild_meson")
env.prepend_path("PYTHONPATH", self._build_site_packages)
env.prepend_path("PATH", os.path.join(self._build_site_packages, "bin"))
env.vars(self).save_script("conanbuild_paths")

tc = MesonToolchain(self)
tc.project_options["allow-noblas"] = False
Expand Down Expand Up @@ -100,6 +105,9 @@ def _patch_sources(self):
CALL python %~dp0/meson.py %*
"""))

# Install cython
self.run(f"python -m pip install cython --no-cache-dir --target {self._build_site_packages}")

def build(self):
self._patch_sources()
meson = Meson(self)
Expand All @@ -110,18 +118,28 @@ def package(self):
copy(self, "LICENSE*.txt", self.source_folder, os.path.join(self.package_folder, "licenses"))
meson = Meson(self)
meson.install()

@property
def _rel_site_packages(self):
python_minor = Version(self.dependencies["cpython"].ref.version).minor
python_lib = os.path.join(self.package_folder, "lib", f"python3.{python_minor}")
pkg_root = os.path.join(python_lib, "site-packages", "numpy")
copy(self, "*.a", pkg_root, os.path.join(self.package_folder, "lib"), keep_path=False)
mkdir(self, os.path.join(self.package_folder, "include"))
move_folder_contents(self, os.path.join(pkg_root, "core", "include"), os.path.join(self.package_folder, "include"))
rmdir(self, python_lib)
fix_apple_shared_install_name(self)
return os.path.join("lib", f"python3.{python_minor}", "site-packages")

@property
def _rel_pkg_root(self):
return os.path.join(self._rel_site_packages, "numpy")

def package_info(self):
self.cpp_info.components["npymath"].set_property("pkg_config_name", "npymath")
self.cpp_info.components["npymath"].libs = ["npymath", "npyrandom"]
self.cpp_info.components["npymath"].requires = ["openblas::openblas", "cpython::cpython"]
self.cpp_info.components["npymath"].libs = ["npymath"]
self.cpp_info.components["npymath"].libdirs = [
os.path.join(self._rel_pkg_root, "core", "lib"),
os.path.join(self._rel_pkg_root, "random", "lib"),
]
self.cpp_info.components["npymath"].includedirs = [os.path.join(self._rel_pkg_root, "core", "include")]
if self.settings.os in ["Linux", "FreeBSD"]:
self.cpp_info.components["npymath"].system_libs = ["m"]

self.cpp_info.components["numpy"].libdirs = [os.path.join(self._rel_pkg_root, "core")]
self.cpp_info.components["numpy"].requires = ["openblas::openblas", "cpython::cpython", "npymath"]

self.runenv_info.prepend_path("PYTHONPATH", os.path.join(self.package_folder, self._rel_site_packages))
132 changes: 0 additions & 132 deletions recipes/numpy/all/patches/1.26.4-patch-meson.patch

This file was deleted.

5 changes: 2 additions & 3 deletions recipes/numpy/all/test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from conan.tools.build import can_run
from conan.tools.cmake import cmake_layout
from conan.tools.meson import Meson
import os


class TestPackageConan(ConanFile):
Expand All @@ -28,5 +27,5 @@ def build(self):

def test(self):
if can_run(self):
bin_path = os.path.join(self.cpp.build.bindir, "test_package")
self.run(bin_path, env="conanrun")
self.run('python -c "import test_package; print(test_package.create_numpy_array())"',
env="conanrun", cwd=self.cpp.build.bindir)
9 changes: 5 additions & 4 deletions recipes/numpy/all/test_package/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
project('test_package', 'c')
package_dep = dependency('npymath')
executable('test_package',
sources : ['test_package.c'],
dependencies : [package_dep])
package_dep = dependency('numpy')
shared_library('test_package',
sources : ['test_package.c'],
dependencies : [package_dep],
name_prefix: '')
25 changes: 21 additions & 4 deletions recipes/numpy/all/test_package/test_package.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
// Based on https://github.com/numpy/numpy/blob/v1.26.4/numpy/core/tests/examples/limited_api/limited_api.c

#define Py_LIMITED_API 0x03060000
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION

#include <Python.h>
#include <numpy/arrayobject.h>
#include <numpy/ufuncobject.h>

static PyObject* create_numpy_array(PyObject *self, PyObject *args) {
npy_intp dims[2] = {5, 5};
PyObject *pyArray = PyArray_SimpleNew(2, dims, NPY_FLOAT64);

for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
*((double *)PyArray_GETPTR2(pyArray, i, j)) = (double)i * j;
}
}

return pyArray;
}

static PyMethodDef ExampleMethods[] = {
{"create_numpy_array", create_numpy_array, METH_VARARGS, "Create a 2D NumPy array."},
{NULL, NULL, 0, NULL}
};

static PyModuleDef moduledef = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "limited_api"
.m_name = "test_package",
.m_methods = ExampleMethods,
};

PyMODINIT_FUNC PyInit_limited_api(void)
PyMODINIT_FUNC PyInit_test_package(void)
{
import_array();
import_umath();
Expand Down

0 comments on commit b7af0d6

Please sign in to comment.