From e7f04140aea8d05b3ba450841c4ac8c39326ecfb Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Sat, 11 Apr 2020 13:48:40 -0500 Subject: add settings for executables --- app/Main.hs | 50 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 32 insertions(+), 18 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 9e3f264..c4aee75 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -45,10 +45,16 @@ data AppSettings = AppSettings { appSettingsCompiler :: !Text , appSettingsProjectName :: !Text , appSettingsFlags :: ![Text] - , appSettingsLibrary :: !(Maybe Library) } + , appSettingsLibrary :: !(Maybe Library) + , appSettingsExecutables :: ![Executable] } data Library = Library { librarySourceDir :: !Text } +data Executable = Executable { + executableSourceDir :: !Text + , executableMainFile :: !Text + , executableName :: !Text } + data Command = Run | Test | Build main :: IO () @@ -74,7 +80,8 @@ build settings = do let compiler = unpack $ appSettingsCompiler settings let projectName = unpack $ appSettingsProjectName settings let flags = map unpack $ appSettingsFlags settings - case appSettingsLibrary settings of + let executables = appSettingsExecutables settings + executableDepends <- case appSettingsLibrary settings of Just librarySettings -> do let librarySourceDir' = unpack $ librarySourceDir librarySettings buildLibrary librarySourceDir' @@ -84,23 +91,25 @@ build settings = do flags projectName [] - buildProgram "app" - ["build" "library"] - [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" "app") - compiler - flags - projectName - "main.f90" + return ["build" "library"] Nothing -> do - buildProgram "app" - [] - [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" "app") - compiler - flags - projectName - "main.f90" + return [] + mapM_ + (\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } -> + do + let sourceDir' = unpack sourceDir + let name' = unpack name + let mainFile' = unpack mainFile + buildProgram sourceDir' + executableDepends + [".f90", ".f", ".F", ".F90", ".f95", ".f03"] + ("build" sourceDir') + compiler + flags + name' + mainFile' + ) + executables getArguments :: IO Arguments getArguments = execParser @@ -182,6 +191,11 @@ toml2AppSettings tomlSettings release = do , "-fbacktrace" ] , appSettingsLibrary = librarySettings + , appSettingsExecutables = [ Executable { executableSourceDir = "app" + , executableName = "example_project" + , executableMainFile = "main.f90" + } + ] } getLibrarySettings :: Maybe Library -> IO (Maybe Library) -- cgit v1.2.3 From a65fed0b63ca301882c3d2a44a73cb413ac92c3f Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Sat, 11 Apr 2020 15:15:25 -0500 Subject: Read executable settings from file --- app/Main.hs | 36 +++++++++++++++++++++++++++--------- example_project/fpm.toml | 5 +++-- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index c4aee75..b951ad8 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -39,21 +39,25 @@ data Arguments = Arguments { command' :: Command, release :: Bool } data TomlSettings = TomlSettings { tomlSettingsCompiler :: !Text , tomlSettingsProjectName :: !Text - , tomlSettingsLibrary :: !(Maybe Library) } + , tomlSettingsLibrary :: !(Maybe Library) + , tomlSettingsExecutables :: ![Executable] +} data AppSettings = AppSettings { appSettingsCompiler :: !Text , appSettingsProjectName :: !Text , appSettingsFlags :: ![Text] , appSettingsLibrary :: !(Maybe Library) - , appSettingsExecutables :: ![Executable] } + , appSettingsExecutables :: ![Executable] +} data Library = Library { librarySourceDir :: !Text } data Executable = Executable { executableSourceDir :: !Text , executableMainFile :: !Text - , executableName :: !Text } + , executableName :: !Text +} deriving Show data Command = Run | Test | Build @@ -155,13 +159,27 @@ settingsCodec = .= tomlSettingsProjectName <*> Toml.dioptional (Toml.table libraryCodec "library") .= tomlSettingsLibrary + <*> Toml.list executableCodec "executable" + .= tomlSettingsExecutables libraryCodec :: TomlCodec Library libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir +executableCodec :: TomlCodec Executable +executableCodec = + Executable + <$> Toml.text "source-dir" + .= executableSourceDir + <*> Toml.text "main" + .= executableMainFile + <*> Toml.text "name" + .= executableName + toml2AppSettings :: TomlSettings -> Bool -> IO AppSettings toml2AppSettings tomlSettings release = do - librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings + librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings + executableSettings <- getExecutableSettings + $ tomlSettingsExecutables tomlSettings return AppSettings { appSettingsCompiler = tomlSettingsCompiler tomlSettings , appSettingsProjectName = tomlSettingsProjectName tomlSettings @@ -191,11 +209,7 @@ toml2AppSettings tomlSettings release = do , "-fbacktrace" ] , appSettingsLibrary = librarySettings - , appSettingsExecutables = [ Executable { executableSourceDir = "app" - , executableName = "example_project" - , executableMainFile = "main.f90" - } - ] + , appSettingsExecutables = executableSettings } getLibrarySettings :: Maybe Library -> IO (Maybe Library) @@ -206,3 +220,7 @@ getLibrarySettings maybeSettings = case maybeSettings of if defaultExists then return (Just (Library { librarySourceDir = "src" })) else return Nothing + +getExecutableSettings :: [Executable] -> IO [Executable] +getExecutableSettings [] = undefined +getExecutableSettings executables = return executables diff --git a/example_project/fpm.toml b/example_project/fpm.toml index f2ae1c8..f6c6e1d 100644 --- a/example_project/fpm.toml +++ b/example_project/fpm.toml @@ -7,7 +7,8 @@ copyright = "2020 Author" dependencies = [] compiler = "gfortran" -[executables.example_project] -main = "main.f90" +[[executable]] +name = "example_project" source-dir = "app" +main = "main.f90" dependencies = [] -- cgit v1.2.3 From fde13b66701956a53282f558d79a8ddbfca0c905 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Sat, 11 Apr 2020 15:26:40 -0500 Subject: Enable default settings for executables --- app/Main.hs | 29 +++++++++++++++++++++++------ example_project/fpm.toml | 6 ------ 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index b951ad8..66dedb8 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -28,7 +28,9 @@ import Options.Applicative ( Parser , subparser , switch ) -import System.Directory ( doesDirectoryExist ) +import System.Directory ( doesDirectoryExist + , doesFileExist + ) import Toml ( TomlCodec , (.=) ) @@ -177,12 +179,14 @@ executableCodec = toml2AppSettings :: TomlSettings -> Bool -> IO AppSettings toml2AppSettings tomlSettings release = do + let projectName = tomlSettingsProjectName tomlSettings librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings executableSettings <- getExecutableSettings - $ tomlSettingsExecutables tomlSettings + (tomlSettingsExecutables tomlSettings) + projectName return AppSettings { appSettingsCompiler = tomlSettingsCompiler tomlSettings - , appSettingsProjectName = tomlSettingsProjectName tomlSettings + , appSettingsProjectName = projectName , appSettingsFlags = if release then [ "-Wall" @@ -221,6 +225,19 @@ getLibrarySettings maybeSettings = case maybeSettings of then return (Just (Library { librarySourceDir = "src" })) else return Nothing -getExecutableSettings :: [Executable] -> IO [Executable] -getExecutableSettings [] = undefined -getExecutableSettings executables = return executables +getExecutableSettings :: [Executable] -> Text -> IO [Executable] +getExecutableSettings [] projectName = do + defaultDirectoryExists <- doesDirectoryExist "app" + if defaultDirectoryExists + then do + defaultMainExists <- doesFileExist ("app" "main.f90") + if defaultMainExists + then return + [ Executable { executableSourceDir = "app" + , executableMainFile = "main.f90" + , executableName = projectName + } + ] + else return [] + else return [] +getExecutableSettings executables _ = return executables diff --git a/example_project/fpm.toml b/example_project/fpm.toml index f6c6e1d..38cc3b7 100644 --- a/example_project/fpm.toml +++ b/example_project/fpm.toml @@ -6,9 +6,3 @@ maintainer = "example@example.com" copyright = "2020 Author" dependencies = [] compiler = "gfortran" - -[[executable]] -name = "example_project" -source-dir = "app" -main = "main.f90" -dependencies = [] -- cgit v1.2.3 From aea00bcfebff593eb6661b01aa18ad3a91b60ffb Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Sat, 11 Apr 2020 15:32:47 -0500 Subject: Switch to just using String instead of Text --- app/Main.hs | 64 ++++++++++++++++++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 66dedb8..23030e2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -5,9 +5,6 @@ module Main where import Build ( buildLibrary , buildProgram ) -import Data.Text ( Text - , unpack - ) import qualified Data.Text.IO as TIO import Development.Shake ( FilePattern , () @@ -39,27 +36,27 @@ import qualified Toml data Arguments = Arguments { command' :: Command, release :: Bool } data TomlSettings = TomlSettings { - tomlSettingsCompiler :: !Text - , tomlSettingsProjectName :: !Text - , tomlSettingsLibrary :: !(Maybe Library) - , tomlSettingsExecutables :: ![Executable] + tomlSettingsCompiler :: String + , tomlSettingsProjectName :: String + , tomlSettingsLibrary :: (Maybe Library) + , tomlSettingsExecutables :: [Executable] } data AppSettings = AppSettings { - appSettingsCompiler :: !Text - , appSettingsProjectName :: !Text - , appSettingsFlags :: ![Text] - , appSettingsLibrary :: !(Maybe Library) - , appSettingsExecutables :: ![Executable] + appSettingsCompiler :: String + , appSettingsProjectName :: String + , appSettingsFlags :: [String] + , appSettingsLibrary :: (Maybe Library) + , appSettingsExecutables :: [Executable] } -data Library = Library { librarySourceDir :: !Text } +data Library = Library { librarySourceDir :: String } data Executable = Executable { - executableSourceDir :: !Text - , executableMainFile :: !Text - , executableName :: !Text -} deriving Show + executableSourceDir :: String + , executableMainFile :: String + , executableName :: String +} data Command = Run | Test | Build @@ -83,13 +80,13 @@ 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 $ appSettingsFlags settings + let compiler = appSettingsCompiler settings + let projectName = appSettingsProjectName settings + let flags = appSettingsFlags settings let executables = appSettingsExecutables settings executableDepends <- case appSettingsLibrary settings of Just librarySettings -> do - let librarySourceDir' = unpack $ librarySourceDir librarySettings + let librarySourceDir' = librarySourceDir librarySettings buildLibrary librarySourceDir' [".f90", ".f", ".F", ".F90", ".f95", ".f03"] ("build" "library") @@ -103,17 +100,14 @@ build settings = do mapM_ (\Executable { executableSourceDir = sourceDir, executableMainFile = mainFile, executableName = name } -> do - let sourceDir' = unpack sourceDir - let name' = unpack name - let mainFile' = unpack mainFile - buildProgram sourceDir' + buildProgram sourceDir executableDepends [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" sourceDir') + ("build" sourceDir) compiler flags - name' - mainFile' + name + mainFile ) executables @@ -155,9 +149,9 @@ getDirectoriesFiles dirs exts = getDirectoryFilesIO "" newPatterns settingsCodec :: TomlCodec TomlSettings settingsCodec = TomlSettings - <$> Toml.text "compiler" + <$> Toml.string "compiler" .= tomlSettingsCompiler - <*> Toml.text "name" + <*> Toml.string "name" .= tomlSettingsProjectName <*> Toml.dioptional (Toml.table libraryCodec "library") .= tomlSettingsLibrary @@ -165,16 +159,16 @@ settingsCodec = .= tomlSettingsExecutables libraryCodec :: TomlCodec Library -libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir +libraryCodec = Library <$> Toml.string "source-dir" .= librarySourceDir executableCodec :: TomlCodec Executable executableCodec = Executable - <$> Toml.text "source-dir" + <$> Toml.string "source-dir" .= executableSourceDir - <*> Toml.text "main" + <*> Toml.string "main" .= executableMainFile - <*> Toml.text "name" + <*> Toml.string "name" .= executableName toml2AppSettings :: TomlSettings -> Bool -> IO AppSettings @@ -225,7 +219,7 @@ getLibrarySettings maybeSettings = case maybeSettings of then return (Just (Library { librarySourceDir = "src" })) else return Nothing -getExecutableSettings :: [Executable] -> Text -> IO [Executable] +getExecutableSettings :: [Executable] -> String -> IO [Executable] getExecutableSettings [] projectName = do defaultDirectoryExists <- doesDirectoryExist "app" if defaultDirectoryExists -- cgit v1.2.3 From ba4f284b66b23ba00bb7086203af1e9d7630a177 Mon Sep 17 00:00:00 2001 From: Brad Richardson Date: Sat, 11 Apr 2020 16:44:16 -0500 Subject: Build debug and release versions in different places --- app/Main.hs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 23030e2..9438eb2 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -45,6 +45,7 @@ data TomlSettings = TomlSettings { data AppSettings = AppSettings { appSettingsCompiler :: String , appSettingsProjectName :: String + , appSettingsBuildPrefix :: String , appSettingsFlags :: [String] , appSettingsLibrary :: (Maybe Library) , appSettingsExecutables :: [Executable] @@ -82,6 +83,7 @@ build settings = do putStrLn "Building" let compiler = appSettingsCompiler settings let projectName = appSettingsProjectName settings + let buildPrefix = appSettingsBuildPrefix settings let flags = appSettingsFlags settings let executables = appSettingsExecutables settings executableDepends <- case appSettingsLibrary settings of @@ -89,12 +91,12 @@ build settings = do let librarySourceDir' = librarySourceDir librarySettings buildLibrary librarySourceDir' [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" "library") + (buildPrefix "library") compiler flags projectName [] - return ["build" "library"] + return [buildPrefix "library"] Nothing -> do return [] mapM_ @@ -103,7 +105,7 @@ build settings = do buildProgram sourceDir executableDepends [".f90", ".f", ".F", ".F90", ".f95", ".f03"] - ("build" sourceDir) + (buildPrefix sourceDir) compiler flags name @@ -181,6 +183,8 @@ toml2AppSettings tomlSettings release = do return AppSettings { appSettingsCompiler = tomlSettingsCompiler tomlSettings , appSettingsProjectName = projectName + , appSettingsBuildPrefix = "build" + if release then "release" else "debug" , appSettingsFlags = if release then [ "-Wall" -- cgit v1.2.3