You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Homebrew Formula以根据目标环境部署对应二进制文件?

跨平台GraalVM应用Homebrew Formula配置方案

针对你的需求,要让同一个Formula适配Intel Mac、M1 Mac和Linux(Linux Brew),最规范的方式是利用Homebrew的条件分支或Bottle机制,以下是具体实现:

方案一:使用条件分支直接指定平台资源

这种方式直观,适合快速适配不同平台的预编译包:

class MyApp < Formula
  desc "MyApp description"
  license "Apache-2.0"
  VERSION = "0.4"

  # Intel Mac
  on_macos do
    if Hardware::CPU.intel?
      url "https://github.com/account/myapp/releases/download/#{VERSION}/myapp-osx-intel-#{VERSION}.tar.gz"
      sha256 "替换为intel包的sha256哈希值"
    else
      # M1/M2 Mac
      url "https://github.com/account/myapp/releases/download/#{VERSION}/myapp-osx-m1-#{VERSION}.tar.gz"
      sha256 "替换为M1包的sha256哈希值"
    end
  end

  # Linux(Linux Brew)
  on_linux do
    url "https://github.com/account/myapp/releases/download/#{VERSION}/myapp-linux-#{VERSION}.tar.gz"
    sha256 "替换为Linux包的sha256哈希值"
  end

  def install
    libexec.install Dir["*"]
    bin.install_symlink libexec/"myapp"
  end
end

方案二:使用Homebrew Bottle机制(推荐)

Bottle是Homebrew官方推荐的预编译包分发方式,更符合社区规范,同时支持自动匹配系统版本和架构:

Bottle命名规则说明

  • Mac平台:格式为[架构]_[系统版本],比如arm64_sonoma(M系列芯片+Sonoma系统)、x86_64_ventura(Intel芯片+Ventura系统)
  • Linux平台:格式为x86_64_linux(x86_64架构Linux),若需支持ARM Linux可写aarch64_linux

实现代码

class MyApp < Formula
  desc "MyApp description"
  license "Apache-2.0"
  VERSION = "0.4"

  bottle do
    # 指定预编译包的根下载地址
    root_url "https://github.com/account/myapp/releases/download/#{VERSION}"
    # M1/M2 Mac(Sonoma系统)
    sha256 cellar: :any_skip_relocation, arm64_sonoma: "替换为M1包的sha256哈希值"
    # Intel Mac(Ventura系统)
    sha256 cellar: :any_skip_relocation, x86_64_ventura: "替换为intel包的sha256哈希值"
    # Intel Mac(Monterey系统)
    sha256 cellar: :any_skip_relocation, x86_64_monterey: "替换为intel包的sha256哈希值"
    # Linux(x86_64)
    sha256 cellar: :any_skip_relocation, x86_64_linux: "替换为Linux包的sha256哈希值"
  end

  # 为兼容未配置Bottle的情况,可添加默认url(可选)
  url "https://github.com/account/myapp/releases/download/#{VERSION}/myapp-osx-intel-#{VERSION}.tar.gz"
  sha256 "替换为默认包的sha256哈希值"

  def install
    libexec.install Dir["*"]
    bin.install_symlink libexec/"myapp"
  end
end

补充说明

  • cellar: :any_skip_relocation表示预编译包不需要重定位,完全适配GraalVM生成的独立可执行文件
  • 可根据需求添加更多Mac系统版本的Bottle配置(如Big Sur),只要对应上传同名预编译包即可

内容的提问来源于stack exchange,提问作者clunven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:07:03