Giter VIP home page Giter VIP logo

ailia_yolox_rust's People

Contributors

bokutotu avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

ailia_yolox_rust's Issues

iOS Build

iOSビルドの概要

ailia_yolox_rustはWindows、macOSでビルド可能であるが、そのままではiOSでビルドができない。
これは、下記の要因である。

  • iOSにおいてailiaをDynamic Link LibraryではなくStatic Link Libraryとして使用する必要がある
  • rustでコマンドラインアプリを作成できず、Static Link Libraryをビルドする必要がある
  • rustはuniversal binaryを扱えないため、libailia.aを分解する必要がある
  • ailiaの使用するライブラリを明示的にリンクする必要がある

そのため、iOSでビルドするには下記のブランチを使用する必要がある。

#3

以降、このブランチに加えられた変更の解説である。

iOSビルドの環境構築

クロスコンパイルのターゲットを追加。

rustup target add aarch64-apple-ios

追加しない場合はonce_cellでcan't find crate for coreが発生する。
rust-lang/rust#101640

iOSビルド

下記のコマンドでビルド。

export AILIA_INC_DIR=/Users/kyakuno/Desktop/ailia_release/ailia_1_215_0/library/include
export AILIA_BIN_DIR=/Users/kyakuno/Desktop/ailia_release/ailia_1_215_0/library/ios
cargo build --target aarch64-apple-ios

しかし、いくつかのエラーが発生するので、下記の対応が必要。

リンクエラー対策

DylibをStaticに変更

下記のリンクエラーが発生する。

ld: library not found for -lailia_pose_estimate

build.rsをdylibからstaticに書き換え。

println!("cargo:rustc-link-lib=static=ailia");

fat binaryだとfailed to add native library file too small to be an archiveが発生する。
https://www.reddit.com/r/rust/comments/aae6x8/rust_ios_and_linking_against_fat_native_libraries/

下記のコマンドでarm64だけ抽出。

lipo libailia.a -thin arm64 -output libailia-arm64.a

抽出したファイルのみをリンク。

println!("cargo:rustc-link-lib=static=ailia-arm64");

Frameworkをリンク

"_cblas_sgemv"のsymbol(s) not found for architecture arm64が発生する。

ailiaが要求するAccelerate.frameworkとMetalPerformanceShaders.framework、Metal.frameworkをビルドコマンドに追加。

cargo --config 'build.rustflags="-l framework=Accelerate -l framework=MetalPerformanceShaders -l framework=Metal"' build --target aarch64-apple-ios

libclang_rtをリンク

___isPlatformVersionAtLeastが見つからない。
KhronosGroup/MoltenVK#913
https://trac.macports.org/ticket/64088

dead_stripによって消えてしまっているので、これをどうにかする必要がありそう。

cargo --config 'build.rustflags="-l framework=Accelerate -l framework=MetalPerformanceShaders -l framework=Metal -C link-arg=-Wl,no_dead_strip_inits_and_terms"' build --target aarch64-apple-ios

___isPlatformVersionAtLeast自体は
 /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/
のlibclang_rt.ios.aに存在する。
alexcrichton/curl-rust#279

libclang_rt.ios.aをlibailia.aと同じフォルダにコピーして分解する。

lipo libclang_rt.ios.a -thin arm64 -output libclang_rt-arm64.ios.a

build.rsにclang_rt-arm64.iosのリンクを追加。

    println!("cargo:rustc-link-search=native={}", ailia_path);
    println!("cargo:rustc-link-lib=static=ailia-arm64");
    println!("cargo:rustc-link-lib=static=clang_rt-arm64.ios");
    println!("cargo:rerun-if-changed=wrapper.h");

libcのリンク

下記のエラーが出る。

std::runtime_error::what() const, referenced from:
                vtable for boost::wrapexcept<boost::property_tree::ptree_bad_path> in libailia_sys-ac595caaf93b5a9d.rlib[315](BoostPTreeAdapter.cpp.o)
                vtable for boost::property_tree::ptree_bad_path in libailia_sys-ac595caaf93b5a9d.rlib[315](BoostPTreeAdapter.cpp.o)
                vtable for boost::property_tree::ptree_error in libailia_sys-ac595caaf93b5a9d.rlib[315](BoostPTreeAdapter.cpp.o)
                vtable for boost::wrapexcept<boost::property_tree::ptree_bad_data> in libailia_sys-ac595caaf93b5a9d.rlib[315](BoostPTreeAdapter.cpp.o)
                vtable for boost::property_tree::ptree_bad_data in libailia_sys-ac595caaf93b5a9d.rlib[315](BoostPTreeAdapter.cpp.o)

build.rsに下記の追加が必要。

println!("cargo:rustc-link-lib=c++");

これでOpenCV以外は通る。

必要手順まとめ

事前準備

lipo libailia.a -thin arm64 -output libailia-arm64.a
cp /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libclang_rt.ios.a ./libclang_rt.ios.a
lipo libclang_rt.ios.a -thin arm64 -output libclang_rt-arm64.ios.a

build.rsの書き換え

    println!("cargo:rustc-link-search=native={}", ailia_path);
    println!("cargo:rustc-link-lib=static=ailia-arm64");
    println!("cargo:rustc-link-lib=static=clang_rt-arm64.ios");
    println!("cargo:rerun-if-changed=wrapper.h");
    println!("cargo:rustc-link-lib=c++");

ビルド

export AILIA_INC_DIR=/Users/kyakuno/Desktop/ailia_release/ailia_1_215_0/library/include
export AILIA_BIN_DIR=/Users/kyakuno/Desktop/ailia_release/ailia_1_215_0/library/ios
cargo --config 'build.rustflags="-l framework=Accelerate -l framework=MetalPerformanceShaders -l framework=Metal"' build --target aarch64-apple-ios

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.