fn preprocess_cmd()

in src/compiler/gcc.rs [705:785]


fn preprocess_cmd<F, T>(
    cmd: &mut T,
    parsed_args: &ParsedArguments,
    cwd: &Path,
    env_vars: &[(OsString, OsString)],
    may_dist: bool,
    kind: CCompilerKind,
    rewrite_includes_only: bool,
    ignorable_whitespace_flags: Vec<String>,
    language_to_arg: F,
) where
    F: Fn(Language) -> Option<&'static str>,
    T: RunCommand,
{
    let language = language_to_arg(parsed_args.language);
    if let Some(lang) = &language {
        cmd.arg("-x").arg(lang);
    }
    cmd.arg("-E");
    // When performing distributed compilation, line number info is important for error
    // reporting and to not cause spurious compilation failure (e.g. no exceptions build
    // fails due to exceptions transitively included in the stdlib).
    // With -fprofile-generate line number information is important, so don't use -P.
    if !may_dist && !parsed_args.profile_generate {
        cmd.args(&ignorable_whitespace_flags);
    }
    if rewrite_includes_only {
        if parsed_args.suppress_rewrite_includes_only {
            if log_enabled!(Trace) {
                trace!("preprocess: pedantic arguments disable rewrite_includes_only");
            }
        } else {
            match kind {
                CCompilerKind::Clang => {
                    cmd.arg("-frewrite-includes");
                }
                CCompilerKind::Gcc => {
                    cmd.arg("-fdirectives-only");
                }
                _ => {}
            }
        }
    }

    // Explicitly rewrite the -arch args to be preprocessor defines of the form
    // __arch__ so that they affect the preprocessor output but don't cause
    // clang to error.
    let rewritten_arch_args = parsed_args
        .arch_args
        .iter()
        .filter(|&arg| arg.ne(ARCH_FLAG))
        .filter_map(|arg| {
            arg.to_str()
                .map(|arg_string| format!("-D__{}__=1", arg_string).into())
        })
        .collect::<Vec<OsString>>();

    let mut arch_args_to_use = &rewritten_arch_args;
    let mut unique_rewritten = rewritten_arch_args.clone();
    unique_rewritten.sort();
    unique_rewritten.dedup();
    if unique_rewritten.len() <= 1 {
        // don't use rewritten arch args if there is only one arch
        arch_args_to_use = &parsed_args.arch_args;
    } else {
        debug!("-arch args before rewrite: {:?}", parsed_args.arch_args);
        debug!("-arch args after rewrite:  {:?}", arch_args_to_use);
    }

    cmd.args(&parsed_args.preprocessor_args)
        .args(&parsed_args.dependency_args)
        .args(&parsed_args.common_args)
        .args(arch_args_to_use);
    if parsed_args.double_dash_input {
        cmd.arg("--");
    }
    cmd.arg(&parsed_args.input)
        .env_clear()
        .envs(env_vars.to_vec())
        .current_dir(cwd);
}