rust-lang / cargo

The Rust package manager
https://doc.rust-lang.org/cargo
Apache License 2.0
12.7k stars 2.41k forks source link

Add `cargo new` flag for inserting `proc-macro = true` into `Cargo.toml` #14708

Open lthoerner opened 2 days ago

lthoerner commented 2 days ago

Problem

I'm currently in the process of creating a presentation about how to implement derive macros. I found that I could express the process of creating the proc macro subcrate and adding the proper dependencies via a snippet of shell commands:

cargo new my_macros --lib
cargo add my_macros --path my_macros
cd my_macros
cargo add syn
cargo add quote
cargo add proc_macro2

There is one notable exception here: I cannot write a command that adds the proper tag to the manifest.

[lib]
proc-macro = true

Proposed Solution

It would be nice for feature parity with the --lib specification to have a CLI flag for emitting the manifest for a proc macro crate. I suspect this would be a relatively simple thing to add as well, and I am happy to work on it myself if this issue gets approved.

Specifically, I propose that cargo new have a --proc-macro flag that is mutually exclusive with --bin or --lib (to my understanding it would only be valid in the case of library crate anyway, though feel free to correct me here).

Notes

No response

weihanglo commented 1 day ago

Thanks for the idea!

I'm not sure how often the --proc-macro flag would be useful. Personally, I seldom start new proc macro packages, but I can't speak for everyone. On the other hand, for people working on C bindings, should we also provide a --crate-type or --cdylib flag?

I might be a bit conservative when adding new flags to Cargo, as every flag is permanent. I feel like for this kind of cases it might be better to explore templating solution for proc macro package. See relevant discussions in https://github.com/rust-lang/cargo/issues/5151. Perhaps https://github.com/rust-lang/cargo/issues/8365 is also related if one wants to create a workspace with a proc-macro crate.