Function libpng_src::build_artifact

source ·
pub fn build_artifact(
    target_str: &str,
    working_dir: &Path
) -> Result<Artifacts, Box<dyn Error>>
Expand description

Builds all artifacts and aggregates library and include headers in a directory. Would create working directory if missing. Would remove previous content of ‘build/’ and ‘libpng/’ subdirectories if not empty (see below).

§Example

// 'build.rs' of an another crate
use std::{env::var, path::PathBuf};

use libpng_src::build_artifact;

fn main() {
    let target = var("TARGET").unwrap();
    let out_dir = var("OUT_DIR").map(PathBuf::from).unwrap();

    let artifact_info = build_artifact(&target, &out_dir)
        .unwrap();

    println!("cargo:rustc-link-search=native={}", artifact_info.lib_dir.to_string_lossy());
    println!("cargo:rustc-link-lib=static={}", artifact_info.link_name);
}

§Example with bindgen

use std::{env::var, path::PathBuf};
// 'build.rs' of an another crate

use bindgen;

use libpng_src::build_artifact;

fn main() {
    let target = var("TARGET").unwrap();
    let out_dir = var("OUT_DIR").map(PathBuf::from).unwrap();

    let artifact_info = build_artifact(&target, &out_dir)
        .unwrap();

    println!("cargo:rustc-link-search=native={}", artifact_info.lib_dir.to_string_lossy());
    println!("cargo:rustc-link-lib=static={}", artifact_info.link_name);

    let main_header_path = artifact_info.include_dir.join("png.h");

    bindgen::builder()
        .header(main_header_path.to_string_lossy())
        .allowlist_file(main_header_path.to_string_lossy())
        .generate()
        .unwrap()
        .write_to_file(out_dir.join("bindings.rs"))
        .unwrap()
}

§File structure

working_directory/
    |->build/  ... Temporary build directory - do not use directly.
    └->libpng/ ... Artifact root directory.
        |->include/ ... C include headers - generate FFI bindings.
        └->lib/ ... Static library - add to link search path.