编辑已恢复
我们已与您断开连接。尝试重连时会保存您所做的变更。尝试重连
标题 1
已保存
Bin Song
B
要发布此内容,请选择键盘上的 ⌘Enter。
发布
关闭
Rust技术空间
…
跨平台使用调研
iOS使用Rust调研
添加表情符号
添加标题图像
添加状态
一、iOS 项目接入 Rust Library
约定:
iOS 项目目录 DemoApp
Rust Library:demo-library-ffi
1.1、Rust Library
将 Rust Library 代码 Clone 到 iOS 项目根目录下: DemoApp/demo-library-ffi;
修改 DemoApp/rust-library/Cargo.toml,将 Rust Library 修改为静态库
[lib]
crate-type = [“staticlib”]
在 DemoApp 目录下创建一个新的顶层 Cargo.toml 文件:
[workspace]
members = [
“demo-library-ffi”
]
现在可以直接使用 cargo build 来生成 rust 静态库了:
DemoApp/target/debug/demo-library-ffi.a
1.2、iOS Project
1.2.1
打开 Xcode target configuration → Build Phases → Link Binary with Libraries 选项卡,选择 Add files,将前面生成的 DemoApp/target/debug/demo-library-ffi.a 文件添加进去;
是的,这个不是最终我们需要的正确静态库文件,因为它只有当前打包机器的架构,不要紧,我们会在下面步骤中修复它;
1.2.2
用文本编辑器打开 project.pbxproj,搜索并替换 Debug 和 Release 两处 LIBRARY_SEARCH_PATHS
LIBRARY_SEARCH_PATHS = (
“
(
i
n
h
e
r
i
t
e
d
)
"
,
"
(inherited)", "
(inherited)","(PROJECT_DIR)/target/debug”,
);
Debug 替换为:
“LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
a
a
r
c
h
64
−
a
p
p
l
e
−
i
o
s
/
d
e
b
u
g
"
;
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
s
i
m
u
l
a
t
o
r
∗
]
[
a
r
c
h
=
a
r
m
64
]
"
=
"
(PROJECT_DIR)/target/aarch64-apple-ios/debug"; "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]" = "
(PROJECTDIR)/target/aarch64−apple−ios/debug";"LIBRARYSEARCHPATHS[sdk=iphonesimulator∗][arch=arm64]"="(PROJECT_DIR)/target/aarch64-apple-ios-sim/debug”;
“LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
x
8
6
6
4
−
a
p
p
l
e
−
i
o
s
/
d
e
b
u
g
"
;
R
e
l
e
a
s
e
替换为:
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
o
s
∗
]
[
a
r
c
h
=
a
r
m
64
]
"
=
"
(PROJECT_DIR)/target/x86_64-apple-ios/debug"; Release 替换为: "LIBRARY_SEARCH_PATHS[sdk=iphoneos*][arch=arm64]" = "
(PROJECTDIR)/target/x8664−apple−ios/debug";Release替换为:"LIBRARYSEARCHPATHS[sdk=iphoneos∗][arch=arm64]"="(PROJECT_DIR)/target/aarch64-apple-ios/release”;
“LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=arm64]” = “
(
P
R
O
J
E
C
T
D
I
R
)
/
t
a
r
g
e
t
/
a
a
r
c
h
64
−
a
p
p
l
e
−
i
o
s
−
s
i
m
/
r
e
l
e
a
s
e
"
;
"
L
I
B
R
A
R
Y
S
E
A
R
C
H
P
A
T
H
S
[
s
d
k
=
i
p
h
o
n
e
s
i
m
u
l
a
t
o
r
∗
]
[
a
r
c
h
=
x
8
6
6
4
]
"
=
"
(PROJECT_DIR)/target/aarch64-apple-ios-sim/release"; "LIBRARY_SEARCH_PATHS[sdk=iphonesimulator*][arch=x86_64]" = "
(PROJECTDIR)/target/aarch64−apple−ios−sim/release";"LIBRARYSEARCHPATHS[sdk=iphonesimulator∗][arch=x8664]"="(PROJECT_DIR)/target/x86_64-apple-ios/release”;
1.2.3
再次打开 Build Phases 选项卡,添加一个 Script,并移动到 Compile Sources 上方,可以命名为: Build Rust library :
bash ${PROJECT_DIR}/bin/compile-rust-lib.sh demo-library-ffi $RUSTBUILDCONF
第二个参数 RUSTBUILDCONF是传递给脚本的环境变量,我们先定义它们:
打开 Build Settings 选项卡,点击 + 添加 User-Defined Setting,为其命名 RUSTBUILDCONF 并根据构建变量选择一个值:Debug 中填写 DEBUG,Release 中填写 RELEASE。
1.2.4
构建脚本 bin/compile-rust-lib.sh
#!/usr/bin/env bash
if [ “$#” -ne 2 ]
then
echo “Usage (note: only call inside xcode!):”
echo “compile-rust-lib.sh <FFI_TARGET> ”
exit 1
fi
what to pass to cargo build -p, e.g. rust_lib_ffi
FFI_TARGET=$1
RUSTBUILDCONF from our xcconfigs
RUSTBUILDCONF=$2
RELFLAG=
if [[ “$RUSTBUILDCONF” != “DEBUG” ]]; then
RELFLAG=–release
fi
set -euvx
if [[ -n “${DEVELOPER_SDK_DIR:-}” ]]; then
Assume we’re in Xcode, which means we’re probably cross-compiling.
In this case, we need to add an extra library search path for build scripts and proc-macros,
which run on the host instead of the target.
(macOS Big Sur does not have linkable libraries in /usr/lib/.)
export LIBRARY_PATH=“
D
E
V
E
L
O
P
E
R
S
D
K
D
I
R
/
M
a
c
O
S
X
.
s
d
k
/
u
s
r
/
l
i
b
:
{DEVELOPER_SDK_DIR}/MacOSX.sdk/usr/lib:
DEVELOPERSDKDIR/MacOSX.sdk/usr/lib:{LIBRARY_PATH:-}”
fi
IS_SIMULATOR=0
if [ “${LLVM_TARGET_TRIPLE_SUFFIX-}” = “-simulator” ]; then
IS_SIMULATOR=1
fi
for arch in
A
R
C
H
S
;
d
o
c
a
s
e
"
ARCHS; do case "
ARCHS;docase"arch" in
x86_64)
if [ $IS_SIMULATOR -eq 0 ]; then
echo “Building for x86_64, but not a simulator build. What’s going on?” >&2
exit 2
fi
# Intel iOS simulator
export CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios"
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target x86_64-apple-ios
;;
arm64)
if [ $IS_SIMULATOR -eq 0 ]; then
# Hardware iOS targets
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target aarch64-apple-ios
else
$HOME/.cargo/bin/cargo build -p $FFI_TARGET --lib $RELFLAG --target aarch64-apple-ios-sim
fi
esac
done
二、问题调研文章来源:https://www.toymoban.com/news/detail-621448.html
- Rust访问网络是否需要原生语言的干预?需支持协议如:gRPC/RESTful/WebSocket/UDP
Rust 可以直接访问网络,并不需要原生语言的干预 - Rust与原生语言的协作方式,FFI/JNI or 其他?
FFI
cbindgen
rsbind - 如何接收Rust发起的回调?
使用函数指针,可以参考这个人的代码:https://github.com/thombles/dw2019rust/blob/master/modules/07%20-%20Swift%20callbacks.md
或者使用 rsbind - Rust是否可以直接访问本地(图片/文件)/沙箱文件?
https://docs.rs/cacao/latest/cacao/index.html - Rust是否可以直接访问本地数据库?本地支持哪些常用数据库并且Rust有相应的封装?
可以直接使用 sqlite,如果要用 CoreData,需要通过 FFI - 构建时各平台的构建步骤以及命令
参考 1.2.4
或者使用 rsbind - Android/iOS端异常调用栈的分析方法,是否有工具/框架可以配合
- 打包后包大小的变化
1.网络
方案1: reqwest, 关闭bitcode的情况下,包增加7.6MB
2.数据库
方案1:rusqlite, 关闭bitcode的情况下,包增加3.6MB文章来源地址https://www.toymoban.com/news/detail-621448.html
到了这里,关于iOS使用Rust调研的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!