Using meshsearch from another package

The wheel that pip install meshsearch installs carries the C++ library as source, for packages that build on it: the headers, the library’s sources and a CMake package, which needs CMake 3.16 or later. find_package(meshsearch) reads that package, which creates the static library target meshsearch::meshsearch in your build, from those sources, with your compiler and your build type. No library is prebuilt, so nothing has to match the compiler or the standard library the wheel was built with. Build in an optimised configuration, as on every route (install).

The target requires C++17 of whatever links it and brings the platform’s thread library. It compiles meshsearch’s sources with -ffp-contract=off and -fno-math-errno, which your own sources do not get: the first keeps distances, and with them boundary and tie decisions, the same on every platform, so your build returns the same results, in the same order, as every other build of the same version, unless those options change floating-point semantics, as -ffast-math does. The sources are part of your build, so the compile options of the directory that calls find_package, warnings as errors for one, apply to them too.

python -m meshsearch --cmake-dir prints the directory that holds the CMake package. A plain CMake project passes it as meshsearch_DIR:

cmake -S . -B build -DCMAKE_BUILD_TYPE=Release -Dmeshsearch_DIR="$(python -m meshsearch --cmake-dir)"

with a CMakeLists.txt such as this one:

cmake_minimum_required(VERSION 3.16)

project(neighbours LANGUAGES CXX)

find_package(meshsearch 1.0 CONFIG REQUIRED)

add_executable(neighbours main.cpp)
target_link_libraries(neighbours PRIVATE meshsearch::meshsearch)

A Python package built with scikit-build-core needs no setting: it lists meshsearch among its build requirements, pip installs the wheel into the isolated build environment, and scikit-build-core puts that environment’s site-packages on CMake’s search path, where find_package finds it. The pyproject.toml of a package whose extension module is written with nanobind:

[build-system]
requires = ["scikit-build-core >= 0.11", "nanobind >= 2.0", "meshsearch >= 1.0, < 2"]
build-backend = "scikit_build_core.build"

[project]
name = "paircount"
version = "0.1.0"
description = "The number of neighbours within a radius of every point"
requires-python = ">= 3.10"
dependencies = ["numpy"]

[tool.scikit-build]
minimum-version = "0.11"
wheel.packages = ["python/paircount"]

and its CMakeLists.txt:

cmake_minimum_required(VERSION 3.18)

project(${SKBUILD_PROJECT_NAME} LANGUAGES CXX)

find_package(Python 3.10 REQUIRED COMPONENTS Interpreter Development.Module)
find_package(nanobind CONFIG REQUIRED)
find_package(meshsearch 1.0 CONFIG REQUIRED)

nanobind_add_module(_core src/paircount.cpp)
target_link_libraries(_core PRIVATE meshsearch::meshsearch)

install(TARGETS _core LIBRARY DESTINATION paircount)

The complete packages, with their sources, are in examples/downstream; the module is installed into the package directory named in wheel.packages.

Two things follow from how the build environment is assembled. numpy, the wheel’s only dependency, is installed into it as well. And scikit-build-core searches the site-packages of the interpreter that runs pip before those of the build environment, so a meshsearch already installed there is the one found; the version given to find_package is still checked against it. The library compiled into your module is the version the build found.

meshsearch is linked into your module statically: it is needed to build the module, not to run it, and belongs in your dependencies only if your package also imports meshsearch. Your module holds its own copy of the library and shares no types with meshsearch’s Python module: a grid built in one cannot be passed to the other. Nor does it share meshsearch’s binding library: the CMake package brings none, so your module may use any version of nanobind, another binding library, or the C API. The CMake package stops with an error if your build already has a target named meshsearch; rename that target.

The classic CMake install, described in the README, installs a prebuilt static archive with its own CMake package, under the same target name, for projects that do not go through pip. Use the wheel’s CMake package when pip builds your package, and the classic install when your project is itself installed with CMake; the same CMakeLists.txt works with either.