Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for cargo subprojects #11856

Merged
merged 11 commits into from
Oct 10, 2023
21 changes: 21 additions & 0 deletions docs/markdown/Wrap-dependency-system-manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ previously reserved to `wrap-file`:
Supported methods:
- `meson` requires `meson.build` file.
- `cmake` requires `CMakeLists.txt` file. [See details](#cmake-wraps).
- `cargo` requires `Cargo.toml` file. [See details](#cargo-wraps).

### Specific to wrap-file
- `source_url` - download url to retrieve the wrap-file source archive
Expand Down Expand Up @@ -313,6 +314,26 @@ method = cmake
[provide]
foo-bar-1.0 = foo_bar_dep
```
### Cargo wraps

Cargo subprojects automatically override the `<package_name>-rs` dependency name.
`package_name` is defined in `[package] name = ...` section of the `Cargo.toml`
and `-rs` suffix is added. That means the `.wrap` file should have
`dependency_names = foo-rs` in their `[provide]` section when `Cargo.toml` has
package name `foo`.

Cargo subprojects require a toml parser. Python >= 3.11 have one built-in, older
Python versions require either the external `tomli` module or `toml2json` program.

For example, a Cargo project with the package name `foo-bar` would have a wrap
file like that:
```ini
[wrap-file]
...
method = cargo
[provide]
dependency_names = foo-bar-rs
```
xclaesse marked this conversation as resolved.
Show resolved Hide resolved

## Using wrapped projects

Expand Down
3 changes: 2 additions & 1 deletion docs/markdown/snippets/wrap.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## Automatic fallback to `cmake` subproject
## Automatic fallback to `cmake` and `cargo` subproject

CMake subprojects have been supported for a while using the `cmake.subproject()`
module method. However until now it was not possible to use a CMake subproject
Expand All @@ -10,3 +10,4 @@ key in the wrap file's first section. The method defaults to `meson`.
Supported methods:
- `meson` requires `meson.build` file.
- `cmake` requires `CMakeLists.txt` file. [See details](Wrap-dependency-system-manual.md#cmake-wraps).
- `cargo` requires `Cargo.toml` file. [See details](Wrap-dependency-system-manual.md#cargo-wraps).
5 changes: 5 additions & 0 deletions mesonbuild/cargo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
__all__ = [
'interpret'
]

from .interpreter import interpret
229 changes: 54 additions & 175 deletions mesonbuild/cargo/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,215 +17,79 @@
import builtins


def _token(tid: str, filename: str, value: mparser.TV_TokenTypes) -> mparser.Token[mparser.TV_TokenTypes]:
"""Create a Token object, but with the line numbers stubbed out.

:param tid: the token id (such as string, number, etc)
:param filename: the filename that the token was generated from
:param value: the value of the token
:return: A Token object
"""
return mparser.Token(tid, filename, -1, -1, -1, (-1, -1), value)


def _symbol(filename: str, val: str) -> mparser.SymbolNode:
return mparser.SymbolNode(_token('', filename, val))


def string(value: str, filename: str) -> mparser.StringNode:
"""Build A StringNode

:param value: the value of the string
:param filename: the file that the value came from
:return: A StringNode
"""
return mparser.StringNode(_token('string', filename, value))


def number(value: int, filename: str) -> mparser.NumberNode:
"""Build A NumberNode

:param value: the value of the number
:param filename: the file that the value came from
:return: A NumberNode
"""
return mparser.NumberNode(_token('number', filename, str(value)))


def bool(value: builtins.bool, filename: str) -> mparser.BooleanNode:
"""Build A BooleanNode

:param value: the value of the boolean
:param filename: the file that the value came from
:return: A BooleanNode
"""
return mparser.BooleanNode(_token('bool', filename, value))


def array(value: T.List[mparser.BaseNode], filename: str) -> mparser.ArrayNode:
"""Build an Array Node

:param value: A list of nodes to insert into the array
:param filename: The file the array is from
:return: An ArrayNode built from the arguments
"""
args = mparser.ArgumentNode(_token('array', filename, 'unused'))
args.arguments = value
return mparser.ArrayNode(_symbol(filename, '['), args, _symbol(filename, ']'))


def identifier(value: str, filename: str) -> mparser.IdNode:
"""Build A IdNode

:param value: the value of the boolean
:param filename: the file that the value came from
:return: A BooleanNode
"""
return mparser.IdNode(_token('id', filename, value))


def method(name: str, id_: mparser.IdNode,
pos: T.Optional[T.List[mparser.BaseNode]] = None,
kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
) -> mparser.MethodNode:
"""Create a method call.

:param name: the name of the method
:param id_: the object to call the method of
:param pos: a list of positional arguments, defaults to None
:param kw: a dictionary of keyword arguments, defaults to None
:return: a method call object
"""
args = mparser.ArgumentNode(_token('array', id_.filename, 'unused'))
if pos is not None:
args.arguments = pos
if kw is not None:
args.kwargs = {identifier(k, id_.filename): v for k, v in kw.items()}
return mparser.MethodNode(id_, _symbol(id_.filename, '.'), identifier(name, id_.filename), _symbol(id_.filename, '('), args, _symbol(id_.filename, ')'))


def function(name: str, filename: str,
pos: T.Optional[T.List[mparser.BaseNode]] = None,
kw: T.Optional[T.Mapping[str, mparser.BaseNode]] = None,
) -> mparser.FunctionNode:
"""Create a function call.

:param name: the name of the function
:param filename: The name of the current file being evaluated
:param pos: a list of positional arguments, defaults to None
:param kw: a dictionary of keyword arguments, defaults to None
:return: a method call object
"""
args = mparser.ArgumentNode(_token('array', filename, 'unused'))
if pos is not None:
args.arguments = pos
if kw is not None:
args.kwargs = {identifier(k, filename): v for k, v in kw.items()}
return mparser.FunctionNode(identifier(name, filename), _symbol(filename, '('), args, _symbol(filename, ')'))


def equal(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.ComparisonNode:
"""Create an equality operation

:param lhs: The left hand side of the equal
:param rhs: the right hand side of the equal
:return: A compraison node
"""
return mparser.ComparisonNode('==', lhs, _symbol(lhs.filename, '=='), rhs)


def or_(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
"""Create and OrNode

:param lhs: The Left of the Node
:param rhs: The Right of the Node
:return: The OrNode
"""
return mparser.OrNode(lhs, _symbol(lhs.filename, 'or'), rhs)


def and_(lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.AndNode:
"""Create an AndNode

:param lhs: The left of the And
:param rhs: The right of the And
:return: The AndNode
"""
return mparser.AndNode(lhs, _symbol(lhs.filename, 'and'), rhs)


def not_(value: mparser.BaseNode, filename: str) -> mparser.NotNode:
"""Create a not node

:param value: The value to negate
:param filename: the string filename
:return: The NotNode
"""
return mparser.NotNode(_token('not', filename, ''), _symbol(filename, 'not'), value)


def assign(value: mparser.BaseNode, varname: str, filename: str) -> mparser.AssignmentNode:
"""Create an AssignmentNode

:param value: The rvalue
:param varname: The lvalue
:param filename: The filename
:return: An AssignmentNode
"""
return mparser.AssignmentNode(identifier(varname, filename), _symbol(filename, '='), value)


def block(filename: str) -> mparser.CodeBlockNode:
return mparser.CodeBlockNode(_token('node', filename, ''))


@dataclasses.dataclass
class Builder:

filename: str

def _token(self, tid: str, value: mparser.TV_TokenTypes) -> mparser.Token[mparser.TV_TokenTypes]:
"""Create a Token object, but with the line numbers stubbed out.

:param tid: the token id (such as string, number, etc)
:param filename: the filename that the token was generated from
:param value: the value of the token
:return: A Token object
"""
return mparser.Token(tid, self.filename, -1, -1, -1, (-1, -1), value)

def _symbol(self, val: str) -> mparser.SymbolNode:
return mparser.SymbolNode(self._token('', val))

def assign(self, value: mparser.BaseNode, varname: str) -> mparser.AssignmentNode:
return assign(value, varname, self.filename)
return mparser.AssignmentNode(self.identifier(varname), self._symbol('='), value)

def string(self, value: str) -> mparser.StringNode:
"""Build A StringNode

:param value: the value of the string
:return: A StringNode
"""
return string(value, self.filename)
return mparser.StringNode(self._token('string', value))

def number(self, value: int) -> mparser.NumberNode:
"""Build A NumberNode

:param value: the value of the number
:return: A NumberNode
"""
return number(value, self.filename)
return mparser.NumberNode(self._token('number', str(value)))

def bool(self, value: builtins.bool) -> mparser.BooleanNode:
"""Build A BooleanNode

:param value: the value of the boolean
:return: A BooleanNode
"""
return bool(value, self.filename)
return mparser.BooleanNode(self._token('bool', value))

def array(self, value: T.List[mparser.BaseNode]) -> mparser.ArrayNode:
"""Build an Array Node

:param value: A list of nodes to insert into the array
:return: An ArrayNode built from the arguments
"""
return array(value, self.filename)
args = mparser.ArgumentNode(self._token('array', 'unused'))
args.arguments = value
return mparser.ArrayNode(self._symbol('['), args, self._symbol(']'))

def dict(self, value: T.Dict[mparser.BaseNode, mparser.BaseNode]) -> mparser.DictNode:
"""Build an Dictionary Node

:param value: A dict of nodes to insert into the dictionary
:return: An DictNode built from the arguments
"""
args = mparser.ArgumentNode(self._token('dict', 'unused'))
for key, val in value.items():
args.set_kwarg_no_check(key, val)
return mparser.DictNode(self._symbol('{'), args, self._symbol('}'))

def identifier(self, value: str) -> mparser.IdNode:
"""Build A IdNode

:param value: the value of the boolean
:return: A BooleanNode
"""
return identifier(value, self.filename)
return mparser.IdNode(self._token('id', value))

def method(self, name: str, id_: mparser.IdNode,
pos: T.Optional[T.List[mparser.BaseNode]] = None,
Expand All @@ -239,7 +103,12 @@ def method(self, name: str, id_: mparser.IdNode,
:param kw: a dictionary of keyword arguments, defaults to None
:return: a method call object
"""
return method(name, id_, pos or [], kw or {})
args = mparser.ArgumentNode(self._token('array', 'unused'))
if pos is not None:
args.arguments = pos
if kw is not None:
args.kwargs = {self.identifier(k): v for k, v in kw.items()}
return mparser.MethodNode(id_, self._symbol('.'), self.identifier(name), self._symbol('('), args, self._symbol(')'))

def function(self, name: str,
pos: T.Optional[T.List[mparser.BaseNode]] = None,
Expand All @@ -252,7 +121,12 @@ def function(self, name: str,
:param kw: a dictionary of keyword arguments, defaults to None
:return: a method call object
"""
return function(name, self.filename, pos or [], kw or {})
args = mparser.ArgumentNode(self._token('array', 'unused'))
if pos is not None:
args.arguments = pos
if kw is not None:
args.kwargs = {self.identifier(k): v for k, v in kw.items()}
return mparser.FunctionNode(self.identifier(name), self._symbol('('), args, self._symbol(')'))

def equal(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.ComparisonNode:
"""Create an equality operation
Expand All @@ -261,7 +135,7 @@ def equal(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.Compari
:param rhs: the right hand side of the equal
:return: A compraison node
"""
return equal(lhs, rhs)
return mparser.ComparisonNode('==', lhs, self._symbol('=='), rhs)

def or_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
"""Create and OrNode
Expand All @@ -270,7 +144,7 @@ def or_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.OrNode:
:param rhs: The Right of the Node
:return: The OrNode
"""
return or_(lhs, rhs)
return mparser.OrNode(lhs, self._symbol('or'), rhs)

def and_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.AndNode:
"""Create an AndNode
Expand All @@ -279,12 +153,17 @@ def and_(self, lhs: mparser.BaseNode, rhs: mparser.BaseNode) -> mparser.AndNode:
:param rhs: The right of the And
:return: The AndNode
"""
return and_(lhs, rhs)
return mparser.AndNode(lhs, self._symbol('and'), rhs)

def not_(self, value: mparser.BaseNode, filename: str) -> mparser.NotNode:
def not_(self, value: mparser.BaseNode) -> mparser.NotNode:
"""Create a not node

:param value: The value to negate
:return: The NotNode
"""
return not_(value, self.filename)
return mparser.NotNode(self._token('not', ''), self._symbol('not'), value)

def block(self, lines: T.List[mparser.BaseNode]) -> mparser.CodeBlockNode:
block = mparser.CodeBlockNode(self._token('node', ''))
block.lines = lines
return block
Loading
Loading