Skip to content

Commit

Permalink
Add GOARCH and GOOS env vars to Go command
Browse files Browse the repository at this point in the history
  • Loading branch information
Techassi committed Oct 26, 2023
1 parent dd01271 commit 49dccf4
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions rust/helm-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ links = "helm"

[build-dependencies]
bindgen.workspace = true
snafu.workspace = true
64 changes: 62 additions & 2 deletions rust/helm-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
use std::{env, path::PathBuf, process::Command};
use std::{
env::{self, VarError},
path::PathBuf,
process::Command,
};

use snafu::{ResultExt, Snafu};

#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Failed to find env var"))]
EnvVarNotFound { source: VarError },

#[snafu(display("Unsupported GOARCH: {arch}"))]
UnsupportedGoArch { arch: String },

#[snafu(display("Unsupported GOOS: {os}"))]
UnsupportedGoOs { os: String },
}

fn main() {
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

println!("cargo:rerun-if-changed=go-helm-wrapper/main.go");

let goarch = get_goarch().unwrap();
let goos = get_goos().unwrap();

let mut cmd = Command::new("go");
cmd.arg("build")
.args(["-buildmode", "c-archive"])
.arg("-o")
.arg(out_path.join("libgo-helm-wrapper.a"))
.arg("go-helm-wrapper/main.go")
.env("CGO_ENABLED", "1");
.env("CGO_ENABLED", "1")
.env("GOARCH", goarch)
.env("GOOS", goos);

cmd.status().expect("Failed to build go-helm-wrapper");

let bindings = bindgen::builder()
Expand All @@ -30,3 +54,39 @@ fn main() {
out_path.to_str().unwrap()
);
}

fn get_goarch() -> Result<String, Error> {
let arch = env::var("CARGO_CFG_TARGET_ARCH").context(EnvVarNotFoundSnafu)?;

let arch = match arch.as_str() {
"x86" => "386",
"x86_64" => "amd64",
"mips" => "mips",
"powerpc" => "ppc",
"powerpc64" => "ppc64",
"arm" => "arm",
"aarch64" => "arm64",
_ => return UnsupportedGoArchSnafu { arch }.fail(),
};

Ok(arch.into())
}

fn get_goos() -> Result<String, Error> {
let os = env::var("CARGO_CFG_TARGET_OS").context(EnvVarNotFoundSnafu)?;

let os = match os.as_str() {
"windows" => "windows",
"macos" => "darwin",
"ios" => "darwin",
"linux" => "linux",
"android" => "android",
"freebsd" => "freebsd",
"dragonfly" => "dragonfly",
"openbsd" => "openbsd",
"netbsd" => "netbsd",
_ => return UnsupportedGoOsSnafu { os }.fail(),
};

Ok(os.into())
}

0 comments on commit 49dccf4

Please sign in to comment.