module Distribution.Simple.CCompiler (
CDialect(..),
cSourceExtensions,
cDialectFilenameExtension,
filenameCDialect
) where
import Data.Monoid
( Monoid(..) )
import System.FilePath
( takeExtension )
data CDialect = C
| ObjectiveC
| CPlusPlus
| ObjectiveCPlusPlus
deriving (Show)
instance Monoid CDialect where
mempty = C
mappend C anything = anything
mappend ObjectiveC CPlusPlus = ObjectiveCPlusPlus
mappend CPlusPlus ObjectiveC = ObjectiveCPlusPlus
mappend _ ObjectiveCPlusPlus = ObjectiveCPlusPlus
mappend ObjectiveC _ = ObjectiveC
mappend CPlusPlus _ = CPlusPlus
mappend ObjectiveCPlusPlus _ = ObjectiveCPlusPlus
cSourceExtensions :: [String]
cSourceExtensions = ["c", "i", "ii", "m", "mi", "mm", "M", "mii", "cc", "cp",
"cxx", "cpp", "CPP", "c++", "C"]
cDialectFilenameExtension :: CDialect -> Bool -> String
cDialectFilenameExtension C True = "c"
cDialectFilenameExtension C False = "i"
cDialectFilenameExtension ObjectiveC True = "m"
cDialectFilenameExtension ObjectiveC False = "mi"
cDialectFilenameExtension CPlusPlus True = "cpp"
cDialectFilenameExtension CPlusPlus False = "ii"
cDialectFilenameExtension ObjectiveCPlusPlus True = "mm"
cDialectFilenameExtension ObjectiveCPlusPlus False = "mii"
filenameCDialect :: String -> Maybe (CDialect, Bool)
filenameCDialect filename = do
extension <- case takeExtension filename of
'.':ext -> Just ext
_ -> Nothing
case extension of
"c" -> return (C, True)
"i" -> return (C, False)
"ii" -> return (CPlusPlus, False)
"m" -> return (ObjectiveC, True)
"mi" -> return (ObjectiveC, False)
"mm" -> return (ObjectiveCPlusPlus, True)
"M" -> return (ObjectiveCPlusPlus, True)
"mii" -> return (ObjectiveCPlusPlus, False)
"cc" -> return (CPlusPlus, True)
"cp" -> return (CPlusPlus, True)
"cxx" -> return (CPlusPlus, True)
"cpp" -> return (CPlusPlus, True)
"CPP" -> return (CPlusPlus, True)
"c++" -> return (CPlusPlus, True)
"C" -> return (CPlusPlus, True)
_ -> Nothing