From ec61b3907e95324b599da5c7de08ff0bfce2dba4 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 15 Jul 2018 13:40:01 +0100 Subject: [PATCH 1/4] - add sleep - fix suffix --- os/files.c | 10 ++++++-- os/sysbits.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/os/files.c b/os/files.c index b7e9e5ca5..8a7fb6c0c 100644 --- a/os/files.c +++ b/os/files.c @@ -47,7 +47,13 @@ const char *Yap_GetFileName(Term t USES_REGS) { char *buf = Malloc(YAP_FILENAME_MAX + 1); if (IsApplTerm(t) && FunctorOfTerm(t) == FunctorSlash) { snprintf(buf, YAP_FILENAME_MAX, "%s/%s", Yap_GetFileName(ArgOfTerm(1, t)), - Yap_GetFileName(ArgOfTerm(1, t))); + Yap_GetFileName(ArgOfTerm(2, t))); + } + if (IsAtomTerm(t)) { + return RepAtom(AtomOfTerm(t))->StrOfAE; + } + if (IsStringTerm(t)) { + return StringOfTerm(t); } return Yap_TextTermToText(t PASS_REGS); } @@ -90,7 +96,7 @@ static Int file_name_extension(USES_REGS1) { size_t len_b = strlen(f), lenb_b; char *candidate = strrchr(f, '.'); char *file = strrchr(f, '/'); - if (candidate && file && candidate > file) { + if (candidate && candidate > file) { lenb_b = candidate - f; ext = candidate + 1; } else { diff --git a/os/sysbits.c b/os/sysbits.c index bf515795d..5be1d4c84 100644 --- a/os/sysbits.c +++ b/os/sysbits.c @@ -1827,6 +1827,7 @@ static Int p_win_registry_get_value(USES_REGS1) { return FALSE; } + char *Yap_RegistryGetString(char *name) { DWORD type; BYTE data[MAXREGSTRLEN]; @@ -1865,6 +1866,74 @@ char *Yap_RegistryGetString(char *name) { #endif + + +static Int p_sleep(USES_REGS1) { + Term ts = ARG1; +#if defined(__MINGW32__) || _MSC_VER + { + unsigned long int secs = 0, usecs = 0, msecs, out; + if (IsIntegerTerm(ts)) { + secs = IntegerOfTerm(ts); + } else if (IsFloatTerm(ts)) { + double tfl = FloatOfTerm(ts); + if (tfl > 1.0) + secs = tfl; + else + usecs = tfl * 1000000; + } + msecs = secs * 1000 + usecs / 1000; + Sleep(msecs); + /* no ers possible */ + return true; + } +#elif HAVE_NANOSLEEP + { + struct timespec req; + int out; + + if (IsFloatTerm(ts)) { + double tfl = FloatOfTerm(ts); + + req.tv_nsec = (tfl - floor(tfl)) * 1000000000; + req.tv_sec = rint(tfl); + } else { + req.tv_nsec = 0; + req.tv_sec = IntOfTerm(ts); + } + out = nanosleep(&req, NULL); + return true; + } +#elif HAVE_USLEEP + { + useconds_t usecs; + if (IsFloatTerm(ts)) { + double tfl = FloatOfTerm(ts); + + usecs = rint(tfl * 1000000); + } else { + usecs = IntegrOfTerm(ts) * 1000000; + } + out = usleep(usecs); + return; + } +#elif HAVE_SLEEP + { + unsigned int secs, out; + if (IsFloatTerm(ts)) { + secs = rint(FloatOfTerm(ts)); + } else { + secs = IntOfTerm(ts); + } + out = sleep(secs); + return (Yap_unify(ARG2, MkIntTerm(out))); + } +#else + YAP_Error(SYSTEM_ERROR, 0L, "sleep not available in this configuration"); + return FALSE: +#endif +} + void Yap_InitSysPreds(void) { Yap_InitCPred("log_event", 1, p_log_event, SafePredFlag | SyncPredFlag); Yap_InitCPred("sh", 0, p_sh, SafePredFlag | SyncPredFlag); @@ -1901,5 +1970,6 @@ void Yap_InitSysPreds(void) { Yap_InitCPred("win_registry_get_value", 3, p_win_registry_get_value, 0); #endif Yap_InitCPred("rmdir", 2, p_rmdir, SyncPredFlag); + Yap_InitCPred("sleep", 1, p_sleep, SyncPredFlag); Yap_InitCPred("make_directory", 1, make_directory, SyncPredFlag); } From 2a1ec9c36b8064e686ec6eb7149d30c103baddd0 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 15 Jul 2018 13:41:22 +0100 Subject: [PATCH 2/4] remove sleep`qq --- library/system.yap | 13 --------- library/system/sys.c | 68 -------------------------------------------- 2 files changed, 81 deletions(-) diff --git a/library/system.yap b/library/system.yap index c5ac7631e..8f1640711 100644 --- a/library/system.yap +++ b/library/system.yap @@ -50,7 +50,6 @@ shell/0, shell/1, shell/2, - sleep/1, system/0, system/1, system/2, @@ -205,18 +204,6 @@ WIN32 environment YAP will use `COMSPEC` if `SHELL` is undefined, in this case with the option `" /c "`. -*/ -/** @pred sleep(+ _Time_) - - -Block the current thread for _Time_ seconds. When YAP is compiled -without multi-threading support, this predicate blocks the YAP process. -The number of seconds must be a positive number, and it may an integer -or a float. The Unix implementation uses `usleep` if the number of -seconds is below one, and `sleep` if it is over a second. The WIN32 -implementation uses `Sleep` for both cases. - - */ /** @pred system diff --git a/library/system/sys.c b/library/system/sys.c index da9290a29..0a8c89580 100644 --- a/library/system/sys.c +++ b/library/system/sys.c @@ -852,73 +852,6 @@ static YAP_Bool plwait(void) { #endif } -static YAP_Bool p_sleep(void) { - YAP_Term ts = YAP_ARG1; -#if defined(__MINGW32__) || _MSC_VER - { - unsigned long int secs = 0, usecs = 0, msecs, out; - if (YAP_IsIntTerm(ts)) { - secs = YAP_IntOfTerm(ts); - } else if (YAP_IsFloatTerm(ts)) { - double tfl = YAP_FloatOfTerm(ts); - if (tfl > 1.0) - secs = tfl; - else - usecs = tfl * 1000000; - } - msecs = secs * 1000 + usecs / 1000; - Sleep(msecs); - /* no errors possible */ - out = 0; - return (YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out))); - } -#elif HAVE_NANOSLEEP - { - struct timespec req; - int out; - - if (YAP_IsFloatTerm(ts)) { - double tfl = YAP_FloatOfTerm(ts); - - req.tv_nsec = (tfl - floor(tfl)) * 1000000000; - req.tv_sec = rint(tfl); - } else { - req.tv_nsec = 0; - req.tv_sec = YAP_IntOfTerm(ts); - } - out = nanosleep(&req, NULL); - return (YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out))); - } -#elif HAVE_USLEEP - { - useconds_t usecs; - if (YAP_IsFloatTerm(ts)) { - double tfl = YAP_FloatOfTerm(ts); - - usecs = rint(tfl * 1000000); - } else { - usecs = YAP_IntOfTerm(ts) * 1000000; - } - out = usleep(usecs); - return (YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out))); - } -#elif HAVE_SLEEP - { - unsigned int secs, out; - if (YAP_IsFloatTerm(ts)) { - secs = rint(YAP_FloatOfTerm(ts)); - } else { - secs = YAP_IntOfTerm(ts); - } - out = sleep(secs); - return (YAP_Unify(YAP_ARG2, YAP_MkIntTerm(out))); - } -#else - YAP_Error(0, 0L, "sleep not available in this configuration"); - return FALSE: -#endif -} - /* host info */ static YAP_Bool host_name(void) { @@ -1066,7 +999,6 @@ X_API void init_sys(void) { YAP_UserCPredicate("tmpnam", p_tmpnam, 2); YAP_UserCPredicate("tmpdir", p_tmpdir, 2); YAP_UserCPredicate("rename_file", rename_file, 3); - YAP_UserCPredicate("sleep", p_sleep, 2); YAP_UserCPredicate("read_link", read_link, 2); YAP_UserCPredicate("error_message", error_message, 2); YAP_UserCPredicate("win", win, 0); From 94621bf8f2c120518027d3503f07a0c43b498976 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 15 Jul 2018 13:43:29 +0100 Subject: [PATCH 3/4] real --- CMakeLists.txt | 21 +++++++++++---------- packages/real/CMakeLists.txt | 21 +++++++++++++-------- packages/real/real.pl | 10 ++++++++++ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2365fd4bd..88bc15e75 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -557,7 +557,8 @@ option(WITH_R "Use R Interface" ON) IF (WITH_R) - include_directories(packages/real ) + find_host_package(LibR) +add_subDIRECTORY(packages/real) ENDIF (WITH_R) @@ -776,7 +777,6 @@ if (WITH_GECODE) add_subDIRECTORY(packages/gecode) endif () -add_subDIRECTORY(packages/real) @@ -795,17 +795,16 @@ if (Java_Development_FOUND) set (STD_CMAKE_FIND_FRAMEWORK ${CMAKE_FIND_FRAMEWORK}) set (CMAKE_FIND_FRAMEWORK LAST) # CMake will find the Java returned by /usr/libexec/java_home. -find_package(JNI) + + macro_optional_find_package(JNI ON) + + set (CMAKE_FIND_FRAMEWORK ${STD_CMAKE_FIND_FRAMEWORK}) - if (NOT JNI_FOUND) - -set (JAVA_HOME ${JAVA_INCLUDE_PATH}/..) - -endif() - if (JNI_FOUND) +get_filename_component(JAVA_HOME ${JAVA_INCLUDE_PATH} DIRECTORY) + include(UseJava) # @@ -826,7 +825,8 @@ endif() # The Java_ADDITIONAL_VERSIONS variable can be used to specify a list # of version numbers that should be taken into account when searching # for Java. You need to set this variable before calling - # find_package(JavaLibs). + + # #macro_optional_find_package(JNI ON) # JNI_INCLUDE_DIRS = the include dirs to use @@ -838,6 +838,7 @@ endif() # JAVA_INCLUDE_PATH2 = the include path to jni_md.h # JAVA_AWT_INCLUDE_PATH = the include path to jawt.h + endif (JNI_FOUND) diff --git a/packages/real/CMakeLists.txt b/packages/real/CMakeLists.txt index a05d633dd..0a4b1ae32 100644 --- a/packages/real/CMakeLists.txt +++ b/packages/real/CMakeLists.txt @@ -3,31 +3,36 @@ +set(REAL_SOURCES real.c) + # LIBR_FOUND # LIBR_HOME -# LIBR_INCLUDE_DIRS +# LIBLIBR_INCLUDE_DIRSS # LIBR_DOC_DIR # LIBR_LIBRARIES -if (R_LIBRARIES AND R_INCLUDE_DIR) +if (LIBR_LIBRARIES AND LIBR_INCLUDE_DIRS) set_package_properties(R PROPERTIES DESCRIPTION "The R Project for Statistical Computing." URL "https://www.r-project.org/") add_lib(real ${REAL_SOURCES}) - target_link_libraries (real ${R_LIBRARIES} libYap) + target_link_libraries (real ${LIBR_LIBRARIES} libYap) include_directories ( - ${CMAKE_CURRENT_BINARY_DIR} - ${R_INCLUDE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/include + ${LIBR_INCLUDE_DIRS} ) list (APPEND CMAKE_REQUIRED_INCLUDES - ${CMAKE_CURRENT_BINARY_DIR} - ${R_INCLUDE_DIR} + ${CMAKE_CURRENT_BINARY_DIR} + ${CMAKE_SOURCE_DIR}/include + ${LIBR_INCLUDE_DIRS} ) check_include_files( "stdio.h;R.h" HAVE_R_H ) - check_include_files( "R.h,;Rembedded.h" HAVE_R_EMBEDDED_H ) + check_include_files( "R.h,;Rembeddred.h" HAVE_R_EMBEDDED_H ) check_include_files( "Rembedded.h;Rinterface.h" HAVE_R_INTERFACE_H ) configure_file ("rconfig.h.cmake" "rconfig.h" ) diff --git a/packages/real/real.pl b/packages/real/real.pl index 17eea60bf..328daca57 100755 --- a/packages/real/real.pl +++ b/packages/real/real.pl @@ -66,6 +66,7 @@ :- use_module(library(readutil)). :- use_module(library(debug)). :- use_module(library(system)). +:- use_module(library(readutil)). :- dynamic( real:r_started/1 ). @@ -111,6 +112,15 @@ init_r_env :- install_in_ms_windows(ToR). :- endif. +init_r_env :- + current_prolog_flag(unix, true), + open(popen('R RHOME'),read,S), + read_line_to_codes(S,Lc), + close(S), + Lc \= end_of_file, + !, + atom_codes(RH,Lc), + setenv('R_HOME',RH). init_r_env :- current_prolog_flag(unix, true), % typical Linux 64 bit setup (fedora) From 4b3aedccb558670d8f8ea76e8bcbdedf457cfd02 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 15 Jul 2018 13:44:55 +0100 Subject: [PATCH 4/4] misc --- C/flags.c | 2 +- C/load_foreign.c | 4 ++++ H/Foreign.h | 6 ++++++ packages/jpl/src/c/jpl.c | 2 +- packages/swig/yap.i | 4 ++-- pl/load_foreign.yap | 14 ++++++-------- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/C/flags.c b/C/flags.c index e762e41ce..eb1bd9ffe 100644 --- a/C/flags.c +++ b/C/flags.c @@ -1731,7 +1731,7 @@ void Yap_InitFlags(bool bootstrap) { Obtain the value for a YAP Prolog flag, same as current_prolog_flag/2. */ - Yap_InitCPredBack("prolog_flag", 3, 1, current_prolog_flag, cont_yap_flag, + Yap_InitCPredBack("prolog_flag", 3, 1, prolog_flag, cont_yap_flag, 0); Yap_InitCPredBack("yap_flag", 3, 1, yap_flag, cont_yap_flag, 0); Yap_InitCPredBack("prolog_flag", 2, 1, current_prolog_flag2, diff --git a/C/load_foreign.c b/C/load_foreign.c index ea4d8ee97..bc764a289 100644 --- a/C/load_foreign.c +++ b/C/load_foreign.c @@ -283,3 +283,7 @@ void Yap_ReOpenLoadForeign(void) { } CurrentModule = OldModule; } + +X_API bool load_none(void) +{ +} diff --git a/H/Foreign.h b/H/Foreign.h index 135ed4bbf..6778d06df 100644 --- a/H/Foreign.h +++ b/H/Foreign.h @@ -129,4 +129,10 @@ void Yap_ShutdownLoadForeign(void); #define EAGER_LOADING 1 #define GLOBAL_LOADING 2 +/** + * stub can always be called at DLL loading. + * + */ +X_API bool load_none(void); + #endif diff --git a/packages/jpl/src/c/jpl.c b/packages/jpl/src/c/jpl.c index 2a1bbc939..d7e0c6c21 100755 --- a/packages/jpl/src/c/jpl.c +++ b/packages/jpl/src/c/jpl.c @@ -640,7 +640,7 @@ static JNIEnv* jni_env(void) /* economically gets a JNIEnv pointer, valid for this thread */ { JNIEnv *env; - switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_2) ) + switch( (*jvm)->GetEnv(jvm, (void**)&env, JNI_VERSION_1_8) ) { case JNI_OK: return env; case JNI_EDETACHED: diff --git a/packages/swig/yap.i b/packages/swig/yap.i index fca51c2b4..7d38bc546 100644 --- a/packages/swig/yap.i +++ b/packages/swig/yap.i @@ -333,8 +333,8 @@ X_API extern Term YAP_MkCharPTerm( char *n); %template(TermVector) vector; %feature("novaluewrapper") vector; - //%template(YAPTermVector) vector; - //%feature("novaluewrapper") vector; + %template(YAPTermVector) vector; + %feature("novaluewrapper") vector; diff --git a/pl/load_foreign.yap b/pl/load_foreign.yap index 3ff848843..cacb8058c 100644 --- a/pl/load_foreign.yap +++ b/pl/load_foreign.yap @@ -39,11 +39,6 @@ */ -maplist_(_, [], []). -maplist_(Pred, [A1|L1], [A2|L2]) :- - call(Pred, A1, A2), - maplist_(Pred, L1, L2). - /** @pred load_foreign_files( _Files_, _Libs_, _InitRoutine_) should be used, from inside YAP, to load object files produced by the C @@ -74,8 +69,8 @@ load_foreign_files(Objs,Libs,Entry) :- access(read), expand(true), file_errors(fail)], NewObjs), - maplist_( '$load_lib', Libs, NewLibs), - '$load_foreign_files'(NewObjs,NewLibs,Entry), + '$load_libs'( Libs ), + '$load_foreign_files'(NewObjs,[],Entry), !, prolog_load_context(file, F), ignore( recordzifnot( '$load_foreign_done', [F, M], _) ). @@ -95,7 +90,10 @@ load_foreign_files(Objs,Libs,Entry) :- '$name_object'(I, P, O) :- absolute_file_name(I, O, P). -'$load_lib'(_,L,L). +'$load_libs'([]). +'$load_libs'([File|Files]) :- + open_shared_object(File, _Handle), + '$load_libs'(Files). /** @pred load_absolute_foreign_files( Files, Libs, InitRoutine)