Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export fortran symbols via def file #77

Merged
merged 2 commits into from
Sep 16, 2023
Merged
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
3 changes: 3 additions & 0 deletions .github/actions/spelling/allow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2064,3 +2064,6 @@ phimeca
schueller
julia
libclang
dumpbin
objdump

10 changes: 5 additions & 5 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ if (WIN32)
set_target_properties(primac PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()

# because of dllexport
if (CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
set_source_files_properties (cintrf.f90 cobyla_c.f90 lincoa_c.f90 bobyqa_c.f90 newuoa_c.f90 uobyqa_c.f90 PROPERTIES COMPILE_FLAGS "-Wno-attributes")
endif ()

target_include_directories (primac PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand All @@ -22,6 +17,11 @@ if (NOT BUILD_SHARED_LIBS)
target_compile_definitions(primac PUBLIC PRIMAC_STATIC)
endif ()

# export symbols
if (WIN32 AND BUILD_SHARED_LIBS)
target_sources(primac PRIVATE primac.def)
endif ()

install (FILES include/prima/prima.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/prima)

macro (prima_add_c_test name)
Expand Down
3 changes: 0 additions & 3 deletions c/bobyqa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ subroutine bobyqa_c(cobj_ptr, n, x, f, xl, xu, nf, rhobeg, rhoend, ftarget, maxf
use, non_intrinsic :: bobyqa_mod, only : bobyqa
implicit none

!GCC$ attributes dllexport :: bobyqa_c
!DEC$ attributes dllexport :: bobyqa_c

! Compulsory arguments
type(C_FUNPTR), intent(IN), value :: cobj_ptr
integer(C_INT), intent(in), value :: n
Expand Down
3 changes: 0 additions & 3 deletions c/cobyla_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ subroutine cobyla_c(m_nlcon, cobjcon_ptr, n, x, f, cstrv, nlconstr, m_ineq, Aine
use, non_intrinsic :: cobyla_mod, only : cobyla
implicit none

!GCC$ attributes dllexport :: cobyla_c
!DEC$ attributes dllexport :: cobyla_c

! Compulsory arguments
integer(C_INT), intent(in), value :: m_nlcon
type(C_FUNPTR), intent(in), value :: cobjcon_ptr
Expand Down
3 changes: 0 additions & 3 deletions c/lincoa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ subroutine lincoa_c(cobj_ptr, n, x, f, cstrv, m_ineq, Aineq, bineq, m_eq, Aeq, b
use, non_intrinsic :: lincoa_mod, only : lincoa
implicit none

!GCC$ attributes dllexport :: lincoa_c
!DEC$ attributes dllexport :: lincoa_c

! Compulsory arguments
type(C_FUNPTR), intent(IN), value :: cobj_ptr
integer(C_INT), intent(in), value :: n
Expand Down
3 changes: 0 additions & 3 deletions c/newuoa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ subroutine newuoa_c(cobj_ptr, n, x, f, nf, rhobeg, rhoend, ftarget, maxfun, npt,
use, non_intrinsic :: newuoa_mod, only : newuoa
implicit none

!GCC$ attributes dllexport :: newuoa_c
!DEC$ attributes dllexport :: newuoa_c

! Compulsory arguments
type(C_FUNPTR), intent(IN), value :: cobj_ptr
integer(C_INT), intent(in), value :: n
Expand Down
6 changes: 6 additions & 0 deletions c/primac.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
EXPORTS
bobyqa_c
cobyla_c
lincoa_c
newuoa_c
uobyqa_c
3 changes: 0 additions & 3 deletions c/uobyqa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ subroutine uobyqa_c(cobj_ptr, n, x, f, nf, rhobeg, rhoend, ftarget, maxfun, ipri
use, non_intrinsic :: uobyqa_mod, only : uobyqa
implicit none

!GCC$ attributes dllexport :: uobyqa_c
!DEC$ attributes dllexport :: uobyqa_c

! Compulsory arguments
type(C_FUNPTR), intent(IN), value :: cobj_ptr
integer(C_INT), intent(in), value :: n
Expand Down
23 changes: 20 additions & 3 deletions fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,26 @@ if (HAVE_WARN_EXECSTACK)
target_link_options (primaf PUBLIC "-Wl,--no-warn-execstack")
endif ()

# because of dllexport
if (CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
target_compile_options (primaf PUBLIC "-Wno-attributes")
# On Windows, when building shared libs, visible functions must be explicitly listed.
# Unlike C there are no standard keywords to do this in Fortran but only compiler-specific pragmas:
# !GCC$ attributes dllexport :: bobyqa_c
# !DEC$ attributes dllexport :: bobyqa_c
# the downside is unwanted -Wattribute warnings.
# Another option is to use .def files referencing exported symbols:
# https://learn.microsoft.com/en-us/cpp/build/exporting-from-a-dll-using-def-files?view=msvc-170
# Mangling is different between GNU and Intel/LLVM compiler families so we use a dedicated file for each.
# Symbol names can be seen in object files with the dumpbin tool (or objdump) to write a new .def:
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
Fixed Show fixed Hide fixed
# dumpbin /symbols cobyla.f90.obj
# 017 00000000 UNDEF no_type External | COBYLA_MOD_mp_COBYLA
# x86_64-w64-mingw32-objdump -t cobyla.f90.obj
# [ 4](sec 1)(fl 0x00)(ty 20)(scl 2) (nx 0) 0x000000000000218b __cobyla_mod_MOD_cobyla
if (WIN32 AND BUILD_SHARED_LIBS)
if (EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/primaf-${CMAKE_Fortran_COMPILER_ID}.def)
target_sources (primaf PRIVATE primaf-${CMAKE_Fortran_COMPILER_ID}.def)
else ()
message (STATUS "Assuming Intel symbol mangling")
target_sources (primaf PRIVATE primaf-Intel.def)
endif ()
endif ()

if (WIN32)
Expand Down
3 changes: 0 additions & 3 deletions fortran/bobyqa/bobyqa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,6 @@ subroutine bobyqa(calfun, x, f, &

implicit none

!GCC$ attributes dllexport :: bobyqa
!DEC$ attributes dllexport :: bobyqa

! Compulsory arguments
procedure(OBJ) :: calfun ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
real(RP), intent(inout) :: x(:) ! X(N)
Expand Down
3 changes: 0 additions & 3 deletions fortran/cobyla/cobyla.f90
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,6 @@ subroutine cobyla(calcfc, m_nlcon, x, f, &

implicit none

!GCC$ attributes dllexport :: cobyla
!DEC$ attributes dllexport :: cobyla

! Compulsory arguments
procedure(OBJCON) :: calcfc ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
real(RP), intent(inout) :: x(:) ! X(N)
Expand Down
2 changes: 0 additions & 2 deletions fortran/common/debug.F90
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ subroutine assert(condition, description, srname)
use, non_intrinsic :: consts_mod, only : DEBUGGING
use, non_intrinsic :: infos_mod, only : ASSERTION_FAILS
implicit none
!GCC$ attributes dllexport :: assert
!DEC$ attributes dllexport :: assert
logical, intent(in) :: condition ! A condition that is expected to be true
character(len=*), intent(in) :: description ! Description of the condition in human language
character(len=*), intent(in) :: srname ! Name of the subroutine that calls this procedure
Expand Down
3 changes: 0 additions & 3 deletions fortran/lincoa/lincoa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,6 @@ subroutine lincoa(calfun, x, f, &

implicit none

!GCC$ attributes dllexport :: lincoa
!DEC$ attributes dllexport :: lincoa

! Compulsory arguments
procedure(OBJ) :: calfun ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
real(RP), intent(inout) :: x(:) ! X(N)
Expand Down
3 changes: 0 additions & 3 deletions fortran/newuoa/newuoa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,6 @@ subroutine newuoa(calfun, x, f, &

implicit none

!GCC$ attributes dllexport :: newuoa
!DEC$ attributes dllexport :: newuoa

! Arguments
procedure(OBJ) :: calfun ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
real(RP), intent(inout) :: x(:)
Expand Down
7 changes: 7 additions & 0 deletions fortran/primaf-GNU.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
EXPORTS
__bobyqa_mod_MOD_bobyqa
__cobyla_mod_MOD_cobyla
__lincoa_mod_MOD_lincoa
__newuoa_mod_MOD_newuoa
__uobyqa_mod_MOD_uobyqa
__debug_mod_MOD_assert
7 changes: 7 additions & 0 deletions fortran/primaf-Intel.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
EXPORTS
BOBYQA_MOD_mp_BOBYQA
COBYLA_MOD_mp_COBYLA
LINCOA_MOD_mp_LINCOA
NEWUOA_MOD_mp_NEWUOA
UOBYQA_MOD_mp_UOBYQA
DEBUG_MOD_mp_ASSERT
3 changes: 0 additions & 3 deletions fortran/uobyqa/uobyqa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,6 @@ subroutine uobyqa(calfun, x, f, &

implicit none

!GCC$ attributes dllexport :: uobyqa
!DEC$ attributes dllexport :: uobyqa

! Arguments
procedure(OBJ) :: calfun ! N.B.: INTENT cannot be specified if a dummy procedure is not a POINTER
real(RP), intent(inout) :: x(:) ! X(N)
Expand Down