From 93b1df695ed9d38294e873dacb3721cf19e94086 Mon Sep 17 00:00:00 2001 From: Rob Dockins Date: Tue, 21 Sep 2021 14:12:22 -0700 Subject: [PATCH] Add a `cSettingOutputDirectory` setting for codegen. If set, this option defines an output destination directory for generated files. It will be created if necessary. Fixes #255 --- .../src/Copilot/Compile/C99/Compile/Internal.hs | 12 ++++++++++-- .../src/Copilot/Compile/C99/Settings/Internal.hs | 3 ++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/copilot-c99/src/Copilot/Compile/C99/Compile/Internal.hs b/copilot-c99/src/Copilot/Compile/C99/Compile/Internal.hs index 8a07d4dd..b153cdfb 100644 --- a/copilot-c99/src/Copilot/Compile/C99/Compile/Internal.hs +++ b/copilot-c99/src/Copilot/Compile/C99/Compile/Internal.hs @@ -7,6 +7,8 @@ module Copilot.Compile.C99.Compile.Internal import Text.PrettyPrint (render) import Data.List (nub) import Data.Maybe (catMaybes) +import System.Directory (createDirectoryIfMissing) +import System.FilePath (()) import Language.C99.Pretty (pretty) import qualified Language.C99.Simple as C @@ -41,8 +43,14 @@ compileWith cSettings prefix spec = do , "" ] - writeFile (prefix ++ ".c") $ cmacros ++ cfile - writeFile (prefix ++ ".h") hfile + case cSettingsOutputDirectory cSettings of + Nothing -> + do writeFile (prefix ++ ".c") $ cmacros ++ cfile + writeFile (prefix ++ ".h") hfile + Just dir -> + do createDirectoryIfMissing True dir + writeFile (dir prefix ++ ".c") $ cmacros ++ cfile + writeFile (dir prefix ++ ".h") hfile -- | Compile a specification to a .h and a .c file. -- diff --git a/copilot-c99/src/Copilot/Compile/C99/Settings/Internal.hs b/copilot-c99/src/Copilot/Compile/C99/Settings/Internal.hs index 90791a19..cf4b08b9 100644 --- a/copilot-c99/src/Copilot/Compile/C99/Settings/Internal.hs +++ b/copilot-c99/src/Copilot/Compile/C99/Settings/Internal.hs @@ -4,8 +4,9 @@ module Copilot.Compile.C99.Settings.Internal where -- | Settings used to customize the code generated. data CSettings = CSettings { cSettingsStepFunctionName :: String + , cSettingsOutputDirectory :: Maybe FilePath } -- | Default settings with a step function called @step@. mkDefaultCSettings :: CSettings -mkDefaultCSettings = CSettings "step" +mkDefaultCSettings = CSettings "step" Nothing