Skip to content

Commit

Permalink
flang: link to C++ standard lib automatically
Browse files Browse the repository at this point in the history
This addresses issues users have had at Homebrew/discussions#5631.

See also discussion at #192505.

We could probably rebuild LLVM with `LLVM_ENABLE_EH=OFF`, but that
breaks the ABI so we should save it for LLVM 20 at the earliest.
  • Loading branch information
carlocab committed Oct 2, 2024
1 parent 22c68a0 commit 96ca32e
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions Formula/f/flang.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class Flang < Formula
# Building with GCC fails at linking with an obscure error.
fails_with :gcc

def flang_driver
"flang-new"
end

def install

Check failure on line 34 in Formula/f/flang.rb

View workflow job for this annotation

GitHub Actions / Linux

`brew install --verbose --formula --build-bottle flang` failed on Linux!

==> Fetching dependencies for flang: llvm ==> Fetching llvm ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 /usr/bin/env /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --show-error --user-agent Linuxbrew/4.4.0-28-ga161342\ \(Linux\;\ x86_64\ Ubuntu\ 22.04.5\ LTS\)\ curl/7.81.0 --header Accept-Language:\ en --fail --silent --retry 3 --header Accept:\ application/vnd.oci.image.index.v1+json --header Authorization:\ Bearer\ QQ== --remote-time --output /home/linuxbrew/.cache/Homebrew/downloads/142f41cc6a594eecaf1d98c3e0dd10b761791c8a0784469ca8882d1343a0b1e5--llvm-19.1.1-1.bottle_manifest.json.incomplete --location https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 curl: (22) The requested URL returned error: 404 ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/blobs/sha256:4066cc4bba868660b63f4130073b19d551b82d2cbeb9fe6d96e5a68d864ee305 Already downloaded: /home/linuxbrew/.cache/Homebrew/downloads/2ca8abc8359570b7d7b278c6a9e823bffda5d9c9959a86818cb11493a7e07a2b--llvm--19.1.1.x86_64_linux.bottle.1.tar.gz ==> Verifying checksum for '2ca8abc8359570b7d7b278c6a9e823bffda5d9c9959a86818cb11493a7e07a2b--llvm--19.1.1.x86_64_linux.bottle.1.tar.gz' ==> Fetching flang ==> Downloading https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.1/llvm-project-19.1.1.src.tar.xz Already downloaded: /home/linuxbrew/.cache/Homebrew/downloads/78245f86a6af03e9cf3d6ae486d321219a1776ad6973d78c8ce7c1aa8d5dadd4--llvm-project-19.1.1.src.tar.xz ==> Verifying checksum for '78245f86a6af03e9cf3d6ae486d321219a1776ad6973d78c8ce7c1aa8d5dadd4--llvm-project-19.1.1.src.tar.xz' ==> Installing dependencies for flang: llvm ==> Installing flang dependency: llvm ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 /usr/bin/env /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --show-error --user-agent Linuxbrew/4.4.0-28-ga161342\ \(Linux\;\ x86_64\ Ubuntu\ 22.04.5\ LTS\)\ curl/7.81.0 --header Accept-Language:\ en --fail --silent --retry 3 --header Accept:\ application/vnd.oci.image.index.v1+json --header Authorization:\ Bearer\ QQ== --remote-time --output /home/linuxbrew/.cache/Homebrew/downloads/142f41cc6a594eecaf1d98c3e0dd10b761791c8a0784469ca8882d1343a0b1e5--llvm-19.1.1-1.bottle_manifest.json.incomplete --location https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 curl: (22) The requested URL returned error: 404 ==> Verifying attestation for llvm Error: The bottle for llvm has an invalid build provenance attestation. This may indicate that the bottle was not produced by the expected tap, or was maliciously inserted into the expected tap's bottle storage. Additional context: no attestation matches subject: llvm--19.1.1.x86_64_linux.bottle.1.tar.gz ::error::The bottle for llvm has an invalid build provenance attestation.%0A%0AThis may indicate that the bottle was not produced by the expected%0Atap, or was maliciously inserted into the expected tap's bottle%0Astorage.%0A%0AAdditional context:%0A%0Ano attestation matches subject: llvm--19.1.1.x86_64_linux.bottle.1.tar.gz%0A

Check failure on line 34 in Formula/f/flang.rb

View workflow job for this annotation

GitHub Actions / macOS 14-arm64

`brew install --verbose --formula --build-bottle flang` failed on macOS Sonoma (14) on Apple Silicon!

==> Fetching dependencies for flang: llvm ==> Fetching llvm ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 /usr/bin/env /opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --show-error --user-agent Homebrew/4.4.0-28-ga161342\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 14.4.1\)\ curl/8.4.0 --header Accept-Language:\ en --fail --silent --retry 3 --header Accept:\ application/vnd.oci.image.index.v1+json --header Authorization:\ Bearer\ QQ== --remote-time --output /Users/brew/Library/Caches/Homebrew/downloads/142f41cc6a594eecaf1d98c3e0dd10b761791c8a0784469ca8882d1343a0b1e5--llvm-19.1.1-1.bottle_manifest.json.incomplete --location https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 curl: (22) The requested URL returned error: 404 ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/blobs/sha256:0ff527db10ff4a3114e5cea664b9033da0f7582fe4af1f3b4813d20ac81fa8cf Already downloaded: /Users/brew/Library/Caches/Homebrew/downloads/473ffa6658ccd49868326b2fdf1d5df1589e2133387ccd399e906247aae6184b--llvm--19.1.1.arm64_sonoma.bottle.1.tar.gz ==> Verifying checksum for '473ffa6658ccd49868326b2fdf1d5df1589e2133387ccd399e906247aae6184b--llvm--19.1.1.arm64_sonoma.bottle.1.tar.gz' ==> Fetching flang ==> Downloading https://github.com/llvm/llvm-project/releases/download/llvmorg-19.1.1/llvm-project-19.1.1.src.tar.xz Already downloaded: /Users/brew/Library/Caches/Homebrew/downloads/78245f86a6af03e9cf3d6ae486d321219a1776ad6973d78c8ce7c1aa8d5dadd4--llvm-project-19.1.1.src.tar.xz ==> Verifying checksum for '78245f86a6af03e9cf3d6ae486d321219a1776ad6973d78c8ce7c1aa8d5dadd4--llvm-project-19.1.1.src.tar.xz' ==> Installing dependencies for flang: llvm ==> Installing flang dependency: llvm ==> Downloading https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 /usr/bin/env /opt/homebrew/Library/Homebrew/shims/shared/curl --disable --cookie /dev/null --globoff --show-error --user-agent Homebrew/4.4.0-28-ga161342\ \(Macintosh\;\ arm64\ Mac\ OS\ X\ 14.4.1\)\ curl/8.4.0 --header Accept-Language:\ en --fail --silent --retry 3 --header Accept:\ application/vnd.oci.image.index.v1+json --header Authorization:\ Bearer\ QQ== --remote-time --output /Users/brew/Library/Caches/Homebrew/downloads/142f41cc6a594eecaf1d98c3e0dd10b761791c8a0784469ca8882d1343a0b1e5--llvm-19.1.1-1.bottle_manifest.json.incomplete --location https://ghcr.io/v2/homebrew/core/llvm/manifests/19.1.1-1 curl: (22) The requested URL returned error: 404 ==> Verifying attestation for llvm Error: The bottle for llvm has an invalid build provenance attestation. This may indicate that the bottle was not produced by the expected tap, or was maliciously inserted into the expected tap's bottle storage. Additional context: no attestation matches subject: llvm--19.1.1.arm64_sonoma.bottle.1.tar.gz ::error::The bottle for llvm has an invalid build provenance attestation.%0A%0AThis may indicate that the bottle was not produced by the expected%0Atap, or was maliciously inserted into the expected tap's bottle%0Astorage.%0A%0AAdditional context:%0A%0Ano attestation matches subject: llvm--19.1.1.arm64_sonoma.bottle.1.tar.gz%0A
llvm = Formula["llvm"]
# NOTE: Setting `BUILD_SHARED_LIBRARIES=ON` causes the just-built flang to throw ICE.
Expand All @@ -45,12 +49,24 @@ def install
args << "-DFLANG_VENDOR_UTI=sh.brew.flang" if tap&.official?

ENV.append_to_cflags "-ffat-lto-objects" if OS.linux? # Unsupported on macOS.
install_prefix = OS.mac? ? libexec : prefix
system "cmake", "-S", "flang", "-B", "build", *args, *std_cmake_args(install_prefix:)
system "cmake", "-S", "flang", "-B", "build", *args, *std_cmake_args(install_prefix: libexec)
system "cmake", "--build", "build"
system "cmake", "--install", "build"

return if install_prefix == prefix
libexec.find do |pn|
next if pn.directory?

subdir = pn.relative_path_from(libexec).dirname
(prefix/subdir).install_symlink pn
end

# Our LLVM is built with exception-handling, which requires linkage with the C++ standard library.
# TODO: Remove this if/when we've rebuilt LLVM with `LLVM_ENABLE_EH=OFF`.
cxx_stdlib = OS.mac? ? "-lc++" : "-lstdc++"
cxx_stdlib << "\n"
(libexec/"bin/flang.cfg").atomic_write cxx_stdlib

return if OS.linux?

# Convert LTO-generated bitcode in our static archives to MachO.
# Not needed on Linux because of `-ffat-lto-objects`
Expand All @@ -73,19 +89,20 @@ def install
end
end

libexec.find do |pn|
next if pn.directory?

subdir = pn.relative_path_from(libexec).dirname
(prefix/subdir).install_symlink pn
end

# The `flang-new` driver expects `libLTO.dylib` to be in the same prefix,
# but it actually lives in LLVM's prefix.
liblto = Formula["llvm"].opt_lib/shared_library("libLTO")
ln_sf liblto, libexec/"lib"
end

def caveats
<<~EOS
Homebrew LLVM is built with LLVM_ENABLE_EH=ON, so binaries built by `#{flang_driver}`
require linkage to the C++ standard library. `#{flang_driver}` is configured to do this
automatically.
EOS
end

test do
(testpath/"hello.f90").write <<~FORTRAN
PROGRAM hello
Expand All @@ -105,12 +122,10 @@ def install
end
FORTRAN

cxx_stdlib = OS.mac? ? "-lc++" : "-lstdc++"

system bin/"flang-new", "-v", "hello.f90", cxx_stdlib, "-o", "hello"
system bin/flang_driver, "-v", "hello.f90", "-o", "hello"
assert_equal "Hello World!", shell_output("./hello").chomp

system bin/"flang-new", "-v", "test.f90", cxx_stdlib, "-o", "test"
system bin/flang_driver, "-v", "test.f90", "-o", "test"
assert_equal "Done", shell_output("./test").chomp

system "ar", "x", lib/"libFortranCommon.a"
Expand Down

0 comments on commit 96ca32e

Please sign in to comment.