Skip to content

support android ndk #102

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion CMake/FindMINDSPORE.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mark_as_advanced(MINDSPORE_INCLUDE)

# Look for the library (sorted from most current/relevant entry to least).
find_library(MINDSPORE_LIBRARY NAMES
mindspore
NAMES mindspore mindspore-lite
Copy link
Contributor

Choose a reason for hiding this comment

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

mindspore和lite版本能混用吗?
是否应该要分开?

HINTS ${CMAKE_INSTALL_FULL_LIBDIR} ${HITS_MINDSPORE_PATH}/lib
)
mark_as_advanced(MINDSPORE_LIBRARY)
Expand Down
71 changes: 52 additions & 19 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,58 @@ set(RELEASE_PACKAGE_DIR ${CMAKE_BINARY_DIR}/release)
set(CUSTOM_LIBRARY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/libs)
set(MODELBOX_TOP_DIR ${CMAKE_CURRENT_SOURCE_DIR})

find_package(CPPREST)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system)
find_package(CUDA 10.0)
find_package(TENSORRT)
find_package(TENSORFLOW)
find_package(OBS)
find_package(DIS)
find_package(VCN)
find_package(NVCUVID)
find_package(FFMPEG)
find_package(ACL)
find_package(DSMI)
find_package(OpenCV)
find_package(DUKTAPE)
find_package(MINDSPORE)
find_package(FUSE)
find_package(JNI)
find_package(Java)
if (DEFINED ENV{USER_ROOT} AND ANDROID_ABI)
# For Android, CMAKE_FIND_ROOT_xxx is set to 'ONLY' by its cmake file.
# Thus find_package/find_library/find_path only searches files in sysroot path of NDK.
# But we provide thirdparty files in path USER_ROOT, we force cmake to search it now.
set(CMAKE_INCLUDE_PATH $ENV{USER_ROOT}/${ANDROID_ABI}/usr/include)
set(CMAKE_LIBRARY_PATH $ENV{USER_ROOT}/${ANDROID_ABI}/usr/lib)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE NO)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NO)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NO)

find_package(CPPREST)
find_package(Boost COMPONENTS system)

find_package(OpenSSL REQUIRED)
find_package(FFMPEG)

if (EXISTS $ENV{USER_ROOT}/${ANDROID_ABI}/usr/lib/cmake/opencv4/OpenCVConfig.cmake)
set(OpenCV_DIR $ENV{USER_ROOT}/${ANDROID_ABI}/usr/lib/cmake/opencv4)
find_package(OpenCV)
endif()

if (DEFINED ENV{MINDSPORE_PATH})
find_package(MINDSPORE)
endif()

# Resume the settings.
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_INCLUDE_PATH "")
set(CMAKE_LIBRARY_PATH "")
else()
find_package(CPPREST)
find_package(OpenSSL REQUIRED)
find_package(Boost COMPONENTS system)
find_package(CUDA 10.0)
find_package(TENSORRT)
find_package(TENSORFLOW)
find_package(OBS)
find_package(DIS)
find_package(VCN)
find_package(NVCUVID)
find_package(FFMPEG)
find_package(ACL)
find_package(DSMI)
find_package(OpenCV)
find_package(DUKTAPE)
find_package(MINDSPORE)
find_package(FUSE)
find_package(JNI)
find_package(Java)
endif()

add_subdirectory(thirdparty)
add_subdirectory(src)
Expand Down
8 changes: 8 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_BINARY_DIR})
message(FATAL_ERROR "Do not build in source directory!")
endif()

if (DEFINED ENV{USER_ROOT} AND ANDROID_ABI)
Copy link
Contributor

Choose a reason for hiding this comment

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

这个可以放到CMake/options里面。

include_directories($ENV{USER_ROOT}/${ANDROID_ABI}/usr/include)
link_directories($ENV{USER_ROOT}/${ANDROID_ABI}/usr/lib)

set(CMAKE_SHARED_LINKER_FLAGS "-Wl,--as-needed ${CMAKE_SHARED_LINKER_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed ${CMAKE_SHARED_LINKER_FLAGS}")
endif()

add_subdirectory(libmodelbox)
add_subdirectory(drivers)
add_subdirectory(develop)
Expand Down
1 change: 0 additions & 1 deletion src/drivers/devices/cpu/flowunit/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ target_link_libraries(${MODELBOX_UNIT_STATIC} PRIVATE pybind11::embed)
set_property(TARGET ${MODELBOX_UNIT_STATIC} PROPERTY POSITION_INDEPENDENT_CODE ON)

add_library(${MODELBOX_UNIT_SHARED} SHARED ${EMPTY_SOURCE_FILE})
target_link_libraries(${MODELBOX_UNIT_SHARED} ${MODELBOX_UNIT_STATIC} )
target_link_libraries(${MODELBOX_UNIT_SHARED} -Wl,--whole-archive ${MODELBOX_UNIT_STATIC} -Wl,--no-whole-archive)
set(LIBMODELBOX_FLOWUNIT_PYTHON_SHARED ${MODELBOX_UNIT_SHARED})
target_link_libraries(${MODELBOX_UNIT_SHARED} ${MODELBOX_COMMON_MODELBOX_API_LIBRARY})
Expand Down
5 changes: 3 additions & 2 deletions src/drivers/devices/cpu/flowunit/python/python_flowunit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "python_flowunit.h"

#include "modelbox/base/memory_stub.h"
#include "modelbox/device/cpu/device_cpu.h"

using namespace pybind11::literals;
Expand Down Expand Up @@ -44,7 +45,7 @@ void PythonFlowUnit::EnablePythonDebug() {

modelbox::Status PythonFlowUnit::Open(
const std::shared_ptr<modelbox::Configuration>& opts) {
python_desc_ = std::dynamic_pointer_cast<VirtualPythonFlowUnitDesc>(
python_desc_ = modelbox_dynamic_pointer_cast<VirtualPythonFlowUnitDesc>(
this->GetFlowUnitDesc());

auto python_entry = python_desc_->GetPythonEntry();
Expand Down Expand Up @@ -249,7 +250,7 @@ modelbox::Status PythonFlowUnit::Close() {

void PythonFlowUnit::SetFlowUnitDesc(
std::shared_ptr<modelbox::FlowUnitDesc> desc) {
python_desc_ = std::dynamic_pointer_cast<VirtualPythonFlowUnitDesc>(desc);
python_desc_ = modelbox_dynamic_pointer_cast<VirtualPythonFlowUnitDesc>(desc);
}

std::shared_ptr<modelbox::FlowUnitDesc> PythonFlowUnit::GetFlowUnitDesc() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,16 @@ Status FfmpegVideoMuxer::Mux(const AVRational &time_base,
av_packet_rescale_ts(av_packet.get(), time_base, stream_->time_base);
av_packet->stream_index = stream_->index;
if (!is_header_wrote_) {
#ifndef ANDROID

Choose a reason for hiding this comment

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

这里为什么 Android和其它有区别?

auto ret = avformat_write_header(format_ctx_.get(), nullptr);
#else
AVDictionary *options = nullptr;
av_dict_set(&options, "rtsp_transport", "tcp", 0);
av_dict_set(&options, "recv_buffer_size", "10240000", 0);
av_dict_set(&options, "stimeout", "2000000", 0);
auto ret = avformat_write_header(format_ctx_.get(), &options);
#endif

if (ret < 0) {
GET_FFMPEG_ERR(ret, ffmpeg_err);
MBLOG_ERROR << "avformat_write_header failed, ret " << ffmpeg_err;
Expand Down
9 changes: 5 additions & 4 deletions src/drivers/virtual/inference/virtualdriver_inference.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "virtualdriver_inference.h"
#include "modelbox/base/driver.h"
#include "modelbox/base/memory_stub.h"

using namespace modelbox;

Expand Down Expand Up @@ -363,7 +364,7 @@ void VirtualInferenceFlowUnitFactory::SetFlowUnitFactory(
bind_flowunit_factory_list) {
for (auto &bind_flowunit_factory : bind_flowunit_factory_list) {
bind_flowunit_factory_list_.push_back(
std::dynamic_pointer_cast<FlowUnitFactory>(bind_flowunit_factory));
modelbox_dynamic_pointer_cast<FlowUnitFactory>(bind_flowunit_factory));
}
}

Expand All @@ -372,17 +373,17 @@ VirtualInferenceFlowUnitFactory::VirtualCreateFlowUnit(
const std::string &unit_name, const std::string &unit_type,
const std::string &virtual_type) {
for (auto &flowunit_factory : bind_flowunit_factory_list_) {
if (std::dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
if (modelbox_dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
->GetFlowUnitFactoryType() != unit_type) {
continue;
}

if (std::dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
if (modelbox_dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
->GetVirtualType() != virtual_type) {
continue;
}

return std::dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
return modelbox_dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
->CreateFlowUnit(unit_name, unit_type);
}
StatusError = {STATUS_NOTFOUND, "Cannot found virtual flowunit " +
Expand Down
8 changes: 4 additions & 4 deletions src/drivers/virtual/python/virtualdriver_python.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#include "virtualdriver_python.h"

#include "modelbox/base/memory_stub.h"
#include <libgen.h>

using namespace modelbox;
Expand Down Expand Up @@ -386,19 +386,19 @@ void VirtualPythonFlowUnitFactory::SetFlowUnitFactory(
bind_flowunit_factory_list) {
for (auto &bind_flowunit_factory : bind_flowunit_factory_list) {
bind_flowunit_factory_list_.push_back(
std::dynamic_pointer_cast<FlowUnitFactory>(bind_flowunit_factory));
modelbox_dynamic_pointer_cast<FlowUnitFactory>(bind_flowunit_factory));
}
}

std::shared_ptr<modelbox::FlowUnit>
VirtualPythonFlowUnitFactory::CreateFlowUnit(const std::string &unit_name,
const std::string &unit_type) {
for (auto &flowunit_factory : bind_flowunit_factory_list_) {
if (std::dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
if (modelbox_dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
->GetFlowUnitFactoryType() != unit_type) {
continue;
}
return std::dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
return modelbox_dynamic_pointer_cast<FlowUnitFactory>(flowunit_factory)
->CreateFlowUnit(unit_name, unit_type);
}
return nullptr;
Expand Down
3 changes: 2 additions & 1 deletion src/libmodelbox/base/device/device_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "modelbox/base/driver.h"
#include "modelbox/base/log.h"
#include "modelbox/base/status.h"
#include "modelbox/base/memory_stub.h"

namespace modelbox {
DeviceManager::DeviceManager() {}
Expand Down Expand Up @@ -87,7 +88,7 @@ Status DeviceManager::InitDeviceFactory(std::shared_ptr<Drivers> driver) {
}
desc = device_driver->GetDriverDesc();
std::shared_ptr<DeviceFactory> device_factory =
std::dynamic_pointer_cast<DeviceFactory>(temp_factory);
modelbox_dynamic_pointer_cast<DeviceFactory>(temp_factory);

device_factory_.insert(std::make_pair(desc->GetType(), device_factory));
}
Expand Down
3 changes: 2 additions & 1 deletion src/libmodelbox/base/graph_manager/graph_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <modelbox/base/graph_manager.h>
#include <modelbox/base/memory_stub.h>

namespace modelbox {

Expand Down Expand Up @@ -397,7 +398,7 @@ Status GraphConfigManager::InitGraphConfigFactory(
}

std::shared_ptr<GraphConfigFactory> graph_conf_factory =
std::dynamic_pointer_cast<GraphConfigFactory>(temp_factory);
modelbox_dynamic_pointer_cast<GraphConfigFactory>(temp_factory);

graph_conf_factories_.insert(std::make_pair(
graph_conf_factory->GetGraphConfFactoryType(), graph_conf_factory));
Expand Down
9 changes: 9 additions & 0 deletions src/libmodelbox/base/include/modelbox/base/any.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ class Collection {
return false;
}

#ifdef ANDROID
/* When they come from different so files, they may have different hash
* codes even if they have the same names. */
if (strcmp(typeid(T).name(), entrys_[key].type().name()) == 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

strncmp

value = any_cast<T>(entrys_[key]);
return true;
}
#endif

if (!CanConvert(typeid(T).hash_code(), entrys_[key].type().hash_code())) {
// always a bad condition
MBLOG_ERROR << "Get value for " << key
Expand Down
4 changes: 4 additions & 0 deletions src/libmodelbox/base/include/modelbox/base/driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ namespace modelbox {
constexpr const char *DRIVER_CLASS_VIRTUAL = "DRIVER-VIRTUAL";
constexpr const char *DRIVER_CLASS_INFERENCE = "DRIVER-INFERENCE";
constexpr const char *DRIVER_TYPE_VIRTUAL = "virtual";
#ifdef ANDROID
constexpr const char *DEFAULT_SCAN_INFO = "./tmp/modelbox-driver-info";
#else
constexpr const char *DEFAULT_SCAN_INFO = "/tmp/modelbox-driver-info";
#endif
constexpr const char *DEFAULT_LD_CACHE = "/etc/ld.so.cache";

class Driver;
Expand Down
55 changes: 55 additions & 0 deletions src/libmodelbox/base/include/modelbox/base/memory_stub.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2021 The Modelbox Project Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef MODELBOX_MEMORY_STUB_H_
#define MODELBOX_MEMORY_STUB_H_

#include <memory>

#ifndef ANDROID

#define modelbox_dynamic_pointer_cast std::dynamic_pointer_cast

#else /* ANDROID */

#define modelbox_dynamic_pointer_cast modelbox::dynamic_pointer_cast

namespace modelbox {

/* LLVM __dynamic_cast calls is_equal() with use_strcmp=false,
* so the string names are not compared. It will return NULL
* if the object is allocated in one so file and dynamicly casted
* in another so file. */
template<class _Tp, class _Up>
inline _LIBCPP_INLINE_VISIBILITY
typename std::enable_if
<
!std::is_array<_Tp>::value && !std::is_array<_Up>::value,
Copy link
Contributor

Choose a reason for hiding this comment

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

vscode里面格式化代码

std::shared_ptr<_Tp>
>::type
dynamic_pointer_cast(const std::shared_ptr<_Up>& __r) _NOEXCEPT
{
auto __p = std::dynamic_pointer_cast<_Tp>(__r);
if (__p) {
return __p;
}
return std::static_pointer_cast<_Tp>(__r);
}

} // namespace modelbox
#endif /* ANDROID */

#endif /* MODELBOX_MEMORY_STUB_H_ */
Loading