From 47376ac67c9f262e3972ae883315a0e92e6f11c1 Mon Sep 17 00:00:00 2001
From: David Hendriks <davidhendriks93@gmail.com>
Date: Fri, 8 Nov 2019 17:22:10 +0000
Subject: [PATCH] Alot of API visibility and binary_c_exit errors

---
 Makefile                                      |   2 +-
 TODO.org                                      |   4 +-
 binary_c_python.c                             | 118 ++++++++++-----
 binary_c_python.h                             |  12 +-
 binary_c_python_api.c                         | 135 ++++++++++++------
 .../custom_logging_functions.py               |   9 +-
 custom_logging.c                              |   5 +-
 testing.py                                    |   8 +-
 8 files changed, 205 insertions(+), 88 deletions(-)

diff --git a/Makefile b/Makefile
index f04d68ef2..a327be900 100644
--- a/Makefile
+++ b/Makefile
@@ -16,7 +16,7 @@ LIBS 	:= -lbinary_c $(shell $(BINARY_C)/binary_c-config --libs)
 C_SRC   := binary_c_python_api.c
 OBJECTS := $(C_SRC:.c=.o)
 OBJ_FLAGS := -c
-CFLAGS := -fPIC $(shell $(BINARY_C)/binary_c-config --flags) -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API 
+CFLAGS := -fPIC $(shell $(BINARY_C)/binary_c-config --flags | sed s/-fvisibility=hidden// ) -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API 
 SO_FLAGS := -shared -o
 SO_NAME := libbinary_c_api.so
 
diff --git a/TODO.org b/TODO.org
index 9fec89639..6f73f99db 100644
--- a/TODO.org
+++ b/TODO.org
@@ -141,11 +141,13 @@ That went very deep haha. alot of memory allocation stuff
     CLOSED: [2019-11-08 Fri 15:00]
 *** DONE make tag of old master branch for future reference
     CLOSED: [2019-11-08 Fri 15:00]
-*** TODO Implement the autogeneration of the library
+*** DONE Implement the autogeneration of the library
+    CLOSED: [2019-11-08 Fri 15:48]
 *** TODO Load all the things with the c-types
 *** TODO Implement new function for run_binary_with_custom_logging
 *** TODO Make new c function run_binary_with_custom_logging
 *** TODO Put in some new tests in the python test api
+
 ** General:
 *** DONE Get a more reliable way of loading the default values (running a ./tbse echo or something?)
     CLOSED: [2019-10-29 Tue 17:44]
diff --git a/binary_c_python.c b/binary_c_python.c
index a44f974b1..ff67ec101 100644
--- a/binary_c_python.c
+++ b/binary_c_python.c
@@ -22,7 +22,7 @@
  */
 /************************************************************/
 
-
+// Docstrings
 static char module_docstring[] MAYBE_UNUSED =
     "This module is a python wrapper around binary_c";
 #ifdef __DEPRECATED
@@ -33,6 +33,8 @@ static char run_binary_docstring[] =
     "Run one binary using binary_c";
 static char run_binary_with_logdocstring[] =
     "Run one binary using binary_c and allow the logfile to be written. Do not use for populations!";
+static char run_binary_custom_loggingdocstring[] =
+    "TODO";
 static char new_binary_system_docstring[] =
     "Return an object containing a binary, ready for evolution";
 static char function_prototype_docstring[] =
@@ -41,32 +43,38 @@ static char return_arglines_docstring[] =
     "Return the default args for a binary_c system";
 static struct libbinary_c_store_t *store = NULL;
 
+// Initialize pyobjects
 #ifdef __DEPRECATED
 static PyObject* binary_c_create_binary(PyObject *self, PyObject *args);
 #endif
 static PyObject* binary_c_run_binary(PyObject *self, PyObject *args);
 static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args);
+static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args);
 static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args);
 static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args);
 static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args);
 
-
-    
 /*
  * Python 3 interface is described at
  *
  * http://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html
  */
 
+
 static PyMethodDef module_methods[] = {
 #ifdef __DEPRECATED
-    {"create_binary", binary_c_create_binary, METH_VARARGS, create_binary_docstring},
+    {"create_binary", 
+        binary_c_create_binary, 
+        METH_VARARGS, 
+        create_binary_docstring
+    },
 #endif
     {"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring},
     {"run_binary_with_log", binary_c_run_binary_with_log, METH_VARARGS, run_binary_with_logdocstring},
+    // {"run_binary_custom_logging", binary_c_run_binary_custom_logging, METH_VARARGS, run_binary_custom_loggingdocstring},
     {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring},
     {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring},
-    {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring},
+    // {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring},
     
     {NULL, NULL, 0, NULL}
 };
@@ -74,7 +82,6 @@ static PyMethodDef module_methods[] = {
 #if PY_MAJOR_VERSION >= 3
 
 /* Python 3+ */
-
 static struct PyModuleDef Py_binary_c =
 {
     PyModuleDef_HEAD_INIT,
@@ -215,6 +222,48 @@ static PyObject* binary_c_run_binary(PyObject *self, PyObject *args)
     }
 }
 
+// static PyObject* binary_c_run_binary_custom_logging(PyObject *self, PyObject *args)
+// {
+//     /* Parse the input tuple */
+//     char *argstring;
+//     long int str_1;
+
+//     if(!PyArg_ParseTuple(args, "sl", &argstring, &str_1))
+//     {
+//         return NULL;
+//     }
+//     else
+//     {
+//         char * buffer;
+//         char * error_buffer;
+//         size_t nbytes;
+//         int out MAYBE_UNUSED = run_binary_custom_logging(argstring,
+//                                             str_1, 
+//                                           &buffer,
+//                                           &error_buffer,
+//                                           &nbytes);
+//         /* copy the buffer to a python string */
+//         PyObject * return_string = Py_BuildValue("s", buffer);
+//         PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);
+
+//         if(error_buffer != NULL && strlen(error_buffer)>0)
+//         {
+//             fprintf(stderr,
+//                     "Error in binary_c run : %s\n",
+//                     error_buffer);
+//         }
+        
+//         Safe_free(buffer);
+//         Safe_free(error_buffer);
+
+//         /* 
+//          * TODO
+//          * return the return_error_string as well!
+//          */
+//         return return_string;
+//     }
+// }
+
 static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args)
 {
     /* Parse the input tuple */
@@ -256,33 +305,32 @@ static PyObject* binary_c_run_binary_with_log(PyObject *self, PyObject *args)
     }
 }
 
-static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
-{
-    char * buffer;
-    char * error_buffer;
-    size_t nbytes;
-    int out MAYBE_UNUSED = return_arglines(&buffer,
-                                          &error_buffer,
-                                          &nbytes);
-
-    /* copy the buffer to a python string */
-    PyObject * return_string = Py_BuildValue("s", buffer);
-    PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);
-
-    if(error_buffer != NULL && strlen(error_buffer)>0)
-    {
-        fprintf(stderr,
-                "Error in binary_c run : %s\n",
-                error_buffer);
-    }
+// static PyObject* binary_c_return_arglines(PyObject *self, PyObject *args)
+// {
+//     char * buffer;
+//     char * error_buffer;
+//     size_t nbytes;
+//     int out MAYBE_UNUSED = return_arglines(&buffer,
+//                                           &error_buffer,
+//                                           &nbytes);
+
+//     /* copy the buffer to a python string */
+//     PyObject * return_string = Py_BuildValue("s", buffer);
+//     PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer);
+
+//     if(error_buffer != NULL && strlen(error_buffer)>0)
+//     {
+//         fprintf(stderr,
+//                 "Error in binary_c run : %s\n",
+//                 error_buffer);
+//     }
     
-    Safe_free(buffer);
-    Safe_free(error_buffer);
-
-    /* 
-     * TODO
-     * return the return_error_string as well!
-     */
-    return return_string;
-}
-
+//     Safe_free(buffer);
+//     Safe_free(error_buffer);
+
+//     /* 
+//      * TODO
+//      * return the return_error_string as well!
+//      */
+//     return return_string;
+// }
\ No newline at end of file
diff --git a/binary_c_python.h b/binary_c_python.h
index 04a1a1257..7643dcfcc 100644
--- a/binary_c_python.h
+++ b/binary_c_python.h
@@ -19,9 +19,15 @@ int run_binary_with_log (char * argstring,
                 char ** const errorstring,
                 size_t * const nbytes);
 
-int return_arglines(char ** const outstring,
-                char ** const errorstring,
-                size_t * const nbytes);
+// int run_binary_custom_logging(char * argstring,
+//                long int str_1,
+//                char ** const buffer,
+//                char ** const error_buffer,
+//                size_t * const nbytes);
+
+// int return_arglines(char ** const outstring,
+//                 char ** const errorstring,  
+//                 size_t * const nbytes);
 
 /* C macros */
 #define BINARY_C_APITEST_VERSION 0.1
diff --git a/binary_c_python_api.c b/binary_c_python_api.c
index d160ef857..00ddc349d 100644
--- a/binary_c_python_api.c
+++ b/binary_c_python_api.c
@@ -128,56 +128,109 @@ int run_binary(char * argstring,
     return 0;
 }
 
-int return_arglines(char ** const buffer,
-               char ** const error_buffer,
-               size_t * const nbytes)
-{
-    /* memory for N binary systems */
-    struct libbinary_c_stardata_t *stardata;
-    struct libbinary_c_store_t * store = NULL;
+// int run_binary_custom_logging(char * argstring,
+//                long int str_1,
+//                char ** const buffer,
+//                char ** const error_buffer,
+//                size_t * const nbytes)
+// {
+//     /* memory for N binary systems */
+//     struct libbinary_c_store_t * store = NULL;
 
-    /* make new stardata */
-    stardata = NULL;
-    char * empty_str = "";
-    binary_c_new_system(&stardata,
-                        NULL,
-                        NULL,
-                        &store,
-                        &empty_str,
-                        -1);
+//     /* make new stardata */
+//     stardata = NULL;
+//     binary_c_new_system(&stardata,
+//                         NULL,
+//                         NULL,
+//                         &store,
+//                         &argstring,
+//                         -1);
+    
+//     /* disable logging */
+//     snprintf(stardata->preferences->log_filename,
+//              STRING_LENGTH-1,
+//              "%s",
+//              "/dev/null");
+//     snprintf(stardata->preferences->api_log_filename_prefix,
+//              STRING_LENGTH-1,
+//              "%s",
+//              "/dev/null");
+//     /* output to strings */
+//     stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
+//     stardata->preferences->batchmode = BATCHMODE_LIBRARY;
+//     stardata->preferences->custom_output_function = str_1;
 
-    /* disable logging */
-    snprintf(stardata->preferences->log_filename,
-             STRING_LENGTH-1,
-             "%s",
-             "/dev/null");
-    snprintf(stardata->preferences->api_log_filename_prefix,
-             STRING_LENGTH-1,
-             "%s",
-             "/dev/null");
+//     /* do binary evolution */
+//     binary_c_evolve_for_dt(stardata,
+//                            stardata->model.max_evolution_time);
+        
+//     /* get buffer pointer */
+//     binary_c_buffer_info(stardata,buffer,nbytes);
+    
+//     /* get error buffer pointer */
+//     binary_c_error_buffer(stardata,error_buffer);
+    
+//     /* set raw_buffer_size = -1 to prevent it being freed */
+//     stardata->tmpstore->raw_buffer_size = -1;
+    
+//     /* free stardata (except the buffer) */
+//     binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
+//     binary_c_free_store_contents(store);
+        
+//     return 0;
+// }
 
-    /* output to strings */
-    stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
-    stardata->preferences->batchmode = BATCHMODE_LIBRARY;
 
-    /* List available arguments */
-    binary_c_list_args(stardata);
+// int return_arglines(char ** const buffer,
+//                char ** const error_buffer,
+//                size_t * const nbytes)
+// {
+//     /* memory for N binary systems */
+//     struct libbinary_c_stardata_t *stardata;
+//     struct libbinary_c_store_t * store = NULL;
 
-    /* get buffer pointer */
-    binary_c_buffer_info(stardata,buffer,nbytes);
+//     /* make new stardata */
+//     stardata = NULL;
+//     char * empty_str = "";
+//     binary_c_new_system(&stardata,
+//                         NULL,
+//                         NULL,
+//                         &store,
+//                         &empty_str,
+//                         -1);
+
+//     /* disable logging */
+//     snprintf(stardata->preferences->log_filename,
+//              STRING_LENGTH-1,
+//              "%s",
+//              "/dev/null");
+//     snprintf(stardata->preferences->api_log_filename_prefix,
+//              STRING_LENGTH-1,
+//              "%s",
+//              "/dev/null");
+
+//     /* output to strings */
+//     stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE;
+//     stardata->preferences->batchmode = BATCHMODE_LIBRARY;
+
+//     /* List available arguments */
+//     binary_c_list_args(stardata);
+
+//     /* get buffer pointer */
+//     binary_c_buffer_info(stardata,buffer,nbytes);
     
-    /* get error buffer pointer */
-    binary_c_error_buffer(stardata,error_buffer);
+//     /* get error buffer pointer */
+//     binary_c_error_buffer(stardata,error_buffer);
     
-    /* set raw_buffer_size = -1 to prevent it being freed */
-    stardata->tmpstore->raw_buffer_size = -1;
+//     /* set raw_buffer_size = -1 to prevent it being freed */
+//     stardata->tmpstore->raw_buffer_size = -1;
     
-    /* free stardata (except the buffer) */
-    binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
-    binary_c_free_store_contents(store);
+//     /* free stardata (except the buffer) */
+//     binary_c_free_memory(&stardata,TRUE,TRUE,FALSE,FALSE);
+//     binary_c_free_store_contents(store);
 
-    return 0;
-}
+//     return 0;
+// }
 
 int run_binary_with_log(char * argstring,
                char ** const buffer,
diff --git a/binaryc_python_utils/custom_logging_functions.py b/binaryc_python_utils/custom_logging_functions.py
index 49919e6b9..bfd3c2f98 100644
--- a/binaryc_python_utils/custom_logging_functions.py
+++ b/binaryc_python_utils/custom_logging_functions.py
@@ -65,8 +65,9 @@ def binary_c_log_code(code):
 #undef MIN
 #include \"binary_c.h\"
 
-void custom_output_function(struct stardata_t * stardata);
-void custom_output_function(struct stardata_t * stardata)
+// add visibility __attribute__ ((visibility ("default"))) to it 
+void binary_c_API_function custom_output_function(struct stardata_t * stardata);
+void binary_c_API_function custom_output_function(struct stardata_t * stardata)
 {{
     // struct stardata_t * stardata = (struct stardata_t *)x;
     {};
@@ -175,6 +176,10 @@ def return_compilation_dict():
     # you must define _SEARCH_H to prevent it being loaded twice
     ccflags += ' -shared -D_SEARCH_H'
 
+    # remove the visibility=hidden for this compilation
+    ccflags = ccflags.replace('-fvisibility=hidden', '')
+
+
     # ensure library paths to the front of the libs:
     libs_content = libs.split(' ')
     library_paths = [el for el in libs_content if el.startswith('-L')]
diff --git a/custom_logging.c b/custom_logging.c
index ef5b587fb..57b6574fa 100644
--- a/custom_logging.c
+++ b/custom_logging.c
@@ -4,8 +4,9 @@
 #undef MIN
 #include "binary_c.h"
 
-void custom_output_function(struct stardata_t * stardata);
-void custom_output_function(struct stardata_t * stardata)
+// add visibility __attribute__ ((visibility ("default"))) to it 
+void binary_c_API_function custom_output_function(struct stardata_t * stardata);
+void binary_c_API_function custom_output_function(struct stardata_t * stardata)
 {
     // struct stardata_t * stardata = (struct stardata_t *)x;
     Printf("MY_STELLAR_DATA %g %g\n",((double)stardata->model.time),((double)stardata->star[0].mass));
diff --git a/testing.py b/testing.py
index 688e77020..f0f5a52e1 100644
--- a/testing.py
+++ b/testing.py
@@ -1,7 +1,7 @@
 import ctypes
 
 from binaryc_python_utils.custom_logging_functions import autogen_C_logging_code, binary_c_log_code, compile_shared_lib
-
+# import binary_c
 
 # generate logging lines
 logging_line = autogen_C_logging_code(
@@ -18,7 +18,9 @@ created_code = binary_c_log_code(logging_line)
 compile_shared_lib(created_code, sourcefile_name='custom_logging.c', outfile_name='libcustom_logging.so')
 
 # Loading library
-libmean = ctypes.CDLL("libcustom_logging.so") # loads the shared library
+libmean = ctypes.CDLL("libcustom_logging.so", mode=1) # loads the shared library
 
 # Get memory adress of function. mimicking a pointer
-mem = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value
\ No newline at end of file
+mem = ctypes.cast(libmean.custom_output_function, ctypes.c_void_p).value
+
+
-- 
GitLab