homebrew-core/Formula/swift.rb

510 lines
20 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.7.2-RELEASE.tar.gz"
sha256 "8e7f4f6f70e8806128a66342d101a209c924ffb2f9ff3ccc3064e49302e24577"
license "Apache-2.0"
revision 1
# This uses the `GithubLatest` strategy because a `-RELEASE` tag is often
# created several days before the version is officially released.
livecheck do
url :stable
regex(%r{href=["']?[^"' >]*?/tag/swift[._-]v?(\d+(?:\.\d+)+)[^"' >]*?["' >]}i)
strategy :github_latest
end
bottle do
sha256 cellar: :any, arm64_ventura: "e3ba04e7a770377d62236224c78c6d05a7bcb860bd7f9611e23c8f02fa6f38ed"
sha256 cellar: :any, arm64_monterey: "0b25025b6aeefa5ca9543e540b62d6522a85e1dc99fb4dfcbcbc2dd21c002fcf"
sha256 cellar: :any, arm64_big_sur: "aea29984eec8642f1bbee40416af21f223ddb9cbf2e62ba0dc140edc33b397a9"
sha256 cellar: :any, ventura: "9f08f087d33e21cc8b532c345be05c52a293291a6937b30aa5a8af572beb8230"
sha256 cellar: :any, monterey: "4d8640ff5bb8ddc9e66497bee44eb73aa44899c023c000cc3289847002043749"
sha256 cellar: :any, big_sur: "01d10aac79ab771bd8bc052c668866ee1d72904c16821db76db824c10f0638e7"
sha256 cellar: :any_skip_relocation, x86_64_linux: "8983e0e4f3179dae0915fd6981bab7c5cdd1f881429cf61c8a53799eb99a27b0"
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.11"
# 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 "icu4c" # Used in swift-corelibs-foundation
resource "swift-corelibs-foundation" do
url "https://github.com/apple/swift-corelibs-foundation/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "92f06d557f033f4cb4a2e32e3fdd5f809713ea9716af73c959cc235946702935"
end
resource "swift-corelibs-libdispatch" do
url "https://github.com/apple/swift-corelibs-libdispatch/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "99294849b57a2e8443e957483c90405693d09bbd1684f7b96a8b063d812f7825"
# Fix race condition building both shared and static libdispatch.
patch do
url "https://github.com/Bo98/swift-corelibs-libdispatch/commit/4c5380f25706ea60e122a42879f869b2177e84b3.patch?full_index=1"
sha256 "c3fa0ceb3a79b85f3056a45bc11564b8d32cf45767156a64f0ec1c7606910007"
end
end
resource "swift-corelibs-xctest" do
url "https://github.com/apple/swift-corelibs-xctest/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "d03d496ce8c0813a9dab202d0ed8d2d4cc81a371aaf8fe017334f31150f7e198"
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.7.2-RELEASE.tar.gz"
sha256 "e1d310fb5453ef3a6abe5129a8f9029916320baf8422f3e4da94fd84f08e871b"
end
resource "cmark" do
url "https://github.com/apple/swift-cmark/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "fba4e4d40d641daa7752a64f7e45a0806be180942a265b9362168a4a02dc1c65"
end
resource "llbuild" do
url "https://github.com/apple/swift-llbuild/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "7b5bf47d124c5722a423e296b21fb81dfd246a2442d8b05c65cedf8026e2cb4c"
end
resource "swiftpm" do
url "https://github.com/apple/swift-package-manager/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "004c99ac28a54ddb352c1eddb02ac1cb1c3782dc9b8f6c2c2652ed17934a2769"
end
resource "indexstore-db" do
url "https://github.com/apple/indexstore-db/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "9c0aeae36a6c992b95200cdfad3ab08cb2e249893e312bd3732e859cbe26ffac"
end
resource "sourcekit-lsp" do
url "https://github.com/apple/sourcekit-lsp/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "9f72d9ea15f29fa31cd7e25fb97c2165707f2c57907d58195f8140d198bf5499"
end
resource "swift-driver" do
url "https://github.com/apple/swift-driver/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "193750dba9822685daff20dc61d910b6812435b44361adc8093f1b850c20eaa4"
end
resource "swift-tools-support-core" do
url "https://github.com/apple/swift-tools-support-core/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "45a17aa44e0080056f416d71f743d62dbbe03515f95ad2a9482a3ed954535fe5"
end
resource "swift-docc" do
url "https://github.com/apple/swift-docc/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "9c0c1b3bbf4f7c1e5d34c44436702912ee8181c243719e19a00ab6602ade0976"
end
resource "swift-lmdb" do
url "https://github.com/apple/swift-lmdb/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "435ebaec63d134020887d93562bfe1f9aa2cfca94dfec7d9bdaf35a8247d0a46"
end
resource "swift-docc-render-artifact" do
url "https://github.com/apple/swift-docc-render-artifact/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "868ed7df904c8fd52d3647a2f6ea74264e888a762ad9d06565c44d10d2e93af5"
end
resource "swift-docc-symbolkit" do
url "https://github.com/apple/swift-docc-symbolkit/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "15ccd287f6287210dee146bcb32962593c4f6a8f33b21fd293836dd17fb7c5e3"
end
resource "swift-markdown" do
url "https://github.com/apple/swift-markdown/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "6e82c713cd13851e3621bd3318eb1228cded7c105aa24d57ca94dbb00ae4ba96"
end
resource "swift-cmark-gfm" do
url "https://github.com/apple/swift-cmark/archive/refs/tags/swift-5.7.2-RELEASE-gfm.tar.gz"
sha256 "b2546ac1e364b860e1094a4ec45fa046fdc1c540aedda61e3f884bd0a0b4eb4b"
end
resource "swift-experimental-string-processing" do
url "https://github.com/apple/swift-experimental-string-processing/archive/refs/tags/swift-5.7.2-RELEASE.tar.gz"
sha256 "a5f8db10c736cf4e33ab813fa1db1783c936e9587b79e15b1fec643cd78c7703"
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/5.0.1.tar.gz"
sha256 "ec1ad699c30f0db45520006c63a88cc1c946a7d7b36dff32a96460388c0a4af2"
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"
if OS.mac?
# String processing is only available on macOS 13+ SDK, concurrency on macOS 12+ SDK
swiftpm_interface_build_scripts = [
workspace/"swiftpm/Sources/PackageDescription/CMakeLists.txt",
workspace/"swiftpm/Sources/PackagePlugin/CMakeLists.txt",
]
inreplace swiftpm_interface_build_scripts,
"-enable-library-evolution>",
"\\0 " \
'"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-concurrency-module-import>" ' \
'"$<$<COMPILE_LANGUAGE:Swift>:SHELL:-Xfrontend -disable-implicit-string-processing-module-import>"'
inreplace workspace/"swiftpm/Package.swift",
'"999.0"]),',
'\\0 ' \
'.unsafeFlags(["-Xfrontend", ' \
'"-disable-implicit-concurrency-module-import"], .when(platforms: [.macOS])), ' \
'.unsafeFlags(["-Xfrontend", ' \
'"-disable-implicit-string-processing-module-import"], .when(platforms: [.macOS])),'
end
# Fix lldb Python module not being installed (needed for `swift repl`)
lldb_cmake_caches = [
workspace/"llvm-project/lldb/cmake/caches/Apple-lldb-macOS.cmake",
workspace/"llvm-project/lldb/cmake/caches/Apple-lldb-Linux.cmake",
]
inreplace lldb_cmake_caches, "repl_swift", "lldb-python-scripts \\0"
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
static-mirror-lib
]
llvm_components = %w[
llvm-cov llvm-profdata IndexStore clang
clang-resource-headers compiler-rt clangd
clang-features-file
]
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
--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
--build-swift-private-stdlib=0
--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}
--darwin-deployment-version-osx=#{MacOS.version}
--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-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=0
--foundation
--libdispatch
--xctest
--skip-early-swift-driver
--host-target=linux-#{Hardware::CPU.arch}
--stdlib-deployment-targets=linux-#{Hardware::CPU.arch}
--build-swift-static-stdlib
--build-swift-static-sdk-overlay
--install-foundation
--install-libdispatch
--install-xctest
]
rpaths = [loader_path, rpath, rpath(target: lib/"swift/linux")]
extra_cmake_options << "-DCMAKE_INSTALL_RPATH=#{rpaths.join(":")}"
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|
next if sdk.version < :big_sur
# https://github.com/apple/swift/issues/62765
next if sdk.version == :ventura
system "#{prefix}#{install_prefix}/bin/swift", "build-sdk-interfaces",
"-sdk", sdk.path,
"-o", "#{prefix}#{install_prefix}/lib/swift/macosx/prebuilt-modules",
"-log-path", logs/"build-sdk-interfaces",
"-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
# Don't use global cache which is long-lasting and often requires clearing.
module_cache = testpath/"ModuleCache"
module_cache.mkdir
# Temporary hack while macOS 13 SDK prebuilding is disabled.
if MacOS.version == :ventura
ENV.remove_macosxsdk
ENV["SDKROOT"] = "/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk"
end
(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 -module-cache-path #{module_cache} -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 -module-cache-path #{module_cache} -v foundation-test.swift")
assert_match "www.swift.org\n", output
# Test compiler
system "#{bin}/swiftc", "-module-cache-path", module_cache, "-v", "foundation-test.swift", "-o", "foundation-test"
output = shell_output("./foundation-test")
assert_match "www.swift.org\n", output
# Test Swift Package Manager
ENV["SWIFTPM_MODULECACHE_OVERRIDE"] = module_cache
mkdir "swiftpmtest" do
system "#{bin}/swift", "package", "init", "--type=executable"
rm "Sources/swiftpmtest/swiftpmtest.swift"
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"
else
opt_libexec/"lib/swift"
end.to_s
assert_equal expected_resource_dir, default_resource_dir
end
end