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

Make rextendr packages CRAN compatible by default #394

Merged
merged 13 commits into from
Nov 17, 2024
Merged
32 changes: 20 additions & 12 deletions R/use_extendr.R
Original file line number Diff line number Diff line change
Expand Up @@ -101,16 +101,16 @@ use_extendr <- function(path = ".",
)

use_rextendr_template(
"Makevars",
save_as = file.path("src", "Makevars"),
"Makevars.in",
save_as = file.path("src", "Makevars.in"),
quiet = quiet,
overwrite = overwrite,
data = list(lib_name = lib_name)
)

use_rextendr_template(
"Makevars.win",
save_as = file.path("src", "Makevars.win"),
"Makevars.win.in",
save_as = file.path("src", "Makevars.win.in"),
quiet = quiet,
overwrite = overwrite,
data = list(lib_name = lib_name)
Expand Down Expand Up @@ -179,35 +179,43 @@ use_extendr <- function(path = ".",

# add msrv.R template
use_rextendr_template(
"cran/msrv.R",
"msrv.R",
save_as = file.path("tools", "msrv.R"),
quiet = quiet,
overwrite = overwrite
)

# add configure and configure.win templates
use_rextendr_template(
"cran/configure",
"configure",
save_as = "configure",
quiet = quiet,
overwrite = overwrite,
data = list(lib_name = lib_name)
)

# configure needs to be made executable
# ignore for Windows
if (.Platform[["OS.type"]] == "unix") {
Sys.chmod("configure", "0755")
}
use_rextendr_template(
"cleanup",
save_as = "cleanup",
quiet = quiet,
overwrite = overwrite,
data = list(lib_name = lib_name)
)

use_rextendr_template(
"cran/configure.win",
"configure.win",
save_as = "configure.win",
quiet = quiet,
overwrite = overwrite,
data = list(lib_name = lib_name)
)

# configure needs to be made executable
# ignore for Windows
if (.Platform[["OS.type"]] == "unix") {
Sys.chmod("configure", "0755")
Sys.chmod("cleanup", "0755")
}

if (!isTRUE(quiet)) {
cli::cli_alert_success("Finished configuring {.pkg extendr} for package {.pkg {pkg_name}}.")
Expand Down
33 changes: 0 additions & 33 deletions inst/templates/Makevars

This file was deleted.

50 changes: 50 additions & 0 deletions inst/templates/Makevars.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
TARGET_DIR = ./rust/target
LIBDIR = $(TARGET_DIR)/release
STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}}

all: C_clean

$(SHLIB): $(STATLIB)

CARGOTMP = $(CURDIR)/.cargo
VENDOR_DIR = $(CURDIR)/vendor


# RUSTFLAGS appends --print=native-static-libs to ensure that
# the correct linkers are used. Use this for debugging if need.
#
# CRAN note: Cargo and Rustc versions are reported during
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
# configure via tools/msrv.R.
#
# When on CRAN, the vendor.tar.xz file is unzipped and used
# for offline compilation. It is ignored when NOT_CRAN != false
$(STATLIB):

# Check if NOT_CRAN is false and unzip vendor.tar.xz if so
if [ "$(NOT_CRAN)" = "false" ]; then \
if [ -f ./rust/vendor.tar.xz ]; then \
tar xf rust/vendor.tar.xz && \
mkdir -p $(CARGOTMP) && \
cp rust/vendor-config.toml $(CARGOTMP)/config.toml; \
fi; \
fi
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved

export CARGO_HOME=$(CARGOTMP) && \
export PATH="$(PATH):$(HOME)/.cargo/bin" && \
RUSTFLAGS="$(RUSTFLAGS) --print=native-static-libs" cargo build @CRAN_FLAGS@ --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved

# Always clean up CARGOTMP
rm -Rf $(CARGOTMP)

if [ "$(NOT_CRAN)" != "false" ]; then \
rm -Rf $(VENDOR_DIR); \
rm -Rf $(TARGET_DIR); \
rm -Rf $(LIBDIR)/build; \
fi
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved

C_clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS)

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(TARGET_DIR) $(VENDOR_DIR)
35 changes: 22 additions & 13 deletions inst/templates/Makevars.win → inst/templates/Makevars.win.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ LIBDIR = $(TARGET_DIR)/$(TARGET)/release
STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a
PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}} -lws2_32 -ladvapi32 -luserenv -lbcrypt -lntdll

# Print linked static libraries at compile time
export RUSTFLAGS += --print=native-static-libs

all: C_clean

$(SHLIB): $(STATLIB)
Expand All @@ -24,18 +21,30 @@ $(STATLIB):
# https://github.com/r-windows/rtools-packages/blob/2407b23f1e0925bbb20a4162c963600105236318/mingw-w64-gcc/PKGBUILD#L313-L316
touch $(TARGET_DIR)/libgcc_mock/libgcc_eh.a

# CARGO_LINKER is provided in Makevars.ucrt for R >= 4.2
if [ "$(NOT_CRAN)" != "true" ]; then \
export CARGO_HOME=$(CARGOTMP); \
fi && \
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="$(CARGO_LINKER)" && \
export LIBRARY_PATH="$${LIBRARY_PATH};$(CURDIR)/$(TARGET_DIR)/libgcc_mock" && \
cargo build --target=$(TARGET) --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)
if [ "$(NOT_CRAN)" != "true" ]; then \
rm -Rf $(CARGOTMP) && \
rm -Rf $(LIBDIR)/build; \
# Handle NOT_CRAN case: If NOT_CRAN is false, vendor tarball should be handled
if [ "$(NOT_CRAN)" = "false" ]; then \
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved
if [ -f ./rust/vendor.tar.xz ]; then \
tar xf rust/vendor.tar.xz && \
mkdir -p $(CARGOTMP) && \
cp rust/vendor-config.toml $(CARGOTMP)/config.toml; \
fi; \
fi

# Build the project using Cargo with additional flags
export CARGO_HOME=$(CARGOTMP) && \
export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="$(CARGO_LINKER)" && \
export LIBRARY_PATH="$${LIBRARY_PATH};$(CURDIR)/$(TARGET_DIR)/libgcc_mock" && \
RUSTFLAGS="$(RUSTFLAGS) --print=native-static-libs" cargo @CRAN_FLAGS@ build --target=$(TARGET) --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)

# Always clean up CARGOTMP
rm -Rf $(CARGOTMP)

if [ "$(NOT_CRAN)" != "false" ]; then \
rm -Rf $(VENDOR_DIR); \
rm -Rf $(TARGET_DIR); \
rm -Rf $(LIBDIR)/build; \
fi
JosiahParry marked this conversation as resolved.
Show resolved Hide resolved

C_clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS)

Expand Down
4 changes: 4 additions & 0 deletions inst/templates/cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# removes the Makevars file generated by configure
# this allows the Makevars file to be generated each
# time the build is called
rm -rf src/Makevars
13 changes: 13 additions & 0 deletions inst/templates/configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env sh
: "${R_HOME=`R RHOME`}"
"${R_HOME}/bin/Rscript" tools/msrv.R

# Set CRAN_FLAGS based on the NOT_CRAN value
if [ "${NOT_CRAN}" = "false" ]; then
export CRAN_FLAGS="-j 2 --offline"
else
export CRAN_FLAGS=""
fi

# Substitute @CRAN_FLAGS@ in Makevars.in with the actual value of $CRAN_FLAGS
sed -e "s|@CRAN_FLAGS@|$CRAN_FLAGS|" src/Makevars.in > src/Makevars
12 changes: 12 additions & 0 deletions inst/templates/configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env sh
"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" tools/msrv.R

# Set CRAN_FLAGS based on the NOT_CRAN value
if [ "${NOT_CRAN}" = "false" ]; then
export CRAN_FLAGS="-j 2 --offline"
else
export CRAN_FLAGS=""
fi

# Substitute @CRAN_FLAGS@ in Makevars.in with the actual value of $CRAN_FLAGS
sed -e "s|@CRAN_FLAGS@|$CRAN_FLAGS|" src/Makevars.in > src/Makevars
39 changes: 0 additions & 39 deletions inst/templates/cran/Makevars

This file was deleted.

51 changes: 0 additions & 51 deletions inst/templates/cran/Makevars.win

This file was deleted.

3 changes: 0 additions & 3 deletions inst/templates/cran/configure

This file was deleted.

2 changes: 0 additions & 2 deletions inst/templates/cran/configure.win

This file was deleted.

File renamed without changes.
Loading