gpt4 book ai didi

rust - 如何从 Rust 中的 crate 为整个程序设置目标三元组?

转载 作者:行者123 更新时间:2023-11-29 08:29:05 26 4
gpt4 key购买 nike

我正在探索在多目标嵌入式项目中使用 Rust 的想法。

我目前的想法是为我拥有的每个 MCU 准备一个箱子。 MCU crate 将包含该特定设备的 GPIO、SPI 和 UART 等实现。

系统的设计应使 MCU crate 可以换出到另一个 MCU 的 crate 以针对新的 MCU。它也应该尽可能简单。

要立即进行切换,您还需要更改自己启动构建的可执行项目中的 .cargo/config 文件。这使得转换过程分为两步,有时会被遗忘。

基本上,我希望 crate 的目标应用于整个构建。


我在网上搜索过它是否曾经出现过,但要么没有,要么是我的搜索技巧不够用。

我的想法是 MCU crate 中的构建脚本可以将其设置复制到构建实例化器的 .cargo/config 文件中。


我想象的构建脚本可能看起来像(伪):

// Open the config file of the directory from which the build is done
dir = build_dir
config_file = open_or_create(dir + .cargo/config)

// Read the target of our own crate
my_target = open(.cargo/config).get_option(option: target)

// Set the target in the config file of the source
config_file.add_or_replace_option(option: target, value: my_target)

有了这样的东西,切换到不同的 MCU 将非常容易。只需更改 toml 文件中的依赖项即可。

或者,它可以被逆转。可执行文件可能有一个构建脚本,用于查找 MCU 包并复制设置。


我的问题:

  • 这可能吗?
  • 这合理吗?
  • 有更好的方法吗?

最佳答案

如果您担心配置开关“在某个时候会被遗忘”,您可以使用一些条件编译来在目标三元组错误时产生错误。

例如使用

启动 crate .lib文件
#[cfg(not(all(target_arch="...",...)))]
compile_error!("rustc is not correctly configured for this crate - the correct triple is ...");

然后在不更新 cargo 配置的情况下切换箱子会引发错误。

关于rust - 如何从 Rust 中的 crate 为整个程序设置目标三元组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com