-
Notifications
You must be signed in to change notification settings - Fork 444
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
crate_universe: Rewrite generator in starlark #1287
Comments
I'm not entirely sure the generator can be entirely written in starlark but the renderer side of it very likely could be and think that'd be great!
To clarify, are you building a new generator in repository rules? That said though, if the renderer were to be implemented in starlark, then there would be no need to download or build a generator in most cases since if you have a trusted lockfile, the only computation left to do is write BUILD files from pre-defined templates which is something doable in most lanugages. |
Sounds great, thanks! I have some pretty good ideas on how to tackle this and I think the implementation could be quite nice. I was thinking about the generator a bit and don't see why that couldn't also happen in Starlark. The cargo binary will happy output JSON which makes it easy to work with. |
I've made a prototype implementation and it looks pretty good so far. It has shown some weirdness with the format of the lockfile, but has been easy to work with otherwise. The blocker at the moment is figuring out how to handle selects and cfg expressions (conditional compilation, syntax reference). Example: cfg(target_family = "unix") The lockfile isn't quite as data-oriented as it initially seems. It expects tools which work with the lockfile to be able to parse and evaluate these expressions. Does it seem reasonable to suggest a slight change to the lockfile? I don't see any drawbacks for an explicit list of targets as opposed to a cfg expression. |
Can you provide an example of the specific change you're thinking of? |
Yep, of course. So here's an excerpt from the JSON lockfile (represented as CUE for brevity): "sha2 0.10.2": common_attrs: deps: selects: {
"cfg(any(target_arch = \"aarch64\", target_arch = \"x86_64\", target_arch = \"x86\"))": [{
id: "cpufeatures 0.2.2"
target: "cpufeatures"
}]
} Currently the "selects" deps are a map of cfg expressions to deps. I would propose that instead it should be a list of objects which include a list of platform names and deps. Such: "sha2 0.10.2": common_attrs: deps: selects: [{
platforms: [
"aarch64-apple-darwin",
"aarch64-apple-ios",
"aarch64-linux-android",
"aarch64-unknown-linux-gnu",
"i686-apple-darwin",
"i686-linux-android",
"i686-pc-windows-msvc",
"i686-unknown-freebsd",
"i686-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-apple-ios",
"x86_64-linux-android",
"x86_64-pc-windows-msvc",
"x86_64-unknown-freebsd",
"x86_64-unknown-linux-gnu",
]
deps: [{
id: "cpufeatures 0.2.2"
target: "cpufeatures"
}]
} |
That'd probably be fine but seems like it'd be noisy. The reason I wrote the cfg string is because it was shorter and slightly more meaningful. The lockfile includes a mapping of those strings to a list of platforms so they only need to be defined once. Would it still work to use that? "conditions": {
"cfg(any(target_arch = \"aarch64\", target_arch = \"x86_64\", target_arch = \"x86\"))": [
"aarch64-apple-darwin",
"aarch64-apple-ios",
"aarch64-linux-android",
"aarch64-unknown-linux-gnu",
"i686-apple-darwin",
"i686-linux-android",
"i686-pc-windows-msvc",
"i686-unknown-freebsd",
"i686-unknown-linux-gnu",
"x86_64-apple-darwin",
"x86_64-apple-ios",
"x86_64-linux-android",
"x86_64-pc-windows-msvc",
"x86_64-unknown-freebsd",
"x86_64-unknown-linux-gnu"
]
} |
I completely overlooked the Is there a reference which explains the format of the lockfile? Something which isn't source code? Might be helpful for future readers. re: it'd be noisy I agree, though the lockfile is already very large. ❯ cat Cargo.Bazel.lock | wc -l
10719 |
So, I got it all working! :) Just need to clean some stuff up. Hopefully will open a PR for this within the next few days. |
If you're interested, this is the dev branch: main...uhthomas:1287. There are a few more things to do, but it's possible to build and run the cargo-bazel binary with these changes which is a huge milestone.
|
@UebelAndre Would you mind checking out my branch and trying it out? It's not feature complete, though rules_rust builds with it ( The vendoring of BUILD files had to be removed, apologies if this is a problem. The way the manifest is interpreted by starlark makes it infeasible. However Looking forward to hearing your experience! |
This is just an idea, I'd really appreciate some feedback and ideas.
Is it conceivably possible to rewrite the generator in pure starlark? This would remove the bootstrapping problem of distributing generator binaries or hackily building the generator with the native cargo toolchain.
rules_rust/crate_universe/deps_bootstrap.bzl
Line 9 in 59fab4e
We're using a custom generator to work around some issues, but this makes the analysis phase almost a minute slower as repository rules are not remotely cacheable.
Failing a full rewrite, some more ideas:
Thanks for taking the time to read this :) Please don't hesitate to ask any questions.
The text was updated successfully, but these errors were encountered: