Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/fpm_targets.f90
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,17 @@ subroutine resolve_target_linking(targets, model, library, error)
global_link_flags = model%compiler%enumerate_libraries(global_link_flags, model%link_libraries)
end if
end if

! Extract -L library search paths from C/C++ compile flags so that the
! Fortran linker can find libraries specified via link_libraries.
! This handles the case where users pass -L paths via --cxx-flag or --c-flag
! but link_libraries (from fpm.toml [build] link) are resolved at link time.
if (allocated(model%cxx_compile_flags)) then
global_link_flags = global_link_flags // extract_library_paths(model%cxx_compile_flags)
end if
if (allocated(model%c_compile_flags)) then
global_link_flags = global_link_flags // extract_library_paths(model%c_compile_flags)
end if

allocate(character(0) :: global_include_flags)
if (allocated(model%include_dirs)) then
Expand Down Expand Up @@ -1617,4 +1628,37 @@ subroutine add_target_ptr_many(list,new)

end subroutine add_target_ptr_many


!> Extract -L library search path flags from a flags string.
!> This allows -L paths specified in C/C++ flags to be propagated
!> to the Fortran linker when linking executables.
function extract_library_paths(flags) result(paths)
character(*), intent(in) :: flags
character(:), allocatable :: paths

integer :: i, j, n

allocate(character(0) :: paths)
n = len_trim(flags)
i = 1

do while (i <= n - 1)
! Look for -L followed by a path
if (flags(i:i+1) == '-L') then
! Find the end of this flag (next space or end of string)
j = i + 2
do while (j <= n .and. flags(j:j) /= ' ')
j = j + 1
end do
! Append this -L flag
paths = paths // ' ' // flags(i:j-1)
i = j
else
i = i + 1
end if
end do

end function extract_library_paths


end module fpm_targets
2 changes: 1 addition & 1 deletion src/metapackage/fpm_meta_mpi.f90
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ subroutine init_mpi_from_wrappers(this,compiler,mpilib,fort_wrapper,c_wrapper,cx

! Request to use libs in arbitrary order
if (this%has_link_flags .and. compiler%is_gnu() .and. os_is_unix() .and. get_os_type()/=OS_MACOS) then
this%link_flags = string_t(' -Wl,--start-group '//this%link_flags%s)
this%link_flags = string_t(' -Wl,--start-group '//this%link_flags%s//' -Wl,--end-group')
end if

! Add language-specific flags
Expand Down