Skip to content

Commit

Permalink
build:support android ndk
Browse files Browse the repository at this point in the history
  • Loading branch information
lzx1413 committed Aug 31, 2023
1 parent 96258a8 commit a8a736c
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 26 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/linux-x86-cpu-gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ jobs:
conan profile detect --force
- name: build
run: |
mkdir build
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
8 changes: 6 additions & 2 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 Expand Up @@ -119,8 +123,8 @@ if(BUILD_PROTO_FILES)
set_source_files_properties(${FILE_PATH}${FIL_WE}.pb.h PROPERTIES GENERATED True)
# message("Copying " ${PROTO_SRCS} " to " ${FILE_PATH})

file(COPY ${PROTO_SRCS} DESTINATION ${FILE_PATH})
file(COPY ${PROTO_HDRS} DESTINATION ${FILE_PATH})
#file(COPY ${PROTO_SRCS} DESTINATION ${FILE_PATH})
#file(COPY ${PROTO_HDRS} DESTINATION ${FILE_PATH})

ENDFOREACH()
endif()
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, Linux, [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
10 changes: 6 additions & 4 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ 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/*"].highgui=False
@property
def _min_cppstd(self):
return "14"
Expand All @@ -62,15 +64,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 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}
2 changes: 2 additions & 0 deletions docker/x86_gcc_profile
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ compiler.cppstd=17
compiler.libcxx=libstdc++11
compiler.version=9
os=Linux
[conf]
tools.system.package_manager:mode=install
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

0 comments on commit a8a736c

Please sign in to comment.