//! Extension library build module.

use super::{C_STANDARD, CXX_STANDARD};
use crate::linker;
use std::path::{Path, PathBuf};

#[allow(dead_code)]
pub fn build(install_dir: &Path, leveldb_prefix: &Path) -> PathBuf {
    println!("[extension] Building");
    let mut config = cmake::Config::new("extension");
    config
        .define("CMAKE_CXX_STANDARD", CXX_STANDARD)
        .define("CMAKE_C_STANDARD", C_STANDARD)
        .define("CMAKE_INSTALL_LIBDIR", install_dir)
        .define("LEVELDB_INSTALL_DIR", leveldb_prefix)
        .cflag(format!("-I{}", leveldb_prefix.join("include").display()))
        .cxxflag(format!("-I{}", leveldb_prefix.join("include").display()));
    linker::apply_cmake_flags(&mut config);

    let lib_paths = vec![
        leveldb_prefix
            .join(install_dir.file_name().unwrap())
            .display()
            .to_string(),
    ];
    linker::set_ldflags(&lib_paths);

    let dest_prefix = config.build();
    assert_eq!(
        dest_prefix.join(install_dir.file_name().unwrap()),
        install_dir,
        "CMake should build extensions in provided install_dir"
    );
    dest_prefix
}
