-
-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
flang: link to C++ standard lib automatically
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
Showing
1 changed file
with
29 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 GitHub Actions / Linux`brew install --verbose --formula --build-bottle flang` failed on Linux!
Check failure on line 34 in Formula/f/flang.rb GitHub Actions / macOS 14-arm64`brew install --verbose --formula --build-bottle flang` failed on macOS Sonoma (14) on Apple Silicon!
|
||
llvm = Formula["llvm"] | ||
# NOTE: Setting `BUILD_SHARED_LIBRARIES=ON` causes the just-built flang to throw ICE. | ||
|
@@ -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` | ||
|
@@ -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 | ||
|
@@ -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" | ||
|