Skip to content

Commit

Permalink
Improve cross-compiling w/o native deps
Browse files Browse the repository at this point in the history
We now install dummy versions of the dependencies
when cross-compiling, so native dependencies are
not needed. This fixes compilation on PPM.

[ci skip]
  • Loading branch information
gaborcsardi committed Jan 11, 2024
1 parent af10882 commit 01501ed
Show file tree
Hide file tree
Showing 27 changed files with 150 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/dummy/cli/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: cli
Version: 100.0.0
Empty file added src/dummy/cli/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/curl/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: curl
Version: 100.0.0
Empty file added src/dummy/curl/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/desc/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: desc
Version: 100.0.0
Empty file added src/dummy/desc/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/filelock/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: filelock
Version: 100.0.0
Empty file added src/dummy/filelock/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/jsonlite/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: jsonlite
Version: 100.0.0
Empty file added src/dummy/jsonlite/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/lpSolve/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: lpSolve
Version: 100.0.0
Empty file added src/dummy/lpSolve/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/pkgbuild/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: pkgbuild
Version: 100.0.0
Empty file added src/dummy/pkgbuild/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/pkgcache/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: pkgcache
Version: 100.0.0
Empty file added src/dummy/pkgcache/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/ps/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: ps
Version: 100.0.0
Empty file added src/dummy/ps/NAMESPACE
Empty file.
2 changes: 2 additions & 0 deletions src/dummy/zip/DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Package: zip
Version: 100.0.0
Empty file added src/dummy/zip/NAMESPACE
Empty file.
76 changes: 75 additions & 1 deletion src/install-embedded.R
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,89 @@ install_order <- function() {
pkgs
}

install_dummies <- function(lib) {
pkgs <- dir("dummy")
dir.create(lib, showWarnings = FALSE, recursive = TRUE)
on.exit(Sys.unsetenv("PAK_INSTALL_DUMMY_CROSS"), add = TRUE)
Sys.setenv(PAK_INSTALL_DUMMY_CROSS = "true")
message("Dummy packages")
for (pkg in pkgs) {
install.packages(
paste0("dummy/", pkg),
lib = lib,
repos = NULL,
type = "source",
INSTALL_opts = opts()
)
if (!file.exists(file.path(lib, pkg))) {
stop("Failed to install dummy ", pkg)
}
}

# need real R6
message("Real R6")
install.packages(
"library/R6",
lib = lib,
repos = NULL,
type = "source",
INSTALL_opts = opts()
)
if (!file.exists(file.path(lib, "R6"))) {
stop("Failed to install dummy R6")
}

# real processx
message("Real-ish processx")
install.packages(
"library/processx",
lib = lib,
repos = NULL,
type = "source",
INSTALL_opts = c(opts(), "--no-libs")
)
if (!file.exists(file.path(lib, "processx"))) {
stop("Failed to install dummy processx")
}
nspath <- file.path(lib, "processx", "NAMESPACE")
ns <- readLines(nspath)
ns2 <- grep("useDynLib", ns, invert = TRUE, value = TRUE)
writeLines(ns2, nspath)
nsipath <- file.path(lib, "processx", "Meta", "nsInfo.rds")
nsi <- readRDS(nsipath)
nsi$dynlibs <- character()
saveRDS(nsi, nsipath)

# real callr
message("Real-ish callr")
install.packages(
"library/callr",
lib = lib,
repos = NULL,
type = "source",
INSTALL_opts = opts()
)
if (!file.exists(file.path(lib, "callr"))) {
stop("Failed to install dummy callr")
}
}

install_all <- function(lib = NULL) {
pkgs <- install_order()
if (Sys.getenv("CROSS_COMPILING") == "yes") {
# create dummy library
lib <- get_lib(lib)
dummy <- paste0(lib, "-dummy")
.libPaths(c(dummy, .libPaths()))
Sys.setenv(R_LIBS_USER = dummy)
print(Sys.getenv("R_LIBS_USER"))
install_dummies(lib = dummy)

# Update 'Built' field in package itself
update_description_build(
Sys.getenv("R_PACKAGE_DIR"),
Sys.getenv("R_TARGET_PLATFORM")
)
lib <- get_lib(lib)
for (pkg in pkgs) {
install_one(pkg, lib = paste0(lib, "-", pkg))
}
Expand Down
41 changes: 41 additions & 0 deletions src/library/processx.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
diff --git a/src/library/processx/R/client-lib.R b/src/library/processx/R/client-lib.R
index ab77dfa6..3eefd93a 100644
--- a/src/library/processx/R/client-lib.R
+++ b/src/library/processx/R/client-lib.R
@@ -1,6 +1,8 @@

client <- new.env(parent = emptyenv())

+if (Sys.getenv("PAK_INSTALL_DUMMY_CROSS") != "true") {
+
local({
ext <- .Platform$dynlib.ext
arch <- .Platform$r_arch
@@ -47,6 +49,8 @@ local({
}
})

+}
+
# This is really only here for testing

load_client_lib <- function(client) {
diff --git a/src/library/processx/R/on-load.R b/src/library/processx/R/on-load.R
index 6ade28dd..eca25dc3 100644
--- a/src/library/processx/R/on-load.R
+++ b/src/library/processx/R/on-load.R
@@ -1,6 +1,8 @@

## nocov start

+if (Sys.getenv("PAK_INSTALL_DUMMY_CROSS") != "true") {
+
.onLoad <- function(libname, pkgname) {
## This is needed to fix the boot time to a given value,
## because in a Docker container (maybe elsewhere as well?) on
@@ -26,4 +28,5 @@
supervisor_reset()
}

+}
## nocov end
4 changes: 4 additions & 0 deletions src/library/processx/R/client-lib.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

client <- new.env(parent = emptyenv())

if (Sys.getenv("PAK_INSTALL_DUMMY_CROSS") != "true") {

local({
ext <- .Platform$dynlib.ext
arch <- .Platform$r_arch
Expand Down Expand Up @@ -47,6 +49,8 @@ local({
}
})

}

# This is really only here for testing

load_client_lib <- function(client) {
Expand Down
3 changes: 3 additions & 0 deletions src/library/processx/R/on-load.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

## nocov start

if (Sys.getenv("PAK_INSTALL_DUMMY_CROSS") != "true") {

.onLoad <- function(libname, pkgname) {
## This is needed to fix the boot time to a given value,
## because in a Docker container (maybe elsewhere as well?) on
Expand All @@ -26,4 +28,5 @@
supervisor_reset()
}

}
## nocov end
4 changes: 3 additions & 1 deletion tools/build/linux/Dockerfile-aarch64
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ WORKDIR /root

# need to install the native package first --------------------------------

# TODO: aboid this, by using dummy packages.
# this is not needed for the cross-compilation, but it is easier to use
# the native package for deployment, as long as the deploy code is
# inside the pak package itself.

COPY pak_*.tar.gz /root/

Expand Down
2 changes: 2 additions & 0 deletions tools/build/macos/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export PKGFILES := $(patsubst %,lib/%/pak_$(PAKVERSION).tgz,$(RVERSIONS))

deploy:
@echo "----- DEPLOYING packages -----------------------------------"
# this is somewhat redundant, but needed when cross-compiling, to have
# a native package installed.
$(R) CMD INSTALL pak_$(PAKVERSION).tar.gz
R_LIBS=$(RLIB) $(R) -q -e 'pak::pkg_install("deps::$(PKGROOT)", dependencies = "Config/Needs/deploy")'
R_LIBS=$(RLIB) $(R) -q -e \
Expand Down
13 changes: 2 additions & 11 deletions tools/build/macos/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,9 @@ fi

echo "Cross compiling for R ${RVER} using R ${RVERX86}."

# Need to install all dependencies for the build arch first.
# TODO: look up the needed packages automatically
# TODO: should make sure the right versions are installed

R_MAKEVARS_USER="`pwd`/Makevars-macos-${RVERX86}" \
R_COMPILE_AND_INSTALL_PACKAGES=always \
"/usr/local/bin/R-${RVERX86}" -q \
-e 'install.packages(c("pkgdepends", "pkgsearch"))'

# TODO: we will need to hsve R-version specific platform triplets,
# TODO: we will need to have R-version specific platform triplets,
# when a version of R starts using a newer build than the current
# Big Sur (darwin 20).
# Big Sur (darwin 20). (Maybe?)

R_MAKEVARS_USER="`pwd`/Makevars-macos-${RVER}" \
"/usr/local/bin/R-${RVERX86}" CMD INSTALL \
Expand Down

0 comments on commit 01501ed

Please sign in to comment.