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

Allow the binding generator to not depend on builtin bindings. #2066

Merged
merged 1 commit into from
Apr 22, 2024

Conversation

mhammond
Copy link
Member

@mhammond mhammond commented Apr 8, 2024

Previously there were internal Config details which only worked with
builtin bindings types. This leans in to the BindingGenerator trait
to make things less coupled with the builtin bindings and better
for external bindings.

@@ -136,12 +137,13 @@ pub fn calc_cdylib_name(library_path: &Utf8Path) -> Option<&str> {
None
}

fn find_sources<Config: BindingsConfig>(
fn find_sources<T: BindingGenerator>(
generator: &T,
cargo_metadata: &cargo_metadata::Metadata,
library_path: &Utf8Path,
cdylib_name: Option<&str>,
config_file_override: Option<&Utf8Path>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I could wave a magic wand, I'd also have the API take in the actual Config instead of a path to the file that needs to be parsed. I think think the Config loading + parsing should happen in the CLI tool (as soon as possible).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please elaborate here? I've some vague ideas for going further in a later patch and I'd like to understand your thoughts. Here in particular I don't think we have the path to the TOML yet - we use the cargo metadata to find potentially one TOML file per crate.

What I think I'd like to do is work out how to separate the Config from the Generator - this would mean everything could pass around a dyn BindingGenerator and a dyn BindingConfig which would simplify a number of things. However, the only way I can see to make that work is to lean in to std::any::Any and have the binding downcast from the dyn BindingConfig back into its concrete Config. Any thoughts on that?

@SalvatoreT
Copy link
Contributor

I like it!

@SalvatoreT
Copy link
Contributor

Oops, I meant to comment on generate_external_bindings instead of find_source! Instead of loading a config and an override file and then parsing them to Config, I think that should happen outside the library and then pass in the Config. Right now, to use generate_external_bindings, you have to have a file-based configuration.

pub fn generate_external_bindings<T: BindingGenerator>(
binding_generator: &T,
udl_file: impl AsRef<Utf8Path>,
config_file_override: Option<impl AsRef<Utf8Path>>,
out_dir_override: Option<impl AsRef<Utf8Path>>,
library_file: Option<impl AsRef<Utf8Path>>,
crate_name: Option<&str>,
try_format_code: bool,
) -> Result<()> {
let crate_name = crate_name
.map(|c| Ok(c.to_string()))
.unwrap_or_else(|| crate_name_from_cargo_toml(udl_file.as_ref()))?;
let mut component = parse_udl(udl_file.as_ref(), &crate_name)?;
if let Some(ref library_file) = library_file {
macro_metadata::add_to_ci_from_library(&mut component, library_file.as_ref())?;
}
let crate_root = &guess_crate_root(udl_file.as_ref()).context("Failed to guess crate root")?;
let config_file_override = config_file_override.as_ref().map(|p| p.as_ref());
let config = {
let mut config = load_initial_config::<T::Config>(crate_root, config_file_override)?;
config.update_from_ci(&component);
if let Some(ref library_file) = library_file {
if let Some(cdylib_name) = crate::library_mode::calc_cdylib_name(library_file.as_ref())
{
config.update_from_cdylib_name(cdylib_name)
}
};
config
};

@mhammond
Copy link
Member Author

Right now, to use generate_external_bindings, you have to have a file-based configuration.

This seems tricky to avoid in "library mode" - we need to possibly load one file per crate, but don't know the list of crates before hand. What would a non-file-based config setup look like in this scenario?

@mhammond mhammond marked this pull request as ready for review April 15, 2024 14:56
@mhammond mhammond requested a review from a team as a code owner April 15, 2024 14:56
@mhammond mhammond requested review from skhamis and bendk and removed request for a team April 15, 2024 14:56
Copy link
Contributor

@bendk bendk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code looks fine to me and I have a vague sense of how this will simplify the bindings generation. But could you update the commit/PR message to discuss exactly how this will help?

@@ -79,6 +78,112 @@ enum Commands {
},
}

// Type-bounds on trait implementations makes selecting between languages a bit tedious.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't get this docstring at first, I think it refers to the match languages statement which does seem tedious. How about moving it to the match to make it more obvious.

Previously there were internal `Config` details which only worked with
builtin bindings types. This leans in to the `BindingGenerator` trait
to make things less coupled with the builtin bindings and better
for external bindings.

Follows up on mozilla#1991.
@mhammond mhammond changed the title Remove a common "config" for the bindings. Allow the binding generator to not depend on builtin bindings. Apr 19, 2024
@mhammond mhammond merged commit 44d4915 into mozilla:main Apr 22, 2024
5 checks passed
@mhammond mhammond deleted the split-bindings branch April 22, 2024 14:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants