From 8b5aebc74fde829c7c22203b96d592f8e67b9272 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Mon, 30 Mar 2020 16:39:07 -0500 Subject: Split settings into two stages --- app/Main.hs | 62 ++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 23 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a6f3ea2..a80e8f7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -32,11 +32,17 @@ import qualified Toml newtype Arguments = Arguments { command' :: Command } -data Settings = Settings { - settingsCompiler :: !Text - , settingsProjectName :: !Text - , settingsDebugOptions :: ![Text] - , settingsLibrary :: !Library } +data TomlSettings = TomlSettings { + tomlSettingsCompiler :: !Text + , tomlSettingsProjectName :: !Text + , tomlSettingsDebugOptions :: ![Text] + , tomlSettingsLibrary :: !Library } + +data AppSettings = AppSettings { + appSettingsCompiler :: !Text + , appSettingsProjectName :: !Text + , appSettingsDebugOptions :: ![Text] + , appSettingsLibrary :: !Library } data Library = Library { librarySourceDir :: !Text } @@ -46,25 +52,26 @@ main :: IO () main = do args <- getArguments fpmContents <- TIO.readFile "fpm.toml" - let settings = Toml.decode settingsCodec fpmContents - case settings of - Left err -> print err - Right settings -> do - app args settings - -app :: Arguments -> Settings -> IO () + let tomlSettings = Toml.decode settingsCodec fpmContents + case tomlSettings of + Left err -> print err + Right tomlSettings' -> do + appSettings <- toml2AppSettings tomlSettings' + app args appSettings + +app :: Arguments -> AppSettings -> IO () app args settings = case command' args of Run -> putStrLn "Run" Test -> putStrLn "Test" Build -> build settings -build :: Settings -> IO () +build :: AppSettings -> IO () build settings = do putStrLn "Building" - let compiler = unpack $ settingsCompiler settings - let projectName = unpack $ settingsProjectName settings - let flags = map unpack $ settingsDebugOptions settings - let librarySettings = settingsLibrary settings + let compiler = unpack $ appSettingsCompiler settings + let projectName = unpack $ appSettingsProjectName settings + let flags = map unpack $ appSettingsDebugOptions settings + let librarySettings = appSettingsLibrary settings let librarySourceDir' = unpack $ librarySourceDir librarySettings buildLibrary librarySourceDir' [".f90", ".f", ".F", ".F90", ".f95", ".f03"] @@ -113,17 +120,26 @@ getDirectoriesFiles dirs exts = getDirectoryFilesIO "" newPatterns newPatterns = concatMap appendExts dirs appendExts dir = map ((dir "*") ++) exts -settingsCodec :: TomlCodec Settings +settingsCodec :: TomlCodec TomlSettings settingsCodec = - Settings + TomlSettings <$> Toml.text "compiler" - .= settingsCompiler + .= tomlSettingsCompiler <*> Toml.text "name" - .= settingsProjectName + .= tomlSettingsProjectName <*> Toml.arrayOf Toml._Text "debug-options" - .= settingsDebugOptions + .= tomlSettingsDebugOptions <*> Toml.table libraryCodec "library" - .= settingsLibrary + .= tomlSettingsLibrary libraryCodec :: TomlCodec Library libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir + +toml2AppSettings :: TomlSettings -> IO AppSettings +toml2AppSettings tomlSettings = do + return AppSettings + { appSettingsCompiler = tomlSettingsCompiler tomlSettings + , appSettingsProjectName = tomlSettingsProjectName tomlSettings + , appSettingsDebugOptions = tomlSettingsDebugOptions tomlSettings + , appSettingsLibrary = tomlSettingsLibrary tomlSettings + } -- cgit v1.2.3 From cff70a2c97d26e64c65c391ec2121df90196b6cd Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Tue, 31 Mar 2020 15:00:49 -0500 Subject: Make library settings optional --- app/Main.hs | 56 +++++++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a80e8f7..974c7a6 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -36,13 +36,13 @@ data TomlSettings = TomlSettings { tomlSettingsCompiler :: !Text , tomlSettingsProjectName :: !Text , tomlSettingsDebugOptions :: ![Text] - , tomlSettingsLibrary :: !Library } + , tomlSettingsLibrary :: !(Maybe Library) } data AppSettings = AppSettings { appSettingsCompiler :: !Text , appSettingsProjectName :: !Text , appSettingsDebugOptions :: ![Text] - , appSettingsLibrary :: !Library } + , appSettingsLibrary :: !(Maybe Library) } data Library = Library { librarySourceDir :: !Text } @@ -68,26 +68,36 @@ app args settings = case command' args of build :: AppSettings -> IO () build settings = do putStrLn "Building" - let compiler = unpack $ appSettingsCompiler settings - let projectName = unpack $ appSettingsProjectName settings - let flags = map unpack $ appSettingsDebugOptions settings - let librarySettings = appSettingsLibrary settings - let librarySourceDir' = unpack $ librarySourceDir librarySettings - buildLibrary librarySourceDir' - [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" "library") - compiler - flags - projectName - [] - buildProgram "app" - ["build" "library"] - [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" "app") - compiler - flags - projectName - "main.f90" + let compiler = unpack $ appSettingsCompiler settings + let projectName = unpack $ appSettingsProjectName settings + let flags = map unpack $ appSettingsDebugOptions settings + case appSettingsLibrary settings of + Just librarySettings -> do + let librarySourceDir' = unpack $ librarySourceDir librarySettings + buildLibrary librarySourceDir' + [".f90", ".f", ".F", ".F90", ".f95", ".f03"] + ("build" "library") + compiler + flags + projectName + [] + buildProgram "app" + ["build" "library"] + [".f90", ".f", ".F", ".F90", ".f95", ".f03"] + ("build" "app") + compiler + flags + projectName + "main.f90" + Nothing -> do + buildProgram "app" + [] + [".f90", ".f", ".F", ".F90", ".f95", ".f03"] + ("build" "app") + compiler + flags + projectName + "main.f90" getArguments :: IO Arguments getArguments = execParser @@ -129,7 +139,7 @@ settingsCodec = .= tomlSettingsProjectName <*> Toml.arrayOf Toml._Text "debug-options" .= tomlSettingsDebugOptions - <*> Toml.table libraryCodec "library" + <*> Toml.dioptional (Toml.table libraryCodec "library") .= tomlSettingsLibrary libraryCodec :: TomlCodec Library -- cgit v1.2.3 From 43b28f9559cb4cb3affb1bee7063bf27399c7b31 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Tue, 31 Mar 2020 15:12:50 -0500 Subject: Add default for library settings --- app/Main.hs | 13 ++++++++++++- example_project/fpm.toml | 3 --- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 974c7a6..8d07c40 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -25,6 +25,7 @@ import Options.Applicative ( Parser , progDesc , subparser ) +import System.Directory ( doesDirectoryExist ) import Toml ( TomlCodec , (.=) ) @@ -147,9 +148,19 @@ libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir toml2AppSettings :: TomlSettings -> IO AppSettings toml2AppSettings tomlSettings = do + librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings return AppSettings { appSettingsCompiler = tomlSettingsCompiler tomlSettings , appSettingsProjectName = tomlSettingsProjectName tomlSettings , appSettingsDebugOptions = tomlSettingsDebugOptions tomlSettings - , appSettingsLibrary = tomlSettingsLibrary tomlSettings + , appSettingsLibrary = librarySettings } + +getLibrarySettings :: Maybe Library -> IO (Maybe Library) +getLibrarySettings maybeSettings = case maybeSettings of + Just settings -> return maybeSettings + Nothing -> do + defaultExists <- doesDirectoryExist "src" + if defaultExists + then return (Just (Library { librarySourceDir = "src" })) + else return Nothing diff --git a/example_project/fpm.toml b/example_project/fpm.toml index a8bded7..fae3512 100644 --- a/example_project/fpm.toml +++ b/example_project/fpm.toml @@ -9,9 +9,6 @@ compiler = "gfortran" debug-options = ["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"] release-options = ["-O3"] -[library] - source-dir = "src" - [executables.example_project] main = "main.f90" source-dir = "app" -- cgit v1.2.3 From 623a9d5a2f4decf0bb71f8d44d21148b141f6204 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Tue, 31 Mar 2020 15:27:30 -0500 Subject: Remove compiler flags from toml --- app/Main.hs | 25 ++++++++++++++++--------- example_project/fpm.toml | 2 -- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 8d07c40..e9b0737 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -36,13 +36,12 @@ newtype Arguments = Arguments { command' :: Command } data TomlSettings = TomlSettings { tomlSettingsCompiler :: !Text , tomlSettingsProjectName :: !Text - , tomlSettingsDebugOptions :: ![Text] , tomlSettingsLibrary :: !(Maybe Library) } data AppSettings = AppSettings { appSettingsCompiler :: !Text , appSettingsProjectName :: !Text - , appSettingsDebugOptions :: ![Text] + , appSettingsFlags :: ![Text] , appSettingsLibrary :: !(Maybe Library) } data Library = Library { librarySourceDir :: !Text } @@ -71,7 +70,7 @@ build settings = do putStrLn "Building" let compiler = unpack $ appSettingsCompiler settings let projectName = unpack $ appSettingsProjectName settings - let flags = map unpack $ appSettingsDebugOptions settings + let flags = map unpack $ appSettingsFlags settings case appSettingsLibrary settings of Just librarySettings -> do let librarySourceDir' = unpack $ librarySourceDir librarySettings @@ -138,8 +137,6 @@ settingsCodec = .= tomlSettingsCompiler <*> Toml.text "name" .= tomlSettingsProjectName - <*> Toml.arrayOf Toml._Text "debug-options" - .= tomlSettingsDebugOptions <*> Toml.dioptional (Toml.table libraryCodec "library") .= tomlSettingsLibrary @@ -150,10 +147,20 @@ toml2AppSettings :: TomlSettings -> IO AppSettings toml2AppSettings tomlSettings = do librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings return AppSettings - { appSettingsCompiler = tomlSettingsCompiler tomlSettings - , appSettingsProjectName = tomlSettingsProjectName tomlSettings - , appSettingsDebugOptions = tomlSettingsDebugOptions tomlSettings - , appSettingsLibrary = librarySettings + { appSettingsCompiler = tomlSettingsCompiler tomlSettings + , appSettingsProjectName = tomlSettingsProjectName tomlSettings + , appSettingsFlags = [ "-Wall" + , "-Wextra" + , "-Wimplicit-interface" + , "-Werror" + , "-fPIC" + , "-fmax-errors=1" + , "-g" + , "-fbounds-check" + , "-fcheck-array-temporaries" + , "-fbacktrace" + ] + , appSettingsLibrary = librarySettings } getLibrarySettings :: Maybe Library -> IO (Maybe Library) diff --git a/example_project/fpm.toml b/example_project/fpm.toml index fae3512..f2ae1c8 100644 --- a/example_project/fpm.toml +++ b/example_project/fpm.toml @@ -6,8 +6,6 @@ maintainer = "example@example.com" copyright = "2020 Author" dependencies = [] compiler = "gfortran" -debug-options = ["-g", "-Wall", "-Wextra", "-Werror", "-pedantic"] -release-options = ["-O3"] [executables.example_project] main = "main.f90" -- cgit v1.2.3 From 8571ae8c1ba5c0b082492a4e3b3043a580029494 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Tue, 31 Mar 2020 15:44:12 -0500 Subject: Add option for building in release mode --- app/Main.hs | 75 +++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 48 insertions(+), 27 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index e9b0737..9e3f264 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -19,11 +19,14 @@ import Options.Applicative ( Parser , command , execParser , fullDesc - , info , header + , help , helper + , info + , long , progDesc , subparser + , switch ) import System.Directory ( doesDirectoryExist ) import Toml ( TomlCodec @@ -31,7 +34,7 @@ import Toml ( TomlCodec ) import qualified Toml -newtype Arguments = Arguments { command' :: Command } +data Arguments = Arguments { command' :: Command, release :: Bool } data TomlSettings = TomlSettings { tomlSettingsCompiler :: !Text @@ -56,7 +59,7 @@ main = do case tomlSettings of Left err -> print err Right tomlSettings' -> do - appSettings <- toml2AppSettings tomlSettings' + appSettings <- toml2AppSettings tomlSettings' (release args) app args appSettings app :: Arguments -> AppSettings -> IO () @@ -109,20 +112,24 @@ getArguments = execParser ) 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")) - ) +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")) + ) + <*> switch (long "release" <> help "Build in release mode") -runArguments :: Parser Arguments -runArguments = pure $ Arguments Run +runArguments :: Parser Command +runArguments = pure Run -testArguments :: Parser Arguments -testArguments = pure $ Arguments Test +testArguments :: Parser Command +testArguments = pure Test -buildArguments :: Parser Arguments -buildArguments = pure $ Arguments Build +buildArguments :: Parser Command +buildArguments = pure Build getDirectoriesFiles :: [FilePath] -> [FilePattern] -> IO [FilePath] getDirectoriesFiles dirs exts = getDirectoryFilesIO "" newPatterns @@ -143,23 +150,37 @@ settingsCodec = libraryCodec :: TomlCodec Library libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir -toml2AppSettings :: TomlSettings -> IO AppSettings -toml2AppSettings tomlSettings = do +toml2AppSettings :: TomlSettings -> Bool -> IO AppSettings +toml2AppSettings tomlSettings release = do librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings return AppSettings { appSettingsCompiler = tomlSettingsCompiler tomlSettings , appSettingsProjectName = tomlSettingsProjectName tomlSettings - , appSettingsFlags = [ "-Wall" - , "-Wextra" - , "-Wimplicit-interface" - , "-Werror" - , "-fPIC" - , "-fmax-errors=1" - , "-g" - , "-fbounds-check" - , "-fcheck-array-temporaries" - , "-fbacktrace" - ] + , appSettingsFlags = if release + then + [ "-Wall" + , "-Wextra" + , "-Wimplicit-interface" + , "-Werror" + , "-fPIC" + , "-fmax-errors=1" + , "-O3" + , "-march=native" + , "-ffast-math" + , "-funroll-loops" + ] + else + [ "-Wall" + , "-Wextra" + , "-Wimplicit-interface" + , "-Werror" + , "-fPIC" + , "-fmax-errors=1" + , "-g" + , "-fbounds-check" + , "-fcheck-array-temporaries" + , "-fbacktrace" + ] , appSettingsLibrary = librarySettings } -- cgit v1.2.3