Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions .github/actions/auto_gen_bind_pr/action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ runs:
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Set env for iOS build
if: inputs.triple == 'aarch64-apple-ios' || inputs.triple == 'aarch64-apple-ios-sim' || inputs.triple == 'x86_64-apple-ios'
shell: bash
run: echo "ORT_STRATEGY=compile" >> "$GITHUB_ENV"
- name: install triple
run: rustup target add ${{ inputs.triple }}
shell: bash
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/gen_bind.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ jobs:
triple: x86_64-apple-darwin
- os: macos-latest
triple: aarch64-apple-darwin
- os: macos-latest
triple: aarch64-apple-ios
- os: macos-latest
triple: aarch64-apple-ios-sim
- os: macos-latest
triple: x86_64-apple-ios
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions onnxruntime-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ keywords = ["neuralnetworks", "onnx", "bindings"]
once_cell = "1.13.0"
bindgen = { version = "0.60.1", optional = true }
ureq = "2.1"
git2 = "0.16.1"

# Used on Windows
zip = "0.6.2"
Expand Down
127 changes: 126 additions & 1 deletion onnxruntime-sys/build.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![allow(dead_code)]

use git2::{build::CheckoutBuilder, Repository};
use std::{
borrow::Cow,
env, fs,
io::{self, Read, Write},
path::{Path, PathBuf},
process::Command,
str::FromStr,
};

Expand All @@ -22,6 +24,12 @@ const ORT_RELEASE_BASE_URL: &str = "https://github.com/microsoft/onnxruntime/rel
const ORT_MAVEN_RELEASE_BASE_URL: &str =
"https://repo1.maven.org/maven2/com/microsoft/onnxruntime/onnxruntime-android";

/// onnxruntime repository/
const ORT_REPOSITORY_URL: &str = "https://github.com/microsoft/onnxruntime.git";

/// Minimum iOS version of the target platform/
const IOS_MINIMAL_DEPLOY_TARGET: &str = "16.0";

/// Environment variable selecting which strategy to use for finding the library
/// Possibilities:
/// * "download": Download a pre-built library from upstream. This is the default if `ORT_STRATEGY` is not set.
Expand Down Expand Up @@ -382,6 +390,7 @@ enum Os {
Linux,
MacOs,
Android,
IOs,
}

impl Os {
Expand All @@ -391,6 +400,7 @@ impl Os {
Os::Linux => "tgz",
Os::MacOs => "tgz",
Os::Android => "aar",
Os::IOs => "zip",
}
}
}
Expand All @@ -404,6 +414,7 @@ impl FromStr for Os {
"macos" => Ok(Os::MacOs),
"linux" => Ok(Os::Linux),
"android" => Ok(Os::Android),
"ios" => Ok(Os::IOs),
_ => Err(format!("Unsupported os: {}", s)),
}
}
Expand All @@ -416,6 +427,7 @@ impl OnnxPrebuiltArchive for Os {
Os::Linux => Cow::from("linux"),
Os::MacOs => Cow::from("osx"),
Os::Android => Cow::from("android"),
Os::IOs => Cow::from("ios"),
Copy link
Member

Choose a reason for hiding this comment

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

Rustの命名規則だとIosの方がいいんですが、2行上でnbigaouette氏(オリジナルのonnxruntime-rsの作者)がMacOsと書いちゃっているので統一感のためにここは致し方なし...?

In UpperCamelCase, acronyms and contractions of compound words count as one word

Copy link
Member

Choose a reason for hiding this comment

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

個人的には仕方ないかなと思いました・・・!
本当はMacosが良いんだろうなとも思います。が、まあ直しちゃうとコンフリクトが出てしまうので・・・ 😇

}
}
}
Expand Down Expand Up @@ -599,7 +611,120 @@ fn prepare_libort_dir() -> PathBuf {
);
}
}),
Ok("compile") => unimplemented!(),
Ok("compile") => prepare_libort_dir_compiled(),
_ => panic!("Unknown value for {:?}", ORT_ENV_STRATEGY),
}
}

fn prepare_libort_dir_compiled() -> PathBuf {
// Compile is only support iOS currently.

if !matches!(TRIPLET.os, Os::IOs) {
panic!("Compile strategy is only support iOS currently");
}

let is_simulator = env::var("TARGET").unwrap().ends_with("sim")
|| env::var("TARGET").unwrap().starts_with("x86_64");
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let onnxruntime_dir = out_dir.join("onnxruntime");
let build_dir = onnxruntime_dir.join("build");
let extract_dir = out_dir.join(format!("{}_{}_cpu", ORT_PREBUILT_EXTRACT_DIR, ORT_VERSION));
// Clone microsoft/onnxruntime
let repo = match Repository::clone(ORT_REPOSITORY_URL, &onnxruntime_dir) {
Ok(repo) => repo,
Err(e) => panic!("Failed to clone onnxruntime: {}", e),
};
// checkout commit annotated by tag
let reference = format!("refs/tags/v{}", ORT_VERSION);
let reference = match repo.find_reference(&reference) {
Ok(reference) => reference,
Err(e) => panic!("Failed to find tag v{}: {}", ORT_VERSION, e),
};
repo.set_head(reference.name().unwrap()).unwrap();
repo.checkout_head(Some(&mut CheckoutBuilder::new().force()))
.unwrap();

// build onnxruntime
let build_script = onnxruntime_dir.join("build.sh");
let ios_sysroot = if is_simulator {
"iphonesimulator"
} else {
"iphoneos"
};
let arch = if matches!(TRIPLET.arch, Architecture::Arm64) {
"arm64"
} else {
"x86_64"
};
let status = Command::new(build_script)
.args([
"--config",
"Release",
"--build_dir",
build_dir.to_str().unwrap(),
"--skip_tests",
"--parallel",
"--build_shared_lib",
"--use_xcode",
"--ios",
"--ios_sysroot",
ios_sysroot,
"--osx_arch",
arch,
"--apple_deploy_target",
IOS_MINIMAL_DEPLOY_TARGET,
])
.status()
.expect("Failed to execute onnxruntime build process");
if !status.success() {
panic!("Failed to build onnxruntime: {:?}", status.code());
}

// get commit id for copy files
let commit = match reference.peel_to_commit() {
Ok(commit) => commit,
Err(e) => panic!("Failed to peel HEAD: {}", e),
};
let commit_id = commit.id();

// copy files
let copy_script = onnxruntime_dir
.join("tools")
.join("ci_build")
.join("github")
.join("linux")
.join("copy_strip_binary.sh");
let os = if is_simulator { "ios-sim" } else { "ios" };
let artifact_name = format!("onnxruntime-{}-{}", os, arch);

let status = Command::new(copy_script)
.args([
"-r",
build_dir.to_str().unwrap(),
"-a",
&artifact_name,
"-l",
(format!("libonnxruntime.{}.dylib", ORT_VERSION).as_str()),
"-c",
(format!("Release/Release-{}", ios_sysroot).as_str()),
"-s",
onnxruntime_dir.to_str().unwrap(),
"-t",
&commit_id.to_string(),
])
.status()
.expect("Failed to execute copy process");
if !status.success() {
panic!("Failed to copy onnxruntime: {:?}", status.code().unwrap());
}

// move artifact directory
fs::create_dir_all(&extract_dir).unwrap();
fs::rename(
build_dir.join(&artifact_name),
extract_dir.join(&artifact_name),
)
.unwrap();

extract_dir.join(artifact_name)
}
12 changes: 12 additions & 0 deletions onnxruntime-sys/src/generated/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ include!(concat!(
"/src/generated/macos/aarch64/bindings.rs"
));

#[cfg(all(target_os = "ios", target_arch = "aarch64"))]
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/generated/ios/aarch64/bindings.rs"
));

#[cfg(all(target_os = "ios", target_arch = "x86_64"))]
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
"/src/generated/ios/x86_64/bindings.rs"
));

#[cfg(all(target_os = "windows", target_arch = "x86"))]
include!(concat!(
env!("CARGO_MANIFEST_DIR"),
Expand Down
Loading