Skip to content

Commit

Permalink
Wrapping C/C++ for Python
Browse files Browse the repository at this point in the history
  • Loading branch information
dong wang committed Jun 15, 2023
1 parent 2332ce2 commit 5d1b745
Show file tree
Hide file tree
Showing 255 changed files with 61,122 additions and 24 deletions.
26 changes: 18 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.16)
project(aidb)
project(AiDB)
SET(CMAKE_BUILD_TYPE "Release")
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 14)

#add_definitions(-std=c++11 -g -O3 -pthread)
#add_definitions(-std=c++11 -pthread)
Expand All @@ -10,17 +10,18 @@ set(CMAKE_CXX_STANDARD 11)

# backend options
option(ENGINE_MNN "Enable MNN" ON)
option(ENGINE_ORT "Enable ONNXRuntime" ON)
option(ENGINE_NCNN "Enable NCNN" ON)
option(ENGINE_TNN "Enable TNN" ON)
option(ENGINE_ORT "Enable ONNXRuntime" OFF)
option(ENGINE_NCNN "Enable NCNN" OFF)
option(ENGINE_TNN "Enable TNN" OFF)
option(ENGINE_OPV "Enable OpenVINO" OFF) #Mac 与TNN冲突
option(ENGINE_PPLite "Enable paddle-lite" ON)
option(ENGINE_PPLite "Enable paddle-lite" OFF)
option(ENGINE_NCNN_WASM "Enable NCNN WASM" OFF)
option(BUILD_SAMPLE "Enable build sample" OFF)

option(BUILD_PYTHON "Enable python api" OFF)
option(OPENCV_HAS_FREETYPE "opencv-contrib complied with freetype or not. cv::putText can put chinese." OFF)

message(STATUS ">>>>>>>>>>>>>")
message(STATUS "AIDB BUILD INFO:")
message(STATUS "AiDB BUILD INFO:")
message(STATUS "\tMNN: ${ENGINE_MNN}")
message(STATUS "\tONNXRuntime: ${ENGINE_ORT}")
message(STATUS "\tNCNN: ${ENGINE_NCNN}")
Expand All @@ -29,7 +30,12 @@ message(STATUS "\tOpenVINO: ${ENGINE_OPV}")
message(STATUS "\tPaddleLite: ${ENGINE_PPLite}")
message(STATUS "\tWASM: ${ENGINE_NCNN_WASM}")
message(STATUS "\tBuild Sample: ${BUILD_SAMPLE}")
message(STATUS "\tBuild Python: ${BUILD_PYTHON}")
message(STATUS "\tOPENCV_HAS_FREETYPE: ${OPENCV_HAS_FREETYPE}")

add_definitions(-DOPENCV_HAS_FREETYPE)

message("=========>>>> ${CMAKE_BINARY_DIR}")
if(ENGINE_NCNN_WASM)
if(NOT WASM_FEATURE)
message(FATAL_ERROR "You must pass cmake option -DWASM_FEATURE and possible values are basic, simd, threads and simd-threads")
Expand Down Expand Up @@ -62,3 +68,7 @@ endif ()
if(BUILD_SAMPLE)
add_subdirectory(samples)
endif ()

if(BUILD_PYTHON)
add_subdirectory(python)
endif ()
46 changes: 46 additions & 0 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
cmake_minimum_required(VERSION 3.16)
project(pyAiDB CXX C)
SET(CMAKE_CXX_STANDARD 14)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
SET(CMAKE_BUILD_TYPE "Release")

#set(PYTHONVERSION "3.6.12")
set(LIB_VERSION_INFO 1.0.0)



find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})

include_directories(${CMAKE_CURRENT_LIST_DIR}/../source)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../source/core)
include_directories(${CMAKE_CURRENT_LIST_DIR}/../3rdparty)
include_directories(${CMAKE_CURRENT_LIST_DIR}/wrapper)

set(LIB_PATH ${CMAKE_CURRENT_LIST_DIR}/../libs/${PLATFORM})
link_directories(${LIB_PATH})
link_directories(${LIB_PATH}/tnn/)
link_directories(${LIB_PATH}/paddlelite/)
link_directories(${LIB_PATH}/openvino/intel64)

add_subdirectory(./pybind11)
pybind11_add_module(pyAiDB ${PROJECT_SOURCE_DIR}/wrapper.cpp)

target_link_libraries(pyAiDB PRIVATE AiDB ${OpenCV_LIBS})
target_compile_definitions(pyAiDB PRIVATE VERSION_INFO=${LIB_VERSION_INFO})

#if (UNIX AND NOT APPLE)
# add_custom_command(
# TARGET pyAiDB
# PRE_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/../3rdparty/lib/linux/libAiDB.so ${CMAKE_BINARY_DIR}/python/
# )
#elseif (APPLE)
# add_custom_command(
# TARGET pyAiDB
# PRE_BUILD
# COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_BINARY_DIR}/source/libAiDB.dylib ${CMAKE_BINARY_DIR}/python/
# )
#endif()

35 changes: 35 additions & 0 deletions python/pybind11/.appveyor.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
version: 1.0.{build}
image:
- Visual Studio 2017
test: off
skip_branch_with_pr: true
build:
parallel: true
platform:
- x86
environment:
matrix:
- PYTHON: 36
CONFIG: Debug
install:
- ps: |
$env:CMAKE_GENERATOR = "Visual Studio 15 2017"
if ($env:PLATFORM -eq "x64") { $env:PYTHON = "$env:PYTHON-x64" }
$env:PATH = "C:\Python$env:PYTHON\;C:\Python$env:PYTHON\Scripts\;$env:PATH"
python -W ignore -m pip install --upgrade pip wheel
python -W ignore -m pip install pytest numpy --no-warn-script-location pytest-timeout
- ps: |
Start-FileDownload 'https://gitlab.com/libeigen/eigen/-/archive/3.3.7/eigen-3.3.7.zip'
7z x eigen-3.3.7.zip -y > $null
$env:CMAKE_INCLUDE_PATH = "eigen-3.3.7;$env:CMAKE_INCLUDE_PATH"
build_script:
- cmake -G "%CMAKE_GENERATOR%" -A "%CMAKE_ARCH%"
-DCMAKE_CXX_STANDARD=14
-DPYBIND11_WERROR=ON
-DDOWNLOAD_CATCH=ON
-DCMAKE_SUPPRESS_REGENERATION=1
.
- set MSBuildLogger="C:\Program Files\AppVeyor\BuildAgent\Appveyor.MSBuildLogger.dll"
- cmake --build . --config %CONFIG% --target pytest -- /m /v:m /logger:%MSBuildLogger%
- cmake --build . --config %CONFIG% --target cpptest -- /m /v:m /logger:%MSBuildLogger%
on_failure: if exist "tests\test_cmake_build" type tests\test_cmake_build\*.log*
38 changes: 38 additions & 0 deletions python/pybind11/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# See all possible options and defaults with:
# clang-format --style=llvm --dump-config
BasedOnStyle: LLVM
AccessModifierOffset: -4
AllowShortLambdasOnASingleLine: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: false
BinPackParameters: false
BreakBeforeBinaryOperators: All
BreakConstructorInitializers: BeforeColon
ColumnLimit: 99
CommentPragmas: 'NOLINT:.*|^ IWYU pragma:'
IncludeBlocks: Regroup
IndentCaseLabels: true
IndentPPDirectives: AfterHash
IndentWidth: 4
Language: Cpp
SpaceAfterCStyleCast: true
Standard: Cpp11
StatementMacros: ['PyObject_HEAD']
TabWidth: 4
IncludeCategories:
- Regex: '<pybind11/.*'
Priority: -1
- Regex: 'pybind11.h"$'
Priority: 1
- Regex: '^".*/?detail/'
Priority: 1
SortPriority: 2
- Regex: '^"'
Priority: 1
SortPriority: 3
- Regex: '<[[:alnum:]._]+>'
Priority: 4
- Regex: '.*'
Priority: 5
...
75 changes: 75 additions & 0 deletions python/pybind11/.clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
FormatStyle: file

Checks: |
*bugprone*,
*performance*,
clang-analyzer-optin.cplusplus.VirtualCall,
clang-analyzer-optin.performance.Padding,
cppcoreguidelines-init-variables,
cppcoreguidelines-prefer-member-initializer,
cppcoreguidelines-pro-type-static-cast-downcast,
cppcoreguidelines-slicing,
google-explicit-constructor,
llvm-namespace-comment,
misc-definitions-in-headers,
misc-misplaced-const,
misc-non-copyable-objects,
misc-static-assert,
misc-throw-by-value-catch-by-reference,
misc-uniqueptr-reset-release,
misc-unused-parameters,
modernize-avoid-bind,
modernize-loop-convert,
modernize-make-shared,
modernize-redundant-void-arg,
modernize-replace-auto-ptr,
modernize-replace-disallow-copy-and-assign-macro,
modernize-replace-random-shuffle,
modernize-shrink-to-fit,
modernize-use-auto,
modernize-use-bool-literals,
modernize-use-default-member-init,
modernize-use-emplace,
modernize-use-equals-default,
modernize-use-equals-delete,
modernize-use-noexcept,
modernize-use-nullptr,
modernize-use-override,
modernize-use-using,
readability-avoid-const-params-in-decls,
readability-braces-around-statements,
readability-const-return-type,
readability-container-size-empty,
readability-delete-null-pointer,
readability-else-after-return,
readability-implicit-bool-conversion,
readability-inconsistent-declaration-parameter-name,
readability-make-member-function-const,
readability-misplaced-array-index,
readability-non-const-parameter,
readability-qualified-auto,
readability-redundant-function-ptr-dereference,
readability-redundant-smartptr-get,
readability-redundant-string-cstr,
readability-simplify-subscript-expr,
readability-static-accessed-through-instance,
readability-static-definition-in-anonymous-namespace,
readability-string-compare,
readability-suspicious-call-argument,
readability-uniqueptr-delete-release,
-bugprone-easily-swappable-parameters,
-bugprone-exception-escape,
-bugprone-reserved-identifier,
-bugprone-unused-raii,
CheckOptions:
- key: performance-for-range-copy.WarnOnAllAutoCopies
value: true
- key: performance-inefficient-string-concatenation.StrictMode
value: true
- key: performance-unnecessary-value-param.AllowedTypes
value: 'exception_ptr$;'
- key: readability-implicit-bool-conversion.AllowPointerConditions
value: true

HeaderFilterRegex: 'pybind11/.*h'
73 changes: 73 additions & 0 deletions python/pybind11/.cmake-format.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
parse:
additional_commands:
pybind11_add_module:
flags:
- THIN_LTO
- MODULE
- SHARED
- NO_EXTRAS
- EXCLUDE_FROM_ALL
- SYSTEM

format:
line_width: 99
tab_size: 2

# If an argument group contains more than this many sub-groups
# (parg or kwarg groups) then force it to a vertical layout.
max_subgroups_hwrap: 2

# If a positional argument group contains more than this many
# arguments, then force it to a vertical layout.
max_pargs_hwrap: 6

# If a cmdline positional group consumes more than this many
# lines without nesting, then invalidate the layout (and nest)
max_rows_cmdline: 2
separate_ctrl_name_with_space: false
separate_fn_name_with_space: false
dangle_parens: false

# If the trailing parenthesis must be 'dangled' on its on
# 'line, then align it to this reference: `prefix`: the start'
# 'of the statement, `prefix-indent`: the start of the'
# 'statement, plus one indentation level, `child`: align to'
# the column of the arguments
dangle_align: prefix
# If the statement spelling length (including space and
# parenthesis) is smaller than this amount, then force reject
# nested layouts.
min_prefix_chars: 4

# If the statement spelling length (including space and
# parenthesis) is larger than the tab width by more than this
# amount, then force reject un-nested layouts.
max_prefix_chars: 10

# If a candidate layout is wrapped horizontally but it exceeds
# this many lines, then reject the layout.
max_lines_hwrap: 2

line_ending: unix

# Format command names consistently as 'lower' or 'upper' case
command_case: canonical

# Format keywords consistently as 'lower' or 'upper' case
# unchanged is valid too
keyword_case: 'upper'

# A list of command names which should always be wrapped
always_wrap: []

# If true, the argument lists which are known to be sortable
# will be sorted lexicographically
enable_sort: true

# If true, the parsers may infer whether or not an argument
# list is sortable (without annotation).
autosort: false

# Causes a few issues - can be solved later, possibly.
markup:
enable_markup: false
1 change: 1 addition & 0 deletions python/pybind11/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
docs/*.svg binary
9 changes: 9 additions & 0 deletions python/pybind11/.github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*.cmake @henryiii
CMakeLists.txt @henryiii
*.yml @henryiii
*.yaml @henryiii
/tools/ @henryiii
/pybind11/ @henryiii
noxfile.py @henryiii
.clang-format @henryiii
.clang-tidy @henryiii
Loading

0 comments on commit 5d1b745

Please sign in to comment.