diff options
author | Brad Richardson <everythingfunctional@protonmail.com> | 2020-04-11 12:58:58 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-11 12:58:58 -0500 |
commit | ae3ab0973aab3d443c937bf4edab25933e0df931 (patch) | |
tree | 88cf8bb8e6cb657b839f29ad2388314c92a5acb4 /app | |
parent | d0f51b8cb936e550110e44b0dcde4ac4c0f3a282 (diff) | |
parent | 8571ae8c1ba5c0b082492a4e3b3043a580029494 (diff) | |
download | fpm-ae3ab0973aab3d443c937bf4edab25933e0df931.tar.gz fpm-ae3ab0973aab3d443c937bf4edab25933e0df931.zip |
Merge pull request #51 from everythingfunctional/TwoStageSettings
Two stage settings
Diffstat (limited to 'app')
-rw-r--r-- | app/Main.hs | 173 |
1 files changed, 119 insertions, 54 deletions
diff --git a/app/Main.hs b/app/Main.hs index a6f3ea2..9e3f264 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -19,24 +19,33 @@ import Options.Applicative ( Parser , command , execParser , fullDesc - , info , header + , help , helper + , info + , long , progDesc , subparser + , switch ) +import System.Directory ( doesDirectoryExist ) import Toml ( TomlCodec , (.=) ) import qualified Toml -newtype Arguments = Arguments { command' :: Command } +data Arguments = Arguments { command' :: Command, release :: Bool } + +data TomlSettings = TomlSettings { + tomlSettingsCompiler :: !Text + , tomlSettingsProjectName :: !Text + , tomlSettingsLibrary :: !(Maybe Library) } -data Settings = Settings { - settingsCompiler :: !Text - , settingsProjectName :: !Text - , settingsDebugOptions :: ![Text] - , settingsLibrary :: !Library } +data AppSettings = AppSettings { + appSettingsCompiler :: !Text + , appSettingsProjectName :: !Text + , appSettingsFlags :: ![Text] + , appSettingsLibrary :: !(Maybe Library) } data Library = Library { librarySourceDir :: !Text } @@ -46,41 +55,52 @@ 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' (release args) + 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 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 $ appSettingsFlags 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 @@ -92,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 @@ -113,17 +137,58 @@ 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 - <*> Toml.arrayOf Toml._Text "debug-options" - .= settingsDebugOptions - <*> Toml.table libraryCodec "library" - .= settingsLibrary + .= tomlSettingsProjectName + <*> Toml.dioptional (Toml.table libraryCodec "library") + .= tomlSettingsLibrary libraryCodec :: TomlCodec Library libraryCodec = Library <$> Toml.text "source-dir" .= librarySourceDir + +toml2AppSettings :: TomlSettings -> Bool -> IO AppSettings +toml2AppSettings tomlSettings release = do + librarySettings <- getLibrarySettings $ tomlSettingsLibrary tomlSettings + return AppSettings + { appSettingsCompiler = tomlSettingsCompiler tomlSettings + , appSettingsProjectName = tomlSettingsProjectName tomlSettings + , 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 + } + +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 |