dcr 0.6.7

DCR is a utility for managing C/C++ projects in a Cargo-like style.
pub mod common;
pub mod gas;
pub mod msvc;
pub mod nasm;
pub mod unix_cc;

pub struct BuildContext<'a> {
    pub profile: &'a str,
    pub project_name: &'a str,
    pub compiler: &'a str,
    pub language: &'a str,
    pub standard: &'a str,
    #[allow(dead_code)]
    pub target: Option<&'a str>,
    pub target_dir: Option<&'a str>,
    pub kind: &'a str,
    pub platform: Option<&'a str>,
    pub linker: Option<&'a str>,
    pub archiver: Option<&'a str>,
    pub package_type: Option<&'a str>,
    pub source_roots: &'a [std::path::PathBuf],
    pub exclude_dirs: &'a [std::path::PathBuf],
    pub include_paths: &'a [String],
    pub include_dirs: &'a [String],
    pub lib_dirs: &'a [String],
    pub libs: &'a [String],
    pub cflags: &'a [String],
    pub ldflags: &'a [String],
    pub output_filename: Option<&'a str>,
    pub output_extension: Option<&'a str>,
    pub verbose: bool,
}

pub fn build(ctx: &BuildContext) -> Result<f64, String> {
    let compiler = ctx.compiler.to_lowercase();
    if !check_compiler_exists(ctx.compiler) {
        return Err(format!(
            "Compiler not found: {}. Make sure it is installed and available in PATH.",
            ctx.compiler
        ));
    }
    if compiler.contains("clang-cl") {
        return msvc::build(ctx);
    }
    if compiler == "as" || compiler.contains("gas") {
        return gas::build(ctx);
    }
    if compiler.contains("nasm") {
        return nasm::build(ctx);
    }
    if compiler == "cl" || compiler.contains("msvc") {
        return msvc::build(ctx);
    }
    unix_cc::build(ctx)
}

fn check_compiler_exists(compiler: &str) -> bool {
    let name = if compiler.is_empty() { "cc" } else { compiler };
    std::process::Command::new(name)
        .arg("--version")
        .output()
        .is_ok()
}

pub fn collect_sources(ctx: &BuildContext) -> Result<Vec<String>, String> {
    let compiler = ctx.compiler.to_lowercase();
    if compiler.contains("clang-cl") || compiler == "cl" || compiler.contains("msvc") {
        return msvc::collect_sources(ctx);
    }
    if compiler == "as" || compiler.contains("gas") {
        return gas::collect_sources(ctx);
    }
    if compiler.contains("nasm") {
        return nasm::collect_sources(ctx);
    }
    unix_cc::collect_sources(ctx)
}