diff --git a/R/use_extendr.R b/R/use_extendr.R index 7c62d9c7..a878c49c 100644 --- a/R/use_extendr.R +++ b/R/use_extendr.R @@ -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) @@ -179,7 +179,7 @@ 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 @@ -187,27 +187,35 @@ use_extendr <- function(path = ".", # 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}}.") diff --git a/inst/templates/Makevars b/inst/templates/Makevars deleted file mode 100644 index eba6dd7f..00000000 --- a/inst/templates/Makevars +++ /dev/null @@ -1,33 +0,0 @@ -TARGET_DIR = ./rust/target -LIBDIR = $(TARGET_DIR)/release -STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a -PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}} - -# Print linked static libraries at compile time -export RUSTFLAGS += --print=native-static-libs - -all: C_clean - -$(SHLIB): $(STATLIB) - -CARGOTMP = $(CURDIR)/.cargo - -$(STATLIB): - # In some environments, ~/.cargo/bin might not be included in PATH, so we need - # to set it here to ensure cargo can be invoked. It is appended to PATH and - # therefore is only used if cargo is absent from the user's PATH. - if [ "$(NOT_CRAN)" != "true" ]; then \ - export CARGO_HOME=$(CARGOTMP); \ - fi && \ - export PATH="$(PATH):$(HOME)/.cargo/bin" && \ - cargo build --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR) - if [ "$(NOT_CRAN)" != "true" ]; then \ - rm -Rf $(CARGOTMP) && \ - rm -Rf $(LIBDIR)/build; \ - fi - -C_clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) - -clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(TARGET_DIR) diff --git a/inst/templates/Makevars.in b/inst/templates/Makevars.in new file mode 100644 index 00000000..19d72678 --- /dev/null +++ b/inst/templates/Makevars.in @@ -0,0 +1,50 @@ +TARGET_DIR = ./rust/target +LIBDIR = $(TARGET_DIR)/release +STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a +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 +# 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 + + 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) + + # 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 + +C_clean: + rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) + +clean: + rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(TARGET_DIR) $(VENDOR_DIR) diff --git a/inst/templates/Makevars.win b/inst/templates/Makevars.win.in similarity index 53% rename from inst/templates/Makevars.win rename to inst/templates/Makevars.win.in index 9bdff35c..ae47cd81 100644 --- a/inst/templates/Makevars.win +++ b/inst/templates/Makevars.win.in @@ -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) @@ -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 \ + 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 + C_clean: rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) diff --git a/inst/templates/cleanup b/inst/templates/cleanup new file mode 100644 index 00000000..ec35c3f8 --- /dev/null +++ b/inst/templates/cleanup @@ -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 \ No newline at end of file diff --git a/inst/templates/configure b/inst/templates/configure new file mode 100644 index 00000000..3aab1212 --- /dev/null +++ b/inst/templates/configure @@ -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 \ No newline at end of file diff --git a/inst/templates/configure.win b/inst/templates/configure.win new file mode 100644 index 00000000..40335385 --- /dev/null +++ b/inst/templates/configure.win @@ -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 \ No newline at end of file diff --git a/inst/templates/cran/Makevars b/inst/templates/cran/Makevars deleted file mode 100644 index 3858063a..00000000 --- a/inst/templates/cran/Makevars +++ /dev/null @@ -1,39 +0,0 @@ -TARGET_DIR = ./rust/target -LIBDIR = $(TARGET_DIR)/release -STATLIB = $(LIBDIR)/lib{{{lib_name}}}.a -PKG_LIBS = -L$(LIBDIR) -l{{{lib_name}}} - -# Print linked static libraries at compile time -export RUSTFLAGS += --print=native-static-libs - -all: C_clean - -$(SHLIB): $(STATLIB) - -CRAN_FLAGS=-j 2 --offline -CARGOTMP = $(CURDIR)/.cargo -VENDOR_DIR = $(CURDIR)/vendor - -$(STATLIB): - 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 - - # In some environments, ~/.cargo/bin might not be included in PATH, so we need - # to set it here to ensure cargo can be invoked. It is appended to PATH and - # therefore is only used if cargo is absent from the user's PATH. - if [ "$(NOT_CRAN)" != "true" ]; then \ - export CARGO_HOME=$(CARGOTMP); \ - fi && \ - export PATH="$(PATH):$(HOME)/.cargo/bin" && \ - cargo build $(CRAN_FLAGS) --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR) && \ - echo `cargo --version` && echo `rustc --version`; - rm -Rf $(CARGOTMP) $(VENDOR_DIR) $(LIBDIR)/build; \ - -C_clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(CARGOTMP) $(VENDOR_DIR) - -clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(CARGOTMP) $(VENDOR_DIR) $(TARGET_DIR) diff --git a/inst/templates/cran/Makevars.win b/inst/templates/cran/Makevars.win deleted file mode 100644 index 599ab819..00000000 --- a/inst/templates/cran/Makevars.win +++ /dev/null @@ -1,51 +0,0 @@ -TARGET = $(subst 64,x86_64,$(subst 32,i686,$(WIN)))-pc-windows-gnu - -TARGET_DIR = ./rust/target -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) - -CRAN_FLAGS=-j 2 --offline -CARGOTMP = $(CURDIR)/.cargo -VENDOR_DIR = $(CURDIR)/vendor - -$(STATLIB): - # uncompress vendored deps - 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 - - mkdir -p $(TARGET_DIR)/libgcc_mock - # `rustc` adds `-lgcc_eh` flags to the compiler, but Rtools' GCC doesn't have - # `libgcc_eh` due to the compilation settings. So, in order to please the - # compiler, we need to add empty `libgcc_eh` to the library search paths. - # For more details, please refer to - # 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 $(CRAN_FLAGS) --target=$(TARGET) --lib --release --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR) && \ - echo `cargo --version` && echo `rustc --version`; - if [ "$(NOT_CRAN)" != "true" ]; then \ - rm -Rf $(CARGOTMP) $(VENDOR_DIR) $(LIBDIR)/build; \ - fi - -C_clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(CARGOTMP) $(VENDOR_DIR) - -clean: - rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) $(CARGOTMP) $(VENDOR_DIR) $(TARGET_DIR) diff --git a/inst/templates/cran/configure b/inst/templates/cran/configure deleted file mode 100644 index 0f4c1be0..00000000 --- a/inst/templates/cran/configure +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env sh -: "${R_HOME=`R RHOME`}" -"${R_HOME}/bin/Rscript" tools/msrv.R diff --git a/inst/templates/cran/configure.win b/inst/templates/cran/configure.win deleted file mode 100644 index f1945ac1..00000000 --- a/inst/templates/cran/configure.win +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env sh -"${R_HOME}/bin${R_ARCH_BIN}/Rscript.exe" tools/msrv.R diff --git a/inst/templates/cran/msrv.R b/inst/templates/msrv.R similarity index 100% rename from inst/templates/cran/msrv.R rename to inst/templates/msrv.R