Skip to content

Commit

Permalink
Commit the recent changes after finally linking to libr.so
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-carson committed Oct 7, 2024
1 parent 6fc6fc1 commit 5ece59d
Show file tree
Hide file tree
Showing 3 changed files with 248 additions and 177 deletions.
34 changes: 6 additions & 28 deletions cmake/r.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,7 @@ if(BUILD_WITH_R AND NOT N3DS AND NOT BAREMETALAPI)
set(TIC_BUILD_WITH_R TRUE)

if(NOT BUILD_STATIC)
if(NOT DEFINED ENV{R_HOME} AND DEFINED PREFIX)
set(R_HOME "${PREFIX}/lib64/R")
endif()

if(NOT DEFINED ENV{LD_LIBRARY_PATH} AND NOT LD_LIBRARY_PATH)
set(LD_LIBRARY_PATH "/usr/include/R:${R_HOME}/lib:${PREFIX}/lib/jvm/jre/lib/server")
else()
# Add these entries to the this *PATH variable
set(LD_LIBRARY_PATH "${LD_LIBRARY_PATH}:${PREFIX}/lib64/R/lib:${PREFIX}/lib/jvm/jre/lib/server")
endif()

if(NOT DEFINED ENV{R_SHARE_DIR} AND DEFINED ENV{PREFIX})
set(R_SHARE_DIR "${PREFIX}/share/R")
else()
set(R_SHARE_DIR ENV{R_SHARE_DIR})
endif()

if(NOT DEFINED ENV{R_DOC_DIR} AND DEFINED ENV{PREFIX})
set(R_DOC_DIR "${PREFIX}/share/doc/R")
else()
set(R_DOC_DIR ENV{R_DOC_DIR})
endif()

if(DEFINED ENV{PREFIX} OR DEFINED PREFIX)
set(R_SRC "${R_HOME}/lib")
list(APPEND R_SRC "${PREFIX}/lib/jvm/jre/lib/server" R_SHARE_DIR R_DOC_DIR)
endif()
include("cmake/renv.cmake")

set(R_LIB_DIR "/usr/include/R")
include_directories(SYSTEM "/usr/include/R")
Expand All @@ -53,7 +27,11 @@ if(BUILD_WITH_R AND NOT N3DS AND NOT BAREMETALAPI)
set_target_properties(r PROPERTIES PREFIX "")
endif()

target_link_libraries(r PRIVATE runtime)
target_link_libraries(
r
PRIVATE runtime
PUBLIC /usr/lib64/R/lib/libR.so
)

set_target_properties(r PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(r
Expand Down
61 changes: 40 additions & 21 deletions src/api/r.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,38 @@

#define R_NO_REMAP
#include <R.h>
#include <Rinternals.h>

#if !defined R_INTERNALS_H_
#include <Rinternals.h> /* defines LibExtern SEXP R_GlobalEnv */
#endif

#include <Rembedded.h>
#include <Rinterface.h>
#include <Rconfig.h>
#include <Rdefines.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void evalR(tic_mem *memory, const char *code) {
#if !defined R_INTERNALS_H_
#error "R_GlobalEnv not defined because Rinternals.h not properly included... somehow."
#endif
SEXP result = Rf_eval(Rf_mkString(code), R_GlobalEnv);
}

#define killer \
tic_core *core; \
if ((core = (((tic_core *) tic))->currentVM) != NULL) { \
Rf_endEmbeddedR(0); \
core->currentVM = NULL; \
}

tic_core *getTICCore(tic_mem* tic, const char* code);

static bool initR(tic_mem *tic, const char *code) {
killer;
tic_core *core;
if ((core = (((tic_core *) tic))->currentVM) != NULL) {
Rf_endEmbeddedR(0);
core->currentVM = NULL;
}

/* Without this nothing in R will work. */
Rf_mainloop();

fprintf(stderr, "%s\n", __LINE__);

Expand All @@ -67,24 +77,32 @@ static bool initR(tic_mem *tic, const char *code) {
}

static void closeR(tic_mem *tic) {
killer;
tic_core *core;
if ((core = (((tic_core *) tic))->currentVM) != NULL) {
Rf_endEmbeddedR(0);
core->currentVM = NULL;
}
}
static void callRFn_TIC80() {
/* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */
Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\
"`TIC-80`()"),
R_GlobalEnv);
static void callRFn_TIC80(tic_mem* tic) {
#if !defined R_INTERNALS_H_
#error "R_GlobalEnv not defined because Rinternals.h not properly included... somehow."
#endif
/* if (exists("TIC-80") && is.function(`TIC-80`)) `TIC-80`() */
Rf_eval(Rf_mkString("if (exists(\"TIC-80\") && is.function(`TIC-80`)) "\
"`TIC-80`()"),
R_GlobalEnv);
}
#define defineCallRFn_(x) \
static void callRFn_##x(tic_mem *tic) { \
#define defineCallRFn_(x, ...) \
static void callRFn_##x(tic_mem *tic, ##__VA_ARGS__) { \
Rf_eval(Rf_mkString("if (exists(\""#x"\") && is.function("#x")) "#x"()"), \
R_GlobalEnv); \
R_GlobalEnv); \
}
/* if (exists("x") && is.function(x)) x() */
defineCallRFn_(MENU)
defineCallRFn_(BDR)
defineCallRFn_(BOOT)
defineCallRFn_(SCN)
/* s32 row/index, void *data as well as the tic_mem *tic parameters. */
defineCallRFn_(MENU, s32 index, void *data)
defineCallRFn_(BDR, s32 row, void *data)
defineCallRFn_(SCN, s32 row, void *data)
#undef defineCallRFn_

bool initR(tic_mem *tic, const char *code);
Expand Down Expand Up @@ -212,6 +230,7 @@ TIC_EXPORT const tic_script EXPORT_SCRIPT(R) =
.projectComment = "#",
{
.init = initR,

.close = closeR,
.tick = callRFn_TIC80,
.boot = callRFn_BOOT,
Expand Down
Loading

0 comments on commit 5ece59d

Please sign in to comment.