qtah-generator-0.8.0: src/Graphics/UI/Qtah/Generator/Enum.hs
-- This file is part of Qtah.
--
-- Copyright 2015-2021 The Qtah Authors.
--
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Lesser General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU Lesser General Public License for more details.
--
-- You should have received a copy of the GNU Lesser General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
module Graphics.UI.Qtah.Generator.Enum (installEnumCalculator) where
import qualified Data.ByteString.Lazy as B
import Data.Maybe (isJust)
import Foreign.Hoppy.Generator.Hook (
EnumEvaluator,
enumEvaluatorArgsKeepOutputsOnFailure,
hookEvaluateEnums,
interpretOutputToEvaluateEnums,
makeCppSourceToEvaluateEnums,
)
import Foreign.Hoppy.Generator.Spec (Interface, interfaceModifyHooks)
import Foreign.Hoppy.Generator.Util (withTempDirectory)
import Graphics.UI.Qtah.Generator.Config (
qmakeArguments,
qmakeExecutable,
)
import System.Directory (withCurrentDirectory)
import System.FilePath ((</>))
import System.IO (hPutStrLn, stderr)
import System.Process (callProcess, readProcess)
installEnumCalculator :: Interface -> Interface
installEnumCalculator = interfaceModifyHooks $ \hooks ->
hooks { hookEvaluateEnums = calculateEnumValues }
calculateEnumValues :: EnumEvaluator
calculateEnumValues args =
withTempDirectory "qtahenum" removeBuildFailures $ \dir ->
withCurrentDirectory dir $ do
let sourceFile = "qtahenum.cpp"
qmakeFile = "qtahenum.pro"
B.writeFile sourceFile $ makeCppSourceToEvaluateEnums args
writeFile qmakeFile $
unlines
[ "# This file is generated by qtah-generator."
, ""
, "QT += core gui"
, "greaterThan(QT_MAJOR_VERSION, 4): QT += widgets"
-- Prevent separate debug/ and release/ build directories caused by this
-- flag being enabled by default on Windows (see issue #50).
, "CONFIG -= debug_and_release"
, "CONFIG += cmdline"
, "TARGET = qtahenum"
, "SOURCES += " ++ sourceFile
]
callProcess qmakeExecutable $ qmakeArguments ++ [qmakeFile]
-- TODO This should read from a QTAH_MAKE environment variable:
callProcess "make" []
out <- readProcess ("." </> "qtahenum") [] ""
result <- case interpretOutputToEvaluateEnums args out of
Left err -> do hPutStrLn stderr err
return Nothing
Right values -> return $ Just values
let remove = False && (isJust result || removeBuildFailures)
return (remove, result)
where removeBuildFailures = not $ enumEvaluatorArgsKeepOutputsOnFailure args