Fix shared lib build with NAG Fortran on macOS#5824
Open
mathomp4 wants to merge 1 commit into
Open
Conversation
On Apple, the shared library link rule for Fortran used GCC-specific flags: -x f95-cpp-input (GCC preprocessing flag; NAG does not support it) -Wl,-shared (Linux linker flag; macOS needs -dynamiclib) The fix is NAG-specific — other non-GNU compilers (Intel, flang, Cray, etc.) are left alone since they may handle the existing code paths fine. Fix in two places (both already inside if(APPLE) / if(APPLE AND BUILD_STATIC_LIBS)): 1. Before add_library() (affects the main CMake Fortran shared lib rule): For NAG Fortran, substitute CMAKE_C_CREATE_SHARED_LIBRARY so the C compiler link rule is used instead. 2. Inside the APPLE AND BUILD_STATIC_LIBS block (the static->shared conversion trick for long argument lists): add an elseif(NAG) branch that uses the C compiler with -dynamiclib and auto-detects the NAG Fortran runtime (libf72rts) from the compiler's directory so that NAGf90_* symbols resolve. Tested with NAG 7.2.7243 on macOS/arm64.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5823
Note: This was done with Claude's help. I tried myself but it got to the point I needed some good insight on how CMake does shared library stuff.
On macOS, the shared library link rule for Fortran used two GCC-specific constructs that fail with the NAG Fortran compiler:
-x f95-cpp-input— a GCC preprocessing flag; NAG does not support it-Wl,-shared— a Linux linker flag; macOS requires-dynamiclibChanges in
CMakeLists.txt:Before
add_library()(must be set before targets are defined to take effect): for NAG Fortran on Apple, overrideCMAKE_Fortran_CREATE_SHARED_LIBRARYwith the C compiler link rule, which uses-dynamiclibcorrectly.Inside
if(APPLE AND BUILD_STATIC_LIBS)(the static→shared conversion trick for long argument lists): add anelseif(NAG)branch that uses the C compiler with-dynamiclib. The NAG Fortran runtime library (libf<XY>rts) is auto-detected via glob from the compiler's directory so thatNAGf90_*symbols resolve — this handles NAG 7.1 (libf71rts), 7.2 (libf72rts), future versions, etc. without hardcoding version numbers.Other non-GNU compilers (Intel, flang, Cray, etc.) are unaffected.
Tested with: NAG 7.2.7243 on macOS/arm64,
BUILD_SHARED_LIBS=ON BUILD_STATIC_LIBS=ON.