Skip to content

Conversation

@piotrows
Copy link

Description

Flang compiler doesn't recognize macOS-specific linker flags used by CMake:
e.g. when compiling Fiat:
flang-21: error: unknown argument: '-dynamiclib'
flang-21: error: unknown argument: '-install_name'

Following the hint from:
HDFGroup/hdf5#5662
it seems that the fix is to simply enforce C as a linker language for LLVMFortran.
This indeed seems to enable Fiat compilation with flang-21.

Contributor Declaration

By opening this pull request, I affirm the following:

  • All authors agree to the Contributor License Agreement.
  • The code follows the project's coding standards.
  • I have performed self-review and added comments where needed.
  • I have added or updated tests to verify that my changes are effective and functional.
  • I have run all existing tests and confirmed they pass.

@piotrows
Copy link
Author

piotrows commented Oct 28, 2025

Hi @kynan, @wdeconinck, @marcosbento, would you care to look at it or suggest someone else?

@marcosbento
Copy link
Collaborator

From my side, the proposed changes look OK, so I've approved the entire CI build.
That being said, I'm sure @wdeconinck can provide a better review, since I don't actually use Fortran that often.

@wdeconinck
Copy link
Member

wdeconinck commented Nov 5, 2025

Really this is an issue with upstream CMake and not ecbuild.
The proposed solution is quite fragile, and may have unintended side-effects, e.g. missing link flags that have been intended for Fortran (like OpenMP). Also this is something that could be solved in upstream CMake at some point after which this will need to be removed again.

After investigation, this can be solved by a file Platform/Apple-LLVMFlang-Fortran.cmake which has to be present in a path contained in CMAKE_MODULE_PATH, with content:

set(CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS "--shared")
set(CMAKE_SHARED_LIBRARY_SONAME_Fortran_FLAG  "-Wl,-install_name -Wl,")
set(CMAKE_Fortran_CREATE_SHARED_LIBRARY
  "<CMAKE_Fortran_COMPILER> <LANGUAGE_COMPILE_FLAGS> <CMAKE_SHARED_LIBRARY_CREATE_Fortran_FLAGS> <LINK_FLAGS> -o <TARGET> <SONAME_FLAG><TARGET_INSTALLNAME_DIR><TARGET_SONAME> <OBJECTS> <LINK_LIBRARIES>")

I have a small reproducer CMake project that I managed to get working, placing this file in a cmake subdirectory.
The CMakeLists.txt contains simply:

cmake_minimum_required(VERSION 3.31)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
project(test_flag LANGUAGES Fortran)
add_library(mylib SHARED mylib.F90)
add_executable(main main.F90)
target_link_libraries(main mylib)

The content of mylib.F90 and main.F90 does not really matter, but for completeness:

module mylib
contains
subroutine hello_world
  write(0,*) "hello world"
end subroutine
end module
program main
use mylib
call hello_world()
end program

I did not investigate further if the file Platform/Apple-LLVMFlang-Fortran.cmake is feature-complete.
Perhaps @piotrows you could investigate further with this approach whether it can compile our entire software stack. The file does not need to be embedded in any project for testing, as long as you pass "CMAKE_MODULE_PATH" somehow to CMake and it knows how to find it.
It is quite likely that this will get sorted automatically upstream eventually, or a PR can be made by us.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants