Expand description
§Cargo build
§cargo-build is a wrapper around cargo instructions accesible in build.rs with better type safety and support for modern features.
Includes functions by default. Macros are optional, but do provide better experience when there is need to do formatting and variable number of arguments at the same time which is ideal for DSLs.
Add this crate as build-dependency
[build-dependencies]
cargo-build = "0.1.0" # no macros
[build-dependencies]
cargo-build = { version = "0.1.0", features = ["macros"] }https://doc.rust-lang.org/cargo/reference/build-scripts.html
Those instructions are usually implemented by println!("cargo::") call. This crate
provides easy to use wrapper-functions around those instructions.
Benefits:
- Less code.
- Easier to modify later.
- Harder to make typos.
§With cargo-build using functions:
cargo_build::rustc_link_arg_bin("server", "-Wl,--cref");
cargo_build::rustc_link_arg_bin("client", [
"-mlongcalls",
"-ffunction-sections",
"-Wl,--cref",
]);§Without cargo-build:
println!("cargo::rustc-link-arg-bin=server=-Wl,--cref");
println!("cargo::rustc-link-arg-bin=client=-mlongcalls");
println!("cargo::rustc-link-arg-bin=client=-ffunction-sections");
println!("cargo::rustc-link-arg-bin=client=-Wl,--cref");§With cargo-build using functions:
cargo_build::rustc_check_cfgs("cuda");
cargo_build::rustc_cfg("cuda");
cargo_build::rustc_check_cfg("api_version", ["1", "2", "3"]);
cargo_build::rustc_cfg(("api_version", "1"));§Without cargo-build:
- Note the inconsistancy of
cfg - Note the need for escape sequences
println!("cargo::rustc-check-cfg=cfg(cuda)");
println!("cargo::rustc-cfg=cuda");
println!("cargo::rustc-check-cfg=cfg(api_version, values(\"1\", \"2\", \"3\"))");
println!("cargo::rustc-cfg=api_version-\"1\"");§Macros example (enable features = ["macros"] in Cargo.toml):
let env_var = "HOST";
if std::env::var(env_var).is_ok() {
cargo_build::warning!("Warning during compilation: {} is not set", env_var);
cargo_build::error!("Unable to finish compilation: {} is not set", env_var);
}
cargo_build::rustc_link_arg!(cdylib: "-mlongcalls"; "-ffunction-sections");
cargo_build::rustc_link_arg!(
bin "client":
"-mlongcalls";
"-ffunction-sections";
"-Wl,--cref";
"stack-size={}", { 8 * 1024 * 1024 };
);
cargo_build::rustc_link_lib!(
static: "+whole-archive", "+verbatim", "+bundle" =
"nghttp2";
"libssl";
"libcrypto";
"mylib:{}", "renamed_lib";
);
cargo_build::rustc_check_cfg!("api_version": "1", "2", "3");
cargo_build::rustc_cfg!("api_version" = "1");Why use cargo-build when cargo emit already exists:
- Support for modern features (such as
error,rustc_check_cfg). - Support for ‘keywords’ (such as
link-lib:KINDis not a string but defined set of values (static,dylib,framework)). - Better syntax overall (such as
static: "lib1"; "lib2:{}", "renamed_lib2"; "lib3"in macros - no need to declare each libstatic). - Extended examples and documentation for modern use cases.
- Macros are feature - library works even without macros. Enable them by using
features = ["macros"]inCargo.toml
Note: The order of instructions in the build script may affect the order of arguments that
cargo passes to rustc. In turn, the order of arguments passed to rustc may affect the
order of arguments passed to the linker. Therefore, you will want to pay attention to
the order of the build script’s instructions. For example, if object foo needs to link
against library bar, you may want to make sure that library bar’s rustc-link-lib
instruction appears after instructions to link object foo.\
https://doc.rust-lang.org/cargo/reference/build-scripts.html
Modules§
Functions§
- error
- Displays an error on the terminal.
- metadata
- Metadata, used by links scripts.
- rerun_
if_ changed - Tells Cargo to re-run the build script ONLY if file or directory with given name changes.
- rerun_
if_ env_ changed - Tells Cargo to re-run the build script if environment variable with the given name has changed.
- rustc_
cfg - Enables custom compile-time
cfgsettings. - rustc_
check_ cfg - Define expected
cfgnames and values. Those names are used when checking the reachablecfgexpressions with theunexpected_cfgslint. - rustc_
check_ cfgs - Define expected config names. Those names are used when checking the reachable cfg expressions
with the
unexpected_cfgslint. - rustc_
env - Sets an environment variable.
- rustc_
flags - Passes certain flags to the compiler.
- rustc_
link_ arg - Passes custom flags to a linker for benchmarks, binaries,
cdylibcrates, examples, and tests. - rustc_
link_ arg_ benches - Passes custom flags to a linker for benches.
- rustc_
link_ arg_ bin - Passes custom flags to a linker for specific binary name.
- rustc_
link_ arg_ bins - Passes custom flags to a linker for binaries.
- rustc_
link_ arg_ cdylib - Passes custom flags to a linker for
cdylibcrates. - rustc_
link_ arg_ examples - Passes custom flags to a linker for examples.
- rustc_
link_ arg_ tests - Passes custom flags to a linker for tests.
- rustc_
link_ lib - Adds a library to link.
- rustc_
link_ lib_ dylib rustc_link_libalternative that automatically passesdylib=.- rustc_
link_ lib_ framework rustc_link_libalternative that automatically passesframework=.- rustc_
link_ lib_ static rustc_link_libalternative that automatically passesstatic=.- rustc_
link_ search - Adds a directory to the library search path.
- rustc_
link_ search_ all rustc_link_searchalternative that automatically passesall=.- rustc_
link_ search_ crate rustc_link_searchalternative that automatically passescrate=.- rustc_
link_ search_ dependency rustc_link_searchalternative that automatically passesdependency=.- rustc_
link_ search_ framework rustc_link_searchalternative that automatically passesframework=.- rustc_
link_ search_ native rustc_link_searchalternative that automatically passesnative=.- warning
- Displays a warning on the terminal.