Skip to content

Commit

Permalink
Fix release perms
Browse files Browse the repository at this point in the history
  • Loading branch information
littledivy committed May 26, 2024
1 parent a46c2e2 commit 22c881e
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ on:
push:
tags: [ '**' ]

permissions:
contents: write

env:
CARGO_TERM_COLOR: always

Expand Down
95 changes: 95 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
use std::env;
use std::path::PathBuf;

fn main() {
if cfg!(target_os = "windows") {
download_prebuilt();
return;
}

let mut config = cmake::Config::new(".");
config
.define("BUILD_SHARED_LIBS", "OFF")
Expand All @@ -15,3 +23,90 @@ fn main() {
#[cfg(target_os = "macos")]
println!("cargo:rustc-link-lib=dylib=c++");
}

fn download_prebuilt() {
let url = static_lib_url();
println!("Downloading prebuilt lib from {}", url);

let out_dir = static_lib_dir();
std::fs::create_dir_all(&out_dir).unwrap();
println!("cargo:rustc-link-search={}", out_dir.display());

match std::fs::read_to_string(static_checksum_path()) {
Ok(c) if c.trim() == url => {
println!("Using cached prebuilt lib");
return;
}
_ => {}
};

let curl = std::process::Command::new("curl")
.arg("-L")
.arg("-o")
.arg(out_dir.join("sui.lib.gz"))
.arg(url)
.status()
.expect("Failed to download prebuilt lib");
assert!(curl.success());

std::fs::write(static_checksum_path(), url).unwrap();
}

fn static_lib_name() -> &'static str {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
if target_os == "windows" {
"sui.lib"
} else {
"libsui.a"
}
}

fn static_lib_path() -> PathBuf {
static_lib_dir().join(static_lib_name())
}

fn static_lib_dir() -> PathBuf {
build_dir().join("out")
}

fn static_checksum_path() -> PathBuf {
let mut t = static_lib_path();
t.set_extension("sum");
t
}

fn build_dir() -> PathBuf {
let root = env::current_dir().unwrap();

let out_dir = env::var_os("OUT_DIR").expect(
"The 'OUT_DIR' environment is not set (it should be something like \
'target/debug/sui-{hash}').",
);
let out_dir_abs = root.join(out_dir);

// This would be target/debug or target/release
out_dir_abs
.parent()
.unwrap()
.parent()
.unwrap()
.parent()
.unwrap()
.to_path_buf()
}


fn static_lib_url() -> String {
let default_base = "https://github.com/littledivy/sui/releases/download";
let base =
env::var("SUI_MIRROR").unwrap_or_else(|_| default_base.into());
let version = env::var("CARGO_PKG_VERSION").unwrap();
let target = env::var("TARGET").unwrap();
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
// Note: we always use the release build on windows.
if target_os == "windows" {
return format!("{}/v{}/sui_release_{}.lib.gz", base, version, target);
}

unimplemented!()
}

0 comments on commit 22c881e

Please sign in to comment.