Build-Only Package

Currently if a Rust module does not have src/lib.rs or src/main.rs, it will not even build. The build script should still have a life cycle so those files could be generated from the build script itself, additionally, a cargo.toml flag could be set, that this is a "build" module and run the build script on cargo build, but not have any Rust code, here is the use case:

A workspace with mostly Rust code, but also non-rust javascript frontend and directories with other programming languages. I think it'd be pretty neat if running I could startup all these components from a single cargo command. This way Rust could be used to execute build scripts for non-Rust fragments.

# Cargo.toml
[package]
build="build.rs"

In working with build.rs files, there is the general expectation and assumption that they do not write to src but only OUT_DIR, e.g. Files generated by build script into src/ are compiled twice · Issue #3076 · rust-lang/cargo · GitHub

On top of that, Cargo enumerates all build-targets at the very beginning and allowing dynamically generated build-targets would be a massive undertaking and may even run into fundamental problems where we need to know something before we know it. Requiring users to explicitly enumerate these in Cargo.toml may help. In fact, you might even be able to do that today.

There is a somewhat related request at Allow a build script (build.rs) in a virtual manifest Cargo.toml · Issue #8732 · rust-lang/cargo · GitHub

I suspect trying to use cargo as a general purpose build tool will lead to frustrations. For example,

btw I've started a discussion in the opposite direction, on finding in what ways people use build scripts and what we features we can provide to replace them, see Reduce the need for users to write build scripts · Issue #14948 · rust-lang/cargo · GitHub

2 Likes