homebrew-core/Formula/swift.rb

457 lines
17 KiB
Ruby

class Swift < Formula
include Language::Python::Shebang
desc "High-performance system programming language"
homepage "https://www.swift.org"
# NOTE: Keep version in sync with resources below
url "https://github.com/apple/swift/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "15e3e092ad165c35eef59cf2449e3089a0bb1906272127ba58fa31fc53512af5"
license "Apache-2.0"
livecheck do
url "https://www.swift.org/download/"
regex(/Releases<.*?>Swift v?(\d+(?:\.\d+)+)</im)
end
bottle do
sha256 cellar: :any, arm64_monterey: "a6879aa6542113703e6b07156e632a0c4dc2a546d2a8b772e6d991c98f4fb2af"
sha256 cellar: :any, arm64_big_sur: "090e609add8b340687d2821e39c13d836d1a6de471150388a8b22fed3e418ae3"
sha256 cellar: :any, monterey: "9faa4c3d01c300341267b2826032719f199623a20fc2146a4fe686619fa00c11"
sha256 cellar: :any, big_sur: "cba1e95faaf1995c1ae38e0a0c2359f2ec52c78bb891e1e3bd8ef535fad31543"
sha256 cellar: :any_skip_relocation, x86_64_linux: "1ce81e9a00f46be2ee2bb1983402bc7308946b873535388934537132c56f956e"
end
keg_only :provided_by_macos
depends_on "cmake" => :build
depends_on "ninja" => :build
# Has strict requirements on the minimum version of Xcode. See _SUPPORTED_XCODE_BUILDS:
# https://github.com/apple/swift/tree/swift-#{version}-RELEASE/utils/build-script
# This is mostly community sourced, so may be not necessarily be accurate.
depends_on xcode: ["13.0", :build]
depends_on "python@3.10"
# HACK: this should not be a test dependency but is due to a limitation with fails_with
uses_from_macos "llvm" => [:build, :test]
uses_from_macos "rsync" => :build
uses_from_macos "curl"
uses_from_macos "libedit"
uses_from_macos "libxml2"
uses_from_macos "ncurses"
uses_from_macos "sqlite"
uses_from_macos "zlib"
on_linux do
depends_on "six" => :build
depends_on "icu4c" # Used in swift-corelibs-foundation
resource "swift-corelibs-foundation" do
url "https://github.com/apple/swift-corelibs-foundation/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "3fa96321729ea1e99847320bc3b5eefcbc39ba57eb8750a16700afa0173b6bb0"
end
resource "swift-corelibs-libdispatch" do
url "https://github.com/apple/swift-corelibs-libdispatch/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "d2bbfb5b98d129caa2c6bd7662c850bf57cb434572d09844b56641c4558906ab"
end
resource "swift-corelibs-xctest" do
url "https://github.com/apple/swift-corelibs-xctest/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "a5be10d08603e07adb19755ecd1d3aac36f289eb5a17ca76ee4dec8b33d9c3ea"
end
end
# Currently requires Clang to build successfully.
fails_with :gcc
resource "llvm-project" do
url "https://github.com/apple/llvm-project/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "865b63b2a163ab26e6c2303278144b5653b938eeebc491738983001801928080"
end
resource "cmark" do
url "https://github.com/apple/swift-cmark/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "7ef6054a08f6bbd2add72759317ddd37a2a357e043e57c021aa1a17343e37d7d"
end
resource "llbuild" do
url "https://github.com/apple/swift-llbuild/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "ec8d4cfbf9238014e1b23eab93393de7eb6512e8dd4c4aef6005abd95fccfaba"
end
resource "swiftpm" do
url "https://github.com/apple/swift-package-manager/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "add47e6cc7c6b689b2788499497bee806957e7e62da9d2b7aef9f13576d40483"
end
resource "indexstore-db" do
url "https://github.com/apple/indexstore-db/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "edb2d499d47852a99708e799b4779cf40540a7bf234b64c08a71e30e726582a2"
end
resource "sourcekit-lsp" do
url "https://github.com/apple/sourcekit-lsp/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "93b1568621304ea481b2681b94a0ac6af6b766353c8344ddf06fe467886dc7f6"
end
resource "swift-driver" do
url "https://github.com/apple/swift-driver/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "0d4f163f50ad5dd02a066397661c947eb5c9ee8336617780310b8c00c99cb760"
end
resource "swift-tools-support-core" do
url "https://github.com/apple/swift-tools-support-core/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "20641657474b7c38951866ca9abc047fd860d58c26e834b94e4f606b58e38dc3"
end
resource "swift-docc" do
url "https://github.com/apple/swift-docc/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "cfd379cc42bac5a43fb214fd202f917f85541029b6a24fcb8e4494bebc57b47f"
# Fix build with newer Pythons.
# Remove with Swift 5.7.
patch do
url "https://github.com/apple/swift-docc/commit/bff70b847008f91ac729cfd299a85481eef3f581.patch?full_index=1"
sha256 "b3378f7ed6042baa4889f37445221b6dd6cd6828f7db4b66707d361bc30477ca"
end
end
resource "swift-lmdb" do
url "https://github.com/apple/swift-lmdb/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "d3448107f574c337c1c1dee1e73e59ae08556335496f2ab6acf09fd1c9552187"
end
resource "swift-docc-render-artifact" do
url "https://github.com/apple/swift-docc-render-artifact/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "36995677797261f056d367454e250f737acb0abd104fde83395c09a9bd55ac2b"
end
resource "swift-docc-symbolkit" do
url "https://github.com/apple/swift-docc-symbolkit/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "428b20c3fea0eb661af187591ee03f2c3095d32d0be78160ab5ccd20a36aafe2"
end
resource "swift-markdown" do
url "https://github.com/apple/swift-markdown/archive/refs/tags/swift-5.6-RELEASE.tar.gz"
sha256 "98a1d9ab569b333dcdbb4e23461713577f68f01aabdcbaafa5e099afe45a0de5"
end
resource "swift-cmark-gfm" do
url "https://github.com/apple/swift-cmark/archive/refs/tags/swift-5.6-RELEASE-gfm.tar.gz"
sha256 "cb83eea7c0ae8290d5c47de08284b897b07b2a1b279eab336ecee81a181e57a0"
end
# To find the version to use, check the release/#{version.major_minor} entry of:
# https://github.com/apple/swift/blob/swift-#{version}-RELEASE/utils/update_checkout/update-checkout-config.json
resource "swift-argument-parser" do
url "https://github.com/apple/swift-argument-parser/archive/refs/tags/1.0.3.tar.gz"
sha256 "a4d4c08cf280615fe6e00752ef60e28e76f07c25eb4706a9269bf38135cd9c3f"
end
# As above: refer to update-checkout-config.json
resource "swift-atomics" do
url "https://github.com/apple/swift-atomics/archive/refs/tags/1.0.2.tar.gz"
sha256 "c8b88186db4902dc5109340f4a745ea787cb2aa9533c7e6d1e634549f9e527b1"
end
# As above: refer to update-checkout-config.json
resource "swift-collections" do
url "https://github.com/apple/swift-collections/archive/refs/tags/1.0.1.tar.gz"
sha256 "575cf0f88d9068411f9acc6e3ca5d542bef1cc9e87dc5d69f7b5a1d5aec8c6b6"
end
# As above: refer to update-checkout-config.json
resource "swift-crypto" do
url "https://github.com/apple/swift-crypto/archive/refs/tags/1.1.5.tar.gz"
sha256 "86d6c22c9f89394fd579e967b0d5d0b6ce33cdbf52ba70f82fa313baf70c759f"
end
# As above: refer to update-checkout-config.json
resource "swift-numerics" do
url "https://github.com/apple/swift-numerics/archive/refs/tags/1.0.1.tar.gz"
sha256 "3ff05bb89c907d70f51dfff794ea3354a2630488925bf53382246d25089ec742"
end
# As above: refer to update-checkout-config.json
resource "swift-system" do
url "https://github.com/apple/swift-system/archive/refs/tags/1.1.1.tar.gz"
sha256 "865b8c380455eef27e73109835142920c60ae4c4f4178a3d12ad04acc83f1371"
end
# As above: refer to update-checkout-config.json
resource "yams" do
url "https://github.com/jpsim/Yams/archive/refs/tags/4.0.2.tar.gz"
sha256 "8bbb28ef994f60afe54668093d652e4d40831c79885fa92b1c2cd0e17e26735a"
end
# As above: refer to update-checkout-config.json
resource "swift-nio" do
url "https://github.com/apple/swift-nio/archive/refs/tags/2.31.2.tar.gz"
sha256 "8818b8e991d36e886b207ae1023fa43c5eada7d6a1951a52ad70f7f71f57d9fe"
end
# As above: refer to update-checkout-config.json
resource "swift-nio-ssl" do
url "https://github.com/apple/swift-nio-ssl/archive/refs/tags/2.15.0.tar.gz"
sha256 "9ab1f0e347fad651ed5ccadc13d54c4306e6f5cd21908a4ba7d1334278a4cd55"
end
# Homebrew-specific patch to make the default resource directory use opt rather than Cellar.
# This fixes output binaries from `swiftc` having a runpath pointing to the Cellar.
# This should only be removed if an alternative solution is implemented.
patch do
url "https://raw.githubusercontent.com/Homebrew/formula-patches/5e4d9bb4d04c7c9004e95fecba362a843dc00bdd/swift/homebrew-resource-dir.diff"
sha256 "5210ca0fd95b960d596c058f5ac76412a6987d2badf5394856bb9e31d3c68833"
end
def install
workspace = buildpath.parent
build = workspace/"build"
install_prefix = if OS.mac?
toolchain_prefix = "/Swift-#{version.major_minor}.xctoolchain"
"#{toolchain_prefix}/usr"
else
"/libexec"
end
ln_sf buildpath, workspace/"swift"
resources.each { |r| r.stage(workspace/r.name) }
# Fix C++ header path. It wrongly assumes that it's relative to our shims.
if OS.mac?
inreplace workspace/"swift/utils/build-script-impl",
"HOST_CXX_DIR=$(dirname \"${HOST_CXX}\")",
"HOST_CXX_DIR=\"#{MacOS::Xcode.toolchain_path}/usr/bin\""
end
# Disable invoking SwiftPM in a sandbox while building some projects.
# This conflicts with Homebrew's sandbox.
helpers_using_swiftpm = [
workspace/"indexstore-db/Utilities/build-script-helper.py",
workspace/"sourcekit-lsp/Utilities/build-script-helper.py",
workspace/"swift-docc/build-script-helper.py",
]
inreplace helpers_using_swiftpm, "swiftpm_args = [", "\\0'--disable-sandbox',"
inreplace workspace/"swift-docc/build-script-helper.py",
"[swift_exec, 'package',",
"\\0 '--disable-sandbox',"
# Fix finding of brewed sqlite3.h.
unless OS.mac?
inreplace workspace/"swift-tools-support-core/Sources/TSCclibc/include/module.modulemap",
"header \"csqlite3.h\"",
"header \"#{Formula["sqlite3"].opt_include/"sqlite3.h"}\""
end
# Fix swift-driver somehow bypassing the shims.
inreplace workspace/"swift-driver/Utilities/build-script-helper.py",
"-DCMAKE_C_COMPILER:=clang",
"-DCMAKE_C_COMPILER:=#{which(ENV.cc)}"
inreplace workspace/"swift-driver/Utilities/build-script-helper.py",
"-DCMAKE_CXX_COMPILER:=clang++",
"-DCMAKE_CXX_COMPILER:=#{which(ENV.cxx)}"
# Build SwiftPM and dependents in release mode
inreplace workspace/"swiftpm/Utilities/bootstrap",
"-DCMAKE_BUILD_TYPE:=Debug",
"-DCMAKE_BUILD_TYPE:=Release"
mkdir build do
# List of components to build
swift_components = %w[
compiler clang-resource-dir-symlink tools
editor-integration toolchain-tools license
sourcekit-xpc-service swift-remote-mirror
swift-remote-mirror-headers parser-lib stdlib
]
llvm_components = %w[
llvm-cov llvm-profdata IndexStore clang
clang-resource-headers compiler-rt clangd
]
if OS.mac?
swift_components << "back-deployment"
llvm_components << "dsymutil"
end
if OS.linux?
swift_components += %w[
sdk-overlay
autolink-driver
sourcekit-inproc
]
llvm_components << "lld"
end
args = %W[
--host-cc=#{which(ENV.cc)}
--host-cxx=#{which(ENV.cxx)}
--release --assertions
--no-swift-stdlib-assertions
--build-subdir=#{build}
--lldb --llbuild --swiftpm --swift-driver
--swiftdocc --indexstore-db --sourcekit-lsp
--skip-build-benchmarks
--jobs=#{ENV.make_jobs}
--verbose-build
--workspace=#{workspace}
--install-destdir=#{prefix}
--toolchain-prefix=#{toolchain_prefix}
--install-prefix=#{install_prefix}
--swift-include-tests=0
--llvm-include-tests=0
--skip-build-benchmarks
--install-swift
--swift-install-components=#{swift_components.join(";")}
--install-llvm
--llvm-install-components=#{llvm_components.join(";")}
--install-lldb
--install-llbuild
--install-swiftpm
--install-swift-driver
--install-swiftdocc
--install-sourcekit-lsp
]
extra_cmake_options = []
if OS.mac?
args += %W[
--host-target=macosx-#{Hardware::CPU.arch}
--build-swift-dynamic-stdlib=0
--build-swift-dynamic-sdk-overlay=0
--stdlib-deployment-targets=
--swift-darwin-supported-archs=#{Hardware::CPU.arch}
--swift-darwin-module-archs=#{Hardware::CPU.arch}
--back-deploy-concurrency
--install-back-deploy-concurrency
--lldb-no-debugserver
--lldb-use-system-debugserver
]
extra_cmake_options += %w[
-DLLDB_FRAMEWORK_COPY_SWIFT_RESOURCES=0
-DCMAKE_INSTALL_RPATH=@loader_path
]
end
if OS.linux?
args += %W[
--libcxx
--foundation
--libdispatch
--xctest
--skip-early-swift-driver
--host-target=linux-#{Hardware::CPU.arch}
--stdlib-deployment-targets=linux-#{Hardware::CPU.arch}
--install-libcxx
--install-foundation
--install-libdispatch
--install-xctest
]
extra_cmake_options << "-DCMAKE_INSTALL_RPATH=$ORIGIN:$ORIGIN/../lib:$ORIGIN/../lib/swift/linux"
ENV.prepend_path "PATH", Formula["python@3.10"].opt_libexec/"bin"
ENV["CMAKE_Swift_COMPILER"] = "" # Ignore our shim
end
args << "--extra-cmake-options=#{extra_cmake_options.join(" ")}"
ENV["SKIP_XCODE_VERSION_CHECK"] = "1"
system "#{workspace}/swift/utils/build-script", *args
end
if OS.mac?
# Prebuild modules for faster first startup.
ENV["SWIFT_EXEC"] = "#{prefix}#{install_prefix}/bin/swiftc"
MacOS.sdk_locator.all_sdks.each do |sdk|
system "#{prefix}#{install_prefix}/bin/swift-build-sdk-interfaces",
"-sdk", sdk.path,
"-o", "#{prefix}#{install_prefix}/lib/swift/macosx/prebuilt-modules",
"-v"
end
else
# Strip debugging info to make the bottle relocatable.
binaries_to_strip = Pathname.glob("#{prefix}#{install_prefix}/{bin,lib/swift/pm}/**/*").select do |f|
f.file? && f.elf?
end
system "strip", "--strip-debug", "--preserve-dates", *binaries_to_strip
end
bin.install_symlink Dir["#{prefix}#{install_prefix}/bin/{swift,sil,sourcekit}*"]
man1.install_symlink "#{prefix}#{install_prefix}/share/man/man1/swift.1"
elisp.install_symlink "#{prefix}#{install_prefix}/share/emacs/site-lisp/swift-mode.el"
doc.install_symlink Dir["#{prefix}#{install_prefix}/share/doc/swift/*"]
rewrite_shebang detected_python_shebang, *Dir["#{prefix}#{install_prefix}/bin/*.py"]
end
def caveats
on_macos do
<<~EOS
An Xcode toolchain has been installed to:
#{opt_prefix}/Swift-#{version.major_minor}.xctoolchain
This can be symlinked for use within Xcode:
ln -s #{opt_prefix}/Swift-#{version.major_minor}.xctoolchain ~/Library/Developer/Toolchains/Swift-#{version.major_minor}.xctoolchain
EOS
end
end
test do
(testpath/"test.swift").write <<~'EOS'
let base = 2
let exponent_inner = 3
let exponent_outer = 4
var answer = 1
for _ in 1...exponent_outer {
for _ in 1...exponent_inner {
answer *= base
}
}
print("(\(base)^\(exponent_inner))^\(exponent_outer) == \(answer)")
EOS
output = shell_output("#{bin}/swift -v test.swift")
assert_match "(2^3)^4 == 4096\n", output
# Test accessing Foundation
(testpath/"foundation-test.swift").write <<~'EOS'
import Foundation
let swifty = URLComponents(string: "https://www.swift.org")!
print("\(swifty.host!)")
EOS
output = shell_output("#{bin}/swift -v foundation-test.swift")
assert_match "www.swift.org\n", output
# Test compiler
system "#{bin}/swiftc", "-v", "foundation-test.swift", "-o", "foundation-test"
output = shell_output("./foundation-test")
assert_match "www.swift.org\n", output
# Test Swift Package Manager
mkdir "swiftpmtest" do
on_macos do
# Swift Package Manager does not currently support using SDKROOT.
ENV.remove_macosxsdk
end
system "#{bin}/swift", "package", "init", "--type=executable"
cp "../foundation-test.swift", "Sources/swiftpmtest/main.swift"
system "#{bin}/swift", "build", "--verbose", "--disable-sandbox"
assert_match "www.swift.org\n", shell_output("#{bin}/swift run --disable-sandbox")
end
# Make sure the default resource directory is not using a Cellar path
default_resource_dir = JSON.parse(shell_output("#{bin}/swift -print-target-info"))["paths"]["runtimeResourcePath"]
expected_resource_dir = if OS.mac?
(opt_prefix/"Swift-#{version.major_minor}.xctoolchain/usr/lib/swift").to_s
else
(opt_libexec/"lib/swift").to_s
end
assert_equal expected_resource_dir, default_resource_dir
end
end