Skip to content

Commit

Permalink
Try shared libs
Browse files Browse the repository at this point in the history
  • Loading branch information
jschueller committed Sep 14, 2023
1 parent e601a73 commit aeb2534
Show file tree
Hide file tree
Showing 17 changed files with 105 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
- os: windows-latest
toolchain: {compiler: intel, version: '2023.1', cflags: '', fflags: '/warn:all /debug:extended /fimplicit-none /standard-semantics /assume:recursion'}
- os: windows-latest
toolchain: {compiler: intel-classic, version: '2021.9', cflags: '/Qdiag-disable:10441', fflags: '/warn:all /debug:extended /fimplicit-none /standard-semantics /assume:recursion'}
toolchain: {compiler: intel-classic, version: '2021.9', cflags: '/Qdiag-disable:10441', fflags: '/warn:all /fimplicit-none /standard-semantics /assume:recursion'}

steps:

Expand Down Expand Up @@ -124,7 +124,7 @@ jobs:

- name: Build
run: |
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=. -LAH -DCMAKE_C_FLAGS="${{ matrix.toolchain.cflags }}" -DCMAKE_Fortran_FLAGS="${{ matrix.toolchain.fflags }}" -DBUILD_SHARED_LIBS=ON .
cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo -DCMAKE_INSTALL_PREFIX=. -LAH -DCMAKE_C_FLAGS="${{ matrix.toolchain.cflags }}" -DCMAKE_Fortran_FLAGS="${{ matrix.toolchain.fflags }}" .
cmake --build . --target install --parallel 4
cmake --build . --target examples --parallel 4
# cobyla test does not pass on AOCC: https://github.com/libprima/prima/issues/41
Expand Down
4 changes: 0 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type")
project (prima Fortran)

option (BUILD_SHARED_LIBS "shared/static" ON)
if (WIN32)
# symbols are not exported on windows
set (BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
endif ()

include (GNUInstallDirs)

Expand Down
28 changes: 21 additions & 7 deletions c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
set(CMAKE_C_STANDARD 99)

add_library (primac cintrf.f90 cobyla_c.f90 lincoa_c.f90 bobyqa_c.f90 newuoa_c.f90 uobyqa_c.f90 prima.c)
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>
Expand All @@ -10,18 +18,24 @@ target_include_directories (primac PUBLIC
target_link_libraries (primac PUBLIC primaf) # must be PUBLIC for precision macros
set_target_properties(primac PROPERTIES POSITION_INDEPENDENT_CODE ON C_STANDARD 99)

install (TARGETS primac DESTINATION ${CMAKE_INSTALL_LIBDIR})
if (NOT BUILD_SHARED_LIBS)
target_compile_definitions(primac PUBLIC PRIMAC_STATIC)
endif ()

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

macro (prima_add_c_test name)
add_executable (example_${name}_c EXCLUDE_FROM_ALL examples/${name}/${name}_example.c)
target_link_libraries (example_${name}_c PRIVATE primac)
target_include_directories (example_${name}_c PRIVATE ${CMAKE_SOURCE_DIR}/c/include)
add_executable (example_${name}_c_exe EXCLUDE_FROM_ALL examples/${name}/${name}_example.c)
target_link_libraries (example_${name}_c_exe PRIVATE primac)
target_include_directories (example_${name}_c_exe PRIVATE ${CMAKE_SOURCE_DIR}/c/include)
if (PRIMA_ENABLE_EXAMPLES)
set_target_properties (example_${name}_c PROPERTIES EXCLUDE_FROM_ALL FALSE)
set_target_properties (example_${name}_c_exe PROPERTIES EXCLUDE_FROM_ALL FALSE)
endif ()
add_test (NAME example_${name}_c COMMAND example_${name}_c)
add_dependencies(examples example_${name}_c)
if (WIN32)
set_target_properties(example_${name}_c_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()
add_test (NAME example_${name}_c COMMAND example_${name}_c_exe)
add_dependencies(examples example_${name}_c_exe)
endmacro ()

prima_add_c_test (cobyla)
Expand Down
3 changes: 3 additions & 0 deletions c/bobyqa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ subroutine bobyqa_c(cobj_ptr, n, x, f, xl, xu, nf, rhobeg, rhoend, maxfun, iprin
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: 3 additions & 0 deletions c/cobyla_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ 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
26 changes: 26 additions & 0 deletions c/include/prima/prima.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,22 @@
extern "C" {
#endif

#ifdef _WIN32
# define PRIMAC_HELPER_DLL_IMPORT __declspec(dllimport)
# define PRIMAC_HELPER_DLL_EXPORT __declspec(dllexport)
#else
# define PRIMAC_HELPER_DLL_IMPORT
# define PRIMAC_HELPER_DLL_EXPORT
#endif

#ifdef PRIMAC_STATIC
# define PRIMAC_API
#elif defined primac_EXPORTS
# define PRIMAC_API PRIMAC_HELPER_DLL_EXPORT
#else
# define PRIMAC_API PRIMAC_HELPER_DLL_IMPORT
#endif

/*
* Verbosity level
*/
Expand Down Expand Up @@ -42,6 +58,7 @@ typedef enum
/*
* Return code string
*/
PRIMAC_API
const char *prima_get_rc_string(int rc);

/*
Expand Down Expand Up @@ -81,19 +98,28 @@ typedef void (*prima_objcon)(const double x[], double *f, double constr[]);
* return : see prima_rc enum for return codes
*/

PRIMAC_API
int prima_bobyqa(const prima_obj calfun, const int n, double x[n], double *f,
const double xl[n], const double xu[n],
int *nf, const double rhobeg, const double rhoend, const int maxfun, const int iprint);

PRIMAC_API
int prima_newuoa(const prima_obj calfun, const int n, double x[n], double *f,
int *nf, const double rhobeg, const double rhoend, const int maxfun, const int iprint);

PRIMAC_API
int prima_uobyqa(const prima_obj calfun, const int n, double x[n], double *f,
int *nf, const double rhobeg, const double rhoend, const int maxfun, const int iprint);

PRIMAC_API
int prima_cobyla(const int m_nlcon, const prima_objcon calcfc, const int n, double x[n], double *f,
double *cstrv, double nlconstr[m_nlcon],
const int m_ineq, const double Aineq[m_ineq*n], const double bineq[m_ineq],
const int m_eq, const double Aeq[m_eq*n], const double beq[m_eq],
const double xl[n], const double xu[n],
int *nf, const double rhobeg, const double rhoend, const int maxfun, const int iprint);

PRIMAC_API
int prima_lincoa(const prima_obj calfun, const int n, double x[n], double *f,
double *cstrv,
const int m_ineq, const double Aineq[m_ineq*n], const double bineq[m_ineq],
Expand Down
3 changes: 3 additions & 0 deletions c/lincoa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ 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: 3 additions & 0 deletions c/newuoa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ subroutine newuoa_c(cobj_ptr, n, x, f, nf, rhobeg, rhoend, maxfun, iprint, info)
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
7 changes: 5 additions & 2 deletions c/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
add_executable(t_large EXCLUDE_FROM_ALL t_large.c)
if (PRIMA_ENABLE_TESTING)
set_target_properties (t_large PROPERTIES EXCLUDE_FROM_ALL FALSE)
endif ()
set_target_properties (t_large PROPERTIES EXCLUDE_FROM_ALL FALSE)
endif ()
if (WIN32)
set_target_properties(t_large PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()
add_dependencies(tests t_large)

add_test(NAME bobyqa_large_c COMMAND t_large bobyqa)
Expand Down
3 changes: 3 additions & 0 deletions c/uobyqa_c.f90
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ subroutine uobyqa_c(cobj_ptr, n, x, f, nf, rhobeg, rhoend, maxfun, iprint, info)
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
27 changes: 19 additions & 8 deletions fortran/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,19 +72,30 @@ if (HAVE_WARN_EXECSTACK)
target_link_options (primaf PUBLIC "-Wl,--no-warn-execstack")
endif ()

install (TARGETS primaf DESTINATION ${CMAKE_INSTALL_LIBDIR})
# because of dllexport
if (CMAKE_Fortran_COMPILER_ID MATCHES "GNU")
target_compile_options (primaf PUBLIC "-Wno-attributes")
endif ()

if (WIN32)
set_target_properties(primaf PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()

install (DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/mod DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/prima)

macro (prima_add_f_test name)
add_executable (example_${name}_fortran EXCLUDE_FROM_ALL examples/${name}/${name}_example.f90)
target_link_libraries (example_${name}_fortran PRIVATE primaf)
target_include_directories (example_${name}_fortran PRIVATE ${CMAKE_BINARY_DIR}/fortran)
set_target_properties(example_${name}_fortran PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/examples/${name}/mod)
add_executable (example_${name}_fortran_exe EXCLUDE_FROM_ALL examples/${name}/${name}_example.f90)
target_link_libraries (example_${name}_fortran_exe PRIVATE primaf)
target_include_directories (example_${name}_fortran_exe PRIVATE ${CMAKE_BINARY_DIR}/fortran)
set_target_properties(example_${name}_fortran_exe PROPERTIES Fortran_MODULE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/examples/${name}/mod)
if (WIN32)
set_target_properties(example_${name}_fortran_exe PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)
endif()
if (PRIMA_ENABLE_EXAMPLES)
set_target_properties (example_${name}_fortran PROPERTIES EXCLUDE_FROM_ALL FALSE)
set_target_properties (example_${name}_fortran_exe PROPERTIES EXCLUDE_FROM_ALL FALSE)
endif ()
add_test (NAME example_${name}_f COMMAND example_${name}_fortran)
add_dependencies(examples example_${name}_fortran examples)
add_test (NAME ex_${name}_f COMMAND example_${name}_fortran_exe)
add_dependencies(examples example_${name}_fortran_exe)
endmacro ()

prima_add_f_test (cobyla)
Expand Down
3 changes: 3 additions & 0 deletions fortran/bobyqa/bobyqa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ 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: 3 additions & 0 deletions fortran/cobyla/cobyla.f90
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ 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: 2 additions & 0 deletions fortran/common/debug.F90
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ 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: 3 additions & 0 deletions fortran/lincoa/lincoa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ 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: 3 additions & 0 deletions fortran/newuoa/newuoa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ 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
3 changes: 3 additions & 0 deletions fortran/uobyqa/uobyqa.f90
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,9 @@ 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

0 comments on commit aeb2534

Please sign in to comment.