Skip to content

Fix shared lib build with NAG Fortran on macOS#5824

Open
mathomp4 wants to merge 1 commit into
OpenMathLib:developfrom
mathomp4:bugfix/5823-fix-for-nag
Open

Fix shared lib build with NAG Fortran on macOS#5824
mathomp4 wants to merge 1 commit into
OpenMathLib:developfrom
mathomp4:bugfix/5823-fix-for-nag

Conversation

@mathomp4
Copy link
Copy Markdown
Contributor

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 -dynamiclib

Changes in CMakeLists.txt:

  1. Before add_library() (must be set before targets are defined to take effect): for NAG Fortran on Apple, override CMAKE_Fortran_CREATE_SHARED_LIBRARY with the C compiler link rule, which uses -dynamiclib correctly.

  2. Inside if(APPLE AND BUILD_STATIC_LIBS) (the static→shared conversion trick for long argument lists): add an elseif(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 that NAGf90_* 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.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shared library build fails on macOS when using NAG Fortran compiler

1 participant