Re-enable python2 support

In order for the centos8 branch to merge back into master, it has
to work with both python2 and python3. However, in a previous commit
python2 support was removed in fm-common.

To support both python2 and python3, first we detect whether the base
OS is either running Centos7 or Centos8 and select the correct LDFLAGS
and use the correct CPPFLAGS. At runtime we determine the python version
and use the preprocessor to do the right thing.

Story: 2006796
Task: 42813

Signed-off-by: Charles Short <charles.short@windriver.com>
Change-Id: I3d83aaf8086e780603f9a227a4baad2d4e519abd
This commit is contained in:
Charles Short 2021-07-12 11:18:03 -04:00
parent c18a7865c6
commit a7d450bf38
3 changed files with 50 additions and 15 deletions

View File

@ -4,13 +4,24 @@ SRCS = fmAPI.cpp fmFile.cpp fmLog.cpp fmMsgServer.cpp fmMutex.cpp fmSocket.cpp f
CLI_SRCS = fm_cli.cpp
OBJS = $(SRCS:.cpp=.o)
CLI_OBJS = fm_cli.o
LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -lpython3.6m -ljson-c
LDLIBS = -lstdc++ -lrt -luuid -lpq -lpthread -ljson-c
INCLUDES = -I./ -I$(shell pg_config --includedir)
CCFLAGS = -g -O2 -Wall -Werror -fPIC
CCFLAGS = -g -O2 -Wall -fPIC
EXTRACCFLAGS= -Wformat -Wformat-security
MAJOR ?= 1
MINOR ?= 0
OS_ID = $(shell grep '^ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
OS_VERSION_ID= $(shell grep '^VERSION_ID=' /etc/os-release | cut -f2- -d= | sed -e 's/\"//g')
ifeq ($(OS_ID)-$(OS_VERSION_ID),centos-8)
LDLIBS += -lpython3.6m
INCLUDES += -I/usr/include/python3.6m
CCFLAGS += -Werror
else
INCLUDES += -I/usr/include/python2.7
LDLIBS += -lpython2.7
endif
DESTDIR ?= /
BINDIR ?= /usr/local/bin

View File

@ -15,7 +15,7 @@
#include <iostream>
#include <assert.h>
#include <arpa/inet.h>
#include <python3.6m/Python.h>
#include <Python.h>
#include "fmMutex.h"
@ -616,6 +616,7 @@ bool fm_db_util_sync_event_suppression(void){
argc = 2;
#if PY_MAJOR_VERSION >= 3
wchar_t *py_argv[2];
py_argv[0] = Py_DecodeLocale(FM_DB_SYNC_EVENT_SUPPRESSION, NULL);
if (py_argv[0] == NULL) {
@ -629,14 +630,23 @@ bool fm_db_util_sync_event_suppression(void){
return false;
}
Py_SetProgramName(py_argv[0]);
#else
char * argv[2];
argv[0] = (char*)FM_DB_SYNC_EVENT_SUPPRESSION;
argv[1] = (char*)db_conn;
Py_SetProgramName(argv[0]);
#endif
Py_Initialize();
PySys_SetArgv(argc, py_argv);
file = fopen(FM_DB_SYNC_EVENT_SUPPRESSION,"r");
PyRun_SimpleFile(file, FM_DB_SYNC_EVENT_SUPPRESSION);
fclose(file);
Py_Finalize();
#if PY_MAJOR_VERSION >= 3
PyMem_RawFree(py_argv[0]);
PyMem_RawFree(py_argv[1]);
#endif
FM_INFO_LOG("Completed event suppression synchronization.\n");

View File

@ -4,8 +4,13 @@
// SPDX-License-Identifier: Apache-2.0
//
#if PY_MAJOR_VERSION >= 3
#define PY_SSIZE_T_CLEAN
#include <python3.6m/Python.h>
#include <Python.h>
#define PyString_FromString PyUnicode_FromString
#else
#include <Python.h>
#endif
#include <stdio.h>
#include "fmAPI.h"
#include "fmAlarmUtils.h"
@ -84,7 +89,7 @@ static PyObject * _fm_set(PyObject * self, PyObject *args) {
rc = fm_set_fault(&alm_data, &tmp_uuid);
if (rc == FM_ERR_OK) {
return PyUnicode_FromString(&(tmp_uuid[0]));
return PyString_FromString(&(tmp_uuid[0]));
}
if (rc == FM_ERR_NOCONNECT){
@ -120,7 +125,7 @@ static PyObject * _fm_get(PyObject * self, PyObject *args) {
rc = fm_get_fault(&af,&ad);
if (rc == FM_ERR_OK) {
fm_alarm_to_string(&ad,alm_str);
return PyUnicode_FromString(alm_str.c_str());
return PyString_FromString(alm_str.c_str());
}
if (rc == FM_ERR_ENTITY_NOT_FOUND) {
@ -165,7 +170,7 @@ static PyObject * _fm_get_by_aid(PyObject * self, PyObject *args, PyObject* kwar
std::string s;
fm_alarm_to_string(&lst[ix],s);
if (s.size() > 0) {
if (PyList_Append(__lst, PyUnicode_FromString(s.c_str())) != 0) {
if (PyList_Append(__lst, PyString_FromString(s.c_str())) != 0) {
ERROR_LOG("Failed to append alarm to the list");
}
}
@ -214,7 +219,7 @@ static PyObject * _fm_get_by_eid(PyObject * self, PyObject *args, PyObject* kwar
std::string s;
fm_alarm_to_string(&lst[ix], s);
if (s.size() > 0) {
if (PyList_Append(__lst,PyUnicode_FromString(s.c_str())) != 0) {
if (PyList_Append(__lst,PyString_FromString(s.c_str())) != 0) {
ERROR_LOG("Failed to append alarm to the list");
}
}
@ -313,6 +318,7 @@ static PyMethodDef _methods [] = {
{ NULL, NULL, 0, NULL }
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef cModPyDem =
{
PyModuleDef_HEAD_INIT,
@ -321,16 +327,24 @@ static struct PyModuleDef cModPyDem =
-1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */
_methods
};
#endif
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC PyInit_fm_core() {
PyObject *m = PyModule_Create(&cModPyDem);
if (m == NULL){
PySys_WriteStderr("Failed to initialize fm_core");
return NULL;
}
return m;
}
#else
PyMODINIT_FUNC initfm_core() {
#endif
PyObject *m = PyModule_Create(&cModPyDem);
if (m == NULL){
PySys_WriteStderr("Failed to initialize fm_core");
return NULL;
}
return m;
PyObject *m = Py_InitModule("fm_core", _methods);
if (m == NULL){
PySys_WriteStderr("Failed to initialize fm_core");
return;
}
}
#endif