Skip to content

Read fpm toml #47

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

Merged
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
89 changes: 54 additions & 35 deletions app/Main.hs
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
{-# LANGUAGE OverloadedStrings #-}

module Main where

import Build ( buildLibrary
, buildPrograms
)
import Data.Text ( Text
, unpack
)
import qualified Data.Text.IO as TIO
import Development.Shake ( FilePattern
, (<//>)
, getDirectoryFilesIO
Expand All @@ -19,54 +25,64 @@ import Options.Applicative ( Parser
, progDesc
, subparser
)
import Toml ( TomlCodec
, (.=)
)
import qualified Toml

newtype Arguments = Arguments { command' :: Command }

data Settings = Settings { compiler :: !Text }

data Command = Run | Test | Build

main :: IO ()
main = do
args <- getArguments
app args
args <- getArguments
fpmContents <- TIO.readFile "fpm.toml"
let settings = Toml.decode settingsCodec fpmContents
case settings of
Left err -> print err
Right settings -> app args settings

app :: Arguments -> IO ()
app args = case command' args of
Run -> putStrLn "Run"
Test -> putStrLn "Test"
Build -> build
app :: Arguments -> Settings -> IO ()
app args settings = case command' args of
Run -> putStrLn "Run"
Test -> putStrLn "Test"
Build -> build settings

build :: IO ()
build = do
putStrLn "Building"
buildLibrary "src"
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
("build" </> "library")
"gfortran"
["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]
"library"
[]
buildPrograms "app"
["build" </> "library"]
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
("build" </> "app")
"gfortran"
["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]
build :: Settings -> IO ()
build settings = do
putStrLn "Building"
buildLibrary "src"
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
("build" </> "library")
(unpack $ compiler settings)
["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]
"library"
[]
buildPrograms "app"
["build" </> "library"]
[".f90", ".f", ".F", ".F90", ".f95", ".f03"]
("build" </> "app")
(unpack $ compiler settings)
["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]

getArguments :: IO Arguments
getArguments = execParser
(info
(arguments <**> helper)
(fullDesc <> progDesc "Work with Fortran projects" <> header
"fpm - A Fortran package manager and build system"
)
(info
(arguments <**> helper)
(fullDesc <> progDesc "Work with Fortran projects" <> header
"fpm - A Fortran package manager and build system"
)
)

arguments :: Parser Arguments
arguments = subparser
( command "run" (info runArguments (progDesc "Run the executable"))
<> command "test" (info testArguments (progDesc "Run the tests"))
<> command "build" (info buildArguments (progDesc "Build the executable"))
)
( command "run" (info runArguments (progDesc "Run the executable"))
<> command "test" (info testArguments (progDesc "Run the tests"))
<> command "build" (info buildArguments (progDesc "Build the executable"))
)

runArguments :: Parser Arguments
runArguments = pure $ Arguments Run
Expand All @@ -79,6 +95,9 @@ buildArguments = pure $ Arguments Build

getDirectoriesFiles :: [FilePath] -> [FilePattern] -> IO [FilePath]
getDirectoriesFiles dirs exts = getDirectoryFilesIO "" newPatterns
where
newPatterns = concatMap appendExts dirs
appendExts dir = map ((dir <//> "*") ++) exts
where
newPatterns = concatMap appendExts dirs
appendExts dir = map ((dir <//> "*") ++) exts

settingsCodec :: TomlCodec Settings
settingsCodec = Settings <$> Toml.text "compiler" .= compiler
25 changes: 25 additions & 0 deletions example_fpm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name = "package-name"
version = "0.1.0"
license = "BSD3"
author = "Author name here"
maintainer = "[email protected]"
copyright = "2020 Author name here"
dependencies = ["../std-lib.tar.gz"]
compiler = "gfortran"
devel-options = ["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]
release-options = ["-O3"]

[library]
source-dirs = "src"

[executables.executable-name]
main = "Main.f90"
source-dirs = "app"
linker-options = ["-O3"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a compiler option, no? Probably not consequential because this is meant as example metadata. I don't know if -Ox flags can be passed to a linker or if they are just ignored. Is there a more meaningful option we can use here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, based on my comment above, I suggest we first tackle the common case of not specifying such options at all, but have a sane default for Debug / Release for all compilers. Exactly as Cargo works. Only later also allow to explicitly set this somehow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Linkers can do some amount of optimization at link time. It's the only way to do inlining of functions from separate compilation units. This is just an example anyway, and this PR doesn't read it anyway.

dependencies = ["iso_varying_string"]

[tests.test-name]
main = "Spec.f90"
source-dirs = "test"
linker-options = ["-Og"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above.

dependencies = ["vegetables >= 1.0 && < 2.0"]
19 changes: 19 additions & 0 deletions example_project/fpm.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name = "example_project"
version = "0.1.0"
license = "BSD3"
author = "Author"
maintainer = "[email protected]"
copyright = "2020 Author"
dependencies = []
compiler = "gfortran"
devel-options = ["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"]
release-options = ["-O3"]

[library]
source-dirs = "src"

[executables.Hello_world]
main = "Hello_world.f90"
source-dirs = "app"
linker-options = ["-O3"]
dependencies = []
3 changes: 2 additions & 1 deletion package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ dependencies:
- containers
- directory
- filepath
- MissingH
- optparse-applicative
- process
- shake
- split
- text
- tomland >= 1.0


library:
Expand Down
Loading