-
Notifications
You must be signed in to change notification settings - Fork 13
support iOS #19
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
support iOS #19
Changes from 13 commits
bcd180d
75423fc
b666249
0df261b
c7e13ee
2d3f1b5
86691fc
164d9a5
26619b8
772df58
2935b14
c30c6f6
adc9201
082ef93
facd505
652a640
9350cda
ee0f161
1277c6f
cc4f328
c96e34a
692c1dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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, | ||
| }; | ||
|
|
||
|
|
@@ -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"; | ||
|
|
||
Hiroshiba marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// 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. | ||
|
|
@@ -382,6 +390,7 @@ enum Os { | |
| Linux, | ||
| MacOs, | ||
| Android, | ||
| IOs, | ||
| } | ||
|
|
||
| impl Os { | ||
|
|
@@ -391,6 +400,7 @@ impl Os { | |
| Os::Linux => "tgz", | ||
| Os::MacOs => "tgz", | ||
| Os::Android => "aar", | ||
| Os::IOs => "zip", | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -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)), | ||
| } | ||
| } | ||
|
|
@@ -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"), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rustの命名規則だと
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 個人的には仕方ないかなと思いました・・・! |
||
| } | ||
| } | ||
| } | ||
|
|
@@ -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) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.