Skip to content

Commit

Permalink
Merge pull request #17 from liufang-robot/master
Browse files Browse the repository at this point in the history
添加dotnet的支持
  • Loading branch information
liufang-robot authored Dec 26, 2022
2 parents c0db4fe + 671f88f commit 4777ebf
Show file tree
Hide file tree
Showing 20 changed files with 993 additions and 4 deletions.
18 changes: 18 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ message(STATUS "Build C++ library: ${BUILD_CXX}")
option(BUILD_PYTHON "Build Python Library" OFF)
message(STATUS "Build Python: ${BUILD_PYTHON}")

option(BUILD_DOTNET "Build .NET Library" OFF)
message(STATUS "Build .Net: ${BUILD_DOTNET}")

if(BUILD_PYTHON)
if(DEFINED PYTHONPATH)
message(STATUS "Python path: ${PYTHONPATH}")
Expand All @@ -98,6 +101,21 @@ endif()
include(python)


if(BUILD_DOTNET)
# .Net Core 3.1 LTS is not available for osx arm64
if(APPLE AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(USE_DOTNET_CORE_31 OFF)
else()
option(USE_DOTNET_CORE_31 "Use .Net Core 3.1 LTS support" ON)
endif()
message(STATUS ".Net: Use .Net Core 3.1 LTS support: ${USE_DOTNET_CORE_31}")

option(USE_DOTNET_6 "Use .Net 6.0 LTS support" ON)
message(STATUS ".Net: Use .Net 6.0 LTS support: ${USE_DOTNET_6}")
endif()
include(dotnet)


# add_subdirectory(tests)

option(BUILD_EXAMPLES "Build examples" ON)
Expand Down
7 changes: 6 additions & 1 deletion CMakeSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@
},
{
"name": "BUILD_PYTHON",
"value": "True",
"value": "False",
"type": "BOOL"
},
{
"name": "BUILD_TESTING",
"value": "True",
"type": "BOOL"
},
{
"name": "BUILD_DOTNET",
"value": "True",
"type": "BOOL"
}
]
}
Expand Down
2 changes: 1 addition & 1 deletion Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "lebai sdk"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 1.0.9
PROJECT_NUMBER = 1.0.10

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ lebai-sdk的源代码仓库,可以用于控制乐白机械臂。

[SDK在线文档](http://help.lebai.ltd/sdk/)

# 包管理直接安装

# 从源代码构建安装
python开发可以直接从[PyPI](https://pypi.org/project/pylebai/)安装。
```
pip install pylebai
```

# 从源代码构建安装

## Ubuntu&&Debian平台
### 依赖
Expand Down Expand Up @@ -56,6 +61,7 @@ cpack

- BUILD_PYTHON: 是否编译python接口 默认为OFF
- PYTHONPATH: PYTHON的安装目录
- BUILD_DOTNET: 是否编译.net接口 默认为OFF(请先安装好dotnet的开发环境)
- BUILD_TESTING: 是否编译C++测试程序 默认为ON
- TEST_ROBOT_IP: 测试程序的机器人IP地址,正确的设置该值用于单元测试 默认为127.0.0.1
- BUILD_EXAMPLES: 是否编译示例程序 默认为ON
Expand Down
313 changes: 313 additions & 0 deletions cmake/dotnet.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,313 @@
if(NOT BUILD_DOTNET)
return()
endif()

# Will need swig
set(CMAKE_SWIG_FLAGS)
find_package(SWIG REQUIRED)
include(UseSWIG)

#if(${SWIG_VERSION} VERSION_GREATER_EQUAL 4)
# list(APPEND CMAKE_SWIG_FLAGS "-doxygen")
#endif()

if(UNIX AND NOT APPLE)
list(APPEND CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
endif()

# Find dotnet cli
find_program(DOTNET_EXECUTABLE NAMES dotnet)
if(NOT DOTNET_EXECUTABLE)
message(FATAL_ERROR "Check for dotnet Program: not found")
else()
message(STATUS "Found dotnet Program: ${DOTNET_EXECUTABLE}")
endif()

# Needed by dotnet/CMakeLists.txt
set(DOTNET_PACKAGE lebai)
set(DOTNET_PACKAGES_DIR "${PROJECT_BINARY_DIR}/dotnet/packages")

# see: https://docs.microsoft.com/en-us/dotnet/core/rid-catalog
if(APPLE)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(RUNTIME_IDENTIFIER osx-arm64)
else()
set(RUNTIME_IDENTIFIER osx-x64)
endif()
elseif(UNIX)
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(RUNTIME_IDENTIFIER linux-arm64)
else()
set(RUNTIME_IDENTIFIER linux-x64)
endif()
elseif(WIN32)
set(RUNTIME_IDENTIFIER win-x64)
else()
message(FATAL_ERROR "Unsupported system !")
endif()
set(DOTNET_NATIVE_PROJECT ${DOTNET_PACKAGE}.runtime.${RUNTIME_IDENTIFIER})
message(STATUS ".Net runtime project: ${DOTNET_NATIVE_PROJECT}")
set(DOTNET_NATIVE_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_NATIVE_PROJECT})
message(STATUS ".Net runtime project build path: ${DOTNET_NATIVE_PROJECT_DIR}")

# see: Platform
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64)")
set(DOTNET_PLATFORM arm64)
else()
set(DOTNET_PLATFORM x64)
endif()

# see: https://docs.microsoft.com/en-us/dotnet/standard/frameworks
if(USE_DOTNET_CORE_31 AND USE_DOTNET_6)
set(DOTNET_TFM "<TargetFrameworks>netcoreapp3.1;net6.0</TargetFrameworks>")
elseif(USE_DOTNET_6)
set(DOTNET_TFM "<TargetFramework>net6.0</TargetFramework>")
elseif(USE_DOTNET_CORE_31)
set(DOTNET_TFM "<TargetFramework>netcoreapp3.1</TargetFramework>")
else()
message(FATAL_ERROR "No .Net SDK selected !")
endif()

set(DOTNET_PROJECT ${DOTNET_PACKAGE})
message(STATUS ".Net project: ${DOTNET_PROJECT}")
set(DOTNET_PROJECT_DIR ${PROJECT_BINARY_DIR}/dotnet/${DOTNET_PROJECT})
message(STATUS ".Net project build path: ${DOTNET_PROJECT_DIR}")

# Create the native library
add_library(lebai-native SHARED "")
set_target_properties(lebai-native PROPERTIES
PREFIX ""
POSITION_INDEPENDENT_CODE ON)
# note: macOS is APPLE and also UNIX !
if(APPLE)
set_target_properties(lebai-native PROPERTIES INSTALL_RPATH "@loader_path")
# Xcode fails to build if library doesn't contains at least one source file.
if(XCODE)
file(GENERATE
OUTPUT ${PROJECT_BINARY_DIR}/lebai-native/version.cpp
CONTENT "namespace {char* version = \"${PROJECT_VERSION}\";}")
target_sources(lebai-native PRIVATE ${PROJECT_BINARY_DIR}/lebai-native/version.cpp)
endif()
elseif(UNIX)
set_target_properties(lebai-native PROPERTIES INSTALL_RPATH "$ORIGIN")
endif()

list(APPEND CMAKE_SWIG_FLAGS ${FLAGS} "-I${PROJECT_SOURCE_DIR}")

add_subdirectory(sdk/dotnet)
target_link_libraries(lebai-native PRIVATE dotnet_l_master dotnet_zeroconf)

file(COPY ${PROJECT_SOURCE_DIR}/dotnet/logo.png DESTINATION ${PROJECT_BINARY_DIR}/dotnet)
set(DOTNET_LOGO_DIR "${PROJECT_BINARY_DIR}/dotnet")
configure_file(${PROJECT_SOURCE_DIR}/dotnet/Directory.Build.props.in ${PROJECT_BINARY_DIR}/dotnet/Directory.Build.props)

file(MAKE_DIRECTORY ${DOTNET_PACKAGES_DIR})
############################
## .Net Runtime Package ##
############################
# *.csproj.in contains:
# CMake variable(s) (@PROJECT_NAME@) that configure_file() can manage and
# generator expression ($<TARGET_FILE:...>) that file(GENERATE) can manage.
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PACKAGE}.runtime.csproj.in
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj.in
@ONLY)
file(GENERATE
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in
INPUT ${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj.in)

add_custom_command(
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E copy ./$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in ${DOTNET_NATIVE_PROJECT}.csproj
DEPENDS
${DOTNET_NATIVE_PROJECT_DIR}/$<CONFIG>/${DOTNET_NATIVE_PROJECT}.csproj.in
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})

add_custom_command(
OUTPUT ${DOTNET_NATIVE_PROJECT_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} build -c Release /p:Platform=${DOTNET_PLATFORM} ${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} pack -c Release ${DOTNET_NATIVE_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_NATIVE_PROJECT_DIR}/timestamp
DEPENDS
${PROJECT_BINARY_DIR}/dotnet/Directory.Build.props
${DOTNET_NATIVE_PROJECT_DIR}/${DOTNET_NATIVE_PROJECT}.csproj
lebai-native
BYPRODUCTS
${DOTNET_NATIVE_PROJECT_DIR}/bin
${DOTNET_NATIVE_PROJECT_DIR}/obj
VERBATIM
COMMENT "Generate .Net native package ${DOTNET_NATIVE_PROJECT} (${DOTNET_NATIVE_PROJECT_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})

add_custom_target(dotnet_native_package
DEPENDS
${DOTNET_NATIVE_PROJECT_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_NATIVE_PROJECT_DIR})

####################
## .Net Package ##
####################
configure_file(
${PROJECT_SOURCE_DIR}/dotnet/${DOTNET_PROJECT}.csproj.in
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj.in
@ONLY)

add_custom_command(
OUTPUT ${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E copy ${DOTNET_PROJECT}.csproj.in ${DOTNET_PROJECT}.csproj
DEPENDS
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj.in
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})

add_custom_command(
OUTPUT ${DOTNET_PROJECT_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} build -c Release /p:Platform=${DOTNET_PLATFORM} ${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} pack -c Release ${DOTNET_PROJECT}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_PROJECT_DIR}/timestamp
DEPENDS
${DOTNET_PROJECT_DIR}/${DOTNET_PROJECT}.csproj
dotnet_native_package
BYPRODUCTS
${DOTNET_PROJECT_DIR}/bin
${DOTNET_PROJECT_DIR}/obj
VERBATIM
COMMENT "Generate .Net package ${DOTNET_PROJECT} (${DOTNET_PROJECT_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})

add_custom_target(dotnet_package ALL
DEPENDS
${DOTNET_PROJECT_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_PROJECT_DIR})

# #################
# ## .Net Test ##
# #################
# # add_dotnet_test()
# # CMake function to generate and build dotnet test.
# # Parameters:
# # the dotnet filename
# # e.g.:
# # add_dotnet_test(FooTests.cs)
# function(add_dotnet_test FILE_NAME)
# message(STATUS "Configuring test ${FILE_NAME} ...")
# get_filename_component(TEST_NAME ${FILE_NAME} NAME_WE)
# get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
# get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)

# set(DOTNET_TEST_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${TEST_NAME})
# message(STATUS "build path: ${DOTNET_TEST_DIR}")

# configure_file(
# ${PROJECT_SOURCE_DIR}/dotnet/Test.csproj.in
# ${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
# @ONLY)

# add_custom_command(
# OUTPUT ${DOTNET_TEST_DIR}/${TEST_NAME}.cs
# COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_TEST_DIR}
# COMMAND ${CMAKE_COMMAND} -E copy
# ${FILE_NAME}
# ${DOTNET_TEST_DIR}/
# MAIN_DEPENDENCY ${FILE_NAME}
# VERBATIM
# WORKING_DIRECTORY ${DOTNET_TEST_DIR})

# add_custom_command(
# OUTPUT ${DOTNET_TEST_DIR}/timestamp
# COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} build -c Release ${TEST_NAME}.csproj
# COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_TEST_DIR}/timestamp
# DEPENDS
# ${DOTNET_TEST_DIR}/${TEST_NAME}.csproj
# ${DOTNET_TEST_DIR}/${TEST_NAME}.cs
# dotnet_package
# BYPRODUCTS
# ${DOTNET_TEST_DIR}/bin
# ${DOTNET_TEST_DIR}/obj
# VERBATIM
# COMMENT "Compiling .Net ${COMPONENT_NAME}/${TEST_NAME}.cs (${DOTNET_TEST_DIR}/timestamp)"
# WORKING_DIRECTORY ${DOTNET_TEST_DIR})

# add_custom_target(dotnet_${COMPONENT_NAME}_${TEST_NAME} ALL
# DEPENDS
# ${DOTNET_TEST_DIR}/timestamp
# WORKING_DIRECTORY ${DOTNET_TEST_DIR})

# if(BUILD_TESTING)
# add_test(
# NAME dotnet_${COMPONENT_NAME}_${TEST_NAME}
# COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} test --no-build -c Release ${TEST_NAME}.csproj
# WORKING_DIRECTORY ${DOTNET_TEST_DIR})
# endif()
# message(STATUS "Configuring test ${FILE_NAME} done")
# endfunction()

####################
## .Net Example ##
####################
# add_dotnet_example()
# CMake function to generate and build dotnet example.
# Parameters:
# the dotnet filename
# e.g.:
# add_dotnet_example(Foo.cs)
function(add_dotnet_example FILE_NAME)
message(STATUS "Configuring example ${FILE_NAME} ...")
get_filename_component(EXAMPLE_NAME ${FILE_NAME} NAME_WE)
get_filename_component(COMPONENT_DIR ${FILE_NAME} DIRECTORY)
get_filename_component(COMPONENT_NAME ${COMPONENT_DIR} NAME)

set(DOTNET_EXAMPLE_DIR ${PROJECT_BINARY_DIR}/dotnet/${COMPONENT_NAME}/${EXAMPLE_NAME})
message(STATUS "build path: ${DOTNET_EXAMPLE_DIR}")

configure_file(
${PROJECT_SOURCE_DIR}/dotnet/Example.csproj.in
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.csproj
@ONLY)

add_custom_command(
OUTPUT ${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.cs
COMMAND ${CMAKE_COMMAND} -E make_directory ${DOTNET_EXAMPLE_DIR}
COMMAND ${CMAKE_COMMAND} -E copy
${FILE_NAME}
${DOTNET_EXAMPLE_DIR}/
MAIN_DEPENDENCY ${FILE_NAME}
VERBATIM
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})

add_custom_command(
OUTPUT ${DOTNET_EXAMPLE_DIR}/timestamp
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} build -c Release ${EXAMPLE_NAME}.csproj
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} pack -c Release ${EXAMPLE_NAME}.csproj
COMMAND ${CMAKE_COMMAND} -E touch ${DOTNET_EXAMPLE_DIR}/timestamp
DEPENDS
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.csproj
${DOTNET_EXAMPLE_DIR}/${EXAMPLE_NAME}.cs
dotnet_package
BYPRODUCTS
${DOTNET_EXAMPLE_DIR}/bin
${DOTNET_EXAMPLE_DIR}/obj
VERBATIM
COMMENT "Compiling .Net ${COMPONENT_NAME}/${EXAMPLE_NAME}.cs (${DOTNET_EXAMPLE_DIR}/timestamp)"
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})

add_custom_target(dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME} ALL
DEPENDS
${DOTNET_EXAMPLE_DIR}/timestamp
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})

if(BUILD_TESTING)
if(USE_DOTNET_CORE_31)
add_test(
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_netcoreapp31
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} run --no-build --framework netcoreapp3.1 -c Release ${EXAMPLE_NAME}.csproj
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
endif()
if(USE_DOTNET_6)
add_test(
NAME dotnet_${COMPONENT_NAME}_${EXAMPLE_NAME}_net60
COMMAND ${CMAKE_COMMAND} -E env --unset=TARGETNAME ${DOTNET_EXECUTABLE} run --no-build --framework net6.0 -c Release ${EXAMPLE_NAME}.csproj
WORKING_DIRECTORY ${DOTNET_EXAMPLE_DIR})
endif()
endif()
message(STATUS "Configuring example ${FILE_NAME} done")
endfunction()
Loading

0 comments on commit 4777ebf

Please sign in to comment.