Skip to content

Commit

Permalink
Default scaffolding with savvy::savvy_init(). (#7)
Browse files Browse the repository at this point in the history
* Default scaffolding with `savvy::savvy_init()`.

* Update configure

Co-authored-by: Jackson Hoffart <[email protected]>

---------

Co-authored-by: Jackson Hoffart <[email protected]>
  • Loading branch information
jibarozzo and jdhoffa authored Dec 9, 2024
1 parent cc66a7a commit 35a0612
Show file tree
Hide file tree
Showing 20 changed files with 672 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@
^_pkgdown\.yml$
^docs$
^pkgdown$

^src/rust/.cargo$
^src/rust/target$
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ Config/testthat/edition: 3
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
SystemRequirements: Cargo (Rust's package manager), rustc
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
# Generated by roxygen2: do not edit by hand

S3method("$<-",savvy_multiverse__sealed)
S3method("[[<-",savvy_multiverse__sealed)
S3method(print,Person__bundle)
export(int_times_int)
export(to_upper)
useDynLib(multiverse, .registration = TRUE)
104 changes: 104 additions & 0 deletions R/000-wrappers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Generated by savvy: do not edit by hand
#
# Note:
# This wrapper file is named as `000-wrappers.R` so that this file is loaded
# first, which allows users to override the functions defined here (e.g., a
# print() method for an enum).

#' @useDynLib multiverse, .registration = TRUE
#' @keywords internal
NULL

# Check class and extract the external pointer embedded in the environment
.savvy_extract_ptr <- function(e, class) {
if(is.null(e)) {
return(NULL)
}

if(inherits(e, class)) {
e$.ptr
} else {
msg <- paste0("Expected ", class, ", got ", class(e)[1])
stop(msg, call. = FALSE)
}
}

# Prohibit modifying environments

#' @export
`$<-.savvy_multiverse__sealed` <- function(x, name, value) {
class <- gsub("__bundle$", "", class(x)[1])
stop(class, " cannot be modified", call. = FALSE)
}

#' @export
`[[<-.savvy_multiverse__sealed` <- function(x, i, value) {
class <- gsub("__bundle$", "", class(x)[1])
stop(class, " cannot be modified", call. = FALSE)
}

#' Convert Input To Upper-Case
#'
#' @param x A character vector.
#' @returns A character vector with upper case version of the input.
#' @export
`to_upper` <- function(`x`) {
.Call(savvy_to_upper__impl, `x`)
}

#' Multiply Input By Another Input
#'
#' @param x An integer vector.
#' @param y An integer to multiply.
#' @returns An integer vector with values multiplied by `y`.
#' @export
`int_times_int` <- function(`x`, `y`) {
.Call(savvy_int_times_int__impl, `x`, `y`)
}

### wrapper functions for Person

`Person_set_name` <- function(self) {
function(`name`) {
invisible(.Call(savvy_Person_set_name__impl, `self`, `name`))
}
}

`Person_name` <- function(self) {
function() {
.Call(savvy_Person_name__impl, `self`)
}
}

`.savvy_wrap_Person` <- function(ptr) {
e <- new.env(parent = emptyenv())
e$.ptr <- ptr
e$`set_name` <- `Person_set_name`(ptr)
e$`name` <- `Person_name`(ptr)

class(e) <- c("Person", "savvy_multiverse__sealed")
e
}



`Person` <- new.env(parent = emptyenv())

### associated functions for Person

`Person`$`new` <- function() {
.savvy_wrap_Person(.Call(savvy_Person_new__impl))
}

`Person`$`associated_function` <- function() {
.Call(savvy_Person_associated_function__impl)
}


class(`Person`) <- c("Person__bundle", "savvy_multiverse__sealed")

#' @export
`print.Person__bundle` <- function(x, ...) {
cat('Person')
}

1 change: 1 addition & 0 deletions cleanup
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars
1 change: 1 addition & 0 deletions cleanup.win
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rm -f src/Makevars.win
45 changes: 45 additions & 0 deletions configure
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Even when `cargo` is on `PATH`, `rustc` might not be in some cases. This adds
# ~/.cargo/bin to PATH to address such cases. Note that is not always available
# (e.g. or on Ubuntu with Rust installed via APT).
if [ -d "${HOME}/.cargo/bin" ]; then
export PATH="${PATH}:${HOME}/.cargo/bin"
fi

CARGO_VERSION="$(cargo --version)"

if [ $? -ne 0 ]; then
echo "-------------- ERROR: CONFIGURATION FAILED --------------------"
echo ""
echo "The cargo command is not available. To install Rust, please refer"
echo "to the official instruction:"
echo ""
echo "https://www.rust-lang.org/tools/install"
echo ""
echo "---------------------------------------------------------------"

exit 1
fi

# There's a little chance that rustc is not available on PATH while cargo is.
# So, just ignore the error case.
RUSTC_VERSION="$(rustc --version || true)"

# Report the version of Rustc to comply with the CRAN policy
echo "using Rust package manager: '${CARGO_VERSION}'"
echo "using Rust compiler: '${RUSTC_VERSION}'"

if [ "$(uname)" = "Emscripten" ]; then
TARGET="wasm32-unknown-emscripten"
fi

# catch DEBUG envvar, which is passed from pkgbuild::compile_dll()
if [ "${DEBUG}" = "true" ]; then
PROFILE=dev
else
PROFILE=release
fi

sed \
-e "s/@TARGET@/${TARGET}/" \
-e "s/@PROFILE@/${PROFILE}/" \
src/Makevars.in > src/Makevars
34 changes: 34 additions & 0 deletions configure.win
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
CARGO_VERSION="$(cargo --version)"

if [ $? -ne 0 ]; then
echo "-------------- ERROR: CONFIGURATION FAILED --------------------"
echo ""
echo "The cargo command is not available. To install Rust, please refer"
echo "to the official instruction:"
echo ""
echo "https://www.rust-lang.org/tools/install"
echo ""
echo "---------------------------------------------------------------"

exit 1
fi

# There's a little chance that rustc is not available on PATH while cargo is.
# So, just ignore the error case.
RUSTC_VERSION="$(rustc --version || true)"

# Report the version of Rustc to comply with the CRAN policy
echo "using Rust package manager: '${CARGO_VERSION}'"
echo "using Rust compiler: '${RUSTC_VERSION}'"

# catch DEBUG envvar, which is passed from pkgbuild::compile_dll()
if [ "${DEBUG}" = "true" ]; then
PROFILE=dev
else
PROFILE=release
fi

sed \
-e "s/@TARGET@/x86_64-pc-windows-gnu/" \
-e "s/@PROFILE@/${PROFILE}/" \
src/Makevars.win.in > src/Makevars.win
19 changes: 19 additions & 0 deletions man/int_times_int.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions man/to_upper.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
*.o
*.so
*.dll
target

Makevars
Makevars.win
39 changes: 39 additions & 0 deletions src/Makevars.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
TARGET = @TARGET@

PROFILE = @PROFILE@

# Add flags if necessary
RUSTFLAGS =

TARGET_DIR = $(CURDIR)/rust/target
LIBDIR = $(TARGET_DIR)/$(TARGET)/$(subst dev,debug,$(PROFILE))
STATLIB = $(LIBDIR)/libmultiverse.a
PKG_LIBS = -L$(LIBDIR) -lmultiverse

CARGO_BUILD_ARGS = --lib --profile $(PROFILE) --manifest-path=./rust/Cargo.toml --target-dir $(TARGET_DIR)

all: C_clean

$(SHLIB): $(STATLIB)

$(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.
export PATH="$(PATH):$(HOME)/.cargo/bin" && \
export CC="$(CC)" && \
export CFLAGS="$(CFLAGS)" && \
export RUSTFLAGS="$(RUSTFLAGS)" && \
if [ "$(TARGET)" != "wasm32-unknown-emscripten" ]; then \
cargo build $(CARGO_BUILD_ARGS); \
else \
export CARGO_PROFILE_DEV_PANIC="abort" && \
export CARGO_PROFILE_RELEASE_PANIC="abort" && \
cargo +nightly build $(CARGO_BUILD_ARGS) --target $(TARGET) -Zbuild-std=panic_abort,std; \
fi

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

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) rust/target
38 changes: 38 additions & 0 deletions src/Makevars.win.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
TARGET = @TARGET@

PROFILE = @PROFILE@

# Add flags if necessary
RUSTFLAGS =

TARGET_DIR = $(CURDIR)/rust/target
LIBDIR = $(TARGET_DIR)/$(TARGET)/$(subst dev,debug,$(PROFILE))
STATLIB = $(LIBDIR)/libmultiverse.a
PKG_LIBS = -L$(LIBDIR) -lmultiverse -lws2_32 -ladvapi32 -luserenv -lbcrypt -lntdll

# Rtools doesn't have the linker in the location that cargo expects, so we need
# to overwrite it via configuration.
CARGO_LINKER = x86_64-w64-mingw32.static.posix-gcc.exe

all: C_clean

$(SHLIB): $(STATLIB)

$(STATLIB):
# When the GNU toolchain is used (i.e. on CRAN), -lgcc_eh is specified for
# building proc-macro2, but Rtools doesn't contain libgcc_eh. This isn't used
# in actual, but we need this tweak to please the compiler.
mkdir -p $(LIBDIR)/libgcc_mock && touch $(LIBDIR)/libgcc_mock/libgcc_eh.a

export CARGO_TARGET_X86_64_PC_WINDOWS_GNU_LINKER="$(CARGO_LINKER)" && \
export LIBRARY_PATH="$${LIBRARY_PATH};$(LIBDIR)/libgcc_mock" && \
export CC="$(CC)" && \
export CFLAGS="$(CFLAGS)" && \
export RUSTFLAGS="$(RUSTFLAGS)" && \
cargo build --target $(TARGET) --lib --profile $(PROFILE) --manifest-path ./rust/Cargo.toml --target-dir $(TARGET_DIR)

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

clean:
rm -Rf $(SHLIB) $(STATLIB) $(OBJECTS) rust/target
Loading

0 comments on commit 35a0612

Please sign in to comment.