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

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

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

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

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

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

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

我对buildscript的镜像可能看起来像(伪):

// 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 并复制设置。

我的问题:
  • 这可能吗?
  • 这很明智吗?
  • 有更好的方法吗?
  • 最佳答案

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

    例如使用以下命令启动 crate .lib文件

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

    然后,在不更新 cargo 配置的情况下切换 crate 将引发错误。

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

    10-12 20:09