Revision: 75385
http://sourceforge.net/p/brlcad/code/75385
Author: starseeker
Date: 2020-04-14 03:19:00 +0000 (Tue, 14 Apr 2020)
Log Message:
-----------
checkpoint
Modified Paths:
--------------
brlcad/trunk/src/libbu/tests/CMakeLists.txt
Added Paths:
-----------
brlcad/trunk/src/libbu/tests/dylib/
brlcad/trunk/src/libbu/tests/dylib/CMakeLists.txt
brlcad/trunk/src/libbu/tests/dylib/README.txt
brlcad/trunk/src/libbu/tests/dylib/dylib.c
brlcad/trunk/src/libbu/tests/dylib/dylib.h
brlcad/trunk/src/libbu/tests/dylib/plugin_1.cpp
brlcad/trunk/src/libbu/tests/dylib/plugin_2.cpp
brlcad/trunk/src/libbu/tests/dylib/run.c
Modified: brlcad/trunk/src/libbu/tests/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbu/tests/CMakeLists.txt 2020-04-13 18:40:09 UTC (rev
75384)
+++ brlcad/trunk/src/libbu/tests/CMakeLists.txt 2020-04-14 03:19:00 UTC (rev
75385)
@@ -734,10 +734,23 @@
add_test(NAME bu_file_realpath_1 COMMAND bu_test realpath 1)
add_test(NAME bu_file_realpath_2 COMMAND bu_test realpath 2)
+#
+# *********** bu_dlopen/bu_dlsym test ************
+#
+#add_subdirectory(dylib)
+
CMAKEFILES(
tests_bitv.cmake
tests_vls.cmake
-)
+ dylib
+ dylib/README.txt
+ dylib/CMakeLists.txt
+ dylib/plugin_1.cpp
+ dylib/plugin_2.cpp
+ dylib/dylib.c
+ dylib/run.c
+ dylib/dylib.h
+ )
CMAKEFILES(CMakeLists.txt)
Added: brlcad/trunk/src/libbu/tests/dylib/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/CMakeLists.txt
(rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/CMakeLists.txt 2020-04-14 03:19:00 UTC
(rev 75385)
@@ -0,0 +1,24 @@
+
+set_property(SOURCE dylib.c APPEND PROPERTY COMPILE_DEFINITIONS
"DYLIB_PLUGIN_SUFFIX=\"${CMAKE_SHARED_LIBRARY_SUFFIX}\"")
+
+add_library(plugin_1 SHARED plugin_1.cpp)
+add_library(plugin_2 SHARED plugin_2.cpp)
+set_target_properties(plugin_1 plugin_2
+ PROPERTIES
+ ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${LIBEXEC_DIR}/dylib"
+ LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${LIBEXEC_DIR}/dylib"
+ RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/${LIBEXEC_DIR}/dylib"
+)
+
+add_library(libdylib SHARED dylib.c)
+if (CPP_DLL_DEFINES)
+ set_property(TARGET plugin_1 APPEND PROPERTY COMPILE_DEFINITIONS
BU_DYLIB_EXPORTS)
+ set_property(TARGET plugin_2 APPEND PROPERTY COMPILE_DEFINITIONS
BU_DYLIB_EXPORTS)
+ set_property(TARGET libdylib APPEND PROPERTY COMPILE_DEFINITIONS
BU_DYLIB_EXPORTS)
+endif (CPP_DLL_DEFINES)
+
+add_executable(bu_dylib run.c)
+target_link_libraries(bu_dylib libdylib libbu)
+if (CPP_DLL_DEFINES)
+ set_property(TARGET bu_dylib APPEND PROPERTY COMPILE_DEFINITIONS
BU_DYLIB_IMPORTS)
+endif (CPP_DLL_DEFINES)
Property changes on: brlcad/trunk/src/libbu/tests/dylib/CMakeLists.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/README.txt
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/README.txt
(rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/README.txt 2020-04-14 03:19:00 UTC
(rev 75385)
@@ -0,0 +1,7 @@
+A simple test to exercise the use of bu_dl* functions in the operation of a
+plugin style system. Two plugins are built along with a library to manage them
+and an executable to exercise the system.
+
+Due to the relatively high complexity of this setup, it is not integrated with
+the usual bu_test parent executable.
+
Property changes on: brlcad/trunk/src/libbu/tests/dylib/README.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/dylib.c
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/dylib.c (rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/dylib.c 2020-04-14 03:19:00 UTC (rev
75385)
@@ -0,0 +1,70 @@
+#include "common.h"
+#include <string.h>
+
+#include "bu/app.h"
+#include "bu/dylib.h"
+#include "bu/file.h"
+#include "bu/log.h"
+#include "bu/ptbl.h"
+#include "bu/vls.h"
+
+#include "dylib.h"
+
+int
+dylib_load_plugins()
+{
+ static struct bu_ptbl plugins = BU_PTBL_INIT_ZERO;
+ const char *ppath = bu_dir(NULL, 0, BU_DIR_LIBEXEC, "dylib", NULL);
+ char **filenames;
+ const char *psymbol = "dylib_plugin_info";
+ struct bu_vls plugin_pattern = BU_VLS_INIT_ZERO;
+ bu_vls_sprintf(&plugin_pattern, "*%s", DYLIB_PLUGIN_SUFFIX);
+ size_t nfiles = bu_file_list(ppath, bu_vls_cstr(&plugin_pattern),
&filenames);
+ for (size_t i = 0; i < nfiles; i++) {
+ char pfile[MAXPATHLEN] = {0};
+ bu_dir(pfile, MAXPATHLEN, BU_DIR_LIBEXEC, "dylib", filenames[i], NULL);
+ void *dl_handle, *info_val;
+ if (!(dl_handle = bu_dlopen(pfile, BU_RTLD_NOW))) {
+ const char * const error_msg = bu_dlerror();
+ if (error_msg)
+ bu_log("%s\n", error_msg);
+
+ bu_log("Unable to dynamically load '%s' (skipping)\n", pfile);
+ continue;
+ }
+ info_val = bu_dlsym(dl_handle, psymbol);
+ const struct dylib_plugin *(*plugin_info)() = (const struct
dylib_plugin *(*)())(intptr_t)info_val;
+ if (!plugin_info) {
+ const char * const error_msg = bu_dlerror();
+
+ if (error_msg)
+ bu_log("%s\n", error_msg);
+
+ bu_log("Unable to load symbols from '%s' (skipping)\n", pfile);
+ bu_log("Could not find '%s' symbol in plugin\n", psymbol);
+ continue;
+ }
+
+ const struct dylib_plugin *plugin = plugin_info();
+
+ if (!plugin || !plugin->i) {
+ bu_log("Invalid plugin encountered from '%s' (skipping)\n", pfile);
+ continue;
+ }
+
+ const struct dylib_contents *pcontents = plugin->i;
+ bu_ptbl_ins(&plugins, (long *)pcontents);
+ }
+
+ return (nfiles == BU_PTBL_LEN(&plugins));
+}
+
+/*
+ * Local Variables:
+ * tab-width: 8
+ * mode: C
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
Property changes on: brlcad/trunk/src/libbu/tests/dylib/dylib.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/dylib.h
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/dylib.h (rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/dylib.h 2020-04-14 03:19:00 UTC (rev
75385)
@@ -0,0 +1,12 @@
+struct dylib_contents {
+ const char *const name;
+ double version;
+ int (*calc)(char **result, int rmaxlen, int input);
+};
+
+struct dylib_plugin {
+ const struct dylib_contents * const i;
+};
+
+extern int dylib_load_plugins();
+
Property changes on: brlcad/trunk/src/libbu/tests/dylib/dylib.h
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/plugin_1.cpp
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/plugin_1.cpp
(rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/plugin_1.cpp 2020-04-14 03:19:00 UTC
(rev 75385)
@@ -0,0 +1,51 @@
+#include "common.h"
+
+#include <string>
+#include <string.h>
+
+#include "./dylib.h"
+
+extern "C" int
+calc(char **result, int rlen, int input)
+{
+ if (rlen <= 0 || !result) {
+ return -1;
+ }
+
+ int ret = 0;
+ int output = 2 * input;
+ std::string sout = std::to_string(output);
+
+ if (sout.length() > (size_t)(rlen - 1)) {
+ // We'll copy what we can, but we don't have enough room for
+ // everything.
+ ret = 1;
+ }
+
+ // Copy the result in to the provided buffer and null terminate
+ strncpy((*result), sout.c_str(), rlen - 1);
+ size_t npos = ((size_t)rlen < sout.length()) ? rlen - 1 : sout.length();
+ (*result)[npos] = '\0';
+
+ return ret;
+}
+
+static const struct dylib_contents pcontents = {"Plugin 1", 1.0, &calc};
+
+const struct dylib_plugin pinfo = { &pcontents };
+
+extern "C" const struct dylib_plugin *
+dylib_plugin_info()
+{
+ return &pinfo;
+}
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: brlcad/trunk/src/libbu/tests/dylib/plugin_1.cpp
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/plugin_2.cpp
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/plugin_2.cpp
(rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/plugin_2.cpp 2020-04-14 03:19:00 UTC
(rev 75385)
@@ -0,0 +1,51 @@
+#include "common.h"
+
+#include <string>
+#include <string.h>
+
+#include "./dylib.h"
+
+extern "C" int
+calc(char **result, int rlen, int input)
+{
+ if (rlen <= 0 || !result) {
+ return -1;
+ }
+
+ int ret = 0;
+ int output = 100 * input;
+ std::string sout = std::to_string(output);
+
+ if (sout.length() > (size_t)(rlen - 1)) {
+ // We'll copy what we can, but we don't have enough room for
+ // everything.
+ ret = 1;
+ }
+
+ // Copy the result in to the provided buffer and null terminate
+ strncpy((*result), sout.c_str(), rlen - 1);
+ size_t npos = ((size_t)rlen < sout.length()) ? rlen - 1 : sout.length();
+ (*result)[npos] = '\0';
+
+ return ret;
+}
+
+static const struct dylib_contents pcontents = {"Plugin 2", 2.3, &calc};
+
+const struct dylib_plugin pinfo = { &pcontents };
+
+extern "C" const struct dylib_plugin *
+dylib_plugin_info()
+{
+ return &pinfo;
+}
+
+
+// Local Variables:
+// tab-width: 8
+// mode: C++
+// c-basic-offset: 4
+// indent-tabs-mode: t
+// c-file-style: "stroustrup"
+// End:
+// ex: shiftwidth=4 tabstop=8
Property changes on: brlcad/trunk/src/libbu/tests/dylib/plugin_2.cpp
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Added: brlcad/trunk/src/libbu/tests/dylib/run.c
===================================================================
--- brlcad/trunk/src/libbu/tests/dylib/run.c (rev 0)
+++ brlcad/trunk/src/libbu/tests/dylib/run.c 2020-04-14 03:19:00 UTC (rev
75385)
@@ -0,0 +1,16 @@
+#include "common.h"
+#include "dylib.h"
+
+int main() {
+ return dylib_load_plugins();
+}
+
+/*
+ * Local Variables:
+ * tab-width: 8
+ * mode: C
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */
Property changes on: brlcad/trunk/src/libbu/tests/dylib/run.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the
world's largest Open Source development site.
_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits