1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//! compiler collection

use cc::Build;
use std::env;
use std::path::Path;

pub fn cl_cpp_openmp() -> Build {
    let mut builder = Build::new();
    println!("cargo:rerun-if-env-changed=CC");
    if cfg!(target_os = "macos") {
        if !env::var("CC").is_ok() {
            // if CC env var is not set, we try some common
            // overrides.
            if Path::new("/opt/homebrew/opt/llvm/bin/clang").exists() {
                // on macos m1, we use the homebrew clang compiler.
                // this supports openmp.
                //
                // this also supports some static linking of openmp,
                // but according to the openmp docs it is not recommended.
                builder.compiler("/opt/homebrew/opt/llvm/bin/clang");
                println!("cargo:rustc-link-search=/opt/homebrew/opt/llvm/lib");
                println!("cargo:rustc-link-search=/opt/homebrew/lib");
            }
        }
        // on macos, the library is omp.
        println!("cargo:rustc-link-lib=dylib=omp");
    }
    else {
        // on linux, the library is gomp.
        // static linking is also available but not very straightforward,
        // as the libgomp.a is hidden somewhere. also not preferred.
        println!("cargo:rustc-link-lib=dylib=gomp");
    }
    builder
        .cpp(true)
        .flag("-Wall")
        .flag("-fopenmp");
    builder
}

pub fn cl_cuda(archs: Option<&[u32]>) -> Build {
    let mut builder_cuda = Build::new();
    builder_cuda
        .cuda(true)
        .flag("-Xcompiler").flag("-Wall");
    for arch in archs.unwrap_or(&[86, 75, 61]) {
        builder_cuda.flag("-gencode")
            .flag(&format!("arch=compute_{arch},code=sm_{arch}"));
    }
    builder_cuda
}