-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate rudimentary Rust mojom bindings
Create a mojom binding generator for Rust. The generated bindings are not useful yet, and only even compile for basic structs. However, this is a good starting point for iteration. A simple test in Rust is added to check that the Rect bindings build and have the expected fields. Bug: 1274864 Change-Id: I4107e26c50d2ddb36067a0238d7d2e72aeecd9d2 Cq-Include-Trybots: luci.chromium.try:android-rust-arm-dbg,android-rust-arm-rel,linux-rust-x64-dbg,linux-rust-x64-rel Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4198179 Auto-Submit: Collin Baker <collinbaker@chromium.org> Reviewed-by: Hans Wennborg <hans@chromium.org> Commit-Queue: Hans Wennborg <hans@chromium.org> Reviewed-by: Daniel Cheng <dcheng@chromium.org> Cr-Commit-Position: refs/heads/main@{#1100917}
- Loading branch information
Showing
14 changed files
with
189 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,3 +11,6 @@ pub mod encoding; | |
pub mod message; | ||
pub mod mojom; | ||
pub mod run_loop; | ||
|
||
#[cfg(test)] | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
//! Simply test that we can compile some generated Rust struct bindings and that | ||
//! they have the expected fields. | ||
|
||
// TODO(crbug.com/1274864): find a better way to reference generated bindings. | ||
// This is not sustainable, but it's sufficient for a single test while | ||
// prototyping. | ||
mod rect_mojom { | ||
include!(concat!(env!("GEN_DIR"), "/mojo/public/interfaces/bindings/tests/rect.mojom.rs")); | ||
} | ||
|
||
#[test] | ||
fn basic_struct_test() { | ||
let _rect = rect_mojom::Rect { x: 1, y: 1, width: 1, height: 1 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// Copyright 2023 The Chromium Authors | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
pub mod basic_unittest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# TypeScript bindings generator | ||
per-file mojom_ts_generator.py=rbpotter@chromium.org | ||
|
||
# Rust bindings generator | ||
per-file mojom_rust_generator.py=file://build/rust/OWNERS |
67 changes: 67 additions & 0 deletions
67
mojo/public/tools/bindings/generators/mojom_rust_generator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2023 The Chromium Authors | ||
# Use of this source code is governed by a BSD-style license that can be | ||
# found in the LICENSE file. | ||
|
||
import mojom.generate.generator as generator | ||
import mojom.generate.module as mojom | ||
import mojom.generate.pack as pack | ||
from mojom.generate.template_expander import UseJinja, UseJinjaForImportedTemplate | ||
|
||
_kind_to_rust_type = { | ||
mojom.BOOL: "bool", | ||
mojom.INT8: "i8", | ||
mojom.INT16: "i16", | ||
mojom.INT32: "i32", | ||
mojom.INT64: "i64", | ||
mojom.UINT8: "u8", | ||
mojom.UINT16: "u16", | ||
mojom.UINT32: "u32", | ||
mojom.UINT64: "u64", | ||
mojom.FLOAT: "f32", | ||
mojom.DOUBLE: "f64", | ||
} | ||
|
||
|
||
class Generator(generator.Generator): | ||
def __init__(self, *args, **kwargs): | ||
super(Generator, self).__init__(*args, **kwargs) | ||
|
||
def _GetNameForKind(self, kind): | ||
"::".join(kind.module.namespace.split(".")) | ||
|
||
def _GetRustFieldType(self, kind): | ||
if mojom.IsEnumKind(kind) or mojom.IsStructKind(kind) or mojom.IsUnionKind( | ||
kind): | ||
return self._GetNameForKind(kind) | ||
if mojom.IsArrayKind(kind): | ||
return f"Vec<{self._GetRustFieldType(kind.kind)}>" | ||
if mojom.IsMapKind(kind): | ||
return (f"HashMap<" | ||
f"{self._GetRustFieldType(kind.key_kind)}, " | ||
f"{self._GetRustFieldType(kind.value_kind)}>") | ||
if mojom.IsStringKind(kind): | ||
return "String" | ||
if mojom.IsAnyHandleKind(kind): | ||
return "::mojo::UntypedHandle" | ||
if mojom.IsReferenceKind(kind): | ||
return "usize" | ||
return _kind_to_rust_type[kind] | ||
|
||
@staticmethod | ||
def GetTemplatePrefix(): | ||
return "rust_templates" | ||
|
||
def GetFilters(self): | ||
rust_filters = { | ||
"rust_field_type": self._GetRustFieldType, | ||
} | ||
return rust_filters | ||
|
||
@UseJinja("module.tmpl") | ||
def _GenerateModule(self): | ||
return {"module": self.module} | ||
|
||
def GenerateFiles(self, args): | ||
self.module.Stylize(generator.Stylizer()) | ||
|
||
self.WriteWithComment(self._GenerateModule(), f"{self.module.path}.rs") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
file://build/rust/OWNERS |
3 changes: 3 additions & 0 deletions
3
mojo/public/tools/bindings/generators/rust_templates/module.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{%- for struct in module.structs %} | ||
{% include "struct.tmpl" %} | ||
{% endfor %} |
6 changes: 6 additions & 0 deletions
6
mojo/public/tools/bindings/generators/rust_templates/struct.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#[derive(Debug)] | ||
pub struct {{struct.name}} { | ||
{%- for packed_field in struct.packed.packed_fields %} | ||
pub {{packed_field.field.name}}: {{packed_field.field.kind|rust_field_type}}, | ||
{%- endfor %} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters