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

build:support android ndk #6

Merged
merged 2 commits into from
Aug 31, 2023
Merged
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
8 changes: 4 additions & 4 deletions .github/workflows/linux-x86-cpu-gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ jobs:
- name: update
run: sudo apt update
- name: install_dep
run: sudo apt install libgtk2.0-dev
run: sudo apt install libgtk2.0-dev libva-dev
- name: install_conan
run: |
pip3 install conan
conan profile detect --force
- name: build
run: |
mkdir build
conan graph info .
conan install . --build=missing -pr:h=docker/x86_gcc_profile
cd build
cd build_Linux
cmake ..
make -j 2
- name: test
run: cd build && make test
run: cd build_Linux && make test
5 changes: 2 additions & 3 deletions .github/workflows/macos-x86-cpu-clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,9 @@ jobs:
- name: build
run: |
rm /usr/local/Cellar/cmake/3.27.3/share/cmake/Modules/Findprotobuf.cmake
mkdir build
conan install . --build=missing
cd build
cd build_Macos
cmake ..
make -j 2
- name: test
run: cd build && make test
run: cd build_Macos && make test
5 changes: 2 additions & 3 deletions .github/workflows/windows-x86-cpu-msvc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ jobs:
- name: build
run: |
rm 'C:/Program Files/CMake/share/cmake-3.27/Modules/FindProtobuf.cmake'
mkdir build
conan install . --build=missing -pr:h=docker/x86_msvc_profile
cd build
cd build_Windows
cmake ..
cmake --build . --config Release
- name: test
run: cd build && ctest
run: cd build_Windows && ctest
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ option(BUILD_PYTHON "build python bindings" OFF)
option(BUILD_GRAPH_ONLY "only build graph module" OFF)
option(BUILD_EXAMPLES "build examples " ON)
option(ENABLE_RTTI "enable rtti " ON)
if (ANDROID)
include_directories($ENV{ANDROID_NDK_HOME}/sources/android)
add_definitions(-DMEDIAPIPE_MOBILE)
endif()
if(USE_CCACHE AND NOT MSVC)
find_program(CCACHE_EXE ccache)
if(CCACHE_EXE )
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

Builds the mediapipe graph module using cmake, conan, and allows conan packaging for various graphing tasks.

## Change Log
* 2023/08/30 supports building on [Mac](.github/workflows/macos-x86-cpu-clang.yml), [Linux](.github/workflows/linux-x86-cpu-gcc.yml), [Windows](doc/build_with_msvc.md), [Android](doc/build_with_ndk.md)

## Quick Start
1. Build the Docker image
```bash
Expand Down Expand Up @@ -145,10 +148,8 @@ I20230721 17:08:09.525879 140015 hello_world.cc:57] Hello World!
I20230721 17:08:09.525885 140015 hello_world.cc:57] Hello World!
I20230721 17:08:09.525892 140015 hello_world.cc:57] Hello World!
```
Building with MSVC on windows, please refer to [BuildWithMSVC](./doc/build_with_msvc.md)
## TODO List
* [ ] Clean up redundant code step by step
* [ ] Use docker/conan to support multi-platform compilation.
* [ ] Add doc, CI, CD, CT, code formatting checking and other related processes.
* [ ] Improve tutorials and code examples
* [ ] Add pipieline test benchmark.
Expand Down
19 changes: 15 additions & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ def config_options(self):
del self.options.fPIC

def configure(self):
self.options["glog"].with_unwind=False
if self.options.shared:
self.options.rm_safe("fPIC")
self.options['glog'].with_unwind = False
self.options["opencv/*"].with_gtk = False
self.options["opencv/*"].with_vulkan = False
self.options["opencv/*"].with_ffmpeg = False
self.options["opencv/*"].gapi = False
self.options["opencv/*"].objdetect = False
self.options["opencv/*"].photo = False
self.options["opencv/*"].dnn = False
self.options["opencv/*"].optflow = True
self.options["opencv/*"].ximgproc = True
@property
def _min_cppstd(self):
return "14"
Expand All @@ -62,15 +71,15 @@ def validate(self):

def layout(self):
cmake_layout(self)
self.folders.generators = 'build'
self.folders.generators = f'build_{self.settings.os}'

def requirements(self):
self.requires("abseil/20230125.1", visible=True)
self.requires("protobuf/3.17.1", visible=True)
self.requires("protobuf/3.21.12", visible=True)
self.requires("glog/0.5.0", visible=True)
self.test_requires("gtest/1.13.0")
self.test_requires("zlib/1.2.13")
self.test_requires("opencv/3.4.12")
self.test_requires("opencv/4.5.5")
self.test_requires("tensorflow-lite/2.10.0")
self.test_requires("cpuinfo/cci.20220228")
self.test_requires("pybind11/2.10.1")
Expand All @@ -86,6 +95,8 @@ def generate(self):
tc.variables["BUILD_PYTHON"] = False
tc.variables["BUILD_EXAMPLES"] = False
tc.variables['BUILD_GRAPH_ONLY'] = True
if self.settings.os == 'Android':
tc.variables['BUILD_PROTO_FILES'] = False
if self.options.enable_rtti:
tc.variables['ENABLE_RTTI'] = True
else:
Expand Down
32 changes: 32 additions & 0 deletions doc/build_with_ndk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
## Build with NDK for android

1. setup ndk
* download and unzip android ndk package (we have tested with ndk v22b)
* set 'export ANDROID_NDK_HOME=Your NDK root dir' to your .bashrc or .zshrc
2. cross-build
* generate protobuf cpp files with your host profile(Linux, Mac or Windows)
* `cp mediapipe/framework/stream_handler/default_input_stream_handler.pb.cc test_package`
* `conan create . -pr docker/ndk_profile --build=missing`
3. test
* copy the generated 'example' file in test_package to you android deivce's '/data/local/tmp'
* just run it and the results will be like bellow:
``` bash
c2q:/data/local/tmp $ ./example
I20230831 13:39:44.527252 16470 example.cc:39] node {
calculator: "SquareIntCalculator"
input_stream: "in"
output_stream: "out"
}
input_stream: "in"
output_stream: "out"
I20230831 13:39:44.532116 16470 example.cc:55] 0
I20230831 13:39:44.532189 16470 example.cc:55] 1
I20230831 13:39:44.532212 16470 example.cc:55] 4
I20230831 13:39:44.532227 16470 example.cc:55] 9
I20230831 13:39:44.532239 16470 example.cc:55] 16
I20230831 13:39:44.532253 16470 example.cc:55] 25
I20230831 13:39:44.532266 16470 example.cc:55] 36
I20230831 13:39:44.532279 16470 example.cc:55] 49
I20230831 13:39:44.532290 16470 example.cc:55] 64
I20230831 13:39:44.532302 16470 example.cc:55] 81
```
6 changes: 1 addition & 5 deletions docker/ndk_profile
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ compiler=clang
compiler.libcxx=c++_static
compiler.cppstd=17
compiler.version=12
[tool_requires]
[options]
[env]
CONAN_CMAKE_TOOLCHAIN_FILE=/opt/android-ndk/build/cmake/android.toolchain.cmake
[conf]
tools.system.package_manager:mode=install
tools.android:ndk_path=/opt/android-ndk
tools.android:ndk_path=$ENV{ANDROID_NDK_HOME}
7 changes: 5 additions & 2 deletions mediapipe/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(graph

${CMAKE_CURRENT_SOURCE_DIR}/timestamp.cc
${CMAKE_CURRENT_SOURCE_DIR}/basic_types_registration.cc
${CMAKE_CURRENT_SOURCE_DIR}/calculator.pb.cc
Expand Down Expand Up @@ -63,8 +62,12 @@ ${CMAKE_CURRENT_SOURCE_DIR}/tool/packet_generator_wrapper_calculator.pb.cc
${CMAKE_CURRENT_SOURCE_DIR}/tool/simulation_clock.cc
${CMAKE_CURRENT_SOURCE_DIR}/tool/simulation_clock_executor.cc
${PROJECT_SOURCE_DIR}/mediapipe/util/cpu_util.cc
#/opt/android-ndk/sources/android/cpufeatures/cpu-features.c
)
if (ANDROID)
target_sources(graph PRIVATE
$ENV{ANDROID_NDK_HOME}/sources/android/cpufeatures/cpu-features.c
)
endif()
add_subdirectory(api2)
add_subdirectory(deps)
if(ENABLE_PROFILER)
Expand Down
2 changes: 1 addition & 1 deletion mediapipe/util/cpu_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <cmath>

#ifdef __ANDROID__
#include "/opt/android-ndk/sources/android/cpufeatures/cpu-features.h"
#include "cpufeatures/cpu-features.h"
#elif _WIN32
#include <windows.h>
#else
Expand Down
2 changes: 1 addition & 1 deletion test_package/conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def generate(self):
def requirements(self):
self.requires(self.tested_reference_str)
self.requires("abseil/20230125.1", visible=True)
self.requires("protobuf/3.17.1", visible=True)
self.requires("protobuf/3.21.12", visible=True)
self.requires("glog/0.5.0", visible=True)

def build(self):
Expand Down
Loading