Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(installation): download lib binaries #191

Merged
merged 21 commits into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
^\.task$
^src/\.cargo$
^src/rust/vendor$
^src/Makevars$
^tools/libprqlr\.a$
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ docs

src/rust/vendor
src/rust/vendor.tar.xz
src/Makevars
tools/libprqlr.a
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Language: en-US
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
SystemRequirements: GNU make, Cargo (Rust's package manager), rustc
SystemRequirements: Cargo (Rust's package manager), rustc
VignetteBuilder: knitr
Config/testthat/edition: 3
Config/rextendr/version: 0.3.1
Expand All @@ -45,3 +45,4 @@ Config/Needs/dev:
Config/Needs/website:
pkgdown,
rextendr
Config/prqlr/LibVersion: 0.1.0
3 changes: 3 additions & 0 deletions cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env sh

rm -f src/Makevars
27 changes: 27 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#!/usr/bin/env sh

NOT_CRAN=${NOT_CRAN:-"false"}
LIBPRQLR_BUILD=${LIBPRQLR_BUILD:-""}
LIBNAME="libprqlr.a"

export PATH="$PATH:$HOME/.cargo/bin"

check_cargo() {
Expand All @@ -18,6 +22,29 @@ check_cargo() {
fi
}

check_bin_lib() {
if [ "${NOT_CRAN}" = "true" ] && [ -z "${LIBPRQLR_BUILD}" ]; then
LIBPRQLR_BUILD="false"
fi

if [ "${LIBPRQLR_BUILD}" = "false" ] && [ -f "tools/lib-sums.tsv" ] && [ ! -f "tools/${LIBNAME}" ] ; then
echo "Try to download pre-built binary..."
Rscript "tools/prep-lib.R" || echo "Failed to download pre-built binary..."
fi

if [ "${LIBPRQLR_BUILD}" = "false" ] && [ -f "tools/${LIBNAME}" ]; then
echo "----------------------- [LIBRARY FOUND]---------------------------"
echo "The library was found in the tools directory. No need to build it."
echo "-------------------------------------------------------------------"
echo ""
sed -e "s|@RUST_TARGET@||" src/Makevars.in >src/Makevars
exit 0
fi
}

check_bin_lib
check_cargo

sed -e "s|@RUST_TARGET@|$(rustc -vV | grep host | cut -d' ' -f2)|" src/Makevars.in >src/Makevars

exit 0
40 changes: 40 additions & 0 deletions dev/config-lib.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
base_url <- "https://github.com/eitsupi/prqlr/releases/download/"

tag_prefix <- "lib-"

lib_data_file_path <- file.path("tools", "lib-sums.tsv")

package_name <- desc::desc_get("Package")
current_lib_version <- RcppTOML::parseTOML("src/rust/Cargo.toml")$package$version

latest_released_lib_version <- gert::git_remote_ls(remote = "https://github.com/eitsupi/prqlr/") |>
dplyr::pull(ref) |>
stringr::str_subset(stringr::str_c(r"(^refs/tags/)", tag_prefix)) |>
stringr::str_remove(stringr::str_c(".*", tag_prefix)) |>
sort(decreasing = TRUE) |>
_[1]

write_bin_lib_data <- function(path, sums_url, libs_base_url) {
df <- readr::read_table(sums_url, col_names = FALSE, show_col_types = FALSE) |>
dplyr::mutate(
url = glue::glue("{libs_base_url}{X2}"),
sha256sum = X1,
.keep = "none"
)

readr::write_tsv(df, path)
}

desc::desc_set(paste0("Config/", package_name, "/LibVersion"), current_lib_version)

if (identical(current_lib_version, latest_released_lib_version)) {
message("Current lib version is available via the binary release.")
write_bin_lib_data(
lib_data_file_path,
glue::glue("{base_url}{tag_prefix }{latest_released_lib_version}/sha256sums.txt"),
glue::glue("{base_url}{tag_prefix }{latest_released_lib_version}/")
)
} else {
message("Current lib version is not available via binary releases.")
fs::file_delete(lib_data_file_path)
}
4 changes: 2 additions & 2 deletions src/Makevars → src/Makevars.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
TARGET ?= $(shell export PATH="$(PATH):$(HOME)/.cargo/bin" && rustc -vV | grep host | cut -d" " -f2)
TARGET ?= @RUST_TARGET@
PRQLR_PROFILE ?= release
PRQLR_FEATURES ?=

Expand Down Expand Up @@ -43,4 +43,4 @@ C_clean:
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)"

clean:
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)" "$(CURDIR)/rust/target"
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)" "$(TARGET_DIR)"
2 changes: 1 addition & 1 deletion src/Makevars.win
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ C_clean:
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)"

clean:
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)" "$(CURDIR)/rust/target"
rm -Rf "$(SHLIB)" "$(STATLIB)" "$(OBJECTS)" "$(TARGET_DIR)"
6 changes: 6 additions & 0 deletions tools/lib-sums.tsv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
url sha256sum
https://github.com/eitsupi/prqlr/releases/download/lib-0.1.0/libprqlr-0.1.0-aarch64-apple-darwin.tar.gz 2b4a7d38fab3dc56d4502aa54adf5bfe9480f14bf2b9a2bc6d952d5c5f15285b
https://github.com/eitsupi/prqlr/releases/download/lib-0.1.0/libprqlr-0.1.0-aarch64-unknown-linux-musl.tar.gz a81a8643eb6dce90c249dbd704759759d54a295b38cc23525e8b1aa1da9bd570
https://github.com/eitsupi/prqlr/releases/download/lib-0.1.0/libprqlr-0.1.0-x86_64-apple-darwin.tar.gz e49045b9418083a2a7a1cb0fc5f301e6d630618a26e048948ad6cc039043c4f6
https://github.com/eitsupi/prqlr/releases/download/lib-0.1.0/libprqlr-0.1.0-x86_64-pc-windows-gnu.tar.gz c0e97ac0424b1700088e52878ca599c5c241344646526755335590867725bd3e
https://github.com/eitsupi/prqlr/releases/download/lib-0.1.0/libprqlr-0.1.0-x86_64-unknown-linux-musl.tar.gz 6a25a5c7ea446dce24683fbc64845c1ffb51badff76b05e1b02bd28221c35bfb
88 changes: 88 additions & 0 deletions tools/prep-lib.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
check_sha256 <- function(file, sum, os = c("linux", "macos", "windows")) {
message("Checking SHA256 hash to <", sum, "> for <", file, ">...")

if (match.arg(os) == "linux") {
out <- system2("sha256sum", args = file, stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _)
} else if (match.arg(os) == "macos") {
out <- system2("shasum", args = c("-a", "256", file), stdout = TRUE) |>
gsub(r"(\s.*)", "", x = _)
} else if (match.arg(os) == "windows") {
out <- system2("certutil", args = c("-hashfile", file, "SHA256"), stdout = TRUE) |>
_[2]
eitsupi marked this conversation as resolved.
Show resolved Hide resolved
} else {
stop("Unsupported OS: ", os)
}

if (out != sum) {
stop("SHA256 mismatch for <", file, ">. Expected: <", sum, ">. Got: <", out, ">")
}

message("SHA256 matches for <", file, ">.")

invisible()
}

which_os <- function() {
if (identical(.Platform$OS.type, "windows")) {
"windows"
} else if (grepl("^darwin", R.version$os)) {
"macos"
} else if (identical(R.version$os, "linux-gnu")) {
"linux"
} else {
stop("Pre built binaries are not available for OS: ", R.version$os)
}
}

current_os <- which_os()

if (identical(current_os, "windows")) {
vendor_sys_abi <- "pc-windows-gnu"
} else if (identical(current_os, "macos")) {
vendor_sys_abi <- "apple-darwin"
} else if (identical(current_os, "linux")) {
vendor_sys_abi <- "unknown-linux-musl"
}

if ((R.version$arch %in% c("amd64", "x86_64"))) {
vendor_cpu_abi <- "x86_64"
} else if (R.version$arch %in% c("arm64", "aarch64")) {
vendor_cpu_abi <- "aarch64"
} else {
stop("Pre built binaries are not available for Arch: ", R.version$arch)
}

target_triple <- paste0(vendor_cpu_abi, "-", vendor_sys_abi)

lib_data <- utils::read.table("tools/lib-sums.tsv", header = TRUE, stringsAsFactors = FALSE)

package_name <- read.dcf("DESCRIPTION", fields = "Package", all = TRUE)
lib_version <- read.dcf("DESCRIPTION", fields = sprintf("Config/%s/LibVersion", package_name), all = TRUE)
lib_tag_prefix <- "lib-"

target_url <- sprintf(
"https://github.com/eitsupi/prqlr/releases/download/%s%s/libprqlr-%s-%s.tar.gz",
lib_tag_prefix,
lib_version,
lib_version,
target_triple
)

lib_sum <- lib_data |>
subset(url == target_url) |>
_$sha256sum
eitsupi marked this conversation as resolved.
Show resolved Hide resolved

if (!length(lib_sum)) stop("No pre built binary found at <", target_url, ">")

message("Found pre built binary at <", target_url, ">.\nDownloading...")

destfile <- tempfile(fileext = ".tar.gz")
on.exit(unlink(destfile))

utils::download.file(target_url, destfile, quiet = TRUE, mode = "wb")
check_sha256(destfile, lib_sum, os = current_os)

utils::untar(destfile, exdir = "tools")

message("Extracted pre built binary to <tools> directory.")