Struct Build
pub struct Build { /* private fields */ }
A builder for compilation of a native library.
A Build is the main type of the cc crate and is used to control all the
various configuration options and such of a compile. You'll find more
documentation on each method itself.
A Build reads the process environment once, the first time it needs it
(for example in Build::compile, Build::get_compiler,
Build::is_flag_supported or Build::try_flags_from_environment), and
keeps that copy. Its own lookups of variables such as CC or CFLAGS, and
of those Cargo sets for build scripts such as TARGET, OUT_DIR or
CARGO_CFG_TARGET_FEATURE, use the copy, and the compiler and other tools
it runs get the copy as their whole environment instead of inheriting the
process environment, so later changes to the process environment are not
seen. A clone made after that point keeps the copy, one made before takes
its own. Use Build::env to change the environment of the child
processes.
Implementations
impl Build
fn is_flag_supported(&self, flag: impl AsRef<OsStr>) -> Result<bool, Error>Run the compiler to test if it accepts the given flag.
For a convenience method for setting flags conditionally, see
flag_if_supported().It may return error if it's unable to run the compiler with a test file (e.g. the compiler is missing or a write to the
out_dirfailed).Note: Once computed, the result of this call is cached, and the cache is shared with clones of this
Build. It is reused for the same compiler, flag, target, host, language and environment.fn try_compile(&self, output: &str) -> Result<(), Error>Run the compiler, generating the file
outputThis will return a result instead of panicking; see [
Self::compile()] for the complete description.fn compile(&self, output: &str)Run the compiler, generating the file
outputLibrary name
The
outputstring argument determines the file name for the compiled library. The Rust compiler will create an assembly named "lib"+output+".a". MSVC will create a file named output+".lib".The choice of
outputis close to arbitrary, but:- must be nonempty,
- must not contain a path separator (
/), - must be unique across all
compileinvocations made by the same build script.
If your build script compiles a single source file, the base name of that source file would usually be reasonable:
cc::Build::new().file("blobstore.c").compile("blobstore");Compiling multiple source files, some people use their crate's name, or their crate's name + "-cc".
Otherwise, please use your imagination.
For backwards compatibility, if
outputstarts with "lib" and ends with ".a", a second "lib" prefix and ".a" suffix do not get added on, but this usage is deprecated; please omitliband.ain the argument that you pass.Panics
Panics if
outputis not formatted correctly or if one of the underlying compiler commands fails. It can also panic if it fails reading file names or creating directories.fn compile_intermediates(&self) -> Vec<PathBuf>Run the compiler, generating intermediate files, but without linking them into an archive file.
This will return a list of compiled object files, in the same order as they were passed in as
file/filesmethods.Build::create_archiveandemit_link_directivesdo the rest of whatBuild::compiledoes, so the objects can be changed or added to in between.fn try_compile_intermediates(&self) -> Result<Vec<PathBuf>, Error>Run the compiler, generating intermediate files, but without linking them into an archive file.
This will return a result instead of panicking; see
compile_intermediates()for the complete description.fn create_archive<P>(&self, output: &str, objects: P) -> PathBuf where P: IntoIterator, P::Item: AsRef<Path>,Create a static library from object files, the way
Build::compiledoes after compiling.outputnames the library as described under "Library name" inBuild::compile. The archive is written tolib<output>.ain the output directory (Build::out_dir, orOUT_DIRby default), replacing any existing file, and that path is returned. On MSVC targets a copy named<output>.libis placed next to it, ascompiledoes.The archive holds
objectsfollowed by any objects added withBuild::objectorBuild::objects. It is created with the same archiver and flags ascompileuses, seeBuild::get_archiver. WithBuild::cudaand CUDA files added, the device code is linked into it as well.No
cargo:lines are emitted. Useemit_link_directiveswith the returned path to link the library.let mut build = cc::Build::new(); build.file("foo.c"); let objects = build.compile_intermediates(); // Change the objects here. let library = build.create_archive("foo", &objects); cc::emit_link_directives(&build, &library);Panics
Panics if
outputis not formatted correctly, ifCC_FORCE_DISABLEis set, or if the archiver fails.fn try_create_archive<P>(&self, output: &str, objects: P) -> Result<PathBuf, Error> where P: IntoIterator, P::Item: AsRef<Path>,Create a static library from object files, the way
Build::compiledoes after compiling.This will return a result instead of panicking; see
Build::create_archivefor the complete description.fn try_expand(&self) -> Result<Vec<u8>, Error>This will return a result instead of panicking; see [
Self::expand()] for the complete description.fn expand(&self) -> Vec<u8>Run the compiler, returning the macro-expanded version of the input files.
This is only relevant for C and C++ files.
Panics
Panics if more than one file is present in the config, or if compiler path has an invalid file name.
Example
let out = cc::Build::new().file("src/foo.c").expand();fn get_compiler(&self) -> ToolGet the compiler that's in use for this configuration.
This function will return a
Toolwhich represents the culmination of this configuration at a snapshot in time. The returned compiler can be inspected (e.g. the path, arguments, environment) to forward along to other tools, or theto_commandmethod can be used to invoke the compiler itself.This method will take into account all configuration such as debug information, optimization level, include directories, defines, etc. Additionally, the compiler binary in use follows the standard conventions for this path, e.g. looking at the explicitly set compiler, environment variables (a number of which are inspected here), and then falling back to the default configuration.
Panics
Panics if an error occurred while determining the architecture.
fn try_get_compiler(&self) -> Result<Tool, Error>Get the compiler that's in use for this configuration.
This will return a result instead of panicking; see
get_compiler()for the complete description.fn get_archiver(&self) -> CommandGet the archiver (ar) that's in use for this configuration.
You can use
Command::get_programto get just the path to the command.This method will take into account all configuration such as debug information, optimization level, include directories, defines, etc. Additionally, the compiler binary in use follows the standard conventions for this path, e.g. looking at the explicitly set compiler, environment variables (a number of which are inspected here), and then falling back to the default configuration.
Panics
Panics if an error occurred while determining the architecture.
fn try_get_archiver(&self) -> Result<Command, Error>Get the archiver that's in use for this configuration.
This will return a result instead of panicking; see
Self::get_archiverfor the complete description.fn get_ranlib(&self) -> CommandGet the ranlib that's in use for this configuration.
You can use
Command::get_programto get just the path to the command.This method will take into account all configuration such as debug information, optimization level, include directories, defines, etc. Additionally, the compiler binary in use follows the standard conventions for this path, e.g. looking at the explicitly set compiler, environment variables (a number of which are inspected here), and then falling back to the default configuration.
Panics
Panics if an error occurred while determining the architecture.
fn try_get_ranlib(&self) -> Result<Command, Error>Get the ranlib that's in use for this configuration.
This will return a result instead of panicking; see
Self::get_ranlibfor the complete description.
impl Build
fn new() -> BuildConstruct a new instance of a blank set of configuration.
This builder is finished with the
compilefunction.fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut BuildAdd a directory to the
-Ior include path for headersExample
use std::path::Path; let library_path = Path::new("/path/to/library"); cc::Build::new() .file("src/foo.c") .include(library_path) .include("src") .compile("foo");fn includes<P>(&mut self, dirs: P) -> &mut Build where P: IntoIterator, P::Item: AsRef<Path>,Add multiple directories to the
-Iinclude path.Example
# use std::path::Path; # let condition = true; # let mut extra_dir = None; if condition { extra_dir = Some(Path::new("/path/to")); } cc::Build::new() .file("src/foo.c") .includes(extra_dir) .compile("foo");fn define<'a, V: Into<Option<&'a str>>>(&mut self, var: &str, val: V) -> &mut BuildSpecify a
-Dvariable with an optional value.Example
cc::Build::new() .file("src/foo.c") .define("FOO", "BAR") .define("BAZ", None) .compile("foo");fn object<P: AsRef<Path>>(&mut self, obj: P) -> &mut BuildAdd an arbitrary object file to link in
fn objects<P>(&mut self, objs: P) -> &mut Build where P: IntoIterator, P::Item: AsRef<Path>,Add arbitrary object files to link in
fn flag(&mut self, flag: impl AsRef<OsStr>) -> &mut BuildAdd an arbitrary flag to the invocation of the compiler
Example
cc::Build::new() .file("src/foo.c") .flag("-ffunction-sections") .compile("foo");fn flags<Iter>(&mut self, flags: Iter) -> &mut Build where Iter: IntoIterator, Iter::Item: AsRef<OsStr>,Add multiple flags to the invocation of the compiler. This is equivalent to calling
flagfor each item in the iterator.Example
cc::Build::new() .file("src/foo.c") .flags(["-Wall", "-Wextra"]) .compile("foo");fn remove_flag(&mut self, flag: &str) -> &mut BuildRemoves a compiler flag that was added by
Build::flag.Will not remove flags added by other means (default flags, flags from env, and so on).
Example
cc::Build::new() .file("src/foo.c") .flag("unwanted_flag") .remove_flag("unwanted_flag");fn ar_flag(&mut self, flag: impl AsRef<OsStr>) -> &mut BuildAdd a flag to the invocation of the ar
Example
cc::Build::new() .file("src/foo.c") .file("src/bar.c") .ar_flag("/NODEFAULTLIB:libc.dll") .compile("foo");fn asm_flag(&mut self, flag: impl AsRef<OsStr>) -> &mut BuildAdd a flag that will only be used with assembly files.
The flag will be applied to input files with either a
.sor.asmextension (case insensitive).Example
cc::Build::new() .asm_flag("-Wa,-defsym,abc=1") .file("src/foo.S") // The asm flag will be applied here .file("src/bar.c") // The asm flag will not be applied here .compile("foo");fn flag_if_supported(&mut self, flag: impl AsRef<OsStr>) -> &mut BuildAdd an arbitrary flag to the invocation of the compiler if it supports it
The flag keeps its place among the flags added with
Build::flag.Example
cc::Build::new() .file("src/foo.c") .flag_if_supported("-Wlogical-op") // only supported by GCC .flag_if_supported("-Wunreachable-code") // only supported by clang .compile("foo");fn try_flags_from_environment(&mut self, environ_key: &str) -> Result<&mut Build, Error>Add flags from the specified environment variable.
Normally the
cccrate will consult with the standard set of environment variables (such asCFLAGSandCXXFLAGS) to construct the compiler invocation. Use of this method provides additional levers for the end user to use when configuring the build process.Just like the standard variables, this method will search for an environment variable with appropriate target prefixes, when appropriate.
Examples
This method is particularly beneficial in introducing the ability to specify crate-specific flags.
cc::Build::new() .file("src/foo.c") .try_flags_from_environment(concat!(env!("CARGO_PKG_NAME"), "_CFLAGS")) .expect("the environment variable must be specified and UTF-8") .compile("foo");Set the
-sharedflag.This will typically be ignored by the compiler when calling [
Self::compile()] since it only produces static libraries.Example
// This will create a library named "liblibfoo.so.a" cc::Build::new() .file("src/foo.c") .shared_flag(true) .compile("libfoo.so");fn static_flag(&mut self, static_flag: bool) -> &mut BuildSet the
-staticflag.This will typically be ignored by the compiler when calling [
Self::compile()] since it only produces static libraries.Example
cc::Build::new() .file("src/foo.c") .shared_flag(true) .static_flag(true) .compile("foo");fn no_default_flags(&mut self, no_default_flags: bool) -> &mut BuildDisables the generation of default compiler flags. The default compiler flags may cause conflicts in some cross compiling scenarios.
Setting the
CRATE_CC_NO_DEFAULTSenvironment variable has the same effect as setting this totrue. The presence of the environment variable and the value ofno_default_flagswill be OR'd together.fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut BuildAdd a file which will be compiled
fn files<P>(&mut self, p: P) -> &mut Build where P: IntoIterator, P::Item: AsRef<Path>,Add files which will be compiled
fn source_root<P: AsRef<Path>>(&mut self, root: P) -> &mut BuildSet an additional root used to generate stable object filenames for source files outside Cargo's manifest and output directories.
Source directories beneath this root are hashed relative to it, rather than by their machine-specific absolute paths. This does not remap debug information embedded by the compiler.
The root and source paths must use the same path representation. In particular, an absolute source path requires an absolute root.
If a source is also under
OUT_DIRorCARGO_MANIFEST_DIR, those take precedence over this root.Example
let root = std::path::PathBuf::from("/external/compiler-rt"); cc::Build::new() .source_root(&root) .file(root.join("lib/builtins/absvdi2.c")) .compile("compiler-rt");fn get_files(&self) -> impl Iterator<Item = &Path>Get the files which will be compiled
fn cpp(&mut self, cpp: bool) -> &mut BuildSet C++ support.
The other
cpp_*options will only become active if this is set totrue.The name of the C++ standard library to link is decided by:
- If
cpp_link_stdlibis set, use its value. - Else if the
CXXSTDLIBenvironment variable is set, use its value. - Else the default is
c++for OS X and BSDs,c++_sharedfor Android,Nonefor MSVC andstdc++for anything else, orc++there if the compiler is a Clang that uses libc++, see C++ support.
On MSVC this also passes
-Tpimmediately before each.ccsource file to ensure that they are compiled as C++ rather than assumed to be object files. The per-file form is used instead of/TP, which would compile every following input as C++. clang-cl recognizes.ccitself and is not passed-Tp.- If
fn cuda(&mut self, cuda: bool) -> &mut BuildSet CUDA C++ support.
Enabling CUDA will invoke the CUDA compiler, NVCC. While NVCC accepts the most common compiler flags, e.g.
-std=c++17, some project-specific flags might have to be prefixed with "-Xcompiler" flag, for example as.flag("-Xcompiler").flag("-fpermissive"). See the documentation fornvcc, the CUDA compiler driver, at https://docs.nvidia.com/cuda/cuda-compiler-driver-nvcc/ for more information.If enabled, this also implicitly enables C++ support.
fn cudart(&mut self, cudart: &str) -> &mut BuildLink CUDA run-time.
This option mimics the
--cudartNVCC command-line option. Just like the original it accepts{none|shared|static}, with default beingstatic. The method has to be invoked after.cuda(true), or not at all, if the default is right for the project.fn ccbin(&mut self, ccbin: bool) -> &mut BuildSet CUDA host compiler.
By default, a
-ccbinflag will be passed to NVCC to specify the underlying host compiler. The value of-ccbinis the same as the chosen C++ compiler. This is not always desired, because NVCC might not support that compiler. In this case, you can remove the-ccbinflag so that NVCC will choose the host compiler by itself.fn std(&mut self, std: &str) -> &mut BuildSpecify the C or C++ language standard version.
These values are common to modern versions of GCC, Clang and MSVC:
c11for ISO/IEC 9899:2011c17for ISO/IEC 9899:2018c++14for ISO/IEC 14882:2014c++17for ISO/IEC 14882:2017c++20for ISO/IEC 14882:2020
Other values have less broad support, e.g. MSVC does not support
c++11(c++14is the minimum),c89(omit the flag instead) orc99.For compiling C++ code, you should also set
.cpp(true).The default is that no standard flag is passed to the compiler, so the language version will be the compiler's default.
Example
cc::Build::new() .file("src/modern.cpp") .cpp(true) .std("c++17") .compile("modern");fn warnings_into_errors(&mut self, warnings_into_errors: bool) -> &mut BuildSet warnings into errors flag.
Disabled by default.
Warning: turning warnings into errors only make sense if you are a developer of the crate using cc-rs. Some warnings only appear on some architecture or specific version of the compiler. Any user of this crate, or any other crate depending on it, could fail during compile time.
Example
cc::Build::new() .file("src/foo.c") .warnings_into_errors(true) .compile("libfoo.a");fn warnings(&mut self, warnings: bool) -> &mut BuildSet warnings flags.
Adds some flags:
- "-Wall" for MSVC.
- "-Wall", "-Wextra" for GNU and Clang.
Enabled by default.
Example
cc::Build::new() .file("src/foo.c") .warnings(false) .compile("libfoo.a");fn extra_warnings(&mut self, warnings: bool) -> &mut BuildSet extra warnings flags.
Adds some flags:
- nothing for MSVC.
- "-Wextra" for GNU and Clang.
Enabled by default.
Example
// Disables -Wextra, -Wall remains enabled: cc::Build::new() .file("src/foo.c") .extra_warnings(false) .compile("libfoo.a");fn cpp_link_stdlib<'a, V: Into<Option<&'a str>>>(&mut self, cpp_link_stdlib: V) -> &mut BuildSet the standard library to link against when compiling with C++ support.
If the
CXXSTDLIBenvironment variable is set, its value will override the default value, but not the value explicitly set by calling this function.A value of
Noneindicates that no automatic linking should happen, otherwise cargo will link against the specified library.Setting this, or
CXXSTDLIB, also skips checking whether a Clang compiler uses libc++, see C++ support.The given library name must not contain the
libprefix.Common values:
stdc++for GNUc++for Clangc++_sharedorc++_staticfor Android
Example
cc::Build::new() .file("src/foo.c") .shared_flag(true) .cpp_link_stdlib("stdc++") .compile("libfoo.so");fn cpp_link_stdlib_static(&mut self, is_static: bool) -> &mut BuildForce linker to statically link C++ stdlib. By default cc-rs will emit rustc-link flag to link against system C++ stdlib (e.g. libstdc++.so, libc++.so) Provide value of
trueif linking against system library is not desiredNote that for
wasm32target C++ stdlib will always be linked staticallyIf the
CXXSTDLIB_STATICenvironment variable is set, and not to"","0","no"or"false", the C++ stdlib is linked statically even if this is set tofalse. It is read with the same target prefixes asCXXSTDLIB.The C++ stdlib is emitted as
cargo:rustc-link-lib=static:-bundle=<stdlib>, so the linker of the final binary finds it in the toolchain's search paths instead of rustc bundling it into the rlib. Onwasm32andpauthtesttargets, where cc may also link the C++ stdlib throughcargo:rustc-flags, and on Apple targets, where the linker would pick the dynamic library over an unbundled static one, it is emitted asstatic=<stdlib>. A stdlib value that already names a link kind, such asCXXSTDLIB=static:-bundle=stdc++, is emitted unchanged.rustc rejects a library that is passed twice when either mention has link modifiers ("overriding linking modifiers from command line is not supported"). The C++ stdlib is therefore emitted only once per
Build, shared with its clones, however many times it is compiled. A build script that links the same C++ stdlib from two separateBuilds with this option, or links it both statically and dynamically, fails to build. Compile such libraries with oneBuildor its clones, or setcpp_link_stdlib(None)on all but one of them.Example
cc::Build::new() .file("src/foo.cpp") .cpp(true) .cpp_link_stdlib("stdc++") .cpp_link_stdlib_static(true) .compile("foo");fn cpp_set_stdlib<'a, V: Into<Option<&'a str>>>(&mut self, cpp_set_stdlib: V) -> &mut BuildForce the C++ compiler to use the specified standard library.
Setting this option will automatically set
cpp_link_stdlibto the same value.The default value of this option is always
None.This option has no effect when compiling for a Visual Studio based target.
This option sets the
-stdlibflag, which is only supported by some compilers (clang, icc) but not by others (gcc). The library will not detect which compiler is used, as such it is the responsibility of the caller to ensure that this option is only used in conjunction with a compiler which supports the-stdlibflag.A value of
Noneindicates that no specific C++ standard library should be used, otherwise-stdlibis added to the compile invocation.The given library name must not contain the
libprefix.Common values:
stdc++for GNUc++for Clang
Example
cc::Build::new() .file("src/foo.c") .cpp_set_stdlib("c++") .compile("libfoo.a");fn target(&mut self, target: &str) -> &mut BuildConfigures the
rustctarget this configuration will be compiling for.This will fail if using a target not in a pre-compiled list taken from
rustc +nightly --print target-list. The list will be updated periodically.You should avoid setting this in build scripts, target information will instead be retrieved from the environment variables
TARGETandCARGO_CFG_TARGET_*that Cargo sets.Example
cc::Build::new() .file("src/foo.c") .target("aarch64-linux-android") .compile("foo");fn host(&mut self, host: &str) -> &mut BuildConfigures the host assumed by this configuration.
This option is automatically scraped from the
HOSTenvironment variable by build scripts, so it's not required to call this function.Example
cc::Build::new() .file("src/foo.c") .host("arm-linux-gnueabihf") .compile("foo");fn opt_level(&mut self, opt_level: u32) -> &mut BuildConfigures the optimization level of the generated object files.
This option is automatically scraped from the
OPT_LEVELenvironment variable by build scripts, so it's not required to call this function.fn opt_level_str(&mut self, opt_level: &str) -> &mut BuildConfigures the optimization level of the generated object files.
This option is automatically scraped from the
OPT_LEVELenvironment variable by build scripts, so it's not required to call this function.fn debug(&mut self, debug: bool) -> &mut BuildConfigures whether the compiler will emit debug information when generating object files.
This option is automatically scraped from the
DEBUGenvironment variable by build scripts, so it's not required to call this function.fn debug_str(&mut self, debug: &str) -> &mut BuildConfigures whether the compiler will emit debug information when generating object files.
This should be one of the values accepted by Cargo's
debugprofile setting, which cc-rs will try to map to the appropriate C compiler flag.This option is automatically scraped from the
DEBUGenvironment variable by build scripts, so it's not required to call this function.fn force_frame_pointer(&mut self, force: bool) -> &mut BuildConfigures whether the compiler will emit instructions to store frame pointers during codegen.
This option is automatically enabled when debug information is emitted. Otherwise the target platform compiler's default will be used. You can use this option to force a specific setting.
fn out_dir<P: AsRef<Path>>(&mut self, out_dir: P) -> &mut BuildConfigures the output directory where all object files and static libraries will be located.
This option is automatically scraped from the
OUT_DIRenvironment variable by build scripts, so it's not required to call this function.fn compiler<P: AsRef<Path>>(&mut self, compiler: P) -> &mut BuildConfigures the compiler to be used to produce output.
This option is automatically determined from the target platform or a number of environment variables, so it's not required to call this function.
fn archiver<P: AsRef<Path>>(&mut self, archiver: P) -> &mut BuildConfigures the tool used to assemble archives.
This option is automatically determined from the target platform or a number of environment variables, so it's not required to call this function.
fn ranlib<P: AsRef<Path>>(&mut self, ranlib: P) -> &mut BuildConfigures the tool used to index archives.
This option is automatically determined from the target platform or a number of environment variables, so it's not required to call this function.
fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut BuildDefine whether metadata should be emitted for cargo allowing it to automatically link the binary. Defaults to
true.The emitted metadata is:
rustc-link-lib=static=compiled librustc-link-search=native=target folder- When target is MSVC, the ATL-MFC libs are added via
rustc-link-search=native= - When C++ is enabled, the C++ stdlib is added via
rustc-link-lib - If
emit_rerun_if_env_changedis notfalse,rerun-if-env-changed=env
fn cargo_warnings(&mut self, cargo_warnings: bool) -> &mut BuildDefine whether compile warnings should be emitted for cargo. Defaults to
true.If disabled, compiler messages will not be printed. They still go to a logger set with
message_logger. Issues unrelated to the compilation will always produce cargo warnings regardless of this setting.fn message_logger(&mut self, logger: Option<Arc<dyn BuildMessageLogger>>) -> &mut BuildSets a logger that receives cc's messages: its own warnings, each line the commands it runs write to stderr, the commands that failed, and the command that compiles each object file. See
BuildMessageKindfor the details, andcompile_commandsfor writing the compile commands to acompile_commands.json.The warnings and stderr lines are still printed as cargo warnings too. Disable
cargo_warningsto send them only to the logger. Othercargo:lines, such ascargo:rustc-link-lib, are always printed to stdout, since Cargo reads them there.Clones of this
Buildshare the logger. PassingNoneremoves a logger set earlier.Example
use std::{any::Any, sync::{Arc, Mutex}}; use cc::{BuildMessage, BuildMessageKind, BuildMessageLogger}; #[derive(Default)] struct Messages(Mutex<Vec<String>>); impl BuildMessageLogger for Messages { fn log(&self, kind: BuildMessageKind, msg: BuildMessage<'_>, _extra: &dyn Any) { self.0.lock().unwrap().push(format!("{kind:?}: {msg}")); } } let messages = Arc::new(Messages::default()); let result = cc::Build::new() .file("src/foo.c") .message_logger(Some(messages.clone())) .try_compile("foo"); // The messages are there whether the build failed or not. println!("{:#?}", messages.0.lock().unwrap()); result.unwrap();fn cargo_debug(&mut self, cargo_debug: bool) -> &mut BuildDefine whether debug information should be emitted for cargo. Defaults to whether or not the environment variable
CC_ENABLE_DEBUG_OUTPUTis set.If enabled, the compiler will emit debug information when generating object files, such as the command invoked and the exit status.
fn cargo_output(&mut self, cargo_output: bool) -> &mut BuildDefine whether compiler output (to stdout) should be emitted. Defaults to
true(forward compiler stdout to this process' stdout)Some compilers emit errors to stdout, so if you really need stdout to be clean you should also set this to
false.fn link_lib_modifier(&mut self, link_lib_modifier: impl AsRef<OsStr>) -> &mut BuildAdds a native library modifier that will be added to the
rustc-link-lib=static:MODIFIERS=LIBRARY_NAMEmetadata line emitted for cargo ifcargo_metadatais enabled. See https://doc.rust-lang.org/rustc/command-line-arguments.html#-l-link-the-generated-crate-to-a-native-library for the list of modifiers accepted by rustc.fn pic(&mut self, pic: bool) -> &mut BuildConfigures whether the compiler will emit position independent code.
This option defaults to
falseforwindows-gnuand bare metal targets and totruefor all other targets.fn use_plt(&mut self, use_plt: bool) -> &mut BuildConfigures whether the Procedure Linkage Table is used for indirect calls into shared libraries.
The PLT is used to provide features like lazy binding, but introduces a small performance loss due to extra pointer indirection. Setting
use_plttofalsecan provide a small performance increase.Note that skipping the PLT requires a recent version of GCC/Clang.
This only applies to ELF targets. It has no effect on other platforms.
fn emit_rerun_if_env_changed(&mut self, emit_rerun_if_env_changed: bool) -> &mut BuildDefine whether metadata should be emitted for cargo to only trigger rebuild when detected environment changes, by default build script is always run on every compilation if no rerun cargo metadata is emitted.
The
cargo:rerun-if-env-changedlines name the variables cc reads, and those that the compiler, or a program it runs, reads by itself and that change what it compiles or which programs it runs:- GCC and Clang, clang-cl included:
CPATH,C_INCLUDE_PATH,CPLUS_INCLUDE_PATH,OBJC_INCLUDE_PATH,OBJCPLUS_INCLUDE_PATH,COMPILER_PATHandSOURCE_DATE_EPOCH - GCC:
GCC_EXEC_PREFIX - Clang, clang-cl included:
CCC_OVERRIDE_OPTIONSandCLANG_NO_DEFAULT_CONFIG zig cc:ZIG_LIB_DIRandZIG_LIBC- cl.exe, and Clang on MSVC targets:
INCLUDEandEXTERNAL_INCLUDE - Clang on MSVC targets:
VCToolsInstallDirandVCINSTALLDIR - cl.exe and clang-cl:
CLand_CL_, and for cl.exeLIBPATH ml.exeandml64.exe, for.asmfiles:ML- nvcc, for CUDA:
NVCC_PREPEND_FLAGS,NVCC_APPEND_FLAGSandNVCC_CCBIN, and on MSVC targetsVSCMD_VER - emcc, on Emscripten targets:
EMCC_CFLAGS,EMCC_LOCAL_PORTS,EM_CONFIG,EM_CACHE,EM_PORTS,EM_LLVM_ROOT,EM_CLANG_ADD_VERSIONandEM_COMPILER_WRAPPER xcrun, on Apple targets, where cc runs it to find the SDK, and on macOS hosts, where the compilers in/usr/binrun through it:DEVELOPER_DIR,TOOLCHAINSandSDKROOT- Apple targets: the target's deployment target variable, such as
MACOSX_DEPLOYMENT_TARGET, and for ClangRC_DEBUG_OPTIONSandRC_DEBUG_PREFIX_MAP
Variables that only change diagnostics, logs, temporary files or linking are left out, such as
GCC_COLORS,CC_PRINT_OPTIONS,TMPDIRandLIBRARY_PATH. cc prints the lines for the compiler's variables whenever it works out the compiler, such as inBuild::compile,Build::expand,Build::get_compileror a flag check that runs the compiler, and the line forMLwhen it compiles.asmfiles. Like the lines for the variables cc looks up in its copy of the environment, each is printed once for aBuildand its clones.NOTE that cc does not emit metadata to detect changes for
PATH, since it could be changed every compilation yet does not affect the result of compilation (i.e. rust-analyzer adds temporary directory toPATH).cc in general, has no way detecting changes to compiler, as there are so many ways to change it and sidestep the detection, for example the compiler might be wrapped in a script so detecting change of the file, or using checksum won't work.
We recommend users to decide for themselves, if they want rebuild if the compiler has been upgraded or changed, and how to detect that.
This has no effect if the
cargo_metadataoption isfalse.This option defaults to
true.- GCC and Clang, clang-cl included:
fn static_crt(&mut self, static_crt: bool) -> &mut BuildConfigures whether the /MT flag or the /MD flag will be passed to msvc build tools.
This option defaults to
false, and affect only msvc targets.fn shell_escaped_flags(&mut self, shell_escaped_flags: bool) -> &mut BuildConfigure whether *FLAGS variables are parsed using
shlex, similarly tomakeandcmake.This option defaults to
false.fn inherit_rustflags(&mut self, inherit_rustflags: bool) -> &mut BuildConfigure whether cc should automatically inherit compatible flags passed to rustc from
CARGO_ENCODED_RUSTFLAGS.This option defaults to
true.fn inherit_trim_paths(&mut self, inherit_trim_paths: bool) -> &mut BuildConfigure whether cc should automatically inherit path remap rules from cargo's
trim-pathsprofile setting, and translate them into-fmacro-prefix-map/-fdebug-prefix-mapflags.This option defaults to
true.This option doesn't support Windows MSVC cl.exe yet. Only clang and GCC are supported.
trim-pathsis currently an unstable cargo feature, only available on nightly with-Ztrim-paths. The contract around this option may change as the cargo feature evolves.fn prefer_clang_cl_over_msvc(&mut self, prefer_clang_cl_over_msvc: bool) -> &mut BuildPrefer to use clang-cl over msvc.
This option defaults to
false.On MSVC targets, this makes
cclook forclang-cl.exeinstead ofcl.exewhen no compiler is set withBuild::compileror theCCandCXXenvironment variables.The
CC_PREFER_CLANG_CL_OVER_MSVCenvironment variable takes precedence over this option both ways, unless it is empty, so whoever runs the build can choose for every crate that usescc. It counts astrueunless set to"0","no"or"false". A build that only works with clang-cl can check the compiler it got withTool::is_like_clang_cl.fn env<K, V>(&mut self, key: K, val: V) -> &mut Build where K: AsRef<OsStr>, V: AsRef<OsStr>,Set an environment variable for compiler invocations and other child processes.
ccreads a lot of different variables from the current process' environment. It currently allows the following standard environment variables to be overwritten by this function:SDKROOT*_DEPLOYMENT_TARGETWASI_SDK_PATH
The logic here is "environment variables that the C compiler could itself reasonably have read".
Trait Implementations
impl Clone for Build
fn clone(&self) -> Self
impl Debug for Build
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Default for Build
fn default() -> Build