[build2] Python Wrapper with Pybind11, Calling Python, and Renaming Libraries

Boris Kolpackov boris at codesynthesis.com
Tue Mar 23 10:56:46 UTC 2021


Markus Pawellek via users <users at build2.org> writes:

> To be able to compile a shared library which can be used by Python,
> one needs to access environment variables
> of the currently used Python environment.
> This can easily be done by calling the used Python executable
> on the command line with certain parameters
> and saving its output in appropriate configuration variables.
> In build2 alone, this seemed not to be possible for me
> and therefore I have introduced another layer of indirection by using a Makefile.
> The Makefile then calls the Python executable
> and bdep to set the configuration variables.
> Afterwards the project can be built by build2 and installed by Pip.
> Is there a way to call the Python executable from inside build2
> and use its output directly to set some configuration variables?

There are the $process.run()[1] and $process.run_regex()[2] functions
that I think should allow you to achieve what you want. We use[3] the
former in ODB for something similar (extract the GCC plugin directory).

In your case I would probably use the extracted values as defaults for
the relevant config.pxart_python.* variables.


> Furthermore, shared libraries that can be called by Python
> use a different naming scheme and extension than typical shared libraries.
> There was no possibility of changing
> the suffix or naming scheme of a library target.
> Therefore I have defined a new file type with the appropriate requirements
> and copied libraries to the new type by using inline build recipes with cp.
> Is there a better way of renaming libraries?

Yes, turns out we again do something similar in ODB[4]. In your case it
could look along these lines:

define pylib: libs

pylib{*}:
{
  extension = ($cxx.target.class == 'windows' ? pyd : so)
  bin.lib.prefix = # No lib prefix.
  backlink = true  # Backlink in forwarded configs.
}

pylib{hello}: {hxx ixx txx cxx}{**}

Note that since pylib{} is derived from libs{}, the standard link rule
will match and build it.

[1] https://github.com/build2/build2/blob/master/libbuild2/functions-process.cxx#L413
[2] https://github.com/build2/build2/blob/master/libbuild2/functions-process.cxx#L429
[3] https://git.codesynthesis.com/cgit/odb/odb/tree/build/root.build#n19
[4] https://git.codesynthesis.com/cgit/odb/odb/tree/odb/buildfile#n4



More information about the users mailing list