如何在整个程序中从Rust中的板条箱设置目标三元组?

How to set the target triplet from a crate in Rust for the entire program?

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

我目前的想法是为我拥有的每个MCU都装一个板条箱。 MCU包装箱将包含该特定设备的GPIO,SPI和UART之类的实现。

系统的设计应使MCU板条箱可以换成另一个MCU的板条箱,以该新MCU为目标。它也应该尽可能的容易。

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

基本上,我希望将板条箱的目标应用于整个构建。

我已经在网络上进行了一些搜索,看是否曾经有过,但是要么不存在,要么我的搜索技能不足。

我的想法是,MCU板条箱中的构建脚本可以将其设置复制到构建实例化程序的.cargo / config文件中。

我对buildscript的印象可能是(伪):

1
2
3
4
5
6
7
8
9
// 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板条箱并复制设置。

我的问题:

  • 这可能吗?
  • 这明智吗?
  • 有更好的方法吗?


如果您担心配置开关会"在某个时候会被遗忘",那么如果目标三元组是错误的,则可以使用一些条件编译来创建错误。

例如 用以下命令启动包装箱.lib文件

1
2
#[cfg(not(all(target_arch="...",...)))]
compile_error!("rustc is not correctly configured for this crate - the correct triple is ...");

然后,在不更新货物配置的情况下切换板条箱将引发错误。