Skip to content

Commit 249d177

Browse files
authored
Merge pull request #123 from EmperorYP7/python-bindings
feat: Initiated pybind11 Configuration
2 parents b6bedd2 + 6d1c0e2 commit 249d177

File tree

10 files changed

+184
-83
lines changed

10 files changed

+184
-83
lines changed

CMakeLists.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ endif()
3131
# Project definition.
3232

3333
project(Casbin-CPP
34-
VERSION 1.0.0
34+
VERSION 1.27.0
3535
DESCRIPTION "An authorization library that supports access control models like ACL, RBAC, ABAC in C/C++"
3636
HOMEPAGE_URL https://github.com/casbin/casbin-cpp
37-
LANGUAGES CXX C)
37+
LANGUAGES CXX C
38+
)
3839

3940

4041
###############################################################################
@@ -50,18 +51,23 @@ endif()
5051

5152
option(CASBIN_BUILD_TEST "State whether to build test" ON)
5253
option(CASBIN_BUILD_BENCHMARK "State whether to build benchmarks" ON)
54+
option(CASBIN_BUILD_BINDINGS "State whether to build language bindings" ON)
55+
option(CASBIN_BUILD_PYTHON_BINDINGS "State whether to build python bindings" ON)
5356

5457
# Do not output install messages.
5558
if(NOT DEFINED CMAKE_INSTALL_MESSAGE)
5659
set(CMAKE_INSTALL_MESSAGE "NEVER")
5760
endif()
5861

62+
if(CASBIN_BUILD_BINDINGS)
63+
add_subdirectory(bindings)
64+
endif()
65+
5966
if(CASBIN_BUILD_TEST)
6067
enable_testing()
68+
add_subdirectory(tests)
6169
endif()
6270

63-
add_subdirectory(tests)
64-
6571
# Change the path max size to avoid problem on Windows.
6672
if(NOT DEFINED CMAKE_OBJECT_PATH_MAX)
6773
set(CMAKE_OBJECT_PATH_MAX 300)

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
Casbin-CPP
22
====
33

4-
[![Build Status](https://dev.azure.com/Casbin/casbin/_apis/build/status/casbin.casbin-cpp?branchName=master)](https://dev.azure.com/Casbin/casbin/_build?definitionId=2&branchName=master)
54
[![CI](https://github.com/casbin/casbin-cpp/actions/workflows/ci.yml/badge.svg)](https://github.com/casbin/casbin-cpp/actions/workflows/ci.yml)
65
[![GitHub release (latest by date)](https://img.shields.io/github/v/release/casbin/casbin-cpp)](https://github.com/casbin/casbin-cpp/releases/latest)
76
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)

bindings/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
if(CASBIN_BUILD_PYTHON_BINDINGS)
16+
add_subdirectory(python)
17+
endif()

bindings/python/CMakeLists.txt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
set(SOURCES
16+
main.cpp
17+
)
18+
19+
add_library(pycasbin MODULE ${SOURCES})
20+
21+
set_target_properties(pycasbin PROPERTIES
22+
PREFIX ""
23+
)
24+
25+
if(WIN32)
26+
# Windows uses .pyd extension for python modules
27+
set_target_properties(pycasbin PROPERTIES
28+
SUFFIX ".pyd"
29+
)
30+
endif()
31+
32+
33+
# NOTE: Depending of the compiler version pybind11 2.4.3 does not compile with C++17 so revert to c++11
34+
35+
if(UNIX)
36+
# A 'module' is a dynamic library on Linux (i.e. '-fPIC' needed),
37+
# but a static library on Windows.
38+
39+
# If supported for the target machine, emit position-independent code
40+
# suitable for dynamic linking.
41+
set_property(TARGET pycasbin PROPERTY POSITION_INDEPENDENT_CODE ON)
42+
endif()
43+
44+
# macOS demands that the linker resolve all symbols at build time
45+
# Pass this flag to allow dynamic linking
46+
if(APPLE)
47+
set_target_properties(pycasbin PROPERTIES
48+
LINK_FLAGS "-undefined dynamic_lookup"
49+
)
50+
endif()
51+
52+
target_link_libraries(pycasbin
53+
PRIVATE
54+
# casbin
55+
pybind11::module
56+
)
57+

bindings/python/main.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2021 The casbin Authors. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* This is the main file for python bindings workflow
17+
*/
18+
19+
#include <pybind11/pybind11.h>
20+
21+
#define STRINGIFY(x) #x
22+
#define MACRO_STRINGIFY(x) STRINGIFY(x)
23+
24+
int add(int i, int j) {
25+
return i + j;
26+
}
27+
28+
namespace py = pybind11;
29+
30+
PYBIND11_MODULE(pycasbin, m) {
31+
m.doc() = R"pbdoc(
32+
Pybind11 example plugin
33+
-----------------------
34+
35+
.. currentmodule:: pycasbin
36+
37+
.. autosummary::
38+
:toctree: _generate
39+
40+
add
41+
subtract
42+
)pbdoc";
43+
44+
m.def("add", &add, R"pbdoc(
45+
Add two numbers
46+
47+
Some other explanation about the add function.
48+
)pbdoc");
49+
50+
m.def("subtract", [](int i, int j) { return i - j; }, R"pbdoc(
51+
Subtract two numbers
52+
53+
Some other explanation about the subtract function.
54+
)pbdoc");
55+
56+
m.attr("__version__") = "dev";
57+
}

casbin/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
FILE(GLOB_RECURSE SRC_FILES "*.cpp" "*.h")
1616

17+
# Setting to C++ standard to C++17
18+
set(CMAKE_CXX_STANDARD 17)
19+
1720
add_library(casbin ${SRC_FILES})
1821
include_directories(${CMAKE_SOURCE_DIR}/casbin)
1922

casbin/pch.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include <thread>
3838
#include <future>
3939
#include <condition_variable>
40+
#include <variant>
4041

4142
#include "attribute_types.h"
4243
#include "data_types.h"

cmake/modules/FindExtPackages.cmake

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,22 @@ set(CMAKE_FIND_PACKAGE_NO_SYSTEM_PACKAGE_REGISTRY ON CACHE BOOL
2424
###############################################################################
2525
### Packages and versions ###
2626

27-
# googletest
28-
# https://github.com/google/googletest
29-
find_package(googletest 1.11.0 REQUIRED)
27+
if(CASBIN_BUILD_TEST)
28+
# googletest
29+
# https://github.com/google/googletest
30+
find_package(googletest 1.11.0 REQUIRED)
3031

31-
if(CASBIN_BUILD_BENCHMARK)
32-
# benchmark
33-
# https://github.com/google/benchmark
34-
find_package(benchmark 1.5.5 REQUIRED)
32+
if(CASBIN_BUILD_BENCHMARK)
33+
# benchmark
34+
# https://github.com/google/benchmark
35+
find_package(benchmark 1.5.5 REQUIRED)
36+
endif()
37+
endif()
38+
39+
if(CASBIN_BUILD_BINDINGS)
40+
if(CASBIN_BUILD_PYTHON_BINDINGS)
41+
# pybind11
42+
# https://github.com/pybind/pybind11
43+
find_package(pybind11 2.7.0 REQUIRED)
44+
endif()
3545
endif()

cmake/modules/Findpybind11.cmake

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2021 The casbin Authors. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
include(FetchContent)
16+
17+
FetchContent_Declare(
18+
pybind11
19+
URL https://github.com/pybind/pybind11/archive/refs/tags/v2.7.0.zip
20+
)
21+
22+
FetchContent_MakeAvailable(pybind11)

makefile

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)