Skip to content

Commit edf79a8

Browse files
Merge pull request #85 from everythingfunctional/TestOnlyDependencies
Implement test/executable specific dependencies
2 parents c2d5c65 + 3a4f1db commit edf79a8

File tree

25 files changed

+357
-101
lines changed

25 files changed

+357
-101
lines changed

PACKAGING.md

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,3 +532,133 @@ ar: creating build/debug/library/math_constants.a
532532
# gfortran (for build/debug/test/runTests)
533533
sin(pi) = 1.2246467991473532E-016
534534
```
535+
536+
### Adding Dependencies
537+
538+
Inevitably you'll want to be able to include other libraries in your project.
539+
fpm makes this incredibly simple, by taking care of fetching and compiling your
540+
dependencies for you. You just tell it what your dependencies are, and where to
541+
find them. Let's add a dependency to our library. Now our `fpm.toml` file looks
542+
like this:
543+
544+
```toml
545+
name = "math_constants"
546+
version = "0.1.0"
547+
license = "MIT"
548+
author = "Jane Programmer"
549+
maintainer = "[email protected]"
550+
copyright = "2020 Jane Programmer"
551+
552+
[library]
553+
source-dir="src"
554+
555+
[dependencies]
556+
helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git" }
557+
558+
[[executable]]
559+
name="math_constants"
560+
source-dir="app"
561+
main="main.f90"
562+
563+
[[test]]
564+
name="runTests"
565+
source-dir="test"
566+
main="main.f90"
567+
```
568+
569+
Now you can use any modules from this library anywhere in your code. Just like
570+
this:
571+
572+
```fortran
573+
program demo
574+
use helloff, only: create_greeting
575+
use math_constants, only: e, pi, half_pi, two_pi
576+
print *, 'math_constants library demo'
577+
print *, 'pi = ', pi
578+
print *, '2*pi = ', two_pi
579+
print *, 'pi/2 = ', half_pi
580+
print *, 'e = ', e
581+
print *, create_greeting("fpm")
582+
end program demo
583+
```
584+
585+
And now `fpm run` will output the following:
586+
587+
```
588+
math_constants library demo
589+
pi = 3.1415926535897931
590+
2*pi = 6.2831853071795862
591+
pi/2 = 1.5707963267948966
592+
e = 2.7182818284590451
593+
Hello, fpm!
594+
```
595+
596+
Additionally, any users of your library will now automatically depend on your
597+
dependencies too. So if you don't need that depedency for the library, like in
598+
the above example, then you can specify it for the specific executable like
599+
below. Then fpm will still fetch and compile it when building your executable,
600+
but users of your library won't have to.
601+
602+
```toml
603+
name = "math_constants"
604+
version = "0.1.0"
605+
license = "MIT"
606+
author = "Jane Programmer"
607+
maintainer = "[email protected]"
608+
copyright = "2020 Jane Programmer"
609+
610+
[library]
611+
source-dir="src"
612+
613+
[[executable]]
614+
name="math_constants"
615+
source-dir="app"
616+
main="main.f90"
617+
[executable.dependencies]
618+
helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git" }
619+
620+
621+
[[test]]
622+
name="runTests"
623+
source-dir="test"
624+
main="main.f90"
625+
```
626+
627+
You can also specify dependencies for your tests in a similar way, with
628+
`[test.dependencies]` instead of `[executable.dependencies]`. There's also
629+
another option for test dependencies. The below example makes the dependencies
630+
available for all the tests, but again your users won't depend on these.
631+
632+
```toml
633+
name = "math_constants"
634+
version = "0.1.0"
635+
license = "MIT"
636+
author = "Jane Programmer"
637+
maintainer = "[email protected]"
638+
copyright = "2020 Jane Programmer"
639+
640+
[library]
641+
source-dir="src"
642+
643+
[dev-dependencies]
644+
helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git" }
645+
646+
[[executable]]
647+
name="math_constants"
648+
source-dir="app"
649+
main="main.f90"
650+
651+
[[test]]
652+
name="runTests"
653+
source-dir="test"
654+
main="main.f90"
655+
```
656+
657+
You can also be specific about which version of a dependency you'd like. You can
658+
specify a branch to use like `helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git", branch = "master" }`,
659+
or a tag like `helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git", tag = "v1.2.3" }`,
660+
or even a specific commit like `helloff = { git = "https://gitlab.com/everythingfunctional/helloff.git", rev = "a1b2c3" }`.
661+
You can even specify the path to another folder, if for example you've got another
662+
fpm package in the same repository. Like this: `helloff = { path = "helloff" }`.
663+
Note that you should *not* specify paths outside of your repository, or things
664+
won't work for your users.

src/Build.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ buildProgram
6969
-> [String]
7070
-> String
7171
-> FilePath
72+
-> [FilePath]
7273
-> IO ()
73-
buildProgram programDirectory libraryDirectories sourceExtensions buildDirectory compiler flags programName programSource
74+
buildProgram programDirectory libraryDirectories sourceExtensions buildDirectory compiler flags programName programSource archives
7475
= do
7576
sourceFiles <- getDirectoriesFiles [programDirectory] sourceExtensions
7677
canonicalProgramSource <- makeAbsolute $ programDirectory </> programSource
@@ -93,7 +94,6 @@ buildProgram programDirectory libraryDirectories sourceExtensions buildDirectory
9394
let allModuleMaps =
9495
moduleLookupMap `Map.union` foldl Map.union Map.empty otherModuleMaps
9596
let includeFlags = map ("-I" ++) libraryDirectories
96-
archives <- getDirectoriesFiles libraryDirectories [".a"]
9797
shake shakeOptions { shakeFiles = buildDirectory
9898
, shakeChange = ChangeModtimeAndDigest
9999
, shakeColor = True
@@ -151,7 +151,7 @@ buildLibrary
151151
-> [String]
152152
-> String
153153
-> [FilePath]
154-
-> IO ()
154+
-> IO (FilePath)
155155
buildLibrary libraryDirectory sourceExtensions buildDirectory compiler flags libraryName otherLibraryDirectories
156156
= do
157157
sourceFiles <- getDirectoriesFiles [libraryDirectory] sourceExtensions
@@ -193,6 +193,7 @@ buildLibrary libraryDirectory sourceExtensions buildDirectory compiler flags lib
193193
need objectFiles
194194
cmd "ar" ["rs"] a objectFiles
195195
want [archiveFile]
196+
return archiveFile
196197

197198
-- A little wrapper around getDirectoryFiles so we can get files from multiple directories
198199
getDirectoriesFiles :: [FilePath] -> [FilePattern] -> IO [FilePath]

0 commit comments

Comments
 (0)