diff options
author | Valentin Popov <valentin@popov.link> | 2024-07-19 15:37:58 +0300 |
---|---|---|
committer | Valentin Popov <valentin@popov.link> | 2024-07-19 15:37:58 +0300 |
commit | a990de90fe41456a23e58bd087d2f107d321f3a1 (patch) | |
tree | 15afc392522a9e85dc3332235e311b7d39352ea9 /vendor/cc/src | |
parent | 3d48cd3f81164bbfc1a755dc1d4a9a02f98c8ddd (diff) | |
download | fparkan-a990de90fe41456a23e58bd087d2f107d321f3a1.tar.xz fparkan-a990de90fe41456a23e58bd087d2f107d321f3a1.zip |
Deleted vendor folder
Diffstat (limited to 'vendor/cc/src')
-rw-r--r-- | vendor/cc/src/bin/gcc-shim.rs | 70 | ||||
-rw-r--r-- | vendor/cc/src/com.rs | 156 | ||||
-rw-r--r-- | vendor/cc/src/lib.rs | 4024 | ||||
-rw-r--r-- | vendor/cc/src/os_pipe.rs | 28 | ||||
-rw-r--r-- | vendor/cc/src/os_pipe/unix.rs | 121 | ||||
-rw-r--r-- | vendor/cc/src/os_pipe/windows.rs | 24 | ||||
-rw-r--r-- | vendor/cc/src/registry.rs | 190 | ||||
-rw-r--r-- | vendor/cc/src/setup_config.rs | 283 | ||||
-rw-r--r-- | vendor/cc/src/vs_instances.rs | 199 | ||||
-rw-r--r-- | vendor/cc/src/winapi.rs | 146 | ||||
-rw-r--r-- | vendor/cc/src/windows_registry.rs | 896 | ||||
-rw-r--r-- | vendor/cc/src/windows_sys.rs | 198 |
12 files changed, 0 insertions, 6335 deletions
diff --git a/vendor/cc/src/bin/gcc-shim.rs b/vendor/cc/src/bin/gcc-shim.rs deleted file mode 100644 index e5b5372..0000000 --- a/vendor/cc/src/bin/gcc-shim.rs +++ /dev/null @@ -1,70 +0,0 @@ -#![cfg_attr(test, allow(dead_code))] - -use std::env; -use std::fs::File; -use std::io::{self, prelude::*}; -use std::path::PathBuf; - -fn main() { - let mut args = env::args(); - let program = args.next().expect("Unexpected empty args"); - - let out_dir = PathBuf::from( - env::var_os("GCCTEST_OUT_DIR") - .unwrap_or_else(|| panic!("{}: GCCTEST_OUT_DIR not found", program)), - ); - - // Find the first nonexistent candidate file to which the program's args can be written. - let candidate = (0..).find_map(|i| { - let candidate = out_dir.join(format!("out{}", i)); - - if candidate.exists() { - // If the file exists, commands have already run. Try again. - None - } else { - Some(candidate) - } - }).unwrap_or_else(|| panic!("Cannot find the first nonexistent candidate file to which the program's args can be written under out_dir '{}'", out_dir.display())); - - // Create a file and record the args passed to the command. - let f = File::create(&candidate).unwrap_or_else(|e| { - panic!( - "{}: can't create candidate: {}, error: {}", - program, - candidate.display(), - e - ) - }); - let mut f = io::BufWriter::new(f); - - (|| { - for arg in args { - writeln!(f, "{}", arg)?; - } - - f.flush()?; - - let mut f = f.into_inner()?; - f.flush()?; - f.sync_all() - })() - .unwrap_or_else(|e| { - panic!( - "{}: can't write to candidate: {}, error: {}", - program, - candidate.display(), - e - ) - }); - - // Create a file used by some tests. - let path = &out_dir.join("libfoo.a"); - File::create(path).unwrap_or_else(|e| { - panic!( - "{}: can't create libfoo.a: {}, error: {}", - program, - path.display(), - e - ) - }); -} diff --git a/vendor/cc/src/com.rs b/vendor/cc/src/com.rs deleted file mode 100644 index cd22844..0000000 --- a/vendor/cc/src/com.rs +++ /dev/null @@ -1,156 +0,0 @@ -// Copyright © 2017 winapi-rs developers -// Licensed under the Apache License, Version 2.0 -// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. -// All files in the project carrying such notice may not be copied, modified, or distributed -// except according to those terms. - -#![allow(unused)] - -use crate::{ - winapi::{IUnknown, Interface}, - windows_sys::{ - CoInitializeEx, SysFreeString, SysStringLen, BSTR, COINIT_MULTITHREADED, HRESULT, S_FALSE, - S_OK, - }, -}; -use std::{ - ffi::{OsStr, OsString}, - mem::ManuallyDrop, - ops::Deref, - os::windows::ffi::{OsStrExt, OsStringExt}, - ptr::{null, null_mut}, - slice::from_raw_parts, -}; - -pub fn initialize() -> Result<(), HRESULT> { - let err = unsafe { CoInitializeEx(null(), COINIT_MULTITHREADED) }; - if err != S_OK && err != S_FALSE { - // S_FALSE just means COM is already initialized - Err(err) - } else { - Ok(()) - } -} - -pub struct ComPtr<T>(*mut T) -where - T: Interface; -impl<T> ComPtr<T> -where - T: Interface, -{ - /// Creates a `ComPtr` to wrap a raw pointer. - /// It takes ownership over the pointer which means it does __not__ call `AddRef`. - /// `T` __must__ be a COM interface that inherits from `IUnknown`. - pub unsafe fn from_raw(ptr: *mut T) -> ComPtr<T> { - assert!(!ptr.is_null()); - ComPtr(ptr) - } - /// Casts up the inheritance chain - pub fn up<U>(self) -> ComPtr<U> - where - T: Deref<Target = U>, - U: Interface, - { - ComPtr(self.into_raw() as *mut U) - } - /// Extracts the raw pointer. - /// You are now responsible for releasing it yourself. - pub fn into_raw(self) -> *mut T { - ManuallyDrop::new(self).0 - } - /// For internal use only. - fn as_unknown(&self) -> &IUnknown { - unsafe { &*(self.0 as *mut IUnknown) } - } - /// Performs QueryInterface fun. - pub fn cast<U>(&self) -> Result<ComPtr<U>, i32> - where - U: Interface, - { - let mut obj = null_mut(); - let err = unsafe { self.as_unknown().QueryInterface(&U::uuidof(), &mut obj) }; - if err < 0 { - return Err(err); - } - Ok(unsafe { ComPtr::from_raw(obj as *mut U) }) - } -} -impl<T> Deref for ComPtr<T> -where - T: Interface, -{ - type Target = T; - fn deref(&self) -> &T { - unsafe { &*self.0 } - } -} -impl<T> Clone for ComPtr<T> -where - T: Interface, -{ - fn clone(&self) -> Self { - unsafe { - self.as_unknown().AddRef(); - ComPtr::from_raw(self.0) - } - } -} -impl<T> Drop for ComPtr<T> -where - T: Interface, -{ - fn drop(&mut self) { - unsafe { - self.as_unknown().Release(); - } - } -} -pub struct BStr(BSTR); -impl BStr { - pub unsafe fn from_raw(s: BSTR) -> BStr { - BStr(s) - } - pub fn to_osstring(&self) -> OsString { - let len = unsafe { SysStringLen(self.0) }; - let slice = unsafe { from_raw_parts(self.0, len as usize) }; - OsStringExt::from_wide(slice) - } -} -impl Drop for BStr { - fn drop(&mut self) { - unsafe { SysFreeString(self.0) }; - } -} - -pub trait ToWide { - fn to_wide(&self) -> Vec<u16>; - fn to_wide_null(&self) -> Vec<u16>; -} -impl<T> ToWide for T -where - T: AsRef<OsStr>, -{ - fn to_wide(&self) -> Vec<u16> { - self.as_ref().encode_wide().collect() - } - fn to_wide_null(&self) -> Vec<u16> { - self.as_ref().encode_wide().chain(Some(0)).collect() - } -} -pub trait FromWide -where - Self: Sized, -{ - fn from_wide(wide: &[u16]) -> Self; - fn from_wide_null(wide: &[u16]) -> Self { - let len = wide.iter().take_while(|&&c| c != 0).count(); - Self::from_wide(&wide[..len]) - } -} -impl FromWide for OsString { - fn from_wide(wide: &[u16]) -> OsString { - OsStringExt::from_wide(wide) - } -} diff --git a/vendor/cc/src/lib.rs b/vendor/cc/src/lib.rs deleted file mode 100644 index 2fe30b9..0000000 --- a/vendor/cc/src/lib.rs +++ /dev/null @@ -1,4024 +0,0 @@ -//! A library for build scripts to compile custom C code -//! -//! This library is intended to be used as a `build-dependencies` entry in -//! `Cargo.toml`: -//! -//! ```toml -//! [build-dependencies] -//! cc = "1.0" -//! ``` -//! -//! The purpose of this crate is to provide the utility functions necessary to -//! compile C code into a static archive which is then linked into a Rust crate. -//! Configuration is available through the `Build` struct. -//! -//! This crate will automatically detect situations such as cross compilation or -//! other environment variables set by Cargo and will build code appropriately. -//! -//! The crate is not limited to C code, it can accept any source code that can -//! be passed to a C or C++ compiler. As such, assembly files with extensions -//! `.s` (gcc/clang) and `.asm` (MSVC) can also be compiled. -//! -//! [`Build`]: struct.Build.html -//! -//! # Parallelism -//! -//! To parallelize computation, enable the `parallel` feature for the crate. -//! -//! ```toml -//! [build-dependencies] -//! cc = { version = "1.0", features = ["parallel"] } -//! ``` -//! To specify the max number of concurrent compilation jobs, set the `NUM_JOBS` -//! environment variable to the desired amount. -//! -//! Cargo will also set this environment variable when executed with the `-jN` flag. -//! -//! # Examples -//! -//! Use the `Build` struct to compile `src/foo.c`: -//! -//! ```no_run -//! fn main() { -//! cc::Build::new() -//! .file("src/foo.c") -//! .define("FOO", Some("bar")) -//! .include("src") -//! .compile("foo"); -//! } -//! ``` - -#![doc(html_root_url = "https://docs.rs/cc/1.0")] -#![cfg_attr(test, deny(warnings))] -#![allow(deprecated)] -#![deny(missing_docs)] - -use std::borrow::Cow; -use std::collections::{hash_map, HashMap}; -use std::env; -use std::ffi::{OsStr, OsString}; -use std::fmt::{self, Display, Formatter}; -use std::fs::{self, File}; -use std::hash::Hasher; -use std::io::{self, BufRead, BufReader, Read, Write}; -use std::path::{Component, Path, PathBuf}; -use std::process::{Child, Command, Stdio}; -use std::sync::{Arc, Mutex}; -use std::thread::{self, JoinHandle}; - -mod os_pipe; - -// These modules are all glue to support reading the MSVC version from -// the registry and from COM interfaces -#[cfg(windows)] -mod registry; -#[cfg(windows)] -#[macro_use] -mod winapi; -#[cfg(windows)] -mod com; -#[cfg(windows)] -mod setup_config; -#[cfg(windows)] -mod vs_instances; -#[cfg(windows)] -mod windows_sys; - -pub mod windows_registry; - -/// 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. -#[derive(Clone, Debug)] -pub struct Build { - include_directories: Vec<Arc<Path>>, - definitions: Vec<(Arc<str>, Option<Arc<str>>)>, - objects: Vec<Arc<Path>>, - flags: Vec<Arc<str>>, - flags_supported: Vec<Arc<str>>, - known_flag_support_status: Arc<Mutex<HashMap<String, bool>>>, - ar_flags: Vec<Arc<str>>, - asm_flags: Vec<Arc<str>>, - no_default_flags: bool, - files: Vec<Arc<Path>>, - cpp: bool, - cpp_link_stdlib: Option<Option<Arc<str>>>, - cpp_set_stdlib: Option<Arc<str>>, - cuda: bool, - cudart: Option<Arc<str>>, - std: Option<Arc<str>>, - target: Option<Arc<str>>, - host: Option<Arc<str>>, - out_dir: Option<Arc<Path>>, - opt_level: Option<Arc<str>>, - debug: Option<bool>, - force_frame_pointer: Option<bool>, - env: Vec<(Arc<OsStr>, Arc<OsStr>)>, - compiler: Option<Arc<Path>>, - archiver: Option<Arc<Path>>, - ranlib: Option<Arc<Path>>, - cargo_metadata: bool, - link_lib_modifiers: Vec<Arc<str>>, - pic: Option<bool>, - use_plt: Option<bool>, - static_crt: Option<bool>, - shared_flag: Option<bool>, - static_flag: Option<bool>, - warnings_into_errors: bool, - warnings: Option<bool>, - extra_warnings: Option<bool>, - env_cache: Arc<Mutex<HashMap<String, Option<Arc<str>>>>>, - apple_sdk_root_cache: Arc<Mutex<HashMap<String, OsString>>>, - emit_rerun_if_env_changed: bool, -} - -/// Represents the types of errors that may occur while using cc-rs. -#[derive(Clone, Debug)] -enum ErrorKind { - /// Error occurred while performing I/O. - IOError, - /// Invalid architecture supplied. - ArchitectureInvalid, - /// Environment variable not found, with the var in question as extra info. - EnvVarNotFound, - /// Error occurred while using external tools (ie: invocation of compiler). - ToolExecError, - /// Error occurred due to missing external tools. - ToolNotFound, - /// One of the function arguments failed validation. - InvalidArgument, -} - -/// Represents an internal error that occurred, with an explanation. -#[derive(Clone, Debug)] -pub struct Error { - /// Describes the kind of error that occurred. - kind: ErrorKind, - /// More explanation of error that occurred. - message: Cow<'static, str>, -} - -impl Error { - fn new(kind: ErrorKind, message: impl Into<Cow<'static, str>>) -> Error { - Error { - kind, - message: message.into(), - } - } -} - -impl From<io::Error> for Error { - fn from(e: io::Error) -> Error { - Error::new(ErrorKind::IOError, format!("{}", e)) - } -} - -impl Display for Error { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "{:?}: {}", self.kind, self.message) - } -} - -impl std::error::Error for Error {} - -/// Configuration used to represent an invocation of a C compiler. -/// -/// This can be used to figure out what compiler is in use, what the arguments -/// to it are, and what the environment variables look like for the compiler. -/// This can be used to further configure other build systems (e.g. forward -/// along CC and/or CFLAGS) or the `to_command` method can be used to run the -/// compiler itself. -#[derive(Clone, Debug)] -pub struct Tool { - path: PathBuf, - cc_wrapper_path: Option<PathBuf>, - cc_wrapper_args: Vec<OsString>, - args: Vec<OsString>, - env: Vec<(OsString, OsString)>, - family: ToolFamily, - cuda: bool, - removed_args: Vec<OsString>, -} - -/// Represents the family of tools this tool belongs to. -/// -/// Each family of tools differs in how and what arguments they accept. -/// -/// Detection of a family is done on best-effort basis and may not accurately reflect the tool. -#[derive(Copy, Clone, Debug, PartialEq)] -enum ToolFamily { - /// Tool is GNU Compiler Collection-like. - Gnu, - /// Tool is Clang-like. It differs from the GCC in a sense that it accepts superset of flags - /// and its cross-compilation approach is different. - Clang, - /// Tool is the MSVC cl.exe. - Msvc { clang_cl: bool }, -} - -impl ToolFamily { - /// What the flag to request debug info for this family of tools look like - fn add_debug_flags(&self, cmd: &mut Tool, dwarf_version: Option<u32>) { - match *self { - ToolFamily::Msvc { .. } => { - cmd.push_cc_arg("-Z7".into()); - } - ToolFamily::Gnu | ToolFamily::Clang => { - cmd.push_cc_arg( - dwarf_version - .map_or_else(|| "-g".into(), |v| format!("-gdwarf-{}", v)) - .into(), - ); - } - } - } - - /// What the flag to force frame pointers. - fn add_force_frame_pointer(&self, cmd: &mut Tool) { - match *self { - ToolFamily::Gnu | ToolFamily::Clang => { - cmd.push_cc_arg("-fno-omit-frame-pointer".into()); - } - _ => (), - } - } - - /// What the flags to enable all warnings - fn warnings_flags(&self) -> &'static str { - match *self { - ToolFamily::Msvc { .. } => "-W4", - ToolFamily::Gnu | ToolFamily::Clang => "-Wall", - } - } - - /// What the flags to enable extra warnings - fn extra_warnings_flags(&self) -> Option<&'static str> { - match *self { - ToolFamily::Msvc { .. } => None, - ToolFamily::Gnu | ToolFamily::Clang => Some("-Wextra"), - } - } - - /// What the flag to turn warning into errors - fn warnings_to_errors_flag(&self) -> &'static str { - match *self { - ToolFamily::Msvc { .. } => "-WX", - ToolFamily::Gnu | ToolFamily::Clang => "-Werror", - } - } - - fn verbose_stderr(&self) -> bool { - *self == ToolFamily::Clang - } -} - -/// Represents an object. -/// -/// This is a source file -> object file pair. -#[derive(Clone, Debug)] -struct Object { - src: PathBuf, - dst: PathBuf, -} - -impl Object { - /// Create a new source file -> object file pair. - fn new(src: PathBuf, dst: PathBuf) -> Object { - Object { src: src, dst: dst } - } -} - -impl Build { - /// Construct a new instance of a blank set of configuration. - /// - /// This builder is finished with the [`compile`] function. - /// - /// [`compile`]: struct.Build.html#method.compile - pub fn new() -> Build { - Build { - include_directories: Vec::new(), - definitions: Vec::new(), - objects: Vec::new(), - flags: Vec::new(), - flags_supported: Vec::new(), - known_flag_support_status: Arc::new(Mutex::new(HashMap::new())), - ar_flags: Vec::new(), - asm_flags: Vec::new(), - no_default_flags: false, - files: Vec::new(), - shared_flag: None, - static_flag: None, - cpp: false, - cpp_link_stdlib: None, - cpp_set_stdlib: None, - cuda: false, - cudart: None, - std: None, - target: None, - host: None, - out_dir: None, - opt_level: None, - debug: None, - force_frame_pointer: None, - env: Vec::new(), - compiler: None, - archiver: None, - ranlib: None, - cargo_metadata: true, - link_lib_modifiers: Vec::new(), - pic: None, - use_plt: None, - static_crt: None, - warnings: None, - extra_warnings: None, - warnings_into_errors: false, - env_cache: Arc::new(Mutex::new(HashMap::new())), - apple_sdk_root_cache: Arc::new(Mutex::new(HashMap::new())), - emit_rerun_if_env_changed: true, - } - } - - /// Add a directory to the `-I` or include path for headers - /// - /// # Example - /// - /// ```no_run - /// 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"); - /// ``` - pub fn include<P: AsRef<Path>>(&mut self, dir: P) -> &mut Build { - self.include_directories.push(dir.as_ref().into()); - self - } - - /// Add multiple directories to the `-I` include path. - /// - /// # Example - /// - /// ```no_run - /// # 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"); - /// ``` - pub fn includes<P>(&mut self, dirs: P) -> &mut Build - where - P: IntoIterator, - P::Item: AsRef<Path>, - { - for dir in dirs { - self.include(dir); - } - self - } - - /// Specify a `-D` variable with an optional value. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .define("FOO", "BAR") - /// .define("BAZ", None) - /// .compile("foo"); - /// ``` - pub fn define<'a, V: Into<Option<&'a str>>>(&mut self, var: &str, val: V) -> &mut Build { - self.definitions - .push((var.into(), val.into().map(Into::into))); - self - } - - /// Add an arbitrary object file to link in - pub fn object<P: AsRef<Path>>(&mut self, obj: P) -> &mut Build { - self.objects.push(obj.as_ref().into()); - self - } - - /// Add an arbitrary flag to the invocation of the compiler - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .flag("-ffunction-sections") - /// .compile("foo"); - /// ``` - pub fn flag(&mut self, flag: &str) -> &mut Build { - self.flags.push(flag.into()); - self - } - - /// Add a flag to the invocation of the ar - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .file("src/bar.c") - /// .ar_flag("/NODEFAULTLIB:libc.dll") - /// .compile("foo"); - /// ``` - pub fn ar_flag(&mut self, flag: &str) -> &mut Build { - self.ar_flags.push(flag.into()); - self - } - - /// Add a flag that will only be used with assembly files. - /// - /// The flag will be applied to input files with either a `.s` or - /// `.asm` extension (case insensitive). - /// - /// # Example - /// - /// ```no_run - /// 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"); - /// ``` - pub fn asm_flag(&mut self, flag: &str) -> &mut Build { - self.asm_flags.push(flag.into()); - self - } - - fn ensure_check_file(&self) -> Result<PathBuf, Error> { - let out_dir = self.get_out_dir()?; - let src = if self.cuda { - assert!(self.cpp); - out_dir.join("flag_check.cu") - } else if self.cpp { - out_dir.join("flag_check.cpp") - } else { - out_dir.join("flag_check.c") - }; - - if !src.exists() { - let mut f = fs::File::create(&src)?; - write!(f, "int main(void) {{ return 0; }}")?; - } - - Ok(src) - } - - /// 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_dir` failed). - /// - /// Note: Once computed, the result of this call is stored in the - /// `known_flag_support` field. If `is_flag_supported(flag)` - /// is called again, the result will be read from the hash table. - pub fn is_flag_supported(&self, flag: &str) -> Result<bool, Error> { - let mut known_status = self.known_flag_support_status.lock().unwrap(); - if let Some(is_supported) = known_status.get(flag).cloned() { - return Ok(is_supported); - } - - let out_dir = self.get_out_dir()?; - let src = self.ensure_check_file()?; - let obj = out_dir.join("flag_check"); - let target = self.get_target()?; - let host = self.get_host()?; - let mut cfg = Build::new(); - cfg.flag(flag) - .target(&target) - .opt_level(0) - .host(&host) - .debug(false) - .cpp(self.cpp) - .cuda(self.cuda); - if let Some(ref c) = self.compiler { - cfg.compiler(c.clone()); - } - let mut compiler = cfg.try_get_compiler()?; - - // Clang uses stderr for verbose output, which yields a false positive - // result if the CFLAGS/CXXFLAGS include -v to aid in debugging. - if compiler.family.verbose_stderr() { - compiler.remove_arg("-v".into()); - } - - let mut cmd = compiler.to_command(); - let is_arm = target.contains("aarch64") || target.contains("arm"); - let clang = compiler.family == ToolFamily::Clang; - let gnu = compiler.family == ToolFamily::Gnu; - command_add_output_file( - &mut cmd, - &obj, - self.cuda, - target.contains("msvc"), - clang, - gnu, - false, - is_arm, - ); - - // We need to explicitly tell msvc not to link and create an exe - // in the root directory of the crate - if target.contains("msvc") && !self.cuda { - cmd.arg("-c"); - } - - cmd.arg(&src); - - let output = cmd.output()?; - let is_supported = output.status.success() && output.stderr.is_empty(); - - known_status.insert(flag.to_owned(), is_supported); - Ok(is_supported) - } - - /// Add an arbitrary flag to the invocation of the compiler if it supports it - /// - /// # Example - /// - /// ```no_run - /// 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"); - /// ``` - pub fn flag_if_supported(&mut self, flag: &str) -> &mut Build { - self.flags_supported.push(flag.into()); - self - } - - /// Add flags from the specified environment variable. - /// - /// Normally the `cc` crate will consult with the standard set of environment - /// variables (such as `CFLAGS` and `CXXFLAGS`) 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. - /// - /// ```no_run - /// 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"); - /// ``` - /// - pub fn try_flags_from_environment(&mut self, environ_key: &str) -> Result<&mut Build, Error> { - let flags = self.envflags(environ_key)?; - self.flags.extend(flags.into_iter().map(Into::into)); - Ok(self) - } - - /// Set the `-shared` flag. - /// - /// When enabled, the compiler will produce a shared object which can - /// then be linked with other objects to form an executable. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .shared_flag(true) - /// .compile("libfoo.so"); - /// ``` - pub fn shared_flag(&mut self, shared_flag: bool) -> &mut Build { - self.shared_flag = Some(shared_flag); - self - } - - /// Set the `-static` flag. - /// - /// When enabled on systems that support dynamic linking, this prevents - /// linking with the shared libraries. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .shared_flag(true) - /// .static_flag(true) - /// .compile("foo"); - /// ``` - pub fn static_flag(&mut self, static_flag: bool) -> &mut Build { - self.static_flag = Some(static_flag); - self - } - - /// Disables the generation of default compiler flags. The default compiler - /// flags may cause conflicts in some cross compiling scenarios. - /// - /// Setting the `CRATE_CC_NO_DEFAULTS` environment variable has the same - /// effect as setting this to `true`. The presence of the environment - /// variable and the value of `no_default_flags` will be OR'd together. - pub fn no_default_flags(&mut self, no_default_flags: bool) -> &mut Build { - self.no_default_flags = no_default_flags; - self - } - - /// Add a file which will be compiled - pub fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut Build { - self.files.push(p.as_ref().into()); - self - } - - /// Add files which will be compiled - pub fn files<P>(&mut self, p: P) -> &mut Build - where - P: IntoIterator, - P::Item: AsRef<Path>, - { - for file in p.into_iter() { - self.file(file); - } - self - } - - /// Set C++ support. - /// - /// The other `cpp_*` options will only become active if this is set to - /// `true`. - /// - /// The name of the C++ standard library to link is decided by: - /// 1. If [cpp_link_stdlib](Build::cpp_link_stdlib) is set, use its value. - /// 2. Else if the `CXXSTDLIB` environment variable is set, use its value. - /// 3. Else the default is `libc++` for OS X and BSDs, `libc++_shared` for Android, - /// `None` for MSVC and `libstdc++` for anything else. - pub fn cpp(&mut self, cpp: bool) -> &mut Build { - self.cpp = cpp; - self - } - - /// Set 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 for - /// `nvcc`, 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. - pub fn cuda(&mut self, cuda: bool) -> &mut Build { - self.cuda = cuda; - if cuda { - self.cpp = true; - self.cudart = Some("static".into()); - } - self - } - - /// Link CUDA run-time. - /// - /// This option mimics the `--cudart` NVCC command-line option. Just like - /// the original it accepts `{none|shared|static}`, with default being - /// `static`. The method has to be invoked after `.cuda(true)`, or not - /// at all, if the default is right for the project. - pub fn cudart(&mut self, cudart: &str) -> &mut Build { - if self.cuda { - self.cudart = Some(cudart.into()); - } - self - } - - /// Specify the C or C++ language standard version. - /// - /// These values are common to modern versions of GCC, Clang and MSVC: - /// - `c11` for ISO/IEC 9899:2011 - /// - `c17` for ISO/IEC 9899:2018 - /// - `c++14` for ISO/IEC 14882:2014 - /// - `c++17` for ISO/IEC 14882:2017 - /// - `c++20` for ISO/IEC 14882:2020 - /// - /// Other values have less broad support, e.g. MSVC does not support `c++11` - /// (`c++14` is the minimum), `c89` (omit the flag instead) or `c99`. - /// - /// 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 - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/modern.cpp") - /// .cpp(true) - /// .std("c++17") - /// .compile("modern"); - /// ``` - pub fn std(&mut self, std: &str) -> &mut Build { - self.std = Some(std.into()); - self - } - - /// Set 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 - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .warnings_into_errors(true) - /// .compile("libfoo.a"); - /// ``` - pub fn warnings_into_errors(&mut self, warnings_into_errors: bool) -> &mut Build { - self.warnings_into_errors = warnings_into_errors; - self - } - - /// Set warnings flags. - /// - /// Adds some flags: - /// - "-Wall" for MSVC. - /// - "-Wall", "-Wextra" for GNU and Clang. - /// - /// Enabled by default. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .warnings(false) - /// .compile("libfoo.a"); - /// ``` - pub fn warnings(&mut self, warnings: bool) -> &mut Build { - self.warnings = Some(warnings); - self.extra_warnings = Some(warnings); - self - } - - /// Set extra warnings flags. - /// - /// Adds some flags: - /// - nothing for MSVC. - /// - "-Wextra" for GNU and Clang. - /// - /// Enabled by default. - /// - /// # Example - /// - /// ```no_run - /// // Disables -Wextra, -Wall remains enabled: - /// cc::Build::new() - /// .file("src/foo.c") - /// .extra_warnings(false) - /// .compile("libfoo.a"); - /// ``` - pub fn extra_warnings(&mut self, warnings: bool) -> &mut Build { - self.extra_warnings = Some(warnings); - self - } - - /// Set the standard library to link against when compiling with C++ - /// support. - /// - /// If the `CXXSTDLIB` environment variable is set, its value will - /// override the default value, but not the value explicitly set by calling - /// this function. - /// - /// A value of `None` indicates that no automatic linking should happen, - /// otherwise cargo will link against the specified library. - /// - /// The given library name must not contain the `lib` prefix. - /// - /// Common values: - /// - `stdc++` for GNU - /// - `c++` for Clang - /// - `c++_shared` or `c++_static` for Android - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .shared_flag(true) - /// .cpp_link_stdlib("stdc++") - /// .compile("libfoo.so"); - /// ``` - pub fn cpp_link_stdlib<'a, V: Into<Option<&'a str>>>( - &mut self, - cpp_link_stdlib: V, - ) -> &mut Build { - self.cpp_link_stdlib = Some(cpp_link_stdlib.into().map(|s| s.into())); - self - } - - /// Force the C++ compiler to use the specified standard library. - /// - /// Setting this option will automatically set `cpp_link_stdlib` to 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 `-stdlib` flag, 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 `-stdlib` flag. - /// - /// A value of `None` indicates that no specific C++ standard library should - /// be used, otherwise `-stdlib` is added to the compile invocation. - /// - /// The given library name must not contain the `lib` prefix. - /// - /// Common values: - /// - `stdc++` for GNU - /// - `c++` for Clang - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .cpp_set_stdlib("c++") - /// .compile("libfoo.a"); - /// ``` - pub fn cpp_set_stdlib<'a, V: Into<Option<&'a str>>>( - &mut self, - cpp_set_stdlib: V, - ) -> &mut Build { - let cpp_set_stdlib = cpp_set_stdlib.into(); - self.cpp_set_stdlib = cpp_set_stdlib.map(|s| s.into()); - self.cpp_link_stdlib(cpp_set_stdlib); - self - } - - /// Configures the target this configuration will be compiling for. - /// - /// This option is automatically scraped from the `TARGET` environment - /// variable by build scripts, so it's not required to call this function. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .target("aarch64-linux-android") - /// .compile("foo"); - /// ``` - pub fn target(&mut self, target: &str) -> &mut Build { - self.target = Some(target.into()); - self - } - - /// Configures the host assumed by this configuration. - /// - /// This option is automatically scraped from the `HOST` environment - /// variable by build scripts, so it's not required to call this function. - /// - /// # Example - /// - /// ```no_run - /// cc::Build::new() - /// .file("src/foo.c") - /// .host("arm-linux-gnueabihf") - /// .compile("foo"); - /// ``` - pub fn host(&mut self, host: &str) -> &mut Build { - self.host = Some(host.into()); - self - } - - /// Configures the optimization level of the generated object files. - /// - /// This option is automatically scraped from the `OPT_LEVEL` environment - /// variable by build scripts, so it's not required to call this function. - pub fn opt_level(&mut self, opt_level: u32) -> &mut Build { - self.opt_level = Some(opt_level.to_string().into()); - self - } - - /// Configures the optimization level of the generated object files. - /// - /// This option is automatically scraped from the `OPT_LEVEL` environment - /// variable by build scripts, so it's not required to call this function. - pub fn opt_level_str(&mut self, opt_level: &str) -> &mut Build { - self.opt_level = Some(opt_level.into()); - self - } - - /// Configures whether the compiler will emit debug information when - /// generating object files. - /// - /// This option is automatically scraped from the `DEBUG` environment - /// variable by build scripts, so it's not required to call this function. - pub fn debug(&mut self, debug: bool) -> &mut Build { - self.debug = Some(debug); - self - } - - /// Configures 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. - pub fn force_frame_pointer(&mut self, force: bool) -> &mut Build { - self.force_frame_pointer = Some(force); - self - } - - /// Configures the output directory where all object files and static - /// libraries will be located. - /// - /// This option is automatically scraped from the `OUT_DIR` environment - /// variable by build scripts, so it's not required to call this function. - pub fn out_dir<P: AsRef<Path>>(&mut self, out_dir: P) -> &mut Build { - self.out_dir = Some(out_dir.as_ref().into()); - self - } - - /// Configures 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. - pub fn compiler<P: AsRef<Path>>(&mut self, compiler: P) -> &mut Build { - self.compiler = Some(compiler.as_ref().into()); - self - } - - /// Configures 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. - pub fn archiver<P: AsRef<Path>>(&mut self, archiver: P) -> &mut Build { - self.archiver = Some(archiver.as_ref().into()); - self - } - - /// Configures 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. - pub fn ranlib<P: AsRef<Path>>(&mut self, ranlib: P) -> &mut Build { - self.ranlib = Some(ranlib.as_ref().into()); - self - } - - /// Define 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 lib* - /// - `rustc-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_changed` is not `false`, `rerun-if-env-changed=`*env* - /// - pub fn cargo_metadata(&mut self, cargo_metadata: bool) -> &mut Build { - self.cargo_metadata = cargo_metadata; - self - } - - /// Adds a native library modifier that will be added to the - /// `rustc-link-lib=static:MODIFIERS=LIBRARY_NAME` metadata line - /// emitted for cargo if `cargo_metadata` is 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. - pub fn link_lib_modifier(&mut self, link_lib_modifier: &str) -> &mut Build { - self.link_lib_modifiers.push(link_lib_modifier.into()); - self - } - - /// Configures whether the compiler will emit position independent code. - /// - /// This option defaults to `false` for `windows-gnu` and bare metal targets and - /// to `true` for all other targets. - pub fn pic(&mut self, pic: bool) -> &mut Build { - self.pic = Some(pic); - self - } - - /// Configures 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_plt` to `false` can 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. - pub fn use_plt(&mut self, use_plt: bool) -> &mut Build { - self.use_plt = Some(use_plt); - self - } - - /// Define whether metadata should be emitted for cargo to detect environment - /// changes that should trigger a rebuild. - /// - /// This has no effect if the `cargo_metadata` option is `false`. - /// - /// This option defaults to `true`. - pub fn emit_rerun_if_env_changed(&mut self, emit_rerun_if_env_changed: bool) -> &mut Build { - self.emit_rerun_if_env_changed = emit_rerun_if_env_changed; - self - } - - /// Configures 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. - pub fn static_crt(&mut self, static_crt: bool) -> &mut Build { - self.static_crt = Some(static_crt); - self - } - - #[doc(hidden)] - pub fn __set_env<A, B>(&mut self, a: A, b: B) -> &mut Build - where - A: AsRef<OsStr>, - B: AsRef<OsStr>, - { - self.env.push((a.as_ref().into(), b.as_ref().into())); - self - } - - /// Run the compiler, generating the file `output` - /// - /// This will return a result instead of panicing; see compile() for the complete description. - pub fn try_compile(&self, output: &str) -> Result<(), Error> { - let mut output_components = Path::new(output).components(); - match (output_components.next(), output_components.next()) { - (Some(Component::Normal(_)), None) => {} - _ => { - return Err(Error::new( - ErrorKind::InvalidArgument, - "argument of `compile` must be a single normal path component", - )); - } - } - - let (lib_name, gnu_lib_name) = if output.starts_with("lib") && output.ends_with(".a") { - (&output[3..output.len() - 2], output.to_owned()) - } else { - let mut gnu = String::with_capacity(5 + output.len()); - gnu.push_str("lib"); - gnu.push_str(&output); - gnu.push_str(".a"); - (output, gnu) - }; - let dst = self.get_out_dir()?; - - let mut objects = Vec::new(); - for file in self.files.iter() { - let obj = if file.has_root() || file.components().any(|x| x == Component::ParentDir) { - // If `file` is an absolute path or might not be usable directly as a suffix due to - // using "..", use the `basename` prefixed with the `dirname`'s hash to ensure name - // uniqueness. - let basename = file - .file_name() - .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "file_name() failure"))? - .to_string_lossy(); - let dirname = file - .parent() - .ok_or_else(|| Error::new(ErrorKind::InvalidArgument, "parent() failure"))? - .to_string_lossy(); - let mut hasher = hash_map::DefaultHasher::new(); - hasher.write(dirname.to_string().as_bytes()); - dst.join(format!("{:016x}-{}", hasher.finish(), basename)) - .with_extension("o") - } else { - dst.join(file).with_extension("o") - }; - let obj = if !obj.starts_with(&dst) { - dst.join(obj.file_name().ok_or_else(|| { - Error::new(ErrorKind::IOError, "Getting object file details failed.") - })?) - } else { - obj - }; - - match obj.parent() { - Some(s) => fs::create_dir_all(s)?, - None => { - return Err(Error::new( - ErrorKind::IOError, - "Getting object file details failed.", - )); - } - }; - - objects.push(Object::new(file.to_path_buf(), obj)); - } - - let print = PrintThread::new()?; - - self.compile_objects(&objects, &print)?; - self.assemble(lib_name, &dst.join(gnu_lib_name), &objects, &print)?; - - if self.get_target()?.contains("msvc") { - let compiler = self.get_base_compiler()?; - let atlmfc_lib = compiler - .env() - .iter() - .find(|&&(ref var, _)| var.as_os_str() == OsStr::new("LIB")) - .and_then(|&(_, ref lib_paths)| { - env::split_paths(lib_paths).find(|path| { - let sub = Path::new("atlmfc/lib"); - path.ends_with(sub) || path.parent().map_or(false, |p| p.ends_with(sub)) - }) - }); - - if let Some(atlmfc_lib) = atlmfc_lib { - self.print(&format_args!( - "cargo:rustc-link-search=native={}", - atlmfc_lib.display() - )); - } - } - - if self.link_lib_modifiers.is_empty() { - self.print(&format_args!("cargo:rustc-link-lib=static={}", lib_name)); - } else { - let m = self.link_lib_modifiers.join(","); - self.print(&format_args!( - "cargo:rustc-link-lib=static:{}={}", - m, lib_name - )); - } - self.print(&format_args!( - "cargo:rustc-link-search=native={}", - dst.display() - )); - - // Add specific C++ libraries, if enabled. - if self.cpp { - if let Some(stdlib) = self.get_cpp_link_stdlib()? { - self.print(&format_args!("cargo:rustc-link-lib={}", stdlib)); - } - } - - let cudart = match &self.cudart { - Some(opt) => &*opt, // {none|shared|static} - None => "none", - }; - if cudart != "none" { - if let Some(nvcc) = which(&self.get_compiler().path, None) { - // Try to figure out the -L search path. If it fails, - // it's on user to specify one by passing it through - // RUSTFLAGS environment variable. - let mut libtst = false; - let mut libdir = nvcc; - libdir.pop(); // remove 'nvcc' - libdir.push(".."); - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap(); - if cfg!(target_os = "linux") { - libdir.push("targets"); - libdir.push(target_arch.to_owned() + "-linux"); - libdir.push("lib"); - libtst = true; - } else if cfg!(target_env = "msvc") { - libdir.push("lib"); - match target_arch.as_str() { - "x86_64" => { - libdir.push("x64"); - libtst = true; - } - "x86" => { - libdir.push("Win32"); - libtst = true; - } - _ => libtst = false, - } - } - if libtst && libdir.is_dir() { - println!( - "cargo:rustc-link-search=native={}", - libdir.to_str().unwrap() - ); - } - - // And now the -l flag. - let lib = match cudart { - "shared" => "cudart", - "static" => "cudart_static", - bad => panic!("unsupported cudart option: {}", bad), - }; - println!("cargo:rustc-link-lib={}", lib); - } - } - - Ok(()) - } - - /// Run the compiler, generating the file `output` - /// - /// # Library name - /// - /// The `output` string 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 `output` is close to arbitrary, but: - /// - /// - must be nonempty, - /// - must not contain a path separator (`/`), - /// - must be unique across all `compile` invocations 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: - /// - /// ```no_run - /// 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 `output` starts with "lib" *and* ends - /// with ".a", a second "lib" prefix and ".a" suffix do not get added on, - /// but this usage is deprecated; please omit `lib` and `.a` in the argument - /// that you pass. - /// - /// # Panics - /// - /// Panics if `output` is 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. - pub fn compile(&self, output: &str) { - if let Err(e) = self.try_compile(output) { - fail(&e.message); - } - } - - #[cfg(feature = "parallel")] - fn compile_objects(&self, objs: &[Object], print: &PrintThread) -> Result<(), Error> { - use std::sync::{mpsc, Once}; - - if objs.len() <= 1 { - for obj in objs { - let (mut cmd, name) = self.create_compile_object_cmd(obj)?; - run(&mut cmd, &name, print)?; - } - - return Ok(()); - } - - // Limit our parallelism globally with a jobserver. Start off by - // releasing our own token for this process so we can have a bit of an - // easier to write loop below. If this fails, though, then we're likely - // on Windows with the main implicit token, so we just have a bit extra - // parallelism for a bit and don't reacquire later. - let server = jobserver(); - // Reacquire our process's token on drop - let _reacquire = server.release_raw().ok().map(|_| JobserverToken(server)); - - // When compiling objects in parallel we do a few dirty tricks to speed - // things up: - // - // * First is that we use the `jobserver` crate to limit the parallelism - // of this build script. The `jobserver` crate will use a jobserver - // configured by Cargo for build scripts to ensure that parallelism is - // coordinated across C compilations and Rust compilations. Before we - // compile anything we make sure to wait until we acquire a token. - // - // Note that this jobserver is cached globally so we only used one per - // process and only worry about creating it once. - // - // * Next we use spawn the process to actually compile objects in - // parallel after we've acquired a token to perform some work - // - // With all that in mind we compile all objects in a loop here, after we - // acquire the appropriate tokens, Once all objects have been compiled - // we wait on all the processes and propagate the results of compilation. - - let (tx, rx) = mpsc::channel::<(_, String, KillOnDrop, _)>(); - - // Since jobserver::Client::acquire can block, waiting - // must be done in parallel so that acquire won't block forever. - let wait_thread = thread::Builder::new().spawn(move || { - let mut error = None; - let mut pendings = Vec::new(); - // Buffer the stdout - let mut stdout = io::BufWriter::with_capacity(128, io::stdout()); - let mut backoff_cnt = 0; - - loop { - let mut has_made_progress = false; - - // Reading new pending tasks - loop { - match rx.try_recv() { - Ok(pending) => { - has_made_progress = true; - pendings.push(pending) - } - Err(mpsc::TryRecvError::Disconnected) if pendings.is_empty() => { - let _ = stdout.flush(); - return if let Some(err) = error { - Err(err) - } else { - Ok(()) - }; - } - _ => break, - } - } - - // Try waiting on them. - pendings.retain_mut(|(cmd, program, child, _)| { - match try_wait_on_child(cmd, program, &mut child.0, &mut stdout) { - Ok(Some(())) => { - // Task done, remove the entry - has_made_progress = true; - false - } - Ok(None) => true, // Task still not finished, keep the entry - Err(err) => { - // Task fail, remove the entry. - has_made_progress = true; - - // Since we can only return one error, log the error to make - // sure users always see all the compilation failures. - let _ = writeln!(stdout, "cargo:warning={}", err); - error = Some(err); - - false - } - } - }); - - if !has_made_progress { - if backoff_cnt > 3 { - // We have yielded at least three times without making' - // any progress, so we will sleep for a while. - let duration = - std::time::Duration::from_millis(100 * (backoff_cnt - 3).min(10)); - thread::sleep(duration); - } else { - // Given that we spawned a lot of compilation tasks, it is unlikely - // that OS cannot find other ready task to execute. - // - // If all of them are done, then we will yield them and spawn more, - // or simply returns. - // - // Thus this will not be turned into a busy-wait loop and it will not - // waste CPU resource. - thread::yield_now(); - } - } - - backoff_cnt = if has_made_progress { - 0 - } else { - backoff_cnt + 1 - }; - } - })?; - - for obj in objs { - let (mut cmd, program) = self.create_compile_object_cmd(obj)?; - let token = server.acquire()?; - - let child = spawn(&mut cmd, &program, print.pipe_writer_cloned()?.unwrap())?; - - tx.send((cmd, program, KillOnDrop(child), token)) - .expect("Wait thread must be alive until all compilation jobs are done, otherwise we risk deadlock"); - } - // Drop tx so that the wait_thread could return - drop(tx); - - return wait_thread.join().expect("wait_thread panics"); - - /// Returns a suitable `jobserver::Client` used to coordinate - /// parallelism between build scripts. - fn jobserver() -> &'static jobserver::Client { - static INIT: Once = Once::new(); - static mut JOBSERVER: Option<jobserver::Client> = None; - - fn _assert_sync<T: Sync>() {} - _assert_sync::<jobserver::Client>(); - - unsafe { - INIT.call_once(|| { - let server = default_jobserver(); - JOBSERVER = Some(server); - }); - JOBSERVER.as_ref().unwrap() - } - } - - unsafe fn default_jobserver() -> jobserver::Client { - // Try to use the environmental jobserver which Cargo typically - // initializes for us... - if let Some(client) = jobserver::Client::from_env() { - return client; - } - - // ... but if that fails for whatever reason select something - // reasonable and crate a new jobserver. Use `NUM_JOBS` if set (it's - // configured by Cargo) and otherwise just fall back to a - // semi-reasonable number. Note that we could use `num_cpus` here - // but it's an extra dependency that will almost never be used, so - // it's generally not too worth it. - let mut parallelism = 4; - if let Ok(amt) = env::var("NUM_JOBS") { - if let Ok(amt) = amt.parse() { - parallelism = amt; - } - } - - // If we create our own jobserver then be sure to reserve one token - // for ourselves. - let client = jobserver::Client::new(parallelism).expect("failed to create jobserver"); - client.acquire_raw().expect("failed to acquire initial"); - return client; - } - - struct KillOnDrop(Child); - - impl Drop for KillOnDrop { - fn drop(&mut self) { - let child = &mut self.0; - - child.kill().ok(); - } - } - - struct JobserverToken(&'static jobserver::Client); - impl Drop for JobserverToken { - fn drop(&mut self) { - let _ = self.0.acquire_raw(); - } - } - } - - #[cfg(not(feature = "parallel"))] - fn compile_objects(&self, objs: &[Object], print: &PrintThread) -> Result<(), Error> { - for obj in objs { - let (mut cmd, name) = self.create_compile_object_cmd(obj)?; - run(&mut cmd, &name, print)?; - } - - Ok(()) - } - - fn create_compile_object_cmd(&self, obj: &Object) -> Result<(Command, String), Error> { - let asm_ext = AsmFileExt::from_path(&obj.src); - let is_asm = asm_ext.is_some(); - let target = self.get_target()?; - let msvc = target.contains("msvc"); - let compiler = self.try_get_compiler()?; - let clang = compiler.family == ToolFamily::Clang; - let gnu = compiler.family == ToolFamily::Gnu; - - let (mut cmd, name) = if msvc && asm_ext == Some(AsmFileExt::DotAsm) { - self.msvc_macro_assembler()? - } else { - let mut cmd = compiler.to_command(); - for &(ref a, ref b) in self.env.iter() { - cmd.env(a, b); - } - ( - cmd, - compiler - .path - .file_name() - .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path."))? - .to_string_lossy() - .into_owned(), - ) - }; - let is_arm = target.contains("aarch64") || target.contains("arm"); - command_add_output_file( - &mut cmd, &obj.dst, self.cuda, msvc, clang, gnu, is_asm, is_arm, - ); - // armasm and armasm64 don't requrie -c option - if !msvc || !is_asm || !is_arm { - cmd.arg("-c"); - } - if self.cuda && self.cuda_file_count() > 1 { - cmd.arg("--device-c"); - } - if is_asm { - cmd.args(self.asm_flags.iter().map(std::ops::Deref::deref)); - } - if compiler.family == (ToolFamily::Msvc { clang_cl: true }) && !is_asm { - // #513: For `clang-cl`, separate flags/options from the input file. - // When cross-compiling macOS -> Windows, this avoids interpreting - // common `/Users/...` paths as the `/U` flag and triggering - // `-Wslash-u-filename` warning. - cmd.arg("--"); - } - cmd.arg(&obj.src); - if cfg!(target_os = "macos") { - self.fix_env_for_apple_os(&mut cmd)?; - } - - Ok((cmd, name)) - } - - /// This will return a result instead of panicing; see expand() for the complete description. - pub fn try_expand(&self) -> Result<Vec<u8>, Error> { - let compiler = self.try_get_compiler()?; - let mut cmd = compiler.to_command(); - for &(ref a, ref b) in self.env.iter() { - cmd.env(a, b); - } - cmd.arg("-E"); - - assert!( - self.files.len() <= 1, - "Expand may only be called for a single file" - ); - - cmd.args(self.files.iter().map(std::ops::Deref::deref)); - - let name = compiler - .path - .file_name() - .ok_or_else(|| Error::new(ErrorKind::IOError, "Failed to get compiler path."))? - .to_string_lossy() - .into_owned(); - - Ok(run_output(&mut cmd, &name)?) - } - - /// 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 - /// ```no_run - /// let out = cc::Build::new().file("src/foo.c").expand(); - /// ``` - pub fn expand(&self) -> Vec<u8> { - match self.try_expand() { - Err(e) => fail(&e.message), - Ok(v) => v, - } - } - - /// Get the compiler that's in use for this configuration. - /// - /// This function will return a `Tool` which 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 the `to_command` method 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. - pub fn get_compiler(&self) -> Tool { - match self.try_get_compiler() { - Ok(tool) => tool, - Err(e) => fail(&e.message), - } - } - - /// Get the compiler that's in use for this configuration. - /// - /// This will return a result instead of panicing; see get_compiler() for the complete description. - pub fn try_get_compiler(&self) -> Result<Tool, Error> { - let opt_level = self.get_opt_level()?; - let target = self.get_target()?; - - let mut cmd = self.get_base_compiler()?; - - // Disable default flag generation via `no_default_flags` or environment variable - let no_defaults = self.no_default_flags || self.getenv("CRATE_CC_NO_DEFAULTS").is_some(); - - if !no_defaults { - self.add_default_flags(&mut cmd, &target, &opt_level)?; - } else { - println!("Info: default compiler flags are disabled"); - } - - if let Some(ref std) = self.std { - let separator = match cmd.family { - ToolFamily::Msvc { .. } => ':', - ToolFamily::Gnu | ToolFamily::Clang => '=', - }; - cmd.push_cc_arg(format!("-std{}{}", separator, std).into()); - } - - if let Ok(flags) = self.envflags(if self.cpp { "CXXFLAGS" } else { "CFLAGS" }) { - for arg in flags { - cmd.push_cc_arg(arg.into()); - } - } - - for directory in self.include_directories.iter() { - cmd.args.push("-I".into()); - cmd.args.push((**directory).into()); - } - - // If warnings and/or extra_warnings haven't been explicitly set, - // then we set them only if the environment doesn't already have - // CFLAGS/CXXFLAGS, since those variables presumably already contain - // the desired set of warnings flags. - - if self - .warnings - .unwrap_or(if self.has_flags() { false } else { true }) - { - let wflags = cmd.family.warnings_flags().into(); - cmd.push_cc_arg(wflags); - } - - if self - .extra_warnings - .unwrap_or(if self.has_flags() { false } else { true }) - { - if let Some(wflags) = cmd.family.extra_warnings_flags() { - cmd.push_cc_arg(wflags.into()); - } - } - - for flag in self.flags.iter() { - cmd.args.push((**flag).into()); - } - - for flag in self.flags_supported.iter() { - if self.is_flag_supported(flag).unwrap_or(false) { - cmd.push_cc_arg((**flag).into()); - } - } - - for &(ref key, ref value) in self.definitions.iter() { - if let Some(ref value) = *value { - cmd.args.push(format!("-D{}={}", key, value).into()); - } else { - cmd.args.push(format!("-D{}", key).into()); - } - } - - if self.warnings_into_errors { - let warnings_to_errors_flag = cmd.family.warnings_to_errors_flag().into(); - cmd.push_cc_arg(warnings_to_errors_flag); - } - - Ok(cmd) - } - - fn add_default_flags( - &self, - cmd: &mut Tool, - target: &str, - opt_level: &str, - ) -> Result<(), Error> { - // Non-target flags - // If the flag is not conditioned on target variable, it belongs here :) - match cmd.family { - ToolFamily::Msvc { .. } => { - cmd.push_cc_arg("-nologo".into()); - - let crt_flag = match self.static_crt { - Some(true) => "-MT", - Some(false) => "-MD", - None => { - let features = self.getenv("CARGO_CFG_TARGET_FEATURE"); - let features = features.as_deref().unwrap_or_default(); - if features.contains("crt-static") { - "-MT" - } else { - "-MD" - } - } - }; - cmd.push_cc_arg(crt_flag.into()); - - match &opt_level[..] { - // Msvc uses /O1 to enable all optimizations that minimize code size. - "z" | "s" | "1" => cmd.push_opt_unless_duplicate("-O1".into()), - // -O3 is a valid value for gcc and clang compilers, but not msvc. Cap to /O2. - "2" | "3" => cmd.push_opt_unless_duplicate("-O2".into()), - _ => {} - } - } - ToolFamily::Gnu | ToolFamily::Clang => { - // arm-linux-androideabi-gcc 4.8 shipped with Android NDK does - // not support '-Oz' - if opt_level == "z" && cmd.family != ToolFamily::Clang { - cmd.push_opt_unless_duplicate("-Os".into()); - } else { - cmd.push_opt_unless_duplicate(format!("-O{}", opt_level).into()); - } - - if cmd.family == ToolFamily::Clang && target.contains("windows") { - // Disambiguate mingw and msvc on Windows. Problem is that - // depending on the origin clang can default to a mismatchig - // run-time. - cmd.push_cc_arg(format!("--target={}", target).into()); - } - - if cmd.family == ToolFamily::Clang && target.contains("android") { - // For compatibility with code that doesn't use pre-defined `__ANDROID__` macro. - // If compiler used via ndk-build or cmake (officially supported build methods) - // this macros is defined. - // See https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/cmake/android.toolchain.cmake#456 - // https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/build/core/build-binary.mk#141 - cmd.push_opt_unless_duplicate("-DANDROID".into()); - } - - if !target.contains("apple-ios") && !target.contains("apple-watchos") { - cmd.push_cc_arg("-ffunction-sections".into()); - cmd.push_cc_arg("-fdata-sections".into()); - } - // Disable generation of PIC on bare-metal for now: rust-lld doesn't support this yet - if self.pic.unwrap_or( - !target.contains("windows") - && !target.contains("-none-") - && !target.contains("uefi"), - ) { - cmd.push_cc_arg("-fPIC".into()); - // PLT only applies if code is compiled with PIC support, - // and only for ELF targets. - if target.contains("linux") && !self.use_plt.unwrap_or(true) { - cmd.push_cc_arg("-fno-plt".into()); - } - } - } - } - - if self.get_debug() { - if self.cuda { - // NVCC debug flag - cmd.args.push("-G".into()); - } - let family = cmd.family; - family.add_debug_flags(cmd, self.get_dwarf_version()); - } - - if self.get_force_frame_pointer() { - let family = cmd.family; - family.add_force_frame_pointer(cmd); - } - - // Target flags - match cmd.family { - ToolFamily::Clang => { - if !(target.contains("android") - && android_clang_compiler_uses_target_arg_internally(&cmd.path)) - { - if target.contains("darwin") { - if let Some(arch) = - map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args - .push(format!("--target={}-apple-darwin", arch).into()); - } - } else if target.contains("macabi") { - if let Some(arch) = - map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args - .push(format!("--target={}-apple-ios-macabi", arch).into()); - } - } else if target.contains("ios-sim") { - if let Some(arch) = - map_darwin_target_from_rust_to_compiler_architecture(target) - { - let deployment_target = env::var("IPHONEOS_DEPLOYMENT_TARGET") - .unwrap_or_else(|_| "7.0".into()); - cmd.args.push( - format!( - "--target={}-apple-ios{}-simulator", - arch, deployment_target - ) - .into(), - ); - } - } else if target.contains("watchos-sim") { - if let Some(arch) = - map_darwin_target_from_rust_to_compiler_architecture(target) - { - let deployment_target = env::var("WATCHOS_DEPLOYMENT_TARGET") - .unwrap_or_else(|_| "5.0".into()); - cmd.args.push( - format!( - "--target={}-apple-watchos{}-simulator", - arch, deployment_target - ) - .into(), - ); - } - } else if target.starts_with("riscv64gc-") { - cmd.args.push( - format!("--target={}", target.replace("riscv64gc", "riscv64")).into(), - ); - } else if target.starts_with("riscv32gc-") { - cmd.args.push( - format!("--target={}", target.replace("riscv32gc", "riscv32")).into(), - ); - } else if target.contains("uefi") { - if target.contains("x86_64") { - cmd.args.push("--target=x86_64-unknown-windows-gnu".into()); - } else if target.contains("i686") { - cmd.args.push("--target=i686-unknown-windows-gnu".into()) - } else if target.contains("aarch64") { - cmd.args.push("--target=aarch64-unknown-windows-gnu".into()) - } - } else if target.ends_with("-freebsd") { - // FreeBSD only supports C++11 and above when compiling against libc++ - // (available from FreeBSD 10 onwards). Under FreeBSD, clang uses libc++ by - // default on FreeBSD 10 and newer unless `--target` is manually passed to - // the compiler, in which case its default behavior differs: - // * If --target=xxx-unknown-freebsdX(.Y) is specified and X is greater than - // or equal to 10, clang++ uses libc++ - // * If --target=xxx-unknown-freebsd is specified (without a version), - // clang++ cannot assume libc++ is available and reverts to a default of - // libstdc++ (this behavior was changed in llvm 14). - // - // This breaks C++11 (or greater) builds if targeting FreeBSD with the - // generic xxx-unknown-freebsd triple on clang 13 or below *without* - // explicitly specifying that libc++ should be used. - // When cross-compiling, we can't infer from the rust/cargo target triple - // which major version of FreeBSD we are targeting, so we need to make sure - // that libc++ is used (unless the user has explicitly specified otherwise). - // There's no compelling reason to use a different approach when compiling - // natively. - if self.cpp && self.cpp_set_stdlib.is_none() { - cmd.push_cc_arg("-stdlib=libc++".into()); - } - - cmd.push_cc_arg(format!("--target={}", target).into()); - } else { - cmd.push_cc_arg(format!("--target={}", target).into()); - } - } - } - ToolFamily::Msvc { clang_cl } => { - // This is an undocumented flag from MSVC but helps with making - // builds more reproducible by avoiding putting timestamps into - // files. - cmd.push_cc_arg("-Brepro".into()); - - if clang_cl { - if target.contains("x86_64") { - cmd.push_cc_arg("-m64".into()); - } else if target.contains("86") { - cmd.push_cc_arg("-m32".into()); - cmd.push_cc_arg("-arch:IA32".into()); - } else { - cmd.push_cc_arg(format!("--target={}", target).into()); - } - } else { - if target.contains("i586") { - cmd.push_cc_arg("-arch:IA32".into()); - } - } - - // There is a check in corecrt.h that will generate a - // compilation error if - // _ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE is - // not defined to 1. The check was added in Windows - // 8 days because only store apps were allowed on ARM. - // This changed with the release of Windows 10 IoT Core. - // The check will be going away in future versions of - // the SDK, but for all released versions of the - // Windows SDK it is required. - if target.contains("arm") || target.contains("thumb") { - cmd.args - .push("-D_ARM_WINAPI_PARTITION_DESKTOP_SDK_AVAILABLE=1".into()); - } - } - ToolFamily::Gnu => { - if target.contains("i686") || target.contains("i586") { - cmd.args.push("-m32".into()); - } else if target == "x86_64-unknown-linux-gnux32" { - cmd.args.push("-mx32".into()); - } else if target.contains("x86_64") || target.contains("powerpc64") { - cmd.args.push("-m64".into()); - } - - if target.contains("darwin") { - if let Some(arch) = map_darwin_target_from_rust_to_compiler_architecture(target) - { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - } - } - - if target.contains("-kmc-solid_") { - cmd.args.push("-finput-charset=utf-8".into()); - } - - if self.static_flag.is_none() { - let features = self.getenv("CARGO_CFG_TARGET_FEATURE"); - let features = features.as_deref().unwrap_or_default(); - if features.contains("crt-static") { - cmd.args.push("-static".into()); - } - } - - // armv7 targets get to use armv7 instructions - if (target.starts_with("armv7") || target.starts_with("thumbv7")) - && (target.contains("-linux-") || target.contains("-kmc-solid_")) - { - cmd.args.push("-march=armv7-a".into()); - - if target.ends_with("eabihf") { - // lowest common denominator FPU - cmd.args.push("-mfpu=vfpv3-d16".into()); - } - } - - // (x86 Android doesn't say "eabi") - if target.contains("-androideabi") && target.contains("v7") { - // -march=armv7-a handled above - cmd.args.push("-mthumb".into()); - if !target.contains("neon") { - // On android we can guarantee some extra float instructions - // (specified in the android spec online) - // NEON guarantees even more; see below. - cmd.args.push("-mfpu=vfpv3-d16".into()); - } - cmd.args.push("-mfloat-abi=softfp".into()); - } - - if target.contains("neon") { - cmd.args.push("-mfpu=neon-vfpv4".into()); - } - - if target.starts_with("armv4t-unknown-linux-") { - cmd.args.push("-march=armv4t".into()); - cmd.args.push("-marm".into()); - cmd.args.push("-mfloat-abi=soft".into()); - } - - if target.starts_with("armv5te-unknown-linux-") { - cmd.args.push("-march=armv5te".into()); - cmd.args.push("-marm".into()); - cmd.args.push("-mfloat-abi=soft".into()); - } - - // For us arm == armv6 by default - if target.starts_with("arm-unknown-linux-") { - cmd.args.push("-march=armv6".into()); - cmd.args.push("-marm".into()); - if target.ends_with("hf") { - cmd.args.push("-mfpu=vfp".into()); - } else { - cmd.args.push("-mfloat-abi=soft".into()); - } - } - - // We can guarantee some settings for FRC - if target.starts_with("arm-frc-") { - cmd.args.push("-march=armv7-a".into()); - cmd.args.push("-mcpu=cortex-a9".into()); - cmd.args.push("-mfpu=vfpv3".into()); - cmd.args.push("-mfloat-abi=softfp".into()); - cmd.args.push("-marm".into()); - } - - // Turn codegen down on i586 to avoid some instructions. - if target.starts_with("i586-unknown-linux-") { - cmd.args.push("-march=pentium".into()); - } - - // Set codegen level for i686 correctly - if target.starts_with("i686-unknown-linux-") { - cmd.args.push("-march=i686".into()); - } - - // Looks like `musl-gcc` makes it hard for `-m32` to make its way - // all the way to the linker, so we need to actually instruct the - // linker that we're generating 32-bit executables as well. This'll - // typically only be used for build scripts which transitively use - // these flags that try to compile executables. - if target == "i686-unknown-linux-musl" || target == "i586-unknown-linux-musl" { - cmd.args.push("-Wl,-melf_i386".into()); - } - - if target.starts_with("thumb") { - cmd.args.push("-mthumb".into()); - - if target.ends_with("eabihf") { - cmd.args.push("-mfloat-abi=hard".into()) - } - } - if target.starts_with("thumbv6m") { - cmd.args.push("-march=armv6s-m".into()); - } - if target.starts_with("thumbv7em") { - cmd.args.push("-march=armv7e-m".into()); - - if target.ends_with("eabihf") { - cmd.args.push("-mfpu=fpv4-sp-d16".into()) - } - } - if target.starts_with("thumbv7m") { - cmd.args.push("-march=armv7-m".into()); - } - if target.starts_with("thumbv8m.base") { - cmd.args.push("-march=armv8-m.base".into()); - } - if target.starts_with("thumbv8m.main") { - cmd.args.push("-march=armv8-m.main".into()); - - if target.ends_with("eabihf") { - cmd.args.push("-mfpu=fpv5-sp-d16".into()) - } - } - if target.starts_with("armebv7r") | target.starts_with("armv7r") { - if target.starts_with("armeb") { - cmd.args.push("-mbig-endian".into()); - } else { - cmd.args.push("-mlittle-endian".into()); - } - - // ARM mode - cmd.args.push("-marm".into()); - - // R Profile - cmd.args.push("-march=armv7-r".into()); - - if target.ends_with("eabihf") { - // Calling convention - cmd.args.push("-mfloat-abi=hard".into()); - - // lowest common denominator FPU - // (see Cortex-R4 technical reference manual) - cmd.args.push("-mfpu=vfpv3-d16".into()) - } else { - // Calling convention - cmd.args.push("-mfloat-abi=soft".into()); - } - } - if target.starts_with("armv7a") { - cmd.args.push("-march=armv7-a".into()); - - if target.ends_with("eabihf") { - // lowest common denominator FPU - cmd.args.push("-mfpu=vfpv3-d16".into()); - } - } - if target.starts_with("riscv32") || target.starts_with("riscv64") { - // get the 32i/32imac/32imc/64gc/64imac/... part - let mut parts = target.split('-'); - if let Some(arch) = parts.next() { - let arch = &arch[5..]; - if target.contains("linux") && arch.starts_with("64") { - cmd.args.push(("-march=rv64gc").into()); - cmd.args.push("-mabi=lp64d".into()); - } else if target.contains("freebsd") && arch.starts_with("64") { - cmd.args.push(("-march=rv64gc").into()); - cmd.args.push("-mabi=lp64d".into()); - } else if target.contains("netbsd") && arch.starts_with("64") { - cmd.args.push(("-march=rv64gc").into()); - cmd.args.push("-mabi=lp64d".into()); - } else if target.contains("openbsd") && arch.starts_with("64") { - cmd.args.push(("-march=rv64gc").into()); - cmd.args.push("-mabi=lp64d".into()); - } else if target.contains("linux") && arch.starts_with("32") { - cmd.args.push(("-march=rv32gc").into()); - cmd.args.push("-mabi=ilp32d".into()); - } else if arch.starts_with("64") { - cmd.args.push(("-march=rv".to_owned() + arch).into()); - cmd.args.push("-mabi=lp64".into()); - } else { - cmd.args.push(("-march=rv".to_owned() + arch).into()); - cmd.args.push("-mabi=ilp32".into()); - } - cmd.args.push("-mcmodel=medany".into()); - } - } - } - } - - if target.contains("apple-ios") || target.contains("apple-watchos") { - self.ios_watchos_flags(cmd)?; - } - - if self.static_flag.unwrap_or(false) { - cmd.args.push("-static".into()); - } - if self.shared_flag.unwrap_or(false) { - cmd.args.push("-shared".into()); - } - - if self.cpp { - match (self.cpp_set_stdlib.as_ref(), cmd.family) { - (None, _) => {} - (Some(stdlib), ToolFamily::Gnu) | (Some(stdlib), ToolFamily::Clang) => { - cmd.push_cc_arg(format!("-stdlib=lib{}", stdlib).into()); - } - _ => { - println!( - "cargo:warning=cpp_set_stdlib is specified, but the {:?} compiler \ - does not support this option, ignored", - cmd.family - ); - } - } - } - - Ok(()) - } - - fn has_flags(&self) -> bool { - let flags_env_var_name = if self.cpp { "CXXFLAGS" } else { "CFLAGS" }; - let flags_env_var_value = self.getenv_with_target_prefixes(flags_env_var_name); - if let Ok(_) = flags_env_var_value { - true - } else { - false - } - } - - fn msvc_macro_assembler(&self) -> Result<(Command, String), Error> { - let target = self.get_target()?; - let tool = if target.contains("x86_64") { - "ml64.exe" - } else if target.contains("arm") { - "armasm.exe" - } else if target.contains("aarch64") { - "armasm64.exe" - } else { - "ml.exe" - }; - let mut cmd = windows_registry::find(&target, tool).unwrap_or_else(|| self.cmd(tool)); - cmd.arg("-nologo"); // undocumented, yet working with armasm[64] - for directory in self.include_directories.iter() { - cmd.arg("-I").arg(&**directory); - } - if target.contains("aarch64") || target.contains("arm") { - if self.get_debug() { - cmd.arg("-g"); - } - - println!("cargo:warning=The MSVC ARM assemblers do not support -D flags"); - } else { - if self.get_debug() { - cmd.arg("-Zi"); - } - - for &(ref key, ref value) in self.definitions.iter() { - if let Some(ref value) = *value { - cmd.arg(&format!("-D{}={}", key, value)); - } else { - cmd.arg(&format!("-D{}", key)); - } - } - } - - if target.contains("i686") || target.contains("i586") { - cmd.arg("-safeseh"); - } - for flag in self.flags.iter() { - cmd.arg(&**flag); - } - - Ok((cmd, tool.to_string())) - } - - fn assemble( - &self, - lib_name: &str, - dst: &Path, - objs: &[Object], - print: &PrintThread, - ) -> Result<(), Error> { - // Delete the destination if it exists as we want to - // create on the first iteration instead of appending. - let _ = fs::remove_file(dst); - - // Add objects to the archive in limited-length batches. This helps keep - // the length of the command line within a reasonable length to avoid - // blowing system limits on limiting platforms like Windows. - let objs: Vec<_> = objs - .iter() - .map(|o| o.dst.as_path()) - .chain(self.objects.iter().map(std::ops::Deref::deref)) - .collect(); - for chunk in objs.chunks(100) { - self.assemble_progressive(dst, chunk, print)?; - } - - if self.cuda && self.cuda_file_count() > 0 { - // Link the device-side code and add it to the target library, - // so that non-CUDA linker can link the final binary. - - let out_dir = self.get_out_dir()?; - let dlink = out_dir.join(lib_name.to_owned() + "_dlink.o"); - let mut nvcc = self.get_compiler().to_command(); - nvcc.arg("--device-link").arg("-o").arg(&dlink).arg(dst); - run(&mut nvcc, "nvcc", print)?; - self.assemble_progressive(dst, &[dlink.as_path()], print)?; - } - - let target = self.get_target()?; - if target.contains("msvc") { - // The Rust compiler will look for libfoo.a and foo.lib, but the - // MSVC linker will also be passed foo.lib, so be sure that both - // exist for now. - - let lib_dst = dst.with_file_name(format!("{}.lib", lib_name)); - let _ = fs::remove_file(&lib_dst); - match fs::hard_link(&dst, &lib_dst).or_else(|_| { - // if hard-link fails, just copy (ignoring the number of bytes written) - fs::copy(&dst, &lib_dst).map(|_| ()) - }) { - Ok(_) => (), - Err(_) => { - return Err(Error::new( - ErrorKind::IOError, - "Could not copy or create a hard-link to the generated lib file.", - )); - } - }; - } else { - // Non-msvc targets (those using `ar`) need a separate step to add - // the symbol table to archives since our construction command of - // `cq` doesn't add it for us. - let (mut ar, cmd, _any_flags) = self.get_ar()?; - - // NOTE: We add `s` even if flags were passed using $ARFLAGS/ar_flag, because `s` - // here represents a _mode_, not an arbitrary flag. Further discussion of this choice - // can be seen in https://github.com/rust-lang/cc-rs/pull/763. - run(ar.arg("s").arg(dst), &cmd, print)?; - } - - Ok(()) - } - - fn assemble_progressive( - &self, - dst: &Path, - objs: &[&Path], - print: &PrintThread, - ) -> Result<(), Error> { - let target = self.get_target()?; - - if target.contains("msvc") { - let (mut cmd, program, any_flags) = self.get_ar()?; - // NOTE: -out: here is an I/O flag, and so must be included even if $ARFLAGS/ar_flag is - // in use. -nologo on the other hand is just a regular flag, and one that we'll skip if - // the caller has explicitly dictated the flags they want. See - // https://github.com/rust-lang/cc-rs/pull/763 for further discussion. - let mut out = OsString::from("-out:"); - out.push(dst); - cmd.arg(out); - if !any_flags { - cmd.arg("-nologo"); - } - // If the library file already exists, add the library name - // as an argument to let lib.exe know we are appending the objs. - if dst.exists() { - cmd.arg(dst); - } - cmd.args(objs); - run(&mut cmd, &program, print)?; - } else { - let (mut ar, cmd, _any_flags) = self.get_ar()?; - - // Set an environment variable to tell the OSX archiver to ensure - // that all dates listed in the archive are zero, improving - // determinism of builds. AFAIK there's not really official - // documentation of this but there's a lot of references to it if - // you search google. - // - // You can reproduce this locally on a mac with: - // - // $ touch foo.c - // $ cc -c foo.c -o foo.o - // - // # Notice that these two checksums are different - // $ ar crus libfoo1.a foo.o && sleep 2 && ar crus libfoo2.a foo.o - // $ md5sum libfoo*.a - // - // # Notice that these two checksums are the same - // $ export ZERO_AR_DATE=1 - // $ ar crus libfoo1.a foo.o && sleep 2 && touch foo.o && ar crus libfoo2.a foo.o - // $ md5sum libfoo*.a - // - // In any case if this doesn't end up getting read, it shouldn't - // cause that many issues! - ar.env("ZERO_AR_DATE", "1"); - - // NOTE: We add cq here regardless of whether $ARFLAGS/ar_flag have been used because - // it dictates the _mode_ ar runs in, which the setter of $ARFLAGS/ar_flag can't - // dictate. See https://github.com/rust-lang/cc-rs/pull/763 for further discussion. - run(ar.arg("cq").arg(dst).args(objs), &cmd, print)?; - } - - Ok(()) - } - - fn ios_watchos_flags(&self, cmd: &mut Tool) -> Result<(), Error> { - enum ArchSpec { - Device(&'static str), - Simulator(&'static str), - Catalyst(&'static str), - } - - enum Os { - Ios, - WatchOs, - } - impl Display for Os { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - Os::Ios => f.write_str("iOS"), - Os::WatchOs => f.write_str("WatchOS"), - } - } - } - - let target = self.get_target()?; - let os = if target.contains("-watchos") { - Os::WatchOs - } else { - Os::Ios - }; - - let arch = target.split('-').nth(0).ok_or_else(|| { - Error::new( - ErrorKind::ArchitectureInvalid, - format!("Unknown architecture for {} target.", os), - ) - })?; - - let is_catalyst = match target.split('-').nth(3) { - Some(v) => v == "macabi", - None => false, - }; - - let is_sim = match target.split('-').nth(3) { - Some(v) => v == "sim", - None => false, - }; - - let arch = if is_catalyst { - match arch { - "arm64e" => ArchSpec::Catalyst("arm64e"), - "arm64" | "aarch64" => ArchSpec::Catalyst("arm64"), - "x86_64" => ArchSpec::Catalyst("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - "Unknown architecture for iOS target.", - )); - } - } - } else if is_sim { - match arch { - "arm64" | "aarch64" => ArchSpec::Simulator("arm64"), - "x86_64" => ArchSpec::Simulator("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - "Unknown architecture for iOS simulator target.", - )); - } - } - } else { - match arch { - "arm" | "armv7" | "thumbv7" => ArchSpec::Device("armv7"), - "armv7k" => ArchSpec::Device("armv7k"), - "armv7s" | "thumbv7s" => ArchSpec::Device("armv7s"), - "arm64e" => ArchSpec::Device("arm64e"), - "arm64" | "aarch64" => ArchSpec::Device("arm64"), - "arm64_32" => ArchSpec::Device("arm64_32"), - "i386" | "i686" => ArchSpec::Simulator("-m32"), - "x86_64" => ArchSpec::Simulator("-m64"), - _ => { - return Err(Error::new( - ErrorKind::ArchitectureInvalid, - format!("Unknown architecture for {} target.", os), - )); - } - } - }; - - let (sdk_prefix, sim_prefix, min_version) = match os { - Os::Ios => ( - "iphone", - "ios-", - std::env::var("IPHONEOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "7.0".into()), - ), - Os::WatchOs => ( - "watch", - "watch", - std::env::var("WATCHOS_DEPLOYMENT_TARGET").unwrap_or_else(|_| "2.0".into()), - ), - }; - - let sdk = match arch { - ArchSpec::Device(arch) => { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - cmd.args - .push(format!("-m{}os-version-min={}", sdk_prefix, min_version).into()); - format!("{}os", sdk_prefix) - } - ArchSpec::Simulator(arch) => { - if arch.starts_with('-') { - // -m32 or -m64 - cmd.args.push(arch.into()); - } else { - cmd.args.push("-arch".into()); - cmd.args.push(arch.into()); - } - cmd.args - .push(format!("-m{}simulator-version-min={}", sim_prefix, min_version).into()); - format!("{}simulator", sdk_prefix) - } - ArchSpec::Catalyst(_) => "macosx".to_owned(), - }; - - self.print(&format_args!("Detecting {} SDK path for {}", os, sdk)); - let sdk_path = if let Some(sdkroot) = env::var_os("SDKROOT") { - sdkroot - } else { - self.apple_sdk_root(sdk.as_str())? - }; - - cmd.args.push("-isysroot".into()); - cmd.args.push(sdk_path); - // TODO: Remove this once Apple stops accepting apps built with Xcode 13 - cmd.args.push("-fembed-bitcode".into()); - - Ok(()) - } - - fn cmd<P: AsRef<OsStr>>(&self, prog: P) -> Command { - let mut cmd = Command::new(prog); - for &(ref a, ref b) in self.env.iter() { - cmd.env(a, b); - } - cmd - } - - fn get_base_compiler(&self) -> Result<Tool, Error> { - if let Some(c) = &self.compiler { - return Ok(Tool::new((**c).to_owned())); - } - let host = self.get_host()?; - let target = self.get_target()?; - let target = &*target; - let (env, msvc, gnu, traditional, clang) = if self.cpp { - ("CXX", "cl.exe", "g++", "c++", "clang++") - } else { - ("CC", "cl.exe", "gcc", "cc", "clang") - }; - - // On historical Solaris systems, "cc" may have been Sun Studio, which - // is not flag-compatible with "gcc". This history casts a long shadow, - // and many modern illumos distributions today ship GCC as "gcc" without - // also making it available as "cc". - let default = if host.contains("solaris") || host.contains("illumos") { - gnu - } else { - traditional - }; - - let cl_exe = windows_registry::find_tool(&target, "cl.exe"); - - let tool_opt: Option<Tool> = self - .env_tool(env) - .map(|(tool, wrapper, args)| { - // find the driver mode, if any - const DRIVER_MODE: &str = "--driver-mode="; - let driver_mode = args - .iter() - .find(|a| a.starts_with(DRIVER_MODE)) - .map(|a| &a[DRIVER_MODE.len()..]); - // Chop off leading/trailing whitespace to work around - // semi-buggy build scripts which are shared in - // makefiles/configure scripts (where spaces are far more - // lenient) - let mut t = Tool::with_clang_driver(tool, driver_mode); - if let Some(cc_wrapper) = wrapper { - t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); - } - for arg in args { - t.cc_wrapper_args.push(arg.into()); - } - t - }) - .or_else(|| { - if target.contains("emscripten") { - let tool = if self.cpp { "em++" } else { "emcc" }; - // Windows uses bat file so we have to be a bit more specific - if cfg!(windows) { - let mut t = Tool::new(PathBuf::from("cmd")); - t.args.push("/c".into()); - t.args.push(format!("{}.bat", tool).into()); - Some(t) - } else { - Some(Tool::new(PathBuf::from(tool))) - } - } else { - None - } - }) - .or_else(|| cl_exe.clone()); - - let tool = match tool_opt { - Some(t) => t, - None => { - let compiler = if host.contains("windows") && target.contains("windows") { - if target.contains("msvc") { - msvc.to_string() - } else { - let cc = if target.contains("llvm") { clang } else { gnu }; - format!("{}.exe", cc) - } - } else if target.contains("apple-ios") { - clang.to_string() - } else if target.contains("apple-watchos") { - clang.to_string() - } else if target.contains("android") { - autodetect_android_compiler(&target, &host, gnu, clang) - } else if target.contains("cloudabi") { - format!("{}-{}", target, traditional) - } else if target == "wasm32-wasi" - || target == "wasm32-unknown-wasi" - || target == "wasm32-unknown-unknown" - { - "clang".to_string() - } else if target.contains("vxworks") { - if self.cpp { - "wr-c++".to_string() - } else { - "wr-cc".to_string() - } - } else if target.starts_with("armv7a-kmc-solid_") { - format!("arm-kmc-eabi-{}", gnu) - } else if target.starts_with("aarch64-kmc-solid_") { - format!("aarch64-kmc-elf-{}", gnu) - } else if &*self.get_host()? != target { - let prefix = self.prefix_for_target(&target); - match prefix { - Some(prefix) => { - let cc = if target.contains("llvm") { clang } else { gnu }; - format!("{}-{}", prefix, cc) - } - None => default.to_string(), - } - } else { - default.to_string() - }; - - let mut t = Tool::new(PathBuf::from(compiler)); - if let Some(cc_wrapper) = Self::rustc_wrapper_fallback() { - t.cc_wrapper_path = Some(PathBuf::from(cc_wrapper)); - } - t - } - }; - - let mut tool = if self.cuda { - assert!( - tool.args.is_empty(), - "CUDA compilation currently assumes empty pre-existing args" - ); - let nvcc = match self.getenv_with_target_prefixes("NVCC") { - Err(_) => PathBuf::from("nvcc"), - Ok(nvcc) => PathBuf::from(&*nvcc), - }; - let mut nvcc_tool = Tool::with_features(nvcc, None, self.cuda); - nvcc_tool - .args - .push(format!("-ccbin={}", tool.path.display()).into()); - nvcc_tool.family = tool.family; - nvcc_tool - } else { - tool - }; - - // New "standalone" C/C++ cross-compiler executables from recent Android NDK - // are just shell scripts that call main clang binary (from Android NDK) with - // proper `--target` argument. - // - // For example, armv7a-linux-androideabi16-clang passes - // `--target=armv7a-linux-androideabi16` to clang. - // - // As the shell script calls the main clang binary, the command line limit length - // on Windows is restricted to around 8k characters instead of around 32k characters. - // To remove this limit, we call the main clang binary directly and construct the - // `--target=` ourselves. - if host.contains("windows") && android_clang_compiler_uses_target_arg_internally(&tool.path) - { - if let Some(path) = tool.path.file_name() { - let file_name = path.to_str().unwrap().to_owned(); - let (target, clang) = file_name.split_at(file_name.rfind("-").unwrap()); - - tool.path.set_file_name(clang.trim_start_matches("-")); - tool.path.set_extension("exe"); - tool.args.push(format!("--target={}", target).into()); - - // Additionally, shell scripts for target i686-linux-android versions 16 to 24 - // pass the `mstackrealign` option so we do that here as well. - if target.contains("i686-linux-android") { - let (_, version) = target.split_at(target.rfind("d").unwrap() + 1); - if let Ok(version) = version.parse::<u32>() { - if version > 15 && version < 25 { - tool.args.push("-mstackrealign".into()); - } - } - } - }; - } - - // If we found `cl.exe` in our environment, the tool we're returning is - // an MSVC-like tool, *and* no env vars were set then set env vars for - // the tool that we're returning. - // - // Env vars are needed for things like `link.exe` being put into PATH as - // well as header include paths sometimes. These paths are automatically - // included by default but if the `CC` or `CXX` env vars are set these - // won't be used. This'll ensure that when the env vars are used to - // configure for invocations like `clang-cl` we still get a "works out - // of the box" experience. - if let Some(cl_exe) = cl_exe { - if tool.family == (ToolFamily::Msvc { clang_cl: true }) - && tool.env.len() == 0 - && target.contains("msvc") - { - for &(ref k, ref v) in cl_exe.env.iter() { - tool.env.push((k.to_owned(), v.to_owned())); - } - } - } - - if target.contains("msvc") && tool.family == ToolFamily::Gnu { - println!("cargo:warning=GNU compiler is not supported for this target"); - } - - Ok(tool) - } - - /// Returns a fallback `cc_compiler_wrapper` by introspecting `RUSTC_WRAPPER` - fn rustc_wrapper_fallback() -> Option<String> { - // No explicit CC wrapper was detected, but check if RUSTC_WRAPPER - // is defined and is a build accelerator that is compatible with - // C/C++ compilers (e.g. sccache) - const VALID_WRAPPERS: &[&'static str] = &["sccache", "cachepot"]; - - let rustc_wrapper = std::env::var_os("RUSTC_WRAPPER")?; - let wrapper_path = Path::new(&rustc_wrapper); - let wrapper_stem = wrapper_path.file_stem()?; - - if VALID_WRAPPERS.contains(&wrapper_stem.to_str()?) { - Some(rustc_wrapper.to_str()?.to_owned()) - } else { - None - } - } - - /// Returns compiler path, optional modifier name from whitelist, and arguments vec - fn env_tool(&self, name: &str) -> Option<(PathBuf, Option<String>, Vec<String>)> { - let tool = match self.getenv_with_target_prefixes(name) { - Ok(tool) => tool, - Err(_) => return None, - }; - - // If this is an exact path on the filesystem we don't want to do any - // interpretation at all, just pass it on through. This'll hopefully get - // us to support spaces-in-paths. - if Path::new(&*tool).exists() { - return Some((PathBuf::from(&*tool), None, Vec::new())); - } - - // Ok now we want to handle a couple of scenarios. We'll assume from - // here on out that spaces are splitting separate arguments. Two major - // features we want to support are: - // - // CC='sccache cc' - // - // aka using `sccache` or any other wrapper/caching-like-thing for - // compilations. We want to know what the actual compiler is still, - // though, because our `Tool` API support introspection of it to see - // what compiler is in use. - // - // additionally we want to support - // - // CC='cc -flag' - // - // where the CC env var is used to also pass default flags to the C - // compiler. - // - // It's true that everything here is a bit of a pain, but apparently if - // you're not literally make or bash then you get a lot of bug reports. - let known_wrappers = ["ccache", "distcc", "sccache", "icecc", "cachepot"]; - - let mut parts = tool.split_whitespace(); - let maybe_wrapper = match parts.next() { - Some(s) => s, - None => return None, - }; - - let file_stem = Path::new(maybe_wrapper) - .file_stem() - .unwrap() - .to_str() - .unwrap(); - if known_wrappers.contains(&file_stem) { - if let Some(compiler) = parts.next() { - return Some(( - compiler.into(), - Some(maybe_wrapper.to_string()), - parts.map(|s| s.to_string()).collect(), - )); - } - } - - Some(( - maybe_wrapper.into(), - Self::rustc_wrapper_fallback(), - parts.map(|s| s.to_string()).collect(), - )) - } - - /// Returns the C++ standard library: - /// 1. If [cpp_link_stdlib](cc::Build::cpp_link_stdlib) is set, uses its value. - /// 2. Else if the `CXXSTDLIB` environment variable is set, uses its value. - /// 3. Else the default is `libc++` for OS X and BSDs, `libc++_shared` for Android, - /// `None` for MSVC and `libstdc++` for anything else. - fn get_cpp_link_stdlib(&self) -> Result<Option<String>, Error> { - match &self.cpp_link_stdlib { - Some(s) => Ok(s.as_ref().map(|s| (*s).to_string())), - None => { - if let Ok(stdlib) = self.getenv_with_target_prefixes("CXXSTDLIB") { - if stdlib.is_empty() { - Ok(None) - } else { - Ok(Some(stdlib.to_string())) - } - } else { - let target = self.get_target()?; - if target.contains("msvc") { - Ok(None) - } else if target.contains("apple") { - Ok(Some("c++".to_string())) - } else if target.contains("freebsd") { - Ok(Some("c++".to_string())) - } else if target.contains("openbsd") { - Ok(Some("c++".to_string())) - } else if target.contains("aix") { - Ok(Some("c++".to_string())) - } else if target.contains("android") { - Ok(Some("c++_shared".to_string())) - } else { - Ok(Some("stdc++".to_string())) - } - } - } - } - } - - fn get_ar(&self) -> Result<(Command, String, bool), Error> { - self.try_get_archiver_and_flags() - } - - /// Get the archiver (ar) that's in use for this configuration. - /// - /// You can use [`Command::get_program`] to 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. - pub fn get_archiver(&self) -> Command { - match self.try_get_archiver() { - Ok(tool) => tool, - Err(e) => fail(&e.message), - } - } - - /// Get the archiver that's in use for this configuration. - /// - /// This will return a result instead of panicing; - /// see [`get_archiver()`] for the complete description. - pub fn try_get_archiver(&self) -> Result<Command, Error> { - Ok(self.try_get_archiver_and_flags()?.0) - } - - fn try_get_archiver_and_flags(&self) -> Result<(Command, String, bool), Error> { - let (mut cmd, name) = self.get_base_archiver()?; - let mut any_flags = false; - if let Ok(flags) = self.envflags("ARFLAGS") { - any_flags = any_flags | !flags.is_empty(); - cmd.args(flags); - } - for flag in &self.ar_flags { - any_flags = true; - cmd.arg(&**flag); - } - Ok((cmd, name, any_flags)) - } - - fn get_base_archiver(&self) -> Result<(Command, String), Error> { - if let Some(ref a) = self.archiver { - return Ok((self.cmd(&**a), a.to_string_lossy().into_owned())); - } - - self.get_base_archiver_variant("AR", "ar") - } - - /// Get the ranlib that's in use for this configuration. - /// - /// You can use [`Command::get_program`] to 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. - pub fn get_ranlib(&self) -> Command { - match self.try_get_ranlib() { - Ok(tool) => tool, - Err(e) => fail(&e.message), - } - } - - /// Get the ranlib that's in use for this configuration. - /// - /// This will return a result instead of panicing; - /// see [`get_ranlib()`] for the complete description. - pub fn try_get_ranlib(&self) -> Result<Command, Error> { - let mut cmd = self.get_base_ranlib()?; - if let Ok(flags) = self.envflags("RANLIBFLAGS") { - cmd.args(flags); - } - Ok(cmd) - } - - fn get_base_ranlib(&self) -> Result<Command, Error> { - if let Some(ref r) = self.ranlib { - return Ok(self.cmd(&**r)); - } - - Ok(self.get_base_archiver_variant("RANLIB", "ranlib")?.0) - } - - fn get_base_archiver_variant(&self, env: &str, tool: &str) -> Result<(Command, String), Error> { - let target = self.get_target()?; - let mut name = String::new(); - let tool_opt: Option<Command> = self - .env_tool(env) - .map(|(tool, _wrapper, args)| { - let mut cmd = self.cmd(tool); - cmd.args(args); - cmd - }) - .or_else(|| { - if target.contains("emscripten") { - // Windows use bat files so we have to be a bit more specific - if cfg!(windows) { - let mut cmd = self.cmd("cmd"); - name = format!("em{}.bat", tool); - cmd.arg("/c").arg(&name); - Some(cmd) - } else { - name = format!("em{}", tool); - Some(self.cmd(&name)) - } - } else if target.starts_with("wasm32") { - // Formally speaking one should be able to use this approach, - // parsing -print-search-dirs output, to cover all clang targets, - // including Android SDKs and other cross-compilation scenarios... - // And even extend it to gcc targets by seaching for "ar" instead - // of "llvm-ar"... - let compiler = self.get_base_compiler().ok()?; - if compiler.family == ToolFamily::Clang { - name = format!("llvm-{}", tool); - search_programs(&mut self.cmd(&compiler.path), &name) - .map(|name| self.cmd(&name)) - } else { - None - } - } else { - None - } - }); - - let default = tool.to_string(); - let tool = match tool_opt { - Some(t) => t, - None => { - if target.contains("android") { - name = format!("{}-{}", target.replace("armv7", "arm"), tool); - self.cmd(&name) - } else if target.contains("msvc") { - // NOTE: There isn't really a ranlib on msvc, so arguably we should return - // `None` somehow here. But in general, callers will already have to be aware - // of not running ranlib on Windows anyway, so it feels okay to return lib.exe - // here. - - let compiler = self.get_base_compiler()?; - let mut lib = String::new(); - if compiler.family == (ToolFamily::Msvc { clang_cl: true }) { - // See if there is 'llvm-lib' next to 'clang-cl' - // Another possibility could be to see if there is 'clang' - // next to 'clang-cl' and use 'search_programs()' to locate - // 'llvm-lib'. This is because 'clang-cl' doesn't support - // the -print-search-dirs option. - if let Some(mut cmd) = which(&compiler.path, None) { - cmd.pop(); - cmd.push("llvm-lib.exe"); - if let Some(llvm_lib) = which(&cmd, None) { - lib = llvm_lib.to_str().unwrap().to_owned(); - } - } - } - - if lib.is_empty() { - name = String::from("lib.exe"); - match windows_registry::find(&target, "lib.exe") { - Some(t) => t, - None => self.cmd("lib.exe"), - } - } else { - name = lib; - self.cmd(&name) - } - } else if target.contains("illumos") { - // The default 'ar' on illumos uses a non-standard flags, - // but the OS comes bundled with a GNU-compatible variant. - // - // Use the GNU-variant to match other Unix systems. - name = format!("g{}", tool); - self.cmd(&name) - } else if self.get_host()? != target { - match self.prefix_for_target(&target) { - Some(p) => { - // GCC uses $target-gcc-ar, whereas binutils uses $target-ar -- try both. - // Prefer -ar if it exists, as builds of `-gcc-ar` have been observed to be - // outright broken (such as when targetting freebsd with `--disable-lto` - // toolchain where the archiver attempts to load the LTO plugin anyway but - // fails to find one). - // - // The same applies to ranlib. - let mut chosen = default; - for &infix in &["", "-gcc"] { - let target_p = format!("{}{}-{}", p, infix, tool); - if Command::new(&target_p).output().is_ok() { - chosen = target_p; - break; - } - } - name = chosen; - self.cmd(&name) - } - None => { - name = default; - self.cmd(&name) - } - } - } else { - name = default; - self.cmd(&name) - } - } - }; - - Ok((tool, name)) - } - - fn prefix_for_target(&self, target: &str) -> Option<String> { - // Put aside RUSTC_LINKER's prefix to be used as second choice, after CROSS_COMPILE - let linker_prefix = self - .getenv("RUSTC_LINKER") - .and_then(|var| var.strip_suffix("-gcc").map(str::to_string)); - // CROSS_COMPILE is of the form: "arm-linux-gnueabi-" - let cc_env = self.getenv("CROSS_COMPILE"); - let cross_compile = cc_env.as_ref().map(|s| s.trim_end_matches('-').to_owned()); - cross_compile.or(linker_prefix).or(match &target[..] { - // Note: there is no `aarch64-pc-windows-gnu` target, only `-gnullvm` - "aarch64-pc-windows-gnullvm" => Some("aarch64-w64-mingw32"), - "aarch64-uwp-windows-gnu" => Some("aarch64-w64-mingw32"), - "aarch64-unknown-linux-gnu" => Some("aarch64-linux-gnu"), - "aarch64-unknown-linux-musl" => Some("aarch64-linux-musl"), - "aarch64-unknown-netbsd" => Some("aarch64--netbsd"), - "arm-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv4t-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv5te-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv5te-unknown-linux-musleabi" => Some("arm-linux-gnueabi"), - "arm-frc-linux-gnueabi" => Some("arm-frc-linux-gnueabi"), - "arm-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "arm-unknown-linux-musleabi" => Some("arm-linux-musleabi"), - "arm-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "arm-unknown-netbsd-eabi" => Some("arm--netbsdelf-eabi"), - "armv6-unknown-netbsd-eabihf" => Some("armv6--netbsdelf-eabihf"), - "armv7-unknown-linux-gnueabi" => Some("arm-linux-gnueabi"), - "armv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "armv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "armv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "armv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "thumbv7-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "thumbv7-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "thumbv7neon-unknown-linux-gnueabihf" => Some("arm-linux-gnueabihf"), - "thumbv7neon-unknown-linux-musleabihf" => Some("arm-linux-musleabihf"), - "armv7-unknown-netbsd-eabihf" => Some("armv7--netbsdelf-eabihf"), - "hexagon-unknown-linux-musl" => Some("hexagon-linux-musl"), - "i586-unknown-linux-musl" => Some("musl"), - "i686-pc-windows-gnu" => Some("i686-w64-mingw32"), - "i686-uwp-windows-gnu" => Some("i686-w64-mingw32"), - "i686-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ - "i686-linux-gnu", - "x86_64-linux-gnu", // transparently support gcc-multilib - ]), // explicit None if not found, so caller knows to fall back - "i686-unknown-linux-musl" => Some("musl"), - "i686-unknown-netbsd" => Some("i486--netbsdelf"), - "loongarch64-unknown-linux-gnu" => Some("loongarch64-linux-gnu"), - "mips-unknown-linux-gnu" => Some("mips-linux-gnu"), - "mips-unknown-linux-musl" => Some("mips-linux-musl"), - "mipsel-unknown-linux-gnu" => Some("mipsel-linux-gnu"), - "mipsel-unknown-linux-musl" => Some("mipsel-linux-musl"), - "mips64-unknown-linux-gnuabi64" => Some("mips64-linux-gnuabi64"), - "mips64el-unknown-linux-gnuabi64" => Some("mips64el-linux-gnuabi64"), - "mipsisa32r6-unknown-linux-gnu" => Some("mipsisa32r6-linux-gnu"), - "mipsisa32r6el-unknown-linux-gnu" => Some("mipsisa32r6el-linux-gnu"), - "mipsisa64r6-unknown-linux-gnuabi64" => Some("mipsisa64r6-linux-gnuabi64"), - "mipsisa64r6el-unknown-linux-gnuabi64" => Some("mipsisa64r6el-linux-gnuabi64"), - "powerpc-unknown-linux-gnu" => Some("powerpc-linux-gnu"), - "powerpc-unknown-linux-gnuspe" => Some("powerpc-linux-gnuspe"), - "powerpc-unknown-netbsd" => Some("powerpc--netbsd"), - "powerpc64-unknown-linux-gnu" => Some("powerpc-linux-gnu"), - "powerpc64le-unknown-linux-gnu" => Some("powerpc64le-linux-gnu"), - "riscv32i-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imac-unknown-xous-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv32imc-esp-espidf" => Some("riscv32-esp-elf"), - "riscv32imc-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv32-unknown-elf", - "riscv64-unknown-elf", - "riscv-none-embed", - ]), - "riscv64gc-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv64-unknown-elf", - "riscv32-unknown-elf", - "riscv-none-embed", - ]), - "riscv64imac-unknown-none-elf" => self.find_working_gnu_prefix(&[ - "riscv64-unknown-elf", - "riscv32-unknown-elf", - "riscv-none-embed", - ]), - "riscv64gc-unknown-linux-gnu" => Some("riscv64-linux-gnu"), - "riscv32gc-unknown-linux-gnu" => Some("riscv32-linux-gnu"), - "riscv64gc-unknown-linux-musl" => Some("riscv64-linux-musl"), - "riscv32gc-unknown-linux-musl" => Some("riscv32-linux-musl"), - "riscv64gc-unknown-netbsd" => Some("riscv64--netbsd"), - "s390x-unknown-linux-gnu" => Some("s390x-linux-gnu"), - "sparc-unknown-linux-gnu" => Some("sparc-linux-gnu"), - "sparc64-unknown-linux-gnu" => Some("sparc64-linux-gnu"), - "sparc64-unknown-netbsd" => Some("sparc64--netbsd"), - "sparcv9-sun-solaris" => Some("sparcv9-sun-solaris"), - "armv7a-none-eabi" => Some("arm-none-eabi"), - "armv7a-none-eabihf" => Some("arm-none-eabi"), - "armebv7r-none-eabi" => Some("arm-none-eabi"), - "armebv7r-none-eabihf" => Some("arm-none-eabi"), - "armv7r-none-eabi" => Some("arm-none-eabi"), - "armv7r-none-eabihf" => Some("arm-none-eabi"), - "thumbv6m-none-eabi" => Some("arm-none-eabi"), - "thumbv7em-none-eabi" => Some("arm-none-eabi"), - "thumbv7em-none-eabihf" => Some("arm-none-eabi"), - "thumbv7m-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.base-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.main-none-eabi" => Some("arm-none-eabi"), - "thumbv8m.main-none-eabihf" => Some("arm-none-eabi"), - "x86_64-pc-windows-gnu" => Some("x86_64-w64-mingw32"), - "x86_64-pc-windows-gnullvm" => Some("x86_64-w64-mingw32"), - "x86_64-uwp-windows-gnu" => Some("x86_64-w64-mingw32"), - "x86_64-rumprun-netbsd" => Some("x86_64-rumprun-netbsd"), - "x86_64-unknown-linux-gnu" => self.find_working_gnu_prefix(&[ - "x86_64-linux-gnu", // rustfmt wrap - ]), // explicit None if not found, so caller knows to fall back - "x86_64-unknown-linux-musl" => Some("musl"), - "x86_64-unknown-netbsd" => Some("x86_64--netbsd"), - _ => None, - } - .map(|x| x.to_owned())) - } - - /// Some platforms have multiple, compatible, canonical prefixes. Look through - /// each possible prefix for a compiler that exists and return it. The prefixes - /// should be ordered from most-likely to least-likely. - fn find_working_gnu_prefix(&self, prefixes: &[&'static str]) -> Option<&'static str> { - let suffix = if self.cpp { "-g++" } else { "-gcc" }; - let extension = std::env::consts::EXE_SUFFIX; - - // Loop through PATH entries searching for each toolchain. This ensures that we - // are more likely to discover the toolchain early on, because chances are good - // that the desired toolchain is in one of the higher-priority paths. - env::var_os("PATH") - .as_ref() - .and_then(|path_entries| { - env::split_paths(path_entries).find_map(|path_entry| { - for prefix in prefixes { - let target_compiler = format!("{}{}{}", prefix, suffix, extension); - if path_entry.join(&target_compiler).exists() { - return Some(prefix); - } - } - None - }) - }) - .map(|prefix| *prefix) - .or_else(|| - // If no toolchain was found, provide the first toolchain that was passed in. - // This toolchain has been shown not to exist, however it will appear in the - // error that is shown to the user which should make it easier to search for - // where it should be obtained. - prefixes.first().map(|prefix| *prefix)) - } - - fn get_target(&self) -> Result<Arc<str>, Error> { - match &self.target { - Some(t) => Ok(t.clone()), - None => self.getenv_unwrap("TARGET"), - } - } - - fn get_host(&self) -> Result<Arc<str>, Error> { - match &self.host { - Some(h) => Ok(h.clone()), - None => self.getenv_unwrap("HOST"), - } - } - - fn get_opt_level(&self) -> Result<Arc<str>, Error> { - match &self.opt_level { - Some(ol) => Ok(ol.clone()), - None => self.getenv_unwrap("OPT_LEVEL"), - } - } - - fn get_debug(&self) -> bool { - self.debug.unwrap_or_else(|| match self.getenv("DEBUG") { - Some(s) => &*s != "false", - None => false, - }) - } - - fn get_dwarf_version(&self) -> Option<u32> { - // Tentatively matches the DWARF version defaults as of rustc 1.62. - let target = self.get_target().ok()?; - if target.contains("android") - || target.contains("apple") - || target.contains("dragonfly") - || target.contains("freebsd") - || target.contains("netbsd") - || target.contains("openbsd") - || target.contains("windows-gnu") - { - Some(2) - } else if target.contains("linux") { - Some(4) - } else { - None - } - } - - fn get_force_frame_pointer(&self) -> bool { - self.force_frame_pointer.unwrap_or_else(|| self.get_debug()) - } - - fn get_out_dir(&self) -> Result<Cow<'_, Path>, Error> { - match &self.out_dir { - Some(p) => Ok(Cow::Borrowed(&**p)), - None => env::var_os("OUT_DIR") - .map(PathBuf::from) - .map(Cow::Owned) - .ok_or_else(|| { - Error::new( - ErrorKind::EnvVarNotFound, - "Environment variable OUT_DIR not defined.", - ) - }), - } - } - - fn getenv(&self, v: &str) -> Option<Arc<str>> { - // Returns true for environment variables cargo sets for build scripts: - // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts - // - // This handles more of the vars than we actually use (it tries to check - // complete-ish set), just to avoid needing maintenance if/when new - // calls to `getenv`/`getenv_unwrap` are added. - fn provided_by_cargo(envvar: &str) -> bool { - match envvar { - v if v.starts_with("CARGO") || v.starts_with("RUSTC") => true, - "HOST" | "TARGET" | "RUSTDOC" | "OUT_DIR" | "OPT_LEVEL" | "DEBUG" | "PROFILE" - | "NUM_JOBS" | "RUSTFLAGS" => true, - _ => false, - } - } - let mut cache = self.env_cache.lock().unwrap(); - if let Some(val) = cache.get(v) { - return val.clone(); - } - if self.emit_rerun_if_env_changed && !provided_by_cargo(v) { - self.print(&format_args!("cargo:rerun-if-env-changed={}", v)); - } - let r = env::var(v).ok().map(Arc::from); - self.print(&format_args!("{} = {:?}", v, r)); - cache.insert(v.to_string(), r.clone()); - r - } - - fn getenv_unwrap(&self, v: &str) -> Result<Arc<str>, Error> { - match self.getenv(v) { - Some(s) => Ok(s), - None => Err(Error::new( - ErrorKind::EnvVarNotFound, - format!("Environment variable {} not defined.", v), - )), - } - } - - fn getenv_with_target_prefixes(&self, var_base: &str) -> Result<Arc<str>, Error> { - let target = self.get_target()?; - let host = self.get_host()?; - let kind = if host == target { "HOST" } else { "TARGET" }; - let target_u = target.replace("-", "_"); - let res = self - .getenv(&format!("{}_{}", var_base, target)) - .or_else(|| self.getenv(&format!("{}_{}", var_base, target_u))) - .or_else(|| self.getenv(&format!("{}_{}", kind, var_base))) - .or_else(|| self.getenv(var_base)); - - match res { - Some(res) => Ok(res), - None => Err(Error::new( - ErrorKind::EnvVarNotFound, - format!("Could not find environment variable {}.", var_base), - )), - } - } - - fn envflags(&self, name: &str) -> Result<Vec<String>, Error> { - Ok(self - .getenv_with_target_prefixes(name)? - .split_ascii_whitespace() - .map(|slice| slice.to_string()) - .collect()) - } - - fn print(&self, s: &dyn Display) { - if self.cargo_metadata { - println!("{}", s); - } - } - - fn fix_env_for_apple_os(&self, cmd: &mut Command) -> Result<(), Error> { - let target = self.get_target()?; - let host = self.get_host()?; - if host.contains("apple-darwin") && target.contains("apple-darwin") { - // If, for example, `cargo` runs during the build of an XCode project, then `SDKROOT` environment variable - // would represent the current target, and this is the problem for us, if we want to compile something - // for the host, when host != target. - // We can not just remove `SDKROOT`, because, again, for example, XCode add to PATH - // /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin - // and `cc` from this path can not find system include files, like `pthread.h`, if `SDKROOT` - // is not set - if let Ok(sdkroot) = env::var("SDKROOT") { - if !sdkroot.contains("MacOSX") { - let macos_sdk = self.apple_sdk_root("macosx")?; - cmd.env("SDKROOT", macos_sdk); - } - } - // Additionally, `IPHONEOS_DEPLOYMENT_TARGET` must not be set when using the Xcode linker at - // "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld", - // although this is apparently ignored when using the linker at "/usr/bin/ld". - cmd.env_remove("IPHONEOS_DEPLOYMENT_TARGET"); - } - Ok(()) - } - - fn apple_sdk_root(&self, sdk: &str) -> Result<OsString, Error> { - let mut cache = self - .apple_sdk_root_cache - .lock() - .expect("apple_sdk_root_cache lock failed"); - if let Some(ret) = cache.get(sdk) { - return Ok(ret.clone()); - } - - let sdk_path = run_output( - self.cmd("xcrun") - .arg("--show-sdk-path") - .arg("--sdk") - .arg(sdk), - "xcrun", - )?; - - let sdk_path = match String::from_utf8(sdk_path) { - Ok(p) => p, - Err(_) => { - return Err(Error::new( - ErrorKind::IOError, - "Unable to determine Apple SDK path.", - )); - } - }; - let ret: OsString = sdk_path.trim().into(); - cache.insert(sdk.into(), ret.clone()); - Ok(ret) - } - - fn cuda_file_count(&self) -> usize { - self.files - .iter() - .filter(|file| file.extension() == Some(OsStr::new("cu"))) - .count() - } -} - -impl Default for Build { - fn default() -> Build { - Build::new() - } -} - -impl Tool { - fn new(path: PathBuf) -> Self { - Tool::with_features(path, None, false) - } - - fn with_clang_driver(path: PathBuf, clang_driver: Option<&str>) -> Self { - Self::with_features(path, clang_driver, false) - } - - #[cfg(windows)] - /// Explicitly set the `ToolFamily`, skipping name-based detection. - fn with_family(path: PathBuf, family: ToolFamily) -> Self { - Self { - path: path, - cc_wrapper_path: None, - cc_wrapper_args: Vec::new(), - args: Vec::new(), - env: Vec::new(), - family: family, - cuda: false, - removed_args: Vec::new(), - } - } - - fn with_features(path: PathBuf, clang_driver: Option<&str>, cuda: bool) -> Self { - // Try to detect family of the tool from its name, falling back to Gnu. - let family = if let Some(fname) = path.file_name().and_then(|p| p.to_str()) { - if fname.contains("clang-cl") { - ToolFamily::Msvc { clang_cl: true } - } else if fname.ends_with("cl") || fname == "cl.exe" { - ToolFamily::Msvc { clang_cl: false } - } else if fname.contains("clang") { - match clang_driver { - Some("cl") => ToolFamily::Msvc { clang_cl: true }, - _ => ToolFamily::Clang, - } - } else { - ToolFamily::Gnu - } - } else { - ToolFamily::Gnu - }; - - Tool { - path: path, - cc_wrapper_path: None, - cc_wrapper_args: Vec::new(), - args: Vec::new(), - env: Vec::new(), - family: family, - cuda: cuda, - removed_args: Vec::new(), - } - } - - /// Add an argument to be stripped from the final command arguments. - fn remove_arg(&mut self, flag: OsString) { - self.removed_args.push(flag); - } - - /// Push an "exotic" flag to the end of the compiler's arguments list. - /// - /// Nvidia compiler accepts only the most common compiler flags like `-D`, - /// `-I`, `-c`, etc. Options meant specifically for the underlying - /// host C++ compiler have to be prefixed with '-Xcompiler`. - /// [Another possible future application for this function is passing - /// clang-specific flags to clang-cl, which otherwise accepts only - /// MSVC-specific options.] - fn push_cc_arg(&mut self, flag: OsString) { - if self.cuda { - self.args.push("-Xcompiler".into()); - } - self.args.push(flag); - } - - /// Checks if an argument or flag has already been specified or conflicts. - /// - /// Currently only checks optimization flags. - fn is_duplicate_opt_arg(&self, flag: &OsString) -> bool { - let flag = flag.to_str().unwrap(); - let mut chars = flag.chars(); - - // Only duplicate check compiler flags - if self.is_like_msvc() { - if chars.next() != Some('/') { - return false; - } - } else if self.is_like_gnu() || self.is_like_clang() { - if chars.next() != Some('-') { - return false; - } - } - - // Check for existing optimization flags (-O, /O) - if chars.next() == Some('O') { - return self - .args() - .iter() - .any(|ref a| a.to_str().unwrap_or("").chars().nth(1) == Some('O')); - } - - // TODO Check for existing -m..., -m...=..., /arch:... flags - return false; - } - - /// Don't push optimization arg if it conflicts with existing args. - fn push_opt_unless_duplicate(&mut self, flag: OsString) { - if self.is_duplicate_opt_arg(&flag) { - println!("Info: Ignoring duplicate arg {:?}", &flag); - } else { - self.push_cc_arg(flag); - } - } - - /// Converts this compiler into a `Command` that's ready to be run. - /// - /// This is useful for when the compiler needs to be executed and the - /// command returned will already have the initial arguments and environment - /// variables configured. - pub fn to_command(&self) -> Command { - let mut cmd = match self.cc_wrapper_path { - Some(ref cc_wrapper_path) => { - let mut cmd = Command::new(&cc_wrapper_path); - cmd.arg(&self.path); - cmd - } - None => Command::new(&self.path), - }; - cmd.args(&self.cc_wrapper_args); - - let value = self - .args - .iter() - .filter(|a| !self.removed_args.contains(a)) - .collect::<Vec<_>>(); - cmd.args(&value); - - for &(ref k, ref v) in self.env.iter() { - cmd.env(k, v); - } - cmd - } - - /// Returns the path for this compiler. - /// - /// Note that this may not be a path to a file on the filesystem, e.g. "cc", - /// but rather something which will be resolved when a process is spawned. - pub fn path(&self) -> &Path { - &self.path - } - - /// Returns the default set of arguments to the compiler needed to produce - /// executables for the target this compiler generates. - pub fn args(&self) -> &[OsString] { - &self.args - } - - /// Returns the set of environment variables needed for this compiler to - /// operate. - /// - /// This is typically only used for MSVC compilers currently. - pub fn env(&self) -> &[(OsString, OsString)] { - &self.env - } - - /// Returns the compiler command in format of CC environment variable. - /// Or empty string if CC env was not present - /// - /// This is typically used by configure script - pub fn cc_env(&self) -> OsString { - match self.cc_wrapper_path { - Some(ref cc_wrapper_path) => { - let mut cc_env = cc_wrapper_path.as_os_str().to_owned(); - cc_env.push(" "); - cc_env.push(self.path.to_path_buf().into_os_string()); - for arg in self.cc_wrapper_args.iter() { - cc_env.push(" "); - cc_env.push(arg); - } - cc_env - } - None => OsString::from(""), - } - } - - /// Returns the compiler flags in format of CFLAGS environment variable. - /// Important here - this will not be CFLAGS from env, its internal gcc's flags to use as CFLAGS - /// This is typically used by configure script - pub fn cflags_env(&self) -> OsString { - let mut flags = OsString::new(); - for (i, arg) in self.args.iter().enumerate() { - if i > 0 { - flags.push(" "); - } - flags.push(arg); - } - flags - } - - /// Whether the tool is GNU Compiler Collection-like. - pub fn is_like_gnu(&self) -> bool { - self.family == ToolFamily::Gnu - } - - /// Whether the tool is Clang-like. - pub fn is_like_clang(&self) -> bool { - self.family == ToolFamily::Clang - } - - /// Whether the tool is MSVC-like. - pub fn is_like_msvc(&self) -> bool { - match self.family { - ToolFamily::Msvc { .. } => true, - _ => false, - } - } -} - -fn wait_on_child(cmd: &Command, program: &str, child: &mut Child) -> Result<(), Error> { - let status = match child.wait() { - Ok(s) => s, - Err(e) => { - return Err(Error::new( - ErrorKind::ToolExecError, - format!( - "Failed to wait on spawned child process, command {:?} with args {:?}: {}.", - cmd, program, e - ), - )); - } - }; - println!("{}", status); - - if status.success() { - Ok(()) - } else { - Err(Error::new( - ErrorKind::ToolExecError, - format!( - "Command {:?} with args {:?} did not execute successfully (status code {}).", - cmd, program, status - ), - )) - } -} - -#[cfg(feature = "parallel")] -fn try_wait_on_child( - cmd: &Command, - program: &str, - child: &mut Child, - stdout: &mut dyn io::Write, -) -> Result<Option<()>, Error> { - match child.try_wait() { - Ok(Some(status)) => { - let _ = writeln!(stdout, "{}", status); - - if status.success() { - Ok(Some(())) - } else { - Err(Error::new( - ErrorKind::ToolExecError, - format!( - "Command {:?} with args {:?} did not execute successfully (status code {}).", - cmd, program, status - ), - )) - } - } - Ok(None) => Ok(None), - Err(e) => Err(Error::new( - ErrorKind::ToolExecError, - format!( - "Failed to wait on spawned child process, command {:?} with args {:?}: {}.", - cmd, program, e - ), - )), - } -} - -fn run_inner(cmd: &mut Command, program: &str, pipe_writer: File) -> Result<(), Error> { - let mut child = spawn(cmd, program, pipe_writer)?; - wait_on_child(cmd, program, &mut child) -} - -fn run(cmd: &mut Command, program: &str, print: &PrintThread) -> Result<(), Error> { - run_inner(cmd, program, print.pipe_writer_cloned()?.unwrap())?; - - Ok(()) -} - -fn run_output(cmd: &mut Command, program: &str) -> Result<Vec<u8>, Error> { - cmd.stdout(Stdio::piped()); - - let mut print = PrintThread::new()?; - let mut child = spawn(cmd, program, print.pipe_writer().take().unwrap())?; - - let mut stdout = vec![]; - child - .stdout - .take() - .unwrap() - .read_to_end(&mut stdout) - .unwrap(); - - wait_on_child(cmd, program, &mut child)?; - - Ok(stdout) -} - -fn spawn(cmd: &mut Command, program: &str, pipe_writer: File) -> Result<Child, Error> { - struct ResetStderr<'cmd>(&'cmd mut Command); - - impl Drop for ResetStderr<'_> { - fn drop(&mut self) { - // Reset stderr to default to release pipe_writer so that print thread will - // not block forever. - self.0.stderr(Stdio::inherit()); - } - } - - println!("running: {:?}", cmd); - - let cmd = ResetStderr(cmd); - - match cmd.0.stderr(pipe_writer).spawn() { - Ok(child) => Ok(child), - Err(ref e) if e.kind() == io::ErrorKind::NotFound => { - let extra = if cfg!(windows) { - " (see https://github.com/rust-lang/cc-rs#compile-time-requirements \ - for help)" - } else { - "" - }; - Err(Error::new( - ErrorKind::ToolNotFound, - format!("Failed to find tool. Is `{}` installed?{}", program, extra), - )) - } - Err(e) => Err(Error::new( - ErrorKind::ToolExecError, - format!( - "Command {:?} with args {:?} failed to start: {:?}", - cmd.0, program, e - ), - )), - } -} - -fn fail(s: &str) -> ! { - eprintln!("\n\nerror occurred: {}\n\n", s); - std::process::exit(1); -} - -fn command_add_output_file( - cmd: &mut Command, - dst: &Path, - cuda: bool, - msvc: bool, - clang: bool, - gnu: bool, - is_asm: bool, - is_arm: bool, -) { - if msvc && !clang && !gnu && !cuda && !(is_asm && is_arm) { - let mut s = OsString::from("-Fo"); - s.push(&dst); - cmd.arg(s); - } else { - cmd.arg("-o").arg(&dst); - } -} - -// Use by default minimum available API level -// See note about naming here -// https://android.googlesource.com/platform/ndk/+/refs/heads/ndk-release-r21/docs/BuildSystemMaintainers.md#Clang -static NEW_STANDALONE_ANDROID_COMPILERS: [&str; 4] = [ - "aarch64-linux-android21-clang", - "armv7a-linux-androideabi16-clang", - "i686-linux-android16-clang", - "x86_64-linux-android21-clang", -]; - -// New "standalone" C/C++ cross-compiler executables from recent Android NDK -// are just shell scripts that call main clang binary (from Android NDK) with -// proper `--target` argument. -// -// For example, armv7a-linux-androideabi16-clang passes -// `--target=armv7a-linux-androideabi16` to clang. -// So to construct proper command line check if -// `--target` argument would be passed or not to clang -fn android_clang_compiler_uses_target_arg_internally(clang_path: &Path) -> bool { - if let Some(filename) = clang_path.file_name() { - if let Some(filename_str) = filename.to_str() { - if let Some(idx) = filename_str.rfind("-") { - return filename_str.split_at(idx).0.contains("android"); - } - } - } - false -} - -#[test] -fn test_android_clang_compiler_uses_target_arg_internally() { - for version in 16..21 { - assert!(android_clang_compiler_uses_target_arg_internally( - &PathBuf::from(format!("armv7a-linux-androideabi{}-clang", version)) - )); - assert!(android_clang_compiler_uses_target_arg_internally( - &PathBuf::from(format!("armv7a-linux-androideabi{}-clang++", version)) - )); - } - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang-i686-linux-android") - )); - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang") - )); - assert!(!android_clang_compiler_uses_target_arg_internally( - &PathBuf::from("clang++") - )); -} - -fn autodetect_android_compiler(target: &str, host: &str, gnu: &str, clang: &str) -> String { - let new_clang_key = match target { - "aarch64-linux-android" => Some("aarch64"), - "armv7-linux-androideabi" => Some("armv7a"), - "i686-linux-android" => Some("i686"), - "x86_64-linux-android" => Some("x86_64"), - _ => None, - }; - - let new_clang = new_clang_key - .map(|key| { - NEW_STANDALONE_ANDROID_COMPILERS - .iter() - .find(|x| x.starts_with(key)) - }) - .unwrap_or(None); - - if let Some(new_clang) = new_clang { - if Command::new(new_clang).output().is_ok() { - return (*new_clang).into(); - } - } - - let target = target - .replace("armv7neon", "arm") - .replace("armv7", "arm") - .replace("thumbv7neon", "arm") - .replace("thumbv7", "arm"); - let gnu_compiler = format!("{}-{}", target, gnu); - let clang_compiler = format!("{}-{}", target, clang); - - // On Windows, the Android clang compiler is provided as a `.cmd` file instead - // of a `.exe` file. `std::process::Command` won't run `.cmd` files unless the - // `.cmd` is explicitly appended to the command name, so we do that here. - let clang_compiler_cmd = format!("{}-{}.cmd", target, clang); - - // Check if gnu compiler is present - // if not, use clang - if Command::new(&gnu_compiler).output().is_ok() { - gnu_compiler - } else if host.contains("windows") && Command::new(&clang_compiler_cmd).output().is_ok() { - clang_compiler_cmd - } else { - clang_compiler - } -} - -// Rust and clang/cc don't agree on how to name the target. -fn map_darwin_target_from_rust_to_compiler_architecture(target: &str) -> Option<&'static str> { - if target.contains("x86_64h") { - Some("x86_64h") - } else if target.contains("x86_64") { - Some("x86_64") - } else if target.contains("arm64e") { - Some("arm64e") - } else if target.contains("aarch64") { - Some("arm64") - } else if target.contains("i686") { - Some("i386") - } else if target.contains("powerpc") { - Some("ppc") - } else if target.contains("powerpc64") { - Some("ppc64") - } else { - None - } -} - -fn which(tool: &Path, path_entries: Option<OsString>) -> Option<PathBuf> { - fn check_exe(exe: &mut PathBuf) -> bool { - let exe_ext = std::env::consts::EXE_EXTENSION; - exe.exists() || (!exe_ext.is_empty() && exe.set_extension(exe_ext) && exe.exists()) - } - - // If |tool| is not just one "word," assume it's an actual path... - if tool.components().count() > 1 { - let mut exe = PathBuf::from(tool); - return if check_exe(&mut exe) { Some(exe) } else { None }; - } - - // Loop through PATH entries searching for the |tool|. - let path_entries = path_entries.or(env::var_os("PATH"))?; - env::split_paths(&path_entries).find_map(|path_entry| { - let mut exe = path_entry.join(tool); - return if check_exe(&mut exe) { Some(exe) } else { None }; - }) -} - -// search for |prog| on 'programs' path in '|cc| -print-search-dirs' output -fn search_programs(cc: &mut Command, prog: &str) -> Option<PathBuf> { - let search_dirs = run_output(cc.arg("-print-search-dirs"), "cc").ok()?; - // clang driver appears to be forcing UTF-8 output even on Windows, - // hence from_utf8 is assumed to be usable in all cases. - let search_dirs = std::str::from_utf8(&search_dirs).ok()?; - for dirs in search_dirs.split(|c| c == '\r' || c == '\n') { - if let Some(path) = dirs.strip_prefix("programs: =") { - return which(Path::new(prog), Some(OsString::from(path))); - } - } - None -} - -#[derive(Clone, Copy, PartialEq)] -enum AsmFileExt { - /// `.asm` files. On MSVC targets, we assume these should be passed to MASM - /// (`ml{,64}.exe`). - DotAsm, - /// `.s` or `.S` files, which do not have the special handling on MSVC targets. - DotS, -} - -impl AsmFileExt { - fn from_path(file: &Path) -> Option<Self> { - if let Some(ext) = file.extension() { - if let Some(ext) = ext.to_str() { - let ext = ext.to_lowercase(); - match &*ext { - "asm" => return Some(AsmFileExt::DotAsm), - "s" => return Some(AsmFileExt::DotS), - _ => return None, - } - } - } - None - } -} - -struct PrintThread { - handle: Option<JoinHandle<()>>, - pipe_writer: Option<File>, -} - -impl PrintThread { - fn new() -> Result<Self, Error> { - let (pipe_reader, pipe_writer) = os_pipe::pipe()?; - - // Capture the standard error coming from compilation, and write it out - // with cargo:warning= prefixes. Note that this is a bit wonky to avoid - // requiring the output to be UTF-8, we instead just ship bytes from one - // location to another. - let print = thread::spawn(move || { - let mut stderr = BufReader::with_capacity(4096, pipe_reader); - let mut line = Vec::with_capacity(20); - let stdout = io::stdout(); - - // read_until returns 0 on Eof - while stderr.read_until(b'\n', &mut line).unwrap() != 0 { - { - let mut stdout = stdout.lock(); - - stdout.write_all(b"cargo:warning=").unwrap(); - stdout.write_all(&line).unwrap(); - stdout.write_all(b"\n").unwrap(); - } - - // read_until does not clear the buffer - line.clear(); - } - }); - - Ok(Self { - handle: Some(print), - pipe_writer: Some(pipe_writer), - }) - } - - fn pipe_writer(&mut self) -> &mut Option<File> { - &mut self.pipe_writer - } - - fn pipe_writer_cloned(&self) -> Result<Option<File>, Error> { - self.pipe_writer - .as_ref() - .map(File::try_clone) - .transpose() - .map_err(From::from) - } -} - -impl Drop for PrintThread { - fn drop(&mut self) { - // Drop pipe_writer first to avoid deadlock - self.pipe_writer.take(); - - self.handle.take().unwrap().join().unwrap(); - } -} diff --git a/vendor/cc/src/os_pipe.rs b/vendor/cc/src/os_pipe.rs deleted file mode 100644 index 218c9bc..0000000 --- a/vendor/cc/src/os_pipe.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! Adapted from: -//! - https://doc.rust-lang.org/src/std/sys/unix/pipe.rs.html -//! - https://doc.rust-lang.org/src/std/sys/unix/fd.rs.html#385 -//! - https://github.com/rust-lang/rust/blob/master/library/std/src/sys/mod.rs#L57 -//! - https://github.com/oconnor663/os_pipe.rs -use std::fs::File; - -/// Open a new pipe and return a pair of [`File`] objects for the reader and writer. -/// -/// This corresponds to the `pipe2` library call on Posix and the -/// `CreatePipe` library call on Windows (though these implementation -/// details might change). These pipes are non-inheritable, so new child -/// processes won't receive a copy of them unless they're explicitly -/// passed as stdin/stdout/stderr. -pub fn pipe() -> std::io::Result<(File, File)> { - sys::pipe() -} - -#[cfg(unix)] -#[path = "os_pipe/unix.rs"] -mod sys; - -#[cfg(windows)] -#[path = "os_pipe/windows.rs"] -mod sys; - -#[cfg(all(not(unix), not(windows)))] -compile_error!("Only unix and windows support os_pipe!"); diff --git a/vendor/cc/src/os_pipe/unix.rs b/vendor/cc/src/os_pipe/unix.rs deleted file mode 100644 index ec4e547..0000000 --- a/vendor/cc/src/os_pipe/unix.rs +++ /dev/null @@ -1,121 +0,0 @@ -use std::{ - fs::File, - io, - os::{raw::c_int, unix::io::FromRawFd}, -}; - -pub(super) fn pipe() -> io::Result<(File, File)> { - let mut fds = [0; 2]; - - // The only known way right now to create atomically set the CLOEXEC flag is - // to use the `pipe2` syscall. This was added to Linux in 2.6.27, glibc 2.9 - // and musl 0.9.3, and some other targets also have it. - #[cfg(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd", - target_os = "openbsd", - target_os = "redox" - ))] - { - unsafe { - cvt(libc::pipe2(fds.as_mut_ptr(), libc::O_CLOEXEC))?; - } - } - - #[cfg(not(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd", - target_os = "openbsd", - target_os = "redox" - )))] - { - unsafe { - cvt(libc::pipe(fds.as_mut_ptr()))?; - } - - cloexec::set_cloexec(fds[0])?; - cloexec::set_cloexec(fds[1])?; - } - - unsafe { Ok((File::from_raw_fd(fds[0]), File::from_raw_fd(fds[1]))) } -} - -fn cvt(t: c_int) -> io::Result<c_int> { - if t == -1 { - Err(io::Error::last_os_error()) - } else { - Ok(t) - } -} - -#[cfg(not(any( - target_os = "dragonfly", - target_os = "freebsd", - target_os = "linux", - target_os = "netbsd", - target_os = "openbsd", - target_os = "redox" -)))] -mod cloexec { - use super::{c_int, cvt, io}; - - #[cfg(not(any( - target_env = "newlib", - target_os = "solaris", - target_os = "illumos", - target_os = "emscripten", - target_os = "fuchsia", - target_os = "l4re", - target_os = "linux", - target_os = "haiku", - target_os = "redox", - target_os = "vxworks", - target_os = "nto", - )))] - pub(super) fn set_cloexec(fd: c_int) -> io::Result<()> { - unsafe { - cvt(libc::ioctl(fd, libc::FIOCLEX))?; - } - - Ok(()) - } - - #[cfg(any( - all( - target_env = "newlib", - not(any(target_os = "espidf", target_os = "horizon")) - ), - target_os = "solaris", - target_os = "illumos", - target_os = "emscripten", - target_os = "fuchsia", - target_os = "l4re", - target_os = "linux", - target_os = "haiku", - target_os = "redox", - target_os = "vxworks", - target_os = "nto", - ))] - pub(super) fn set_cloexec(fd: c_int) -> io::Result<()> { - unsafe { - let previous = cvt(libc::fcntl(fd, libc::F_GETFD))?; - let new = previous | libc::FD_CLOEXEC; - if new != previous { - cvt(libc::fcntl(fd, libc::F_SETFD, new))?; - } - } - - Ok(()) - } - - // FD_CLOEXEC is not supported in ESP-IDF and Horizon OS but there's no need to, - // because neither supports spawning processes. - #[cfg(any(target_os = "espidf", target_os = "horizon"))] - pub(super) fn set_cloexec(_fd: c_int) -> io::Result<()> { - Ok(()) - } -} diff --git a/vendor/cc/src/os_pipe/windows.rs b/vendor/cc/src/os_pipe/windows.rs deleted file mode 100644 index 212632e..0000000 --- a/vendor/cc/src/os_pipe/windows.rs +++ /dev/null @@ -1,24 +0,0 @@ -use crate::windows_sys::{CreatePipe, INVALID_HANDLE_VALUE}; -use std::{fs::File, io, os::windows::prelude::*, ptr}; - -/// NOTE: These pipes do not support IOCP. -/// -/// If IOCP is needed, then you might want to emulate -/// anonymous pipes with CreateNamedPipe, as Rust's stdlib does. -pub(super) fn pipe() -> io::Result<(File, File)> { - let mut read_pipe = INVALID_HANDLE_VALUE; - let mut write_pipe = INVALID_HANDLE_VALUE; - - let ret = unsafe { CreatePipe(&mut read_pipe, &mut write_pipe, ptr::null_mut(), 0) }; - - if ret == 0 { - Err(io::Error::last_os_error()) - } else { - unsafe { - Ok(( - File::from_raw_handle(read_pipe as RawHandle), - File::from_raw_handle(write_pipe as RawHandle), - )) - } - } -} diff --git a/vendor/cc/src/registry.rs b/vendor/cc/src/registry.rs deleted file mode 100644 index 7c1c2dd..0000000 --- a/vendor/cc/src/registry.rs +++ /dev/null @@ -1,190 +0,0 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -use crate::windows_sys::{ - RegCloseKey, RegEnumKeyExW, RegOpenKeyExW, RegQueryValueExW, ERROR_NO_MORE_ITEMS, - ERROR_SUCCESS, HKEY, HKEY_LOCAL_MACHINE, KEY_READ, KEY_WOW64_32KEY, REG_SZ, -}; -use std::{ - ffi::{OsStr, OsString}, - io, - ops::RangeFrom, - os::windows::prelude::*, - ptr::null_mut, -}; - -/// Must never be `HKEY_PERFORMANCE_DATA`. -pub(crate) struct RegistryKey(Repr); - -type DWORD = u32; - -struct OwnedKey(HKEY); - -/// Note: must not encode `HKEY_PERFORMANCE_DATA` or one of its subkeys. -enum Repr { - /// `HKEY_LOCAL_MACHINE`. - LocalMachine, - /// A subkey of `HKEY_LOCAL_MACHINE`. - Owned(OwnedKey), -} - -pub struct Iter<'a> { - idx: RangeFrom<DWORD>, - key: &'a RegistryKey, -} - -unsafe impl Sync for Repr {} -unsafe impl Send for Repr {} - -pub(crate) const LOCAL_MACHINE: RegistryKey = RegistryKey(Repr::LocalMachine); - -impl RegistryKey { - fn raw(&self) -> HKEY { - match self.0 { - Repr::LocalMachine => HKEY_LOCAL_MACHINE, - Repr::Owned(ref val) => val.0, - } - } - - /// Open a sub-key of `self`. - pub fn open(&self, key: &OsStr) -> io::Result<RegistryKey> { - let key = key.encode_wide().chain(Some(0)).collect::<Vec<_>>(); - let mut ret = null_mut(); - let err = unsafe { - RegOpenKeyExW( - self.raw(), - key.as_ptr(), - 0, - KEY_READ | KEY_WOW64_32KEY, - &mut ret, - ) - }; - if err == ERROR_SUCCESS { - Ok(RegistryKey(Repr::Owned(OwnedKey(ret)))) - } else { - Err(io::Error::from_raw_os_error(err as i32)) - } - } - - pub fn iter(&self) -> Iter { - Iter { - idx: 0.., - key: self, - } - } - - pub fn query_str(&self, name: &str) -> io::Result<OsString> { - let name: &OsStr = name.as_ref(); - let name = name.encode_wide().chain(Some(0)).collect::<Vec<_>>(); - let mut len = 0; - let mut kind = 0; - unsafe { - let err = RegQueryValueExW( - self.raw(), - name.as_ptr(), - null_mut(), - &mut kind, - null_mut(), - &mut len, - ); - if err != ERROR_SUCCESS { - return Err(io::Error::from_raw_os_error(err as i32)); - } - if kind != REG_SZ { - return Err(io::Error::new( - io::ErrorKind::Other, - "registry key wasn't a string", - )); - } - - // The length here is the length in bytes, but we're using wide - // characters so we need to be sure to halve it for the length - // passed in. - assert!(len % 2 == 0, "impossible wide string size: {} bytes", len); - let vlen = len as usize / 2; - // Defensively initialized, see comment about - // `HKEY_PERFORMANCE_DATA` below. - let mut v = vec![0u16; vlen]; - let err = RegQueryValueExW( - self.raw(), - name.as_ptr(), - null_mut(), - null_mut(), - v.as_mut_ptr() as *mut _, - &mut len, - ); - // We don't check for `ERROR_MORE_DATA` (which would if the value - // grew between the first and second call to `RegQueryValueExW`), - // both because it's extremely unlikely, and this is a bit more - // defensive more defensive against weird types of registry keys. - if err != ERROR_SUCCESS { - return Err(io::Error::from_raw_os_error(err as i32)); - } - // The length is allowed to change, but should still be even, as - // well as smaller. - assert!(len % 2 == 0, "impossible wide string size: {} bytes", len); - // If the length grew but returned a success code, it *probably* - // indicates we're `HKEY_PERFORMANCE_DATA` or a subkey(?). We - // consider this UB, since those keys write "undefined" or - // "unpredictable" values to len, and need to use a completely - // different loop structure. This should be impossible (and enforce - // it in the API to the best of our ability), but to mitigate the - // damage we do some smoke-checks on the len, and ensure `v` has - // been fully initialized (rather than trusting the result of - // `RegQueryValueExW`). - let actual_len = len as usize / 2; - assert!(actual_len <= v.len()); - v.truncate(actual_len); - // Some registry keys may have a terminating nul character, but - // we're not interested in that, so chop it off if it's there. - if !v.is_empty() && v[v.len() - 1] == 0 { - v.pop(); - } - return Ok(OsString::from_wide(&v)); - } - } -} - -impl Drop for OwnedKey { - fn drop(&mut self) { - unsafe { - RegCloseKey(self.0); - } - } -} - -impl<'a> Iterator for Iter<'a> { - type Item = io::Result<OsString>; - - fn next(&mut self) -> Option<io::Result<OsString>> { - self.idx.next().and_then(|i| unsafe { - let mut v = Vec::with_capacity(256); - let mut len = v.capacity() as DWORD; - let ret = RegEnumKeyExW( - self.key.raw(), - i, - v.as_mut_ptr(), - &mut len, - null_mut(), - null_mut(), - null_mut(), - null_mut(), - ); - if ret == ERROR_NO_MORE_ITEMS { - None - } else if ret != ERROR_SUCCESS { - Some(Err(io::Error::from_raw_os_error(ret as i32))) - } else { - v.set_len(len as usize); - Some(Ok(OsString::from_wide(&v))) - } - }) - } -} diff --git a/vendor/cc/src/setup_config.rs b/vendor/cc/src/setup_config.rs deleted file mode 100644 index fe2c030..0000000 --- a/vendor/cc/src/setup_config.rs +++ /dev/null @@ -1,283 +0,0 @@ -// Copyright © 2017 winapi-rs developers -// Licensed under the Apache License, Version 2.0 -// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. -// All files in the project carrying such notice may not be copied, modified, or distributed -// except according to those terms. - -#![allow(bad_style)] -#![allow(unused)] - -use crate::{ - com::{BStr, ComPtr}, - winapi::{ - IUnknown, IUnknownVtbl, Interface, LCID, LPCOLESTR, LPCWSTR, LPFILETIME, LPSAFEARRAY, - PULONGLONG, ULONG, - }, - windows_sys::{CoCreateInstance, BSTR, CLSCTX_ALL, HRESULT, S_FALSE}, -}; - -use std::{ - ffi::OsString, - ptr::{null, null_mut}, -}; - -// Bindings to the Setup.Configuration stuff -pub type InstanceState = u32; - -pub const eNone: InstanceState = 0; -pub const eLocal: InstanceState = 1; -pub const eRegistered: InstanceState = 2; -pub const eNoRebootRequired: InstanceState = 4; -pub const eComplete: InstanceState = -1i32 as u32; - -RIDL! {#[uuid(0xb41463c3, 0x8866, 0x43b5, 0xbc, 0x33, 0x2b, 0x06, 0x76, 0xf7, 0xf4, 0x2e)] -interface ISetupInstance(ISetupInstanceVtbl): IUnknown(IUnknownVtbl) { - fn GetInstanceId( - pbstrInstanceId: *mut BSTR, - ) -> HRESULT, - fn GetInstallDate( - pInstallDate: LPFILETIME, - ) -> HRESULT, - fn GetInstallationName( - pbstrInstallationName: *mut BSTR, - ) -> HRESULT, - fn GetInstallationPath( - pbstrInstallationPath: *mut BSTR, - ) -> HRESULT, - fn GetInstallationVersion( - pbstrInstallationVersion: *mut BSTR, - ) -> HRESULT, - fn GetDisplayName( - lcid: LCID, - pbstrDisplayName: *mut BSTR, - ) -> HRESULT, - fn GetDescription( - lcid: LCID, - pbstrDescription: *mut BSTR, - ) -> HRESULT, - fn ResolvePath( - pwszRelativePath: LPCOLESTR, - pbstrAbsolutePath: *mut BSTR, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0x89143c9a, 0x05af, 0x49b0, 0xb7, 0x17, 0x72, 0xe2, 0x18, 0xa2, 0x18, 0x5c)] -interface ISetupInstance2(ISetupInstance2Vtbl): ISetupInstance(ISetupInstanceVtbl) { - fn GetState( - pState: *mut InstanceState, - ) -> HRESULT, - fn GetPackages( - ppsaPackages: *mut LPSAFEARRAY, - ) -> HRESULT, - fn GetProduct( - ppPackage: *mut *mut ISetupPackageReference, - ) -> HRESULT, - fn GetProductPath( - pbstrProductPath: *mut BSTR, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0x6380bcff, 0x41d3, 0x4b2e, 0x8b, 0x2e, 0xbf, 0x8a, 0x68, 0x10, 0xc8, 0x48)] -interface IEnumSetupInstances(IEnumSetupInstancesVtbl): IUnknown(IUnknownVtbl) { - fn Next( - celt: ULONG, - rgelt: *mut *mut ISetupInstance, - pceltFetched: *mut ULONG, - ) -> HRESULT, - fn Skip( - celt: ULONG, - ) -> HRESULT, - fn Reset() -> HRESULT, - fn Clone( - ppenum: *mut *mut IEnumSetupInstances, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0x42843719, 0xdb4c, 0x46c2, 0x8e, 0x7c, 0x64, 0xf1, 0x81, 0x6e, 0xfd, 0x5b)] -interface ISetupConfiguration(ISetupConfigurationVtbl): IUnknown(IUnknownVtbl) { - fn EnumInstances( - ppEnumInstances: *mut *mut IEnumSetupInstances, - ) -> HRESULT, - fn GetInstanceForCurrentProcess( - ppInstance: *mut *mut ISetupInstance, - ) -> HRESULT, - fn GetInstanceForPath( - wzPath: LPCWSTR, - ppInstance: *mut *mut ISetupInstance, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0x26aab78c, 0x4a60, 0x49d6, 0xaf, 0x3b, 0x3c, 0x35, 0xbc, 0x93, 0x36, 0x5d)] -interface ISetupConfiguration2(ISetupConfiguration2Vtbl): - ISetupConfiguration(ISetupConfigurationVtbl) { - fn EnumAllInstances( - ppEnumInstances: *mut *mut IEnumSetupInstances, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0xda8d8a16, 0xb2b6, 0x4487, 0xa2, 0xf1, 0x59, 0x4c, 0xcc, 0xcd, 0x6b, 0xf5)] -interface ISetupPackageReference(ISetupPackageReferenceVtbl): IUnknown(IUnknownVtbl) { - fn GetId( - pbstrId: *mut BSTR, - ) -> HRESULT, - fn GetVersion( - pbstrVersion: *mut BSTR, - ) -> HRESULT, - fn GetChip( - pbstrChip: *mut BSTR, - ) -> HRESULT, - fn GetLanguage( - pbstrLanguage: *mut BSTR, - ) -> HRESULT, - fn GetBranch( - pbstrBranch: *mut BSTR, - ) -> HRESULT, - fn GetType( - pbstrType: *mut BSTR, - ) -> HRESULT, - fn GetUniqueId( - pbstrUniqueId: *mut BSTR, - ) -> HRESULT, -}} - -RIDL! {#[uuid(0x42b21b78, 0x6192, 0x463e, 0x87, 0xbf, 0xd5, 0x77, 0x83, 0x8f, 0x1d, 0x5c)] -interface ISetupHelper(ISetupHelperVtbl): IUnknown(IUnknownVtbl) { - fn ParseVersion( - pwszVersion: LPCOLESTR, - pullVersion: PULONGLONG, - ) -> HRESULT, - fn ParseVersionRange( - pwszVersionRange: LPCOLESTR, - pullMinVersion: PULONGLONG, - pullMaxVersion: PULONGLONG, - ) -> HRESULT, -}} - -DEFINE_GUID! {CLSID_SetupConfiguration, -0x177f0c4a, 0x1cd3, 0x4de7, 0xa3, 0x2c, 0x71, 0xdb, 0xbb, 0x9f, 0xa3, 0x6d} - -// Safe wrapper around the COM interfaces -pub struct SetupConfiguration(ComPtr<ISetupConfiguration>); - -impl SetupConfiguration { - pub fn new() -> Result<SetupConfiguration, i32> { - let mut obj = null_mut(); - let err = unsafe { - CoCreateInstance( - &CLSID_SetupConfiguration, - null_mut(), - CLSCTX_ALL, - &ISetupConfiguration::uuidof(), - &mut obj, - ) - }; - if err < 0 { - return Err(err); - } - let obj = unsafe { ComPtr::from_raw(obj as *mut ISetupConfiguration) }; - Ok(SetupConfiguration(obj)) - } - pub fn get_instance_for_current_process(&self) -> Result<SetupInstance, i32> { - let mut obj = null_mut(); - let err = unsafe { self.0.GetInstanceForCurrentProcess(&mut obj) }; - if err < 0 { - return Err(err); - } - Ok(unsafe { SetupInstance::from_raw(obj) }) - } - pub fn enum_instances(&self) -> Result<EnumSetupInstances, i32> { - let mut obj = null_mut(); - let err = unsafe { self.0.EnumInstances(&mut obj) }; - if err < 0 { - return Err(err); - } - Ok(unsafe { EnumSetupInstances::from_raw(obj) }) - } - pub fn enum_all_instances(&self) -> Result<EnumSetupInstances, i32> { - let mut obj = null_mut(); - let this = self.0.cast::<ISetupConfiguration2>()?; - let err = unsafe { this.EnumAllInstances(&mut obj) }; - if err < 0 { - return Err(err); - } - Ok(unsafe { EnumSetupInstances::from_raw(obj) }) - } -} - -pub struct SetupInstance(ComPtr<ISetupInstance>); - -impl SetupInstance { - pub unsafe fn from_raw(obj: *mut ISetupInstance) -> SetupInstance { - SetupInstance(ComPtr::from_raw(obj)) - } - pub fn instance_id(&self) -> Result<OsString, i32> { - let mut s = null(); - let err = unsafe { self.0.GetInstanceId(&mut s) }; - let bstr = unsafe { BStr::from_raw(s) }; - if err < 0 { - return Err(err); - } - Ok(bstr.to_osstring()) - } - pub fn installation_name(&self) -> Result<OsString, i32> { - let mut s = null(); - let err = unsafe { self.0.GetInstallationName(&mut s) }; - let bstr = unsafe { BStr::from_raw(s) }; - if err < 0 { - return Err(err); - } - Ok(bstr.to_osstring()) - } - pub fn installation_path(&self) -> Result<OsString, i32> { - let mut s = null(); - let err = unsafe { self.0.GetInstallationPath(&mut s) }; - let bstr = unsafe { BStr::from_raw(s) }; - if err < 0 { - return Err(err); - } - Ok(bstr.to_osstring()) - } - pub fn installation_version(&self) -> Result<OsString, i32> { - let mut s = null(); - let err = unsafe { self.0.GetInstallationVersion(&mut s) }; - let bstr = unsafe { BStr::from_raw(s) }; - if err < 0 { - return Err(err); - } - Ok(bstr.to_osstring()) - } - pub fn product_path(&self) -> Result<OsString, i32> { - let mut s = null(); - let this = self.0.cast::<ISetupInstance2>()?; - let err = unsafe { this.GetProductPath(&mut s) }; - let bstr = unsafe { BStr::from_raw(s) }; - if err < 0 { - return Err(err); - } - Ok(bstr.to_osstring()) - } -} - -pub struct EnumSetupInstances(ComPtr<IEnumSetupInstances>); - -impl EnumSetupInstances { - pub unsafe fn from_raw(obj: *mut IEnumSetupInstances) -> EnumSetupInstances { - EnumSetupInstances(ComPtr::from_raw(obj)) - } -} - -impl Iterator for EnumSetupInstances { - type Item = Result<SetupInstance, i32>; - fn next(&mut self) -> Option<Result<SetupInstance, i32>> { - let mut obj = null_mut(); - let err = unsafe { self.0.Next(1, &mut obj, null_mut()) }; - if err < 0 { - return Some(Err(err)); - } - if err == S_FALSE { - return None; - } - Some(Ok(unsafe { SetupInstance::from_raw(obj) })) - } -} diff --git a/vendor/cc/src/vs_instances.rs b/vendor/cc/src/vs_instances.rs deleted file mode 100644 index 31d3dd1..0000000 --- a/vendor/cc/src/vs_instances.rs +++ /dev/null @@ -1,199 +0,0 @@ -use std::borrow::Cow; -use std::collections::HashMap; -use std::convert::TryFrom; -use std::io::BufRead; -use std::path::PathBuf; - -use crate::setup_config::{EnumSetupInstances, SetupInstance}; - -pub enum VsInstance { - Com(SetupInstance), - Vswhere(VswhereInstance), -} - -impl VsInstance { - pub fn installation_name(&self) -> Option<Cow<str>> { - match self { - VsInstance::Com(s) => s - .installation_name() - .ok() - .and_then(|s| s.into_string().ok()) - .map(Cow::from), - VsInstance::Vswhere(v) => v.map.get("installationName").map(Cow::from), - } - } - - pub fn installation_path(&self) -> Option<PathBuf> { - match self { - VsInstance::Com(s) => s.installation_path().ok().map(PathBuf::from), - VsInstance::Vswhere(v) => v.map.get("installationPath").map(PathBuf::from), - } - } - - pub fn installation_version(&self) -> Option<Cow<str>> { - match self { - VsInstance::Com(s) => s - .installation_version() - .ok() - .and_then(|s| s.into_string().ok()) - .map(Cow::from), - VsInstance::Vswhere(v) => v.map.get("installationVersion").map(Cow::from), - } - } -} - -pub enum VsInstances { - ComBased(EnumSetupInstances), - VswhereBased(VswhereInstance), -} - -impl IntoIterator for VsInstances { - type Item = VsInstance; - #[allow(bare_trait_objects)] - type IntoIter = Box<Iterator<Item = Self::Item>>; - - fn into_iter(self) -> Self::IntoIter { - match self { - VsInstances::ComBased(e) => { - Box::new(e.into_iter().filter_map(Result::ok).map(VsInstance::Com)) - } - VsInstances::VswhereBased(v) => Box::new(std::iter::once(VsInstance::Vswhere(v))), - } - } -} - -#[derive(Debug)] -pub struct VswhereInstance { - map: HashMap<String, String>, -} - -impl TryFrom<&Vec<u8>> for VswhereInstance { - type Error = &'static str; - - fn try_from(output: &Vec<u8>) -> Result<Self, Self::Error> { - let map: HashMap<_, _> = output - .lines() - .filter_map(Result::ok) - .filter_map(|s| { - let mut splitn = s.splitn(2, ": "); - Some((splitn.next()?.to_owned(), splitn.next()?.to_owned())) - }) - .collect(); - - if !map.contains_key("installationName") - || !map.contains_key("installationPath") - || !map.contains_key("installationVersion") - { - return Err("required properties not found"); - } - - Ok(Self { map }) - } -} - -#[cfg(test)] -mod tests_ { - use std::borrow::Cow; - use std::convert::TryFrom; - use std::path::PathBuf; - - #[test] - fn it_parses_vswhere_output_correctly() { - let output = br"instanceId: 58104422 -installDate: 21/02/2021 21:50:33 -installationName: VisualStudio/16.9.2+31112.23 -installationPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools -installationVersion: 16.9.31112.23 -productId: Microsoft.VisualStudio.Product.BuildTools -productPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\LaunchDevCmd.bat -state: 4294967295 -isComplete: 1 -isLaunchable: 1 -isPrerelease: 0 -isRebootRequired: 0 -displayName: Visual Studio Build Tools 2019 -description: The Visual Studio Build Tools allows you to build native and managed MSBuild-based applications without requiring the Visual Studio IDE. There are options to install the Visual C++ compilers and libraries, MFC, ATL, and C++/CLI support. -channelId: VisualStudio.16.Release -channelUri: https://aka.ms/vs/16/release/channel -enginePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\resources\app\ServiceHub\Services\Microsoft.VisualStudio.Setup.Service -releaseNotes: https://docs.microsoft.com/en-us/visualstudio/releases/2019/release-notes-v16.9#16.9.2 -thirdPartyNotices: https://go.microsoft.com/fwlink/?LinkId=660909 -updateDate: 2021-03-17T21:16:46.5963702Z -catalog_buildBranch: d16.9 -catalog_buildVersion: 16.9.31112.23 -catalog_id: VisualStudio/16.9.2+31112.23 -catalog_localBuild: build-lab -catalog_manifestName: VisualStudio -catalog_manifestType: installer -catalog_productDisplayVersion: 16.9.2 -catalog_productLine: Dev16 -catalog_productLineVersion: 2019 -catalog_productMilestone: RTW -catalog_productMilestoneIsPreRelease: False -catalog_productName: Visual Studio -catalog_productPatchVersion: 2 -catalog_productPreReleaseMilestoneSuffix: 1.0 -catalog_productSemanticVersion: 16.9.2+31112.23 -catalog_requiredEngineVersion: 2.9.3365.38425 -properties_campaignId: 156063665.1613940062 -properties_channelManifestId: VisualStudio.16.Release/16.9.2+31112.23 -properties_nickname: -properties_setupEngineFilePath: C:\Program Files (x86)\Microsoft Visual Studio\Installer\vs_installershell.exe -" - .to_vec(); - - let vswhere_instance = super::VswhereInstance::try_from(&output); - assert!(vswhere_instance.is_ok()); - - let vs_instance = super::VsInstance::Vswhere(vswhere_instance.unwrap()); - assert_eq!( - vs_instance.installation_name(), - Some(Cow::from("VisualStudio/16.9.2+31112.23")) - ); - assert_eq!( - vs_instance.installation_path(), - Some(PathBuf::from( - r"C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools" - )) - ); - assert_eq!( - vs_instance.installation_version(), - Some(Cow::from("16.9.31112.23")) - ); - } - - #[test] - fn it_returns_an_error_for_empty_output() { - let output = b"".to_vec(); - - let vswhere_instance = super::VswhereInstance::try_from(&output); - - assert!(vswhere_instance.is_err()); - } - - #[test] - fn it_returns_an_error_for_output_consisting_of_empty_lines() { - let output = br" - -" - .to_vec(); - - let vswhere_instance = super::VswhereInstance::try_from(&output); - - assert!(vswhere_instance.is_err()); - } - - #[test] - fn it_returns_an_error_for_output_without_required_properties() { - let output = br"instanceId: 58104422 -installDate: 21/02/2021 21:50:33 -productId: Microsoft.VisualStudio.Product.BuildTools -productPath: C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\LaunchDevCmd.bat -" - .to_vec(); - - let vswhere_instance = super::VswhereInstance::try_from(&output); - - assert!(vswhere_instance.is_err()); - } -} diff --git a/vendor/cc/src/winapi.rs b/vendor/cc/src/winapi.rs deleted file mode 100644 index 223599f..0000000 --- a/vendor/cc/src/winapi.rs +++ /dev/null @@ -1,146 +0,0 @@ -// Copyright © 2015-2017 winapi-rs developers -// Licensed under the Apache License, Version 2.0 -// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. -// All files in the project carrying such notice may not be copied, modified, or distributed -// except according to those terms. - -#![allow(bad_style)] - -use std::os::raw; - -pub type wchar_t = u16; - -pub use crate::windows_sys::{FILETIME, GUID, HRESULT, SAFEARRAY, SAFEARRAYBOUND}; - -pub type REFIID = *const IID; -pub type IID = GUID; -pub type ULONG = raw::c_ulong; -pub type DWORD = u32; -pub type LPFILETIME = *mut FILETIME; -pub type OLECHAR = WCHAR; -pub type WCHAR = wchar_t; -pub type LPCOLESTR = *const OLECHAR; -pub type LCID = DWORD; -pub type LPCWSTR = *const WCHAR; -pub type PULONGLONG = *mut ULONGLONG; -pub type ULONGLONG = u64; - -pub trait Interface { - fn uuidof() -> GUID; -} - -pub type LPSAFEARRAY = *mut SAFEARRAY; - -macro_rules! DEFINE_GUID { - ( - $name:ident, $l:expr, $w1:expr, $w2:expr, - $b1:expr, $b2:expr, $b3:expr, $b4:expr, $b5:expr, $b6:expr, $b7:expr, $b8:expr - ) => { - pub const $name: $crate::winapi::GUID = $crate::winapi::GUID { - data1: $l, - data2: $w1, - data3: $w2, - data4: [$b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8], - }; - }; -} - -macro_rules! RIDL { - (#[uuid($($uuid:expr),+)] - interface $interface:ident ($vtbl:ident) {$( - fn $method:ident($($p:ident : $t:ty,)*) -> $rtr:ty, - )+}) => ( - #[repr(C)] - pub struct $vtbl { - $(pub $method: unsafe extern "system" fn( - This: *mut $interface, - $($p: $t),* - ) -> $rtr,)+ - } - #[repr(C)] - pub struct $interface { - pub lpVtbl: *const $vtbl, - } - RIDL!{@impl $interface {$(fn $method($($p: $t,)*) -> $rtr,)+}} - RIDL!{@uuid $interface $($uuid),+} - ); - (#[uuid($($uuid:expr),+)] - interface $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ident) { - }) => ( - #[repr(C)] - pub struct $vtbl { - pub parent: $pvtbl, - } - #[repr(C)] - pub struct $interface { - pub lpVtbl: *const $vtbl, - } - RIDL!{@deref $interface $pinterface} - RIDL!{@uuid $interface $($uuid),+} - ); - (#[uuid($($uuid:expr),+)] - interface $interface:ident ($vtbl:ident) : $pinterface:ident ($pvtbl:ident) {$( - fn $method:ident($($p:ident : $t:ty,)*) -> $rtr:ty, - )+}) => ( - #[repr(C)] - pub struct $vtbl { - pub parent: $pvtbl, - $(pub $method: unsafe extern "system" fn( - This: *mut $interface, - $($p: $t,)* - ) -> $rtr,)+ - } - #[repr(C)] - pub struct $interface { - pub lpVtbl: *const $vtbl, - } - RIDL!{@impl $interface {$(fn $method($($p: $t,)*) -> $rtr,)+}} - RIDL!{@deref $interface $pinterface} - RIDL!{@uuid $interface $($uuid),+} - ); - (@deref $interface:ident $pinterface:ident) => ( - impl ::std::ops::Deref for $interface { - type Target = $pinterface; - #[inline] - fn deref(&self) -> &$pinterface { - unsafe { &*(self as *const $interface as *const $pinterface) } - } - } - ); - (@impl $interface:ident {$( - fn $method:ident($($p:ident : $t:ty,)*) -> $rtr:ty, - )+}) => ( - impl $interface { - $(#[inline] pub unsafe fn $method(&self, $($p: $t,)*) -> $rtr { - ((*self.lpVtbl).$method)(self as *const _ as *mut _, $($p,)*) - })+ - } - ); - (@uuid $interface:ident - $l:expr, $w1:expr, $w2:expr, - $b1:expr, $b2:expr, $b3:expr, $b4:expr, $b5:expr, $b6:expr, $b7:expr, $b8:expr - ) => ( - impl $crate::winapi::Interface for $interface { - #[inline] - fn uuidof() -> $crate::winapi::GUID { - $crate::winapi::GUID { - data1: $l, - data2: $w1, - data3: $w2, - data4: [$b1, $b2, $b3, $b4, $b5, $b6, $b7, $b8], - } - } - } - ); -} - -RIDL! {#[uuid(0x00000000, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x46)] -interface IUnknown(IUnknownVtbl) { - fn QueryInterface( - riid: REFIID, - ppvObject: *mut *mut raw::c_void, - ) -> HRESULT, - fn AddRef() -> ULONG, - fn Release() -> ULONG, -}} diff --git a/vendor/cc/src/windows_registry.rs b/vendor/cc/src/windows_registry.rs deleted file mode 100644 index 07a9c4d..0000000 --- a/vendor/cc/src/windows_registry.rs +++ /dev/null @@ -1,896 +0,0 @@ -// Copyright 2015 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or -// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license -// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -//! A helper module to probe the Windows Registry when looking for -//! windows-specific tools. - -use std::process::Command; - -use crate::Tool; -#[cfg(windows)] -use crate::ToolFamily; - -#[cfg(windows)] -const MSVC_FAMILY: ToolFamily = ToolFamily::Msvc { clang_cl: false }; - -/// Attempts to find a tool within an MSVC installation using the Windows -/// registry as a point to search from. -/// -/// The `target` argument is the target that the tool should work for (e.g. -/// compile or link for) and the `tool` argument is the tool to find (e.g. -/// `cl.exe` or `link.exe`). -/// -/// This function will return `None` if the tool could not be found, or it will -/// return `Some(cmd)` which represents a command that's ready to execute the -/// tool with the appropriate environment variables set. -/// -/// Note that this function always returns `None` for non-MSVC targets. -pub fn find(target: &str, tool: &str) -> Option<Command> { - find_tool(target, tool).map(|c| c.to_command()) -} - -/// Similar to the `find` function above, this function will attempt the same -/// operation (finding a MSVC tool in a local install) but instead returns a -/// `Tool` which may be introspected. -#[cfg(not(windows))] -pub fn find_tool(_target: &str, _tool: &str) -> Option<Tool> { - None -} - -/// Documented above. -#[cfg(windows)] -pub fn find_tool(target: &str, tool: &str) -> Option<Tool> { - // This logic is all tailored for MSVC, if we're not that then bail out - // early. - if !target.contains("msvc") { - return None; - } - - // Looks like msbuild isn't located in the same location as other tools like - // cl.exe and lib.exe. To handle this we probe for it manually with - // dedicated registry keys. - if tool.contains("msbuild") { - return impl_::find_msbuild(target); - } - - if tool.contains("devenv") { - return impl_::find_devenv(target); - } - - // Ok, if we're here, now comes the fun part of the probing. Default shells - // or shells like MSYS aren't really configured to execute `cl.exe` and the - // various compiler tools shipped as part of Visual Studio. Here we try to - // first find the relevant tool, then we also have to be sure to fill in - // environment variables like `LIB`, `INCLUDE`, and `PATH` to ensure that - // the tool is actually usable. - - return impl_::find_msvc_environment(tool, target) - .or_else(|| impl_::find_msvc_15plus(tool, target)) - .or_else(|| impl_::find_msvc_14(tool, target)) - .or_else(|| impl_::find_msvc_12(tool, target)) - .or_else(|| impl_::find_msvc_11(tool, target)); -} - -/// A version of Visual Studio -#[derive(Debug, PartialEq, Eq, Copy, Clone)] -#[non_exhaustive] -pub enum VsVers { - /// Visual Studio 12 (2013) - Vs12, - /// Visual Studio 14 (2015) - Vs14, - /// Visual Studio 15 (2017) - Vs15, - /// Visual Studio 16 (2019) - Vs16, - /// Visual Studio 17 (2022) - Vs17, -} - -/// Find the most recent installed version of Visual Studio -/// -/// This is used by the cmake crate to figure out the correct -/// generator. -#[cfg(not(windows))] -pub fn find_vs_version() -> Result<VsVers, String> { - Err("not windows".to_string()) -} - -/// Documented above -#[cfg(windows)] -pub fn find_vs_version() -> Result<VsVers, String> { - use std::env; - - match env::var("VisualStudioVersion") { - Ok(version) => match &version[..] { - "17.0" => Ok(VsVers::Vs17), - "16.0" => Ok(VsVers::Vs16), - "15.0" => Ok(VsVers::Vs15), - "14.0" => Ok(VsVers::Vs14), - "12.0" => Ok(VsVers::Vs12), - vers => Err(format!( - "\n\n\ - unsupported or unknown VisualStudio version: {}\n\ - if another version is installed consider running \ - the appropriate vcvars script before building this \ - crate\n\ - ", - vers - )), - }, - _ => { - // Check for the presence of a specific registry key - // that indicates visual studio is installed. - if impl_::has_msbuild_version("17.0") { - Ok(VsVers::Vs17) - } else if impl_::has_msbuild_version("16.0") { - Ok(VsVers::Vs16) - } else if impl_::has_msbuild_version("15.0") { - Ok(VsVers::Vs15) - } else if impl_::has_msbuild_version("14.0") { - Ok(VsVers::Vs14) - } else if impl_::has_msbuild_version("12.0") { - Ok(VsVers::Vs12) - } else { - Err(format!( - "\n\n\ - couldn't determine visual studio generator\n\ - if VisualStudio is installed, however, consider \ - running the appropriate vcvars script before building \ - this crate\n\ - " - )) - } - } - } -} - -#[cfg(windows)] -mod impl_ { - use crate::com; - use crate::registry::{RegistryKey, LOCAL_MACHINE}; - use crate::setup_config::SetupConfiguration; - use crate::vs_instances::{VsInstances, VswhereInstance}; - use std::convert::TryFrom; - use std::env; - use std::ffi::OsString; - use std::fs::File; - use std::io::Read; - use std::iter; - use std::mem; - use std::path::{Path, PathBuf}; - use std::process::Command; - use std::str::FromStr; - - use super::MSVC_FAMILY; - use crate::Tool; - - struct MsvcTool { - tool: PathBuf, - libs: Vec<PathBuf>, - path: Vec<PathBuf>, - include: Vec<PathBuf>, - } - - impl MsvcTool { - fn new(tool: PathBuf) -> MsvcTool { - MsvcTool { - tool: tool, - libs: Vec::new(), - path: Vec::new(), - include: Vec::new(), - } - } - - fn into_tool(self) -> Tool { - let MsvcTool { - tool, - libs, - path, - include, - } = self; - let mut tool = Tool::with_family(tool.into(), MSVC_FAMILY); - add_env(&mut tool, "LIB", libs); - add_env(&mut tool, "PATH", path); - add_env(&mut tool, "INCLUDE", include); - tool - } - } - - /// Checks to see if the `VSCMD_ARG_TGT_ARCH` environment variable matches the - /// given target's arch. Returns `None` if the variable does not exist. - #[cfg(windows)] - fn is_vscmd_target(target: &str) -> Option<bool> { - let vscmd_arch = env::var("VSCMD_ARG_TGT_ARCH").ok()?; - // Convert the Rust target arch to its VS arch equivalent. - let arch = match target.split("-").next() { - Some("x86_64") => "x64", - Some("aarch64") => "arm64", - Some("i686") | Some("i586") => "x86", - Some("thumbv7a") => "arm", - // An unrecognized arch. - _ => return Some(false), - }; - Some(vscmd_arch == arch) - } - - /// Attempt to find the tool using environment variables set by vcvars. - pub fn find_msvc_environment(tool: &str, target: &str) -> Option<Tool> { - // Early return if the environment doesn't contain a VC install. - if env::var_os("VCINSTALLDIR").is_none() { - return None; - } - let vs_install_dir = env::var_os("VSINSTALLDIR")?.into(); - - // If the vscmd target differs from the requested target then - // attempt to get the tool using the VS install directory. - if is_vscmd_target(target) == Some(false) { - // We will only get here with versions 15+. - tool_from_vs15plus_instance(tool, target, &vs_install_dir) - } else { - // Fallback to simply using the current environment. - env::var_os("PATH") - .and_then(|path| { - env::split_paths(&path) - .map(|p| p.join(tool)) - .find(|p| p.exists()) - }) - .map(|path| Tool::with_family(path.into(), MSVC_FAMILY)) - } - } - - fn find_msbuild_vs17(target: &str) -> Option<Tool> { - find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "17") - } - - #[allow(bare_trait_objects)] - fn vs16plus_instances(target: &str, version: &'static str) -> Box<Iterator<Item = PathBuf>> { - let instances = if let Some(instances) = vs15plus_instances(target) { - instances - } else { - return Box::new(iter::empty()); - }; - Box::new(instances.into_iter().filter_map(move |instance| { - let installation_name = instance.installation_name()?; - if installation_name.starts_with(&format!("VisualStudio/{}.", version)) { - Some(instance.installation_path()?) - } else if installation_name.starts_with(&format!("VisualStudioPreview/{}.", version)) { - Some(instance.installation_path()?) - } else { - None - } - })) - } - - fn find_tool_in_vs16plus_path(tool: &str, target: &str, version: &'static str) -> Option<Tool> { - vs16plus_instances(target, version) - .filter_map(|path| { - let path = path.join(tool); - if !path.is_file() { - return None; - } - let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target.contains("x86_64") { - tool.env.push(("Platform".into(), "X64".into())); - } - if target.contains("aarch64") { - tool.env.push(("Platform".into(), "ARM64".into())); - } - Some(tool) - }) - .next() - } - - fn find_msbuild_vs16(target: &str) -> Option<Tool> { - find_tool_in_vs16plus_path(r"MSBuild\Current\Bin\MSBuild.exe", target, "16") - } - - // In MSVC 15 (2017) MS once again changed the scheme for locating - // the tooling. Now we must go through some COM interfaces, which - // is super fun for Rust. - // - // Note that much of this logic can be found [online] wrt paths, COM, etc. - // - // [online]: https://blogs.msdn.microsoft.com/vcblog/2017/03/06/finding-the-visual-c-compiler-tools-in-visual-studio-2017/ - // - // Returns MSVC 15+ instances (15, 16 right now), the order should be consider undefined. - // - // However, on ARM64 this method doesn't work because VS Installer fails to register COM component on ARM64. - // Hence, as the last resort we try to use vswhere.exe to list available instances. - fn vs15plus_instances(target: &str) -> Option<VsInstances> { - vs15plus_instances_using_com().or_else(|| vs15plus_instances_using_vswhere(target)) - } - - fn vs15plus_instances_using_com() -> Option<VsInstances> { - com::initialize().ok()?; - - let config = SetupConfiguration::new().ok()?; - let enum_setup_instances = config.enum_all_instances().ok()?; - - Some(VsInstances::ComBased(enum_setup_instances)) - } - - fn vs15plus_instances_using_vswhere(target: &str) -> Option<VsInstances> { - let program_files_path: PathBuf = env::var("ProgramFiles(x86)") - .or_else(|_| env::var("ProgramFiles")) - .ok()? - .into(); - - let vswhere_path = - program_files_path.join(r"Microsoft Visual Studio\Installer\vswhere.exe"); - - if !vswhere_path.exists() { - return None; - } - - let arch = target.split('-').next().unwrap(); - let tools_arch = match arch { - "i586" | "i686" | "x86_64" => Some("x86.x64"), - "arm" | "thumbv7a" => Some("ARM"), - "aarch64" => Some("ARM64"), - _ => None, - }; - - let vswhere_output = Command::new(vswhere_path) - .args(&[ - "-latest", - "-products", - "*", - "-requires", - &format!("Microsoft.VisualStudio.Component.VC.Tools.{}", tools_arch?), - "-format", - "text", - "-nologo", - ]) - .stderr(std::process::Stdio::inherit()) - .output() - .ok()?; - - let vs_instances = - VsInstances::VswhereBased(VswhereInstance::try_from(&vswhere_output.stdout).ok()?); - - Some(vs_instances) - } - - // Inspired from official microsoft/vswhere ParseVersionString - // i.e. at most four u16 numbers separated by '.' - fn parse_version(version: &str) -> Option<Vec<u16>> { - version - .split('.') - .map(|chunk| u16::from_str(chunk).ok()) - .collect() - } - - pub fn find_msvc_15plus(tool: &str, target: &str) -> Option<Tool> { - let iter = vs15plus_instances(target)?; - iter.into_iter() - .filter_map(|instance| { - let version = parse_version(&instance.installation_version()?)?; - let instance_path = instance.installation_path()?; - let tool = tool_from_vs15plus_instance(tool, target, &instance_path)?; - Some((version, tool)) - }) - .max_by(|(a_version, _), (b_version, _)| a_version.cmp(b_version)) - .map(|(_version, tool)| tool) - } - - // While the paths to Visual Studio 2017's devenv and MSBuild could - // potentially be retrieved from the registry, finding them via - // SetupConfiguration has shown to be [more reliable], and is preferred - // according to Microsoft. To help head off potential regressions though, - // we keep the registry method as a fallback option. - // - // [more reliable]: https://github.com/rust-lang/cc-rs/pull/331 - fn find_tool_in_vs15_path(tool: &str, target: &str) -> Option<Tool> { - let mut path = match vs15plus_instances(target) { - Some(instances) => instances - .into_iter() - .filter_map(|instance| instance.installation_path()) - .map(|path| path.join(tool)) - .find(|ref path| path.is_file()), - None => None, - }; - - if path.is_none() { - let key = r"SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7"; - path = LOCAL_MACHINE - .open(key.as_ref()) - .ok() - .and_then(|key| key.query_str("15.0").ok()) - .map(|path| PathBuf::from(path).join(tool)) - .and_then(|path| if path.is_file() { Some(path) } else { None }); - } - - path.map(|path| { - let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target.contains("x86_64") { - tool.env.push(("Platform".into(), "X64".into())); - } - if target.contains("aarch64") { - tool.env.push(("Platform".into(), "ARM64".into())); - } - tool - }) - } - - fn tool_from_vs15plus_instance( - tool: &str, - target: &str, - instance_path: &PathBuf, - ) -> Option<Tool> { - let (root_path, bin_path, host_dylib_path, lib_path, include_path) = - vs15plus_vc_paths(target, instance_path)?; - let tool_path = bin_path.join(tool); - if !tool_path.exists() { - return None; - }; - - let mut tool = MsvcTool::new(tool_path); - tool.path.push(bin_path.clone()); - tool.path.push(host_dylib_path); - tool.libs.push(lib_path); - tool.include.push(include_path); - - if let Some((atl_lib_path, atl_include_path)) = atl_paths(target, &root_path) { - tool.libs.push(atl_lib_path); - tool.include.push(atl_include_path); - } - - add_sdks(&mut tool, target)?; - - Some(tool.into_tool()) - } - - fn vs15plus_vc_paths( - target: &str, - instance_path: &PathBuf, - ) -> Option<(PathBuf, PathBuf, PathBuf, PathBuf, PathBuf)> { - let version_path = - instance_path.join(r"VC\Auxiliary\Build\Microsoft.VCToolsVersion.default.txt"); - let mut version_file = File::open(version_path).ok()?; - let mut version = String::new(); - version_file.read_to_string(&mut version).ok()?; - let version = version.trim(); - let host = match host_arch() { - X86 => "X86", - X86_64 => "X64", - // There is no natively hosted compiler on ARM64. - // Instead, use the x86 toolchain under emulation (there is no x64 emulation). - AARCH64 => "X86", - _ => return None, - }; - let target = lib_subdir(target)?; - // The directory layout here is MSVC/bin/Host$host/$target/ - let path = instance_path.join(r"VC\Tools\MSVC").join(version); - // This is the path to the toolchain for a particular target, running - // on a given host - let bin_path = path - .join("bin") - .join(&format!("Host{}", host)) - .join(&target); - // But! we also need PATH to contain the target directory for the host - // architecture, because it contains dlls like mspdb140.dll compiled for - // the host architecture. - let host_dylib_path = path - .join("bin") - .join(&format!("Host{}", host)) - .join(&host.to_lowercase()); - let lib_path = path.join("lib").join(&target); - let include_path = path.join("include"); - Some((path, bin_path, host_dylib_path, lib_path, include_path)) - } - - fn atl_paths(target: &str, path: &Path) -> Option<(PathBuf, PathBuf)> { - let atl_path = path.join("atlmfc"); - let sub = lib_subdir(target)?; - if atl_path.exists() { - Some((atl_path.join("lib").join(sub), atl_path.join("include"))) - } else { - None - } - } - - // For MSVC 14 we need to find the Universal CRT as well as either - // the Windows 10 SDK or Windows 8.1 SDK. - pub fn find_msvc_14(tool: &str, target: &str) -> Option<Tool> { - let vcdir = get_vc_dir("14.0")?; - let mut tool = get_tool(tool, &vcdir, target)?; - add_sdks(&mut tool, target)?; - Some(tool.into_tool()) - } - - fn add_sdks(tool: &mut MsvcTool, target: &str) -> Option<()> { - let sub = lib_subdir(target)?; - let (ucrt, ucrt_version) = get_ucrt_dir()?; - - let host = match host_arch() { - X86 => "x86", - X86_64 => "x64", - AARCH64 => "arm64", - _ => return None, - }; - - tool.path - .push(ucrt.join("bin").join(&ucrt_version).join(host)); - - let ucrt_include = ucrt.join("include").join(&ucrt_version); - tool.include.push(ucrt_include.join("ucrt")); - - let ucrt_lib = ucrt.join("lib").join(&ucrt_version); - tool.libs.push(ucrt_lib.join("ucrt").join(sub)); - - if let Some((sdk, version)) = get_sdk10_dir() { - tool.path.push(sdk.join("bin").join(host)); - let sdk_lib = sdk.join("lib").join(&version); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk.join("include").join(&version); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("cppwinrt")); - tool.include.push(sdk_include.join("winrt")); - tool.include.push(sdk_include.join("shared")); - } else if let Some(sdk) = get_sdk81_dir() { - tool.path.push(sdk.join("bin").join(host)); - let sdk_lib = sdk.join("lib").join("winv6.3"); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk.join("include"); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("winrt")); - tool.include.push(sdk_include.join("shared")); - } - - Some(()) - } - - // For MSVC 12 we need to find the Windows 8.1 SDK. - pub fn find_msvc_12(tool: &str, target: &str) -> Option<Tool> { - let vcdir = get_vc_dir("12.0")?; - let mut tool = get_tool(tool, &vcdir, target)?; - let sub = lib_subdir(target)?; - let sdk81 = get_sdk81_dir()?; - tool.path.push(sdk81.join("bin").join(sub)); - let sdk_lib = sdk81.join("lib").join("winv6.3"); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk81.join("include"); - tool.include.push(sdk_include.join("shared")); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("winrt")); - Some(tool.into_tool()) - } - - // For MSVC 11 we need to find the Windows 8 SDK. - pub fn find_msvc_11(tool: &str, target: &str) -> Option<Tool> { - let vcdir = get_vc_dir("11.0")?; - let mut tool = get_tool(tool, &vcdir, target)?; - let sub = lib_subdir(target)?; - let sdk8 = get_sdk8_dir()?; - tool.path.push(sdk8.join("bin").join(sub)); - let sdk_lib = sdk8.join("lib").join("win8"); - tool.libs.push(sdk_lib.join("um").join(sub)); - let sdk_include = sdk8.join("include"); - tool.include.push(sdk_include.join("shared")); - tool.include.push(sdk_include.join("um")); - tool.include.push(sdk_include.join("winrt")); - Some(tool.into_tool()) - } - - fn add_env(tool: &mut Tool, env: &str, paths: Vec<PathBuf>) { - let prev = env::var_os(env).unwrap_or(OsString::new()); - let prev = env::split_paths(&prev); - let new = paths.into_iter().chain(prev); - tool.env - .push((env.to_string().into(), env::join_paths(new).unwrap())); - } - - // Given a possible MSVC installation directory, we look for the linker and - // then add the MSVC library path. - fn get_tool(tool: &str, path: &Path, target: &str) -> Option<MsvcTool> { - bin_subdir(target) - .into_iter() - .map(|(sub, host)| { - ( - path.join("bin").join(sub).join(tool), - path.join("bin").join(host), - ) - }) - .filter(|&(ref path, _)| path.is_file()) - .map(|(path, host)| { - let mut tool = MsvcTool::new(path); - tool.path.push(host); - tool - }) - .filter_map(|mut tool| { - let sub = vc_lib_subdir(target)?; - tool.libs.push(path.join("lib").join(sub)); - tool.include.push(path.join("include")); - let atlmfc_path = path.join("atlmfc"); - if atlmfc_path.exists() { - tool.libs.push(atlmfc_path.join("lib").join(sub)); - tool.include.push(atlmfc_path.join("include")); - } - Some(tool) - }) - .next() - } - - // To find MSVC we look in a specific registry key for the version we are - // trying to find. - fn get_vc_dir(ver: &str) -> Option<PathBuf> { - let key = r"SOFTWARE\Microsoft\VisualStudio\SxS\VC7"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let path = key.query_str(ver).ok()?; - Some(path.into()) - } - - // To find the Universal CRT we look in a specific registry key for where - // all the Universal CRTs are located and then sort them asciibetically to - // find the newest version. While this sort of sorting isn't ideal, it is - // what vcvars does so that's good enough for us. - // - // Returns a pair of (root, version) for the ucrt dir if found - fn get_ucrt_dir() -> Option<(PathBuf, String)> { - let key = r"SOFTWARE\Microsoft\Windows Kits\Installed Roots"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let root = key.query_str("KitsRoot10").ok()?; - let readdir = Path::new(&root).join("lib").read_dir().ok()?; - let max_libdir = readdir - .filter_map(|dir| dir.ok()) - .map(|dir| dir.path()) - .filter(|dir| { - dir.components() - .last() - .and_then(|c| c.as_os_str().to_str()) - .map(|c| c.starts_with("10.") && dir.join("ucrt").is_dir()) - .unwrap_or(false) - }) - .max()?; - let version = max_libdir.components().last().unwrap(); - let version = version.as_os_str().to_str().unwrap().to_string(); - Some((root.into(), version)) - } - - // Vcvars finds the correct version of the Windows 10 SDK by looking - // for the include `um\Windows.h` because sometimes a given version will - // only have UCRT bits without the rest of the SDK. Since we only care about - // libraries and not includes, we instead look for `um\x64\kernel32.lib`. - // Since the 32-bit and 64-bit libraries are always installed together we - // only need to bother checking x64, making this code a tiny bit simpler. - // Like we do for the Universal CRT, we sort the possibilities - // asciibetically to find the newest one as that is what vcvars does. - // Before doing that, we check the "WindowsSdkDir" and "WindowsSDKVersion" - // environment variables set by vcvars to use the environment sdk version - // if one is already configured. - fn get_sdk10_dir() -> Option<(PathBuf, String)> { - if let (Ok(root), Ok(version)) = (env::var("WindowsSdkDir"), env::var("WindowsSDKVersion")) - { - return Some((root.into(), version.trim_end_matches('\\').to_string())); - } - - let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let root = key.query_str("InstallationFolder").ok()?; - let readdir = Path::new(&root).join("lib").read_dir().ok()?; - let mut dirs = readdir - .filter_map(|dir| dir.ok()) - .map(|dir| dir.path()) - .collect::<Vec<_>>(); - dirs.sort(); - let dir = dirs - .into_iter() - .rev() - .filter(|dir| dir.join("um").join("x64").join("kernel32.lib").is_file()) - .next()?; - let version = dir.components().last().unwrap(); - let version = version.as_os_str().to_str().unwrap().to_string(); - Some((root.into(), version)) - } - - // Interestingly there are several subdirectories, `win7` `win8` and - // `winv6.3`. Vcvars seems to only care about `winv6.3` though, so the same - // applies to us. Note that if we were targeting kernel mode drivers - // instead of user mode applications, we would care. - fn get_sdk81_dir() -> Option<PathBuf> { - let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.1"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let root = key.query_str("InstallationFolder").ok()?; - Some(root.into()) - } - - fn get_sdk8_dir() -> Option<PathBuf> { - let key = r"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v8.0"; - let key = LOCAL_MACHINE.open(key.as_ref()).ok()?; - let root = key.query_str("InstallationFolder").ok()?; - Some(root.into()) - } - - const PROCESSOR_ARCHITECTURE_INTEL: u16 = 0; - const PROCESSOR_ARCHITECTURE_AMD64: u16 = 9; - const PROCESSOR_ARCHITECTURE_ARM64: u16 = 12; - const X86: u16 = PROCESSOR_ARCHITECTURE_INTEL; - const X86_64: u16 = PROCESSOR_ARCHITECTURE_AMD64; - const AARCH64: u16 = PROCESSOR_ARCHITECTURE_ARM64; - - // When choosing the tool to use, we have to choose the one which matches - // the target architecture. Otherwise we end up in situations where someone - // on 32-bit Windows is trying to cross compile to 64-bit and it tries to - // invoke the native 64-bit compiler which won't work. - // - // For the return value of this function, the first member of the tuple is - // the folder of the tool we will be invoking, while the second member is - // the folder of the host toolchain for that tool which is essential when - // using a cross linker. We return a Vec since on x64 there are often two - // linkers that can target the architecture we desire. The 64-bit host - // linker is preferred, and hence first, due to 64-bit allowing it more - // address space to work with and potentially being faster. - fn bin_subdir(target: &str) -> Vec<(&'static str, &'static str)> { - let arch = target.split('-').next().unwrap(); - match (arch, host_arch()) { - ("i586", X86) | ("i686", X86) => vec![("", "")], - ("i586", X86_64) | ("i686", X86_64) => vec![("amd64_x86", "amd64"), ("", "")], - ("x86_64", X86) => vec![("x86_amd64", "")], - ("x86_64", X86_64) => vec![("amd64", "amd64"), ("x86_amd64", "")], - ("arm", X86) | ("thumbv7a", X86) => vec![("x86_arm", "")], - ("arm", X86_64) | ("thumbv7a", X86_64) => vec![("amd64_arm", "amd64"), ("x86_arm", "")], - _ => vec![], - } - } - - fn lib_subdir(target: &str) -> Option<&'static str> { - let arch = target.split('-').next().unwrap(); - match arch { - "i586" | "i686" => Some("x86"), - "x86_64" => Some("x64"), - "arm" | "thumbv7a" => Some("arm"), - "aarch64" => Some("arm64"), - _ => None, - } - } - - // MSVC's x86 libraries are not in a subfolder - fn vc_lib_subdir(target: &str) -> Option<&'static str> { - let arch = target.split('-').next().unwrap(); - match arch { - "i586" | "i686" => Some(""), - "x86_64" => Some("amd64"), - "arm" | "thumbv7a" => Some("arm"), - "aarch64" => Some("arm64"), - _ => None, - } - } - - #[allow(bad_style)] - fn host_arch() -> u16 { - type DWORD = u32; - type WORD = u16; - type LPVOID = *mut u8; - type DWORD_PTR = usize; - - #[repr(C)] - struct SYSTEM_INFO { - wProcessorArchitecture: WORD, - _wReserved: WORD, - _dwPageSize: DWORD, - _lpMinimumApplicationAddress: LPVOID, - _lpMaximumApplicationAddress: LPVOID, - _dwActiveProcessorMask: DWORD_PTR, - _dwNumberOfProcessors: DWORD, - _dwProcessorType: DWORD, - _dwAllocationGranularity: DWORD, - _wProcessorLevel: WORD, - _wProcessorRevision: WORD, - } - - extern "system" { - fn GetNativeSystemInfo(lpSystemInfo: *mut SYSTEM_INFO); - } - - unsafe { - let mut info = mem::zeroed(); - GetNativeSystemInfo(&mut info); - info.wProcessorArchitecture - } - } - - // Given a registry key, look at all the sub keys and find the one which has - // the maximal numeric value. - // - // Returns the name of the maximal key as well as the opened maximal key. - fn max_version(key: &RegistryKey) -> Option<(OsString, RegistryKey)> { - let mut max_vers = 0; - let mut max_key = None; - for subkey in key.iter().filter_map(|k| k.ok()) { - let val = subkey - .to_str() - .and_then(|s| s.trim_left_matches("v").replace(".", "").parse().ok()); - let val = match val { - Some(s) => s, - None => continue, - }; - if val > max_vers { - if let Ok(k) = key.open(&subkey) { - max_vers = val; - max_key = Some((subkey, k)); - } - } - } - max_key - } - - pub fn has_msbuild_version(version: &str) -> bool { - match version { - "17.0" => { - find_msbuild_vs17("x86_64-pc-windows-msvc").is_some() - || find_msbuild_vs17("i686-pc-windows-msvc").is_some() - || find_msbuild_vs17("aarch64-pc-windows-msvc").is_some() - } - "16.0" => { - find_msbuild_vs16("x86_64-pc-windows-msvc").is_some() - || find_msbuild_vs16("i686-pc-windows-msvc").is_some() - || find_msbuild_vs16("aarch64-pc-windows-msvc").is_some() - } - "15.0" => { - find_msbuild_vs15("x86_64-pc-windows-msvc").is_some() - || find_msbuild_vs15("i686-pc-windows-msvc").is_some() - || find_msbuild_vs15("aarch64-pc-windows-msvc").is_some() - } - "12.0" | "14.0" => LOCAL_MACHINE - .open(&OsString::from(format!( - "SOFTWARE\\Microsoft\\MSBuild\\ToolsVersions\\{}", - version - ))) - .is_ok(), - _ => false, - } - } - - pub fn find_devenv(target: &str) -> Option<Tool> { - find_devenv_vs15(&target) - } - - fn find_devenv_vs15(target: &str) -> Option<Tool> { - find_tool_in_vs15_path(r"Common7\IDE\devenv.exe", target) - } - - // see http://stackoverflow.com/questions/328017/path-to-msbuild - pub fn find_msbuild(target: &str) -> Option<Tool> { - // VS 15 (2017) changed how to locate msbuild - if let Some(r) = find_msbuild_vs17(target) { - return Some(r); - } else if let Some(r) = find_msbuild_vs16(target) { - return Some(r); - } else if let Some(r) = find_msbuild_vs15(target) { - return Some(r); - } else { - find_old_msbuild(target) - } - } - - fn find_msbuild_vs15(target: &str) -> Option<Tool> { - find_tool_in_vs15_path(r"MSBuild\15.0\Bin\MSBuild.exe", target) - } - - fn find_old_msbuild(target: &str) -> Option<Tool> { - let key = r"SOFTWARE\Microsoft\MSBuild\ToolsVersions"; - LOCAL_MACHINE - .open(key.as_ref()) - .ok() - .and_then(|key| { - max_version(&key).and_then(|(_vers, key)| key.query_str("MSBuildToolsPath").ok()) - }) - .map(|path| { - let mut path = PathBuf::from(path); - path.push("MSBuild.exe"); - let mut tool = Tool::with_family(path, MSVC_FAMILY); - if target.contains("x86_64") { - tool.env.push(("Platform".into(), "X64".into())); - } - tool - }) - } -} diff --git a/vendor/cc/src/windows_sys.rs b/vendor/cc/src/windows_sys.rs deleted file mode 100644 index ee4704d..0000000 --- a/vendor/cc/src/windows_sys.rs +++ /dev/null @@ -1,198 +0,0 @@ -// This file is autogenerated. -// -// To add bindings, edit windows_sys.lst then run: -// -// ``` -// cd generate-windows-sys/ -// cargo run -// ``` -// Bindings generated by `windows-bindgen` 0.49.0 - -#![allow( - non_snake_case, - non_upper_case_globals, - non_camel_case_types, - dead_code, - clippy::all -)] -#[link(name = "advapi32")] -extern "system" { - pub fn RegCloseKey(hkey: HKEY) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegEnumKeyExW( - hkey: HKEY, - dwindex: u32, - lpname: PWSTR, - lpcchname: *mut u32, - lpreserved: *const u32, - lpclass: PWSTR, - lpcchclass: *mut u32, - lpftlastwritetime: *mut FILETIME, - ) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegOpenKeyExW( - hkey: HKEY, - lpsubkey: PCWSTR, - uloptions: u32, - samdesired: REG_SAM_FLAGS, - phkresult: *mut HKEY, - ) -> WIN32_ERROR; -} -#[link(name = "advapi32")] -extern "system" { - pub fn RegQueryValueExW( - hkey: HKEY, - lpvaluename: PCWSTR, - lpreserved: *const u32, - lptype: *mut REG_VALUE_TYPE, - lpdata: *mut u8, - lpcbdata: *mut u32, - ) -> WIN32_ERROR; -} -#[link(name = "kernel32")] -extern "system" { - pub fn CreatePipe( - hreadpipe: *mut HANDLE, - hwritepipe: *mut HANDLE, - lppipeattributes: *const SECURITY_ATTRIBUTES, - nsize: u32, - ) -> BOOL; -} -#[link(name = "ole32")] -extern "system" { - pub fn CoCreateInstance( - rclsid: *const GUID, - punkouter: IUnknown, - dwclscontext: CLSCTX, - riid: *const GUID, - ppv: *mut *mut ::core::ffi::c_void, - ) -> HRESULT; -} -#[link(name = "ole32")] -extern "system" { - pub fn CoInitializeEx(pvreserved: *const ::core::ffi::c_void, dwcoinit: COINIT) -> HRESULT; -} -#[link(name = "oleaut32")] -extern "system" { - pub fn SysFreeString(bstrstring: BSTR) -> (); -} -#[link(name = "oleaut32")] -extern "system" { - pub fn SysStringLen(pbstr: BSTR) -> u32; -} -pub type ADVANCED_FEATURE_FLAGS = u16; -pub type BOOL = i32; -pub type BSTR = *const u16; -pub type CLSCTX = u32; -pub const CLSCTX_ALL: CLSCTX = 23u32; -pub type COINIT = i32; -pub const COINIT_MULTITHREADED: COINIT = 0i32; -pub const ERROR_NO_MORE_ITEMS: WIN32_ERROR = 259u32; -pub const ERROR_SUCCESS: WIN32_ERROR = 0u32; -#[repr(C)] -pub struct FILETIME { - pub dwLowDateTime: u32, - pub dwHighDateTime: u32, -} -impl ::core::marker::Copy for FILETIME {} -impl ::core::clone::Clone for FILETIME { - fn clone(&self) -> Self { - *self - } -} -#[repr(C)] -pub struct GUID { - pub data1: u32, - pub data2: u16, - pub data3: u16, - pub data4: [u8; 8], -} -impl GUID { - pub const fn from_u128(uuid: u128) -> Self { - Self { - data1: (uuid >> 96) as u32, - data2: (uuid >> 80 & 0xffff) as u16, - data3: (uuid >> 64 & 0xffff) as u16, - data4: (uuid as u64).to_be_bytes(), - } - } -} -impl ::core::marker::Copy for GUID {} -impl ::core::clone::Clone for GUID { - fn clone(&self) -> Self { - *self - } -} -pub type HANDLE = *mut ::core::ffi::c_void; -pub type HKEY = *mut ::core::ffi::c_void; -pub const HKEY_LOCAL_MACHINE: HKEY = invalid_mut(-2147483646i32 as _); -pub type HRESULT = i32; -pub const INVALID_HANDLE_VALUE: HANDLE = invalid_mut(-1i32 as _); -pub type IUnknown = *mut ::core::ffi::c_void; -pub const KEY_READ: REG_SAM_FLAGS = 131097u32; -pub const KEY_WOW64_32KEY: REG_SAM_FLAGS = 512u32; -pub type PCWSTR = *const u16; -pub type PWSTR = *mut u16; -pub type REG_SAM_FLAGS = u32; -pub const REG_SZ: REG_VALUE_TYPE = 1u32; -pub type REG_VALUE_TYPE = u32; -#[repr(C)] -pub struct SAFEARRAY { - pub cDims: u16, - pub fFeatures: ADVANCED_FEATURE_FLAGS, - pub cbElements: u32, - pub cLocks: u32, - pub pvData: *mut ::core::ffi::c_void, - pub rgsabound: [SAFEARRAYBOUND; 1], -} -impl ::core::marker::Copy for SAFEARRAY {} -impl ::core::clone::Clone for SAFEARRAY { - fn clone(&self) -> Self { - *self - } -} -#[repr(C)] -pub struct SAFEARRAYBOUND { - pub cElements: u32, - pub lLbound: i32, -} -impl ::core::marker::Copy for SAFEARRAYBOUND {} -impl ::core::clone::Clone for SAFEARRAYBOUND { - fn clone(&self) -> Self { - *self - } -} -#[repr(C)] -pub struct SECURITY_ATTRIBUTES { - pub nLength: u32, - pub lpSecurityDescriptor: *mut ::core::ffi::c_void, - pub bInheritHandle: BOOL, -} -impl ::core::marker::Copy for SECURITY_ATTRIBUTES {} -impl ::core::clone::Clone for SECURITY_ATTRIBUTES { - fn clone(&self) -> Self { - *self - } -} -pub const S_FALSE: HRESULT = 1i32; -pub const S_OK: HRESULT = 0i32; -pub type WIN32_ERROR = u32; - -/// Adapted from -/// [`core::ptr::invalid_mut()`](https://doc.rust-lang.org/src/core/ptr/mod.rs.html#600-607). -/// -/// This function should actually use `core::mem::transmute` but due to msrv -/// we use `as` casting instead. -/// -/// Once msrv is bumped to 1.56, replace this with `core::mem::transmute` since -/// it is const stablised in 1.56 -/// -/// NOTE that once supports `strict_provenance` we would also have to update -/// this. -const fn invalid_mut<T>(addr: usize) -> *mut T { - addr as *mut T -} |