From b62dcce61ea16364909f0d600cca6e37a246f58c Mon Sep 17 00:00:00 2001
From: "alexey.stratulat" <alexey.stratulat@denlabs.net>
Date: Fri, 4 Oct 2019 13:35:22 +0700
Subject: [PATCH] [*] Added the ability to build and install python-cellframe
 using scikit-build.

---
 CMakeLists.txt             | 41 +++++++++++++++++---
 include/python-cellframe.h |  5 +--
 setup.py                   | 22 +++++++++++
 src/__init__.py            |  1 +
 src/python-cellframe.c     | 78 +++++++++++++++++++-------------------
 test/main_test.py          |  2 +-
 6 files changed, 100 insertions(+), 49 deletions(-)
 create mode 100644 setup.py
 create mode 100644 src/__init__.py

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 9b772458..173b4819 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,21 +18,51 @@ file(GLOB PYTHON_CELLFRAME_HEADERS include/*.h)
 
 set(Python_ADDITIONAL_VERSIONS 3.7)
 find_package (PythonLibs REQUIRED)
-#find_package(PkgConfig)
-#pkg_check_modules(PC_JSON-C REQUIRED json-c)
+
 include_directories(${PYTHON_INCLUDE_DIR} include/)
 
-add_library(${PROJECT_NAME} SHARED ${PYTHON_CELLFRAME_SRCS} ${PYTHON_CELLFRAME_HEADERS})
+if (SKBUILD)
+    message(STATUS "The project is built using scikit-build")
+    find_package(PythonExtensions REQUIRED)
+    add_library(${PROJECT_NAME} MODULE ${PYTHON_CELLFRAME_SRCS} ${PYTHON_CELLFRAME_HEADERS})
+    python_extension_module(${PROJECT_NAME})
+    python_extension_module(${PROJECT_NAME} LINKED_MODULES_VAR dap_python_module)
+    python_extension_module(${PROJECT_NAME} LINKED_MODULES_VAR dap_crypto_python_module)
+    python_extension_module(${PROJECT_NAME} LINKED_MODULES_VAR DapServerCore)
+    install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION CellFrame)
+    install(FILES src/__init__.py DESTINATION CellFrame)
+#    install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libdap-python/libdap_python_module.so DESTINATION CellFrame/lib)
+#    install(TARGETS dap_python_module LIBRARY DESTINATION CellFrame)
+#    install(TARGETS dap_crypto_python_module LIBRARY DESTINATION CellFrame)
+#    install(TARGETS DapServerCore LIBRARY DESTINATION CellFrame)
+else()
+    add_library(${PROJECT_NAME} SHARED ${PYTHON_CELLFRAME_SRCS} ${PYTHON_CELLFRAME_HEADERS})
+endif()
 
-target_link_libraries(${PROJECT_NAME})
+    target_link_libraries(${PROJECT_NAME})
 
-target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})
+    target_link_libraries(${PROJECT_NAME} ${PYTHON_LIBRARIES})
 
 #target_compile_options(
 #    dap_core PRIVATE 
 #    "-fpic"
 #)
 
+#target_compile_options(
+#    dap_python_module PRIVATE 
+#    "-fpic"
+#)
+
+#target_compile_options(
+#    dap_crypto_python_module PRIVATE 
+#    "-fpic"
+#)
+
+#target_compile_options(
+#    DapServerCore PRIVATE 
+#    "-fpic"
+#)
+
 target_link_libraries(${PROJECT_NAME} dap_python_module 
                       dap_crypto_python_module 
                       DapServerCore
@@ -54,4 +84,3 @@ target_include_directories(${PROJECT_NAME} INTERFACE include/)
 #    enable_testing()
     #add_subdirectory(test)
 #endif()
-
diff --git a/include/python-cellframe.h b/include/python-cellframe.h
index cd32e72c..f974ce95 100644
--- a/include/python-cellframe.h
+++ b/include/python-cellframe.h
@@ -48,15 +48,14 @@ static PyMethodDef CellFramePythonMethods[] = {
 
 static struct PyModuleDef CellFramePythonModule = {
         PyModuleDef_HEAD_INIT,
-        "libCellFrame",   /* name of module */
+        "CellFrame",   /* name of module */
         NULL, /* module documentation, may be NULL */
         -1,       /* size of per-interpreter state of the module,
                  or -1 if the module keeps state in global variables. */
         CellFramePythonMethods
 };
 
-PyMODINIT_FUNC PyInit_libCellFrame(void);
-
+PyMODINIT_FUNC PyInit_CellFrame(void);
 
 #ifdef __cplusplus
 }
diff --git a/setup.py b/setup.py
new file mode 100644
index 00000000..108c2e6a
--- /dev/null
+++ b/setup.py
@@ -0,0 +1,22 @@
+import sys
+
+from skbuild import setup
+
+# Require pytest-runner only when running tests
+#pytest_runner = (['pytest-runner>=2.0,<3dev']
+#                 if any(arg in sys.argv for arg in ('pytest', 'test'))
+#                 else [])
+
+#setup_requires = pytest_runner
+
+setup(
+    name="CellFrame",
+    version="0.5.0",
+    description="SDK CellFrame network",
+    author='DEMLABS Inc. (2017-2019)',
+    license="GNU GPL",
+    packages=['CellFrame'],
+#    tests_require=['pytest'],
+#    setup_requires=setup_requires
+)
+
diff --git a/src/__init__.py b/src/__init__.py
new file mode 100644
index 00000000..4e6e61f6
--- /dev/null
+++ b/src/__init__.py
@@ -0,0 +1 @@
+from .CellFrame import *
diff --git a/src/python-cellframe.c b/src/python-cellframe.c
index 492c8501..54a5d0fc 100644
--- a/src/python-cellframe.c
+++ b/src/python-cellframe.c
@@ -1,43 +1,6 @@
 #include "python-cellframe.h"
 
 
-PyMODINIT_FUNC PyInit_libCellFrame(void){
-
-    if (PyType_Ready(&DapObject_DapObjectType) < 0 || PyType_Ready(&dapCrypto_dapCryptoType) < 0 ||
-            PyType_Ready(&ServerCore_ServerCoreType) < 0 || PyType_Ready(&dapEvents_dapEventsType) < 0 ||
-            PyType_Ready(&dapEventsSocket_dapEventsSocketType) < 0 ||
-            PyType_Ready(&CryptoKeyTypeObjecy_CryptoKeyTypeObjecyType) < 0 ||
-            PyType_Ready(&CryptoDataTypeObjecy_CryptoDataTypeObjecyType) < 0)
-               return NULL;
-
-    PyObject *module = PyModule_Create(&CellFramePythonModule);
-
-    CellFrame_error = PyErr_NewException("libCellFrame.error", NULL, NULL);
-    PyModule_AddObject(module, "error", CellFrame_error);
-    PyModule_AddObject(module, "DEBUG", PyLong_FromLong(L_DEBUG));
-    PyModule_AddObject(module, "INFO", PyLong_FromLong(L_INFO));
-    PyModule_AddObject(module, "NOTICE", PyLong_FromLong(L_NOTICE));
-    PyModule_AddObject(module, "MESSAGE", PyLong_FromLong(L_MSG));
-    PyModule_AddObject(module, "DAP", PyLong_FromLong(L_DAP));
-    PyModule_AddObject(module, "WARNING", PyLong_FromLong(L_WARNING));
-    PyModule_AddObject(module, "ATT", PyLong_FromLong(L_ATT));
-    PyModule_AddObject(module, "ERROR", PyLong_FromLong(L_ERROR));
-    PyModule_AddObject(module, "CRITICAL", PyLong_FromLong(L_CRITICAL));
-
-    PyModule_AddObject(module, "Crypto", (PyObject*)&dapCrypto_dapCryptoType);
-
-    PyModule_AddObject(module, "ServerCore", (PyObject*)&ServerCore_ServerCoreType);
-    PyModule_AddObject(module, "Events", (PyObject*)&dapEvents_dapEventsType);
-    PyModule_AddObject(module, "EventsSocket", (PyObject*)&dapEventsSocket_dapEventsSocketType);
-
-    PyModule_AddObject(module, "CryptoKeyType", (PyObject*)&CryptoKeyTypeObjecy_CryptoKeyTypeObjecyType);
-    PyModule_AddObject(module, "CryptoDataType", (PyObject*)&CryptoDataTypeObjecy_CryptoDataTypeObjecyType);
-
-
-    //PyModule_AddObject(module, "Dap", (PyObject*)&DapObject_DapObjectType);
-    return module;
-}
-
 static PyObject *python_cellframe_init(PyObject *self, PyObject *args){
     const char *app_name;
     const char *file_name_log;
@@ -136,6 +99,43 @@ static PyObject *python_cellframe_init(PyObject *self, PyObject *args){
     return PyLong_FromLong(0);
 }
 
+PyMODINIT_FUNC PyInit_CellFrame(void){
+
+    if (PyType_Ready(&DapObject_DapObjectType) < 0 || PyType_Ready(&dapCrypto_dapCryptoType) < 0 ||
+            PyType_Ready(&ServerCore_ServerCoreType) < 0 || PyType_Ready(&dapEvents_dapEventsType) < 0 ||
+            PyType_Ready(&dapEventsSocket_dapEventsSocketType) < 0 ||
+            PyType_Ready(&CryptoKeyTypeObjecy_CryptoKeyTypeObjecyType) < 0 ||
+            PyType_Ready(&CryptoDataTypeObjecy_CryptoDataTypeObjecyType) < 0)
+               return NULL;
+
+    PyObject *module = PyModule_Create(&CellFramePythonModule);
+
+    CellFrame_error = PyErr_NewException("libCellFrame.error", NULL, NULL);
+    PyModule_AddObject(module, "error", CellFrame_error);
+    PyModule_AddObject(module, "DEBUG", PyLong_FromLong(L_DEBUG));
+    PyModule_AddObject(module, "INFO", PyLong_FromLong(L_INFO));
+    PyModule_AddObject(module, "NOTICE", PyLong_FromLong(L_NOTICE));
+    PyModule_AddObject(module, "MESSAGE", PyLong_FromLong(L_MSG));
+    PyModule_AddObject(module, "DAP", PyLong_FromLong(L_DAP));
+    PyModule_AddObject(module, "WARNING", PyLong_FromLong(L_WARNING));
+    PyModule_AddObject(module, "ATT", PyLong_FromLong(L_ATT));
+    PyModule_AddObject(module, "ERROR", PyLong_FromLong(L_ERROR));
+    PyModule_AddObject(module, "CRITICAL", PyLong_FromLong(L_CRITICAL));
+
+    PyModule_AddObject(module, "Crypto", (PyObject*)&dapCrypto_dapCryptoType);
+
+    PyModule_AddObject(module, "ServerCore", (PyObject*)&ServerCore_ServerCoreType);
+    PyModule_AddObject(module, "Events", (PyObject*)&dapEvents_dapEventsType);
+    PyModule_AddObject(module, "EventsSocket", (PyObject*)&dapEventsSocket_dapEventsSocketType);
+
+    PyModule_AddObject(module, "CryptoKeyType", (PyObject*)&CryptoKeyTypeObjecy_CryptoKeyTypeObjecyType);
+    PyModule_AddObject(module, "CryptoDataType", (PyObject*)&CryptoDataTypeObjecy_CryptoDataTypeObjecyType);
+
+
+    //PyModule_AddObject(module, "Dap", (PyObject*)&DapObject_DapObjectType);
+    return module;
+}
+
 static PyObject *python_cellframe_deinit(PyObject *self, PyObject *args){
     dap_config_close(g_config);
     dap_config_deinit();
@@ -153,7 +153,7 @@ int main(int argc, char **argv) {
     }
 
     /* Add a built-in module, before Py_Initialize */
-    PyImport_AppendInittab("libCellFrame", PyInit_libCellFrame);
+    PyImport_AppendInittab("CellFrame", PyInit_CellFrame);
 
     /* Pass argv[0] to the Python interpreter */
     Py_SetProgramName(program);
@@ -164,7 +164,7 @@ int main(int argc, char **argv) {
     /* Optionally import the module; alternatively,
        import can be deferred until the embedded script
        imports it. */
-    PyImport_ImportModule("libCellFrame");
+    PyImport_ImportModule("CellFrame");
 
     PyMem_RawFree(program);
     return 0;
diff --git a/test/main_test.py b/test/main_test.py
index 60e6ad88..7ff2e640 100644
--- a/test/main_test.py
+++ b/test/main_test.py
@@ -1,4 +1,4 @@
-from libCellFrame import *
+from CellFrame import *
 import pickle
 import os
 import sys
-- 
GitLab