Skip to content

Commit c03ceaa

Browse files
committed
create import library and definition file
1 parent 7950b0b commit c03ceaa

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

src/fpm_compiler.F90

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ module fpm_compiler
4747
use tomlf, only: toml_table
4848
use fpm_toml, only: serializable_t, set_string, set_value, toml_stat, get_value
4949
use fpm_compile_commands, only: compile_command_t, compile_command_table_t
50-
use shlex_module, only: sh_split => split, ms_split
50+
use shlex_module, only: sh_split => split, ms_split, quote => ms_quote
5151
implicit none
5252
public :: compiler_t, new_compiler, archiver_t, new_archiver, get_macros
5353
public :: append_clean_flags, append_clean_flags_array
@@ -103,6 +103,8 @@ module fpm_compiler
103103
procedure :: get_feature_flag
104104
!> Get flags for the main linking command
105105
procedure :: get_main_flags
106+
!> Get library export flags
107+
procedure :: get_export_flags
106108
!> Compile a Fortran object
107109
procedure :: compile_fortran
108110
!> Compile a C object
@@ -122,6 +124,7 @@ module fpm_compiler
122124
!> Enumerate libraries, based on compiler and platform
123125
procedure :: enumerate_libraries
124126

127+
125128
!> Serialization interface
126129
procedure :: serializable_is_same => compiler_is_same
127130
procedure :: dump_to_toml => compiler_dump
@@ -1070,6 +1073,50 @@ function enumerate_libraries(self, prefix, libs) result(r)
10701073

10711074
end function enumerate_libraries
10721075

1076+
!>
1077+
!> Generate library export flags for a shared library build
1078+
!>
1079+
function get_export_flags(self, target_dir, target_name) result(export_flags)
1080+
!> Instance of the compiler
1081+
class(compiler_t), intent(in) :: self
1082+
!> Path and package name
1083+
character(len=*), intent(in) :: target_dir, target_name
1084+
character(len=:), allocatable :: export_flags
1085+
1086+
character(len=:), allocatable :: implib_path, def_path
1087+
1088+
! Only apply on Windows
1089+
if (get_os_type() /= OS_WINDOWS) then
1090+
export_flags = ""
1091+
return
1092+
end if
1093+
1094+
select case (self%id)
1095+
1096+
case (id_gcc, id_caf, id_f95)
1097+
! GNU-based: emit both import library and def file
1098+
implib_path = quote(join_path(target_dir, target_name // ".dll.a") , for_cmd=.true.)
1099+
def_path = quote(join_path(target_dir, target_name // ".def" ) , for_cmd=.true.)
1100+
1101+
export_flags = " -Wl,--out-implib," // implib_path // &
1102+
" -Wl,--output-def," // def_path
1103+
1104+
case (id_intel_classic_windows, id_intel_llvm_windows)
1105+
! Intel/MSVC-style
1106+
implib_path = quote(join_path(target_dir, target_name // ".lib") , for_cmd=.true.)
1107+
def_path = quote(join_path(target_dir, target_name // ".def") , for_cmd=.true.)
1108+
1109+
export_flags = " /IMPLIB:" // implib_path // &
1110+
" /DEF:" // def_path
1111+
1112+
case default
1113+
1114+
export_flags = "" ! Do nothing elsewhere
1115+
1116+
end select
1117+
1118+
end function get_export_flags
1119+
10731120
!> Create new compiler instance
10741121
subroutine new_compiler(self, fc, cc, cxx, echo, verbose)
10751122
!> New instance of the compiler

src/fpm_targets.f90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ subroutine resolve_target_linking(targets, model, library, error)
10551055
exclude_self=.true., &
10561056
dep_IDs=package_deps, &
10571057
error=error)
1058-
1058+
10591059
if (allocated(error)) return
10601060

10611061
! Now that they're available, add these dependencies to the targets
@@ -1073,6 +1073,10 @@ subroutine resolve_target_linking(targets, model, library, error)
10731073
end if
10741074
end if
10751075

1076+
! Add shared library exports (import library + .def)
1077+
target%link_flags = target%link_flags // " " // &
1078+
model%compiler%get_export_flags(target%output_dir,target%package_name)
1079+
10761080
! Add global link flags (e.g., system-wide libraries)
10771081
target%link_flags = target%link_flags // " " // global_link_flags
10781082

0 commit comments

Comments
 (0)