Skip to content

Commit

Permalink
unify all compiled C++ into 1 binary file
Browse files Browse the repository at this point in the history
should resolve #61
  • Loading branch information
2bndy5 committed Jun 13, 2024
1 parent 4db31f1 commit 93e0802
Show file tree
Hide file tree
Showing 14 changed files with 325 additions and 357 deletions.
105 changes: 28 additions & 77 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ cmake_minimum_required(VERSION 3.15)
project(pyrf24)

if(SKBUILD)
message(STATUS "This project is being built using scikit-build & pybind11")
message(STATUS "This project is being built using scikit-build & pybind11")
endif()

set(CMAKE_INTERPROCEDURAL_OPTIMIZATION ON)
Expand All @@ -14,112 +14,63 @@ include(cmake/using_flags.cmake)
add_subdirectory(pybind11)
include(RF24/cmake/AutoConfig_RF24_DRIVER.cmake)
add_subdirectory(RF24/utility) # configure the RF24_DRIVER

if(NOT "${RF24_LINKED_DRIVER}" STREQUAL "")
message(STATUS "Linking to utility driver '${RF24_LINKED_DRIVER}'")
endif()

# suplement our copy of linux/gpio.h into SPIDEV & RPi driver sources
set(SUPPLEMENT_LINUX_GPIO_H FALSE)

if("${RF24_DRIVER}" STREQUAL "SPIDEV")
set(SUPPLEMENT_LINUX_GPIO_H TRUE)
message(STATUS "Supplementing ${RF24_DRIVER} driver with linux/gpio.h")

# file(COPY src/linux/gpio.h DESTINATION RF24/utility/${RF24_DRIVER}/linux)
list(APPEND RF24_DRIVER_SOURCES src/linux/gpio.h)
endif()

### Build C++ RF24 stack as shared libraries (resulting in isolated binary drivers)

################################# RF24 C++ #########################
add_library(cpp_rf24 SHARED
# ## Build python bindings for RF24 stack into 1 binary
pybind11_add_module(pyrf24
RF24/RF24.cpp
${RF24_DRIVER_SOURCES}
)
if(SUPPLEMENT_LINUX_GPIO_H)
target_include_directories(cpp_rf24 PUBLIC src)
endif()
target_include_directories(cpp_rf24 PUBLIC utility)
if(NOT "${RF24_LINKED_DRIVER}" STREQUAL "")
if("${RF24_DRIVER}" STREQUAL "wiringPi")
target_link_libraries(cpp_rf24 PUBLIC rt crypt ${RF24_LINKED_DRIVER})
else()
target_link_libraries(cpp_rf24 PUBLIC ${RF24_LINKED_DRIVER})
endif()
endif()
apply_flags(cpp_rf24)

################################# RF24Network C++ #########################
add_library(cpp_rf24_network SHARED
RF24Network/RF24Network.cpp
)
# don't let source look for an installed RF24 lib
target_compile_definitions(cpp_rf24_network PUBLIC USE_RF24_LIB_SRC)
target_include_directories(cpp_rf24_network PUBLIC
RF24
RF24/utility
RF24/utility/${RF24_DRIVER}
RF24Network
)
target_link_libraries(cpp_rf24_network PUBLIC cpp_rf24)
apply_flags(cpp_rf24_network)

################################# RF24Mesh C++ #########################
add_library(cpp_rf24_mesh SHARED
RF24Mesh/RF24Mesh.cpp
)
# don't let source look for an installed RF24 lib
target_compile_definitions(cpp_rf24_mesh PUBLIC USE_RF24_LIB_SRC)
target_include_directories(cpp_rf24_mesh PUBLIC
RF24
RF24/utility
RF24/utility/${RF24_DRIVER}
RF24Network
RF24Mesh
)
target_link_libraries(cpp_rf24_mesh PUBLIC cpp_rf24_network)
apply_flags(cpp_rf24_mesh)

### Build python bindings for RF24 stack and link to shared C++ lib binaries

################################# RF24 #############################
pybind11_add_module(rf24 src/pyRF24.cpp)
target_link_libraries(rf24 PUBLIC cpp_rf24)
target_include_directories(rf24 PUBLIC
RF24
RF24/utility
RF24/utility/${RF24_DRIVER}
src/pyRF24.cpp
src/pyRF24Network.cpp
src/pyRF24Mesh.cpp
src/glue.cpp
)

################################# RF24NETWORK #############################
pybind11_add_module(rf24_network src/pyRF24Network.cpp)
target_link_libraries(rf24_network PUBLIC cpp_rf24_network)
target_include_directories(rf24_network PUBLIC
RF24
RF24/utility
RF24/utility/${RF24_DRIVER}
RF24Network
)
# don't let source look for an installed RF24 lib
target_compile_definitions(rf24_network PUBLIC USE_RF24_LIB_SRC)
target_compile_definitions(pyrf24 PUBLIC USE_RF24_LIB_SRC)

if(SUPPLEMENT_LINUX_GPIO_H)
target_include_directories(pyrf24 PUBLIC src)
endif()

################################# RF24MESH #############################
pybind11_add_module(rf24_mesh src/pyRF24Mesh.cpp)
target_link_libraries(rf24_mesh PUBLIC cpp_rf24_mesh)
target_include_directories(rf24_mesh PUBLIC
target_include_directories(pyrf24 PUBLIC
RF24
RF24/utility
RF24/utility/${RF24_DRIVER}
RF24Network
RF24Mesh
)
# don't let source look for an installed RF24 lib
target_compile_definitions(rf24_mesh PUBLIC USE_RF24_LIB_SRC)

if(NOT "${RF24_LINKED_DRIVER}" STREQUAL "")
if("${RF24_DRIVER}" STREQUAL "wiringPi")
target_link_libraries(pyrf24 PUBLIC rt crypt ${RF24_LINKED_DRIVER})
else()
target_link_libraries(pyrf24 PUBLIC ${RF24_LINKED_DRIVER})
endif()
endif()

apply_flags(pyrf24)

################################ INSTALL RULES ####################################
# these are needed for scikit builds since the resulting .so files are copied into
# ############################### INSTALL RULES ####################################
# these are needed since the resulting .so files are copied into
# the binary distribution wheels (.whl files) for python.
install(TARGETS rf24 rf24_network rf24_mesh DESTINATION .)
install(TARGETS cpp_rf24 cpp_rf24_network cpp_rf24_mesh DESTINATION .)
install(TARGETS pyrf24 DESTINATION .)

# uncomment to show compiler args used in build logs
# set(CMAKE_VERBOSE_MAKEFILE ON)
1 change: 0 additions & 1 deletion examples/scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ def scan(timeout: int = 30):
if endl:
signals = [0] * 126 # reset the signal counts for new line


# finish printing results and end with a new line
while curr_channel < len(signals) - 1:
curr_channel += 1
Expand Down
14 changes: 14 additions & 0 deletions src/glue.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* The main module that allows compiling all bindings into 1 python extension. */
#include "pyRF24.h"
#include "pyRF24Network.h"
#include "pyRF24Mesh.h"

PYBIND11_MODULE(pyrf24, m)
{
m.doc() = "A Python module that wraps all RF24 C++ library's API";
py::options options;
options.disable_function_signatures();
init_rf24(m);
init_rf24network(m);
init_rf24mesh(m);
}
38 changes: 35 additions & 3 deletions src/pyRF24.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,41 @@
#include <pybind11/pybind11.h>
#include "pyRF24.h"

PYBIND11_MODULE(rf24, m)
void throw_ba_exception(void)
{
PyErr_SetString(PyExc_TypeError, "buf parameter must be bytes or bytearray");
py::error_already_set();
}

char* get_bytes_or_bytearray_str(py::object buf)
{
PyObject* py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_AsString(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_AsString(py_ba);
else
throw_ba_exception();

return NULL;
}

int get_bytes_or_bytearray_ln(py::object buf)
{
PyObject* py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_Size(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_Size(py_ba);
else
throw_ba_exception();

return 0;
}

void init_rf24(py::module& m)
{
m.doc() = "A Python module that wraps all RF24 C++ library's API";
m.attr("RF24_DRIVER") = RF24_DRIVER;
Expand Down Expand Up @@ -68,8 +102,6 @@ PYBIND11_MODULE(rf24, m)
.export_values();

// ******************** RF24 class **************************
py::options options;
options.disable_function_signatures();
py::class_<RF24Wrapper>(m, "RF24")

// *****************************************************************************
Expand Down
43 changes: 9 additions & 34 deletions src/pyRF24.h
Original file line number Diff line number Diff line change
@@ -1,43 +1,14 @@
#ifndef PYRF24_H
#define PYRF24_H
#include <pybind11/pybind11.h>
#include <RF24.h>
#include <nRF24L01.h>

namespace py = pybind11;

void throw_ba_exception(void)
{
PyErr_SetString(PyExc_TypeError, "buf parameter must be bytes or bytearray");
py::error_already_set();
}

char* get_bytes_or_bytearray_str(py::object buf)
{
PyObject* py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_AsString(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_AsString(py_ba);
else
throw_ba_exception();

return NULL;
}

int get_bytes_or_bytearray_ln(py::object buf)
{
PyObject* py_ba;
py_ba = buf.ptr();
if (PyByteArray_Check(py_ba))
return PyByteArray_Size(py_ba);
else if (PyBytes_Check(py_ba))
return PyBytes_Size(py_ba);
else
throw_ba_exception();

return 0;
}

void throw_ba_exception(void);
char* get_bytes_or_bytearray_str(py::object buf);
int get_bytes_or_bytearray_ln(py::object buf);
class RF24Wrapper : public RF24
{

Expand Down Expand Up @@ -240,3 +211,7 @@ class RF24Wrapper : public RF24
stopListening();
}
};

void init_rf24(py::module& m);

#endif // PYRF24_H
6 changes: 1 addition & 5 deletions src/pyRF24Mesh.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include "pyRF24Mesh.h"

PYBIND11_MODULE(rf24_mesh, m)
void init_rf24mesh(py::module& m)
{
m.doc() = "A Python module that wraps the RF24Mesh C++ library's API";
py::options options;
options.disable_function_signatures();

m.attr("MESH_DEFAULT_ADDRESS") = MESH_DEFAULT_ADDRESS;
m.attr("MESH_ADDR_LOOKUP") = MESH_ADDR_LOOKUP;
m.attr("MESH_ADDR_RELEASE") = MESH_ADDR_RELEASE;
Expand Down
10 changes: 7 additions & 3 deletions src/pyRF24Mesh.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef PYRF24MESH_H
#define PYRF24MESH_H
#include <pybind11/pybind11.h>
#include "pyRF24Network.cpp"
#include "pyRF24Network.h"
#include <RF24Mesh.h>

namespace py = pybind11;

class RF24MeshWrapper : public RF24Mesh
{
public:
Expand Down Expand Up @@ -47,3 +47,7 @@ class RF24MeshWrapper : public RF24Mesh
return list;
}
};

void init_rf24mesh(py::module& m);

#endif // PYRF24MESH_H
6 changes: 1 addition & 5 deletions src/pyRF24Network.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
#include "pyRF24Network.h"

PYBIND11_MODULE(rf24_network, m)
void init_rf24network(py::module& m)
{
m.doc() = "A Python module that wraps the RF24Network C++ library's API";
py::options options;
options.disable_function_signatures();

// **************** Module level constants *********************
//
m.attr("MAX_USER_DEFINED_HEADER_TYPE") = MAX_USER_DEFINED_HEADER_TYPE;
Expand Down
8 changes: 6 additions & 2 deletions src/pyRF24Network.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#ifndef PYRF24NETWORK_H
#define PYRF24NETWORK_H
#include <pybind11/pybind11.h>
#include "pyRF24.h"
#include <RF24Network.h>
// #include <queue>
// #include <pybind11/stl.h>

namespace py = pybind11;

/*
namespace pybind11 {
namespace detail {
Expand Down Expand Up @@ -116,3 +116,7 @@ class RF24NetworkWrapper : public RF24Network
return RF24Network::node_address;
}
};

void init_rf24network(py::module& m);

#endif // PYRF24NETWORK_H
6 changes: 1 addition & 5 deletions src/pyrf24/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .rf24 import (
from .pyrf24 import ( # type: ignore
RF24,
RF24_CRC_DISABLED,
RF24_CRC_8,
Expand All @@ -11,8 +11,6 @@
RF24_PA_HIGH,
RF24_PA_MAX,
RF24_DRIVER,
)
from .rf24_network import (
RF24Network,
RF24NetworkHeader,
# RF24NetworkFrame,
Expand All @@ -29,8 +27,6 @@
NETWORK_REQ_ADDRESS,
FLAG_FAST_FRAG,
FLAG_NO_POLL,
)
from .rf24_mesh import (
RF24Mesh,
MESH_DEFAULT_ADDRESS,
MESH_ADDR_LOOKUP,
Expand Down
2 changes: 1 addition & 1 deletion src/pyrf24/fake_ble.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
from os import urandom
import struct
from typing import Union, List, Optional
from .rf24 import (
from .pyrf24 import (
RF24,
RF24_CRC_DISABLED,
RF24_PA_HIGH,
Expand Down
Loading

0 comments on commit 93e0802

Please sign in to comment.