Skip to content

Commit

Permalink
Check available rrdtool API version
Browse files Browse the repository at this point in the history
  • Loading branch information
matwey committed Sep 10, 2024
1 parent 21132c0 commit ec70cd8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
22 changes: 22 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,36 @@
cmake_minimum_required(VERSION 3.15)
project(${SKBUILD_PROJECT_NAME} VERSION ${SKBUILD_PROJECT_VERSION} LANGUAGES C)

include(CheckCSourceCompiles)

set(CMAKE_C_STANDARD 99)

function(check_mutable_argv)
set(CMAKE_REQUIRED_LIBRARIES PkgConfig::LIBRRD)
check_c_source_compiles(
"
#include <rrd.h>
int main() {
(void)rrd_graph_v(0, (char**)NULL);
return 0;
}
"
LIBRRD_HAS_MUTABLE_ARGV)
unset(CMAKE_REQUIRED_LIBRARIES)
endfunction()

find_package(Python REQUIRED COMPONENTS Interpreter Development.Module)
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBRRD REQUIRED IMPORTED_TARGET librrd)

check_mutable_argv()

python_add_library(rrd MODULE c_src/rrd.c WITH_SOABI)
target_link_libraries(rrd PRIVATE PkgConfig::LIBRRD)
if(LIBRRD_HAS_MUTABLE_ARGV)
target_compile_definitions(rrd PRIVATE LIBRRD_HAS_MUTABLE_ARGV)
endif()
set_property(TARGET rrd PROPERTY INTERPROCEDURAL_OPTIMIZATION True)

install(TARGETS rrd DESTINATION collectd_rest)
10 changes: 8 additions & 2 deletions c_src/rrd.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,18 @@ static int rrd_parse_arguments(char* command) {

static PyObject*
rrd_render(PyObject* self, PyObject* args) {
#ifdef LIBRRD_HAS_MUTABLE_ARGV // before rrdtool 1.9.0
typedef char* arg_t;
#else
typedef const char* arg_t;
#endif // LIBRRD_HAS_MUTABLE_ARGV

PyObject* ret = NULL;

int i;
char* it;
int argc;
char** argv;
arg_t* argv;
char* format;
char* command;
int command_argc;
Expand Down Expand Up @@ -129,7 +135,7 @@ rrd_render(PyObject* self, PyObject* args) {
}
command_argc = rrd_parse_arguments(command_args);

argv = PyMem_New(char*, 3 + 1 + command_argc + 1);
argv = PyMem_New(arg_t, 3 + 1 + command_argc + 1);
if (argv == NULL) {
PyErr_SetString(PyExc_MemoryError, "PyMem_New: Out of memory");
goto err_PyMem_New;
Expand Down

0 comments on commit ec70cd8

Please sign in to comment.