Support GHC 9.12
[cabal.git] / Cabal / src / Distribution / Simple / CCompiler.hs
blob1b2c63ceb69e89c825954c361763b1b87c03a5eb
1 -----------------------------------------------------------------------------
3 {-
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
16 * Neither the name of Isaac Jones nor the names of other
17 contributors may be used to endorse or promote products derived
18 from this software without specific prior written permission.
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -}
32 -- |
33 -- Module : Distribution.Simple.CCompiler
34 -- Copyright : 2011, Dan Knapp
36 -- Maintainer : cabal-devel@haskell.org
37 -- Portability : portable
39 -- This simple package provides types and functions for interacting with
40 -- C compilers. Currently it's just a type enumerating extant C-like
41 -- languages, which we call dialects.
42 module Distribution.Simple.CCompiler
43 ( CDialect (..)
44 , cSourceExtensions
45 , cDialectFilenameExtension
46 , filenameCDialect
47 ) where
49 import Distribution.Compat.Prelude
50 import Prelude ()
52 import System.FilePath
53 ( takeExtension
56 -- | Represents a dialect of C. The Monoid instance expresses backward
57 -- compatibility, in the sense that 'mappend a b' is the least inclusive
58 -- dialect which both 'a' and 'b' can be correctly interpreted as.
59 data CDialect
60 = C
61 | ObjectiveC
62 | CPlusPlus
63 | ObjectiveCPlusPlus
64 deriving (Eq, Show)
66 instance Monoid CDialect where
67 mempty = C
68 mappend = (<>)
70 instance Semigroup CDialect where
71 C <> anything = anything
72 ObjectiveC <> CPlusPlus = ObjectiveCPlusPlus
73 CPlusPlus <> ObjectiveC = ObjectiveCPlusPlus
74 _ <> ObjectiveCPlusPlus = ObjectiveCPlusPlus
75 ObjectiveC <> _ = ObjectiveC
76 CPlusPlus <> _ = CPlusPlus
77 ObjectiveCPlusPlus <> _ = ObjectiveCPlusPlus
79 -- | A list of all file extensions which are recognized as possibly containing
80 -- some dialect of C code. Note that this list is only for source files,
81 -- not for header files.
82 cSourceExtensions :: [String]
83 cSourceExtensions =
84 [ "c"
85 , "i"
86 , "ii"
87 , "m"
88 , "mi"
89 , "mm"
90 , "M"
91 , "mii"
92 , "cc"
93 , "cp"
94 , "cxx"
95 , "cpp"
96 , "CPP"
97 , "c++"
98 , "C"
101 -- | Takes a dialect of C and whether code is intended to be passed through
102 -- the preprocessor, and returns a filename extension for containing that
103 -- code.
104 cDialectFilenameExtension :: CDialect -> Bool -> String
105 cDialectFilenameExtension C True = "c"
106 cDialectFilenameExtension C False = "i"
107 cDialectFilenameExtension ObjectiveC True = "m"
108 cDialectFilenameExtension ObjectiveC False = "mi"
109 cDialectFilenameExtension CPlusPlus True = "cpp"
110 cDialectFilenameExtension CPlusPlus False = "ii"
111 cDialectFilenameExtension ObjectiveCPlusPlus True = "mm"
112 cDialectFilenameExtension ObjectiveCPlusPlus False = "mii"
114 -- | Infers from a filename's extension the dialect of C which it contains,
115 -- and whether it is intended to be passed through the preprocessor.
116 filenameCDialect :: String -> Maybe (CDialect, Bool)
117 filenameCDialect filename = do
118 extension <- case takeExtension filename of
119 '.' : ext -> Just ext
120 _ -> Nothing
121 case extension of
122 "c" -> return (C, True)
123 "i" -> return (C, False)
124 "ii" -> return (CPlusPlus, False)
125 "m" -> return (ObjectiveC, True)
126 "mi" -> return (ObjectiveC, False)
127 "mm" -> return (ObjectiveCPlusPlus, True)
128 "M" -> return (ObjectiveCPlusPlus, True)
129 "mii" -> return (ObjectiveCPlusPlus, False)
130 "cc" -> return (CPlusPlus, True)
131 "cp" -> return (CPlusPlus, True)
132 "cxx" -> return (CPlusPlus, True)
133 "cpp" -> return (CPlusPlus, True)
134 "CPP" -> return (CPlusPlus, True)
135 "c++" -> return (CPlusPlus, True)
136 "C" -> return (CPlusPlus, True)
137 _ -> Nothing