Skip to main content

Modson

Modson is a lightweight Python wrapper around Meson that enables dynamic loading of custom Meson modules via Python entry points.

It is designed for advanced and complex build systems where extending Meson with external modules is required.

Installation

From PyPI:

pip install modson

From Source:

git clone https://github.com/alex-bouget/modson.git
cd modson
pip install .

Usage

After installation, you can use modson as a drop-in replacement for the meson command. It will automatically load any custom Meson modules registered via Python entry points.

meson setup builddir
# replaced by
modson setup builddir

How it works

Modson scans for Python packages that declare Meson modules in their setup.py or pyproject.toml files using the modson.modules entry point group. When you run a Meson command via Modson, and the Meson build system attempts to import a module, Modson intercepts the import call and checks if the requested module is available in the registered entry points. If found, it loads the module dynamically.

For this to work, modson overrides the importlib.import_module function at runtime to redirect module import requests to its custom loader. This allows for seamless integration of external modules into the Meson build process.

Create a custom Meson module

To create a custom Meson module, you need to define a Python package that includes your module and registers it under the modson.modules entry point group. This typically involves creating a setup.py file that specifies the entry points and the module's location.

# example_module/__init__.py

# This code is just an example of a Meson module definition.
# It may not be functional as-is.
# for more examples, refer to the folder mesonbuild/modules on the Meson repository.

from mesonbuild import mesonlib
from mesonbuild.modules import ModuleReturnValue, NewExtensionModule, ModuleInfo, ModuleState
from mesonbuild.interpreterbase.decorators import KwargInfo, typed_pos_args, typed_kwargs

class ExampleModule(NewExtensionModule):
    INFO = ModuleInfo('example', '0.1.0', 'Example module', unstable=True)

    def __init__(self):
        super().__init__(self.INFO)
        self.methods.update({
            'example_method': self.example_method,
        })
    
     @typed_pos_args(
        'example.example_method',
        varargs=(str, mesonlib.File),
        min_varargs=2,
    )
    @typed_kwargs(
        'example.example_method',
        # KwargInfo('files', ContainerTypeInfo(list, (str, mesonlib.File)), default=[], listify=True, required=True),
        KwargInfo('source', (str, int, NoneType), default=None),
        KwargInfo('target', (str, int, NoneType), default=None),
        KwargInfo('bootclasspath', (str, NoneType), default=None),
    )
    def example_method(self, state: ModuleState, args: Tuple[List[mesonlib.FileOrString]],
                       kwargs: Dict[str, Optional[str]]) -> ModuleReturnValue:
        return ModuleReturnValue(None, [])

def initialize(*args: Any, **kwargs: Any) -> JavaModule:
    return ExampleModule(*args, **kwargs)
from setuptools import setup
from pathlib import Path


setup(
    name="modson-example-module",
    version="0.1.0",
    packages=["example_module"],
    entry_points={
        "modson.modules": [
            "example = example_module",
        ],
    },
)

Usage out of the box

The runtime can be used as a standalone library to embed modson capabilities into other Python applications (fake a file with an entry group in a package for example).

from modson import ModsonRuntime

runtime = ModsonRuntime(
    group_name="modson.modules"  # Optional, defaults to "modson.modules"
    regex_pattern=r"^mesonbuild\.modules\.(.+)"  # Optional, defaults to "^mesonbuild\.modules\.(.+)"
    # the first capturing group should match the module name from an entry point
)
runtime.initialized_runtime()

# Do whatever you need with the modson runtime active

runtime.stop_runtime()  # Is also called automatically when the runtime is destructed (Some bad things may happen if multiple runtimes are active simultaneously, check the source code for more details and feel free to modify it to suit your needs)

Contributing

Contributions are welcome! Please feel free to open issues or submit pull requests on the GitHub repository.

Metadata

Release files for modson 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for modson 1.0.0
File Size Uploaded
modson-1.0.0.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for modson 1.0.0
File Interpreter ABI Platform
modson-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.7 kB

Release files / modson-1.0.0.tar.gz

Download URL modson-1.0.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4f4e59c8a6d5da18fe92aed7de66527cab6213a6347861168bce794634d6d8f9
BLAKE2b-256 checksum
How to use checksums
cabd99c200f98e82b1cdf5e6c5bb22d3ecb57fbc4ee01975914ad17ef0c01047
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / modson-1.0.0-py3-none-any.whl

Download URL modson-1.0.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e016726f2a5cf9ea3b3ba22071154f3237e61a53b16dd9b697eb53b2e1c9d019
BLAKE2b-256 checksum
How to use checksums
acc488ada3731bfc737a90da6477288dfc94c55e3f53723c89c59cef845f8d30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page