diff --git a/.github/workflows/build-apple.yml b/.github/workflows/build-apple.yml index d1dba49..3b9516a 100644 --- a/.github/workflows/build-apple.yml +++ b/.github/workflows/build-apple.yml @@ -18,14 +18,22 @@ jobs: destination: generic/platform=iOS platform: iOS sdk-name: iphoneos + rust-targets: + - aarch64-apple-ios - scheme: Burrow destination: platform=iOS Simulator,OS=16.2,name=iPhone 14 Pro platform: iOS Simulator sdk-name: iphonesimulator + rust-targets: + - aarch64-apple-ios-sim + - x86_64-apple-ios - scheme: Burrow destination: platform=macOS platform: macOS sdk-name: macos + rust-targets: + - x86_64-apple-darwin + - aarch64-apple-darwin env: DEVELOPER_DIR: /Applications/Xcode_14.2.app/Contents/Developer steps: @@ -39,6 +47,11 @@ jobs: with: certificate: ${{ secrets.DEVELOPER_CERT }} password: ${{ secrets.DEVELOPER_CERT_PASSWORD }} + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + toolchain: stable + targets: ${{ join(matrix.rust-targets, ', ') }} - name: Build id: build uses: ./.github/actions/build-for-testing diff --git a/.github/workflows/build-rust.yml b/.github/workflows/build-rust.yml index 8048a3a..c767eca 100644 --- a/.github/workflows/build-rust.yml +++ b/.github/workflows/build-rust.yml @@ -50,7 +50,7 @@ jobs: shell: bash run: sudo apt-get install -y ${{ join(matrix.packages, ' ') }} - name: Install Rust - uses: dtolnay/rust-toolchain@master + uses: dtolnay/rust-toolchain@stable with: toolchain: stable components: rustfmt diff --git a/Apple/Burrow.xcodeproj/project.pbxproj b/Apple/Burrow.xcodeproj/project.pbxproj index fa4009a..adee0ed 100644 --- a/Apple/Burrow.xcodeproj/project.pbxproj +++ b/Apple/Burrow.xcodeproj/project.pbxproj @@ -12,6 +12,7 @@ D05B9F7629E39EEC008CB1F9 /* BurrowApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05B9F7529E39EEC008CB1F9 /* BurrowApp.swift */; }; D05B9F7829E39EEC008CB1F9 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D05B9F7729E39EEC008CB1F9 /* ContentView.swift */; }; D05B9F7A29E39EED008CB1F9 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D05B9F7929E39EED008CB1F9 /* Assets.xcassets */; }; + D0B98FC529FDA49E004E7149 /* libburrow.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D0B98FC429FDA476004E7149 /* libburrow.a */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -57,6 +58,8 @@ D05B9F7529E39EEC008CB1F9 /* BurrowApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BurrowApp.swift; sourceTree = ""; }; D05B9F7729E39EEC008CB1F9 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; D05B9F7929E39EED008CB1F9 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + D0B98FBF29FD8072004E7149 /* build-rust.sh */ = {isa = PBXFileReference; lastKnownFileType = text.script.sh; path = "build-rust.sh"; sourceTree = ""; }; + D0B98FC429FDA476004E7149 /* libburrow.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; path = libburrow.a; sourceTree = BUILT_PRODUCTS_DIR; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -64,6 +67,7 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( + D0B98FC529FDA49E004E7149 /* libburrow.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -84,6 +88,7 @@ D020F64A29E4A452002790F6 /* App.xcconfig */, D020F66329E4A703002790F6 /* Extension.xcconfig */, D020F64029E4A1FF002790F6 /* Compiler.xcconfig */, + D0B98FBF29FD8072004E7149 /* build-rust.sh */, D020F64229E4A1FF002790F6 /* Info.plist */, ); path = Configuration; @@ -108,6 +113,7 @@ D020F65629E4A697002790F6 /* NetworkExtension */, D020F63C29E4A1FF002790F6 /* Configuration */, D05B9F7329E39EEC008CB1F9 /* Products */, + D0B98FC129FDA45D004E7149 /* Frameworks */, ); sourceTree = ""; }; @@ -133,6 +139,14 @@ path = App; sourceTree = ""; }; + D0B98FC129FDA45D004E7149 /* Frameworks */ = { + isa = PBXGroup; + children = ( + D0B98FC429FDA476004E7149 /* libburrow.a */, + ); + name = Frameworks; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ @@ -140,6 +154,7 @@ isa = PBXNativeTarget; buildConfigurationList = D020F65E29E4A697002790F6 /* Build configuration list for PBXNativeTarget "NetworkExtension" */; buildPhases = ( + D0B98FC029FD809A004E7149 /* Compile Rust */, D020F64F29E4A697002790F6 /* Sources */, D020F65029E4A697002790F6 /* Frameworks */, D020F65129E4A697002790F6 /* Resources */, @@ -227,6 +242,30 @@ }; /* End PBXResourcesBuildPhase section */ +/* Begin PBXShellScriptBuildPhase section */ + D0B98FC029FD809A004E7149 /* Compile Rust */ = { + isa = PBXShellScriptBuildPhase; + alwaysOutOfDate = 1; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + ); + inputPaths = ( + ); + name = "Compile Rust"; + outputFileListPaths = ( + ); + outputPaths = ( + "$(BUILT_PRODUCTS_DIR)/libburrow.a", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PROJECT_DIR}/Configuration/build-rust.sh\"\n"; + showEnvVarsInLog = 0; + }; +/* End PBXShellScriptBuildPhase section */ + /* Begin PBXSourcesBuildPhase section */ D020F64F29E4A697002790F6 /* Sources */ = { isa = PBXSourcesBuildPhase; diff --git a/Apple/Configuration/build-rust.sh b/Apple/Configuration/build-rust.sh new file mode 100755 index 0000000..1f62701 --- /dev/null +++ b/Apple/Configuration/build-rust.sh @@ -0,0 +1,69 @@ +#!/bin/bash + +export PATH="${PATH}:${HOME}/.cargo/bin:/opt/homebrew/bin:/usr/local/bin:/etc/profiles/per-user/${USER}/bin" + +if ! [[ -x "$(command -v cargo)" ]]; then + echo 'error: Unable to find cargo' + exit 127 +fi + +set -e + +cd -- "$(dirname -- "${BASH_SOURCE[0]}")"/../../burrow + +RUST_TARGETS=() + +IFS=' ' read -a BURROW_ARCHS <<< "${ARCHS[@]}" +for ARCH in "${BURROW_ARCHS[@]}"; do + case $PLATFORM_NAME in + iphonesimulator) + case $ARCH in + arm64) RUST_TARGETS+=("aarch64-apple-ios-sim") ;; + x86_64) RUST_TARGETS+=("x86_64-apple-ios") ;; + *) echo "error: Unknown $PLATFORM_NAME arch, $ARCH"; exit 1 ;; + esac + ;; + iphoneos) + case $ARCH in + arm64) RUST_TARGETS+=("aarch64-apple-ios") ;; + *) echo "error: Unknown $PLATFORM_NAME arch, $ARCH"; exit 1 ;; + esac + ;; + macos*) + case $ARCH in + arm64) RUST_TARGETS+=("aarch64-apple-darwin") ;; + x86_64) RUST_TARGETS+=("x86_64-apple-darwin") ;; + *) echo "error: Unknown $PLATFORM_NAME arch, $ARCH"; exit 1 ;; + esac + ;; + *) echo "error: Unsupported platform $PLATFORM_NAME"; exit 1 ;; + esac +done + +CARGO_ARGS=() +for TARGET in "${RUST_TARGETS[@]}"; do + CARGO_ARGS+=("--target") + CARGO_ARGS+=("$TARGET") +done + +CARGO_ARGS+=("--lib") + +if [[ $SWIFT_ACTIVE_COMPILATION_CONDITIONS == *DEBUG* ]]; then + CARGO_DIR="debug" +else + CARGO_ARGS+=("--release") + CARGO_DIR="release" +fi + +if [[ -x "$(command -v rustup)" ]]; then + CARGO_PATH="$(dirname $(rustup which cargo)):/usr/bin" +else + CARGO_PATH="$(dirname $(readlink -f $(which cargo))):/usr/bin" +fi + +env -i PATH="$CARGO_PATH" cargo build "${CARGO_ARGS[@]}" + +mkdir -p "${BUILT_PRODUCTS_DIR}" +/usr/bin/xcrun --sdk $PLATFORM_NAME lipo \ + -create $(printf "${PROJECT_DIR}/../target/%q/${CARGO_DIR}/libburrow.a " "${RUST_TARGETS[@]}") \ + -output "${BUILT_PRODUCTS_DIR}/libburrow.a" diff --git a/burrow/Cargo.toml b/burrow/Cargo.toml index ec16981..a984553 100644 --- a/burrow/Cargo.toml +++ b/burrow/Cargo.toml @@ -3,7 +3,8 @@ name = "burrow" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +crate-type = ["lib", "staticlib"] [dependencies] tokio = { version = "1.21", features = ["rt", "macros"] } diff --git a/burrow/src/lib.rs b/burrow/src/lib.rs new file mode 100644 index 0000000..7d83484 --- /dev/null +++ b/burrow/src/lib.rs @@ -0,0 +1,3 @@ +pub fn hello_world() { + println!("Hello, world!"); +} diff --git a/burrow/src/main.rs b/burrow/src/main.rs index 3d59fd3..8dae842 100644 --- a/burrow/src/main.rs +++ b/burrow/src/main.rs @@ -1,7 +1,7 @@ use tokio::io::Result; use tun::TunInterface; -async fn lol() -> Result<()> { +async fn try_main() -> Result<()> { let iface = TunInterface::new()?; println!("{:?}", iface.name()); @@ -10,5 +10,5 @@ async fn lol() -> Result<()> { #[tokio::main(flavor = "current_thread")] async fn main() { - lol().await.unwrap(); + try_main().await.unwrap(); }