From 117e4faa6bb3bc69c1282ac1bd9c0509ac0b162b Mon Sep 17 00:00:00 2001 From: simlay Date: Thu, 24 Sep 2020 11:49:06 -0700 Subject: [PATCH] Initial iOS support (#83) This commit defines CMAKE_OSX_SYSROOT to point to the iOS SDK path to enable iOS. It also support for iphonesimulator. --- .travis-ios.sh | 13 +++++++++++ .travis.yml | 2 ++ shaderc-sys/build/build.rs | 45 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 58 insertions(+), 2 deletions(-) create mode 100755 .travis-ios.sh diff --git a/.travis-ios.sh b/.travis-ios.sh new file mode 100755 index 0000000..a33b649 --- /dev/null +++ b/.travis-ios.sh @@ -0,0 +1,13 @@ +#!/bin/bash +set -e +if [ `uname` = Darwin ] +then + rustup target add x86_64-apple-ios aarch64-apple-ios; + cargo build --target x86_64-apple-ios + cargo build --target aarch64-apple-ios + RUNTIME_ID=$(xcrun simctl list runtimes | grep iOS | cut -d ' ' -f 7 | tail -1) + export SIM_ID=$(xcrun simctl create My-iphone7 com.apple.CoreSimulator.SimDeviceType.iPhone-7 $RUNTIME_ID) + xcrun simctl boot $SIM_ID + cargo install cargo-dinghy + cargo dinghy test --target x86_64-apple-ios +fi diff --git a/.travis.yml b/.travis.yml index c9c445f..ab716b2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ sudo: false os: - linux - osx + - osx_image: xcode12 rust: - stable - nightly @@ -23,6 +24,7 @@ before_script: script: - travis_wait cargo build --verbose - cargo test --verbose + - if [ "$TRAVIS_OS_NAME" = "osx" ]; then ./.travis-ios.sh; fi notifications: email: diff --git a/shaderc-sys/build/build.rs b/shaderc-sys/build/build.rs index aa0c257..6acf905 100644 --- a/shaderc-sys/build/build.rs +++ b/shaderc-sys/build/build.rs @@ -24,7 +24,34 @@ static SHADERC_SHARED_LIB: &str = "shaderc_shared"; static SHADERC_STATIC_LIB_FILE: &str = "libshaderc_combined.a"; static SHADERC_STATIC_LIB_FILE_MSVC: &str = "shaderc_combined.lib"; -fn build_shaderc(shaderc_dir: &PathBuf, use_ninja: bool) -> PathBuf { +fn sdk_path() -> Option { + let target = std::env::var("TARGET").unwrap(); + use std::process::Command; + + // tvOS (and the simulator) could be added here in the future. + let sdk = if target == "x86_64-apple-ios" || target == "i386-apple-ios" { + "iphonesimulator" + } else if target == "aarch64-apple-ios" + || target == "armv7-apple-ios" + || target == "armv7s-apple-ios" + { + "iphoneos" + } else { + return None; + }; + + let output = if let Ok(out) = Command::new("xcrun") + .args(&["--sdk", sdk, "--show-sdk-path"]) + .output() { + out.stdout + } else { + return None; + }; + let prefix_str = std::str::from_utf8(&output).expect("invalid output from `xcrun`"); + Some(PathBuf::from(prefix_str.trim_end().to_string())) +} + +fn build_shaderc(shaderc_dir: &PathBuf, use_ninja: bool, target_os: String) -> PathBuf { let mut config = cmake::Config::new(shaderc_dir); config .profile("Release") @@ -36,6 +63,13 @@ fn build_shaderc(shaderc_dir: &PathBuf, use_ninja: bool) -> PathBuf { if use_ninja { config.generator("Ninja"); } + + if target_os == "ios" { + if let Some(path) = sdk_path() { + config.define("CMAKE_OSX_SYSROOT", path); + } + } + config.build() } @@ -244,6 +278,13 @@ fn main() { println!("cargo:rustc-link-lib=dylib=c++"); return; } + ("ios", _) => { + println!("cargo:warning=MacOS static builds experimental"); + println!("cargo:rustc-link-search=native={}", search_dir_str); + println!("cargo:rustc-link-lib={}={}", kind, lib_name); + println!("cargo:rustc-link-lib=dylib=c++"); + return; + } (_, _) => { println!("cargo:warning=Platform unsupported for linking against system installed shaderc libraries"); } @@ -274,7 +315,7 @@ fn main() { build_shaderc_msvc(&shaderc_dir) } else { let has_ninja = finder.maybe_have("ninja").is_some(); - build_shaderc(&shaderc_dir, has_ninja) + build_shaderc(&shaderc_dir, has_ninja, target_os) }; lib_path.push("lib");