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