From c9f9782a895666dab451b22a6e9af0dfac61dbf7 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Mon, 18 Nov 2019 14:24:47 +0000 Subject: [PATCH] kutzooi --- Makefile | 6 - binary_c_python.c | 333 ---------------------------------------------- new_makefile.make | 65 +++++++++ setup.py | 3 +- 4 files changed, 67 insertions(+), 340 deletions(-) delete mode 100644 binary_c_python.c create mode 100644 new_makefile.make diff --git a/Makefile b/Makefile index 679099833..1a1a76efa 100644 --- a/Makefile +++ b/Makefile @@ -8,12 +8,6 @@ ifeq ($(BINARY_C),) $(error BINARY_C is not set) endif -SRC_DIR = src - - - - - CC := gcc LD := gcc PROGRAM := binary_c_python_api diff --git a/binary_c_python.c b/binary_c_python.c deleted file mode 100644 index 7a96ccc4d..000000000 --- a/binary_c_python.c +++ /dev/null @@ -1,333 +0,0 @@ -#include <Python.h> -#include "binary_c_python.h" - -/* - * binary_c/PYTHON API interface functions - * - * Remember: variables must be passed by references - * (i.e. as pointers). - * - * See apitest.py for an example of how to use these functions. - * - * See also - * http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html - */ - -/* list of variables used in the Py<>C interface */ - -/************************************************************/ -/* - * function prototypes : these are the functions - * called by PYTHON code, without the trailing underscore. - */ -/************************************************************/ - -// Docstrings -static char module_docstring[] MAYBE_UNUSED = - "This module is a python wrapper around binary_c"; -#ifdef __DEPRECATED -static char create_binary_docstring[] = - "Allocate memory for a binary"; -#endif -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[] = - "The prototype for a binary_c python function"; -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_logfile(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 - }, -#endif - {"run_binary", binary_c_run_binary, METH_VARARGS, run_binary_docstring}, - {"run_binary_with_logfile", binary_c_run_binary_with_logfile, 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}, - - {NULL, NULL, 0, NULL} -}; - -#if PY_MAJOR_VERSION >= 3 - -/* Python 3+ */ -static struct PyModuleDef Py_binary_c = -{ - PyModuleDef_HEAD_INIT, - "binary_c", /* name of module */ - "binary_c docs", /* module documentation, may be NULL */ - -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ - module_methods -}; - -PyMODINIT_FUNC PyInit_binary_c(void) -{ - return PyModule_Create(&Py_binary_c); -} - -#else - -/* - * Python pre-V3 - * - * NOT TESTED THOROUGHLY! - */ - -PyMODINIT_FUNC initbinary_c(void) -{ - PyObject *m = Py_InitModule3("binary_c", module_methods, module_docstring); - if(m == NULL) - return; -} -#endif // Python version check - - -#ifdef __DEPRECATED -static PyObject* binary_c_create_binary(PyObject *self, PyObject *args){ - - double var1, var2; - char * empty_str = ""; - int i; - const int N = 1; - - /* Parse the input tuple */ - if(!PyArg_ParseTuple(args, "dd", &var1, &var2)) - return NULL; - - - /* Binary structures */ - struct libbinary_c_stardata_t *stardata[N]; - struct libbinary_c_store_t *store = NULL; - - /* Allocate memory for binaries */ - for(i=0;i<N;i++){ - stardata[i] = NULL; - binary_c_new_system(&stardata[i], NULL, NULL, &store, &empty_str, -1); - } - - /* Return the evolved binary */ - PyObject *ret = Py_BuildValue(""); - - return ret; -} -#endif - - -static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args) -{ - /* Binary structures */ - struct libbinary_c_stardata_t *stardata; - - /* Allocate memory for binaries */ - char * empty_str = ""; - stardata = NULL; - binary_c_new_system(&stardata, NULL, NULL, &store, &empty_str, -1); - - /* Return an object containing the stardata */ - PyObject *ret = Py_BuildValue(""); - return ret; -} - -static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) -{ - - // This function is an very bare example of how a function would look like. - - double var1, var2; - - /* Parse the input tuple */ - if(!PyArg_ParseTuple(args, "dd", &var1, &var2)) - { - return NULL; - } - else - { - /* Return the evolved binary */ - PyObject *ret = Py_BuildValue(""); - return ret; - } -} - -static PyObject* binary_c_run_binary(PyObject *self, PyObject *args) -{ - /* Parse the input tuple */ - char *argstring; - - if(!PyArg_ParseTuple(args, "s", &argstring)) - { - return NULL; - } - else - { - char * buffer; - char * error_buffer; - size_t nbytes; - int out MAYBE_UNUSED = run_binary(argstring, - &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_custom_logging(PyObject *self, PyObject *args) -{ - /* Parse the input tuple */ - char *argstring; - long int func_memaddr; - - if(!PyArg_ParseTuple(args, "sl", &argstring, &func_memaddr)) - { - return NULL; - } - else - { - char * buffer; - char * error_buffer; - size_t nbytes; - int out MAYBE_UNUSED = run_binary_custom_logging(argstring, - func_memaddr, - &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_logfile(PyObject *self, PyObject *args) -{ - /* Parse the input tuple */ - char *argstring; - - if(!PyArg_ParseTuple(args, "s", &argstring)) - { - return NULL; - } - else - { - char * buffer; - char * error_buffer; - size_t nbytes; - int out MAYBE_UNUSED = run_binary_with_logfile(argstring, - &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_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; -} \ No newline at end of file diff --git a/new_makefile.make b/new_makefile.make new file mode 100644 index 000000000..c2b69a842 --- /dev/null +++ b/new_makefile.make @@ -0,0 +1,65 @@ +# Makefile for Rapid Binary Star Evolution program + +# you will need to set the BINARY_C variable to point +# to the root binary_c directory + +# https://stackoverflow.com/questions/30573481/path-include-and-src-directory-makefile/30602701#30602701 +ifeq ($(BINARY_C),) + $(error BINARY_C is not set) +endif + +SRC_DIR := src +OBJ_DIR := obj +INC_DIR := inc +BIN_DIR := bin +TARGET_LIB_DIR := lib + +SRC_FILES := $(wildcard $(SRC_DIR)/binary_c_python_api.c) +OBJ_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.o,$(SRC_FILES)) +DEP_FILES := $(patsubst $(SRC_DIR)/%.c,$(OBJ_DIR)/%.d,$(SRC_FILES)) + +# https://latedev.wordpress.com/2014/12/02/generic-makefiles-with-gcc-and-gnu-make-part-2 + +# SRC = $(wildcard $(SRC_DIR)/binary_c_python_api.c) +# OBJECTS = $(C_SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) + + +# + +CC := gcc +LD := gcc +MAKE := /usr/bin/make + +PROGRAM := binary_c_python_api + +LIBS := -lbinary_c $(shell $(BINARY_C)/binary_c-config --libs) + +# C_SRC := src/binary_c_python_api.c +C_SRC = $(wildcard $(SRC_DIR)/binary_c_python_api.c) +CFLAGS := -fPIC $(shell $(BINARY_C)/binary_c-config --flags | sed s/-fvisibility=hidden// ) -I$(BINARY_C)/src/ -I$(BINARY_C)/src/API -Iinclude + +OBJECTS := $(C_SRC:.c=.o) +OBJ_FLAGS := -c + +SO_FLAGS := -shared -o +SO_NAME := libbinary_c_api.so + +# To create python shared library +PY_EXEC := python3 +PY_SETUP := setup.py +PY_OPTIONS := build_ext --inplace + +# rules +.PHONY: all clean + +# all: $(PROGRAM) +all: $(OBJECTS) + $(CC) -DBINARY_C=$(BINARY_C) $(CFLAGS) $(INCDIRS) $(C_SRC) $(OBJ_FLAGS) $(INCDIRS) $(LIBS) + + # Make shared lib + $(CC) -DBINARY_C=$(BINARY_C) $(SO_FLAGS) $(SO_NAME) $(OBJECTS) + $(PY_EXEC) $(PY_SETUP) $(PY_OPTIONS) + +clean: + $(RM) *.o *.so + $(RM) build/ diff --git a/setup.py b/setup.py index 84868603a..ee511bdd9 100644 --- a/setup.py +++ b/setup.py @@ -62,11 +62,12 @@ setup( ext_modules=[ Extension( "binary_c", - ["binary_c_python.c"], + ["src/binary_c_python.c"], libraries=["binary_c"] + binary_c_libs + ["binary_c_api"], include_dirs=[ os.environ["BINARY_C"] + "/src", os.environ["BINARY_C"] + "/src/API", + 'include/', ] + binary_c_incdirs, library_dirs=[os.environ["BINARY_C"] + "/src", "./"] + binary_c_libdirs, -- GitLab