Skip to content

Commit 1b4da71

Browse files
committed
[macos] add install name flags
1 parent 06d5bea commit 1b4da71

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/fpm_compiler.F90

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ module fpm_compiler
105105
procedure :: get_main_flags
106106
!> Get library export flags
107107
procedure :: get_export_flags
108+
!> Get library install name flags
109+
procedure :: get_install_name_flags
108110
!> Compile a Fortran object
109111
procedure :: compile_fortran
110112
!> Compile a C object
@@ -1117,6 +1119,28 @@ function get_export_flags(self, target_dir, target_name) result(export_flags)
11171119

11181120
end function get_export_flags
11191121

1122+
!>
1123+
!> Generate `install_name` flag for a shared library build on macOS
1124+
!>
1125+
function get_install_name_flags(self, target_dir, target_name) result(flags)
1126+
class(compiler_t), intent(in) :: self
1127+
character(len=*), intent(in) :: target_dir, target_name
1128+
character(len=:), allocatable :: flags
1129+
1130+
if (get_os_type() /= OS_MACOS) then
1131+
flags = ""
1132+
return
1133+
end if
1134+
1135+
! Shared library basename (e.g., libfoo.dylib)
1136+
if (str_ends_with(target_name, ".dylib")) then
1137+
flags = " -Wl,-install_name,@rpath/" // target_name
1138+
else
1139+
flags = " -Wl,-install_name,@rpath/" // target_name // ".dylib"
1140+
end if
1141+
1142+
end function get_install_name_flags
1143+
11201144
!> Create new compiler instance
11211145
subroutine new_compiler(self, fc, cc, cxx, echo, verbose)
11221146
!> New instance of the compiler

src/fpm_targets.f90

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,8 +1142,13 @@ subroutine resolve_target_linking(targets, model, library, error)
11421142
target%link_flags = target%link_flags // " " // &
11431143
model%compiler%get_export_flags(target%output_dir,target%package_name)
11441144

1145+
! Add install_name flag (macOS only)
1146+
target%link_flags = target%link_flags // " " // &
1147+
model%compiler%get_install_name_flags(target%output_dir, target%package_name)
1148+
11451149
! Add global link flags (e.g., system-wide libraries)
1146-
target%link_flags = target%link_flags // " " // global_link_flags
1150+
target%link_flags = target%link_flags // " " // global_link_flags
1151+
11471152

11481153
case (FPM_TARGET_EXECUTABLE)
11491154

0 commit comments

Comments
 (0)