如何检查 CPU 类型以从 Homebrew 获取源代码?

How to check CPU type to source from Homebrew?

我同时使用 M1 和 Intel MBP。他们确实与我的 dotfiles 共享相同的 .zshrc。我想根据 CPU 类型获取不同的文件。例如:

在 M1 上,仅应获取这些文件:

source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

在 Intel 上,仅应获取这些文件:

source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

我怎样才能做到这一点?

更新:我现在使用@chepner 的


我有 ended up 和一个简单的 if 语句:

local cpu_name=$(sysctl -n machdep.cpu.brand_string)

if [ $cpu_name = "Apple M1" ]; then
  source /opt/homebrew/share/zsh-autosuggestions/zsh-autosuggestions.zsh
  source /opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
else
  source /usr/local/share/zsh-autosuggestions/zsh-autosuggestions.zsh
  source /usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
fi

zsh 在启动时自动设置 CPUTYPE,因此您可以使用它代替 运行 sysctl.

declare -A foo
foo[x86_64]=/usr/local/
foo[arm64]=/opt/homebrew

source $foo[$CPUTYPE]/share/zsh-autosuggestions/zsh-autosuggetions.zsh
source $foo[$CPUTYPE]/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh

请注意,在某种意义上,您只是使用 CPU 类型作为特定机器的代理。 (您可以很容易地让两台不同的 Intel 机器具有不同的 ZSH 配置文件位置。)更好的解决方案可能是使用 $HOST 而不是 $CPUTYPE 来识别特定于机器的选项。