Skip to content

Commit f425106

Browse files
authored
Merge pull request #7 from swig-fortran/cmake-tweaks
Improve support for downstream usage
2 parents 207c621 + 874aafc commit f425106

File tree

7 files changed

+64
-42
lines changed

7 files changed

+64
-42
lines changed

CMakeLists.txt

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,14 +137,15 @@ set(FLIBCPP_NAMESPACE Flibcpp::)
137137
set(FLIBCPP_LIBRARIES)
138138

139139
function(flibcpp_add_module name)
140-
set(src_file "${FLIBCPP_INTERFACE_DIR}/${name}.i")
141-
# We're using C++
142-
set_property(SOURCE "${src_file}" PROPERTY CPLUSPLUS ON)
143-
# We need to include the source directory
144-
set_property(SOURCE "${src_file}" PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON)
145-
146140
if (FLIBCPP_USE_SWIG)
147141
# SWIG is available; actually generate the library dynamically.
142+
143+
set(src_file "${FLIBCPP_INTERFACE_DIR}/${name}.i")
144+
# We're using C++
145+
set_property(SOURCE "${src_file}" PROPERTY CPLUSPLUS ON)
146+
# We need to include the source directory
147+
set_property(SOURCE "${src_file}" PROPERTY USE_TARGET_INCLUDE_DIRECTORIES ON)
148+
148149
# Create the library
149150
swig_add_library(${name}
150151
LANGUAGE Fortran
@@ -176,6 +177,10 @@ function(flibcpp_add_module name)
176177
)
177178
endif()
178179

180+
# Allow the library to be referred to by its namespaced version, for use by
181+
# downstream projects that *directly* compile flibcpp
182+
add_library(${FLIBCPP_NAMESPACE}${name} ALIAS ${name})
183+
179184
# Compile C++ code with C++11
180185
target_compile_features(${name}
181186
PRIVATE

cmake/FlibcppConfig.cmake.in

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ endif()
99

1010
set(Flibcpp_LIBRARIES @FLIBCPP_LIBRARIES@)
1111

12+
set(Flibcpp_BUILD_SHARED_LIBS @BUILD_SHARED_LIBS@)
13+
14+
if (NOT "@Flibcpp_BUILD_SHARED_LIBS@")
15+
# Downstream libraries must find and link to the C++ runtimes themselves since
16+
# they can't use the shared library dependencies
17+
enable_language(CXX)
18+
endif()

src/flc_algorithm.f90

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -640,16 +640,17 @@ subroutine SWIGTM_fin_int32_t_Sb__SB_(finp, iminp)
640640
use, intrinsic :: ISO_C_BINDING
641641
integer(C_INT32_T), dimension(:), intent(in), target :: finp
642642
type(SwigArrayWrapper), intent(out) :: iminp
643+
integer(C_SIZE_T) :: sz
643644
integer(C_INT32_T), pointer :: imtemp
644645

645-
if (size(finp) > 0) then
646+
sz = size(finp, kind=C_SIZE_T)
647+
if (sz > 0_c_size_t) then
646648
imtemp => finp(1)
647649
iminp%data = c_loc(imtemp)
648-
iminp%size = size(finp)
649650
else
650651
iminp%data = c_null_ptr
651-
iminp%size = 0
652652
end if
653+
iminp%size = sz
653654
end subroutine
654655
subroutine swigf_sort__SWIG_1(data)
655656
use, intrinsic :: ISO_C_BINDING
@@ -664,16 +665,17 @@ subroutine SWIGTM_fin_int64_t_Sb__SB_(finp, iminp)
664665
use, intrinsic :: ISO_C_BINDING
665666
integer(C_INT64_T), dimension(:), intent(in), target :: finp
666667
type(SwigArrayWrapper), intent(out) :: iminp
668+
integer(C_SIZE_T) :: sz
667669
integer(C_INT64_T), pointer :: imtemp
668670

669-
if (size(finp) > 0) then
671+
sz = size(finp, kind=C_SIZE_T)
672+
if (sz > 0_c_size_t) then
670673
imtemp => finp(1)
671674
iminp%data = c_loc(imtemp)
672-
iminp%size = size(finp)
673675
else
674676
iminp%data = c_null_ptr
675-
iminp%size = 0
676677
end if
678+
iminp%size = sz
677679
end subroutine
678680
subroutine swigf_sort__SWIG_2(data)
679681
use, intrinsic :: ISO_C_BINDING
@@ -688,16 +690,17 @@ subroutine SWIGTM_fin_double_Sb__SB_(finp, iminp)
688690
use, intrinsic :: ISO_C_BINDING
689691
real(C_DOUBLE), dimension(:), intent(in), target :: finp
690692
type(SwigArrayWrapper), intent(out) :: iminp
693+
integer(C_SIZE_T) :: sz
691694
real(C_DOUBLE), pointer :: imtemp
692695

693-
if (size(finp) > 0) then
696+
sz = size(finp, kind=C_SIZE_T)
697+
if (sz > 0_c_size_t) then
694698
imtemp => finp(1)
695699
iminp%data = c_loc(imtemp)
696-
iminp%size = size(finp)
697700
else
698701
iminp%data = c_null_ptr
699-
iminp%size = 0
700702
end if
703+
iminp%size = sz
701704
end subroutine
702705
subroutine swigf_sort__SWIG_3(data)
703706
use, intrinsic :: ISO_C_BINDING
@@ -748,16 +751,17 @@ subroutine SWIGTM_fin_void_Sm__Sb__SB_(finp, iminp)
748751
use, intrinsic :: ISO_C_BINDING
749752
type(C_PTR), dimension(:), intent(in), target :: finp
750753
type(SwigArrayWrapper), intent(out) :: iminp
754+
integer(C_SIZE_T) :: sz
751755
type(C_PTR), pointer :: imtemp
752756

753-
if (size(finp) > 0) then
757+
sz = size(finp, kind=C_SIZE_T)
758+
if (sz > 0_c_size_t) then
754759
imtemp => finp(1)
755760
iminp%data = c_loc(imtemp)
756-
iminp%size = size(finp)
757761
else
758762
iminp%data = c_null_ptr
759-
iminp%size = 0
760763
end if
764+
iminp%size = sz
761765
end subroutine
762766
subroutine swigf_sort__SWIG_7(data, cmp)
763767
use, intrinsic :: ISO_C_BINDING
@@ -891,16 +895,17 @@ subroutine SWIGTM_fin_int_Sb__SB_(finp, iminp)
891895
use, intrinsic :: ISO_C_BINDING
892896
integer(C_INT), dimension(:), intent(in), target :: finp
893897
type(SwigArrayWrapper), intent(out) :: iminp
898+
integer(C_SIZE_T) :: sz
894899
integer(C_INT), pointer :: imtemp
895900

896-
if (size(finp) > 0) then
901+
sz = size(finp, kind=C_SIZE_T)
902+
if (sz > 0_c_size_t) then
897903
imtemp => finp(1)
898904
iminp%data = c_loc(imtemp)
899-
iminp%size = size(finp)
900905
else
901906
iminp%data = c_null_ptr
902-
iminp%size = 0
903907
end if
908+
iminp%size = sz
904909
end subroutine
905910
subroutine swigf_argsort__SWIG_1(data, idx)
906911
use, intrinsic :: ISO_C_BINDING

src/flc_random.f90

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -237,16 +237,17 @@ subroutine SWIGTM_fin_int32_t_Sb__SB_(finp, iminp)
237237
use, intrinsic :: ISO_C_BINDING
238238
integer(C_INT32_T), dimension(:), intent(in), target :: finp
239239
type(SwigArrayWrapper), intent(out) :: iminp
240+
integer(C_SIZE_T) :: sz
240241
integer(C_INT32_T), pointer :: imtemp
241242

242-
if (size(finp) > 0) then
243+
sz = size(finp, kind=C_SIZE_T)
244+
if (sz > 0_c_size_t) then
243245
imtemp => finp(1)
244246
iminp%data = c_loc(imtemp)
245-
iminp%size = size(finp)
246247
else
247248
iminp%data = c_null_ptr
248-
iminp%size = 0
249249
end if
250+
iminp%size = sz
250251
end subroutine
251252
subroutine swigf_uniform_int_distribution__SWIG_0(left, right, g, data)
252253
use, intrinsic :: ISO_C_BINDING
@@ -270,16 +271,17 @@ subroutine SWIGTM_fin_int64_t_Sb__SB_(finp, iminp)
270271
use, intrinsic :: ISO_C_BINDING
271272
integer(C_INT64_T), dimension(:), intent(in), target :: finp
272273
type(SwigArrayWrapper), intent(out) :: iminp
274+
integer(C_SIZE_T) :: sz
273275
integer(C_INT64_T), pointer :: imtemp
274276

275-
if (size(finp) > 0) then
277+
sz = size(finp, kind=C_SIZE_T)
278+
if (sz > 0_c_size_t) then
276279
imtemp => finp(1)
277280
iminp%data = c_loc(imtemp)
278-
iminp%size = size(finp)
279281
else
280282
iminp%data = c_null_ptr
281-
iminp%size = 0
282283
end if
284+
iminp%size = sz
283285
end subroutine
284286
subroutine swigf_uniform_int_distribution__SWIG_1(left, right, g, data)
285287
use, intrinsic :: ISO_C_BINDING
@@ -303,16 +305,17 @@ subroutine SWIGTM_fin_double_Sb__SB_(finp, iminp)
303305
use, intrinsic :: ISO_C_BINDING
304306
real(C_DOUBLE), dimension(:), intent(in), target :: finp
305307
type(SwigArrayWrapper), intent(out) :: iminp
308+
integer(C_SIZE_T) :: sz
306309
real(C_DOUBLE), pointer :: imtemp
307310

308-
if (size(finp) > 0) then
311+
sz = size(finp, kind=C_SIZE_T)
312+
if (sz > 0_c_size_t) then
309313
imtemp => finp(1)
310314
iminp%data = c_loc(imtemp)
311-
iminp%size = size(finp)
312315
else
313316
iminp%data = c_null_ptr
314-
iminp%size = 0
315317
end if
318+
iminp%size = sz
316319
end subroutine
317320
subroutine uniform_real_distribution(left, right, g, data)
318321
use, intrinsic :: ISO_C_BINDING

src/flc_string.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ subroutine SWIGTM_fin_char_Sm_(finp, iminp, temp)
405405
i = len(finp) + 1
406406
temp(i) = C_NULL_CHAR ! C finp compatibility
407407
iminp%data = c_loc(temp)
408-
iminp%size = len(finp)
408+
iminp%size = len(finp, kind=C_SIZE_T)
409409
end subroutine
410410

411411
function swigf_new_string__SWIG_2(s) &

src/flc_vector.f90

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,16 +1471,17 @@ subroutine SWIGTM_fin_int32_t_Sb__SB_(finp, iminp)
14711471
use, intrinsic :: ISO_C_BINDING
14721472
integer(C_INT32_T), dimension(:), intent(in), target :: finp
14731473
type(SwigArrayWrapper), intent(out) :: iminp
1474+
integer(C_SIZE_T) :: sz
14741475
integer(C_INT32_T), pointer :: imtemp
14751476

1476-
if (size(finp) > 0) then
1477+
sz = size(finp, kind=C_SIZE_T)
1478+
if (sz > 0_c_size_t) then
14771479
imtemp => finp(1)
14781480
iminp%data = c_loc(imtemp)
1479-
iminp%size = size(finp)
14801481
else
14811482
iminp%data = c_null_ptr
1482-
iminp%size = 0
14831483
end if
1484+
iminp%size = sz
14841485
end subroutine
14851486
function swigf_new_VectorInt4__SWIG_4(data) &
14861487
result(self)
@@ -1863,16 +1864,17 @@ subroutine SWIGTM_fin_int64_t_Sb__SB_(finp, iminp)
18631864
use, intrinsic :: ISO_C_BINDING
18641865
integer(C_INT64_T), dimension(:), intent(in), target :: finp
18651866
type(SwigArrayWrapper), intent(out) :: iminp
1867+
integer(C_SIZE_T) :: sz
18661868
integer(C_INT64_T), pointer :: imtemp
18671869

1868-
if (size(finp) > 0) then
1870+
sz = size(finp, kind=C_SIZE_T)
1871+
if (sz > 0_c_size_t) then
18691872
imtemp => finp(1)
18701873
iminp%data = c_loc(imtemp)
1871-
iminp%size = size(finp)
18721874
else
18731875
iminp%data = c_null_ptr
1874-
iminp%size = 0
18751876
end if
1877+
iminp%size = sz
18761878
end subroutine
18771879
function swigf_new_VectorInt8__SWIG_4(data) &
18781880
result(self)
@@ -2255,16 +2257,17 @@ subroutine SWIGTM_fin_double_Sb__SB_(finp, iminp)
22552257
use, intrinsic :: ISO_C_BINDING
22562258
real(C_DOUBLE), dimension(:), intent(in), target :: finp
22572259
type(SwigArrayWrapper), intent(out) :: iminp
2260+
integer(C_SIZE_T) :: sz
22582261
real(C_DOUBLE), pointer :: imtemp
22592262

2260-
if (size(finp) > 0) then
2263+
sz = size(finp, kind=C_SIZE_T)
2264+
if (sz > 0_c_size_t) then
22612265
imtemp => finp(1)
22622266
iminp%data = c_loc(imtemp)
2263-
iminp%size = size(finp)
22642267
else
22652268
iminp%data = c_null_ptr
2266-
iminp%size = 0
22672269
end if
2270+
iminp%size = sz
22682271
end subroutine
22692272
function swigf_new_VectorReal8__SWIG_4(data) &
22702273
result(self)
@@ -2393,7 +2396,7 @@ subroutine SWIGTM_fin_char_Sm_(finp, iminp, temp)
23932396
i = len(finp) + 1
23942397
temp(i) = C_NULL_CHAR ! C finp compatibility
23952398
iminp%data = c_loc(temp)
2396-
iminp%size = len(finp)
2399+
iminp%size = len(finp, kind=C_SIZE_T)
23972400
end subroutine
23982401

23992402
function swigf_new_VectorString__SWIG_3(count, v) &

test/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
# Create test with dependencies
99
macro(swig_fortran_add_test TESTNAME)
1010
add_executable(${TESTNAME}.exe ${TESTNAME}.F90)
11-
message("Linking ${TESTNAME} against ${ARGN}")
1211
target_link_libraries(${TESTNAME}.exe ${ARGN})
1312
add_test(NAME ${TESTNAME} COMMAND ${TESTNAME}.exe)
1413
endmacro()

0 commit comments

Comments
 (0)