Rust应用打包工具apple-bundles的使用,快速构建和分发macOS/iOS应用的Rust插件库
Rust应用打包工具apple-bundles的使用,快速构建和分发macOS/iOS应用的Rust插件库
apple-bundles
是一个实现了与 Apple bundles 相关功能的库 crate。bundle 是 Apple 操作系统中用于封装代码和资源的基础原语(例如 /Applications
中的 .app
目录就是应用程序 bundle)。
安装
在项目目录中运行以下 Cargo 命令:
cargo add apple-bundles
或者在 Cargo.toml 中添加:
apple-bundles = "0.21.0"
使用示例
以下是一个完整的使用示例,展示如何使用 apple-bundles 创建一个基本的 macOS 应用程序 bundle:
use apple_bundles::{
Bundle,
DirectoryBundle,
InfoPlist,
InfoPlistKey,
InfoPlistValue
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建 Info.plist 内容
let mut info_plist = InfoPlist::new();
info_plist.insert(
InfoPlistKey::CFBundleExecutable.to_string(),
InfoPlistValue::String("my_app".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleIdentifier.to_string(),
InfoPlistValue::String("com.example.myapp".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleName.to_string(),
InfoPlistValue::String("My App".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleVersion.to_string(),
InfoPlistValue::String("1.0".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleShortVersionString.to_string(),
InfoPlistValue::String("1.0".to_string())
);
// 创建应用程序 bundle
let bundle = DirectoryBundle::new("MyApp.app")
.with_info_plist(info_plist)?
.with_executable("target/release/my_app")?;
// 写入文件系统
bundle.write_to("dist")?;
println!("Successfully created application bundle at dist/MyApp.app");
Ok(())
}
创建 Framework Bundle 示例
use apple_bundles::{
Bundle,
FrameworkBundle,
InfoPlist,
InfoPlistKey,
InfoPlistValue
};
fn create_framework() -> Result<(), Box<dyn std::error::Error>> {
// 创建 Info.plist 内容
let mut info_plist = InfoPlist::new();
info_plist.insert(
InfoPlistKey::CFBundleIdentifier.to_string(),
InfoPlistValue::String("com.example.myframework".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleName.to_string(),
InfoPlistValue::String("MyFramework".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundleVersion.to_string(),
InfoPlistValue::String("1.0".to_string())
);
info_plist.insert(
InfoPlistKey::CFBundlePackageType.to_string(),
InfoPlistValue::String("FMWK".to_string())
);
// 创建 Framework bundle
let framework = FrameworkBundle::new("MyFramework.framework")
.with_info_plist(info_plist)?
.with_library("target/universal/release/libmyframework.dylib")?;
// 写入文件系统
framework.write_to("dist")?;
println!("Successfully created framework at dist/MyFramework.framework");
Ok(())
}
功能特点
- 支持创建标准的 Apple bundle 结构
- 提供类型安全的 Info.plist 操作接口
- 支持应用程序 bundle 和 framework bundle
- 可扩展支持其他 bundle 类型
完整示例代码
以下是一个结合应用程序 bundle 和 framework bundle 的完整示例:
use apple_bundles::{
Bundle,
DirectoryBundle,
FrameworkBundle,
InfoPlist,
InfoPlistKey,
InfoPlistValue
};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 创建应用程序 bundle
let mut app_info_plist = InfoPlist::new();
app_info_plist.insert(
InfoPlistKey::CFBundleExecutable.to_string(),
InfoPlistValue::String("my_app".to_string())
);
app_info_plist.insert(
InfoPlistKey::CFBundleIdentifier.to_string(),
InfoPlistValue::String("com.example.myapp".to_string())
);
app_info_plist.insert(
InfoPlistKey::CFBundleName.to_string(),
InfoPlistValue::String("My App".to_string())
);
app_info_plist.insert(
InfoPlistKey::CFBundleVersion.to_string(),
InfoPlistValue::String("1.0".to_string())
);
let app_bundle = DirectoryBundle::new("MyApp.app")
.with_info_plist(app_info_plist)?
.with_executable("target/release/my_app")?;
// 创建 framework bundle
let mut framework_info_plist = InfoPlist::new();
framework_info_plist.insert(
InfoPlistKey::CFBundleIdentifier.to_string(),
InfoPlistValue::String("com.example.myframework".to_string())
);
framework_info_plist.insert(
InfoPlistKey::CFBundleName.to_string(),
InfoPlistValue::String("MyFramework".to_string())
);
framework_info_plist.insert(
InfoPlistKey::CFBundleVersion.to_string(),
InfoPlistValue::String("1.0".to_string())
);
framework_info_plist.insert(
InfoPlistKey::CFBundlePackageType.to_string(),
InfoPlistValue::String("FMWK".to_string())
);
let framework = FrameworkBundle::new("MyFramework.framework")
.with_info_plist(framework_info_plist)?
.with_library("target/universal/release/libmyframework.dylib")?;
// 写入文件系统
app_bundle.write_to("dist")?;
framework.write_to("dist")?;
println!("Successfully created bundles in dist/ directory");
Ok(())
}
许可证
MPL-2.0
1 回复
Rust应用打包工具apple-bundles使用指南
工具简介
apple-bundles
是一个专门为Rust开发者设计的工具,用于将Rust代码打包成macOS和iOS应用或插件库。它简化了将Rust代码集成到Apple生态系统中的过程,特别适合需要分发Rust库给其他Apple平台开发者使用的场景。
主要功能
- 为macOS/iOS应用创建框架包(Framework)
- 生成Xcode兼容的静态库和动态库
- 自动处理签名和代码嵌入
- 支持Swift Package Manager集成
安装方法
cargo install apple-bundles
基本使用方法
1. 创建简单的静态库
cargo apple-bundles new my_library --kind staticlib
cd my_library
cargo apple-bundles build
2. 创建iOS框架
cargo apple-bundles new my_framework --kind framework --platform ios
cd my_framework
cargo apple-bundles build --release
配置示例
在Cargo.toml
中添加以下配置:
[package.metadata.apple-bundles]
name = "MyRustFramework"
identifier = "com.example.my-rust-framework"
platform = ["ios", "macos"]
kind = "framework"
version = "1.0.0"
高级功能示例
1. 构建多平台框架
cargo apple-bundles build --platform ios --platform macos
2. 自定义输出路径
cargo apple-bundles build --output ./build/MyFramework.framework
3. 启用Bitcode支持
cargo apple-bundles build --bitcode
集成到Xcode项目
- 构建框架:
cargo apple-bundles build --release
-
将生成的
.framework
文件拖入Xcode项目 -
在Swift代码中使用:
import MyRustFramework
let result = rust_function(42)
print("Result from Rust: \(result)")
常见问题解决
- 签名问题:确保在构建时提供有效的开发者证书
cargo apple-bundles build --sign "Apple Development: your@email.com"
- 架构支持:默认支持arm64和x86_64,如需其他架构:
cargo apple-bundles build --arch arm64 --arch x86_64
- 版本兼容性:可以在配置中指定最低部署目标
[package.metadata.apple-bundles]
ios_deployment_target = "13.0"
macos_deployment_target = "10.15"
完整示例demo
下面是一个完整的从创建到集成的示例:
- 首先创建一个新的Rust框架项目:
cargo apple-bundles new MyRustSDK --kind framework --platform ios
cd MyRustSDK
- 编辑
src/lib.rs
添加Rust代码:
// 定义一个简单的Rust函数
#[no_mangle]
pub extern "C" fn add_numbers(a: i32, b: i32) -> i32 {
a + b
}
- 更新
Cargo.toml
配置:
[package]
name = "my-rust-sdk"
version = "0.1.0"
[lib]
name = "MyRustSDK"
crate-type = ["cdylib"]
[package.metadata.apple-bundles]
name = "MyRustSDK"
identifier = "com.example.my-rust-sdk"
platform = ["ios"]
kind = "framework"
version = "0.1.0"
ios_deployment_target = "13.0"
- 构建框架:
cargo apple-bundles build --release --platform ios
-
在Xcode项目中集成:
- 将生成的
MyRustSDK.framework
拖入Xcode项目 - 在
General
选项卡的Frameworks,Libraries,and Embedded Content
中添加框架
- 将生成的
-
在Swift中使用Rust代码:
import MyRustSDK
let sum = add_numbers(10, 20)
print("The sum is: \(sum)") // 将输出 "The sum is: 30"
apple-bundles
极大简化了Rust代码在Apple平台的分发过程,是开发跨平台库或插件的理想工具。