Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SWIGPYTHON to improve SWIG for GO wrapper. #714

Merged
merged 8 commits into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .azure-pipelines/build-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ jobs:
pytest --cov=. --cov-report=xml
mv coverage.xml tests/coverage.xml
gcovr -r ./ -e ".*/swsscommon_wrap.cpp" --exclude-unreachable-branches --exclude-throw-branches -x --xml-pretty -o coverage.xml
redis-cli FLUSHALL
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redis-cli FLUSHALL

I guess you flush redis for test purpose, so it should be just above make -C goext check line. #Closed

make -C goext
make -C goext check

rm -rf $(Build.ArtifactStagingDirectory)/download
displayName: "Run swss common unit tests"
Expand Down
4 changes: 2 additions & 2 deletions common/configdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
std::string m_db_name;
};

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## Note: diamond inheritance, reusing functions in both classes
class ConfigDBConnector(SonicV2Connector, ConfigDBConnector_Native):
Expand Down Expand Up @@ -307,7 +307,7 @@ class ConfigDBPipeConnector_Native: public ConfigDBConnector_Native
int _get_config(DBConnector& client, RedisTransactioner& pipe, std::map<std::string, std::map<std::string, std::map<std::string, std::string>>>& data, int cursor);
};

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
class ConfigDBPipeConnector(ConfigDBConnector, ConfigDBPipeConnector_Native):

Expand Down
2 changes: 1 addition & 1 deletion common/countertable.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class KeyCache {
return m_keyMap.at(name);
}

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
def __getitem__(self, name):
return self.at(name)
Expand Down
10 changes: 5 additions & 5 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SonicDBConfig
static constexpr const char *DEFAULT_SONIC_DB_CONFIG_FILE = "/var/run/redis/sonic-db/database_config.json";
static constexpr const char *DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE = "/var/run/redis/sonic-db/database_global.json";
static void initialize(const std::string &file = DEFAULT_SONIC_DB_CONFIG_FILE);
#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## TODO: the python function and C++ one is not on-par
@staticmethod
Expand All @@ -51,7 +51,7 @@ class SonicDBConfig
#endif

static void initializeGlobalConfig(const std::string &file = DEFAULT_SONIC_DB_GLOBAL_CONFIG_FILE);
#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## TODO: the python function and C++ one is not on-par
@staticmethod
Expand All @@ -70,7 +70,7 @@ class SonicDBConfig
static std::string getDbHostname(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE);
static int getDbPort(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE);
static std::vector<std::string> getNamespaces();
#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
## TODO: the python function and C++ one is not on-par
@staticmethod
Expand Down Expand Up @@ -163,7 +163,7 @@ class DBConnector : public RedisContext
std::string getDbName() const;
std::string getNamespace() const;

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
namespace = property(getNamespace)
%}
Expand All @@ -181,7 +181,7 @@ class DBConnector : public RedisContext

int64_t del(const std::string &key);

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
// SWIG interface file (.i) globally rename map C++ `del` to python `delete`,
// but applications already followed the old behavior of auto renamed `_del`.
// So we implemented old behavior for backward compatibility
Expand Down
2 changes: 1 addition & 1 deletion common/producerstatetable.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ProducerStateTable : public TableBase, public TableName_KeySet
const std::string &op = DEL_COMMAND,
const std::string &prefix = EMPTY_PREFIX);

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
// SWIG interface file (.i) globally rename map C++ `del` to python `delete`,
// but applications already followed the old behavior of auto renamed `_del`.
// So we implemented old behavior for backward compatibility
Expand Down
2 changes: 1 addition & 1 deletion common/producertable.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ProducerTable : public TableBase, public TableName_KeyValueOpQueues
const std::string &op = DEL_COMMAND,
const std::string &prefix = EMPTY_PREFIX);

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
// SWIG interface file (.i) globally rename map C++ `del` to python `delete`,
// but applications already followed the old behavior of auto renamed `_del`.
// So we implemented old behavior for backward compatibility
Expand Down
2 changes: 1 addition & 1 deletion common/sonicv2connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SonicV2Connector_Native
std::string m_netns;
};

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
class SonicV2Connector(SonicV2Connector_Native):

Expand Down
4 changes: 2 additions & 2 deletions common/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class TableEntryPoppable {
}
};

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
%pythoncode %{
def transpose_pops(m):
return [tuple(m[j][i] for j in range(len(m))) for i in range(len(m[0]))]
Expand Down Expand Up @@ -207,7 +207,7 @@ class Table : public TableBase, public TableEntryEnumerable {
/* Get the configured ttl value for key */
bool ttl(const std::string &key, int64_t &reply_value);

#ifdef SWIG
#if defined(SWIG) && defined(SWIGPYTHON)
// SWIG interface file (.i) globally rename map C++ `del` to python `delete`,
// but applications already followed the old behavior of auto renamed `_del`.
// So we implemented old behavior for backward compatibility
Expand Down
25 changes: 25 additions & 0 deletions goext/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export CGO_LDFLAGS := -lswsscommon -lhiredis
export CGO_CXXFLAGS := -I../ -I../common/ -w -Wall -fpermissive

GO ?= /usr/local/go/bin/go
SWIG ?= /usr/bin/swig
RM=rm -f
LN=ln

SWIG_FLAG = -go -cgo -c++ -intgosize 64
ifeq ($(CONFIGURED_ARCH),arm64)
SWIG_FLAG += -DSWIGWORDSIZE64
endif

.PHONY: all check clean

all:
-$(LN) -s ../pyext/swsscommon.i swsscommon.i
Copy link
Contributor

@qiluo-msft qiluo-msft Nov 23, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why -? #Closed

$(SWIG) $(SWIG_FLAG) -I/usr/include/swss/ swsscommon.i

check:
sudo CGO_LDFLAGS="$(CGO_LDFLAGS)" CGO_CXXFLAGS="$(CGO_CXXFLAGS)" $(GO) test

clean:
$(RM) swsscommon.go *.cxx

13 changes: 13 additions & 0 deletions goext/swsscommon_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package swsscommon

import (
"testing"
"os"
"path/filepath"
)

func TestSonicDBConfig(t *testing.T) {
pwd, _ := os.Getwd()
SonicDBConfigInitialize(filepath.Dir(pwd) + "/tests/redis_multi_db_ut_config/database_config.json")
}

4 changes: 4 additions & 0 deletions pyext/swsscommon.i
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
%include <std_vector.i>
%include <std_pair.i>
%include <std_map.i>
#ifdef SWIGPYTHON
%include <std_shared_ptr.i>
#endif
%include <typemaps.i>
%include <stdint.i>
%include <exception.i>
Expand All @@ -64,6 +66,7 @@
%template(GetConfigResult) std::map<std::string, std::map<std::string, std::map<std::string, std::string>>>;
%template(GetInstanceListResult) std::map<std::string, swss::RedisInstInfo>;

#ifdef SWIGPYTHON
%exception {
try
{
Expand Down Expand Up @@ -136,6 +139,7 @@
temp = SWIG_NewPointerObj(*$1, SWIGTYPE_p_swss__Selectable, 0);
SWIG_Python_AppendOutput($result, temp);
}
#endif

%inline %{
template <typename T>
Expand Down