Skip to content

Commit 09dc0c7

Browse files
char5742qryxipHiroshiba
authored
arm64-android対応 (#17)
* feat: Android対応 * bind, test用のスクリプトを追加 * Update onnxruntime-sys/build.rs Co-authored-by: Ryo Yamashita <[email protected]> * Update .github/workflows/general.yml Co-authored-by: Hiroshiba <[email protected]> * Update onnxruntime-sys/build.rs Co-authored-by: Hiroshiba <[email protected]> * Update onnxruntime-sys/build.rs Co-authored-by: Hiroshiba <[email protected]> * androidのリリースURLを切り出し * NDK用のGithub Actionsを使うように * fix typo * ndkのパスを修正 * androidのas_onnx_str を指定 --------- Co-authored-by: Ryo Yamashita <[email protected]> Co-authored-by: Hiroshiba <[email protected]>
1 parent 33fd03f commit 09dc0c7

File tree

6 files changed

+7062
-10
lines changed

6 files changed

+7062
-10
lines changed

.github/actions/auto_gen_bind_pr/action.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ runs:
2020
run: |
2121
sudo apt update
2222
sudo apt install gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
23+
- uses: nttld/setup-ndk@v1
24+
if: inputs.triple == 'aarch64-linux-android'
25+
with:
26+
ndk-version: r25b
27+
- name: Set path for android
28+
if: inputs.triple == 'aarch64-linux-android'
29+
shell: bash
30+
run: |
31+
echo "$ANDROID_NDK/toolchains/llvm/prebuilt/linux-x86_64/bin" >> "$GITHUB_PATH"
32+
echo "AR_aarch64_linux_android=llvm-ar" >> "$GITHUB_ENV"
2333
- uses: actions-rs/toolchain@v1
2434
with:
2535
toolchain: stable

.github/workflows/gen_bind.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ jobs:
1515
triple: x86_64-unknown-linux-gnu
1616
- os: ubuntu-latest
1717
triple: aarch64-unknown-linux-gnu
18+
- os: ubuntu-latest
19+
triple: aarch64-linux-android
1820
- os: windows-latest
1921
triple: x86_64-pc-windows-msvc
2022
- os: windows-latest

.github/workflows/general.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ jobs:
3838
- run: rustup target install x86_64-apple-darwin
3939
- run: rustup target install i686-pc-windows-msvc
4040
- run: rustup target install x86_64-pc-windows-msvc
41+
- run: rustup target install aarch64-linux-android
4142
# ******************************************************************
4243
- name: Download prebuilt archive (CPU, x86_64-unknown-linux-gnu)
4344
uses: actions-rs/cargo@v1
@@ -47,6 +48,14 @@ jobs:
4748
- name: Verify prebuilt archive downloaded (CPU, x86_64-unknown-linux-gnu)
4849
run: ls -lh target/x86_64-unknown-linux-gnu/debug/build/onnxruntime-sys-*/out/onnxruntime-linux-x64-1.*.tgz
4950
# ******************************************************************
51+
- name: Download prebuilt archive (CPU, aarch64-linux-android)
52+
uses: actions-rs/cargo@v1
53+
with:
54+
command: build
55+
args: --target aarch64-linux-android
56+
- name: Verify prebuilt archive downloaded (CPU, aarch64-linux-android)
57+
run: ls -lh target/aarch64-linux-android/debug/build/onnxruntime-sys-*/out/onnxruntime*.aar
58+
# ******************************************************************
5059
- name: Download prebuilt archive (CPU, x86_64-apple-darwin)
5160
uses: actions-rs/cargo@v1
5261
with:

onnxruntime-sys/build.rs

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ const ORT_VERSION: &str = "1.13.1";
1818
/// Base Url from which to download pre-built releases/
1919
const ORT_RELEASE_BASE_URL: &str = "https://github.com/microsoft/onnxruntime/releases/download";
2020

21+
/// Base Url from which to download pre-built releases for android/
22+
const ORT_MAVEN_RELEASE_BASE_URL: &str =
23+
"https://repo1.maven.org/maven2/com/microsoft/onnxruntime/onnxruntime-android";
24+
2125
/// Environment variable selecting which strategy to use for finding the library
2226
/// Possibilities:
2327
/// * "download": Download a pre-built library from upstream. This is the default if `ORT_STRATEGY` is not set.
@@ -63,11 +67,21 @@ fn main() {
6367
fn main() {
6468
let libort_install_dir = prepare_libort_dir();
6569

70+
// FIXME: directmlとandroidで処理の表現が違うので統一する
6671
#[cfg(not(feature = "directml"))]
67-
let (include_dir, lib_dir) = (
68-
libort_install_dir.join("include"),
69-
libort_install_dir.join("lib"),
70-
);
72+
let (include_dir, lib_dir) = match TRIPLET.os {
73+
Os::Android => {
74+
let include_dir = libort_install_dir.join("headers");
75+
let runtimes_dir = libort_install_dir
76+
.join("jni")
77+
.join(&*TRIPLET.arch.as_onnx_android_str());
78+
(include_dir, runtimes_dir)
79+
}
80+
_ => (
81+
libort_install_dir.join("include"),
82+
libort_install_dir.join("lib"),
83+
),
84+
};
7185

7286
#[cfg(feature = "directml")]
7387
let (include_dir, lib_dir) = {
@@ -265,6 +279,7 @@ fn extract_archive(filename: &Path, output: &Path) {
265279
match filename.extension().map(|e| e.to_str()) {
266280
Some(Some("zip")) => extract_zip(filename, output),
267281
Some(Some("tgz")) => extract_tgz(filename, output),
282+
Some(Some("aar")) => extract_zip(filename, output),
268283
_ => unimplemented!(),
269284
}
270285
}
@@ -349,6 +364,15 @@ impl Architecture {
349364
Architecture::Arm64 => Cow::from("arm64"),
350365
}
351366
}
367+
368+
fn as_onnx_android_str(&self) -> Cow<str> {
369+
match self {
370+
Architecture::X86 => Cow::from("x86"),
371+
Architecture::X86_64 => Cow::from("x86_64"),
372+
Architecture::Arm => Cow::from("armeabi-v7a"),
373+
Architecture::Arm64 => Cow::from("arm64-v8a"),
374+
}
375+
}
352376
}
353377

354378
#[derive(Debug)]
@@ -357,6 +381,7 @@ enum Os {
357381
Windows,
358382
Linux,
359383
MacOs,
384+
Android,
360385
}
361386

362387
impl Os {
@@ -365,6 +390,7 @@ impl Os {
365390
Os::Windows => "zip",
366391
Os::Linux => "tgz",
367392
Os::MacOs => "tgz",
393+
Os::Android => "aar",
368394
}
369395
}
370396
}
@@ -377,6 +403,7 @@ impl FromStr for Os {
377403
"windows" => Ok(Os::Windows),
378404
"macos" => Ok(Os::MacOs),
379405
"linux" => Ok(Os::Linux),
406+
"android" => Ok(Os::Android),
380407
_ => Err(format!("Unsupported os: {}", s)),
381408
}
382409
}
@@ -388,6 +415,7 @@ impl OnnxPrebuiltArchive for Os {
388415
Os::Windows => Cow::from("win"),
389416
Os::Linux => Cow::from("linux"),
390417
Os::MacOs => Cow::from("osx"),
418+
Os::Android => Cow::from("android"),
391419
}
392420
}
393421
}
@@ -436,6 +464,7 @@ impl OnnxPrebuiltArchive for Triplet {
436464
}
437465
(Os::Windows, Architecture::X86, Accelerator::None)
438466
| (Os::MacOs, Architecture::X86_64, Accelerator::None)
467+
| (Os::Android, Architecture::Arm64, Accelerator::None)
439468
| (Os::Linux, Architecture::Arm64, Accelerator::None) => Cow::from(format!(
440469
"{}-{}",
441470
self.os.as_onnx_str(),
@@ -484,11 +513,19 @@ fn prebuilt_archive_url() -> (PathBuf, String) {
484513
ORT_VERSION,
485514
TRIPLET.os.archive_extension()
486515
);
487-
let prebuilt_url = format!(
488-
"{}/v{}/{}",
489-
ORT_RELEASE_BASE_URL, ORT_VERSION, prebuilt_archive
490-
);
491-
516+
let prebuilt_url = match TRIPLET.os {
517+
Os::Android => format!(
518+
"{}/{}/onnxruntime-android-{}.{}",
519+
ORT_MAVEN_RELEASE_BASE_URL,
520+
ORT_VERSION,
521+
ORT_VERSION,
522+
TRIPLET.os.archive_extension()
523+
),
524+
_ => format!(
525+
"{}/v{}/{}",
526+
ORT_RELEASE_BASE_URL, ORT_VERSION, prebuilt_archive
527+
),
528+
};
492529
(PathBuf::from(prebuilt_archive), prebuilt_url)
493530
}
494531

@@ -534,7 +571,10 @@ fn prepare_libort_dir_prebuilt() -> PathBuf {
534571
// directmlの場合はzipの子ディレクトリがzipファイル名のディレクトリではないため、
535572
// この処理は非directmlの場合のみ行う
536573
#[cfg(not(feature = "directml"))]
537-
let extract_dir = extract_dir.join(prebuilt_archive.file_stem().unwrap());
574+
let extract_dir = match TRIPLET.os {
575+
Os::Android => extract_dir,
576+
_ => extract_dir.join(prebuilt_archive.file_stem().unwrap()),
577+
};
538578

539579
extract_dir
540580
}

0 commit comments

Comments
 (0)