Skip to content

Commit fd1b1f8

Browse files
authored
feat: shared and static library targets (#1138)
2 parents 9ea4ee2 + f95cc34 commit fd1b1f8

32 files changed

+1736
-244
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ build/*
66
# CodeBlocks
77
project/
88

9+
# Temporary files
10+
*.swp
11+

ci/run_tests.sh

+34
Original file line numberDiff line numberDiff line change
@@ -310,5 +310,39 @@ fi
310310

311311
popd
312312

313+
# Test shared library dependencies
314+
pushd shared_lib
315+
"$fpm" build || EXIT_CODE=$?
316+
test $EXIT_CODE -eq 0
317+
popd
318+
319+
pushd shared_lib_extra
320+
"$fpm" build || EXIT_CODE=$?
321+
test $EXIT_CODE -eq 0
322+
popd
323+
324+
pushd shared_lib_empty
325+
"$fpm" build
326+
"$fpm" run
327+
"$fpm" test
328+
popd
329+
330+
pushd static_lib_empty
331+
"$fpm" build
332+
"$fpm" run
333+
"$fpm" test
334+
popd
335+
336+
pushd shared_app_only
337+
"$fpm" test || EXIT_CODE=$?
338+
test $EXIT_CODE -eq 0
339+
popd
340+
341+
# Static library dependencies
342+
pushd static_app_only
343+
"$fpm" test || EXIT_CODE=$?
344+
test $EXIT_CODE -eq 0
345+
popd
346+
313347
# Cleanup
314348
rm -rf ./*/build
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
program main
2+
use testdrive
3+
print *, 'Hello, world!'
4+
end program main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# App only, use shared lib from other folder
2+
name = "shared_app_only"
3+
library.type="shared"
4+
install.library=true
5+
[dependencies]
6+
shared_lib_extra = { path = "../shared_lib_extra" }
7+
[dev-dependencies]
8+
test-drive = { git = "https://github.com/fortran-lang/test-drive", tag="v0.5.0" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module test_shared_lib
2+
use testdrive, only : new_unittest, unittest_type, error_type, check
3+
use shared_lib, only: test_something
4+
5+
implicit none
6+
7+
public :: collect
8+
9+
10+
contains
11+
12+
!> Collect all exported unit tests
13+
subroutine collect(testsuite)
14+
!> Collection of tests
15+
type(unittest_type), allocatable, intent(out) :: testsuite(:)
16+
17+
testsuite = [ new_unittest("shared_lib", test_shared) ]
18+
19+
end subroutine collect
20+
21+
subroutine test_shared(error)
22+
type(error_type), allocatable, intent(out) :: error
23+
24+
call check(error, test_something(), 123, "Should be test_something==123")
25+
26+
end subroutine test_shared
27+
28+
end module test_shared_lib
29+
30+
program tester
31+
use, intrinsic :: iso_fortran_env, only : error_unit
32+
use testdrive, only : run_testsuite, new_testsuite, testsuite_type
33+
use test_shared_lib, only : collect
34+
implicit none
35+
integer :: stat
36+
type(testsuite_type), allocatable :: testsuite
37+
character(len=*), parameter :: fmt = '("#", *(1x, a))'
38+
39+
stat = 0
40+
41+
testsuite = new_testsuite("shared_lib", collect)
42+
43+
write(error_unit, fmt) "Testing:", testsuite%name
44+
call run_testsuite(testsuite%collect, error_unit, stat)
45+
46+
if (stat > 0) then
47+
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
48+
error stop
49+
end if
50+
end program tester

example_packages/shared_lib/fpm.toml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Shared library with no executables
2+
name = "shared_lib"
3+
library.type="shared"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module shared_lib
2+
implicit none
3+
private
4+
5+
public :: say_hello
6+
public :: test_something
7+
contains
8+
subroutine say_hello
9+
print *, "Hello, shared_lib!"
10+
end subroutine say_hello
11+
integer function test_something()
12+
test_something = 123
13+
end function test_something
14+
end module shared_lib
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name = "shared_lib_empty"
2+
library.type="shared"
3+
[dependencies]
4+
shared_lib = { path = "../shared_lib" }
5+
shared_app_only = { path = "../shared_app_only" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name = "shared_lib_extra"
2+
library.type="shared"
3+
[dependencies]
4+
shared_lib = { path = "../shared_lib" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module shared_lib_extra
2+
implicit none
3+
private
4+
5+
public :: say_extra_hello
6+
contains
7+
subroutine say_extra_hello
8+
print *, "Hello, shared_lib_extra!"
9+
end subroutine say_extra_hello
10+
end module shared_lib_extra
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
program main
2+
use testdrive
3+
print *, 'Hello, world!'
4+
end program main
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# App only, use shared libs from other folder, no provided sources
2+
name = "static_app_only"
3+
library.type="static"
4+
install.library=true
5+
[dependencies]
6+
shared_lib_extra = { path = "../shared_lib_extra" }
7+
[dev-dependencies]
8+
test-drive = { git = "https://github.com/fortran-lang/test-drive", tag="v0.5.0" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module test_shared_lib
2+
use testdrive, only : new_unittest, unittest_type, error_type, check
3+
use shared_lib, only: test_something
4+
5+
implicit none
6+
7+
public :: collect
8+
9+
10+
contains
11+
12+
!> Collect all exported unit tests
13+
subroutine collect(testsuite)
14+
!> Collection of tests
15+
type(unittest_type), allocatable, intent(out) :: testsuite(:)
16+
17+
testsuite = [ new_unittest("shared_lib", test_shared) ]
18+
19+
end subroutine collect
20+
21+
subroutine test_shared(error)
22+
type(error_type), allocatable, intent(out) :: error
23+
24+
call check(error, test_something(), 123, "Should be test_something==123")
25+
26+
end subroutine test_shared
27+
28+
end module test_shared_lib
29+
30+
program tester
31+
use, intrinsic :: iso_fortran_env, only : error_unit
32+
use testdrive, only : run_testsuite, new_testsuite, testsuite_type
33+
use test_shared_lib, only : collect
34+
implicit none
35+
integer :: stat
36+
type(testsuite_type), allocatable :: testsuite
37+
character(len=*), parameter :: fmt = '("#", *(1x, a))'
38+
39+
stat = 0
40+
41+
testsuite = new_testsuite("shared_lib", collect)
42+
43+
write(error_unit, fmt) "Testing:", testsuite%name
44+
call run_testsuite(testsuite%collect, error_unit, stat)
45+
46+
if (stat > 0) then
47+
write(error_unit, '(i0, 1x, a)') stat, "test(s) failed!"
48+
error stop
49+
end if
50+
end program tester
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name = "static_lib_empty"
2+
library.type="static"
3+
[dependencies]
4+
shared_lib = { path = "../shared_lib" }
5+
shared_app_only = { path = "../shared_app_only" }

0 commit comments

Comments
 (0)