本文主要是介绍如何根据安装源码手动安装依赖,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Homebrew 配方(Formula)核心部分:
1. url
& sha256
- 用途: 指定软件包的下载地址和校验和,用于确保下载文件的完整性。
- 示例:
url "https://example.com/download/tool-1.0.0.tar.gz" sha256 "3bcbdbb9a50cc6ff38f5d3214b6bfa929c7e35b8966f1c994b601d70b5cf9a9f"
2. depends_on
- 用途: 指定软件包的依赖项,包括 Homebrew 其他配方或系统库。确保在安装前已经安装这些依赖项。
- 示例:
depends_on "openssl"
3. install
- 用途: 这是配方的核心部分,定义了如何编译和安装软件包。通常包括配置、编译和安装命令。
- 示例:
def installsystem "./configure", "--prefix=#{prefix}"system "make", "install" end
4. test
- 用途: 定义安装后用于验证软件包是否正常工作的测试代码。这个部分在用户运行
brew test
时执行,确保软件包功能正常。 - 示例:
test doassert_match "Tool version 1.0.0", shell_output("#{bin}/tool --version") end
以macos安装openssl 为例子:
# 这个类定义了 Homebrew 用来安装 OpenSSL 3.3.1 的配方
class OpensslAT3 < Formuladesc "Cryptography and SSL/TLS Toolkit" # 描述这个工具包的用途homepage "https://openssl-library.org" # OpenSSL 的主页url "https://github.com/openssl/openssl/releases/download/openssl-3.3.1/openssl-3.3.1.tar.gz" # OpenSSL 3.3.1 的下载地址mirror "http://fresh-center.net/linux/misc/openssl-3.3.1.tar.gz" # 备用下载地址sha256 "777cd596284c883375a2a7a11bf5d2786fc5413255efab20c50d6ffe6d020b7e" # 文件的校验值,用于验证下载是否正确license "Apache-2.0" # OpenSSL 使用的许可证# 这部分用于检查 OpenSSL 是否有新的版本livecheck dourl "https://openssl-library.org/source/"regex(/href=.*?openssl[._-]v?(3(?:\.\d+)+)\.t/i)end# 下面定义了 Homebrew 如何为不同的操作系统构建和安装 OpenSSLbottle dorebuild 1sha256 arm64_sonoma: "f0dc71fe6bb0ce1618acd7c4a68dcaf5d725bd2beb2b703c1992e8ba91b5c7c7"sha256 arm64_ventura: "b39924b6b665832c7dcb46e99a5e257ca3e932313c528086631769933c78d9a0"sha256 arm64_monterey: "4cde73aab115e6c814c8a98488c742a622f26ee8d7b2cfb422b69eccbde8148f"sha256 sonoma: "5bf5e00bd262cb450490fad19b167cb684dfe8ba9c4f3dfa079871f437cac84a"sha256 ventura: "c3148aa9a81e9cd5e05f6171a9febdbe2de43a9ce1d9b8dc58bf041ce112c7fc"sha256 monterey: "f18b36971ea359ccc7d69b5b4a7ab22ccf363c45a51417d984301700c1c73fdf"sha256 x86_64_linux: "63d76975c55730b4f46dd00ed325de913e8319b7fa6dae1e03eb11cc86514c7a"end# 依赖项部分:定义了 OpenSSL 需要的依赖包depends_on "ca-certificates" # 需要的证书管理工具# 如果在 Linux 上,则需要 Perl 的几个模块on_linux doresource "Test::Harness" dourl "https://cpan.metacpan.org/authors/id/L/LE/LEONT/Test-Harness-3.48.tar.gz"mirror "http://cpan.metacpan.org/authors/id/L/LE/LEONT/Test-Harness-3.48.tar.gz"sha256 "e73ff89c81c1a53f6baeef6816841b89d3384403ad97422a7da9d1eeb20ef9c5"endresource "Test::More" dourl "https://cpan.metacpan.org/authors/id/E/EX/EXODIST/Test-Simple-1.302198.tar.gz"mirror "http://cpan.metacpan.org/authors/id/E/EX/EXODIST/Test-Simple-1.302198.tar.gz"sha256 "1dc07bcffd23e49983433c948de3e3f377e6e849ad7fe3432c717fa782024faa"endresource "ExtUtils::MakeMaker" dourl "https://cpan.metacpan.org/authors/id/B/BI/BINGOS/ExtUtils-MakeMaker-7.70.tar.gz"mirror "http://cpan.metacpan.org/authors/id/B/BI/BINGOS/ExtUtils-MakeMaker-7.70.tar.gz"sha256 "f108bd46420d2f00d242825f865b0f68851084924924f92261d684c49e3e7a74"endend# 这些命令将处理安装时的符号链接link_overwrite "bin/c_rehash", "bin/openssl", "include/openssl/*"link_overwrite "lib/libcrypto*", "lib/libssl*"link_overwrite "lib/pkgconfig/libcrypto.pc", "lib/pkgconfig/libssl.pc", "lib/pkgconfig/openssl.pc"link_overwrite "share/doc/openssl/*", "share/man/man*/*ssl"# 这个方法定义了 Homebrew 运行 `./config` 时使用的参数def configure_argsargs = %W[--prefix=#{prefix}--openssldir=#{openssldir}--libdir=libno-ssl3no-ssl3-methodno-zlib]on_linux doargs += (ENV.cflags || "").splitargs += (ENV.cppflags || "").splitargs += (ENV.ldflags || "").splitendargsend# 这部分是在 macOS 和 Linux 上指定的额外配置参数def installif OS.linux?ENV.prepend_create_path "PERL5LIB", buildpath/"lib/perl5"ENV.prepend_path "PATH", buildpath/"bin"%w[ExtUtils::MakeMaker Test::Harness Test::More].each do |r|resource(r).stage dosystem "perl", "Makefile.PL", "INSTALL_BASE=#{buildpath}"system "make", "PERL5LIB=#{ENV["PERL5LIB"]}", "CC=#{ENV.cc}"system "make", "install"endendend# 在配置和编译之前,删除干扰 OpenSSL 构建的环境变量ENV.delete("OPENSSL_LOCAL_CONFIG_DIR")# 设置 Perl 的路径,确保 OpenSSL 构建脚本不会因为 Perl 的路径变化而中断ENV["PERL"] = Formula["perl"].opt_bin/"perl" if which("perl") == Formula["perl"].opt_bin/"perl"# 指定架构相关的编译选项arch_args = []if OS.mac?arch_args += %W[darwin64-#{Hardware::CPU.arch}-cc enable-ec_nistp_64_gcc_128]elsif Hardware::CPU.intel?arch_args << (Hardware::CPU.is_64_bit? ? "linux-x86_64" : "linux-elf")elsif Hardware::CPU.arm?arch_args << (Hardware::CPU.is_64_bit? ? "linux-aarch64" : "linux-armv4")end# 创建安装目录并运行配置脚本openssldir.mkpathsystem "perl", "./Configure", *(configure_args + arch_args)# 编译并安装system "make"system "make", "install", "MANDIR=#{man}", "MANSUFFIX=ssl"# 运行测试system "make", "test", "TESTS=-test_afalg"# 防止 `brew` 删除 `certs` 和 `private` 目录touch %w[certs private].map { |subdir| openssldir/subdir/".keepme" }end# 定义 OpenSSL 的配置目录def openssldiretc/"openssl@3"end# 安装完成后的额外步骤def post_installrm(openssldir/"cert.pem") if (openssldir/"cert.pem").exist?openssldir.install_symlink Formula["ca-certificates"].pkgetc/"cert.pem"end# 安装后输出的注意事项def caveats<<~EOSA CA file has been bootstrapped using certificates from the systemkeychain. To add additional certificates, place .pem files in#{openssldir}/certsand run#{opt_bin}/c_rehashEOSend# 测试部分:验证 OpenSSL 是否正确安装和工作test do# 确保必要的配置文件存在assert_predicate pkgetc/"openssl.cnf", :exist?,"OpenSSL requires the .cnf file for some functionality"# 检查 OpenSSL 本身是否按预期工作(testpath/"testfile.txt").write("This is a test file")expected_checksum = "e2d0fe1585a63ec6009c8016ff8dda8b17719a637405a4e23c0ff81339148249"system bin/"openssl", "dgst", "-sha256", "-out", "checksum.txt", "testfile.txt"open("checksum.txt") do |f|checksum = f.read(100).split("=").last.stripassert_equal checksum, expected_checksumendend
end
1. 配置文件下载和解压
通过配方文件的开头部分,我们知道需要下载 OpenSSL 源代码并解压:
url "https://github.com/openssl/openssl/releases/download/openssl-3.3.1/openssl-3.3.1.tar.gz"
对应的手动步骤:
curl -O https://www.openssl.org/source/openssl-3.3.1.tar.gz
tar xzfv openssl-3.3.1.tar.gz
cd openssl-3.3.1
2. 配置编译选项
配方中的 configure_args
部分和 arch_args
部分提供了编译时的配置选项:
def configure_argsargs = %W[--prefix=#{prefix}--openssldir=#{openssldir}--libdir=libno-ssl3no-ssl3-methodno-zlib]
endif OS.mac?arch_args += %W[darwin64-#{Hardware::CPU.arch}-cc enable-ec_nistp_64_gcc_128]
对应的手动步骤:
./config \--prefix=/usr/local/Cellar/openssl@3/3.3.1 \--openssldir=/usr/local/openssl@3 \--libdir=lib \no-ssl3 \no-ssl3-method \no-zlib \darwin64-x86_64-cc \enable-ec_nistp_64_gcc_128
3. 编译和安装
从 install
方法中可以看到实际的编译和安装过程:
system "make"
system "make", "install", "MANDIR=#{man}", "MANSUFFIX=ssl"
对应的手动步骤:
make
make install MANDIR=/usr/local/Cellar/openssl@3/3.3.1/share/man MANSUFFIX=ssl
4. 测试和清理
虽然通常手动安装不需要执行所有测试,但如果你想确保安装无误,可以参考配方中的测试步骤:
system "make", "test", "TESTS=-test_afalg"
对应的手动步骤:
make test
这篇关于如何根据安装源码手动安装依赖的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!